xref: /openbmc/linux/fs/udf/truncate.c (revision e50e86dbcabda570fc8a1435fe2fca97e9ab7312)
15ce34554SBagas 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);
725fc8da4dSZhao Mengmeng 	int ret;
731da177e4SLinus Torvalds 
7448d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
7548d6d8ffSMarcin Slusarz 	    inode->i_size == iinfo->i_lenExtents)
761da177e4SLinus Torvalds 		return;
7774584ae5SJan Kara 	/* Are we going to delete the file anyway? */
7874584ae5SJan Kara 	if (inode->i_nlink == 0)
7974584ae5SJan Kara 		return;
8074584ae5SJan Kara 
8148d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
825ca4e4beSPekka Enberg 		adsize = sizeof(struct short_ad);
8348d6d8ffSMarcin Slusarz 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
845ca4e4beSPekka Enberg 		adsize = sizeof(struct long_ad);
8574584ae5SJan Kara 	else
8674584ae5SJan Kara 		BUG();
8774584ae5SJan Kara 
8874584ae5SJan Kara 	/* Find the last extent in the file */
895fc8da4dSZhao Mengmeng 	while (1) {
905fc8da4dSZhao Mengmeng 		ret = udf_next_aext(inode, &epos, &eloc, &elen, &netype, 1);
915fc8da4dSZhao Mengmeng 		if (ret <= 0)
925fc8da4dSZhao Mengmeng 			break;
9374584ae5SJan Kara 		etype = netype;
9474584ae5SJan Kara 		lbcount += elen;
9574584ae5SJan Kara 		if (lbcount > inode->i_size) {
9674584ae5SJan Kara 			if (lbcount - inode->i_size >= inode->i_sb->s_blocksize)
9778ace70cSJoe Perches 				udf_warn(inode->i_sb,
9878ace70cSJoe Perches 					 "Too long extent after EOF in inode %u: i_size: %lld lbcount: %lld extent %u+%u\n",
9974584ae5SJan Kara 					 (unsigned)inode->i_ino,
10074584ae5SJan Kara 					 (long long)inode->i_size,
10174584ae5SJan Kara 					 (long long)lbcount,
10274584ae5SJan Kara 					 (unsigned)eloc.logicalBlockNum,
10374584ae5SJan Kara 					 (unsigned)elen);
10474584ae5SJan Kara 			nelen = elen - (lbcount - inode->i_size);
10574584ae5SJan Kara 			epos.offset -= adsize;
10697e961fdSPekka Enberg 			extent_trunc(inode, &epos, &eloc, etype, elen, nelen);
10774584ae5SJan Kara 			epos.offset += adsize;
1085fc8da4dSZhao Mengmeng 			if (udf_next_aext(inode, &epos, &eloc, &elen,
1095fc8da4dSZhao Mengmeng 					  &netype, 1) > 0)
11078ace70cSJoe Perches 				udf_err(inode->i_sb,
11178ace70cSJoe Perches 					"Extent after EOF in inode %u\n",
11274584ae5SJan Kara 					(unsigned)inode->i_ino);
11374584ae5SJan Kara 			break;
11474584ae5SJan Kara 		}
11574584ae5SJan Kara 	}
11674584ae5SJan Kara 	/* This inode entry is in-memory only and thus we don't have to mark
11774584ae5SJan Kara 	 * the inode dirty */
1185fc8da4dSZhao Mengmeng 	if (ret == 0)
11948d6d8ffSMarcin Slusarz 		iinfo->i_lenExtents = inode->i_size;
12074584ae5SJan Kara 	brelse(epos.bh);
12174584ae5SJan Kara }
12274584ae5SJan Kara 
udf_discard_prealloc(struct inode * inode)12374584ae5SJan Kara void udf_discard_prealloc(struct inode *inode)
12474584ae5SJan Kara {
125cfe4c1b2SJan Kara 	struct extent_position epos = {};
126cfe4c1b2SJan Kara 	struct extent_position prev_epos = {};
1275ca4e4beSPekka Enberg 	struct kernel_lb_addr eloc;
12874584ae5SJan Kara 	uint32_t elen;
12974584ae5SJan Kara 	uint64_t lbcount = 0;
1301fb40763SColin Ian King 	int8_t etype = -1;
13148d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
132f768dc3cSYangtao Li 	int bsize = i_blocksize(inode);
1335fc8da4dSZhao Mengmeng 	int8_t tmpetype = -1;
1345fc8da4dSZhao Mengmeng 	int ret;
13574584ae5SJan Kara 
13648d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
1376ad53f0fSJan Kara 	    ALIGN(inode->i_size, bsize) == ALIGN(iinfo->i_lenExtents, bsize))
13874584ae5SJan Kara 		return;
1391da177e4SLinus Torvalds 
14048d6d8ffSMarcin Slusarz 	epos.block = iinfo->i_location;
1411da177e4SLinus Torvalds 
142ff116fc8SJan Kara 	/* Find the last extent in the file */
1435fc8da4dSZhao Mengmeng 	while (1) {
1445fc8da4dSZhao Mengmeng 		ret = udf_next_aext(inode, &epos, &eloc, &elen, &tmpetype, 0);
1455fc8da4dSZhao Mengmeng 		if (ret < 0)
1465fc8da4dSZhao Mengmeng 			goto out;
1475fc8da4dSZhao Mengmeng 		if (ret == 0)
1485fc8da4dSZhao Mengmeng 			break;
149cfe4c1b2SJan Kara 		brelse(prev_epos.bh);
150cfe4c1b2SJan Kara 		prev_epos = epos;
151cfe4c1b2SJan Kara 		if (prev_epos.bh)
152cfe4c1b2SJan Kara 			get_bh(prev_epos.bh);
153cfe4c1b2SJan Kara 
1545fc8da4dSZhao Mengmeng 		ret = udf_next_aext(inode, &epos, &eloc, &elen, &etype, 1);
1555fc8da4dSZhao Mengmeng 		if (ret < 0)
1565fc8da4dSZhao Mengmeng 			goto out;
1571da177e4SLinus Torvalds 		lbcount += elen;
1581da177e4SLinus Torvalds 	}
1595fc8da4dSZhao Mengmeng 
160ff116fc8SJan Kara 	if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1611da177e4SLinus Torvalds 		lbcount -= elen;
162cfe4c1b2SJan Kara 		udf_delete_aext(inode, prev_epos);
163cfe4c1b2SJan Kara 		udf_free_blocks(inode->i_sb, inode, &eloc, 0,
164f768dc3cSYangtao Li 				DIV_ROUND_UP(elen, bsize));
1651da177e4SLinus Torvalds 	}
16674584ae5SJan Kara 	/* This inode entry is in-memory only and thus we don't have to mark
16774584ae5SJan Kara 	 * the inode dirty */
16848d6d8ffSMarcin Slusarz 	iinfo->i_lenExtents = lbcount;
1695fc8da4dSZhao Mengmeng out:
1703bf25cb4SJan Kara 	brelse(epos.bh);
171cfe4c1b2SJan Kara 	brelse(prev_epos.bh);
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
udf_update_alloc_ext_desc(struct inode * inode,struct extent_position * epos,u32 lenalloc)174456390deSmarcin.slusarz@gmail.com static void udf_update_alloc_ext_desc(struct inode *inode,
175456390deSmarcin.slusarz@gmail.com 				      struct extent_position *epos,
176456390deSmarcin.slusarz@gmail.com 				      u32 lenalloc)
177456390deSmarcin.slusarz@gmail.com {
178456390deSmarcin.slusarz@gmail.com 	struct super_block *sb = inode->i_sb;
179456390deSmarcin.slusarz@gmail.com 	struct udf_sb_info *sbi = UDF_SB(sb);
180456390deSmarcin.slusarz@gmail.com 
181456390deSmarcin.slusarz@gmail.com 	struct allocExtDesc *aed = (struct allocExtDesc *) (epos->bh->b_data);
182456390deSmarcin.slusarz@gmail.com 	int len = sizeof(struct allocExtDesc);
183456390deSmarcin.slusarz@gmail.com 
184456390deSmarcin.slusarz@gmail.com 	aed->lengthAllocDescs =	cpu_to_le32(lenalloc);
185456390deSmarcin.slusarz@gmail.com 	if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || sbi->s_udfrev >= 0x0201)
186456390deSmarcin.slusarz@gmail.com 		len += lenalloc;
187456390deSmarcin.slusarz@gmail.com 
188456390deSmarcin.slusarz@gmail.com 	udf_update_tag(epos->bh->b_data, len);
189456390deSmarcin.slusarz@gmail.com 	mark_buffer_dirty_inode(epos->bh, inode);
190456390deSmarcin.slusarz@gmail.com }
191456390deSmarcin.slusarz@gmail.com 
1927e49b6f2SJan Kara /*
1937e49b6f2SJan Kara  * Truncate extents of inode to inode->i_size. This function can be used only
1947e49b6f2SJan Kara  * for making file shorter. For making file longer, udf_extend_file() has to
1957e49b6f2SJan Kara  * be used.
1967e49b6f2SJan Kara  */
udf_truncate_extents(struct inode * inode)1972b42be5eSJan Kara int udf_truncate_extents(struct inode *inode)
1981da177e4SLinus Torvalds {
199ff116fc8SJan Kara 	struct extent_position epos;
2005ca4e4beSPekka Enberg 	struct kernel_lb_addr eloc, neloc = {};
201ff116fc8SJan Kara 	uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
2021da177e4SLinus Torvalds 	int8_t etype;
20331170b6aSJan Kara 	struct super_block *sb = inode->i_sb;
20431170b6aSJan Kara 	sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
20560448b1dSJan Kara 	loff_t byte_offset;
2061da177e4SLinus Torvalds 	int adsize;
20748d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
208402c8862SZhao Mengmeng 	int ret = 0;
2091da177e4SLinus Torvalds 
21048d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2115ca4e4beSPekka Enberg 		adsize = sizeof(struct short_ad);
21248d6d8ffSMarcin Slusarz 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2135ca4e4beSPekka Enberg 		adsize = sizeof(struct long_ad);
2141da177e4SLinus Torvalds 	else
215ff116fc8SJan Kara 		BUG();
2161da177e4SLinus Torvalds 
217*493447ddSZhao Mengmeng 	ret = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset, &etype);
218*493447ddSZhao Mengmeng 	if (ret < 0)
219*493447ddSZhao Mengmeng 		return ret;
22028de7948SCyrill Gorcunov 	byte_offset = (offset << sb->s_blocksize_bits) +
221cb00ea35SCyrill Gorcunov 		(inode->i_size & (sb->s_blocksize - 1));
222*493447ddSZhao Mengmeng 	if (ret == 0) {
2237e49b6f2SJan Kara 		/* We should extend the file? */
2247e49b6f2SJan Kara 		WARN_ON(byte_offset);
2252b42be5eSJan Kara 		return 0;
2267e49b6f2SJan Kara 	}
227ff116fc8SJan Kara 	epos.offset -= adsize;
22897e961fdSPekka Enberg 	extent_trunc(inode, &epos, &eloc, etype, elen, byte_offset);
229ff116fc8SJan Kara 	epos.offset += adsize;
23060448b1dSJan Kara 	if (byte_offset)
231ff116fc8SJan Kara 		lenalloc = epos.offset;
2321da177e4SLinus Torvalds 	else
233ff116fc8SJan Kara 		lenalloc = epos.offset - adsize;
2341da177e4SLinus Torvalds 
235ff116fc8SJan Kara 	if (!epos.bh)
2361da177e4SLinus Torvalds 		lenalloc -= udf_file_entry_alloc_offset(inode);
2371da177e4SLinus Torvalds 	else
2381da177e4SLinus Torvalds 		lenalloc -= sizeof(struct allocExtDesc);
2391da177e4SLinus Torvalds 
240402c8862SZhao Mengmeng 	while ((ret = udf_current_aext(inode, &epos, &eloc,
241402c8862SZhao Mengmeng 				       &elen, &etype, 0)) > 0) {
242800552ceSPali Rohár 		if (etype == (EXT_NEXT_EXTENT_ALLOCDESCS >> 30)) {
24397e961fdSPekka Enberg 			udf_write_aext(inode, &epos, &neloc, nelen, 0);
244cb00ea35SCyrill Gorcunov 			if (indirect_ext_len) {
245ff116fc8SJan Kara 				/* We managed to free all extents in the
246ff116fc8SJan Kara 				 * indirect extent - free it too */
2479de90b76Smarcin.slusarz@gmail.com 				BUG_ON(!epos.bh);
24817dc59baSJan Kara 				udf_free_blocks(sb, NULL, &epos.block,
249cb00ea35SCyrill Gorcunov 						0, indirect_ext_len);
2509de90b76Smarcin.slusarz@gmail.com 			} else if (!epos.bh) {
2519de90b76Smarcin.slusarz@gmail.com 				iinfo->i_lenAlloc = lenalloc;
2521da177e4SLinus Torvalds 				mark_inode_dirty(inode);
253456390deSmarcin.slusarz@gmail.com 			} else
254456390deSmarcin.slusarz@gmail.com 				udf_update_alloc_ext_desc(inode,
255456390deSmarcin.slusarz@gmail.com 						&epos, lenalloc);
256ff116fc8SJan Kara 			brelse(epos.bh);
257ff116fc8SJan Kara 			epos.offset = sizeof(struct allocExtDesc);
258ff116fc8SJan Kara 			epos.block = eloc;
259101ee137SJan Kara 			epos.bh = sb_bread(sb,
26097e961fdSPekka Enberg 					udf_get_lb_pblock(sb, &eloc, 0));
261d3ca4651SJan Kara 			/* Error reading indirect block? */
262d3ca4651SJan Kara 			if (!epos.bh)
2632b42be5eSJan Kara 				return -EIO;
2641da177e4SLinus Torvalds 			if (elen)
2654b11111aSMarcin Slusarz 				indirect_ext_len =
2664b11111aSMarcin Slusarz 					(elen + sb->s_blocksize - 1) >>
26728de7948SCyrill Gorcunov 					sb->s_blocksize_bits;
2681da177e4SLinus Torvalds 			else
269ff116fc8SJan Kara 				indirect_ext_len = 1;
270cb00ea35SCyrill Gorcunov 		} else {
2717e49b6f2SJan Kara 			extent_trunc(inode, &epos, &eloc, etype, elen, 0);
272ff116fc8SJan Kara 			epos.offset += adsize;
2731da177e4SLinus Torvalds 		}
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds 
276402c8862SZhao Mengmeng 	if (ret < 0) {
277402c8862SZhao Mengmeng 		brelse(epos.bh);
278402c8862SZhao Mengmeng 		return ret;
279402c8862SZhao Mengmeng 	}
280402c8862SZhao Mengmeng 
281cb00ea35SCyrill Gorcunov 	if (indirect_ext_len) {
2829de90b76Smarcin.slusarz@gmail.com 		BUG_ON(!epos.bh);
28317dc59baSJan Kara 		udf_free_blocks(sb, NULL, &epos.block, 0, indirect_ext_len);
2849de90b76Smarcin.slusarz@gmail.com 	} else if (!epos.bh) {
28548d6d8ffSMarcin Slusarz 		iinfo->i_lenAlloc = lenalloc;
2861da177e4SLinus Torvalds 		mark_inode_dirty(inode);
287456390deSmarcin.slusarz@gmail.com 	} else
288456390deSmarcin.slusarz@gmail.com 		udf_update_alloc_ext_desc(inode, &epos, lenalloc);
28948d6d8ffSMarcin Slusarz 	iinfo->i_lenExtents = inode->i_size;
2901da177e4SLinus Torvalds 
2913bf25cb4SJan Kara 	brelse(epos.bh);
2922b42be5eSJan Kara 	return 0;
2931da177e4SLinus Torvalds }
294