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