Linux Audio

Check our new training course

Linux kernel drivers training

May 6-19, 2025
Register
Loading...
v4.17
  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
 29static 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 */
 67void 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
121void udf_discard_prealloc(struct inode *inode)
122{
123	struct extent_position epos = { NULL, 0, {0, 0} };
124	struct kernel_lb_addr eloc;
125	uint32_t elen;
126	uint64_t lbcount = 0;
127	int8_t etype = -1, netype;
128	int adsize;
129	struct udf_inode_info *iinfo = UDF_I(inode);
130
131	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
132	    inode->i_size == iinfo->i_lenExtents)
133		return;
134
135	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
136		adsize = sizeof(struct short_ad);
137	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
138		adsize = sizeof(struct long_ad);
139	else
140		adsize = 0;
141
142	epos.block = iinfo->i_location;
143
144	/* Find the last extent in the file */
145	while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
146		etype = netype;
147		lbcount += elen;
148	}
149	if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
150		epos.offset -= adsize;
151		lbcount -= elen;
152		extent_trunc(inode, &epos, &eloc, etype, elen, 0);
153		if (!epos.bh) {
154			iinfo->i_lenAlloc =
155				epos.offset -
156				udf_file_entry_alloc_offset(inode);
157			mark_inode_dirty(inode);
158		} else {
159			struct allocExtDesc *aed =
160				(struct allocExtDesc *)(epos.bh->b_data);
161			aed->lengthAllocDescs =
162				cpu_to_le32(epos.offset -
163					    sizeof(struct allocExtDesc));
164			if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
165			    UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
166				udf_update_tag(epos.bh->b_data, epos.offset);
167			else
168				udf_update_tag(epos.bh->b_data,
169					       sizeof(struct allocExtDesc));
170			mark_buffer_dirty_inode(epos.bh, inode);
171		}
172	}
173	/* This inode entry is in-memory only and thus we don't have to mark
174	 * the inode dirty */
175	iinfo->i_lenExtents = lbcount;
176	brelse(epos.bh);
177}
178
179static void udf_update_alloc_ext_desc(struct inode *inode,
180				      struct extent_position *epos,
181				      u32 lenalloc)
182{
183	struct super_block *sb = inode->i_sb;
184	struct udf_sb_info *sbi = UDF_SB(sb);
185
186	struct allocExtDesc *aed = (struct allocExtDesc *) (epos->bh->b_data);
187	int len = sizeof(struct allocExtDesc);
188
189	aed->lengthAllocDescs =	cpu_to_le32(lenalloc);
190	if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || sbi->s_udfrev >= 0x0201)
191		len += lenalloc;
192
193	udf_update_tag(epos->bh->b_data, len);
194	mark_buffer_dirty_inode(epos->bh, inode);
195}
196
197/*
198 * Truncate extents of inode to inode->i_size. This function can be used only
199 * for making file shorter. For making file longer, udf_extend_file() has to
200 * be used.
201 */
202void udf_truncate_extents(struct inode *inode)
203{
204	struct extent_position epos;
205	struct kernel_lb_addr eloc, neloc = {};
206	uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
207	int8_t etype;
208	struct super_block *sb = inode->i_sb;
209	sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
210	loff_t byte_offset;
211	int adsize;
212	struct udf_inode_info *iinfo = UDF_I(inode);
213
214	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
215		adsize = sizeof(struct short_ad);
216	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
217		adsize = sizeof(struct long_ad);
218	else
219		BUG();
220
221	etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
222	byte_offset = (offset << sb->s_blocksize_bits) +
223		(inode->i_size & (sb->s_blocksize - 1));
224	if (etype == -1) {
225		/* We should extend the file? */
226		WARN_ON(byte_offset);
227		return;
228	}
229	epos.offset -= adsize;
230	extent_trunc(inode, &epos, &eloc, etype, elen, byte_offset);
231	epos.offset += adsize;
232	if (byte_offset)
233		lenalloc = epos.offset;
234	else
235		lenalloc = epos.offset - adsize;
236
237	if (!epos.bh)
238		lenalloc -= udf_file_entry_alloc_offset(inode);
239	else
240		lenalloc -= sizeof(struct allocExtDesc);
241
242	while ((etype = udf_current_aext(inode, &epos, &eloc,
243					 &elen, 0)) != -1) {
244		if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
245			udf_write_aext(inode, &epos, &neloc, nelen, 0);
246			if (indirect_ext_len) {
247				/* We managed to free all extents in the
248				 * indirect extent - free it too */
249				BUG_ON(!epos.bh);
250				udf_free_blocks(sb, NULL, &epos.block,
251						0, indirect_ext_len);
252			} else if (!epos.bh) {
253				iinfo->i_lenAlloc = lenalloc;
254				mark_inode_dirty(inode);
255			} else
256				udf_update_alloc_ext_desc(inode,
257						&epos, lenalloc);
258			brelse(epos.bh);
259			epos.offset = sizeof(struct allocExtDesc);
260			epos.block = eloc;
261			epos.bh = udf_tread(sb,
262					udf_get_lb_pblock(sb, &eloc, 0));
263			if (elen)
264				indirect_ext_len =
265					(elen + sb->s_blocksize - 1) >>
266					sb->s_blocksize_bits;
267			else
268				indirect_ext_len = 1;
269		} else {
270			extent_trunc(inode, &epos, &eloc, etype, elen, 0);
271			epos.offset += adsize;
272		}
273	}
274
275	if (indirect_ext_len) {
276		BUG_ON(!epos.bh);
277		udf_free_blocks(sb, NULL, &epos.block, 0, indirect_ext_len);
278	} else if (!epos.bh) {
279		iinfo->i_lenAlloc = lenalloc;
280		mark_inode_dirty(inode);
281	} else
282		udf_update_alloc_ext_desc(inode, &epos, lenalloc);
283	iinfo->i_lenExtents = inode->i_size;
284
285	brelse(epos.bh);
286}
v3.1
  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#include <linux/buffer_head.h>
 26
 27#include "udf_i.h"
 28#include "udf_sb.h"
 29
 30static void extent_trunc(struct inode *inode, struct extent_position *epos,
 31			 struct kernel_lb_addr *eloc, int8_t etype, uint32_t elen,
 32			 uint32_t nelen)
 33{
 34	struct kernel_lb_addr neloc = {};
 35	int last_block = (elen + inode->i_sb->s_blocksize - 1) >>
 36		inode->i_sb->s_blocksize_bits;
 37	int first_block = (nelen + inode->i_sb->s_blocksize - 1) >>
 38		inode->i_sb->s_blocksize_bits;
 39
 40	if (nelen) {
 41		if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
 42			udf_free_blocks(inode->i_sb, inode, eloc, 0,
 43					last_block);
 44			etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
 45		} else
 46			neloc = *eloc;
 47		nelen = (etype << 30) | nelen;
 48	}
 49
 50	if (elen != nelen) {
 51		udf_write_aext(inode, epos, &neloc, nelen, 0);
 52		if (last_block - first_block > 0) {
 53			if (etype == (EXT_RECORDED_ALLOCATED >> 30))
 54				mark_inode_dirty(inode);
 55
 56			if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
 57				udf_free_blocks(inode->i_sb, inode, eloc,
 58						first_block,
 59						last_block - first_block);
 60		}
 61	}
 62}
 63
 64/*
 65 * Truncate the last extent to match i_size. This function assumes
 66 * that preallocation extent is already truncated.
 67 */
 68void udf_truncate_tail_extent(struct inode *inode)
 69{
 70	struct extent_position epos = {};
 71	struct kernel_lb_addr eloc;
 72	uint32_t elen, nelen;
 73	uint64_t lbcount = 0;
 74	int8_t etype = -1, netype;
 75	int adsize;
 76	struct udf_inode_info *iinfo = UDF_I(inode);
 77
 78	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
 79	    inode->i_size == iinfo->i_lenExtents)
 80		return;
 81	/* Are we going to delete the file anyway? */
 82	if (inode->i_nlink == 0)
 83		return;
 84
 85	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
 86		adsize = sizeof(struct short_ad);
 87	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
 88		adsize = sizeof(struct long_ad);
 89	else
 90		BUG();
 91
 92	/* Find the last extent in the file */
 93	while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
 94		etype = netype;
 95		lbcount += elen;
 96		if (lbcount > inode->i_size) {
 97			if (lbcount - inode->i_size >= inode->i_sb->s_blocksize)
 98				printk(KERN_WARNING
 99				       "udf_truncate_tail_extent(): Too long "
100				       "extent after EOF in inode %u: i_size: "
101				       "%Ld lbcount: %Ld extent %u+%u\n",
102				       (unsigned)inode->i_ino,
103				       (long long)inode->i_size,
104				       (long long)lbcount,
105				       (unsigned)eloc.logicalBlockNum,
106				       (unsigned)elen);
107			nelen = elen - (lbcount - inode->i_size);
108			epos.offset -= adsize;
109			extent_trunc(inode, &epos, &eloc, etype, elen, nelen);
110			epos.offset += adsize;
111			if (udf_next_aext(inode, &epos, &eloc, &elen, 1) != -1)
112				printk(KERN_ERR "udf_truncate_tail_extent(): "
113				       "Extent after EOF in inode %u.\n",
114				       (unsigned)inode->i_ino);
115			break;
116		}
117	}
118	/* This inode entry is in-memory only and thus we don't have to mark
119	 * the inode dirty */
120	iinfo->i_lenExtents = inode->i_size;
121	brelse(epos.bh);
122}
123
124void udf_discard_prealloc(struct inode *inode)
125{
126	struct extent_position epos = { NULL, 0, {0, 0} };
127	struct kernel_lb_addr eloc;
128	uint32_t elen;
129	uint64_t lbcount = 0;
130	int8_t etype = -1, netype;
131	int adsize;
132	struct udf_inode_info *iinfo = UDF_I(inode);
133
134	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
135	    inode->i_size == iinfo->i_lenExtents)
136		return;
137
138	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
139		adsize = sizeof(struct short_ad);
140	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
141		adsize = sizeof(struct long_ad);
142	else
143		adsize = 0;
144
145	epos.block = iinfo->i_location;
146
147	/* Find the last extent in the file */
148	while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
149		etype = netype;
150		lbcount += elen;
151	}
152	if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
153		epos.offset -= adsize;
154		lbcount -= elen;
155		extent_trunc(inode, &epos, &eloc, etype, elen, 0);
156		if (!epos.bh) {
157			iinfo->i_lenAlloc =
158				epos.offset -
159				udf_file_entry_alloc_offset(inode);
160			mark_inode_dirty(inode);
161		} else {
162			struct allocExtDesc *aed =
163				(struct allocExtDesc *)(epos.bh->b_data);
164			aed->lengthAllocDescs =
165				cpu_to_le32(epos.offset -
166					    sizeof(struct allocExtDesc));
167			if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
168			    UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
169				udf_update_tag(epos.bh->b_data, epos.offset);
170			else
171				udf_update_tag(epos.bh->b_data,
172					       sizeof(struct allocExtDesc));
173			mark_buffer_dirty_inode(epos.bh, inode);
174		}
175	}
176	/* This inode entry is in-memory only and thus we don't have to mark
177	 * the inode dirty */
178	iinfo->i_lenExtents = lbcount;
179	brelse(epos.bh);
180}
181
182static void udf_update_alloc_ext_desc(struct inode *inode,
183				      struct extent_position *epos,
184				      u32 lenalloc)
185{
186	struct super_block *sb = inode->i_sb;
187	struct udf_sb_info *sbi = UDF_SB(sb);
188
189	struct allocExtDesc *aed = (struct allocExtDesc *) (epos->bh->b_data);
190	int len = sizeof(struct allocExtDesc);
191
192	aed->lengthAllocDescs =	cpu_to_le32(lenalloc);
193	if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || sbi->s_udfrev >= 0x0201)
194		len += lenalloc;
195
196	udf_update_tag(epos->bh->b_data, len);
197	mark_buffer_dirty_inode(epos->bh, inode);
198}
199
200/*
201 * Truncate extents of inode to inode->i_size. This function can be used only
202 * for making file shorter. For making file longer, udf_extend_file() has to
203 * be used.
204 */
205void udf_truncate_extents(struct inode *inode)
206{
207	struct extent_position epos;
208	struct kernel_lb_addr eloc, neloc = {};
209	uint32_t elen, nelen = 0, indirect_ext_len = 0, lenalloc;
210	int8_t etype;
211	struct super_block *sb = inode->i_sb;
212	sector_t first_block = inode->i_size >> sb->s_blocksize_bits, offset;
213	loff_t byte_offset;
214	int adsize;
215	struct udf_inode_info *iinfo = UDF_I(inode);
216
217	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
218		adsize = sizeof(struct short_ad);
219	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
220		adsize = sizeof(struct long_ad);
221	else
222		BUG();
223
224	etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
225	byte_offset = (offset << sb->s_blocksize_bits) +
226		(inode->i_size & (sb->s_blocksize - 1));
227	if (etype == -1) {
228		/* We should extend the file? */
229		WARN_ON(byte_offset);
230		return;
231	}
232	epos.offset -= adsize;
233	extent_trunc(inode, &epos, &eloc, etype, elen, byte_offset);
234	epos.offset += adsize;
235	if (byte_offset)
236		lenalloc = epos.offset;
237	else
238		lenalloc = epos.offset - adsize;
239
240	if (!epos.bh)
241		lenalloc -= udf_file_entry_alloc_offset(inode);
242	else
243		lenalloc -= sizeof(struct allocExtDesc);
244
245	while ((etype = udf_current_aext(inode, &epos, &eloc,
246					 &elen, 0)) != -1) {
247		if (etype == (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
248			udf_write_aext(inode, &epos, &neloc, nelen, 0);
249			if (indirect_ext_len) {
250				/* We managed to free all extents in the
251				 * indirect extent - free it too */
252				BUG_ON(!epos.bh);
253				udf_free_blocks(sb, inode, &epos.block,
254						0, indirect_ext_len);
255			} else if (!epos.bh) {
256				iinfo->i_lenAlloc = lenalloc;
257				mark_inode_dirty(inode);
258			} else
259				udf_update_alloc_ext_desc(inode,
260						&epos, lenalloc);
261			brelse(epos.bh);
262			epos.offset = sizeof(struct allocExtDesc);
263			epos.block = eloc;
264			epos.bh = udf_tread(sb,
265					udf_get_lb_pblock(sb, &eloc, 0));
266			if (elen)
267				indirect_ext_len =
268					(elen + sb->s_blocksize - 1) >>
269					sb->s_blocksize_bits;
270			else
271				indirect_ext_len = 1;
272		} else {
273			extent_trunc(inode, &epos, &eloc, etype, elen, 0);
274			epos.offset += adsize;
275		}
276	}
277
278	if (indirect_ext_len) {
279		BUG_ON(!epos.bh);
280		udf_free_blocks(sb, inode, &epos.block, 0, indirect_ext_len);
281	} else if (!epos.bh) {
282		iinfo->i_lenAlloc = lenalloc;
283		mark_inode_dirty(inode);
284	} else
285		udf_update_alloc_ext_desc(inode, &epos, lenalloc);
286	iinfo->i_lenExtents = inode->i_size;
287
288	brelse(epos.bh);
289}