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