xref: /openbmc/linux/fs/udf/truncate.c (revision 7e49b6f2)
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 #include <linux/buffer_head.h>
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #include "udf_i.h"
281da177e4SLinus Torvalds #include "udf_sb.h"
291da177e4SLinus Torvalds 
30ff116fc8SJan Kara static void extent_trunc(struct inode *inode, struct extent_position *epos,
3197e961fdSPekka Enberg 			 struct kernel_lb_addr *eloc, int8_t etype, uint32_t elen,
32cb00ea35SCyrill Gorcunov 			 uint32_t nelen)
331da177e4SLinus Torvalds {
345ca4e4beSPekka Enberg 	struct kernel_lb_addr neloc = {};
3528de7948SCyrill Gorcunov 	int last_block = (elen + inode->i_sb->s_blocksize - 1) >>
3628de7948SCyrill Gorcunov 		inode->i_sb->s_blocksize_bits;
3728de7948SCyrill Gorcunov 	int first_block = (nelen + inode->i_sb->s_blocksize - 1) >>
3828de7948SCyrill Gorcunov 		inode->i_sb->s_blocksize_bits;
391da177e4SLinus Torvalds 
40cb00ea35SCyrill Gorcunov 	if (nelen) {
41cb00ea35SCyrill Gorcunov 		if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
42cb00ea35SCyrill Gorcunov 			udf_free_blocks(inode->i_sb, inode, eloc, 0,
43cb00ea35SCyrill Gorcunov 					last_block);
441da177e4SLinus Torvalds 			etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
45cb00ea35SCyrill Gorcunov 		} else
4697e961fdSPekka Enberg 			neloc = *eloc;
471da177e4SLinus Torvalds 		nelen = (etype << 30) | nelen;
481da177e4SLinus Torvalds 	}
491da177e4SLinus Torvalds 
50cb00ea35SCyrill Gorcunov 	if (elen != nelen) {
5197e961fdSPekka Enberg 		udf_write_aext(inode, epos, &neloc, nelen, 0);
52cb00ea35SCyrill Gorcunov 		if (last_block - first_block > 0) {
531da177e4SLinus Torvalds 			if (etype == (EXT_RECORDED_ALLOCATED >> 30))
541da177e4SLinus Torvalds 				mark_inode_dirty(inode);
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 			if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
57cb00ea35SCyrill Gorcunov 				udf_free_blocks(inode->i_sb, inode, eloc,
58cb00ea35SCyrill Gorcunov 						first_block,
59cb00ea35SCyrill Gorcunov 						last_block - first_block);
601da177e4SLinus Torvalds 		}
611da177e4SLinus Torvalds 	}
621da177e4SLinus Torvalds }
631da177e4SLinus Torvalds 
6474584ae5SJan Kara /*
6574584ae5SJan Kara  * Truncate the last extent to match i_size. This function assumes
6674584ae5SJan Kara  * that preallocation extent is already truncated.
6774584ae5SJan Kara  */
6874584ae5SJan Kara void udf_truncate_tail_extent(struct inode *inode)
691da177e4SLinus Torvalds {
7028de7948SCyrill Gorcunov 	struct extent_position epos = {};
715ca4e4beSPekka Enberg 	struct kernel_lb_addr eloc;
72ff116fc8SJan Kara 	uint32_t elen, nelen;
731da177e4SLinus Torvalds 	uint64_t lbcount = 0;
741da177e4SLinus Torvalds 	int8_t etype = -1, netype;
751da177e4SLinus Torvalds 	int adsize;
7648d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
771da177e4SLinus Torvalds 
7848d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
7948d6d8ffSMarcin Slusarz 	    inode->i_size == iinfo->i_lenExtents)
801da177e4SLinus Torvalds 		return;
8174584ae5SJan Kara 	/* Are we going to delete the file anyway? */
8274584ae5SJan Kara 	if (inode->i_nlink == 0)
8374584ae5SJan Kara 		return;
8474584ae5SJan Kara 
8548d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
865ca4e4beSPekka Enberg 		adsize = sizeof(struct short_ad);
8748d6d8ffSMarcin Slusarz 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
885ca4e4beSPekka Enberg 		adsize = sizeof(struct long_ad);
8974584ae5SJan Kara 	else
9074584ae5SJan Kara 		BUG();
9174584ae5SJan Kara 
9274584ae5SJan Kara 	/* Find the last extent in the file */
93cb00ea35SCyrill Gorcunov 	while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
9474584ae5SJan Kara 		etype = netype;
9574584ae5SJan Kara 		lbcount += elen;
9674584ae5SJan Kara 		if (lbcount > inode->i_size) {
9774584ae5SJan Kara 			if (lbcount - inode->i_size >= inode->i_sb->s_blocksize)
9874584ae5SJan Kara 				printk(KERN_WARNING
9974584ae5SJan Kara 				       "udf_truncate_tail_extent(): Too long "
10074584ae5SJan Kara 				       "extent after EOF in inode %u: i_size: "
10174584ae5SJan Kara 				       "%Ld lbcount: %Ld extent %u+%u\n",
10274584ae5SJan Kara 				       (unsigned)inode->i_ino,
10374584ae5SJan Kara 				       (long long)inode->i_size,
10474584ae5SJan Kara 				       (long long)lbcount,
10574584ae5SJan Kara 				       (unsigned)eloc.logicalBlockNum,
10674584ae5SJan Kara 				       (unsigned)elen);
10774584ae5SJan Kara 			nelen = elen - (lbcount - inode->i_size);
10874584ae5SJan Kara 			epos.offset -= adsize;
10997e961fdSPekka Enberg 			extent_trunc(inode, &epos, &eloc, etype, elen, nelen);
11074584ae5SJan Kara 			epos.offset += adsize;
11174584ae5SJan Kara 			if (udf_next_aext(inode, &epos, &eloc, &elen, 1) != -1)
11274584ae5SJan Kara 				printk(KERN_ERR "udf_truncate_tail_extent(): "
11374584ae5SJan Kara 				       "Extent after EOF in inode %u.\n",
11474584ae5SJan Kara 				       (unsigned)inode->i_ino);
11574584ae5SJan Kara 			break;
11674584ae5SJan Kara 		}
11774584ae5SJan Kara 	}
11874584ae5SJan Kara 	/* This inode entry is in-memory only and thus we don't have to mark
11974584ae5SJan Kara 	 * the inode dirty */
12048d6d8ffSMarcin Slusarz 	iinfo->i_lenExtents = inode->i_size;
12174584ae5SJan Kara 	brelse(epos.bh);
12274584ae5SJan Kara }
12374584ae5SJan Kara 
12474584ae5SJan Kara void udf_discard_prealloc(struct inode *inode)
12574584ae5SJan Kara {
12674584ae5SJan Kara 	struct extent_position epos = { NULL, 0, {0, 0} };
1275ca4e4beSPekka Enberg 	struct kernel_lb_addr eloc;
12874584ae5SJan Kara 	uint32_t elen;
12974584ae5SJan Kara 	uint64_t lbcount = 0;
13074584ae5SJan Kara 	int8_t etype = -1, netype;
13174584ae5SJan Kara 	int adsize;
13248d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
13374584ae5SJan Kara 
13448d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
13548d6d8ffSMarcin Slusarz 	    inode->i_size == iinfo->i_lenExtents)
13674584ae5SJan Kara 		return;
1371da177e4SLinus Torvalds 
13848d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1395ca4e4beSPekka Enberg 		adsize = sizeof(struct short_ad);
14048d6d8ffSMarcin Slusarz 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1415ca4e4beSPekka Enberg 		adsize = sizeof(struct long_ad);
1421da177e4SLinus Torvalds 	else
1431da177e4SLinus Torvalds 		adsize = 0;
1441da177e4SLinus Torvalds 
14548d6d8ffSMarcin Slusarz 	epos.block = iinfo->i_location;
1461da177e4SLinus Torvalds 
147ff116fc8SJan Kara 	/* Find the last extent in the file */
14874584ae5SJan Kara 	while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
1491da177e4SLinus Torvalds 		etype = netype;
1501da177e4SLinus Torvalds 		lbcount += elen;
1511da177e4SLinus Torvalds 	}
152ff116fc8SJan Kara 	if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
153ff116fc8SJan Kara 		epos.offset -= adsize;
1541da177e4SLinus Torvalds 		lbcount -= elen;
15597e961fdSPekka Enberg 		extent_trunc(inode, &epos, &eloc, etype, elen, 0);
15674584ae5SJan Kara 		if (!epos.bh) {
15748d6d8ffSMarcin Slusarz 			iinfo->i_lenAlloc =
1584b11111aSMarcin Slusarz 				epos.offset -
1594b11111aSMarcin Slusarz 				udf_file_entry_alloc_offset(inode);
1601da177e4SLinus Torvalds 			mark_inode_dirty(inode);
16174584ae5SJan Kara 		} else {
162cb00ea35SCyrill Gorcunov 			struct allocExtDesc *aed =
163cb00ea35SCyrill Gorcunov 				(struct allocExtDesc *)(epos.bh->b_data);
164cb00ea35SCyrill Gorcunov 			aed->lengthAllocDescs =
165cb00ea35SCyrill Gorcunov 				cpu_to_le32(epos.offset -
166cb00ea35SCyrill Gorcunov 					    sizeof(struct allocExtDesc));
16728de7948SCyrill Gorcunov 			if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1686c79e987SMarcin Slusarz 			    UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
169ff116fc8SJan Kara 				udf_update_tag(epos.bh->b_data, epos.offset);
1701da177e4SLinus Torvalds 			else
171cb00ea35SCyrill Gorcunov 				udf_update_tag(epos.bh->b_data,
172cb00ea35SCyrill Gorcunov 					       sizeof(struct allocExtDesc));
173ff116fc8SJan Kara 			mark_buffer_dirty_inode(epos.bh, inode);
1741da177e4SLinus Torvalds 		}
1751da177e4SLinus Torvalds 	}
17674584ae5SJan Kara 	/* This inode entry is in-memory only and thus we don't have to mark
17774584ae5SJan Kara 	 * the inode dirty */
17848d6d8ffSMarcin Slusarz 	iinfo->i_lenExtents = lbcount;
1793bf25cb4SJan Kara 	brelse(epos.bh);
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds 
182456390deSmarcin.slusarz@gmail.com static void udf_update_alloc_ext_desc(struct inode *inode,
183456390deSmarcin.slusarz@gmail.com 				      struct extent_position *epos,
184456390deSmarcin.slusarz@gmail.com 				      u32 lenalloc)
185456390deSmarcin.slusarz@gmail.com {
186456390deSmarcin.slusarz@gmail.com 	struct super_block *sb = inode->i_sb;
187456390deSmarcin.slusarz@gmail.com 	struct udf_sb_info *sbi = UDF_SB(sb);
188456390deSmarcin.slusarz@gmail.com 
189456390deSmarcin.slusarz@gmail.com 	struct allocExtDesc *aed = (struct allocExtDesc *) (epos->bh->b_data);
190456390deSmarcin.slusarz@gmail.com 	int len = sizeof(struct allocExtDesc);
191456390deSmarcin.slusarz@gmail.com 
192456390deSmarcin.slusarz@gmail.com 	aed->lengthAllocDescs =	cpu_to_le32(lenalloc);
193456390deSmarcin.slusarz@gmail.com 	if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || sbi->s_udfrev >= 0x0201)
194456390deSmarcin.slusarz@gmail.com 		len += lenalloc;
195456390deSmarcin.slusarz@gmail.com 
196456390deSmarcin.slusarz@gmail.com 	udf_update_tag(epos->bh->b_data, len);
197456390deSmarcin.slusarz@gmail.com 	mark_buffer_dirty_inode(epos->bh, inode);
198456390deSmarcin.slusarz@gmail.com }
199456390deSmarcin.slusarz@gmail.com 
2007e49b6f2SJan Kara /*
2017e49b6f2SJan Kara  * Truncate extents of inode to inode->i_size. This function can be used only
2027e49b6f2SJan Kara  * for making file shorter. For making file longer, udf_extend_file() has to
2037e49b6f2SJan Kara  * be used.
2047e49b6f2SJan Kara  */
2051da177e4SLinus Torvalds void udf_truncate_extents(struct inode *inode)
2061da177e4SLinus Torvalds {
207ff116fc8SJan Kara 	struct extent_position epos;
2085ca4e4beSPekka Enberg 	struct kernel_lb_addr eloc, neloc = {};
209ff116fc8SJan Kara 	uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
2101da177e4SLinus Torvalds 	int8_t etype;
21131170b6aSJan Kara 	struct super_block *sb = inode->i_sb;
21231170b6aSJan Kara 	sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
21360448b1dSJan Kara 	loff_t byte_offset;
2141da177e4SLinus Torvalds 	int adsize;
21548d6d8ffSMarcin Slusarz 	struct udf_inode_info *iinfo = UDF_I(inode);
2161da177e4SLinus Torvalds 
21748d6d8ffSMarcin Slusarz 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2185ca4e4beSPekka Enberg 		adsize = sizeof(struct short_ad);
21948d6d8ffSMarcin Slusarz 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2205ca4e4beSPekka Enberg 		adsize = sizeof(struct long_ad);
2211da177e4SLinus Torvalds 	else
222ff116fc8SJan Kara 		BUG();
2231da177e4SLinus Torvalds 
224ff116fc8SJan Kara 	etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
22528de7948SCyrill Gorcunov 	byte_offset = (offset << sb->s_blocksize_bits) +
226cb00ea35SCyrill Gorcunov 		(inode->i_size & (sb->s_blocksize - 1));
2277e49b6f2SJan Kara 	if (etype == -1) {
2287e49b6f2SJan Kara 		/* We should extend the file? */
2297e49b6f2SJan Kara 		WARN_ON(byte_offset);
2307e49b6f2SJan Kara 		return;
2317e49b6f2SJan Kara 	}
232ff116fc8SJan Kara 	epos.offset -= adsize;
23397e961fdSPekka Enberg 	extent_trunc(inode, &epos, &eloc, etype, elen, byte_offset);
234ff116fc8SJan Kara 	epos.offset += adsize;
23560448b1dSJan Kara 	if (byte_offset)
236ff116fc8SJan Kara 		lenalloc = epos.offset;
2371da177e4SLinus Torvalds 	else
238ff116fc8SJan Kara 		lenalloc = epos.offset - adsize;
2391da177e4SLinus Torvalds 
240ff116fc8SJan Kara 	if (!epos.bh)
2411da177e4SLinus Torvalds 		lenalloc -= udf_file_entry_alloc_offset(inode);
2421da177e4SLinus Torvalds 	else
2431da177e4SLinus Torvalds 		lenalloc -= sizeof(struct allocExtDesc);
2441da177e4SLinus Torvalds 
2454b11111aSMarcin Slusarz 	while ((etype = udf_current_aext(inode, &epos, &eloc,
2464b11111aSMarcin Slusarz 					 &elen, 0)) != -1) {
247cb00ea35SCyrill Gorcunov 		if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
24897e961fdSPekka Enberg 			udf_write_aext(inode, &epos, &neloc, nelen, 0);
249cb00ea35SCyrill Gorcunov 			if (indirect_ext_len) {
250ff116fc8SJan Kara 				/* We managed to free all extents in the
251ff116fc8SJan Kara 				 * indirect extent - free it too */
2529de90b76Smarcin.slusarz@gmail.com 				BUG_ON(!epos.bh);
25397e961fdSPekka Enberg 				udf_free_blocks(sb, inode, &epos.block,
254cb00ea35SCyrill Gorcunov 						0, indirect_ext_len);
2559de90b76Smarcin.slusarz@gmail.com 			} else if (!epos.bh) {
2569de90b76Smarcin.slusarz@gmail.com 				iinfo->i_lenAlloc = lenalloc;
2571da177e4SLinus Torvalds 				mark_inode_dirty(inode);
258456390deSmarcin.slusarz@gmail.com 			} else
259456390deSmarcin.slusarz@gmail.com 				udf_update_alloc_ext_desc(inode,
260456390deSmarcin.slusarz@gmail.com 						&epos, lenalloc);
261ff116fc8SJan Kara 			brelse(epos.bh);
262ff116fc8SJan Kara 			epos.offset = sizeof(struct allocExtDesc);
263ff116fc8SJan Kara 			epos.block = eloc;
2644b11111aSMarcin Slusarz 			epos.bh = udf_tread(sb,
26597e961fdSPekka Enberg 					udf_get_lb_pblock(sb, &eloc, 0));
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);
2807e49b6f2SJan Kara 		udf_free_blocks(sb, inode, &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);
2891da177e4SLinus Torvalds }
290