udf: Factor out trimming of crtime
[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 void udf_adjust_time(struct udf_inode_info *iinfo, struct timespec time)
1616 {
1617         if (iinfo->i_crtime.tv_sec > time.tv_sec ||
1618             (iinfo->i_crtime.tv_sec == time.tv_sec &&
1619              iinfo->i_crtime.tv_nsec > time.tv_nsec))
1620                 iinfo->i_crtime = time;
1621 }
1622
1623 static int udf_update_inode(struct inode *inode, int do_sync)
1624 {
1625         struct buffer_head *bh = NULL;
1626         struct fileEntry *fe;
1627         struct extendedFileEntry *efe;
1628         uint64_t lb_recorded;
1629         uint32_t udfperms;
1630         uint16_t icbflags;
1631         uint16_t crclen;
1632         int err = 0;
1633         struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1634         unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1635         struct udf_inode_info *iinfo = UDF_I(inode);
1636
1637         bh = udf_tgetblk(inode->i_sb,
1638                         udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
1639         if (!bh) {
1640                 udf_debug("getblk failure\n");
1641                 return -EIO;
1642         }
1643
1644         lock_buffer(bh);
1645         memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1646         fe = (struct fileEntry *)bh->b_data;
1647         efe = (struct extendedFileEntry *)bh->b_data;
1648
1649         if (iinfo->i_use) {
1650                 struct unallocSpaceEntry *use =
1651                         (struct unallocSpaceEntry *)bh->b_data;
1652
1653                 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1654                 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1655                        iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
1656                                         sizeof(struct unallocSpaceEntry));
1657                 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1658                 crclen = sizeof(struct unallocSpaceEntry);
1659
1660                 goto finish;
1661         }
1662
1663         if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1664                 fe->uid = cpu_to_le32(-1);
1665         else
1666                 fe->uid = cpu_to_le32(i_uid_read(inode));
1667
1668         if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1669                 fe->gid = cpu_to_le32(-1);
1670         else
1671                 fe->gid = cpu_to_le32(i_gid_read(inode));
1672
1673         udfperms = ((inode->i_mode & S_IRWXO)) |
1674                    ((inode->i_mode & S_IRWXG) << 2) |
1675                    ((inode->i_mode & S_IRWXU) << 4);
1676
1677         udfperms |= (le32_to_cpu(fe->permissions) &
1678                     (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1679                      FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1680                      FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
1681         fe->permissions = cpu_to_le32(udfperms);
1682
1683         if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
1684                 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1685         else
1686                 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1687
1688         fe->informationLength = cpu_to_le64(inode->i_size);
1689
1690         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1691                 struct regid *eid;
1692                 struct deviceSpec *dsea =
1693                         (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1694                 if (!dsea) {
1695                         dsea = (struct deviceSpec *)
1696                                 udf_add_extendedattr(inode,
1697                                                      sizeof(struct deviceSpec) +
1698                                                      sizeof(struct regid), 12, 0x3);
1699                         dsea->attrType = cpu_to_le32(12);
1700                         dsea->attrSubtype = 1;
1701                         dsea->attrLength = cpu_to_le32(
1702                                                 sizeof(struct deviceSpec) +
1703                                                 sizeof(struct regid));
1704                         dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
1705                 }
1706                 eid = (struct regid *)dsea->impUse;
1707                 memset(eid, 0, sizeof(struct regid));
1708                 strcpy(eid->ident, UDF_ID_DEVELOPER);
1709                 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1710                 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1711                 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1712                 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1713         }
1714
1715         if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1716                 lb_recorded = 0; /* No extents => no blocks! */
1717         else
1718                 lb_recorded =
1719                         (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1720                         (blocksize_bits - 9);
1721
1722         if (iinfo->i_efe == 0) {
1723                 memcpy(bh->b_data + sizeof(struct fileEntry),
1724                        iinfo->i_ext.i_data,
1725                        inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1726                 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1727
1728                 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1729                 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1730                 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
1731                 memset(&(fe->impIdent), 0, sizeof(struct regid));
1732                 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1733                 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1734                 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1735                 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1736                 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1737                 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1738                 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1739                 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1740                 crclen = sizeof(struct fileEntry);
1741         } else {
1742                 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1743                        iinfo->i_ext.i_data,
1744                        inode->i_sb->s_blocksize -
1745                                         sizeof(struct extendedFileEntry));
1746                 efe->objectSize = cpu_to_le64(inode->i_size);
1747                 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1748
1749                 udf_adjust_time(iinfo, inode->i_atime);
1750                 udf_adjust_time(iinfo, inode->i_mtime);
1751                 udf_adjust_time(iinfo, inode->i_ctime);
1752
1753                 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1754                 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1755                 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1756                 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
1757
1758                 memset(&(efe->impIdent), 0, sizeof(struct regid));
1759                 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1760                 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1761                 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1762                 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1763                 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1764                 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1765                 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1766                 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1767                 crclen = sizeof(struct extendedFileEntry);
1768         }
1769
1770 finish:
1771         if (iinfo->i_strat4096) {
1772                 fe->icbTag.strategyType = cpu_to_le16(4096);
1773                 fe->icbTag.strategyParameter = cpu_to_le16(1);
1774                 fe->icbTag.numEntries = cpu_to_le16(2);
1775         } else {
1776                 fe->icbTag.strategyType = cpu_to_le16(4);
1777                 fe->icbTag.numEntries = cpu_to_le16(1);
1778         }
1779
1780         if (iinfo->i_use)
1781                 fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
1782         else if (S_ISDIR(inode->i_mode))
1783                 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1784         else if (S_ISREG(inode->i_mode))
1785                 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1786         else if (S_ISLNK(inode->i_mode))
1787                 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1788         else if (S_ISBLK(inode->i_mode))
1789                 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1790         else if (S_ISCHR(inode->i_mode))
1791                 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1792         else if (S_ISFIFO(inode->i_mode))
1793                 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1794         else if (S_ISSOCK(inode->i_mode))
1795                 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1796
1797         icbflags =      iinfo->i_alloc_type |
1798                         ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1799                         ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1800                         ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1801                         (le16_to_cpu(fe->icbTag.flags) &
1802                                 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1803                                 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
1804
1805         fe->icbTag.flags = cpu_to_le16(icbflags);
1806         if (sbi->s_udfrev >= 0x0200)
1807                 fe->descTag.descVersion = cpu_to_le16(3);
1808         else
1809                 fe->descTag.descVersion = cpu_to_le16(2);
1810         fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
1811         fe->descTag.tagLocation = cpu_to_le32(
1812                                         iinfo->i_location.logicalBlockNum);
1813         crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
1814         fe->descTag.descCRCLength = cpu_to_le16(crclen);
1815         fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
1816                                                   crclen));
1817         fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
1818
1819         set_buffer_uptodate(bh);
1820         unlock_buffer(bh);
1821
1822         /* write the data blocks */
1823         mark_buffer_dirty(bh);
1824         if (do_sync) {
1825                 sync_dirty_buffer(bh);
1826                 if (buffer_write_io_error(bh)) {
1827                         udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1828                                  inode->i_ino);
1829                         err = -EIO;
1830                 }
1831         }
1832         brelse(bh);
1833
1834         return err;
1835 }
1836
1837 struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
1838                          bool hidden_inode)
1839 {
1840         unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1841         struct inode *inode = iget_locked(sb, block);
1842         int err;
1843
1844         if (!inode)
1845                 return ERR_PTR(-ENOMEM);
1846
1847         if (!(inode->i_state & I_NEW))
1848                 return inode;
1849
1850         memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
1851         err = udf_read_inode(inode, hidden_inode);
1852         if (err < 0) {
1853                 iget_failed(inode);
1854                 return ERR_PTR(err);
1855         }
1856         unlock_new_inode(inode);
1857
1858         return inode;
1859 }
1860
1861 int udf_setup_indirect_aext(struct inode *inode, int block,
1862                             struct extent_position *epos)
1863 {
1864         struct super_block *sb = inode->i_sb;
1865         struct buffer_head *bh;
1866         struct allocExtDesc *aed;
1867         struct extent_position nepos;
1868         struct kernel_lb_addr neloc;
1869         int ver, adsize;
1870
1871         if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1872                 adsize = sizeof(struct short_ad);
1873         else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1874                 adsize = sizeof(struct long_ad);
1875         else
1876                 return -EIO;
1877
1878         neloc.logicalBlockNum = block;
1879         neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
1880
1881         bh = udf_tgetblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
1882         if (!bh)
1883                 return -EIO;
1884         lock_buffer(bh);
1885         memset(bh->b_data, 0x00, sb->s_blocksize);
1886         set_buffer_uptodate(bh);
1887         unlock_buffer(bh);
1888         mark_buffer_dirty_inode(bh, inode);
1889
1890         aed = (struct allocExtDesc *)(bh->b_data);
1891         if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
1892                 aed->previousAllocExtLocation =
1893                                 cpu_to_le32(epos->block.logicalBlockNum);
1894         }
1895         aed->lengthAllocDescs = cpu_to_le32(0);
1896         if (UDF_SB(sb)->s_udfrev >= 0x0200)
1897                 ver = 3;
1898         else
1899                 ver = 2;
1900         udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
1901                     sizeof(struct tag));
1902
1903         nepos.block = neloc;
1904         nepos.offset = sizeof(struct allocExtDesc);
1905         nepos.bh = bh;
1906
1907         /*
1908          * Do we have to copy current last extent to make space for indirect
1909          * one?
1910          */
1911         if (epos->offset + adsize > sb->s_blocksize) {
1912                 struct kernel_lb_addr cp_loc;
1913                 uint32_t cp_len;
1914                 int cp_type;
1915
1916                 epos->offset -= adsize;
1917                 cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
1918                 cp_len |= ((uint32_t)cp_type) << 30;
1919
1920                 __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
1921                 udf_write_aext(inode, epos, &nepos.block,
1922                                sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1923         } else {
1924                 __udf_add_aext(inode, epos, &nepos.block,
1925                                sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1926         }
1927
1928         brelse(epos->bh);
1929         *epos = nepos;
1930
1931         return 0;
1932 }
1933
1934 /*
1935  * Append extent at the given position - should be the first free one in inode
1936  * / indirect extent. This function assumes there is enough space in the inode
1937  * or indirect extent. Use udf_add_aext() if you didn't check for this before.
1938  */
1939 int __udf_add_aext(struct inode *inode, struct extent_position *epos,
1940                    struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1941 {
1942         struct udf_inode_info *iinfo = UDF_I(inode);
1943         struct allocExtDesc *aed;
1944         int adsize;
1945
1946         if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1947                 adsize = sizeof(struct short_ad);
1948         else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1949                 adsize = sizeof(struct long_ad);
1950         else
1951                 return -EIO;
1952
1953         if (!epos->bh) {
1954                 WARN_ON(iinfo->i_lenAlloc !=
1955                         epos->offset - udf_file_entry_alloc_offset(inode));
1956         } else {
1957                 aed = (struct allocExtDesc *)epos->bh->b_data;
1958                 WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
1959                         epos->offset - sizeof(struct allocExtDesc));
1960                 WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
1961         }
1962
1963         udf_write_aext(inode, epos, eloc, elen, inc);
1964
1965         if (!epos->bh) {
1966                 iinfo->i_lenAlloc += adsize;
1967                 mark_inode_dirty(inode);
1968         } else {
1969                 aed = (struct allocExtDesc *)epos->bh->b_data;
1970                 le32_add_cpu(&aed->lengthAllocDescs, adsize);
1971                 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1972                                 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1973                         udf_update_tag(epos->bh->b_data,
1974                                         epos->offset + (inc ? 0 : adsize));
1975                 else
1976                         udf_update_tag(epos->bh->b_data,
1977                                         sizeof(struct allocExtDesc));
1978                 mark_buffer_dirty_inode(epos->bh, inode);
1979         }
1980
1981         return 0;
1982 }
1983
1984 /*
1985  * Append extent at given position - should be the first free one in inode
1986  * / indirect extent. Takes care of allocating and linking indirect blocks.
1987  */
1988 int udf_add_aext(struct inode *inode, struct extent_position *epos,
1989                  struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1990 {
1991         int adsize;
1992         struct super_block *sb = inode->i_sb;
1993
1994         if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1995                 adsize = sizeof(struct short_ad);
1996         else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1997                 adsize = sizeof(struct long_ad);
1998         else
1999                 return -EIO;
2000
2001         if (epos->offset + (2 * adsize) > sb->s_blocksize) {
2002                 int err;
2003                 int new_block;
2004
2005                 new_block = udf_new_block(sb, NULL,
2006                                           epos->block.partitionReferenceNum,
2007                                           epos->block.logicalBlockNum, &err);
2008                 if (!new_block)
2009                         return -ENOSPC;
2010
2011                 err = udf_setup_indirect_aext(inode, new_block, epos);
2012                 if (err)
2013                         return err;
2014         }
2015
2016         return __udf_add_aext(inode, epos, eloc, elen, inc);
2017 }
2018
2019 void udf_write_aext(struct inode *inode, struct extent_position *epos,
2020                     struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2021 {
2022         int adsize;
2023         uint8_t *ptr;
2024         struct short_ad *sad;
2025         struct long_ad *lad;
2026         struct udf_inode_info *iinfo = UDF_I(inode);
2027
2028         if (!epos->bh)
2029                 ptr = iinfo->i_ext.i_data + epos->offset -
2030                         udf_file_entry_alloc_offset(inode) +
2031                         iinfo->i_lenEAttr;
2032         else
2033                 ptr = epos->bh->b_data + epos->offset;
2034
2035         switch (iinfo->i_alloc_type) {
2036         case ICBTAG_FLAG_AD_SHORT:
2037                 sad = (struct short_ad *)ptr;
2038                 sad->extLength = cpu_to_le32(elen);
2039                 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
2040                 adsize = sizeof(struct short_ad);
2041                 break;
2042         case ICBTAG_FLAG_AD_LONG:
2043                 lad = (struct long_ad *)ptr;
2044                 lad->extLength = cpu_to_le32(elen);
2045                 lad->extLocation = cpu_to_lelb(*eloc);
2046                 memset(lad->impUse, 0x00, sizeof(lad->impUse));
2047                 adsize = sizeof(struct long_ad);
2048                 break;
2049         default:
2050                 return;
2051         }
2052
2053         if (epos->bh) {
2054                 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2055                     UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
2056                         struct allocExtDesc *aed =
2057                                 (struct allocExtDesc *)epos->bh->b_data;
2058                         udf_update_tag(epos->bh->b_data,
2059                                        le32_to_cpu(aed->lengthAllocDescs) +
2060                                        sizeof(struct allocExtDesc));
2061                 }
2062                 mark_buffer_dirty_inode(epos->bh, inode);
2063         } else {
2064                 mark_inode_dirty(inode);
2065         }
2066
2067         if (inc)
2068                 epos->offset += adsize;
2069 }
2070
2071 /*
2072  * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2073  * someone does some weird stuff.
2074  */
2075 #define UDF_MAX_INDIR_EXTS 16
2076
2077 int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
2078                      struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2079 {
2080         int8_t etype;
2081         unsigned int indirections = 0;
2082
2083         while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
2084                (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
2085                 int block;
2086
2087                 if (++indirections > UDF_MAX_INDIR_EXTS) {
2088                         udf_err(inode->i_sb,
2089                                 "too many indirect extents in inode %lu\n",
2090                                 inode->i_ino);
2091                         return -1;
2092                 }
2093
2094                 epos->block = *eloc;
2095                 epos->offset = sizeof(struct allocExtDesc);
2096                 brelse(epos->bh);
2097                 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
2098                 epos->bh = udf_tread(inode->i_sb, block);
2099                 if (!epos->bh) {
2100                         udf_debug("reading block %d failed!\n", block);
2101                         return -1;
2102                 }
2103         }
2104
2105         return etype;
2106 }
2107
2108 int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
2109                         struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2110 {
2111         int alen;
2112         int8_t etype;
2113         uint8_t *ptr;
2114         struct short_ad *sad;
2115         struct long_ad *lad;
2116         struct udf_inode_info *iinfo = UDF_I(inode);
2117
2118         if (!epos->bh) {
2119                 if (!epos->offset)
2120                         epos->offset = udf_file_entry_alloc_offset(inode);
2121                 ptr = iinfo->i_ext.i_data + epos->offset -
2122                         udf_file_entry_alloc_offset(inode) +
2123                         iinfo->i_lenEAttr;
2124                 alen = udf_file_entry_alloc_offset(inode) +
2125                                                         iinfo->i_lenAlloc;
2126         } else {
2127                 if (!epos->offset)
2128                         epos->offset = sizeof(struct allocExtDesc);
2129                 ptr = epos->bh->b_data + epos->offset;
2130                 alen = sizeof(struct allocExtDesc) +
2131                         le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2132                                                         lengthAllocDescs);
2133         }
2134
2135         switch (iinfo->i_alloc_type) {
2136         case ICBTAG_FLAG_AD_SHORT:
2137                 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2138                 if (!sad)
2139                         return -1;
2140                 etype = le32_to_cpu(sad->extLength) >> 30;
2141                 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
2142                 eloc->partitionReferenceNum =
2143                                 iinfo->i_location.partitionReferenceNum;
2144                 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2145                 break;
2146         case ICBTAG_FLAG_AD_LONG:
2147                 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2148                 if (!lad)
2149                         return -1;
2150                 etype = le32_to_cpu(lad->extLength) >> 30;
2151                 *eloc = lelb_to_cpu(lad->extLocation);
2152                 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2153                 break;
2154         default:
2155                 udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
2156                 return -1;
2157         }
2158
2159         return etype;
2160 }
2161
2162 static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
2163                               struct kernel_lb_addr neloc, uint32_t nelen)
2164 {
2165         struct kernel_lb_addr oeloc;
2166         uint32_t oelen;
2167         int8_t etype;
2168
2169         if (epos.bh)
2170                 get_bh(epos.bh);
2171
2172         while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
2173                 udf_write_aext(inode, &epos, &neloc, nelen, 1);
2174                 neloc = oeloc;
2175                 nelen = (etype << 30) | oelen;
2176         }
2177         udf_add_aext(inode, &epos, &neloc, nelen, 1);
2178         brelse(epos.bh);
2179
2180         return (nelen >> 30);
2181 }
2182
2183 int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
2184                        struct kernel_lb_addr eloc, uint32_t elen)
2185 {
2186         struct extent_position oepos;
2187         int adsize;
2188         int8_t etype;
2189         struct allocExtDesc *aed;
2190         struct udf_inode_info *iinfo;
2191
2192         if (epos.bh) {
2193                 get_bh(epos.bh);
2194                 get_bh(epos.bh);
2195         }
2196
2197         iinfo = UDF_I(inode);
2198         if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2199                 adsize = sizeof(struct short_ad);
2200         else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2201                 adsize = sizeof(struct long_ad);
2202         else
2203                 adsize = 0;
2204
2205         oepos = epos;
2206         if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
2207                 return -1;
2208
2209         while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
2210                 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
2211                 if (oepos.bh != epos.bh) {
2212                         oepos.block = epos.block;
2213                         brelse(oepos.bh);
2214                         get_bh(epos.bh);
2215                         oepos.bh = epos.bh;
2216                         oepos.offset = epos.offset - adsize;
2217                 }
2218         }
2219         memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
2220         elen = 0;
2221
2222         if (epos.bh != oepos.bh) {
2223                 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2224                 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2225                 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2226                 if (!oepos.bh) {
2227                         iinfo->i_lenAlloc -= (adsize * 2);
2228                         mark_inode_dirty(inode);
2229                 } else {
2230                         aed = (struct allocExtDesc *)oepos.bh->b_data;
2231                         le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
2232                         if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2233                             UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2234                                 udf_update_tag(oepos.bh->b_data,
2235                                                 oepos.offset - (2 * adsize));
2236                         else
2237                                 udf_update_tag(oepos.bh->b_data,
2238                                                 sizeof(struct allocExtDesc));
2239                         mark_buffer_dirty_inode(oepos.bh, inode);
2240                 }
2241         } else {
2242                 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2243                 if (!oepos.bh) {
2244                         iinfo->i_lenAlloc -= adsize;
2245                         mark_inode_dirty(inode);
2246                 } else {
2247                         aed = (struct allocExtDesc *)oepos.bh->b_data;
2248                         le32_add_cpu(&aed->lengthAllocDescs, -adsize);
2249                         if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2250                             UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2251                                 udf_update_tag(oepos.bh->b_data,
2252                                                 epos.offset - adsize);
2253                         else
2254                                 udf_update_tag(oepos.bh->b_data,
2255                                                 sizeof(struct allocExtDesc));
2256                         mark_buffer_dirty_inode(oepos.bh, inode);
2257                 }
2258         }
2259
2260         brelse(epos.bh);
2261         brelse(oepos.bh);
2262
2263         return (elen >> 30);
2264 }
2265
2266 int8_t inode_bmap(struct inode *inode, sector_t block,
2267                   struct extent_position *pos, struct kernel_lb_addr *eloc,
2268                   uint32_t *elen, sector_t *offset)
2269 {
2270         unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2271         loff_t lbcount = 0, bcount = (loff_t) block << blocksize_bits;
2272         int8_t etype;
2273         struct udf_inode_info *iinfo;
2274
2275         iinfo = UDF_I(inode);
2276         if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2277                 pos->offset = 0;
2278                 pos->block = iinfo->i_location;
2279                 pos->bh = NULL;
2280         }
2281         *elen = 0;
2282         do {
2283                 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2284                 if (etype == -1) {
2285                         *offset = (bcount - lbcount) >> blocksize_bits;
2286                         iinfo->i_lenExtents = lbcount;
2287                         return -1;
2288                 }
2289                 lbcount += *elen;
2290         } while (lbcount <= bcount);
2291         /* update extent cache */
2292         udf_update_extent_cache(inode, lbcount - *elen, pos, 1);
2293         *offset = (bcount + *elen - lbcount) >> blocksize_bits;
2294
2295         return etype;
2296 }
2297
2298 long udf_block_map(struct inode *inode, sector_t block)
2299 {
2300         struct kernel_lb_addr eloc;
2301         uint32_t elen;
2302         sector_t offset;
2303         struct extent_position epos = {};
2304         int ret;
2305
2306         down_read(&UDF_I(inode)->i_data_sem);
2307
2308         if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2309                                                 (EXT_RECORDED_ALLOCATED >> 30))
2310                 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
2311         else
2312                 ret = 0;
2313
2314         up_read(&UDF_I(inode)->i_data_sem);
2315         brelse(epos.bh);
2316
2317         if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2318                 return udf_fixed_to_variable(ret);
2319         else
2320                 return ret;
2321 }