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.
29 static void extent_trunc(struct inode *inode, struct extent_position *epos,
30 struct kernel_lb_addr *eloc, int8_t etype, uint32_t elen,
33 struct kernel_lb_addr neloc = {};
34 int last_block = (elen + inode->i_sb->s_blocksize - 1) >>
35 inode->i_sb->s_blocksize_bits;
36 int first_block = (nelen + inode->i_sb->s_blocksize - 1) >>
37 inode->i_sb->s_blocksize_bits;
40 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
41 udf_free_blocks(inode->i_sb, inode, eloc, 0,
43 etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
46 nelen = (etype << 30) | nelen;
50 udf_write_aext(inode, epos, &neloc, nelen, 0);
51 if (last_block - first_block > 0) {
52 if (etype == (EXT_RECORDED_ALLOCATED >> 30))
53 mark_inode_dirty(inode);
55 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
56 udf_free_blocks(inode->i_sb, inode, eloc,
58 last_block - first_block);
64 * Truncate the last extent to match i_size. This function assumes
65 * that preallocation extent is already truncated.
67 void udf_truncate_tail_extent(struct inode *inode)
69 struct extent_position epos = {};
70 struct kernel_lb_addr eloc;
73 int8_t etype = -1, netype;
75 struct udf_inode_info *iinfo = UDF_I(inode);
77 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
78 inode->i_size == iinfo->i_lenExtents)
80 /* Are we going to delete the file anyway? */
81 if (inode->i_nlink == 0)
84 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
85 adsize = sizeof(struct short_ad);
86 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
87 adsize = sizeof(struct long_ad);
91 /* Find the last extent in the file */
92 while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
95 if (lbcount > inode->i_size) {
96 if (lbcount - inode->i_size >= inode->i_sb->s_blocksize)
98 "Too long extent after EOF in inode %u: i_size: %lld lbcount: %lld extent %u+%u\n",
99 (unsigned)inode->i_ino,
100 (long long)inode->i_size,
102 (unsigned)eloc.logicalBlockNum,
104 nelen = elen - (lbcount - inode->i_size);
105 epos.offset -= adsize;
106 extent_trunc(inode, &epos, &eloc, etype, elen, nelen);
107 epos.offset += adsize;
108 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) != -1)
110 "Extent after EOF in inode %u\n",
111 (unsigned)inode->i_ino);
115 /* This inode entry is in-memory only and thus we don't have to mark
117 iinfo->i_lenExtents = inode->i_size;
121 void udf_discard_prealloc(struct inode *inode)
123 struct extent_position epos = { NULL, 0, {0, 0} };
124 struct kernel_lb_addr eloc;
126 uint64_t lbcount = 0;
127 int8_t etype = -1, netype;
129 struct udf_inode_info *iinfo = UDF_I(inode);
131 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
132 inode->i_size == iinfo->i_lenExtents)
135 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
136 adsize = sizeof(struct short_ad);
137 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
138 adsize = sizeof(struct long_ad);
142 epos.block = iinfo->i_location;
144 /* Find the last extent in the file */
145 while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
149 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
150 epos.offset -= adsize;
152 extent_trunc(inode, &epos, &eloc, etype, elen, 0);
156 udf_file_entry_alloc_offset(inode);
157 mark_inode_dirty(inode);
159 struct allocExtDesc *aed =
160 (struct allocExtDesc *)(epos.bh->b_data);
161 aed->lengthAllocDescs =
162 cpu_to_le32(epos.offset -
163 sizeof(struct allocExtDesc));
164 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
165 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
166 udf_update_tag(epos.bh->b_data, epos.offset);
168 udf_update_tag(epos.bh->b_data,
169 sizeof(struct allocExtDesc));
170 mark_buffer_dirty_inode(epos.bh, inode);
173 /* This inode entry is in-memory only and thus we don't have to mark
175 iinfo->i_lenExtents = lbcount;
179 static void udf_update_alloc_ext_desc(struct inode *inode,
180 struct extent_position *epos,
183 struct super_block *sb = inode->i_sb;
184 struct udf_sb_info *sbi = UDF_SB(sb);
186 struct allocExtDesc *aed = (struct allocExtDesc *) (epos->bh->b_data);
187 int len = sizeof(struct allocExtDesc);
189 aed->lengthAllocDescs = cpu_to_le32(lenalloc);
190 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || sbi->s_udfrev >= 0x0201)
193 udf_update_tag(epos->bh->b_data, len);
194 mark_buffer_dirty_inode(epos->bh, inode);
198 * Truncate extents of inode to inode->i_size. This function can be used only
199 * for making file shorter. For making file longer, udf_extend_file() has to
202 void udf_truncate_extents(struct inode *inode)
204 struct extent_position epos;
205 struct kernel_lb_addr eloc, neloc = {};
206 uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
208 struct super_block *sb = inode->i_sb;
209 sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
212 struct udf_inode_info *iinfo = UDF_I(inode);
214 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
215 adsize = sizeof(struct short_ad);
216 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
217 adsize = sizeof(struct long_ad);
221 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
222 byte_offset = (offset << sb->s_blocksize_bits) +
223 (inode->i_size & (sb->s_blocksize - 1));
225 /* We should extend the file? */
226 WARN_ON(byte_offset);
229 epos.offset -= adsize;
230 extent_trunc(inode, &epos, &eloc, etype, elen, byte_offset);
231 epos.offset += adsize;
233 lenalloc = epos.offset;
235 lenalloc = epos.offset - adsize;
238 lenalloc -= udf_file_entry_alloc_offset(inode);
240 lenalloc -= sizeof(struct allocExtDesc);
242 while ((etype = udf_current_aext(inode, &epos, &eloc,
244 if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
245 udf_write_aext(inode, &epos, &neloc, nelen, 0);
246 if (indirect_ext_len) {
247 /* We managed to free all extents in the
248 * indirect extent - free it too */
250 udf_free_blocks(sb, NULL, &epos.block,
251 0, indirect_ext_len);
252 } else if (!epos.bh) {
253 iinfo->i_lenAlloc = lenalloc;
254 mark_inode_dirty(inode);
256 udf_update_alloc_ext_desc(inode,
259 epos.offset = sizeof(struct allocExtDesc);
261 epos.bh = udf_tread(sb,
262 udf_get_lb_pblock(sb, &eloc, 0));
265 (elen + sb->s_blocksize - 1) >>
266 sb->s_blocksize_bits;
268 indirect_ext_len = 1;
270 extent_trunc(inode, &epos, &eloc, etype, elen, 0);
271 epos.offset += adsize;
275 if (indirect_ext_len) {
277 udf_free_blocks(sb, NULL, &epos.block, 0, indirect_ext_len);
278 } else if (!epos.bh) {
279 iinfo->i_lenAlloc = lenalloc;
280 mark_inode_dirty(inode);
282 udf_update_alloc_ext_desc(inode, &epos, lenalloc);
283 iinfo->i_lenExtents = inode->i_size;