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