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