xref: /openbmc/linux/fs/udf/inode.c (revision 77a87824)
1 /*
2  * inode.c
3  *
4  * PURPOSE
5  *  Inode 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) 1998 Dave Boynton
14  *  (C) 1998-2004 Ben Fennema
15  *  (C) 1999-2000 Stelias Computing Inc
16  *
17  * HISTORY
18  *
19  *  10/04/98 dgb  Added rudimentary directory functions
20  *  10/07/98      Fully working udf_block_map! It works!
21  *  11/25/98      bmap altered to better support extents
22  *  12/06/98 blf  partition support in udf_iget, udf_block_map
23  *                and udf_read_inode
24  *  12/12/98      rewrote udf_block_map to handle next extents and descs across
25  *                block boundaries (which is not actually allowed)
26  *  12/20/98      added support for strategy 4096
27  *  03/07/99      rewrote udf_block_map (again)
28  *                New funcs, inode_bmap, udf_next_aext
29  *  04/19/99      Support for writing device EA's for major/minor #
30  */
31 
32 #include "udfdecl.h"
33 #include <linux/mm.h>
34 #include <linux/module.h>
35 #include <linux/pagemap.h>
36 #include <linux/writeback.h>
37 #include <linux/slab.h>
38 #include <linux/crc-itu-t.h>
39 #include <linux/mpage.h>
40 #include <linux/uio.h>
41 
42 #include "udf_i.h"
43 #include "udf_sb.h"
44 
45 MODULE_AUTHOR("Ben Fennema");
46 MODULE_DESCRIPTION("Universal Disk Format Filesystem");
47 MODULE_LICENSE("GPL");
48 
49 #define EXTENT_MERGE_SIZE 5
50 
51 static umode_t udf_convert_permissions(struct fileEntry *);
52 static int udf_update_inode(struct inode *, int);
53 static int udf_sync_inode(struct inode *inode);
54 static int udf_alloc_i_data(struct inode *inode, size_t size);
55 static sector_t inode_getblk(struct inode *, sector_t, int *, int *);
56 static int8_t udf_insert_aext(struct inode *, struct extent_position,
57 			      struct kernel_lb_addr, uint32_t);
58 static void udf_split_extents(struct inode *, int *, int, int,
59 			      struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
60 static void udf_prealloc_extents(struct inode *, int, int,
61 				 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
62 static void udf_merge_extents(struct inode *,
63 			      struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
64 static void udf_update_extents(struct inode *,
65 			       struct kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
66 			       struct extent_position *);
67 static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
68 
69 static void __udf_clear_extent_cache(struct inode *inode)
70 {
71 	struct udf_inode_info *iinfo = UDF_I(inode);
72 
73 	if (iinfo->cached_extent.lstart != -1) {
74 		brelse(iinfo->cached_extent.epos.bh);
75 		iinfo->cached_extent.lstart = -1;
76 	}
77 }
78 
79 /* Invalidate extent cache */
80 static void udf_clear_extent_cache(struct inode *inode)
81 {
82 	struct udf_inode_info *iinfo = UDF_I(inode);
83 
84 	spin_lock(&iinfo->i_extent_cache_lock);
85 	__udf_clear_extent_cache(inode);
86 	spin_unlock(&iinfo->i_extent_cache_lock);
87 }
88 
89 /* Return contents of extent cache */
90 static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
91 				 loff_t *lbcount, struct extent_position *pos)
92 {
93 	struct udf_inode_info *iinfo = UDF_I(inode);
94 	int ret = 0;
95 
96 	spin_lock(&iinfo->i_extent_cache_lock);
97 	if ((iinfo->cached_extent.lstart <= bcount) &&
98 	    (iinfo->cached_extent.lstart != -1)) {
99 		/* Cache hit */
100 		*lbcount = iinfo->cached_extent.lstart;
101 		memcpy(pos, &iinfo->cached_extent.epos,
102 		       sizeof(struct extent_position));
103 		if (pos->bh)
104 			get_bh(pos->bh);
105 		ret = 1;
106 	}
107 	spin_unlock(&iinfo->i_extent_cache_lock);
108 	return ret;
109 }
110 
111 /* Add extent to extent cache */
112 static void udf_update_extent_cache(struct inode *inode, loff_t estart,
113 				    struct extent_position *pos, int next_epos)
114 {
115 	struct udf_inode_info *iinfo = UDF_I(inode);
116 
117 	spin_lock(&iinfo->i_extent_cache_lock);
118 	/* Invalidate previously cached extent */
119 	__udf_clear_extent_cache(inode);
120 	if (pos->bh)
121 		get_bh(pos->bh);
122 	memcpy(&iinfo->cached_extent.epos, pos,
123 	       sizeof(struct extent_position));
124 	iinfo->cached_extent.lstart = estart;
125 	if (next_epos)
126 		switch (iinfo->i_alloc_type) {
127 		case ICBTAG_FLAG_AD_SHORT:
128 			iinfo->cached_extent.epos.offset -=
129 			sizeof(struct short_ad);
130 			break;
131 		case ICBTAG_FLAG_AD_LONG:
132 			iinfo->cached_extent.epos.offset -=
133 			sizeof(struct long_ad);
134 		}
135 	spin_unlock(&iinfo->i_extent_cache_lock);
136 }
137 
138 void udf_evict_inode(struct inode *inode)
139 {
140 	struct udf_inode_info *iinfo = UDF_I(inode);
141 	int want_delete = 0;
142 
143 	if (!inode->i_nlink && !is_bad_inode(inode)) {
144 		want_delete = 1;
145 		udf_setsize(inode, 0);
146 		udf_update_inode(inode, IS_SYNC(inode));
147 	}
148 	truncate_inode_pages_final(&inode->i_data);
149 	invalidate_inode_buffers(inode);
150 	clear_inode(inode);
151 	if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
152 	    inode->i_size != iinfo->i_lenExtents) {
153 		udf_warn(inode->i_sb, "Inode %lu (mode %o) has inode size %llu different from extent length %llu. Filesystem need not be standards compliant.\n",
154 			 inode->i_ino, inode->i_mode,
155 			 (unsigned long long)inode->i_size,
156 			 (unsigned long long)iinfo->i_lenExtents);
157 	}
158 	kfree(iinfo->i_ext.i_data);
159 	iinfo->i_ext.i_data = NULL;
160 	udf_clear_extent_cache(inode);
161 	if (want_delete) {
162 		udf_free_inode(inode);
163 	}
164 }
165 
166 static void udf_write_failed(struct address_space *mapping, loff_t to)
167 {
168 	struct inode *inode = mapping->host;
169 	struct udf_inode_info *iinfo = UDF_I(inode);
170 	loff_t isize = inode->i_size;
171 
172 	if (to > isize) {
173 		truncate_pagecache(inode, isize);
174 		if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
175 			down_write(&iinfo->i_data_sem);
176 			udf_clear_extent_cache(inode);
177 			udf_truncate_extents(inode);
178 			up_write(&iinfo->i_data_sem);
179 		}
180 	}
181 }
182 
183 static int udf_writepage(struct page *page, struct writeback_control *wbc)
184 {
185 	return block_write_full_page(page, udf_get_block, wbc);
186 }
187 
188 static int udf_writepages(struct address_space *mapping,
189 			struct writeback_control *wbc)
190 {
191 	return mpage_writepages(mapping, wbc, udf_get_block);
192 }
193 
194 static int udf_readpage(struct file *file, struct page *page)
195 {
196 	return mpage_readpage(page, udf_get_block);
197 }
198 
199 static int udf_readpages(struct file *file, struct address_space *mapping,
200 			struct list_head *pages, unsigned nr_pages)
201 {
202 	return mpage_readpages(mapping, pages, nr_pages, udf_get_block);
203 }
204 
205 static int udf_write_begin(struct file *file, struct address_space *mapping,
206 			loff_t pos, unsigned len, unsigned flags,
207 			struct page **pagep, void **fsdata)
208 {
209 	int ret;
210 
211 	ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
212 	if (unlikely(ret))
213 		udf_write_failed(mapping, pos + len);
214 	return ret;
215 }
216 
217 static ssize_t udf_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
218 {
219 	struct file *file = iocb->ki_filp;
220 	struct address_space *mapping = file->f_mapping;
221 	struct inode *inode = mapping->host;
222 	size_t count = iov_iter_count(iter);
223 	ssize_t ret;
224 
225 	ret = blockdev_direct_IO(iocb, inode, iter, udf_get_block);
226 	if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE))
227 		udf_write_failed(mapping, iocb->ki_pos + count);
228 	return ret;
229 }
230 
231 static sector_t udf_bmap(struct address_space *mapping, sector_t block)
232 {
233 	return generic_block_bmap(mapping, block, udf_get_block);
234 }
235 
236 const struct address_space_operations udf_aops = {
237 	.readpage	= udf_readpage,
238 	.readpages	= udf_readpages,
239 	.writepage	= udf_writepage,
240 	.writepages	= udf_writepages,
241 	.write_begin	= udf_write_begin,
242 	.write_end	= generic_write_end,
243 	.direct_IO	= udf_direct_IO,
244 	.bmap		= udf_bmap,
245 };
246 
247 /*
248  * Expand file stored in ICB to a normal one-block-file
249  *
250  * This function requires i_data_sem for writing and releases it.
251  * This function requires i_mutex held
252  */
253 int udf_expand_file_adinicb(struct inode *inode)
254 {
255 	struct page *page;
256 	char *kaddr;
257 	struct udf_inode_info *iinfo = UDF_I(inode);
258 	int err;
259 	struct writeback_control udf_wbc = {
260 		.sync_mode = WB_SYNC_NONE,
261 		.nr_to_write = 1,
262 	};
263 
264 	WARN_ON_ONCE(!inode_is_locked(inode));
265 	if (!iinfo->i_lenAlloc) {
266 		if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
267 			iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
268 		else
269 			iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
270 		/* from now on we have normal address_space methods */
271 		inode->i_data.a_ops = &udf_aops;
272 		up_write(&iinfo->i_data_sem);
273 		mark_inode_dirty(inode);
274 		return 0;
275 	}
276 	/*
277 	 * Release i_data_sem so that we can lock a page - page lock ranks
278 	 * above i_data_sem. i_mutex still protects us against file changes.
279 	 */
280 	up_write(&iinfo->i_data_sem);
281 
282 	page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
283 	if (!page)
284 		return -ENOMEM;
285 
286 	if (!PageUptodate(page)) {
287 		kaddr = kmap(page);
288 		memset(kaddr + iinfo->i_lenAlloc, 0x00,
289 		       PAGE_SIZE - iinfo->i_lenAlloc);
290 		memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
291 			iinfo->i_lenAlloc);
292 		flush_dcache_page(page);
293 		SetPageUptodate(page);
294 		kunmap(page);
295 	}
296 	down_write(&iinfo->i_data_sem);
297 	memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
298 	       iinfo->i_lenAlloc);
299 	iinfo->i_lenAlloc = 0;
300 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
301 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
302 	else
303 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
304 	/* from now on we have normal address_space methods */
305 	inode->i_data.a_ops = &udf_aops;
306 	up_write(&iinfo->i_data_sem);
307 	err = inode->i_data.a_ops->writepage(page, &udf_wbc);
308 	if (err) {
309 		/* Restore everything back so that we don't lose data... */
310 		lock_page(page);
311 		kaddr = kmap(page);
312 		down_write(&iinfo->i_data_sem);
313 		memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr,
314 		       inode->i_size);
315 		kunmap(page);
316 		unlock_page(page);
317 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
318 		inode->i_data.a_ops = &udf_adinicb_aops;
319 		up_write(&iinfo->i_data_sem);
320 	}
321 	put_page(page);
322 	mark_inode_dirty(inode);
323 
324 	return err;
325 }
326 
327 struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
328 					   int *err)
329 {
330 	int newblock;
331 	struct buffer_head *dbh = NULL;
332 	struct kernel_lb_addr eloc;
333 	uint8_t alloctype;
334 	struct extent_position epos;
335 
336 	struct udf_fileident_bh sfibh, dfibh;
337 	loff_t f_pos = udf_ext0_offset(inode);
338 	int size = udf_ext0_offset(inode) + inode->i_size;
339 	struct fileIdentDesc cfi, *sfi, *dfi;
340 	struct udf_inode_info *iinfo = UDF_I(inode);
341 
342 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
343 		alloctype = ICBTAG_FLAG_AD_SHORT;
344 	else
345 		alloctype = ICBTAG_FLAG_AD_LONG;
346 
347 	if (!inode->i_size) {
348 		iinfo->i_alloc_type = alloctype;
349 		mark_inode_dirty(inode);
350 		return NULL;
351 	}
352 
353 	/* alloc block, and copy data to it */
354 	*block = udf_new_block(inode->i_sb, inode,
355 			       iinfo->i_location.partitionReferenceNum,
356 			       iinfo->i_location.logicalBlockNum, err);
357 	if (!(*block))
358 		return NULL;
359 	newblock = udf_get_pblock(inode->i_sb, *block,
360 				  iinfo->i_location.partitionReferenceNum,
361 				0);
362 	if (!newblock)
363 		return NULL;
364 	dbh = udf_tgetblk(inode->i_sb, newblock);
365 	if (!dbh)
366 		return NULL;
367 	lock_buffer(dbh);
368 	memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
369 	set_buffer_uptodate(dbh);
370 	unlock_buffer(dbh);
371 	mark_buffer_dirty_inode(dbh, inode);
372 
373 	sfibh.soffset = sfibh.eoffset =
374 			f_pos & (inode->i_sb->s_blocksize - 1);
375 	sfibh.sbh = sfibh.ebh = NULL;
376 	dfibh.soffset = dfibh.eoffset = 0;
377 	dfibh.sbh = dfibh.ebh = dbh;
378 	while (f_pos < size) {
379 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
380 		sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
381 					 NULL, NULL, NULL);
382 		if (!sfi) {
383 			brelse(dbh);
384 			return NULL;
385 		}
386 		iinfo->i_alloc_type = alloctype;
387 		sfi->descTag.tagLocation = cpu_to_le32(*block);
388 		dfibh.soffset = dfibh.eoffset;
389 		dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
390 		dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
391 		if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
392 				 sfi->fileIdent +
393 					le16_to_cpu(sfi->lengthOfImpUse))) {
394 			iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
395 			brelse(dbh);
396 			return NULL;
397 		}
398 	}
399 	mark_buffer_dirty_inode(dbh, inode);
400 
401 	memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
402 		iinfo->i_lenAlloc);
403 	iinfo->i_lenAlloc = 0;
404 	eloc.logicalBlockNum = *block;
405 	eloc.partitionReferenceNum =
406 				iinfo->i_location.partitionReferenceNum;
407 	iinfo->i_lenExtents = inode->i_size;
408 	epos.bh = NULL;
409 	epos.block = iinfo->i_location;
410 	epos.offset = udf_file_entry_alloc_offset(inode);
411 	udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
412 	/* UniqueID stuff */
413 
414 	brelse(epos.bh);
415 	mark_inode_dirty(inode);
416 	return dbh;
417 }
418 
419 static int udf_get_block(struct inode *inode, sector_t block,
420 			 struct buffer_head *bh_result, int create)
421 {
422 	int err, new;
423 	sector_t phys = 0;
424 	struct udf_inode_info *iinfo;
425 
426 	if (!create) {
427 		phys = udf_block_map(inode, block);
428 		if (phys)
429 			map_bh(bh_result, inode->i_sb, phys);
430 		return 0;
431 	}
432 
433 	err = -EIO;
434 	new = 0;
435 	iinfo = UDF_I(inode);
436 
437 	down_write(&iinfo->i_data_sem);
438 	if (block == iinfo->i_next_alloc_block + 1) {
439 		iinfo->i_next_alloc_block++;
440 		iinfo->i_next_alloc_goal++;
441 	}
442 
443 	udf_clear_extent_cache(inode);
444 	phys = inode_getblk(inode, block, &err, &new);
445 	if (!phys)
446 		goto abort;
447 
448 	if (new)
449 		set_buffer_new(bh_result);
450 	map_bh(bh_result, inode->i_sb, phys);
451 
452 abort:
453 	up_write(&iinfo->i_data_sem);
454 	return err;
455 }
456 
457 static struct buffer_head *udf_getblk(struct inode *inode, long block,
458 				      int create, int *err)
459 {
460 	struct buffer_head *bh;
461 	struct buffer_head dummy;
462 
463 	dummy.b_state = 0;
464 	dummy.b_blocknr = -1000;
465 	*err = udf_get_block(inode, block, &dummy, create);
466 	if (!*err && buffer_mapped(&dummy)) {
467 		bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
468 		if (buffer_new(&dummy)) {
469 			lock_buffer(bh);
470 			memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
471 			set_buffer_uptodate(bh);
472 			unlock_buffer(bh);
473 			mark_buffer_dirty_inode(bh, inode);
474 		}
475 		return bh;
476 	}
477 
478 	return NULL;
479 }
480 
481 /* Extend the file by 'blocks' blocks, return the number of extents added */
482 static int udf_do_extend_file(struct inode *inode,
483 			      struct extent_position *last_pos,
484 			      struct kernel_long_ad *last_ext,
485 			      sector_t blocks)
486 {
487 	sector_t add;
488 	int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
489 	struct super_block *sb = inode->i_sb;
490 	struct kernel_lb_addr prealloc_loc = {};
491 	int prealloc_len = 0;
492 	struct udf_inode_info *iinfo;
493 	int err;
494 
495 	/* The previous extent is fake and we should not extend by anything
496 	 * - there's nothing to do... */
497 	if (!blocks && fake)
498 		return 0;
499 
500 	iinfo = UDF_I(inode);
501 	/* Round the last extent up to a multiple of block size */
502 	if (last_ext->extLength & (sb->s_blocksize - 1)) {
503 		last_ext->extLength =
504 			(last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
505 			(((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
506 			  sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
507 		iinfo->i_lenExtents =
508 			(iinfo->i_lenExtents + sb->s_blocksize - 1) &
509 			~(sb->s_blocksize - 1);
510 	}
511 
512 	/* Last extent are just preallocated blocks? */
513 	if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
514 						EXT_NOT_RECORDED_ALLOCATED) {
515 		/* Save the extent so that we can reattach it to the end */
516 		prealloc_loc = last_ext->extLocation;
517 		prealloc_len = last_ext->extLength;
518 		/* Mark the extent as a hole */
519 		last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
520 			(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
521 		last_ext->extLocation.logicalBlockNum = 0;
522 		last_ext->extLocation.partitionReferenceNum = 0;
523 	}
524 
525 	/* Can we merge with the previous extent? */
526 	if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
527 					EXT_NOT_RECORDED_NOT_ALLOCATED) {
528 		add = ((1 << 30) - sb->s_blocksize -
529 			(last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
530 			sb->s_blocksize_bits;
531 		if (add > blocks)
532 			add = blocks;
533 		blocks -= add;
534 		last_ext->extLength += add << sb->s_blocksize_bits;
535 	}
536 
537 	if (fake) {
538 		udf_add_aext(inode, last_pos, &last_ext->extLocation,
539 			     last_ext->extLength, 1);
540 		count++;
541 	} else {
542 		struct kernel_lb_addr tmploc;
543 		uint32_t tmplen;
544 
545 		udf_write_aext(inode, last_pos, &last_ext->extLocation,
546 				last_ext->extLength, 1);
547 		/*
548 		 * We've rewritten the last extent but there may be empty
549 		 * indirect extent after it - enter it.
550 		 */
551 		udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
552 	}
553 
554 	/* Managed to do everything necessary? */
555 	if (!blocks)
556 		goto out;
557 
558 	/* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
559 	last_ext->extLocation.logicalBlockNum = 0;
560 	last_ext->extLocation.partitionReferenceNum = 0;
561 	add = (1 << (30-sb->s_blocksize_bits)) - 1;
562 	last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
563 				(add << sb->s_blocksize_bits);
564 
565 	/* Create enough extents to cover the whole hole */
566 	while (blocks > add) {
567 		blocks -= add;
568 		err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
569 				   last_ext->extLength, 1);
570 		if (err)
571 			return err;
572 		count++;
573 	}
574 	if (blocks) {
575 		last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
576 			(blocks << sb->s_blocksize_bits);
577 		err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
578 				   last_ext->extLength, 1);
579 		if (err)
580 			return err;
581 		count++;
582 	}
583 
584 out:
585 	/* Do we have some preallocated blocks saved? */
586 	if (prealloc_len) {
587 		err = udf_add_aext(inode, last_pos, &prealloc_loc,
588 				   prealloc_len, 1);
589 		if (err)
590 			return err;
591 		last_ext->extLocation = prealloc_loc;
592 		last_ext->extLength = prealloc_len;
593 		count++;
594 	}
595 
596 	/* last_pos should point to the last written extent... */
597 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
598 		last_pos->offset -= sizeof(struct short_ad);
599 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
600 		last_pos->offset -= sizeof(struct long_ad);
601 	else
602 		return -EIO;
603 
604 	return count;
605 }
606 
607 static int udf_extend_file(struct inode *inode, loff_t newsize)
608 {
609 
610 	struct extent_position epos;
611 	struct kernel_lb_addr eloc;
612 	uint32_t elen;
613 	int8_t etype;
614 	struct super_block *sb = inode->i_sb;
615 	sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
616 	int adsize;
617 	struct udf_inode_info *iinfo = UDF_I(inode);
618 	struct kernel_long_ad extent;
619 	int err;
620 
621 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
622 		adsize = sizeof(struct short_ad);
623 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
624 		adsize = sizeof(struct long_ad);
625 	else
626 		BUG();
627 
628 	etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
629 
630 	/* File has extent covering the new size (could happen when extending
631 	 * inside a block)? */
632 	if (etype != -1)
633 		return 0;
634 	if (newsize & (sb->s_blocksize - 1))
635 		offset++;
636 	/* Extended file just to the boundary of the last file block? */
637 	if (offset == 0)
638 		return 0;
639 
640 	/* Truncate is extending the file by 'offset' blocks */
641 	if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
642 	    (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
643 		/* File has no extents at all or has empty last
644 		 * indirect extent! Create a fake extent... */
645 		extent.extLocation.logicalBlockNum = 0;
646 		extent.extLocation.partitionReferenceNum = 0;
647 		extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
648 	} else {
649 		epos.offset -= adsize;
650 		etype = udf_next_aext(inode, &epos, &extent.extLocation,
651 				      &extent.extLength, 0);
652 		extent.extLength |= etype << 30;
653 	}
654 	err = udf_do_extend_file(inode, &epos, &extent, offset);
655 	if (err < 0)
656 		goto out;
657 	err = 0;
658 	iinfo->i_lenExtents = newsize;
659 out:
660 	brelse(epos.bh);
661 	return err;
662 }
663 
664 static sector_t inode_getblk(struct inode *inode, sector_t block,
665 			     int *err, int *new)
666 {
667 	struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
668 	struct extent_position prev_epos, cur_epos, next_epos;
669 	int count = 0, startnum = 0, endnum = 0;
670 	uint32_t elen = 0, tmpelen;
671 	struct kernel_lb_addr eloc, tmpeloc;
672 	int c = 1;
673 	loff_t lbcount = 0, b_off = 0;
674 	uint32_t newblocknum, newblock;
675 	sector_t offset = 0;
676 	int8_t etype;
677 	struct udf_inode_info *iinfo = UDF_I(inode);
678 	int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
679 	int lastblock = 0;
680 	bool isBeyondEOF;
681 
682 	*err = 0;
683 	*new = 0;
684 	prev_epos.offset = udf_file_entry_alloc_offset(inode);
685 	prev_epos.block = iinfo->i_location;
686 	prev_epos.bh = NULL;
687 	cur_epos = next_epos = prev_epos;
688 	b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
689 
690 	/* find the extent which contains the block we are looking for.
691 	   alternate between laarr[0] and laarr[1] for locations of the
692 	   current extent, and the previous extent */
693 	do {
694 		if (prev_epos.bh != cur_epos.bh) {
695 			brelse(prev_epos.bh);
696 			get_bh(cur_epos.bh);
697 			prev_epos.bh = cur_epos.bh;
698 		}
699 		if (cur_epos.bh != next_epos.bh) {
700 			brelse(cur_epos.bh);
701 			get_bh(next_epos.bh);
702 			cur_epos.bh = next_epos.bh;
703 		}
704 
705 		lbcount += elen;
706 
707 		prev_epos.block = cur_epos.block;
708 		cur_epos.block = next_epos.block;
709 
710 		prev_epos.offset = cur_epos.offset;
711 		cur_epos.offset = next_epos.offset;
712 
713 		etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
714 		if (etype == -1)
715 			break;
716 
717 		c = !c;
718 
719 		laarr[c].extLength = (etype << 30) | elen;
720 		laarr[c].extLocation = eloc;
721 
722 		if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
723 			pgoal = eloc.logicalBlockNum +
724 				((elen + inode->i_sb->s_blocksize - 1) >>
725 				 inode->i_sb->s_blocksize_bits);
726 
727 		count++;
728 	} while (lbcount + elen <= b_off);
729 
730 	b_off -= lbcount;
731 	offset = b_off >> inode->i_sb->s_blocksize_bits;
732 	/*
733 	 * Move prev_epos and cur_epos into indirect extent if we are at
734 	 * the pointer to it
735 	 */
736 	udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
737 	udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
738 
739 	/* if the extent is allocated and recorded, return the block
740 	   if the extent is not a multiple of the blocksize, round up */
741 
742 	if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
743 		if (elen & (inode->i_sb->s_blocksize - 1)) {
744 			elen = EXT_RECORDED_ALLOCATED |
745 				((elen + inode->i_sb->s_blocksize - 1) &
746 				 ~(inode->i_sb->s_blocksize - 1));
747 			udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
748 		}
749 		brelse(prev_epos.bh);
750 		brelse(cur_epos.bh);
751 		brelse(next_epos.bh);
752 		newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
753 		return newblock;
754 	}
755 
756 	/* Are we beyond EOF? */
757 	if (etype == -1) {
758 		int ret;
759 		isBeyondEOF = true;
760 		if (count) {
761 			if (c)
762 				laarr[0] = laarr[1];
763 			startnum = 1;
764 		} else {
765 			/* Create a fake extent when there's not one */
766 			memset(&laarr[0].extLocation, 0x00,
767 				sizeof(struct kernel_lb_addr));
768 			laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
769 			/* Will udf_do_extend_file() create real extent from
770 			   a fake one? */
771 			startnum = (offset > 0);
772 		}
773 		/* Create extents for the hole between EOF and offset */
774 		ret = udf_do_extend_file(inode, &prev_epos, laarr, offset);
775 		if (ret < 0) {
776 			brelse(prev_epos.bh);
777 			brelse(cur_epos.bh);
778 			brelse(next_epos.bh);
779 			*err = ret;
780 			return 0;
781 		}
782 		c = 0;
783 		offset = 0;
784 		count += ret;
785 		/* We are not covered by a preallocated extent? */
786 		if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
787 						EXT_NOT_RECORDED_ALLOCATED) {
788 			/* Is there any real extent? - otherwise we overwrite
789 			 * the fake one... */
790 			if (count)
791 				c = !c;
792 			laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
793 				inode->i_sb->s_blocksize;
794 			memset(&laarr[c].extLocation, 0x00,
795 				sizeof(struct kernel_lb_addr));
796 			count++;
797 		}
798 		endnum = c + 1;
799 		lastblock = 1;
800 	} else {
801 		isBeyondEOF = false;
802 		endnum = startnum = ((count > 2) ? 2 : count);
803 
804 		/* if the current extent is in position 0,
805 		   swap it with the previous */
806 		if (!c && count != 1) {
807 			laarr[2] = laarr[0];
808 			laarr[0] = laarr[1];
809 			laarr[1] = laarr[2];
810 			c = 1;
811 		}
812 
813 		/* if the current block is located in an extent,
814 		   read the next extent */
815 		etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
816 		if (etype != -1) {
817 			laarr[c + 1].extLength = (etype << 30) | elen;
818 			laarr[c + 1].extLocation = eloc;
819 			count++;
820 			startnum++;
821 			endnum++;
822 		} else
823 			lastblock = 1;
824 	}
825 
826 	/* if the current extent is not recorded but allocated, get the
827 	 * block in the extent corresponding to the requested block */
828 	if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
829 		newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
830 	else { /* otherwise, allocate a new block */
831 		if (iinfo->i_next_alloc_block == block)
832 			goal = iinfo->i_next_alloc_goal;
833 
834 		if (!goal) {
835 			if (!(goal = pgoal)) /* XXX: what was intended here? */
836 				goal = iinfo->i_location.logicalBlockNum + 1;
837 		}
838 
839 		newblocknum = udf_new_block(inode->i_sb, inode,
840 				iinfo->i_location.partitionReferenceNum,
841 				goal, err);
842 		if (!newblocknum) {
843 			brelse(prev_epos.bh);
844 			brelse(cur_epos.bh);
845 			brelse(next_epos.bh);
846 			*err = -ENOSPC;
847 			return 0;
848 		}
849 		if (isBeyondEOF)
850 			iinfo->i_lenExtents += inode->i_sb->s_blocksize;
851 	}
852 
853 	/* if the extent the requsted block is located in contains multiple
854 	 * blocks, split the extent into at most three extents. blocks prior
855 	 * to requested block, requested block, and blocks after requested
856 	 * block */
857 	udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
858 
859 #ifdef UDF_PREALLOCATE
860 	/* We preallocate blocks only for regular files. It also makes sense
861 	 * for directories but there's a problem when to drop the
862 	 * preallocation. We might use some delayed work for that but I feel
863 	 * it's overengineering for a filesystem like UDF. */
864 	if (S_ISREG(inode->i_mode))
865 		udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
866 #endif
867 
868 	/* merge any continuous blocks in laarr */
869 	udf_merge_extents(inode, laarr, &endnum);
870 
871 	/* write back the new extents, inserting new extents if the new number
872 	 * of extents is greater than the old number, and deleting extents if
873 	 * the new number of extents is less than the old number */
874 	udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
875 
876 	brelse(prev_epos.bh);
877 	brelse(cur_epos.bh);
878 	brelse(next_epos.bh);
879 
880 	newblock = udf_get_pblock(inode->i_sb, newblocknum,
881 				iinfo->i_location.partitionReferenceNum, 0);
882 	if (!newblock) {
883 		*err = -EIO;
884 		return 0;
885 	}
886 	*new = 1;
887 	iinfo->i_next_alloc_block = block;
888 	iinfo->i_next_alloc_goal = newblocknum;
889 	inode->i_ctime = current_fs_time(inode->i_sb);
890 
891 	if (IS_SYNC(inode))
892 		udf_sync_inode(inode);
893 	else
894 		mark_inode_dirty(inode);
895 
896 	return newblock;
897 }
898 
899 static void udf_split_extents(struct inode *inode, int *c, int offset,
900 			      int newblocknum,
901 			      struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
902 			      int *endnum)
903 {
904 	unsigned long blocksize = inode->i_sb->s_blocksize;
905 	unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
906 
907 	if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
908 	    (laarr[*c].extLength >> 30) ==
909 				(EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
910 		int curr = *c;
911 		int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
912 			    blocksize - 1) >> blocksize_bits;
913 		int8_t etype = (laarr[curr].extLength >> 30);
914 
915 		if (blen == 1)
916 			;
917 		else if (!offset || blen == offset + 1) {
918 			laarr[curr + 2] = laarr[curr + 1];
919 			laarr[curr + 1] = laarr[curr];
920 		} else {
921 			laarr[curr + 3] = laarr[curr + 1];
922 			laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
923 		}
924 
925 		if (offset) {
926 			if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
927 				udf_free_blocks(inode->i_sb, inode,
928 						&laarr[curr].extLocation,
929 						0, offset);
930 				laarr[curr].extLength =
931 					EXT_NOT_RECORDED_NOT_ALLOCATED |
932 					(offset << blocksize_bits);
933 				laarr[curr].extLocation.logicalBlockNum = 0;
934 				laarr[curr].extLocation.
935 						partitionReferenceNum = 0;
936 			} else
937 				laarr[curr].extLength = (etype << 30) |
938 					(offset << blocksize_bits);
939 			curr++;
940 			(*c)++;
941 			(*endnum)++;
942 		}
943 
944 		laarr[curr].extLocation.logicalBlockNum = newblocknum;
945 		if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
946 			laarr[curr].extLocation.partitionReferenceNum =
947 				UDF_I(inode)->i_location.partitionReferenceNum;
948 		laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
949 			blocksize;
950 		curr++;
951 
952 		if (blen != offset + 1) {
953 			if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
954 				laarr[curr].extLocation.logicalBlockNum +=
955 								offset + 1;
956 			laarr[curr].extLength = (etype << 30) |
957 				((blen - (offset + 1)) << blocksize_bits);
958 			curr++;
959 			(*endnum)++;
960 		}
961 	}
962 }
963 
964 static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
965 				 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
966 				 int *endnum)
967 {
968 	int start, length = 0, currlength = 0, i;
969 
970 	if (*endnum >= (c + 1)) {
971 		if (!lastblock)
972 			return;
973 		else
974 			start = c;
975 	} else {
976 		if ((laarr[c + 1].extLength >> 30) ==
977 					(EXT_NOT_RECORDED_ALLOCATED >> 30)) {
978 			start = c + 1;
979 			length = currlength =
980 				(((laarr[c + 1].extLength &
981 					UDF_EXTENT_LENGTH_MASK) +
982 				inode->i_sb->s_blocksize - 1) >>
983 				inode->i_sb->s_blocksize_bits);
984 		} else
985 			start = c;
986 	}
987 
988 	for (i = start + 1; i <= *endnum; i++) {
989 		if (i == *endnum) {
990 			if (lastblock)
991 				length += UDF_DEFAULT_PREALLOC_BLOCKS;
992 		} else if ((laarr[i].extLength >> 30) ==
993 				(EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
994 			length += (((laarr[i].extLength &
995 						UDF_EXTENT_LENGTH_MASK) +
996 				    inode->i_sb->s_blocksize - 1) >>
997 				    inode->i_sb->s_blocksize_bits);
998 		} else
999 			break;
1000 	}
1001 
1002 	if (length) {
1003 		int next = laarr[start].extLocation.logicalBlockNum +
1004 			(((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
1005 			  inode->i_sb->s_blocksize - 1) >>
1006 			  inode->i_sb->s_blocksize_bits);
1007 		int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
1008 				laarr[start].extLocation.partitionReferenceNum,
1009 				next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
1010 				length : UDF_DEFAULT_PREALLOC_BLOCKS) -
1011 				currlength);
1012 		if (numalloc) 	{
1013 			if (start == (c + 1))
1014 				laarr[start].extLength +=
1015 					(numalloc <<
1016 					 inode->i_sb->s_blocksize_bits);
1017 			else {
1018 				memmove(&laarr[c + 2], &laarr[c + 1],
1019 					sizeof(struct long_ad) * (*endnum - (c + 1)));
1020 				(*endnum)++;
1021 				laarr[c + 1].extLocation.logicalBlockNum = next;
1022 				laarr[c + 1].extLocation.partitionReferenceNum =
1023 					laarr[c].extLocation.
1024 							partitionReferenceNum;
1025 				laarr[c + 1].extLength =
1026 					EXT_NOT_RECORDED_ALLOCATED |
1027 					(numalloc <<
1028 					 inode->i_sb->s_blocksize_bits);
1029 				start = c + 1;
1030 			}
1031 
1032 			for (i = start + 1; numalloc && i < *endnum; i++) {
1033 				int elen = ((laarr[i].extLength &
1034 						UDF_EXTENT_LENGTH_MASK) +
1035 					    inode->i_sb->s_blocksize - 1) >>
1036 					    inode->i_sb->s_blocksize_bits;
1037 
1038 				if (elen > numalloc) {
1039 					laarr[i].extLength -=
1040 						(numalloc <<
1041 						 inode->i_sb->s_blocksize_bits);
1042 					numalloc = 0;
1043 				} else {
1044 					numalloc -= elen;
1045 					if (*endnum > (i + 1))
1046 						memmove(&laarr[i],
1047 							&laarr[i + 1],
1048 							sizeof(struct long_ad) *
1049 							(*endnum - (i + 1)));
1050 					i--;
1051 					(*endnum)--;
1052 				}
1053 			}
1054 			UDF_I(inode)->i_lenExtents +=
1055 				numalloc << inode->i_sb->s_blocksize_bits;
1056 		}
1057 	}
1058 }
1059 
1060 static void udf_merge_extents(struct inode *inode,
1061 			      struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
1062 			      int *endnum)
1063 {
1064 	int i;
1065 	unsigned long blocksize = inode->i_sb->s_blocksize;
1066 	unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1067 
1068 	for (i = 0; i < (*endnum - 1); i++) {
1069 		struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
1070 		struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
1071 
1072 		if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
1073 			(((li->extLength >> 30) ==
1074 				(EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1075 			((lip1->extLocation.logicalBlockNum -
1076 			  li->extLocation.logicalBlockNum) ==
1077 			(((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1078 			blocksize - 1) >> blocksize_bits)))) {
1079 
1080 			if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1081 				(lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1082 				blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1083 				lip1->extLength = (lip1->extLength -
1084 						  (li->extLength &
1085 						   UDF_EXTENT_LENGTH_MASK) +
1086 						   UDF_EXTENT_LENGTH_MASK) &
1087 							~(blocksize - 1);
1088 				li->extLength = (li->extLength &
1089 						 UDF_EXTENT_FLAG_MASK) +
1090 						(UDF_EXTENT_LENGTH_MASK + 1) -
1091 						blocksize;
1092 				lip1->extLocation.logicalBlockNum =
1093 					li->extLocation.logicalBlockNum +
1094 					((li->extLength &
1095 						UDF_EXTENT_LENGTH_MASK) >>
1096 						blocksize_bits);
1097 			} else {
1098 				li->extLength = lip1->extLength +
1099 					(((li->extLength &
1100 						UDF_EXTENT_LENGTH_MASK) +
1101 					 blocksize - 1) & ~(blocksize - 1));
1102 				if (*endnum > (i + 2))
1103 					memmove(&laarr[i + 1], &laarr[i + 2],
1104 						sizeof(struct long_ad) *
1105 						(*endnum - (i + 2)));
1106 				i--;
1107 				(*endnum)--;
1108 			}
1109 		} else if (((li->extLength >> 30) ==
1110 				(EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1111 			   ((lip1->extLength >> 30) ==
1112 				(EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
1113 			udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
1114 					((li->extLength &
1115 					  UDF_EXTENT_LENGTH_MASK) +
1116 					 blocksize - 1) >> blocksize_bits);
1117 			li->extLocation.logicalBlockNum = 0;
1118 			li->extLocation.partitionReferenceNum = 0;
1119 
1120 			if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1121 			     (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1122 			     blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1123 				lip1->extLength = (lip1->extLength -
1124 						   (li->extLength &
1125 						   UDF_EXTENT_LENGTH_MASK) +
1126 						   UDF_EXTENT_LENGTH_MASK) &
1127 						   ~(blocksize - 1);
1128 				li->extLength = (li->extLength &
1129 						 UDF_EXTENT_FLAG_MASK) +
1130 						(UDF_EXTENT_LENGTH_MASK + 1) -
1131 						blocksize;
1132 			} else {
1133 				li->extLength = lip1->extLength +
1134 					(((li->extLength &
1135 						UDF_EXTENT_LENGTH_MASK) +
1136 					  blocksize - 1) & ~(blocksize - 1));
1137 				if (*endnum > (i + 2))
1138 					memmove(&laarr[i + 1], &laarr[i + 2],
1139 						sizeof(struct long_ad) *
1140 						(*endnum - (i + 2)));
1141 				i--;
1142 				(*endnum)--;
1143 			}
1144 		} else if ((li->extLength >> 30) ==
1145 					(EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1146 			udf_free_blocks(inode->i_sb, inode,
1147 					&li->extLocation, 0,
1148 					((li->extLength &
1149 						UDF_EXTENT_LENGTH_MASK) +
1150 					 blocksize - 1) >> blocksize_bits);
1151 			li->extLocation.logicalBlockNum = 0;
1152 			li->extLocation.partitionReferenceNum = 0;
1153 			li->extLength = (li->extLength &
1154 						UDF_EXTENT_LENGTH_MASK) |
1155 						EXT_NOT_RECORDED_NOT_ALLOCATED;
1156 		}
1157 	}
1158 }
1159 
1160 static void udf_update_extents(struct inode *inode,
1161 			       struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
1162 			       int startnum, int endnum,
1163 			       struct extent_position *epos)
1164 {
1165 	int start = 0, i;
1166 	struct kernel_lb_addr tmploc;
1167 	uint32_t tmplen;
1168 
1169 	if (startnum > endnum) {
1170 		for (i = 0; i < (startnum - endnum); i++)
1171 			udf_delete_aext(inode, *epos, laarr[i].extLocation,
1172 					laarr[i].extLength);
1173 	} else if (startnum < endnum) {
1174 		for (i = 0; i < (endnum - startnum); i++) {
1175 			udf_insert_aext(inode, *epos, laarr[i].extLocation,
1176 					laarr[i].extLength);
1177 			udf_next_aext(inode, epos, &laarr[i].extLocation,
1178 				      &laarr[i].extLength, 1);
1179 			start++;
1180 		}
1181 	}
1182 
1183 	for (i = start; i < endnum; i++) {
1184 		udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
1185 		udf_write_aext(inode, epos, &laarr[i].extLocation,
1186 			       laarr[i].extLength, 1);
1187 	}
1188 }
1189 
1190 struct buffer_head *udf_bread(struct inode *inode, int block,
1191 			      int create, int *err)
1192 {
1193 	struct buffer_head *bh = NULL;
1194 
1195 	bh = udf_getblk(inode, block, create, err);
1196 	if (!bh)
1197 		return NULL;
1198 
1199 	if (buffer_uptodate(bh))
1200 		return bh;
1201 
1202 	ll_rw_block(REQ_OP_READ, 0, 1, &bh);
1203 
1204 	wait_on_buffer(bh);
1205 	if (buffer_uptodate(bh))
1206 		return bh;
1207 
1208 	brelse(bh);
1209 	*err = -EIO;
1210 	return NULL;
1211 }
1212 
1213 int udf_setsize(struct inode *inode, loff_t newsize)
1214 {
1215 	int err;
1216 	struct udf_inode_info *iinfo;
1217 	int bsize = 1 << inode->i_blkbits;
1218 
1219 	if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1220 	      S_ISLNK(inode->i_mode)))
1221 		return -EINVAL;
1222 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1223 		return -EPERM;
1224 
1225 	iinfo = UDF_I(inode);
1226 	if (newsize > inode->i_size) {
1227 		down_write(&iinfo->i_data_sem);
1228 		if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1229 			if (bsize <
1230 			    (udf_file_entry_alloc_offset(inode) + newsize)) {
1231 				err = udf_expand_file_adinicb(inode);
1232 				if (err)
1233 					return err;
1234 				down_write(&iinfo->i_data_sem);
1235 			} else {
1236 				iinfo->i_lenAlloc = newsize;
1237 				goto set_size;
1238 			}
1239 		}
1240 		err = udf_extend_file(inode, newsize);
1241 		if (err) {
1242 			up_write(&iinfo->i_data_sem);
1243 			return err;
1244 		}
1245 set_size:
1246 		truncate_setsize(inode, newsize);
1247 		up_write(&iinfo->i_data_sem);
1248 	} else {
1249 		if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1250 			down_write(&iinfo->i_data_sem);
1251 			udf_clear_extent_cache(inode);
1252 			memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1253 			       0x00, bsize - newsize -
1254 			       udf_file_entry_alloc_offset(inode));
1255 			iinfo->i_lenAlloc = newsize;
1256 			truncate_setsize(inode, newsize);
1257 			up_write(&iinfo->i_data_sem);
1258 			goto update_time;
1259 		}
1260 		err = block_truncate_page(inode->i_mapping, newsize,
1261 					  udf_get_block);
1262 		if (err)
1263 			return err;
1264 		down_write(&iinfo->i_data_sem);
1265 		udf_clear_extent_cache(inode);
1266 		truncate_setsize(inode, newsize);
1267 		udf_truncate_extents(inode);
1268 		up_write(&iinfo->i_data_sem);
1269 	}
1270 update_time:
1271 	inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1272 	if (IS_SYNC(inode))
1273 		udf_sync_inode(inode);
1274 	else
1275 		mark_inode_dirty(inode);
1276 	return 0;
1277 }
1278 
1279 /*
1280  * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1281  * arbitrary - just that we hopefully don't limit any real use of rewritten
1282  * inode on write-once media but avoid looping for too long on corrupted media.
1283  */
1284 #define UDF_MAX_ICB_NESTING 1024
1285 
1286 static int udf_read_inode(struct inode *inode, bool hidden_inode)
1287 {
1288 	struct buffer_head *bh = NULL;
1289 	struct fileEntry *fe;
1290 	struct extendedFileEntry *efe;
1291 	uint16_t ident;
1292 	struct udf_inode_info *iinfo = UDF_I(inode);
1293 	struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1294 	struct kernel_lb_addr *iloc = &iinfo->i_location;
1295 	unsigned int link_count;
1296 	unsigned int indirections = 0;
1297 	int bs = inode->i_sb->s_blocksize;
1298 	int ret = -EIO;
1299 
1300 reread:
1301 	if (iloc->logicalBlockNum >=
1302 	    sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
1303 		udf_debug("block=%d, partition=%d out of range\n",
1304 			  iloc->logicalBlockNum, iloc->partitionReferenceNum);
1305 		return -EIO;
1306 	}
1307 
1308 	/*
1309 	 * Set defaults, but the inode is still incomplete!
1310 	 * Note: get_new_inode() sets the following on a new inode:
1311 	 *      i_sb = sb
1312 	 *      i_no = ino
1313 	 *      i_flags = sb->s_flags
1314 	 *      i_state = 0
1315 	 * clean_inode(): zero fills and sets
1316 	 *      i_count = 1
1317 	 *      i_nlink = 1
1318 	 *      i_op = NULL;
1319 	 */
1320 	bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
1321 	if (!bh) {
1322 		udf_err(inode->i_sb, "(ino %ld) failed !bh\n", inode->i_ino);
1323 		return -EIO;
1324 	}
1325 
1326 	if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
1327 	    ident != TAG_IDENT_USE) {
1328 		udf_err(inode->i_sb, "(ino %ld) failed ident=%d\n",
1329 			inode->i_ino, ident);
1330 		goto out;
1331 	}
1332 
1333 	fe = (struct fileEntry *)bh->b_data;
1334 	efe = (struct extendedFileEntry *)bh->b_data;
1335 
1336 	if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
1337 		struct buffer_head *ibh;
1338 
1339 		ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
1340 		if (ident == TAG_IDENT_IE && ibh) {
1341 			struct kernel_lb_addr loc;
1342 			struct indirectEntry *ie;
1343 
1344 			ie = (struct indirectEntry *)ibh->b_data;
1345 			loc = lelb_to_cpu(ie->indirectICB.extLocation);
1346 
1347 			if (ie->indirectICB.extLength) {
1348 				brelse(ibh);
1349 				memcpy(&iinfo->i_location, &loc,
1350 				       sizeof(struct kernel_lb_addr));
1351 				if (++indirections > UDF_MAX_ICB_NESTING) {
1352 					udf_err(inode->i_sb,
1353 						"too many ICBs in ICB hierarchy"
1354 						" (max %d supported)\n",
1355 						UDF_MAX_ICB_NESTING);
1356 					goto out;
1357 				}
1358 				brelse(bh);
1359 				goto reread;
1360 			}
1361 		}
1362 		brelse(ibh);
1363 	} else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
1364 		udf_err(inode->i_sb, "unsupported strategy type: %d\n",
1365 			le16_to_cpu(fe->icbTag.strategyType));
1366 		goto out;
1367 	}
1368 	if (fe->icbTag.strategyType == cpu_to_le16(4))
1369 		iinfo->i_strat4096 = 0;
1370 	else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
1371 		iinfo->i_strat4096 = 1;
1372 
1373 	iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
1374 							ICBTAG_FLAG_AD_MASK;
1375 	iinfo->i_unique = 0;
1376 	iinfo->i_lenEAttr = 0;
1377 	iinfo->i_lenExtents = 0;
1378 	iinfo->i_lenAlloc = 0;
1379 	iinfo->i_next_alloc_block = 0;
1380 	iinfo->i_next_alloc_goal = 0;
1381 	if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
1382 		iinfo->i_efe = 1;
1383 		iinfo->i_use = 0;
1384 		ret = udf_alloc_i_data(inode, bs -
1385 					sizeof(struct extendedFileEntry));
1386 		if (ret)
1387 			goto out;
1388 		memcpy(iinfo->i_ext.i_data,
1389 		       bh->b_data + sizeof(struct extendedFileEntry),
1390 		       bs - sizeof(struct extendedFileEntry));
1391 	} else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
1392 		iinfo->i_efe = 0;
1393 		iinfo->i_use = 0;
1394 		ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
1395 		if (ret)
1396 			goto out;
1397 		memcpy(iinfo->i_ext.i_data,
1398 		       bh->b_data + sizeof(struct fileEntry),
1399 		       bs - sizeof(struct fileEntry));
1400 	} else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
1401 		iinfo->i_efe = 0;
1402 		iinfo->i_use = 1;
1403 		iinfo->i_lenAlloc = le32_to_cpu(
1404 				((struct unallocSpaceEntry *)bh->b_data)->
1405 				 lengthAllocDescs);
1406 		ret = udf_alloc_i_data(inode, bs -
1407 					sizeof(struct unallocSpaceEntry));
1408 		if (ret)
1409 			goto out;
1410 		memcpy(iinfo->i_ext.i_data,
1411 		       bh->b_data + sizeof(struct unallocSpaceEntry),
1412 		       bs - sizeof(struct unallocSpaceEntry));
1413 		return 0;
1414 	}
1415 
1416 	ret = -EIO;
1417 	read_lock(&sbi->s_cred_lock);
1418 	i_uid_write(inode, le32_to_cpu(fe->uid));
1419 	if (!uid_valid(inode->i_uid) ||
1420 	    UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1421 	    UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
1422 		inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
1423 
1424 	i_gid_write(inode, le32_to_cpu(fe->gid));
1425 	if (!gid_valid(inode->i_gid) ||
1426 	    UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1427 	    UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
1428 		inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
1429 
1430 	if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
1431 			sbi->s_fmode != UDF_INVALID_MODE)
1432 		inode->i_mode = sbi->s_fmode;
1433 	else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
1434 			sbi->s_dmode != UDF_INVALID_MODE)
1435 		inode->i_mode = sbi->s_dmode;
1436 	else
1437 		inode->i_mode = udf_convert_permissions(fe);
1438 	inode->i_mode &= ~sbi->s_umask;
1439 	read_unlock(&sbi->s_cred_lock);
1440 
1441 	link_count = le16_to_cpu(fe->fileLinkCount);
1442 	if (!link_count) {
1443 		if (!hidden_inode) {
1444 			ret = -ESTALE;
1445 			goto out;
1446 		}
1447 		link_count = 1;
1448 	}
1449 	set_nlink(inode, link_count);
1450 
1451 	inode->i_size = le64_to_cpu(fe->informationLength);
1452 	iinfo->i_lenExtents = inode->i_size;
1453 
1454 	if (iinfo->i_efe == 0) {
1455 		inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
1456 			(inode->i_sb->s_blocksize_bits - 9);
1457 
1458 		if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
1459 			inode->i_atime = sbi->s_record_time;
1460 
1461 		if (!udf_disk_stamp_to_time(&inode->i_mtime,
1462 					    fe->modificationTime))
1463 			inode->i_mtime = sbi->s_record_time;
1464 
1465 		if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
1466 			inode->i_ctime = sbi->s_record_time;
1467 
1468 		iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1469 		iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1470 		iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1471 		iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
1472 	} else {
1473 		inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
1474 		    (inode->i_sb->s_blocksize_bits - 9);
1475 
1476 		if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
1477 			inode->i_atime = sbi->s_record_time;
1478 
1479 		if (!udf_disk_stamp_to_time(&inode->i_mtime,
1480 					    efe->modificationTime))
1481 			inode->i_mtime = sbi->s_record_time;
1482 
1483 		if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
1484 			iinfo->i_crtime = sbi->s_record_time;
1485 
1486 		if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
1487 			inode->i_ctime = sbi->s_record_time;
1488 
1489 		iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1490 		iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1491 		iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
1492 		iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
1493 	}
1494 	inode->i_generation = iinfo->i_unique;
1495 
1496 	/*
1497 	 * Sanity check length of allocation descriptors and extended attrs to
1498 	 * avoid integer overflows
1499 	 */
1500 	if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
1501 		goto out;
1502 	/* Now do exact checks */
1503 	if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
1504 		goto out;
1505 	/* Sanity checks for files in ICB so that we don't get confused later */
1506 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1507 		/*
1508 		 * For file in ICB data is stored in allocation descriptor
1509 		 * so sizes should match
1510 		 */
1511 		if (iinfo->i_lenAlloc != inode->i_size)
1512 			goto out;
1513 		/* File in ICB has to fit in there... */
1514 		if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
1515 			goto out;
1516 	}
1517 
1518 	switch (fe->icbTag.fileType) {
1519 	case ICBTAG_FILE_TYPE_DIRECTORY:
1520 		inode->i_op = &udf_dir_inode_operations;
1521 		inode->i_fop = &udf_dir_operations;
1522 		inode->i_mode |= S_IFDIR;
1523 		inc_nlink(inode);
1524 		break;
1525 	case ICBTAG_FILE_TYPE_REALTIME:
1526 	case ICBTAG_FILE_TYPE_REGULAR:
1527 	case ICBTAG_FILE_TYPE_UNDEF:
1528 	case ICBTAG_FILE_TYPE_VAT20:
1529 		if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1530 			inode->i_data.a_ops = &udf_adinicb_aops;
1531 		else
1532 			inode->i_data.a_ops = &udf_aops;
1533 		inode->i_op = &udf_file_inode_operations;
1534 		inode->i_fop = &udf_file_operations;
1535 		inode->i_mode |= S_IFREG;
1536 		break;
1537 	case ICBTAG_FILE_TYPE_BLOCK:
1538 		inode->i_mode |= S_IFBLK;
1539 		break;
1540 	case ICBTAG_FILE_TYPE_CHAR:
1541 		inode->i_mode |= S_IFCHR;
1542 		break;
1543 	case ICBTAG_FILE_TYPE_FIFO:
1544 		init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1545 		break;
1546 	case ICBTAG_FILE_TYPE_SOCKET:
1547 		init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1548 		break;
1549 	case ICBTAG_FILE_TYPE_SYMLINK:
1550 		inode->i_data.a_ops = &udf_symlink_aops;
1551 		inode->i_op = &page_symlink_inode_operations;
1552 		inode_nohighmem(inode);
1553 		inode->i_mode = S_IFLNK | S_IRWXUGO;
1554 		break;
1555 	case ICBTAG_FILE_TYPE_MAIN:
1556 		udf_debug("METADATA FILE-----\n");
1557 		break;
1558 	case ICBTAG_FILE_TYPE_MIRROR:
1559 		udf_debug("METADATA MIRROR FILE-----\n");
1560 		break;
1561 	case ICBTAG_FILE_TYPE_BITMAP:
1562 		udf_debug("METADATA BITMAP FILE-----\n");
1563 		break;
1564 	default:
1565 		udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n",
1566 			inode->i_ino, fe->icbTag.fileType);
1567 		goto out;
1568 	}
1569 	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1570 		struct deviceSpec *dsea =
1571 			(struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1572 		if (dsea) {
1573 			init_special_inode(inode, inode->i_mode,
1574 				MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1575 				      le32_to_cpu(dsea->minorDeviceIdent)));
1576 			/* Developer ID ??? */
1577 		} else
1578 			goto out;
1579 	}
1580 	ret = 0;
1581 out:
1582 	brelse(bh);
1583 	return ret;
1584 }
1585 
1586 static int udf_alloc_i_data(struct inode *inode, size_t size)
1587 {
1588 	struct udf_inode_info *iinfo = UDF_I(inode);
1589 	iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
1590 
1591 	if (!iinfo->i_ext.i_data) {
1592 		udf_err(inode->i_sb, "(ino %ld) no free memory\n",
1593 			inode->i_ino);
1594 		return -ENOMEM;
1595 	}
1596 
1597 	return 0;
1598 }
1599 
1600 static umode_t udf_convert_permissions(struct fileEntry *fe)
1601 {
1602 	umode_t mode;
1603 	uint32_t permissions;
1604 	uint32_t flags;
1605 
1606 	permissions = le32_to_cpu(fe->permissions);
1607 	flags = le16_to_cpu(fe->icbTag.flags);
1608 
1609 	mode =	((permissions) & S_IRWXO) |
1610 		((permissions >> 2) & S_IRWXG) |
1611 		((permissions >> 4) & S_IRWXU) |
1612 		((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1613 		((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1614 		((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
1615 
1616 	return mode;
1617 }
1618 
1619 int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
1620 {
1621 	return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1622 }
1623 
1624 static int udf_sync_inode(struct inode *inode)
1625 {
1626 	return udf_update_inode(inode, 1);
1627 }
1628 
1629 static int udf_update_inode(struct inode *inode, int do_sync)
1630 {
1631 	struct buffer_head *bh = NULL;
1632 	struct fileEntry *fe;
1633 	struct extendedFileEntry *efe;
1634 	uint64_t lb_recorded;
1635 	uint32_t udfperms;
1636 	uint16_t icbflags;
1637 	uint16_t crclen;
1638 	int err = 0;
1639 	struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1640 	unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1641 	struct udf_inode_info *iinfo = UDF_I(inode);
1642 
1643 	bh = udf_tgetblk(inode->i_sb,
1644 			udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
1645 	if (!bh) {
1646 		udf_debug("getblk failure\n");
1647 		return -EIO;
1648 	}
1649 
1650 	lock_buffer(bh);
1651 	memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1652 	fe = (struct fileEntry *)bh->b_data;
1653 	efe = (struct extendedFileEntry *)bh->b_data;
1654 
1655 	if (iinfo->i_use) {
1656 		struct unallocSpaceEntry *use =
1657 			(struct unallocSpaceEntry *)bh->b_data;
1658 
1659 		use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1660 		memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1661 		       iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
1662 					sizeof(struct unallocSpaceEntry));
1663 		use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1664 		crclen = sizeof(struct unallocSpaceEntry);
1665 
1666 		goto finish;
1667 	}
1668 
1669 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1670 		fe->uid = cpu_to_le32(-1);
1671 	else
1672 		fe->uid = cpu_to_le32(i_uid_read(inode));
1673 
1674 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1675 		fe->gid = cpu_to_le32(-1);
1676 	else
1677 		fe->gid = cpu_to_le32(i_gid_read(inode));
1678 
1679 	udfperms = ((inode->i_mode & S_IRWXO)) |
1680 		   ((inode->i_mode & S_IRWXG) << 2) |
1681 		   ((inode->i_mode & S_IRWXU) << 4);
1682 
1683 	udfperms |= (le32_to_cpu(fe->permissions) &
1684 		    (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1685 		     FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1686 		     FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
1687 	fe->permissions = cpu_to_le32(udfperms);
1688 
1689 	if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
1690 		fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1691 	else
1692 		fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1693 
1694 	fe->informationLength = cpu_to_le64(inode->i_size);
1695 
1696 	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1697 		struct regid *eid;
1698 		struct deviceSpec *dsea =
1699 			(struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1700 		if (!dsea) {
1701 			dsea = (struct deviceSpec *)
1702 				udf_add_extendedattr(inode,
1703 						     sizeof(struct deviceSpec) +
1704 						     sizeof(struct regid), 12, 0x3);
1705 			dsea->attrType = cpu_to_le32(12);
1706 			dsea->attrSubtype = 1;
1707 			dsea->attrLength = cpu_to_le32(
1708 						sizeof(struct deviceSpec) +
1709 						sizeof(struct regid));
1710 			dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
1711 		}
1712 		eid = (struct regid *)dsea->impUse;
1713 		memset(eid, 0, sizeof(struct regid));
1714 		strcpy(eid->ident, UDF_ID_DEVELOPER);
1715 		eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1716 		eid->identSuffix[1] = UDF_OS_ID_LINUX;
1717 		dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1718 		dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1719 	}
1720 
1721 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1722 		lb_recorded = 0; /* No extents => no blocks! */
1723 	else
1724 		lb_recorded =
1725 			(inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1726 			(blocksize_bits - 9);
1727 
1728 	if (iinfo->i_efe == 0) {
1729 		memcpy(bh->b_data + sizeof(struct fileEntry),
1730 		       iinfo->i_ext.i_data,
1731 		       inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1732 		fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1733 
1734 		udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1735 		udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1736 		udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
1737 		memset(&(fe->impIdent), 0, sizeof(struct regid));
1738 		strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1739 		fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1740 		fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1741 		fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1742 		fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1743 		fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1744 		fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1745 		fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1746 		crclen = sizeof(struct fileEntry);
1747 	} else {
1748 		memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1749 		       iinfo->i_ext.i_data,
1750 		       inode->i_sb->s_blocksize -
1751 					sizeof(struct extendedFileEntry));
1752 		efe->objectSize = cpu_to_le64(inode->i_size);
1753 		efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1754 
1755 		if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1756 		    (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1757 		     iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1758 			iinfo->i_crtime = inode->i_atime;
1759 
1760 		if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1761 		    (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1762 		     iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1763 			iinfo->i_crtime = inode->i_mtime;
1764 
1765 		if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1766 		    (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1767 		     iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1768 			iinfo->i_crtime = inode->i_ctime;
1769 
1770 		udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1771 		udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1772 		udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1773 		udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
1774 
1775 		memset(&(efe->impIdent), 0, sizeof(struct regid));
1776 		strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1777 		efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1778 		efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1779 		efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1780 		efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1781 		efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1782 		efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1783 		efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1784 		crclen = sizeof(struct extendedFileEntry);
1785 	}
1786 
1787 finish:
1788 	if (iinfo->i_strat4096) {
1789 		fe->icbTag.strategyType = cpu_to_le16(4096);
1790 		fe->icbTag.strategyParameter = cpu_to_le16(1);
1791 		fe->icbTag.numEntries = cpu_to_le16(2);
1792 	} else {
1793 		fe->icbTag.strategyType = cpu_to_le16(4);
1794 		fe->icbTag.numEntries = cpu_to_le16(1);
1795 	}
1796 
1797 	if (iinfo->i_use)
1798 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
1799 	else if (S_ISDIR(inode->i_mode))
1800 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1801 	else if (S_ISREG(inode->i_mode))
1802 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1803 	else if (S_ISLNK(inode->i_mode))
1804 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1805 	else if (S_ISBLK(inode->i_mode))
1806 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1807 	else if (S_ISCHR(inode->i_mode))
1808 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1809 	else if (S_ISFIFO(inode->i_mode))
1810 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1811 	else if (S_ISSOCK(inode->i_mode))
1812 		fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1813 
1814 	icbflags =	iinfo->i_alloc_type |
1815 			((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1816 			((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1817 			((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1818 			(le16_to_cpu(fe->icbTag.flags) &
1819 				~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1820 				ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
1821 
1822 	fe->icbTag.flags = cpu_to_le16(icbflags);
1823 	if (sbi->s_udfrev >= 0x0200)
1824 		fe->descTag.descVersion = cpu_to_le16(3);
1825 	else
1826 		fe->descTag.descVersion = cpu_to_le16(2);
1827 	fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
1828 	fe->descTag.tagLocation = cpu_to_le32(
1829 					iinfo->i_location.logicalBlockNum);
1830 	crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
1831 	fe->descTag.descCRCLength = cpu_to_le16(crclen);
1832 	fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
1833 						  crclen));
1834 	fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
1835 
1836 	set_buffer_uptodate(bh);
1837 	unlock_buffer(bh);
1838 
1839 	/* write the data blocks */
1840 	mark_buffer_dirty(bh);
1841 	if (do_sync) {
1842 		sync_dirty_buffer(bh);
1843 		if (buffer_write_io_error(bh)) {
1844 			udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1845 				 inode->i_ino);
1846 			err = -EIO;
1847 		}
1848 	}
1849 	brelse(bh);
1850 
1851 	return err;
1852 }
1853 
1854 struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
1855 			 bool hidden_inode)
1856 {
1857 	unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1858 	struct inode *inode = iget_locked(sb, block);
1859 	int err;
1860 
1861 	if (!inode)
1862 		return ERR_PTR(-ENOMEM);
1863 
1864 	if (!(inode->i_state & I_NEW))
1865 		return inode;
1866 
1867 	memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
1868 	err = udf_read_inode(inode, hidden_inode);
1869 	if (err < 0) {
1870 		iget_failed(inode);
1871 		return ERR_PTR(err);
1872 	}
1873 	unlock_new_inode(inode);
1874 
1875 	return inode;
1876 }
1877 
1878 int udf_setup_indirect_aext(struct inode *inode, int block,
1879 			    struct extent_position *epos)
1880 {
1881 	struct super_block *sb = inode->i_sb;
1882 	struct buffer_head *bh;
1883 	struct allocExtDesc *aed;
1884 	struct extent_position nepos;
1885 	struct kernel_lb_addr neloc;
1886 	int ver, adsize;
1887 
1888 	if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1889 		adsize = sizeof(struct short_ad);
1890 	else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1891 		adsize = sizeof(struct long_ad);
1892 	else
1893 		return -EIO;
1894 
1895 	neloc.logicalBlockNum = block;
1896 	neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
1897 
1898 	bh = udf_tgetblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
1899 	if (!bh)
1900 		return -EIO;
1901 	lock_buffer(bh);
1902 	memset(bh->b_data, 0x00, sb->s_blocksize);
1903 	set_buffer_uptodate(bh);
1904 	unlock_buffer(bh);
1905 	mark_buffer_dirty_inode(bh, inode);
1906 
1907 	aed = (struct allocExtDesc *)(bh->b_data);
1908 	if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
1909 		aed->previousAllocExtLocation =
1910 				cpu_to_le32(epos->block.logicalBlockNum);
1911 	}
1912 	aed->lengthAllocDescs = cpu_to_le32(0);
1913 	if (UDF_SB(sb)->s_udfrev >= 0x0200)
1914 		ver = 3;
1915 	else
1916 		ver = 2;
1917 	udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
1918 		    sizeof(struct tag));
1919 
1920 	nepos.block = neloc;
1921 	nepos.offset = sizeof(struct allocExtDesc);
1922 	nepos.bh = bh;
1923 
1924 	/*
1925 	 * Do we have to copy current last extent to make space for indirect
1926 	 * one?
1927 	 */
1928 	if (epos->offset + adsize > sb->s_blocksize) {
1929 		struct kernel_lb_addr cp_loc;
1930 		uint32_t cp_len;
1931 		int cp_type;
1932 
1933 		epos->offset -= adsize;
1934 		cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
1935 		cp_len |= ((uint32_t)cp_type) << 30;
1936 
1937 		__udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
1938 		udf_write_aext(inode, epos, &nepos.block,
1939 			       sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1940 	} else {
1941 		__udf_add_aext(inode, epos, &nepos.block,
1942 			       sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1943 	}
1944 
1945 	brelse(epos->bh);
1946 	*epos = nepos;
1947 
1948 	return 0;
1949 }
1950 
1951 /*
1952  * Append extent at the given position - should be the first free one in inode
1953  * / indirect extent. This function assumes there is enough space in the inode
1954  * or indirect extent. Use udf_add_aext() if you didn't check for this before.
1955  */
1956 int __udf_add_aext(struct inode *inode, struct extent_position *epos,
1957 		   struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1958 {
1959 	struct udf_inode_info *iinfo = UDF_I(inode);
1960 	struct allocExtDesc *aed;
1961 	int adsize;
1962 
1963 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1964 		adsize = sizeof(struct short_ad);
1965 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1966 		adsize = sizeof(struct long_ad);
1967 	else
1968 		return -EIO;
1969 
1970 	if (!epos->bh) {
1971 		WARN_ON(iinfo->i_lenAlloc !=
1972 			epos->offset - udf_file_entry_alloc_offset(inode));
1973 	} else {
1974 		aed = (struct allocExtDesc *)epos->bh->b_data;
1975 		WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
1976 			epos->offset - sizeof(struct allocExtDesc));
1977 		WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
1978 	}
1979 
1980 	udf_write_aext(inode, epos, eloc, elen, inc);
1981 
1982 	if (!epos->bh) {
1983 		iinfo->i_lenAlloc += adsize;
1984 		mark_inode_dirty(inode);
1985 	} else {
1986 		aed = (struct allocExtDesc *)epos->bh->b_data;
1987 		le32_add_cpu(&aed->lengthAllocDescs, adsize);
1988 		if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1989 				UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1990 			udf_update_tag(epos->bh->b_data,
1991 					epos->offset + (inc ? 0 : adsize));
1992 		else
1993 			udf_update_tag(epos->bh->b_data,
1994 					sizeof(struct allocExtDesc));
1995 		mark_buffer_dirty_inode(epos->bh, inode);
1996 	}
1997 
1998 	return 0;
1999 }
2000 
2001 /*
2002  * Append extent at given position - should be the first free one in inode
2003  * / indirect extent. Takes care of allocating and linking indirect blocks.
2004  */
2005 int udf_add_aext(struct inode *inode, struct extent_position *epos,
2006 		 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2007 {
2008 	int adsize;
2009 	struct super_block *sb = inode->i_sb;
2010 
2011 	if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2012 		adsize = sizeof(struct short_ad);
2013 	else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2014 		adsize = sizeof(struct long_ad);
2015 	else
2016 		return -EIO;
2017 
2018 	if (epos->offset + (2 * adsize) > sb->s_blocksize) {
2019 		int err;
2020 		int new_block;
2021 
2022 		new_block = udf_new_block(sb, NULL,
2023 					  epos->block.partitionReferenceNum,
2024 					  epos->block.logicalBlockNum, &err);
2025 		if (!new_block)
2026 			return -ENOSPC;
2027 
2028 		err = udf_setup_indirect_aext(inode, new_block, epos);
2029 		if (err)
2030 			return err;
2031 	}
2032 
2033 	return __udf_add_aext(inode, epos, eloc, elen, inc);
2034 }
2035 
2036 void udf_write_aext(struct inode *inode, struct extent_position *epos,
2037 		    struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2038 {
2039 	int adsize;
2040 	uint8_t *ptr;
2041 	struct short_ad *sad;
2042 	struct long_ad *lad;
2043 	struct udf_inode_info *iinfo = UDF_I(inode);
2044 
2045 	if (!epos->bh)
2046 		ptr = iinfo->i_ext.i_data + epos->offset -
2047 			udf_file_entry_alloc_offset(inode) +
2048 			iinfo->i_lenEAttr;
2049 	else
2050 		ptr = epos->bh->b_data + epos->offset;
2051 
2052 	switch (iinfo->i_alloc_type) {
2053 	case ICBTAG_FLAG_AD_SHORT:
2054 		sad = (struct short_ad *)ptr;
2055 		sad->extLength = cpu_to_le32(elen);
2056 		sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
2057 		adsize = sizeof(struct short_ad);
2058 		break;
2059 	case ICBTAG_FLAG_AD_LONG:
2060 		lad = (struct long_ad *)ptr;
2061 		lad->extLength = cpu_to_le32(elen);
2062 		lad->extLocation = cpu_to_lelb(*eloc);
2063 		memset(lad->impUse, 0x00, sizeof(lad->impUse));
2064 		adsize = sizeof(struct long_ad);
2065 		break;
2066 	default:
2067 		return;
2068 	}
2069 
2070 	if (epos->bh) {
2071 		if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2072 		    UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
2073 			struct allocExtDesc *aed =
2074 				(struct allocExtDesc *)epos->bh->b_data;
2075 			udf_update_tag(epos->bh->b_data,
2076 				       le32_to_cpu(aed->lengthAllocDescs) +
2077 				       sizeof(struct allocExtDesc));
2078 		}
2079 		mark_buffer_dirty_inode(epos->bh, inode);
2080 	} else {
2081 		mark_inode_dirty(inode);
2082 	}
2083 
2084 	if (inc)
2085 		epos->offset += adsize;
2086 }
2087 
2088 /*
2089  * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2090  * someone does some weird stuff.
2091  */
2092 #define UDF_MAX_INDIR_EXTS 16
2093 
2094 int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
2095 		     struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2096 {
2097 	int8_t etype;
2098 	unsigned int indirections = 0;
2099 
2100 	while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
2101 	       (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
2102 		int block;
2103 
2104 		if (++indirections > UDF_MAX_INDIR_EXTS) {
2105 			udf_err(inode->i_sb,
2106 				"too many indirect extents in inode %lu\n",
2107 				inode->i_ino);
2108 			return -1;
2109 		}
2110 
2111 		epos->block = *eloc;
2112 		epos->offset = sizeof(struct allocExtDesc);
2113 		brelse(epos->bh);
2114 		block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
2115 		epos->bh = udf_tread(inode->i_sb, block);
2116 		if (!epos->bh) {
2117 			udf_debug("reading block %d failed!\n", block);
2118 			return -1;
2119 		}
2120 	}
2121 
2122 	return etype;
2123 }
2124 
2125 int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
2126 			struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2127 {
2128 	int alen;
2129 	int8_t etype;
2130 	uint8_t *ptr;
2131 	struct short_ad *sad;
2132 	struct long_ad *lad;
2133 	struct udf_inode_info *iinfo = UDF_I(inode);
2134 
2135 	if (!epos->bh) {
2136 		if (!epos->offset)
2137 			epos->offset = udf_file_entry_alloc_offset(inode);
2138 		ptr = iinfo->i_ext.i_data + epos->offset -
2139 			udf_file_entry_alloc_offset(inode) +
2140 			iinfo->i_lenEAttr;
2141 		alen = udf_file_entry_alloc_offset(inode) +
2142 							iinfo->i_lenAlloc;
2143 	} else {
2144 		if (!epos->offset)
2145 			epos->offset = sizeof(struct allocExtDesc);
2146 		ptr = epos->bh->b_data + epos->offset;
2147 		alen = sizeof(struct allocExtDesc) +
2148 			le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2149 							lengthAllocDescs);
2150 	}
2151 
2152 	switch (iinfo->i_alloc_type) {
2153 	case ICBTAG_FLAG_AD_SHORT:
2154 		sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2155 		if (!sad)
2156 			return -1;
2157 		etype = le32_to_cpu(sad->extLength) >> 30;
2158 		eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
2159 		eloc->partitionReferenceNum =
2160 				iinfo->i_location.partitionReferenceNum;
2161 		*elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2162 		break;
2163 	case ICBTAG_FLAG_AD_LONG:
2164 		lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2165 		if (!lad)
2166 			return -1;
2167 		etype = le32_to_cpu(lad->extLength) >> 30;
2168 		*eloc = lelb_to_cpu(lad->extLocation);
2169 		*elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2170 		break;
2171 	default:
2172 		udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
2173 		return -1;
2174 	}
2175 
2176 	return etype;
2177 }
2178 
2179 static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
2180 			      struct kernel_lb_addr neloc, uint32_t nelen)
2181 {
2182 	struct kernel_lb_addr oeloc;
2183 	uint32_t oelen;
2184 	int8_t etype;
2185 
2186 	if (epos.bh)
2187 		get_bh(epos.bh);
2188 
2189 	while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
2190 		udf_write_aext(inode, &epos, &neloc, nelen, 1);
2191 		neloc = oeloc;
2192 		nelen = (etype << 30) | oelen;
2193 	}
2194 	udf_add_aext(inode, &epos, &neloc, nelen, 1);
2195 	brelse(epos.bh);
2196 
2197 	return (nelen >> 30);
2198 }
2199 
2200 int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
2201 		       struct kernel_lb_addr eloc, uint32_t elen)
2202 {
2203 	struct extent_position oepos;
2204 	int adsize;
2205 	int8_t etype;
2206 	struct allocExtDesc *aed;
2207 	struct udf_inode_info *iinfo;
2208 
2209 	if (epos.bh) {
2210 		get_bh(epos.bh);
2211 		get_bh(epos.bh);
2212 	}
2213 
2214 	iinfo = UDF_I(inode);
2215 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2216 		adsize = sizeof(struct short_ad);
2217 	else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2218 		adsize = sizeof(struct long_ad);
2219 	else
2220 		adsize = 0;
2221 
2222 	oepos = epos;
2223 	if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
2224 		return -1;
2225 
2226 	while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
2227 		udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
2228 		if (oepos.bh != epos.bh) {
2229 			oepos.block = epos.block;
2230 			brelse(oepos.bh);
2231 			get_bh(epos.bh);
2232 			oepos.bh = epos.bh;
2233 			oepos.offset = epos.offset - adsize;
2234 		}
2235 	}
2236 	memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
2237 	elen = 0;
2238 
2239 	if (epos.bh != oepos.bh) {
2240 		udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2241 		udf_write_aext(inode, &oepos, &eloc, elen, 1);
2242 		udf_write_aext(inode, &oepos, &eloc, elen, 1);
2243 		if (!oepos.bh) {
2244 			iinfo->i_lenAlloc -= (adsize * 2);
2245 			mark_inode_dirty(inode);
2246 		} else {
2247 			aed = (struct allocExtDesc *)oepos.bh->b_data;
2248 			le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
2249 			if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2250 			    UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2251 				udf_update_tag(oepos.bh->b_data,
2252 						oepos.offset - (2 * adsize));
2253 			else
2254 				udf_update_tag(oepos.bh->b_data,
2255 						sizeof(struct allocExtDesc));
2256 			mark_buffer_dirty_inode(oepos.bh, inode);
2257 		}
2258 	} else {
2259 		udf_write_aext(inode, &oepos, &eloc, elen, 1);
2260 		if (!oepos.bh) {
2261 			iinfo->i_lenAlloc -= adsize;
2262 			mark_inode_dirty(inode);
2263 		} else {
2264 			aed = (struct allocExtDesc *)oepos.bh->b_data;
2265 			le32_add_cpu(&aed->lengthAllocDescs, -adsize);
2266 			if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2267 			    UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2268 				udf_update_tag(oepos.bh->b_data,
2269 						epos.offset - adsize);
2270 			else
2271 				udf_update_tag(oepos.bh->b_data,
2272 						sizeof(struct allocExtDesc));
2273 			mark_buffer_dirty_inode(oepos.bh, inode);
2274 		}
2275 	}
2276 
2277 	brelse(epos.bh);
2278 	brelse(oepos.bh);
2279 
2280 	return (elen >> 30);
2281 }
2282 
2283 int8_t inode_bmap(struct inode *inode, sector_t block,
2284 		  struct extent_position *pos, struct kernel_lb_addr *eloc,
2285 		  uint32_t *elen, sector_t *offset)
2286 {
2287 	unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2288 	loff_t lbcount = 0, bcount =
2289 	    (loff_t) block << blocksize_bits;
2290 	int8_t etype;
2291 	struct udf_inode_info *iinfo;
2292 
2293 	iinfo = UDF_I(inode);
2294 	if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2295 		pos->offset = 0;
2296 		pos->block = iinfo->i_location;
2297 		pos->bh = NULL;
2298 	}
2299 	*elen = 0;
2300 	do {
2301 		etype = udf_next_aext(inode, pos, eloc, elen, 1);
2302 		if (etype == -1) {
2303 			*offset = (bcount - lbcount) >> blocksize_bits;
2304 			iinfo->i_lenExtents = lbcount;
2305 			return -1;
2306 		}
2307 		lbcount += *elen;
2308 	} while (lbcount <= bcount);
2309 	/* update extent cache */
2310 	udf_update_extent_cache(inode, lbcount - *elen, pos, 1);
2311 	*offset = (bcount + *elen - lbcount) >> blocksize_bits;
2312 
2313 	return etype;
2314 }
2315 
2316 long udf_block_map(struct inode *inode, sector_t block)
2317 {
2318 	struct kernel_lb_addr eloc;
2319 	uint32_t elen;
2320 	sector_t offset;
2321 	struct extent_position epos = {};
2322 	int ret;
2323 
2324 	down_read(&UDF_I(inode)->i_data_sem);
2325 
2326 	if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2327 						(EXT_RECORDED_ALLOCATED >> 30))
2328 		ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
2329 	else
2330 		ret = 0;
2331 
2332 	up_read(&UDF_I(inode)->i_data_sem);
2333 	brelse(epos.bh);
2334 
2335 	if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2336 		return udf_fixed_to_variable(ret);
2337 	else
2338 		return ret;
2339 }
2340