5 * Truncate handling routines for the OSTA-UDF(tm) filesystem.
8 * This file is distributed under the terms of the GNU General Public
9 * License (GPL). Copies of the GPL can be obtained from:
10 * ftp://prep.ai.mit.edu/pub/gnu/GPL
11 * Each contributing author retains all rights to their own work.
13 * (C) 1999-2004 Ben Fennema
14 * (C) 1999 Stelias Computing Inc
18 * 02/24/99 blf Created.
25 #include <linux/buffer_head.h>
30 static void extent_trunc(struct inode *inode, struct extent_position *epos,
31 struct kernel_lb_addr *eloc, int8_t etype, uint32_t elen,
34 struct kernel_lb_addr neloc = {};
35 int last_block = (elen + inode->i_sb->s_blocksize - 1) >>
36 inode->i_sb->s_blocksize_bits;
37 int first_block = (nelen + inode->i_sb->s_blocksize - 1) >>
38 inode->i_sb->s_blocksize_bits;
41 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
42 udf_free_blocks(inode->i_sb, inode, eloc, 0,
44 etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
47 nelen = (etype << 30) | nelen;
51 udf_write_aext(inode, epos, &neloc, nelen, 0);
52 if (last_block - first_block > 0) {
53 if (etype == (EXT_RECORDED_ALLOCATED >> 30))
54 mark_inode_dirty(inode);
56 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
57 udf_free_blocks(inode->i_sb, inode, eloc,
59 last_block - first_block);
65 * Truncate the last extent to match i_size. This function assumes
66 * that preallocation extent is already truncated.
68 void udf_truncate_tail_extent(struct inode *inode)
70 struct extent_position epos = {};
71 struct kernel_lb_addr eloc;
74 int8_t etype = -1, netype;
76 struct udf_inode_info *iinfo = UDF_I(inode);
78 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
79 inode->i_size == iinfo->i_lenExtents)
81 /* Are we going to delete the file anyway? */
82 if (inode->i_nlink == 0)
85 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
86 adsize = sizeof(struct short_ad);
87 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
88 adsize = sizeof(struct long_ad);
92 /* Find the last extent in the file */
93 while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
96 if (lbcount > inode->i_size) {
97 if (lbcount - inode->i_size >= inode->i_sb->s_blocksize)
99 "Too long extent after EOF in inode %u: i_size: %lld lbcount: %lld extent %u+%u\n",
100 (unsigned)inode->i_ino,
101 (long long)inode->i_size,
103 (unsigned)eloc.logicalBlockNum,
105 nelen = elen - (lbcount - inode->i_size);
106 epos.offset -= adsize;
107 extent_trunc(inode, &epos, &eloc, etype, elen, nelen);
108 epos.offset += adsize;
109 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) != -1)
111 "Extent after EOF in inode %u\n",
112 (unsigned)inode->i_ino);
116 /* This inode entry is in-memory only and thus we don't have to mark
118 iinfo->i_lenExtents = inode->i_size;
122 void udf_discard_prealloc(struct inode *inode)
124 struct extent_position epos = { NULL, 0, {0, 0} };
125 struct kernel_lb_addr eloc;
127 uint64_t lbcount = 0;
128 int8_t etype = -1, netype;
130 struct udf_inode_info *iinfo = UDF_I(inode);
132 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
133 inode->i_size == iinfo->i_lenExtents)
136 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
137 adsize = sizeof(struct short_ad);
138 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
139 adsize = sizeof(struct long_ad);
143 epos.block = iinfo->i_location;
145 /* Find the last extent in the file */
146 while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
150 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
151 epos.offset -= adsize;
153 extent_trunc(inode, &epos, &eloc, etype, elen, 0);
157 udf_file_entry_alloc_offset(inode);
158 mark_inode_dirty(inode);
160 struct allocExtDesc *aed =
161 (struct allocExtDesc *)(epos.bh->b_data);
162 aed->lengthAllocDescs =
163 cpu_to_le32(epos.offset -
164 sizeof(struct allocExtDesc));
165 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
166 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
167 udf_update_tag(epos.bh->b_data, epos.offset);
169 udf_update_tag(epos.bh->b_data,
170 sizeof(struct allocExtDesc));
171 mark_buffer_dirty_inode(epos.bh, inode);
174 /* This inode entry is in-memory only and thus we don't have to mark
176 iinfo->i_lenExtents = lbcount;
180 static void udf_update_alloc_ext_desc(struct inode *inode,
181 struct extent_position *epos,
184 struct super_block *sb = inode->i_sb;
185 struct udf_sb_info *sbi = UDF_SB(sb);
187 struct allocExtDesc *aed = (struct allocExtDesc *) (epos->bh->b_data);
188 int len = sizeof(struct allocExtDesc);
190 aed->lengthAllocDescs = cpu_to_le32(lenalloc);
191 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || sbi->s_udfrev >= 0x0201)
194 udf_update_tag(epos->bh->b_data, len);
195 mark_buffer_dirty_inode(epos->bh, inode);
199 * Truncate extents of inode to inode->i_size. This function can be used only
200 * for making file shorter. For making file longer, udf_extend_file() has to
203 void udf_truncate_extents(struct inode *inode)
205 struct extent_position epos;
206 struct kernel_lb_addr eloc, neloc = {};
207 uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
209 struct super_block *sb = inode->i_sb;
210 sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
213 struct udf_inode_info *iinfo = UDF_I(inode);
215 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
216 adsize = sizeof(struct short_ad);
217 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
218 adsize = sizeof(struct long_ad);
222 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
223 byte_offset = (offset << sb->s_blocksize_bits) +
224 (inode->i_size & (sb->s_blocksize - 1));
226 /* We should extend the file? */
227 WARN_ON(byte_offset);
230 epos.offset -= adsize;
231 extent_trunc(inode, &epos, &eloc, etype, elen, byte_offset);
232 epos.offset += adsize;
234 lenalloc = epos.offset;
236 lenalloc = epos.offset - adsize;
239 lenalloc -= udf_file_entry_alloc_offset(inode);
241 lenalloc -= sizeof(struct allocExtDesc);
243 while ((etype = udf_current_aext(inode, &epos, &eloc,
245 if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
246 udf_write_aext(inode, &epos, &neloc, nelen, 0);
247 if (indirect_ext_len) {
248 /* We managed to free all extents in the
249 * indirect extent - free it too */
251 udf_free_blocks(sb, NULL, &epos.block,
252 0, indirect_ext_len);
253 } else if (!epos.bh) {
254 iinfo->i_lenAlloc = lenalloc;
255 mark_inode_dirty(inode);
257 udf_update_alloc_ext_desc(inode,
260 epos.offset = sizeof(struct allocExtDesc);
262 epos.bh = udf_tread(sb,
263 udf_get_lb_pblock(sb, &eloc, 0));
266 (elen + sb->s_blocksize - 1) >>
267 sb->s_blocksize_bits;
269 indirect_ext_len = 1;
271 extent_trunc(inode, &epos, &eloc, etype, elen, 0);
272 epos.offset += adsize;
276 if (indirect_ext_len) {
278 udf_free_blocks(sb, NULL, &epos.block, 0, indirect_ext_len);
279 } else if (!epos.bh) {
280 iinfo->i_lenAlloc = lenalloc;
281 mark_inode_dirty(inode);
283 udf_update_alloc_ext_desc(inode, &epos, lenalloc);
284 iinfo->i_lenExtents = inode->i_size;