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