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