xref: /openbmc/linux/fs/udf/truncate.c (revision 5ce34554)
1*5ce34554SBagas Sanjaya // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * truncate.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * PURPOSE
61da177e4SLinus Torvalds  *	Truncate handling routines for the OSTA-UDF(tm) filesystem.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * COPYRIGHT
91da177e4SLinus Torvalds  *  (C) 1999-2004 Ben Fennema
101da177e4SLinus Torvalds  *  (C) 1999 Stelias Computing Inc
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * HISTORY
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *  02/24/99 blf  Created.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include "udfdecl.h"
191da177e4SLinus Torvalds #include <linux/fs.h>
201da177e4SLinus Torvalds #include <linux/mm.h>
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #include "udf_i.h"
231da177e4SLinus Torvalds #include "udf_sb.h"
241da177e4SLinus Torvalds 
extent_trunc(struct inode * inode,struct extent_position * epos,struct kernel_lb_addr * eloc,int8_t etype,uint32_t elen,uint32_t nelen)25ff116fc8SJan Kara static void extent_trunc(struct inode *inode, struct extent_position *epos,
2697e961fdSPekka Enberg 			 struct kernel_lb_addr *eloc, int8_t etype, uint32_t elen,
27cb00ea35SCyrill Gorcunov 			 uint32_t nelen)
281da177e4SLinus Torvalds {
295ca4e4beSPekka Enberg 	struct kernel_lb_addr neloc = {};
3028de7948SCyrill Gorcunov 	int last_block = (elen + inode->i_sb->s_blocksize - 1) >>
3128de7948SCyrill Gorcunov 		inode->i_sb->s_blocksize_bits;
3228de7948SCyrill Gorcunov 	int first_block = (nelen + inode->i_sb->s_blocksize - 1) >>
3328de7948SCyrill Gorcunov 		inode->i_sb->s_blocksize_bits;
341da177e4SLinus Torvalds 
35cb00ea35SCyrill Gorcunov 	if (nelen) {
36cb00ea35SCyrill Gorcunov 		if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
37cb00ea35SCyrill Gorcunov 			udf_free_blocks(inode->i_sb, inode, eloc, 0,
38cb00ea35SCyrill Gorcunov 					last_block);
391da177e4SLinus Torvalds 			etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
40cb00ea35SCyrill Gorcunov 		} else
4197e961fdSPekka Enberg 			neloc = *eloc;
421da177e4SLinus Torvalds 		nelen = (etype << 30) | nelen;
431da177e4SLinus Torvalds 	}
441da177e4SLinus Torvalds 
45cb00ea35SCyrill Gorcunov 	if (elen != nelen) {
4697e961fdSPekka Enberg 		udf_write_aext(inode, epos, &neloc, nelen, 0);
47b490bdd6SSteve Magnani 		if (last_block > first_block) {
481da177e4SLinus Torvalds 			if (etype == (EXT_RECORDED_ALLOCATED >> 30))
491da177e4SLinus Torvalds 				mark_inode_dirty(inode);
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds 			if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
52cb00ea35SCyrill Gorcunov 				udf_free_blocks(inode->i_sb, inode, eloc,
53cb00ea35SCyrill Gorcunov 						first_block,
54cb00ea35SCyrill Gorcunov 						last_block - first_block);
551da177e4SLinus Torvalds 		}
561da177e4SLinus Torvalds 	}
571da177e4SLinus Torvalds }
581da177e4SLinus Torvalds 
5974584ae5SJan Kara /*
6074584ae5SJan Kara  * Truncate the last extent to match i_size. This function assumes
6174584ae5SJan Kara  * that preallocation extent is already truncated.
6274584ae5SJan Kara  */
udf_truncate_tail_extent(struct inode * inode)6374584ae5SJan Kara void udf_truncate_tail_extent(struct inode *inode)
641da177e4SLinus Torvalds {
6528de7948SCyrill Gorcunov 	struct extent_position epos = {};
665ca4e4beSPekka Enberg 	struct kernel_lb_addr eloc;
67ff116fc8SJan Kara 	uint32_t elen, nelen;
681da177e4SLinus Torvalds 	uint64_t lbcount = 0;
691da177e4SLinus Torvalds 	int8_t etype = -1, netype;
701da177e4SLinus Torvalds 	int adsize;
7148d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
721da177e4SLinus Torvalds 
7348d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
7448d6d8ffSMarcin Slusarz 	    inode->i_size == iinfo->i_lenExtents)
751da177e4SLinus Torvalds 		return;
7674584ae5SJan Kara 	/* Are we going to delete the file anyway? */
7774584ae5SJan Kara 	if (inode->i_nlink == 0)
7874584ae5SJan Kara 		return;
7974584ae5SJan Kara 
8048d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
815ca4e4beSPekka Enberg 		adsize = sizeof(struct short_ad);
8248d6d8ffSMarcin Slusarz 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
835ca4e4beSPekka Enberg 		adsize = sizeof(struct long_ad);
8474584ae5SJan Kara 	else
8574584ae5SJan Kara 		BUG();
8674584ae5SJan Kara 
8774584ae5SJan Kara 	/* Find the last extent in the file */
88cb00ea35SCyrill Gorcunov 	while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
8974584ae5SJan Kara 		etype = netype;
9074584ae5SJan Kara 		lbcount += elen;
9174584ae5SJan Kara 		if (lbcount > inode->i_size) {
9274584ae5SJan Kara 			if (lbcount - inode->i_size >= inode->i_sb->s_blocksize)
9378ace70cSJoe Perches 				udf_warn(inode->i_sb,
9478ace70cSJoe Perches 					 "Too long extent after EOF in inode %u: i_size: %lld lbcount: %lld extent %u+%u\n",
9574584ae5SJan Kara 					 (unsigned)inode->i_ino,
9674584ae5SJan Kara 					 (long long)inode->i_size,
9774584ae5SJan Kara 					 (long long)lbcount,
9874584ae5SJan Kara 					 (unsigned)eloc.logicalBlockNum,
9974584ae5SJan Kara 					 (unsigned)elen);
10074584ae5SJan Kara 			nelen = elen - (lbcount - inode->i_size);
10174584ae5SJan Kara 			epos.offset -= adsize;
10297e961fdSPekka Enberg 			extent_trunc(inode, &epos, &eloc, etype, elen, nelen);
10374584ae5SJan Kara 			epos.offset += adsize;
10474584ae5SJan Kara 			if (udf_next_aext(inode, &epos, &eloc, &elen, 1) != -1)
10578ace70cSJoe Perches 				udf_err(inode->i_sb,
10678ace70cSJoe Perches 					"Extent after EOF in inode %u\n",
10774584ae5SJan Kara 					(unsigned)inode->i_ino);
10874584ae5SJan Kara 			break;
10974584ae5SJan Kara 		}
11074584ae5SJan Kara 	}
11174584ae5SJan Kara 	/* This inode entry is in-memory only and thus we don't have to mark
11274584ae5SJan Kara 	 * the inode dirty */
11348d6d8ffSMarcin Slusarz 	iinfo->i_lenExtents = inode->i_size;
11474584ae5SJan Kara 	brelse(epos.bh);
11574584ae5SJan Kara }
11674584ae5SJan Kara 
udf_discard_prealloc(struct inode * inode)11774584ae5SJan Kara void udf_discard_prealloc(struct inode *inode)
11874584ae5SJan Kara {
119cfe4c1b2SJan Kara 	struct extent_position epos = {};
120cfe4c1b2SJan Kara 	struct extent_position prev_epos = {};
1215ca4e4beSPekka Enberg 	struct kernel_lb_addr eloc;
12274584ae5SJan Kara 	uint32_t elen;
12374584ae5SJan Kara 	uint64_t lbcount = 0;
1241fb40763SColin Ian King 	int8_t etype = -1;
12548d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
126f768dc3cSYangtao Li 	int bsize = i_blocksize(inode);
12774584ae5SJan Kara 
12848d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
1296ad53f0fSJan Kara 	    ALIGN(inode->i_size, bsize) == ALIGN(iinfo->i_lenExtents, bsize))
13074584ae5SJan Kara 		return;
1311da177e4SLinus Torvalds 
13248d6d8ffSMarcin Slusarz 	epos.block = iinfo->i_location;
1331da177e4SLinus Torvalds 
134ff116fc8SJan Kara 	/* Find the last extent in the file */
1351fb40763SColin Ian King 	while (udf_next_aext(inode, &epos, &eloc, &elen, 0) != -1) {
136cfe4c1b2SJan Kara 		brelse(prev_epos.bh);
137cfe4c1b2SJan Kara 		prev_epos = epos;
138cfe4c1b2SJan Kara 		if (prev_epos.bh)
139cfe4c1b2SJan Kara 			get_bh(prev_epos.bh);
140cfe4c1b2SJan Kara 
141cfe4c1b2SJan Kara 		etype = udf_next_aext(inode, &epos, &eloc, &elen, 1);
1421da177e4SLinus Torvalds 		lbcount += elen;
1431da177e4SLinus Torvalds 	}
144ff116fc8SJan Kara 	if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1451da177e4SLinus Torvalds 		lbcount -= elen;
146cfe4c1b2SJan Kara 		udf_delete_aext(inode, prev_epos);
147cfe4c1b2SJan Kara 		udf_free_blocks(inode->i_sb, inode, &eloc, 0,
148f768dc3cSYangtao Li 				DIV_ROUND_UP(elen, bsize));
1491da177e4SLinus Torvalds 	}
15074584ae5SJan Kara 	/* This inode entry is in-memory only and thus we don't have to mark
15174584ae5SJan Kara 	 * the inode dirty */
15248d6d8ffSMarcin Slusarz 	iinfo->i_lenExtents = lbcount;
1533bf25cb4SJan Kara 	brelse(epos.bh);
154cfe4c1b2SJan Kara 	brelse(prev_epos.bh);
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds 
udf_update_alloc_ext_desc(struct inode * inode,struct extent_position * epos,u32 lenalloc)157456390deSmarcin.slusarz@gmail.com static void udf_update_alloc_ext_desc(struct inode *inode,
158456390deSmarcin.slusarz@gmail.com 				      struct extent_position *epos,
159456390deSmarcin.slusarz@gmail.com 				      u32 lenalloc)
160456390deSmarcin.slusarz@gmail.com {
161456390deSmarcin.slusarz@gmail.com 	struct super_block *sb = inode->i_sb;
162456390deSmarcin.slusarz@gmail.com 	struct udf_sb_info *sbi = UDF_SB(sb);
163456390deSmarcin.slusarz@gmail.com 
164456390deSmarcin.slusarz@gmail.com 	struct allocExtDesc *aed = (struct allocExtDesc *) (epos->bh->b_data);
165456390deSmarcin.slusarz@gmail.com 	int len = sizeof(struct allocExtDesc);
166456390deSmarcin.slusarz@gmail.com 
167456390deSmarcin.slusarz@gmail.com 	aed->lengthAllocDescs =	cpu_to_le32(lenalloc);
168456390deSmarcin.slusarz@gmail.com 	if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || sbi->s_udfrev >= 0x0201)
169456390deSmarcin.slusarz@gmail.com 		len += lenalloc;
170456390deSmarcin.slusarz@gmail.com 
171456390deSmarcin.slusarz@gmail.com 	udf_update_tag(epos->bh->b_data, len);
172456390deSmarcin.slusarz@gmail.com 	mark_buffer_dirty_inode(epos->bh, inode);
173456390deSmarcin.slusarz@gmail.com }
174456390deSmarcin.slusarz@gmail.com 
1757e49b6f2SJan Kara /*
1767e49b6f2SJan Kara  * Truncate extents of inode to inode->i_size. This function can be used only
1777e49b6f2SJan Kara  * for making file shorter. For making file longer, udf_extend_file() has to
1787e49b6f2SJan Kara  * be used.
1797e49b6f2SJan Kara  */
udf_truncate_extents(struct inode * inode)1802b42be5eSJan Kara int udf_truncate_extents(struct inode *inode)
1811da177e4SLinus Torvalds {
182ff116fc8SJan Kara 	struct extent_position epos;
1835ca4e4beSPekka Enberg 	struct kernel_lb_addr eloc, neloc = {};
184ff116fc8SJan Kara 	uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
1851da177e4SLinus Torvalds 	int8_t etype;
18631170b6aSJan Kara 	struct super_block *sb = inode->i_sb;
18731170b6aSJan Kara 	sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
18860448b1dSJan Kara 	loff_t byte_offset;
1891da177e4SLinus Torvalds 	int adsize;
19048d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
1911da177e4SLinus Torvalds 
19248d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1935ca4e4beSPekka Enberg 		adsize = sizeof(struct short_ad);
19448d6d8ffSMarcin Slusarz 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1955ca4e4beSPekka Enberg 		adsize = sizeof(struct long_ad);
1961da177e4SLinus Torvalds 	else
197ff116fc8SJan Kara 		BUG();
1981da177e4SLinus Torvalds 
199ff116fc8SJan Kara 	etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
20028de7948SCyrill Gorcunov 	byte_offset = (offset << sb->s_blocksize_bits) +
201cb00ea35SCyrill Gorcunov 		(inode->i_size & (sb->s_blocksize - 1));
2027e49b6f2SJan Kara 	if (etype == -1) {
2037e49b6f2SJan Kara 		/* We should extend the file? */
2047e49b6f2SJan Kara 		WARN_ON(byte_offset);
2052b42be5eSJan Kara 		return 0;
2067e49b6f2SJan Kara 	}
207ff116fc8SJan Kara 	epos.offset -= adsize;
20897e961fdSPekka Enberg 	extent_trunc(inode, &epos, &eloc, etype, elen, byte_offset);
209ff116fc8SJan Kara 	epos.offset += adsize;
21060448b1dSJan Kara 	if (byte_offset)
211ff116fc8SJan Kara 		lenalloc = epos.offset;
2121da177e4SLinus Torvalds 	else
213ff116fc8SJan Kara 		lenalloc = epos.offset - adsize;
2141da177e4SLinus Torvalds 
215ff116fc8SJan Kara 	if (!epos.bh)
2161da177e4SLinus Torvalds 		lenalloc -= udf_file_entry_alloc_offset(inode);
2171da177e4SLinus Torvalds 	else
2181da177e4SLinus Torvalds 		lenalloc -= sizeof(struct allocExtDesc);
2191da177e4SLinus Torvalds 
2204b11111aSMarcin Slusarz 	while ((etype = udf_current_aext(inode, &epos, &eloc,
2214b11111aSMarcin Slusarz 					 &elen, 0)) != -1) {
222800552ceSPali Rohár 		if (etype == (EXT_NEXT_EXTENT_ALLOCDESCS >> 30)) {
22397e961fdSPekka Enberg 			udf_write_aext(inode, &epos, &neloc, nelen, 0);
224cb00ea35SCyrill Gorcunov 			if (indirect_ext_len) {
225ff116fc8SJan Kara 				/* We managed to free all extents in the
226ff116fc8SJan Kara 				 * indirect extent - free it too */
2279de90b76Smarcin.slusarz@gmail.com 				BUG_ON(!epos.bh);
22817dc59baSJan Kara 				udf_free_blocks(sb, NULL, &epos.block,
229cb00ea35SCyrill Gorcunov 						0, indirect_ext_len);
2309de90b76Smarcin.slusarz@gmail.com 			} else if (!epos.bh) {
2319de90b76Smarcin.slusarz@gmail.com 				iinfo->i_lenAlloc = lenalloc;
2321da177e4SLinus Torvalds 				mark_inode_dirty(inode);
233456390deSmarcin.slusarz@gmail.com 			} else
234456390deSmarcin.slusarz@gmail.com 				udf_update_alloc_ext_desc(inode,
235456390deSmarcin.slusarz@gmail.com 						&epos, lenalloc);
236ff116fc8SJan Kara 			brelse(epos.bh);
237ff116fc8SJan Kara 			epos.offset = sizeof(struct allocExtDesc);
238ff116fc8SJan Kara 			epos.block = eloc;
239101ee137SJan Kara 			epos.bh = sb_bread(sb,
24097e961fdSPekka Enberg 					udf_get_lb_pblock(sb, &eloc, 0));
241d3ca4651SJan Kara 			/* Error reading indirect block? */
242d3ca4651SJan Kara 			if (!epos.bh)
2432b42be5eSJan Kara 				return -EIO;
2441da177e4SLinus Torvalds 			if (elen)
2454b11111aSMarcin Slusarz 				indirect_ext_len =
2464b11111aSMarcin Slusarz 					(elen + sb->s_blocksize - 1) >>
24728de7948SCyrill Gorcunov 					sb->s_blocksize_bits;
2481da177e4SLinus Torvalds 			else
249ff116fc8SJan Kara 				indirect_ext_len = 1;
250cb00ea35SCyrill Gorcunov 		} else {
2517e49b6f2SJan Kara 			extent_trunc(inode, &epos, &eloc, etype, elen, 0);
252ff116fc8SJan Kara 			epos.offset += adsize;
2531da177e4SLinus Torvalds 		}
2541da177e4SLinus Torvalds 	}
2551da177e4SLinus Torvalds 
256cb00ea35SCyrill Gorcunov 	if (indirect_ext_len) {
2579de90b76Smarcin.slusarz@gmail.com 		BUG_ON(!epos.bh);
25817dc59baSJan Kara 		udf_free_blocks(sb, NULL, &epos.block, 0, indirect_ext_len);
2599de90b76Smarcin.slusarz@gmail.com 	} else if (!epos.bh) {
26048d6d8ffSMarcin Slusarz 		iinfo->i_lenAlloc = lenalloc;
2611da177e4SLinus Torvalds 		mark_inode_dirty(inode);
262456390deSmarcin.slusarz@gmail.com 	} else
263456390deSmarcin.slusarz@gmail.com 		udf_update_alloc_ext_desc(inode, &epos, lenalloc);
26448d6d8ffSMarcin Slusarz 	iinfo->i_lenExtents = inode->i_size;
2651da177e4SLinus Torvalds 
2663bf25cb4SJan Kara 	brelse(epos.bh);
2672b42be5eSJan Kara 	return 0;
2681da177e4SLinus Torvalds }
269