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