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