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