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