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