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