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