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