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