xref: /openbmc/linux/fs/udf/truncate.c (revision 800552ce)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * truncate.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * PURPOSE
51da177e4SLinus Torvalds  *	Truncate handling routines for the OSTA-UDF(tm) filesystem.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * COPYRIGHT
81da177e4SLinus Torvalds  *	This file is distributed under the terms of the GNU General Public
91da177e4SLinus Torvalds  *	License (GPL). Copies of the GPL can be obtained from:
101da177e4SLinus Torvalds  *		ftp://prep.ai.mit.edu/pub/gnu/GPL
111da177e4SLinus Torvalds  *	Each contributing author retains all rights to their own work.
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  *  (C) 1999-2004 Ben Fennema
141da177e4SLinus Torvalds  *  (C) 1999 Stelias Computing Inc
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  * HISTORY
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  *  02/24/99 blf  Created.
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  */
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #include "udfdecl.h"
231da177e4SLinus Torvalds #include <linux/fs.h>
241da177e4SLinus Torvalds #include <linux/mm.h>
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #include "udf_i.h"
271da177e4SLinus Torvalds #include "udf_sb.h"
281da177e4SLinus Torvalds 
29ff116fc8SJan Kara static void extent_trunc(struct inode *inode, struct extent_position *epos,
3097e961fdSPekka Enberg 			 struct kernel_lb_addr *eloc, int8_t etype, uint32_t elen,
31cb00ea35SCyrill Gorcunov 			 uint32_t nelen)
321da177e4SLinus Torvalds {
335ca4e4beSPekka Enberg 	struct kernel_lb_addr neloc = {};
3428de7948SCyrill Gorcunov 	int last_block = (elen + inode->i_sb->s_blocksize - 1) >>
3528de7948SCyrill Gorcunov 		inode->i_sb->s_blocksize_bits;
3628de7948SCyrill Gorcunov 	int first_block = (nelen + inode->i_sb->s_blocksize - 1) >>
3728de7948SCyrill Gorcunov 		inode->i_sb->s_blocksize_bits;
381da177e4SLinus Torvalds 
39cb00ea35SCyrill Gorcunov 	if (nelen) {
40cb00ea35SCyrill Gorcunov 		if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
41cb00ea35SCyrill Gorcunov 			udf_free_blocks(inode->i_sb, inode, eloc, 0,
42cb00ea35SCyrill Gorcunov 					last_block);
431da177e4SLinus Torvalds 			etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
44cb00ea35SCyrill Gorcunov 		} else
4597e961fdSPekka Enberg 			neloc = *eloc;
461da177e4SLinus Torvalds 		nelen = (etype << 30) | nelen;
471da177e4SLinus Torvalds 	}
481da177e4SLinus Torvalds 
49cb00ea35SCyrill Gorcunov 	if (elen != nelen) {
5097e961fdSPekka Enberg 		udf_write_aext(inode, epos, &neloc, nelen, 0);
51b490bdd6SSteve Magnani 		if (last_block > first_block) {
521da177e4SLinus Torvalds 			if (etype == (EXT_RECORDED_ALLOCATED >> 30))
531da177e4SLinus Torvalds 				mark_inode_dirty(inode);
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 			if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
56cb00ea35SCyrill Gorcunov 				udf_free_blocks(inode->i_sb, inode, eloc,
57cb00ea35SCyrill Gorcunov 						first_block,
58cb00ea35SCyrill Gorcunov 						last_block - first_block);
591da177e4SLinus Torvalds 		}
601da177e4SLinus Torvalds 	}
611da177e4SLinus Torvalds }
621da177e4SLinus Torvalds 
6374584ae5SJan Kara /*
6474584ae5SJan Kara  * Truncate the last extent to match i_size. This function assumes
6574584ae5SJan Kara  * that preallocation extent is already truncated.
6674584ae5SJan Kara  */
6774584ae5SJan Kara void udf_truncate_tail_extent(struct inode *inode)
681da177e4SLinus Torvalds {
6928de7948SCyrill Gorcunov 	struct extent_position epos = {};
705ca4e4beSPekka Enberg 	struct kernel_lb_addr eloc;
71ff116fc8SJan Kara 	uint32_t elen, nelen;
721da177e4SLinus Torvalds 	uint64_t lbcount = 0;
731da177e4SLinus Torvalds 	int8_t etype = -1, netype;
741da177e4SLinus Torvalds 	int adsize;
7548d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
761da177e4SLinus Torvalds 
7748d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
7848d6d8ffSMarcin Slusarz 	    inode->i_size == iinfo->i_lenExtents)
791da177e4SLinus Torvalds 		return;
8074584ae5SJan Kara 	/* Are we going to delete the file anyway? */
8174584ae5SJan Kara 	if (inode->i_nlink == 0)
8274584ae5SJan Kara 		return;
8374584ae5SJan Kara 
8448d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
855ca4e4beSPekka Enberg 		adsize = sizeof(struct short_ad);
8648d6d8ffSMarcin Slusarz 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
875ca4e4beSPekka Enberg 		adsize = sizeof(struct long_ad);
8874584ae5SJan Kara 	else
8974584ae5SJan Kara 		BUG();
9074584ae5SJan Kara 
9174584ae5SJan Kara 	/* Find the last extent in the file */
92cb00ea35SCyrill Gorcunov 	while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
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;
10874584ae5SJan Kara 			if (udf_next_aext(inode, &epos, &eloc, &elen, 1) != -1)
10978ace70cSJoe Perches 				udf_err(inode->i_sb,
11078ace70cSJoe Perches 					"Extent after EOF in inode %u\n",
11174584ae5SJan Kara 					(unsigned)inode->i_ino);
11274584ae5SJan Kara 			break;
11374584ae5SJan Kara 		}
11474584ae5SJan Kara 	}
11574584ae5SJan Kara 	/* This inode entry is in-memory only and thus we don't have to mark
11674584ae5SJan Kara 	 * the inode dirty */
11748d6d8ffSMarcin Slusarz 	iinfo->i_lenExtents = inode->i_size;
11874584ae5SJan Kara 	brelse(epos.bh);
11974584ae5SJan Kara }
12074584ae5SJan Kara 
12174584ae5SJan Kara void udf_discard_prealloc(struct inode *inode)
12274584ae5SJan Kara {
12374584ae5SJan Kara 	struct extent_position epos = { NULL, 0, {0, 0} };
1245ca4e4beSPekka Enberg 	struct kernel_lb_addr eloc;
12574584ae5SJan Kara 	uint32_t elen;
12674584ae5SJan Kara 	uint64_t lbcount = 0;
12774584ae5SJan Kara 	int8_t etype = -1, netype;
12874584ae5SJan Kara 	int adsize;
12948d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
13074584ae5SJan Kara 
13148d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
13248d6d8ffSMarcin Slusarz 	    inode->i_size == iinfo->i_lenExtents)
13374584ae5SJan Kara 		return;
1341da177e4SLinus Torvalds 
13548d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1365ca4e4beSPekka Enberg 		adsize = sizeof(struct short_ad);
13748d6d8ffSMarcin Slusarz 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1385ca4e4beSPekka Enberg 		adsize = sizeof(struct long_ad);
1391da177e4SLinus Torvalds 	else
1401da177e4SLinus Torvalds 		adsize = 0;
1411da177e4SLinus Torvalds 
14248d6d8ffSMarcin Slusarz 	epos.block = iinfo->i_location;
1431da177e4SLinus Torvalds 
144ff116fc8SJan Kara 	/* Find the last extent in the file */
14574584ae5SJan Kara 	while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
1461da177e4SLinus Torvalds 		etype = netype;
1471da177e4SLinus Torvalds 		lbcount += elen;
1481da177e4SLinus Torvalds 	}
149ff116fc8SJan Kara 	if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
150ff116fc8SJan Kara 		epos.offset -= adsize;
1511da177e4SLinus Torvalds 		lbcount -= elen;
15297e961fdSPekka Enberg 		extent_trunc(inode, &epos, &eloc, etype, elen, 0);
15374584ae5SJan Kara 		if (!epos.bh) {
15448d6d8ffSMarcin Slusarz 			iinfo->i_lenAlloc =
1554b11111aSMarcin Slusarz 				epos.offset -
1564b11111aSMarcin Slusarz 				udf_file_entry_alloc_offset(inode);
1571da177e4SLinus Torvalds 			mark_inode_dirty(inode);
15874584ae5SJan Kara 		} else {
159cb00ea35SCyrill Gorcunov 			struct allocExtDesc *aed =
160cb00ea35SCyrill Gorcunov 				(struct allocExtDesc *)(epos.bh->b_data);
161cb00ea35SCyrill Gorcunov 			aed->lengthAllocDescs =
162cb00ea35SCyrill Gorcunov 				cpu_to_le32(epos.offset -
163cb00ea35SCyrill Gorcunov 					    sizeof(struct allocExtDesc));
16428de7948SCyrill Gorcunov 			if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1656c79e987SMarcin Slusarz 			    UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
166ff116fc8SJan Kara 				udf_update_tag(epos.bh->b_data, epos.offset);
1671da177e4SLinus Torvalds 			else
168cb00ea35SCyrill Gorcunov 				udf_update_tag(epos.bh->b_data,
169cb00ea35SCyrill Gorcunov 					       sizeof(struct allocExtDesc));
170ff116fc8SJan Kara 			mark_buffer_dirty_inode(epos.bh, inode);
1711da177e4SLinus Torvalds 		}
1721da177e4SLinus Torvalds 	}
17374584ae5SJan Kara 	/* This inode entry is in-memory only and thus we don't have to mark
17474584ae5SJan Kara 	 * the inode dirty */
17548d6d8ffSMarcin Slusarz 	iinfo->i_lenExtents = lbcount;
1763bf25cb4SJan Kara 	brelse(epos.bh);
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds 
179456390deSmarcin.slusarz@gmail.com static void udf_update_alloc_ext_desc(struct inode *inode,
180456390deSmarcin.slusarz@gmail.com 				      struct extent_position *epos,
181456390deSmarcin.slusarz@gmail.com 				      u32 lenalloc)
182456390deSmarcin.slusarz@gmail.com {
183456390deSmarcin.slusarz@gmail.com 	struct super_block *sb = inode->i_sb;
184456390deSmarcin.slusarz@gmail.com 	struct udf_sb_info *sbi = UDF_SB(sb);
185456390deSmarcin.slusarz@gmail.com 
186456390deSmarcin.slusarz@gmail.com 	struct allocExtDesc *aed = (struct allocExtDesc *) (epos->bh->b_data);
187456390deSmarcin.slusarz@gmail.com 	int len = sizeof(struct allocExtDesc);
188456390deSmarcin.slusarz@gmail.com 
189456390deSmarcin.slusarz@gmail.com 	aed->lengthAllocDescs =	cpu_to_le32(lenalloc);
190456390deSmarcin.slusarz@gmail.com 	if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || sbi->s_udfrev >= 0x0201)
191456390deSmarcin.slusarz@gmail.com 		len += lenalloc;
192456390deSmarcin.slusarz@gmail.com 
193456390deSmarcin.slusarz@gmail.com 	udf_update_tag(epos->bh->b_data, len);
194456390deSmarcin.slusarz@gmail.com 	mark_buffer_dirty_inode(epos->bh, inode);
195456390deSmarcin.slusarz@gmail.com }
196456390deSmarcin.slusarz@gmail.com 
1977e49b6f2SJan Kara /*
1987e49b6f2SJan Kara  * Truncate extents of inode to inode->i_size. This function can be used only
1997e49b6f2SJan Kara  * for making file shorter. For making file longer, udf_extend_file() has to
2007e49b6f2SJan Kara  * be used.
2017e49b6f2SJan Kara  */
2022b42be5eSJan Kara int udf_truncate_extents(struct inode *inode)
2031da177e4SLinus Torvalds {
204ff116fc8SJan Kara 	struct extent_position epos;
2055ca4e4beSPekka Enberg 	struct kernel_lb_addr eloc, neloc = {};
206ff116fc8SJan Kara 	uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
2071da177e4SLinus Torvalds 	int8_t etype;
20831170b6aSJan Kara 	struct super_block *sb = inode->i_sb;
20931170b6aSJan Kara 	sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
21060448b1dSJan Kara 	loff_t byte_offset;
2111da177e4SLinus Torvalds 	int adsize;
21248d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
2131da177e4SLinus Torvalds 
21448d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2155ca4e4beSPekka Enberg 		adsize = sizeof(struct short_ad);
21648d6d8ffSMarcin Slusarz 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2175ca4e4beSPekka Enberg 		adsize = sizeof(struct long_ad);
2181da177e4SLinus Torvalds 	else
219ff116fc8SJan Kara 		BUG();
2201da177e4SLinus Torvalds 
221ff116fc8SJan Kara 	etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
22228de7948SCyrill Gorcunov 	byte_offset = (offset << sb->s_blocksize_bits) +
223cb00ea35SCyrill Gorcunov 		(inode->i_size & (sb->s_blocksize - 1));
2247e49b6f2SJan Kara 	if (etype == -1) {
2257e49b6f2SJan Kara 		/* We should extend the file? */
2267e49b6f2SJan Kara 		WARN_ON(byte_offset);
2272b42be5eSJan Kara 		return 0;
2287e49b6f2SJan Kara 	}
229ff116fc8SJan Kara 	epos.offset -= adsize;
23097e961fdSPekka Enberg 	extent_trunc(inode, &epos, &eloc, etype, elen, byte_offset);
231ff116fc8SJan Kara 	epos.offset += adsize;
23260448b1dSJan Kara 	if (byte_offset)
233ff116fc8SJan Kara 		lenalloc = epos.offset;
2341da177e4SLinus Torvalds 	else
235ff116fc8SJan Kara 		lenalloc = epos.offset - adsize;
2361da177e4SLinus Torvalds 
237ff116fc8SJan Kara 	if (!epos.bh)
2381da177e4SLinus Torvalds 		lenalloc -= udf_file_entry_alloc_offset(inode);
2391da177e4SLinus Torvalds 	else
2401da177e4SLinus Torvalds 		lenalloc -= sizeof(struct allocExtDesc);
2411da177e4SLinus Torvalds 
2424b11111aSMarcin Slusarz 	while ((etype = udf_current_aext(inode, &epos, &eloc,
2434b11111aSMarcin Slusarz 					 &elen, 0)) != -1) {
244800552ceSPali Rohár 		if (etype == (EXT_NEXT_EXTENT_ALLOCDESCS >> 30)) {
24597e961fdSPekka Enberg 			udf_write_aext(inode, &epos, &neloc, nelen, 0);
246cb00ea35SCyrill Gorcunov 			if (indirect_ext_len) {
247ff116fc8SJan Kara 				/* We managed to free all extents in the
248ff116fc8SJan Kara 				 * indirect extent - free it too */
2499de90b76Smarcin.slusarz@gmail.com 				BUG_ON(!epos.bh);
25017dc59baSJan Kara 				udf_free_blocks(sb, NULL, &epos.block,
251cb00ea35SCyrill Gorcunov 						0, indirect_ext_len);
2529de90b76Smarcin.slusarz@gmail.com 			} else if (!epos.bh) {
2539de90b76Smarcin.slusarz@gmail.com 				iinfo->i_lenAlloc = lenalloc;
2541da177e4SLinus Torvalds 				mark_inode_dirty(inode);
255456390deSmarcin.slusarz@gmail.com 			} else
256456390deSmarcin.slusarz@gmail.com 				udf_update_alloc_ext_desc(inode,
257456390deSmarcin.slusarz@gmail.com 						&epos, lenalloc);
258ff116fc8SJan Kara 			brelse(epos.bh);
259ff116fc8SJan Kara 			epos.offset = sizeof(struct allocExtDesc);
260ff116fc8SJan Kara 			epos.block = eloc;
2614b11111aSMarcin Slusarz 			epos.bh = udf_tread(sb,
26297e961fdSPekka Enberg 					udf_get_lb_pblock(sb, &eloc, 0));
263d3ca4651SJan Kara 			/* Error reading indirect block? */
264d3ca4651SJan Kara 			if (!epos.bh)
2652b42be5eSJan Kara 				return -EIO;
2661da177e4SLinus Torvalds 			if (elen)
2674b11111aSMarcin Slusarz 				indirect_ext_len =
2684b11111aSMarcin Slusarz 					(elen + sb->s_blocksize - 1) >>
26928de7948SCyrill Gorcunov 					sb->s_blocksize_bits;
2701da177e4SLinus Torvalds 			else
271ff116fc8SJan Kara 				indirect_ext_len = 1;
272cb00ea35SCyrill Gorcunov 		} else {
2737e49b6f2SJan Kara 			extent_trunc(inode, &epos, &eloc, etype, elen, 0);
274ff116fc8SJan Kara 			epos.offset += adsize;
2751da177e4SLinus Torvalds 		}
2761da177e4SLinus Torvalds 	}
2771da177e4SLinus Torvalds 
278cb00ea35SCyrill Gorcunov 	if (indirect_ext_len) {
2799de90b76Smarcin.slusarz@gmail.com 		BUG_ON(!epos.bh);
28017dc59baSJan Kara 		udf_free_blocks(sb, NULL, &epos.block, 0, indirect_ext_len);
2819de90b76Smarcin.slusarz@gmail.com 	} else if (!epos.bh) {
28248d6d8ffSMarcin Slusarz 		iinfo->i_lenAlloc = lenalloc;
2831da177e4SLinus Torvalds 		mark_inode_dirty(inode);
284456390deSmarcin.slusarz@gmail.com 	} else
285456390deSmarcin.slusarz@gmail.com 		udf_update_alloc_ext_desc(inode, &epos, lenalloc);
28648d6d8ffSMarcin Slusarz 	iinfo->i_lenExtents = inode->i_size;
2871da177e4SLinus Torvalds 
2883bf25cb4SJan Kara 	brelse(epos.bh);
2892b42be5eSJan Kara 	return 0;
2901da177e4SLinus Torvalds }
291