xref: /linux/fs/udf/truncate.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * truncate.c
4  *
5  * PURPOSE
6  *	Truncate handling routines for the OSTA-UDF(tm) filesystem.
7  *
8  * COPYRIGHT
9  *  (C) 1999-2004 Ben Fennema
10  *  (C) 1999 Stelias Computing Inc
11  *
12  * HISTORY
13  *
14  *  02/24/99 blf  Created.
15  *
16  */
17 
18 #include "udfdecl.h"
19 #include <linux/fs.h>
20 #include <linux/mm.h>
21 
22 #include "udf_i.h"
23 #include "udf_sb.h"
24 
25 static void extent_trunc(struct inode *inode, struct extent_position *epos,
26 			 struct kernel_lb_addr *eloc, int8_t etype, uint32_t elen,
27 			 uint32_t nelen)
28 {
29 	struct kernel_lb_addr neloc = {};
30 	int last_block = (elen + inode->i_sb->s_blocksize - 1) >>
31 		inode->i_sb->s_blocksize_bits;
32 	int first_block = (nelen + inode->i_sb->s_blocksize - 1) >>
33 		inode->i_sb->s_blocksize_bits;
34 
35 	if (nelen) {
36 		if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
37 			udf_free_blocks(inode->i_sb, inode, eloc, 0,
38 					last_block);
39 			etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
40 		} else
41 			neloc = *eloc;
42 		nelen = (etype << 30) | nelen;
43 	}
44 
45 	if (elen != nelen) {
46 		udf_write_aext(inode, epos, &neloc, nelen, 0);
47 		if (last_block > first_block) {
48 			if (etype == (EXT_RECORDED_ALLOCATED >> 30))
49 				mark_inode_dirty(inode);
50 
51 			if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
52 				udf_free_blocks(inode->i_sb, inode, eloc,
53 						first_block,
54 						last_block - first_block);
55 		}
56 	}
57 }
58 
59 /*
60  * Truncate the last extent to match i_size. This function assumes
61  * that preallocation extent is already truncated.
62  */
63 void udf_truncate_tail_extent(struct inode *inode)
64 {
65 	struct extent_position epos = {};
66 	struct kernel_lb_addr eloc;
67 	uint32_t elen, nelen;
68 	uint64_t lbcount = 0;
69 	int8_t etype = -1, netype;
70 	int adsize;
71 	struct udf_inode_info *iinfo = UDF_I(inode);
72 	int ret;
73 
74 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
75 	    inode->i_size == iinfo->i_lenExtents)
76 		return;
77 	/* Are we going to delete the file anyway? */
78 	if (inode->i_nlink == 0)
79 		return;
80 
81 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
82 		adsize = sizeof(struct short_ad);
83 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
84 		adsize = sizeof(struct long_ad);
85 	else
86 		BUG();
87 
88 	/* Find the last extent in the file */
89 	while (1) {
90 		ret = udf_next_aext(inode, &epos, &eloc, &elen, &netype, 1);
91 		if (ret <= 0)
92 			break;
93 		etype = netype;
94 		lbcount += elen;
95 		if (lbcount > inode->i_size) {
96 			if (lbcount - inode->i_size >= inode->i_sb->s_blocksize)
97 				udf_warn(inode->i_sb,
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,
101 					 (long long)lbcount,
102 					 (unsigned)eloc.logicalBlockNum,
103 					 (unsigned)elen);
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,
109 					  &netype, 1) > 0)
110 				udf_err(inode->i_sb,
111 					"Extent after EOF in inode %u\n",
112 					(unsigned)inode->i_ino);
113 			break;
114 		}
115 	}
116 	/* This inode entry is in-memory only and thus we don't have to mark
117 	 * the inode dirty */
118 	if (ret >= 0)
119 		iinfo->i_lenExtents = inode->i_size;
120 	brelse(epos.bh);
121 }
122 
123 void udf_discard_prealloc(struct inode *inode)
124 {
125 	struct extent_position epos = {};
126 	struct extent_position prev_epos = {};
127 	struct kernel_lb_addr eloc, tmpeloc;
128 	uint32_t elen, tmpelen;
129 	uint64_t lbcount = 0;
130 	int8_t etype = -1;
131 	struct udf_inode_info *iinfo = UDF_I(inode);
132 	int bsize = i_blocksize(inode);
133 	int8_t tmpetype = -1;
134 	int ret;
135 
136 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
137 	    ALIGN(inode->i_size, bsize) == ALIGN(iinfo->i_lenExtents, bsize))
138 		return;
139 
140 	epos.block = iinfo->i_location;
141 
142 	/* Find the last extent in the file */
143 	while (1) {
144 		ret = udf_next_aext(inode, &epos, &tmpeloc, &tmpelen,
145 				    &tmpetype, 0);
146 		if (ret < 0)
147 			goto out;
148 		if (ret == 0)
149 			break;
150 		brelse(prev_epos.bh);
151 		prev_epos = epos;
152 		if (prev_epos.bh)
153 			get_bh(prev_epos.bh);
154 
155 		ret = udf_next_aext(inode, &epos, &eloc, &elen, &etype, 1);
156 		if (ret < 0)
157 			goto out;
158 		lbcount += elen;
159 	}
160 
161 	if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
162 		lbcount -= elen;
163 		udf_delete_aext(inode, prev_epos, NULL);
164 		udf_free_blocks(inode->i_sb, inode, &eloc, 0,
165 				DIV_ROUND_UP(elen, bsize));
166 	}
167 	/* This inode entry is in-memory only and thus we don't have to mark
168 	 * the inode dirty */
169 	iinfo->i_lenExtents = lbcount;
170 out:
171 	brelse(epos.bh);
172 	brelse(prev_epos.bh);
173 }
174 
175 static void udf_update_alloc_ext_desc(struct inode *inode,
176 				      struct extent_position *epos,
177 				      u32 lenalloc)
178 {
179 	struct super_block *sb = inode->i_sb;
180 	struct udf_sb_info *sbi = UDF_SB(sb);
181 
182 	struct allocExtDesc *aed = (struct allocExtDesc *) (epos->bh->b_data);
183 	int len = sizeof(struct allocExtDesc);
184 
185 	aed->lengthAllocDescs =	cpu_to_le32(lenalloc);
186 	if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || sbi->s_udfrev >= 0x0201)
187 		len += lenalloc;
188 
189 	udf_update_tag(epos->bh->b_data, len);
190 	mmb_mark_buffer_dirty(epos->bh, &UDF_I(inode)->i_metadata_bhs);
191 }
192 
193 /*
194  * Truncate extents of inode to inode->i_size. This function can be used only
195  * for making file shorter. For making file longer, udf_extend_file() has to
196  * be used.
197  */
198 int udf_truncate_extents(struct inode *inode)
199 {
200 	struct extent_position epos;
201 	struct kernel_lb_addr eloc, neloc = {};
202 	uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
203 	int8_t etype;
204 	struct super_block *sb = inode->i_sb;
205 	sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
206 	loff_t byte_offset;
207 	int adsize;
208 	struct udf_inode_info *iinfo = UDF_I(inode);
209 	int ret = 0;
210 
211 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
212 		adsize = sizeof(struct short_ad);
213 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
214 		adsize = sizeof(struct long_ad);
215 	else
216 		BUG();
217 
218 	ret = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset, &etype);
219 	if (ret < 0)
220 		return ret;
221 	byte_offset = (offset << sb->s_blocksize_bits) +
222 		(inode->i_size & (sb->s_blocksize - 1));
223 	if (ret == 0) {
224 		/* We should extend the file? */
225 		WARN_ON(byte_offset);
226 		return 0;
227 	}
228 	epos.offset -= adsize;
229 	extent_trunc(inode, &epos, &eloc, etype, elen, byte_offset);
230 	epos.offset += adsize;
231 	if (byte_offset)
232 		lenalloc = epos.offset;
233 	else
234 		lenalloc = epos.offset - adsize;
235 
236 	if (!epos.bh)
237 		lenalloc -= udf_file_entry_alloc_offset(inode);
238 	else
239 		lenalloc -= sizeof(struct allocExtDesc);
240 
241 	while ((ret = udf_current_aext(inode, &epos, &eloc,
242 				       &elen, &etype, 0)) > 0) {
243 		if (etype == (EXT_NEXT_EXTENT_ALLOCDESCS >> 30)) {
244 			udf_write_aext(inode, &epos, &neloc, nelen, 0);
245 			if (indirect_ext_len) {
246 				/* We managed to free all extents in the
247 				 * indirect extent - free it too */
248 				BUG_ON(!epos.bh);
249 				udf_free_blocks(sb, NULL, &epos.block,
250 						0, indirect_ext_len);
251 			} else if (!epos.bh) {
252 				iinfo->i_lenAlloc = lenalloc;
253 				mark_inode_dirty(inode);
254 			} else
255 				udf_update_alloc_ext_desc(inode,
256 						&epos, lenalloc);
257 			brelse(epos.bh);
258 			epos.offset = sizeof(struct allocExtDesc);
259 			epos.block = eloc;
260 			epos.bh = sb_bread(sb,
261 					udf_get_lb_pblock(sb, &eloc, 0));
262 			/* Error reading indirect block? */
263 			if (!epos.bh)
264 				return -EIO;
265 			if (elen)
266 				indirect_ext_len =
267 					(elen + sb->s_blocksize - 1) >>
268 					sb->s_blocksize_bits;
269 			else
270 				indirect_ext_len = 1;
271 		} else {
272 			extent_trunc(inode, &epos, &eloc, etype, elen, 0);
273 			epos.offset += adsize;
274 		}
275 	}
276 
277 	if (ret < 0) {
278 		brelse(epos.bh);
279 		return ret;
280 	}
281 
282 	if (indirect_ext_len) {
283 		BUG_ON(!epos.bh);
284 		udf_free_blocks(sb, NULL, &epos.block, 0, indirect_ext_len);
285 	} else if (!epos.bh) {
286 		iinfo->i_lenAlloc = lenalloc;
287 		mark_inode_dirty(inode);
288 	} else
289 		udf_update_alloc_ext_desc(inode, &epos, lenalloc);
290 	iinfo->i_lenExtents = inode->i_size;
291 
292 	brelse(epos.bh);
293 	return 0;
294 }
295