GNU Linux-libre 4.14.302-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, int,
56                               struct kernel_long_ad *, int *);
57 static void udf_prealloc_extents(struct inode *, int, int,
58                                  struct kernel_long_ad *, int *);
59 static void udf_merge_extents(struct inode *, struct kernel_long_ad *, int *);
60 static 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, int *block,
322                                            int *err)
323 {
324         int newblock;
325         struct buffer_head *dbh = NULL;
326         struct kernel_lb_addr eloc;
327         uint8_t alloctype;
328         struct extent_position epos;
329
330         struct udf_fileident_bh sfibh, dfibh;
331         loff_t f_pos = udf_ext0_offset(inode);
332         int size = udf_ext0_offset(inode) + inode->i_size;
333         struct fileIdentDesc cfi, *sfi, *dfi;
334         struct udf_inode_info *iinfo = UDF_I(inode);
335
336         if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
337                 alloctype = ICBTAG_FLAG_AD_SHORT;
338         else
339                 alloctype = ICBTAG_FLAG_AD_LONG;
340
341         if (!inode->i_size) {
342                 iinfo->i_alloc_type = alloctype;
343                 mark_inode_dirty(inode);
344                 return NULL;
345         }
346
347         /* alloc block, and copy data to it */
348         *block = udf_new_block(inode->i_sb, inode,
349                                iinfo->i_location.partitionReferenceNum,
350                                iinfo->i_location.logicalBlockNum, err);
351         if (!(*block))
352                 return NULL;
353         newblock = udf_get_pblock(inode->i_sb, *block,
354                                   iinfo->i_location.partitionReferenceNum,
355                                 0);
356         if (!newblock)
357                 return NULL;
358         dbh = udf_tgetblk(inode->i_sb, newblock);
359         if (!dbh)
360                 return NULL;
361         lock_buffer(dbh);
362         memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
363         set_buffer_uptodate(dbh);
364         unlock_buffer(dbh);
365         mark_buffer_dirty_inode(dbh, inode);
366
367         sfibh.soffset = sfibh.eoffset =
368                         f_pos & (inode->i_sb->s_blocksize - 1);
369         sfibh.sbh = sfibh.ebh = NULL;
370         dfibh.soffset = dfibh.eoffset = 0;
371         dfibh.sbh = dfibh.ebh = dbh;
372         while (f_pos < size) {
373                 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
374                 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
375                                          NULL, NULL, NULL);
376                 if (!sfi) {
377                         brelse(dbh);
378                         return NULL;
379                 }
380                 iinfo->i_alloc_type = alloctype;
381                 sfi->descTag.tagLocation = cpu_to_le32(*block);
382                 dfibh.soffset = dfibh.eoffset;
383                 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
384                 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
385                 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
386                                  sfi->fileIdent +
387                                         le16_to_cpu(sfi->lengthOfImpUse))) {
388                         iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
389                         brelse(dbh);
390                         return NULL;
391                 }
392         }
393         mark_buffer_dirty_inode(dbh, inode);
394
395         memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
396                 iinfo->i_lenAlloc);
397         iinfo->i_lenAlloc = 0;
398         eloc.logicalBlockNum = *block;
399         eloc.partitionReferenceNum =
400                                 iinfo->i_location.partitionReferenceNum;
401         iinfo->i_lenExtents = inode->i_size;
402         epos.bh = NULL;
403         epos.block = iinfo->i_location;
404         epos.offset = udf_file_entry_alloc_offset(inode);
405         udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
406         /* UniqueID stuff */
407
408         brelse(epos.bh);
409         mark_inode_dirty(inode);
410         return dbh;
411 }
412
413 static int udf_get_block(struct inode *inode, sector_t block,
414                          struct buffer_head *bh_result, int create)
415 {
416         int err, new;
417         sector_t phys = 0;
418         struct udf_inode_info *iinfo;
419
420         if (!create) {
421                 phys = udf_block_map(inode, block);
422                 if (phys)
423                         map_bh(bh_result, inode->i_sb, phys);
424                 return 0;
425         }
426
427         err = -EIO;
428         new = 0;
429         iinfo = UDF_I(inode);
430
431         down_write(&iinfo->i_data_sem);
432         if (block == iinfo->i_next_alloc_block + 1) {
433                 iinfo->i_next_alloc_block++;
434                 iinfo->i_next_alloc_goal++;
435         }
436
437         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, long 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         int prealloc_len = 0;
488         struct udf_inode_info *iinfo;
489         int err;
490
491         /* The previous extent is fake and we should not extend by anything
492          * - there's nothing to do... */
493         if (!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         uint32_t newblocknum, newblock;
697         sector_t offset = 0;
698         int8_t etype;
699         struct udf_inode_info *iinfo = UDF_I(inode);
700         int 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                               int newblocknum, struct kernel_long_ad *laarr,
915                               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, laarr[i].extLocation,
1183                                         laarr[i].extLength);
1184         } else if (startnum < endnum) {
1185                 for (i = 0; i < (endnum - startnum); i++) {
1186                         udf_insert_aext(inode, *epos, laarr[i].extLocation,
1187                                         laarr[i].extLength);
1188                         udf_next_aext(inode, epos, &laarr[i].extLocation,
1189                                       &laarr[i].extLength, 1);
1190                         start++;
1191                 }
1192         }
1193
1194         for (i = start; i < endnum; i++) {
1195                 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
1196                 udf_write_aext(inode, epos, &laarr[i].extLocation,
1197                                laarr[i].extLength, 1);
1198         }
1199 }
1200
1201 struct buffer_head *udf_bread(struct inode *inode, int block,
1202                               int create, int *err)
1203 {
1204         struct buffer_head *bh = NULL;
1205
1206         bh = udf_getblk(inode, block, create, err);
1207         if (!bh)
1208                 return NULL;
1209
1210         if (buffer_uptodate(bh))
1211                 return bh;
1212
1213         ll_rw_block(REQ_OP_READ, 0, 1, &bh);
1214
1215         wait_on_buffer(bh);
1216         if (buffer_uptodate(bh))
1217                 return bh;
1218
1219         brelse(bh);
1220         *err = -EIO;
1221         return NULL;
1222 }
1223
1224 int udf_setsize(struct inode *inode, loff_t newsize)
1225 {
1226         int err;
1227         struct udf_inode_info *iinfo;
1228         int bsize = i_blocksize(inode);
1229
1230         if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1231               S_ISLNK(inode->i_mode)))
1232                 return -EINVAL;
1233         if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1234                 return -EPERM;
1235
1236         iinfo = UDF_I(inode);
1237         if (newsize > inode->i_size) {
1238                 down_write(&iinfo->i_data_sem);
1239                 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1240                         if (bsize <
1241                             (udf_file_entry_alloc_offset(inode) + newsize)) {
1242                                 err = udf_expand_file_adinicb(inode);
1243                                 if (err)
1244                                         return err;
1245                                 down_write(&iinfo->i_data_sem);
1246                         } else {
1247                                 iinfo->i_lenAlloc = newsize;
1248                                 goto set_size;
1249                         }
1250                 }
1251                 err = udf_extend_file(inode, newsize);
1252                 if (err) {
1253                         up_write(&iinfo->i_data_sem);
1254                         return err;
1255                 }
1256 set_size:
1257                 up_write(&iinfo->i_data_sem);
1258                 truncate_setsize(inode, newsize);
1259         } else {
1260                 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1261                         down_write(&iinfo->i_data_sem);
1262                         udf_clear_extent_cache(inode);
1263                         memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1264                                0x00, bsize - newsize -
1265                                udf_file_entry_alloc_offset(inode));
1266                         iinfo->i_lenAlloc = newsize;
1267                         truncate_setsize(inode, newsize);
1268                         up_write(&iinfo->i_data_sem);
1269                         goto update_time;
1270                 }
1271                 err = block_truncate_page(inode->i_mapping, newsize,
1272                                           udf_get_block);
1273                 if (err)
1274                         return err;
1275                 truncate_setsize(inode, newsize);
1276                 down_write(&iinfo->i_data_sem);
1277                 udf_clear_extent_cache(inode);
1278                 udf_truncate_extents(inode);
1279                 up_write(&iinfo->i_data_sem);
1280         }
1281 update_time:
1282         inode->i_mtime = inode->i_ctime = current_time(inode);
1283         if (IS_SYNC(inode))
1284                 udf_sync_inode(inode);
1285         else
1286                 mark_inode_dirty(inode);
1287         return 0;
1288 }
1289
1290 /*
1291  * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1292  * arbitrary - just that we hopefully don't limit any real use of rewritten
1293  * inode on write-once media but avoid looping for too long on corrupted media.
1294  */
1295 #define UDF_MAX_ICB_NESTING 1024
1296
1297 static int udf_read_inode(struct inode *inode, bool hidden_inode)
1298 {
1299         struct buffer_head *bh = NULL;
1300         struct fileEntry *fe;
1301         struct extendedFileEntry *efe;
1302         uint16_t ident;
1303         struct udf_inode_info *iinfo = UDF_I(inode);
1304         struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1305         struct kernel_lb_addr *iloc = &iinfo->i_location;
1306         unsigned int link_count;
1307         unsigned int indirections = 0;
1308         int bs = inode->i_sb->s_blocksize;
1309         int ret = -EIO;
1310
1311 reread:
1312         if (iloc->partitionReferenceNum >= sbi->s_partitions) {
1313                 udf_debug("partition reference: %d > logical volume partitions: %d\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=%d, partition=%d 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 %ld) 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 %ld) failed ident=%d\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: %d\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         i_uid_write(inode, le32_to_cpu(fe->uid));
1442         if (!uid_valid(inode->i_uid) ||
1443             UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1444             UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
1445                 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
1446
1447         i_gid_write(inode, le32_to_cpu(fe->gid));
1448         if (!gid_valid(inode->i_gid) ||
1449             UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1450             UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
1451                 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
1452
1453         if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
1454                         sbi->s_fmode != UDF_INVALID_MODE)
1455                 inode->i_mode = sbi->s_fmode;
1456         else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
1457                         sbi->s_dmode != UDF_INVALID_MODE)
1458                 inode->i_mode = sbi->s_dmode;
1459         else
1460                 inode->i_mode = udf_convert_permissions(fe);
1461         inode->i_mode &= ~sbi->s_umask;
1462         read_unlock(&sbi->s_cred_lock);
1463
1464         link_count = le16_to_cpu(fe->fileLinkCount);
1465         if (!link_count) {
1466                 if (!hidden_inode) {
1467                         ret = -ESTALE;
1468                         goto out;
1469                 }
1470                 link_count = 1;
1471         }
1472         set_nlink(inode, link_count);
1473
1474         inode->i_size = le64_to_cpu(fe->informationLength);
1475         iinfo->i_lenExtents = inode->i_size;
1476
1477         if (iinfo->i_efe == 0) {
1478                 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
1479                         (inode->i_sb->s_blocksize_bits - 9);
1480
1481                 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
1482                         inode->i_atime = sbi->s_record_time;
1483
1484                 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1485                                             fe->modificationTime))
1486                         inode->i_mtime = sbi->s_record_time;
1487
1488                 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
1489                         inode->i_ctime = sbi->s_record_time;
1490
1491                 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1492                 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1493                 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1494                 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
1495         } else {
1496                 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
1497                     (inode->i_sb->s_blocksize_bits - 9);
1498
1499                 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
1500                         inode->i_atime = sbi->s_record_time;
1501
1502                 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1503                                             efe->modificationTime))
1504                         inode->i_mtime = sbi->s_record_time;
1505
1506                 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
1507                         iinfo->i_crtime = sbi->s_record_time;
1508
1509                 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
1510                         inode->i_ctime = sbi->s_record_time;
1511
1512                 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1513                 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1514                 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
1515                 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
1516         }
1517         inode->i_generation = iinfo->i_unique;
1518
1519         /*
1520          * Sanity check length of allocation descriptors and extended attrs to
1521          * avoid integer overflows
1522          */
1523         if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
1524                 goto out;
1525         /* Now do exact checks */
1526         if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
1527                 goto out;
1528         /* Sanity checks for files in ICB so that we don't get confused later */
1529         if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1530                 /*
1531                  * For file in ICB data is stored in allocation descriptor
1532                  * so sizes should match
1533                  */
1534                 if (iinfo->i_lenAlloc != inode->i_size)
1535                         goto out;
1536                 /* File in ICB has to fit in there... */
1537                 if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
1538                         goto out;
1539         }
1540
1541         switch (fe->icbTag.fileType) {
1542         case ICBTAG_FILE_TYPE_DIRECTORY:
1543                 inode->i_op = &udf_dir_inode_operations;
1544                 inode->i_fop = &udf_dir_operations;
1545                 inode->i_mode |= S_IFDIR;
1546                 inc_nlink(inode);
1547                 break;
1548         case ICBTAG_FILE_TYPE_REALTIME:
1549         case ICBTAG_FILE_TYPE_REGULAR:
1550         case ICBTAG_FILE_TYPE_UNDEF:
1551         case ICBTAG_FILE_TYPE_VAT20:
1552                 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1553                         inode->i_data.a_ops = &udf_adinicb_aops;
1554                 else
1555                         inode->i_data.a_ops = &udf_aops;
1556                 inode->i_op = &udf_file_inode_operations;
1557                 inode->i_fop = &udf_file_operations;
1558                 inode->i_mode |= S_IFREG;
1559                 break;
1560         case ICBTAG_FILE_TYPE_BLOCK:
1561                 inode->i_mode |= S_IFBLK;
1562                 break;
1563         case ICBTAG_FILE_TYPE_CHAR:
1564                 inode->i_mode |= S_IFCHR;
1565                 break;
1566         case ICBTAG_FILE_TYPE_FIFO:
1567                 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1568                 break;
1569         case ICBTAG_FILE_TYPE_SOCKET:
1570                 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1571                 break;
1572         case ICBTAG_FILE_TYPE_SYMLINK:
1573                 inode->i_data.a_ops = &udf_symlink_aops;
1574                 inode->i_op = &udf_symlink_inode_operations;
1575                 inode_nohighmem(inode);
1576                 inode->i_mode = S_IFLNK | 0777;
1577                 break;
1578         case ICBTAG_FILE_TYPE_MAIN:
1579                 udf_debug("METADATA FILE-----\n");
1580                 break;
1581         case ICBTAG_FILE_TYPE_MIRROR:
1582                 udf_debug("METADATA MIRROR FILE-----\n");
1583                 break;
1584         case ICBTAG_FILE_TYPE_BITMAP:
1585                 udf_debug("METADATA BITMAP FILE-----\n");
1586                 break;
1587         default:
1588                 udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n",
1589                         inode->i_ino, fe->icbTag.fileType);
1590                 goto out;
1591         }
1592         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1593                 struct deviceSpec *dsea =
1594                         (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1595                 if (dsea) {
1596                         init_special_inode(inode, inode->i_mode,
1597                                 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1598                                       le32_to_cpu(dsea->minorDeviceIdent)));
1599                         /* Developer ID ??? */
1600                 } else
1601                         goto out;
1602         }
1603         ret = 0;
1604 out:
1605         brelse(bh);
1606         return ret;
1607 }
1608
1609 static int udf_alloc_i_data(struct inode *inode, size_t size)
1610 {
1611         struct udf_inode_info *iinfo = UDF_I(inode);
1612         iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
1613         if (!iinfo->i_ext.i_data)
1614                 return -ENOMEM;
1615         return 0;
1616 }
1617
1618 static umode_t udf_convert_permissions(struct fileEntry *fe)
1619 {
1620         umode_t mode;
1621         uint32_t permissions;
1622         uint32_t flags;
1623
1624         permissions = le32_to_cpu(fe->permissions);
1625         flags = le16_to_cpu(fe->icbTag.flags);
1626
1627         mode =  ((permissions) & 0007) |
1628                 ((permissions >> 2) & 0070) |
1629                 ((permissions >> 4) & 0700) |
1630                 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1631                 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1632                 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
1633
1634         return mode;
1635 }
1636
1637 int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
1638 {
1639         return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1640 }
1641
1642 static int udf_sync_inode(struct inode *inode)
1643 {
1644         return udf_update_inode(inode, 1);
1645 }
1646
1647 static void udf_adjust_time(struct udf_inode_info *iinfo, struct timespec time)
1648 {
1649         if (iinfo->i_crtime.tv_sec > time.tv_sec ||
1650             (iinfo->i_crtime.tv_sec == time.tv_sec &&
1651              iinfo->i_crtime.tv_nsec > time.tv_nsec))
1652                 iinfo->i_crtime = time;
1653 }
1654
1655 static int udf_update_inode(struct inode *inode, int do_sync)
1656 {
1657         struct buffer_head *bh = NULL;
1658         struct fileEntry *fe;
1659         struct extendedFileEntry *efe;
1660         uint64_t lb_recorded;
1661         uint32_t udfperms;
1662         uint16_t icbflags;
1663         uint16_t crclen;
1664         int err = 0;
1665         struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1666         unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1667         struct udf_inode_info *iinfo = UDF_I(inode);
1668
1669         bh = udf_tgetblk(inode->i_sb,
1670                         udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
1671         if (!bh) {
1672                 udf_debug("getblk failure\n");
1673                 return -EIO;
1674         }
1675
1676         lock_buffer(bh);
1677         memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1678         fe = (struct fileEntry *)bh->b_data;
1679         efe = (struct extendedFileEntry *)bh->b_data;
1680
1681         if (iinfo->i_use) {
1682                 struct unallocSpaceEntry *use =
1683                         (struct unallocSpaceEntry *)bh->b_data;
1684
1685                 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1686                 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1687                        iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
1688                                         sizeof(struct unallocSpaceEntry));
1689                 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1690                 crclen = sizeof(struct unallocSpaceEntry);
1691
1692                 goto finish;
1693         }
1694
1695         if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1696                 fe->uid = cpu_to_le32(-1);
1697         else
1698                 fe->uid = cpu_to_le32(i_uid_read(inode));
1699
1700         if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1701                 fe->gid = cpu_to_le32(-1);
1702         else
1703                 fe->gid = cpu_to_le32(i_gid_read(inode));
1704
1705         udfperms = ((inode->i_mode & 0007)) |
1706                    ((inode->i_mode & 0070) << 2) |
1707                    ((inode->i_mode & 0700) << 4);
1708
1709         udfperms |= (le32_to_cpu(fe->permissions) &
1710                     (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1711                      FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1712                      FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
1713         fe->permissions = cpu_to_le32(udfperms);
1714
1715         if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
1716                 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1717         else
1718                 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1719
1720         fe->informationLength = cpu_to_le64(inode->i_size);
1721
1722         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1723                 struct regid *eid;
1724                 struct deviceSpec *dsea =
1725                         (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1726                 if (!dsea) {
1727                         dsea = (struct deviceSpec *)
1728                                 udf_add_extendedattr(inode,
1729                                                      sizeof(struct deviceSpec) +
1730                                                      sizeof(struct regid), 12, 0x3);
1731                         dsea->attrType = cpu_to_le32(12);
1732                         dsea->attrSubtype = 1;
1733                         dsea->attrLength = cpu_to_le32(
1734                                                 sizeof(struct deviceSpec) +
1735                                                 sizeof(struct regid));
1736                         dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
1737                 }
1738                 eid = (struct regid *)dsea->impUse;
1739                 memset(eid, 0, sizeof(*eid));
1740                 strcpy(eid->ident, UDF_ID_DEVELOPER);
1741                 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1742                 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1743                 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1744                 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1745         }
1746
1747         if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1748                 lb_recorded = 0; /* No extents => no blocks! */
1749         else
1750                 lb_recorded =
1751                         (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1752                         (blocksize_bits - 9);
1753
1754         if (iinfo->i_efe == 0) {
1755                 memcpy(bh->b_data + sizeof(struct fileEntry),
1756                        iinfo->i_ext.i_data,
1757                        inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1758                 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1759
1760                 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1761                 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1762                 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
1763                 memset(&(fe->impIdent), 0, sizeof(struct regid));
1764                 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1765                 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1766                 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1767                 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1768                 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1769                 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1770                 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1771                 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1772                 crclen = sizeof(struct fileEntry);
1773         } else {
1774                 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1775                        iinfo->i_ext.i_data,
1776                        inode->i_sb->s_blocksize -
1777                                         sizeof(struct extendedFileEntry));
1778                 efe->objectSize = cpu_to_le64(inode->i_size);
1779                 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1780
1781                 udf_adjust_time(iinfo, inode->i_atime);
1782                 udf_adjust_time(iinfo, inode->i_mtime);
1783                 udf_adjust_time(iinfo, inode->i_ctime);
1784
1785                 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1786                 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1787                 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1788                 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
1789
1790                 memset(&(efe->impIdent), 0, sizeof(efe->impIdent));
1791                 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1792                 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1793                 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1794                 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1795                 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1796                 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1797                 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1798                 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1799                 crclen = sizeof(struct extendedFileEntry);
1800         }
1801
1802 finish:
1803         if (iinfo->i_strat4096) {
1804                 fe->icbTag.strategyType = cpu_to_le16(4096);
1805                 fe->icbTag.strategyParameter = cpu_to_le16(1);
1806                 fe->icbTag.numEntries = cpu_to_le16(2);
1807         } else {
1808                 fe->icbTag.strategyType = cpu_to_le16(4);
1809                 fe->icbTag.numEntries = cpu_to_le16(1);
1810         }
1811
1812         if (iinfo->i_use)
1813                 fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
1814         else if (S_ISDIR(inode->i_mode))
1815                 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1816         else if (S_ISREG(inode->i_mode))
1817                 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1818         else if (S_ISLNK(inode->i_mode))
1819                 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1820         else if (S_ISBLK(inode->i_mode))
1821                 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1822         else if (S_ISCHR(inode->i_mode))
1823                 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1824         else if (S_ISFIFO(inode->i_mode))
1825                 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1826         else if (S_ISSOCK(inode->i_mode))
1827                 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1828
1829         icbflags =      iinfo->i_alloc_type |
1830                         ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1831                         ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1832                         ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1833                         (le16_to_cpu(fe->icbTag.flags) &
1834                                 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1835                                 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
1836
1837         fe->icbTag.flags = cpu_to_le16(icbflags);
1838         if (sbi->s_udfrev >= 0x0200)
1839                 fe->descTag.descVersion = cpu_to_le16(3);
1840         else
1841                 fe->descTag.descVersion = cpu_to_le16(2);
1842         fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
1843         fe->descTag.tagLocation = cpu_to_le32(
1844                                         iinfo->i_location.logicalBlockNum);
1845         crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
1846         fe->descTag.descCRCLength = cpu_to_le16(crclen);
1847         fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
1848                                                   crclen));
1849         fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
1850
1851         set_buffer_uptodate(bh);
1852         unlock_buffer(bh);
1853
1854         /* write the data blocks */
1855         mark_buffer_dirty(bh);
1856         if (do_sync) {
1857                 sync_dirty_buffer(bh);
1858                 if (buffer_write_io_error(bh)) {
1859                         udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1860                                  inode->i_ino);
1861                         err = -EIO;
1862                 }
1863         }
1864         brelse(bh);
1865
1866         return err;
1867 }
1868
1869 struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
1870                          bool hidden_inode)
1871 {
1872         unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1873         struct inode *inode = iget_locked(sb, block);
1874         int err;
1875
1876         if (!inode)
1877                 return ERR_PTR(-ENOMEM);
1878
1879         if (!(inode->i_state & I_NEW))
1880                 return inode;
1881
1882         memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
1883         err = udf_read_inode(inode, hidden_inode);
1884         if (err < 0) {
1885                 iget_failed(inode);
1886                 return ERR_PTR(err);
1887         }
1888         unlock_new_inode(inode);
1889
1890         return inode;
1891 }
1892
1893 int udf_setup_indirect_aext(struct inode *inode, int block,
1894                             struct extent_position *epos)
1895 {
1896         struct super_block *sb = inode->i_sb;
1897         struct buffer_head *bh;
1898         struct allocExtDesc *aed;
1899         struct extent_position nepos;
1900         struct kernel_lb_addr neloc;
1901         int ver, adsize;
1902
1903         if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1904                 adsize = sizeof(struct short_ad);
1905         else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1906                 adsize = sizeof(struct long_ad);
1907         else
1908                 return -EIO;
1909
1910         neloc.logicalBlockNum = block;
1911         neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
1912
1913         bh = udf_tgetblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
1914         if (!bh)
1915                 return -EIO;
1916         lock_buffer(bh);
1917         memset(bh->b_data, 0x00, sb->s_blocksize);
1918         set_buffer_uptodate(bh);
1919         unlock_buffer(bh);
1920         mark_buffer_dirty_inode(bh, inode);
1921
1922         aed = (struct allocExtDesc *)(bh->b_data);
1923         if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
1924                 aed->previousAllocExtLocation =
1925                                 cpu_to_le32(epos->block.logicalBlockNum);
1926         }
1927         aed->lengthAllocDescs = cpu_to_le32(0);
1928         if (UDF_SB(sb)->s_udfrev >= 0x0200)
1929                 ver = 3;
1930         else
1931                 ver = 2;
1932         udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
1933                     sizeof(struct tag));
1934
1935         nepos.block = neloc;
1936         nepos.offset = sizeof(struct allocExtDesc);
1937         nepos.bh = bh;
1938
1939         /*
1940          * Do we have to copy current last extent to make space for indirect
1941          * one?
1942          */
1943         if (epos->offset + adsize > sb->s_blocksize) {
1944                 struct kernel_lb_addr cp_loc;
1945                 uint32_t cp_len;
1946                 int cp_type;
1947
1948                 epos->offset -= adsize;
1949                 cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
1950                 cp_len |= ((uint32_t)cp_type) << 30;
1951
1952                 __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
1953                 udf_write_aext(inode, epos, &nepos.block,
1954                                sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1955         } else {
1956                 __udf_add_aext(inode, epos, &nepos.block,
1957                                sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1958         }
1959
1960         brelse(epos->bh);
1961         *epos = nepos;
1962
1963         return 0;
1964 }
1965
1966 /*
1967  * Append extent at the given position - should be the first free one in inode
1968  * / indirect extent. This function assumes there is enough space in the inode
1969  * or indirect extent. Use udf_add_aext() if you didn't check for this before.
1970  */
1971 int __udf_add_aext(struct inode *inode, struct extent_position *epos,
1972                    struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1973 {
1974         struct udf_inode_info *iinfo = UDF_I(inode);
1975         struct allocExtDesc *aed;
1976         int adsize;
1977
1978         if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1979                 adsize = sizeof(struct short_ad);
1980         else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1981                 adsize = sizeof(struct long_ad);
1982         else
1983                 return -EIO;
1984
1985         if (!epos->bh) {
1986                 WARN_ON(iinfo->i_lenAlloc !=
1987                         epos->offset - udf_file_entry_alloc_offset(inode));
1988         } else {
1989                 aed = (struct allocExtDesc *)epos->bh->b_data;
1990                 WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
1991                         epos->offset - sizeof(struct allocExtDesc));
1992                 WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
1993         }
1994
1995         udf_write_aext(inode, epos, eloc, elen, inc);
1996
1997         if (!epos->bh) {
1998                 iinfo->i_lenAlloc += adsize;
1999                 mark_inode_dirty(inode);
2000         } else {
2001                 aed = (struct allocExtDesc *)epos->bh->b_data;
2002                 le32_add_cpu(&aed->lengthAllocDescs, adsize);
2003                 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2004                                 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2005                         udf_update_tag(epos->bh->b_data,
2006                                         epos->offset + (inc ? 0 : adsize));
2007                 else
2008                         udf_update_tag(epos->bh->b_data,
2009                                         sizeof(struct allocExtDesc));
2010                 mark_buffer_dirty_inode(epos->bh, inode);
2011         }
2012
2013         return 0;
2014 }
2015
2016 /*
2017  * Append extent at given position - should be the first free one in inode
2018  * / indirect extent. Takes care of allocating and linking indirect blocks.
2019  */
2020 int udf_add_aext(struct inode *inode, struct extent_position *epos,
2021                  struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2022 {
2023         int adsize;
2024         struct super_block *sb = inode->i_sb;
2025
2026         if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2027                 adsize = sizeof(struct short_ad);
2028         else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2029                 adsize = sizeof(struct long_ad);
2030         else
2031                 return -EIO;
2032
2033         if (epos->offset + (2 * adsize) > sb->s_blocksize) {
2034                 int err;
2035                 int new_block;
2036
2037                 new_block = udf_new_block(sb, NULL,
2038                                           epos->block.partitionReferenceNum,
2039                                           epos->block.logicalBlockNum, &err);
2040                 if (!new_block)
2041                         return -ENOSPC;
2042
2043                 err = udf_setup_indirect_aext(inode, new_block, epos);
2044                 if (err)
2045                         return err;
2046         }
2047
2048         return __udf_add_aext(inode, epos, eloc, elen, inc);
2049 }
2050
2051 void udf_write_aext(struct inode *inode, struct extent_position *epos,
2052                     struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2053 {
2054         int adsize;
2055         uint8_t *ptr;
2056         struct short_ad *sad;
2057         struct long_ad *lad;
2058         struct udf_inode_info *iinfo = UDF_I(inode);
2059
2060         if (!epos->bh)
2061                 ptr = iinfo->i_ext.i_data + epos->offset -
2062                         udf_file_entry_alloc_offset(inode) +
2063                         iinfo->i_lenEAttr;
2064         else
2065                 ptr = epos->bh->b_data + epos->offset;
2066
2067         switch (iinfo->i_alloc_type) {
2068         case ICBTAG_FLAG_AD_SHORT:
2069                 sad = (struct short_ad *)ptr;
2070                 sad->extLength = cpu_to_le32(elen);
2071                 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
2072                 adsize = sizeof(struct short_ad);
2073                 break;
2074         case ICBTAG_FLAG_AD_LONG:
2075                 lad = (struct long_ad *)ptr;
2076                 lad->extLength = cpu_to_le32(elen);
2077                 lad->extLocation = cpu_to_lelb(*eloc);
2078                 memset(lad->impUse, 0x00, sizeof(lad->impUse));
2079                 adsize = sizeof(struct long_ad);
2080                 break;
2081         default:
2082                 return;
2083         }
2084
2085         if (epos->bh) {
2086                 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2087                     UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
2088                         struct allocExtDesc *aed =
2089                                 (struct allocExtDesc *)epos->bh->b_data;
2090                         udf_update_tag(epos->bh->b_data,
2091                                        le32_to_cpu(aed->lengthAllocDescs) +
2092                                        sizeof(struct allocExtDesc));
2093                 }
2094                 mark_buffer_dirty_inode(epos->bh, inode);
2095         } else {
2096                 mark_inode_dirty(inode);
2097         }
2098
2099         if (inc)
2100                 epos->offset += adsize;
2101 }
2102
2103 /*
2104  * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2105  * someone does some weird stuff.
2106  */
2107 #define UDF_MAX_INDIR_EXTS 16
2108
2109 int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
2110                      struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2111 {
2112         int8_t etype;
2113         unsigned int indirections = 0;
2114
2115         while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
2116                (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
2117                 int block;
2118
2119                 if (++indirections > UDF_MAX_INDIR_EXTS) {
2120                         udf_err(inode->i_sb,
2121                                 "too many indirect extents in inode %lu\n",
2122                                 inode->i_ino);
2123                         return -1;
2124                 }
2125
2126                 epos->block = *eloc;
2127                 epos->offset = sizeof(struct allocExtDesc);
2128                 brelse(epos->bh);
2129                 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
2130                 epos->bh = udf_tread(inode->i_sb, block);
2131                 if (!epos->bh) {
2132                         udf_debug("reading block %d failed!\n", block);
2133                         return -1;
2134                 }
2135         }
2136
2137         return etype;
2138 }
2139
2140 int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
2141                         struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2142 {
2143         int alen;
2144         int8_t etype;
2145         uint8_t *ptr;
2146         struct short_ad *sad;
2147         struct long_ad *lad;
2148         struct udf_inode_info *iinfo = UDF_I(inode);
2149
2150         if (!epos->bh) {
2151                 if (!epos->offset)
2152                         epos->offset = udf_file_entry_alloc_offset(inode);
2153                 ptr = iinfo->i_ext.i_data + epos->offset -
2154                         udf_file_entry_alloc_offset(inode) +
2155                         iinfo->i_lenEAttr;
2156                 alen = udf_file_entry_alloc_offset(inode) +
2157                                                         iinfo->i_lenAlloc;
2158         } else {
2159                 if (!epos->offset)
2160                         epos->offset = sizeof(struct allocExtDesc);
2161                 ptr = epos->bh->b_data + epos->offset;
2162                 alen = sizeof(struct allocExtDesc) +
2163                         le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2164                                                         lengthAllocDescs);
2165         }
2166
2167         switch (iinfo->i_alloc_type) {
2168         case ICBTAG_FLAG_AD_SHORT:
2169                 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2170                 if (!sad)
2171                         return -1;
2172                 etype = le32_to_cpu(sad->extLength) >> 30;
2173                 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
2174                 eloc->partitionReferenceNum =
2175                                 iinfo->i_location.partitionReferenceNum;
2176                 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2177                 break;
2178         case ICBTAG_FLAG_AD_LONG:
2179                 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2180                 if (!lad)
2181                         return -1;
2182                 etype = le32_to_cpu(lad->extLength) >> 30;
2183                 *eloc = lelb_to_cpu(lad->extLocation);
2184                 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2185                 break;
2186         default:
2187                 udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
2188                 return -1;
2189         }
2190
2191         return etype;
2192 }
2193
2194 static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
2195                               struct kernel_lb_addr neloc, uint32_t nelen)
2196 {
2197         struct kernel_lb_addr oeloc;
2198         uint32_t oelen;
2199         int8_t etype;
2200
2201         if (epos.bh)
2202                 get_bh(epos.bh);
2203
2204         while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
2205                 udf_write_aext(inode, &epos, &neloc, nelen, 1);
2206                 neloc = oeloc;
2207                 nelen = (etype << 30) | oelen;
2208         }
2209         udf_add_aext(inode, &epos, &neloc, nelen, 1);
2210         brelse(epos.bh);
2211
2212         return (nelen >> 30);
2213 }
2214
2215 int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
2216                        struct kernel_lb_addr eloc, uint32_t elen)
2217 {
2218         struct extent_position oepos;
2219         int adsize;
2220         int8_t etype;
2221         struct allocExtDesc *aed;
2222         struct udf_inode_info *iinfo;
2223
2224         if (epos.bh) {
2225                 get_bh(epos.bh);
2226                 get_bh(epos.bh);
2227         }
2228
2229         iinfo = UDF_I(inode);
2230         if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2231                 adsize = sizeof(struct short_ad);
2232         else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2233                 adsize = sizeof(struct long_ad);
2234         else
2235                 adsize = 0;
2236
2237         oepos = epos;
2238         if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
2239                 return -1;
2240
2241         while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
2242                 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
2243                 if (oepos.bh != epos.bh) {
2244                         oepos.block = epos.block;
2245                         brelse(oepos.bh);
2246                         get_bh(epos.bh);
2247                         oepos.bh = epos.bh;
2248                         oepos.offset = epos.offset - adsize;
2249                 }
2250         }
2251         memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
2252         elen = 0;
2253
2254         if (epos.bh != oepos.bh) {
2255                 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2256                 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2257                 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2258                 if (!oepos.bh) {
2259                         iinfo->i_lenAlloc -= (adsize * 2);
2260                         mark_inode_dirty(inode);
2261                 } else {
2262                         aed = (struct allocExtDesc *)oepos.bh->b_data;
2263                         le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
2264                         if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2265                             UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2266                                 udf_update_tag(oepos.bh->b_data,
2267                                                 oepos.offset - (2 * adsize));
2268                         else
2269                                 udf_update_tag(oepos.bh->b_data,
2270                                                 sizeof(struct allocExtDesc));
2271                         mark_buffer_dirty_inode(oepos.bh, inode);
2272                 }
2273         } else {
2274                 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2275                 if (!oepos.bh) {
2276                         iinfo->i_lenAlloc -= adsize;
2277                         mark_inode_dirty(inode);
2278                 } else {
2279                         aed = (struct allocExtDesc *)oepos.bh->b_data;
2280                         le32_add_cpu(&aed->lengthAllocDescs, -adsize);
2281                         if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2282                             UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2283                                 udf_update_tag(oepos.bh->b_data,
2284                                                 epos.offset - adsize);
2285                         else
2286                                 udf_update_tag(oepos.bh->b_data,
2287                                                 sizeof(struct allocExtDesc));
2288                         mark_buffer_dirty_inode(oepos.bh, inode);
2289                 }
2290         }
2291
2292         brelse(epos.bh);
2293         brelse(oepos.bh);
2294
2295         return (elen >> 30);
2296 }
2297
2298 int8_t inode_bmap(struct inode *inode, sector_t block,
2299                   struct extent_position *pos, struct kernel_lb_addr *eloc,
2300                   uint32_t *elen, sector_t *offset)
2301 {
2302         unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2303         loff_t lbcount = 0, bcount = (loff_t) block << blocksize_bits;
2304         int8_t etype;
2305         struct udf_inode_info *iinfo;
2306
2307         iinfo = UDF_I(inode);
2308         if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2309                 pos->offset = 0;
2310                 pos->block = iinfo->i_location;
2311                 pos->bh = NULL;
2312         }
2313         *elen = 0;
2314         do {
2315                 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2316                 if (etype == -1) {
2317                         *offset = (bcount - lbcount) >> blocksize_bits;
2318                         iinfo->i_lenExtents = lbcount;
2319                         return -1;
2320                 }
2321                 lbcount += *elen;
2322         } while (lbcount <= bcount);
2323         /* update extent cache */
2324         udf_update_extent_cache(inode, lbcount - *elen, pos);
2325         *offset = (bcount + *elen - lbcount) >> blocksize_bits;
2326
2327         return etype;
2328 }
2329
2330 long udf_block_map(struct inode *inode, sector_t block)
2331 {
2332         struct kernel_lb_addr eloc;
2333         uint32_t elen;
2334         sector_t offset;
2335         struct extent_position epos = {};
2336         int ret;
2337
2338         down_read(&UDF_I(inode)->i_data_sem);
2339
2340         if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2341                                                 (EXT_RECORDED_ALLOCATED >> 30))
2342                 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
2343         else
2344                 ret = 0;
2345
2346         up_read(&UDF_I(inode)->i_data_sem);
2347         brelse(epos.bh);
2348
2349         if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2350                 return udf_fixed_to_variable(ret);
2351         else
2352                 return ret;
2353 }