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