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