[PATCH] libata: convert assert(xxx)'s in low-level drivers to WARN_ON(!xxx)'s
[linux-2.6/suspend2-2.6.18.git] / fs / udf / inode.c
blob395e582ee5425cab1ad65177d5f8c02bedc9e932
1 /*
2 * inode.c
4 * PURPOSE
5 * Inode handling routines for the OSTA-UDF(tm) filesystem.
7 * COPYRIGHT
8 * This file is distributed under the terms of the GNU General Public
9 * License (GPL). Copies of the GPL can be obtained from:
10 * ftp://prep.ai.mit.edu/pub/gnu/GPL
11 * Each contributing author retains all rights to their own work.
13 * (C) 1998 Dave Boynton
14 * (C) 1998-2004 Ben Fennema
15 * (C) 1999-2000 Stelias Computing Inc
17 * HISTORY
19 * 10/04/98 dgb Added rudimentary directory functions
20 * 10/07/98 Fully working udf_block_map! It works!
21 * 11/25/98 bmap altered to better support extents
22 * 12/06/98 blf partition support in udf_iget, udf_block_map and udf_read_inode
23 * 12/12/98 rewrote udf_block_map to handle next extents and descs across
24 * block boundaries (which is not actually allowed)
25 * 12/20/98 added support for strategy 4096
26 * 03/07/99 rewrote udf_block_map (again)
27 * New funcs, inode_bmap, udf_next_aext
28 * 04/19/99 Support for writing device EA's for major/minor #
31 #include "udfdecl.h"
32 #include <linux/mm.h>
33 #include <linux/smp_lock.h>
34 #include <linux/module.h>
35 #include <linux/pagemap.h>
36 #include <linux/buffer_head.h>
37 #include <linux/writeback.h>
38 #include <linux/slab.h>
40 #include "udf_i.h"
41 #include "udf_sb.h"
43 MODULE_AUTHOR("Ben Fennema");
44 MODULE_DESCRIPTION("Universal Disk Format Filesystem");
45 MODULE_LICENSE("GPL");
47 #define EXTENT_MERGE_SIZE 5
49 static mode_t udf_convert_permissions(struct fileEntry *);
50 static int udf_update_inode(struct inode *, int);
51 static void udf_fill_inode(struct inode *, struct buffer_head *);
52 static struct buffer_head *inode_getblk(struct inode *, long, int *,
53 long *, int *);
54 static int8_t udf_insert_aext(struct inode *, kernel_lb_addr, int,
55 kernel_lb_addr, uint32_t, struct buffer_head *);
56 static void udf_split_extents(struct inode *, int *, int, int,
57 kernel_long_ad [EXTENT_MERGE_SIZE], int *);
58 static void udf_prealloc_extents(struct inode *, int, int,
59 kernel_long_ad [EXTENT_MERGE_SIZE], int *);
60 static void udf_merge_extents(struct inode *,
61 kernel_long_ad [EXTENT_MERGE_SIZE], int *);
62 static void udf_update_extents(struct inode *,
63 kernel_long_ad [EXTENT_MERGE_SIZE], int, int,
64 kernel_lb_addr, uint32_t, struct buffer_head **);
65 static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
68 * udf_delete_inode
70 * PURPOSE
71 * Clean-up before the specified inode is destroyed.
73 * DESCRIPTION
74 * This routine is called when the kernel destroys an inode structure
75 * ie. when iput() finds i_count == 0.
77 * HISTORY
78 * July 1, 1997 - Andrew E. Mileski
79 * Written, tested, and released.
81 * Called at the last iput() if i_nlink is zero.
83 void udf_delete_inode(struct inode * inode)
85 truncate_inode_pages(&inode->i_data, 0);
87 if (is_bad_inode(inode))
88 goto no_delete;
90 inode->i_size = 0;
91 udf_truncate(inode);
92 lock_kernel();
94 udf_update_inode(inode, IS_SYNC(inode));
95 udf_free_inode(inode);
97 unlock_kernel();
98 return;
99 no_delete:
100 clear_inode(inode);
103 void udf_clear_inode(struct inode *inode)
105 if (!(inode->i_sb->s_flags & MS_RDONLY)) {
106 lock_kernel();
107 udf_discard_prealloc(inode);
108 unlock_kernel();
111 kfree(UDF_I_DATA(inode));
112 UDF_I_DATA(inode) = NULL;
115 static int udf_writepage(struct page *page, struct writeback_control *wbc)
117 return block_write_full_page(page, udf_get_block, wbc);
120 static int udf_readpage(struct file *file, struct page *page)
122 return block_read_full_page(page, udf_get_block);
125 static int udf_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
127 return block_prepare_write(page, from, to, udf_get_block);
130 static sector_t udf_bmap(struct address_space *mapping, sector_t block)
132 return generic_block_bmap(mapping,block,udf_get_block);
135 struct address_space_operations udf_aops = {
136 .readpage = udf_readpage,
137 .writepage = udf_writepage,
138 .sync_page = block_sync_page,
139 .prepare_write = udf_prepare_write,
140 .commit_write = generic_commit_write,
141 .bmap = udf_bmap,
144 void udf_expand_file_adinicb(struct inode * inode, int newsize, int * err)
146 struct page *page;
147 char *kaddr;
148 struct writeback_control udf_wbc = {
149 .sync_mode = WB_SYNC_NONE,
150 .nr_to_write = 1,
153 /* from now on we have normal address_space methods */
154 inode->i_data.a_ops = &udf_aops;
156 if (!UDF_I_LENALLOC(inode))
158 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
159 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_SHORT;
160 else
161 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_LONG;
162 mark_inode_dirty(inode);
163 return;
166 page = grab_cache_page(inode->i_mapping, 0);
167 BUG_ON(!PageLocked(page));
169 if (!PageUptodate(page))
171 kaddr = kmap(page);
172 memset(kaddr + UDF_I_LENALLOC(inode), 0x00,
173 PAGE_CACHE_SIZE - UDF_I_LENALLOC(inode));
174 memcpy(kaddr, UDF_I_DATA(inode) + UDF_I_LENEATTR(inode),
175 UDF_I_LENALLOC(inode));
176 flush_dcache_page(page);
177 SetPageUptodate(page);
178 kunmap(page);
180 memset(UDF_I_DATA(inode) + UDF_I_LENEATTR(inode), 0x00,
181 UDF_I_LENALLOC(inode));
182 UDF_I_LENALLOC(inode) = 0;
183 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
184 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_SHORT;
185 else
186 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_LONG;
188 inode->i_data.a_ops->writepage(page, &udf_wbc);
189 page_cache_release(page);
191 mark_inode_dirty(inode);
194 struct buffer_head * udf_expand_dir_adinicb(struct inode *inode, int *block, int *err)
196 int newblock;
197 struct buffer_head *sbh = NULL, *dbh = NULL;
198 kernel_lb_addr bloc, eloc;
199 uint32_t elen, extoffset;
200 uint8_t alloctype;
202 struct udf_fileident_bh sfibh, dfibh;
203 loff_t f_pos = udf_ext0_offset(inode) >> 2;
204 int size = (udf_ext0_offset(inode) + inode->i_size) >> 2;
205 struct fileIdentDesc cfi, *sfi, *dfi;
207 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
208 alloctype = ICBTAG_FLAG_AD_SHORT;
209 else
210 alloctype = ICBTAG_FLAG_AD_LONG;
212 if (!inode->i_size)
214 UDF_I_ALLOCTYPE(inode) = alloctype;
215 mark_inode_dirty(inode);
216 return NULL;
219 /* alloc block, and copy data to it */
220 *block = udf_new_block(inode->i_sb, inode,
221 UDF_I_LOCATION(inode).partitionReferenceNum,
222 UDF_I_LOCATION(inode).logicalBlockNum, err);
224 if (!(*block))
225 return NULL;
226 newblock = udf_get_pblock(inode->i_sb, *block,
227 UDF_I_LOCATION(inode).partitionReferenceNum, 0);
228 if (!newblock)
229 return NULL;
230 dbh = udf_tgetblk(inode->i_sb, newblock);
231 if (!dbh)
232 return NULL;
233 lock_buffer(dbh);
234 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
235 set_buffer_uptodate(dbh);
236 unlock_buffer(dbh);
237 mark_buffer_dirty_inode(dbh, inode);
239 sfibh.soffset = sfibh.eoffset = (f_pos & ((inode->i_sb->s_blocksize - 1) >> 2)) << 2;
240 sbh = sfibh.sbh = sfibh.ebh = NULL;
241 dfibh.soffset = dfibh.eoffset = 0;
242 dfibh.sbh = dfibh.ebh = dbh;
243 while ( (f_pos < size) )
245 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_IN_ICB;
246 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL, NULL, NULL, NULL, NULL, NULL);
247 if (!sfi)
249 udf_release_data(dbh);
250 return NULL;
252 UDF_I_ALLOCTYPE(inode) = alloctype;
253 sfi->descTag.tagLocation = cpu_to_le32(*block);
254 dfibh.soffset = dfibh.eoffset;
255 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
256 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
257 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
258 sfi->fileIdent + le16_to_cpu(sfi->lengthOfImpUse)))
260 UDF_I_ALLOCTYPE(inode) = ICBTAG_FLAG_AD_IN_ICB;
261 udf_release_data(dbh);
262 return NULL;
265 mark_buffer_dirty_inode(dbh, inode);
267 memset(UDF_I_DATA(inode) + UDF_I_LENEATTR(inode), 0, UDF_I_LENALLOC(inode));
268 UDF_I_LENALLOC(inode) = 0;
269 bloc = UDF_I_LOCATION(inode);
270 eloc.logicalBlockNum = *block;
271 eloc.partitionReferenceNum = UDF_I_LOCATION(inode).partitionReferenceNum;
272 elen = inode->i_size;
273 UDF_I_LENEXTENTS(inode) = elen;
274 extoffset = udf_file_entry_alloc_offset(inode);
275 udf_add_aext(inode, &bloc, &extoffset, eloc, elen, &sbh, 0);
276 /* UniqueID stuff */
278 udf_release_data(sbh);
279 mark_inode_dirty(inode);
280 return dbh;
283 static int udf_get_block(struct inode *inode, sector_t block, struct buffer_head *bh_result, int create)
285 int err, new;
286 struct buffer_head *bh;
287 unsigned long phys;
289 if (!create)
291 phys = udf_block_map(inode, block);
292 if (phys)
293 map_bh(bh_result, inode->i_sb, phys);
294 return 0;
297 err = -EIO;
298 new = 0;
299 bh = NULL;
301 lock_kernel();
303 if (block < 0)
304 goto abort_negative;
306 if (block == UDF_I_NEXT_ALLOC_BLOCK(inode) + 1)
308 UDF_I_NEXT_ALLOC_BLOCK(inode) ++;
309 UDF_I_NEXT_ALLOC_GOAL(inode) ++;
312 err = 0;
314 bh = inode_getblk(inode, block, &err, &phys, &new);
315 if (bh)
316 BUG();
317 if (err)
318 goto abort;
319 if (!phys)
320 BUG();
322 if (new)
323 set_buffer_new(bh_result);
324 map_bh(bh_result, inode->i_sb, phys);
325 abort:
326 unlock_kernel();
327 return err;
329 abort_negative:
330 udf_warning(inode->i_sb, "udf_get_block", "block < 0");
331 goto abort;
334 static struct buffer_head *
335 udf_getblk(struct inode *inode, long block, int create, int *err)
337 struct buffer_head dummy;
339 dummy.b_state = 0;
340 dummy.b_blocknr = -1000;
341 *err = udf_get_block(inode, block, &dummy, create);
342 if (!*err && buffer_mapped(&dummy))
344 struct buffer_head *bh;
345 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
346 if (buffer_new(&dummy))
348 lock_buffer(bh);
349 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
350 set_buffer_uptodate(bh);
351 unlock_buffer(bh);
352 mark_buffer_dirty_inode(bh, inode);
354 return bh;
356 return NULL;
359 static struct buffer_head * inode_getblk(struct inode * inode, long block,
360 int *err, long *phys, int *new)
362 struct buffer_head *pbh = NULL, *cbh = NULL, *nbh = NULL, *result = NULL;
363 kernel_long_ad laarr[EXTENT_MERGE_SIZE];
364 uint32_t pextoffset = 0, cextoffset = 0, nextoffset = 0;
365 int count = 0, startnum = 0, endnum = 0;
366 uint32_t elen = 0;
367 kernel_lb_addr eloc, pbloc, cbloc, nbloc;
368 int c = 1;
369 uint64_t lbcount = 0, b_off = 0;
370 uint32_t newblocknum, newblock, offset = 0;
371 int8_t etype;
372 int goal = 0, pgoal = UDF_I_LOCATION(inode).logicalBlockNum;
373 char lastblock = 0;
375 pextoffset = cextoffset = nextoffset = udf_file_entry_alloc_offset(inode);
376 b_off = (uint64_t)block << inode->i_sb->s_blocksize_bits;
377 pbloc = cbloc = nbloc = UDF_I_LOCATION(inode);
379 /* find the extent which contains the block we are looking for.
380 alternate between laarr[0] and laarr[1] for locations of the
381 current extent, and the previous extent */
384 if (pbh != cbh)
386 udf_release_data(pbh);
387 atomic_inc(&cbh->b_count);
388 pbh = cbh;
390 if (cbh != nbh)
392 udf_release_data(cbh);
393 atomic_inc(&nbh->b_count);
394 cbh = nbh;
397 lbcount += elen;
399 pbloc = cbloc;
400 cbloc = nbloc;
402 pextoffset = cextoffset;
403 cextoffset = nextoffset;
405 if ((etype = udf_next_aext(inode, &nbloc, &nextoffset, &eloc, &elen, &nbh, 1)) == -1)
406 break;
408 c = !c;
410 laarr[c].extLength = (etype << 30) | elen;
411 laarr[c].extLocation = eloc;
413 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
414 pgoal = eloc.logicalBlockNum +
415 ((elen + inode->i_sb->s_blocksize - 1) >>
416 inode->i_sb->s_blocksize_bits);
418 count ++;
419 } while (lbcount + elen <= b_off);
421 b_off -= lbcount;
422 offset = b_off >> inode->i_sb->s_blocksize_bits;
424 /* if the extent is allocated and recorded, return the block
425 if the extent is not a multiple of the blocksize, round up */
427 if (etype == (EXT_RECORDED_ALLOCATED >> 30))
429 if (elen & (inode->i_sb->s_blocksize - 1))
431 elen = EXT_RECORDED_ALLOCATED |
432 ((elen + inode->i_sb->s_blocksize - 1) &
433 ~(inode->i_sb->s_blocksize - 1));
434 etype = udf_write_aext(inode, nbloc, &cextoffset, eloc, elen, nbh, 1);
436 udf_release_data(pbh);
437 udf_release_data(cbh);
438 udf_release_data(nbh);
439 newblock = udf_get_lb_pblock(inode->i_sb, eloc, offset);
440 *phys = newblock;
441 return NULL;
444 if (etype == -1)
446 endnum = startnum = ((count > 1) ? 1 : count);
447 if (laarr[c].extLength & (inode->i_sb->s_blocksize - 1))
449 laarr[c].extLength =
450 (laarr[c].extLength & UDF_EXTENT_FLAG_MASK) |
451 (((laarr[c].extLength & UDF_EXTENT_LENGTH_MASK) +
452 inode->i_sb->s_blocksize - 1) &
453 ~(inode->i_sb->s_blocksize - 1));
454 UDF_I_LENEXTENTS(inode) =
455 (UDF_I_LENEXTENTS(inode) + inode->i_sb->s_blocksize - 1) &
456 ~(inode->i_sb->s_blocksize - 1);
458 c = !c;
459 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
460 ((offset + 1) << inode->i_sb->s_blocksize_bits);
461 memset(&laarr[c].extLocation, 0x00, sizeof(kernel_lb_addr));
462 count ++;
463 endnum ++;
464 lastblock = 1;
466 else
467 endnum = startnum = ((count > 2) ? 2 : count);
469 /* if the current extent is in position 0, swap it with the previous */
470 if (!c && count != 1)
472 laarr[2] = laarr[0];
473 laarr[0] = laarr[1];
474 laarr[1] = laarr[2];
475 c = 1;
478 /* if the current block is located in a extent, read the next extent */
479 if (etype != -1)
481 if ((etype = udf_next_aext(inode, &nbloc, &nextoffset, &eloc, &elen, &nbh, 0)) != -1)
483 laarr[c+1].extLength = (etype << 30) | elen;
484 laarr[c+1].extLocation = eloc;
485 count ++;
486 startnum ++;
487 endnum ++;
489 else
490 lastblock = 1;
492 udf_release_data(cbh);
493 udf_release_data(nbh);
495 /* if the current extent is not recorded but allocated, get the
496 block in the extent corresponding to the requested block */
497 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
498 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
499 else /* otherwise, allocate a new block */
501 if (UDF_I_NEXT_ALLOC_BLOCK(inode) == block)
502 goal = UDF_I_NEXT_ALLOC_GOAL(inode);
504 if (!goal)
506 if (!(goal = pgoal))
507 goal = UDF_I_LOCATION(inode).logicalBlockNum + 1;
510 if (!(newblocknum = udf_new_block(inode->i_sb, inode,
511 UDF_I_LOCATION(inode).partitionReferenceNum, goal, err)))
513 udf_release_data(pbh);
514 *err = -ENOSPC;
515 return NULL;
517 UDF_I_LENEXTENTS(inode) += inode->i_sb->s_blocksize;
520 /* if the extent the requsted block is located in contains multiple blocks,
521 split the extent into at most three extents. blocks prior to requested
522 block, requested block, and blocks after requested block */
523 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
525 #ifdef UDF_PREALLOCATE
526 /* preallocate blocks */
527 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
528 #endif
530 /* merge any continuous blocks in laarr */
531 udf_merge_extents(inode, laarr, &endnum);
533 /* write back the new extents, inserting new extents if the new number
534 of extents is greater than the old number, and deleting extents if
535 the new number of extents is less than the old number */
536 udf_update_extents(inode, laarr, startnum, endnum, pbloc, pextoffset, &pbh);
538 udf_release_data(pbh);
540 if (!(newblock = udf_get_pblock(inode->i_sb, newblocknum,
541 UDF_I_LOCATION(inode).partitionReferenceNum, 0)))
543 return NULL;
545 *phys = newblock;
546 *err = 0;
547 *new = 1;
548 UDF_I_NEXT_ALLOC_BLOCK(inode) = block;
549 UDF_I_NEXT_ALLOC_GOAL(inode) = newblocknum;
550 inode->i_ctime = current_fs_time(inode->i_sb);
552 if (IS_SYNC(inode))
553 udf_sync_inode(inode);
554 else
555 mark_inode_dirty(inode);
556 return result;
559 static void udf_split_extents(struct inode *inode, int *c, int offset, int newblocknum,
560 kernel_long_ad laarr[EXTENT_MERGE_SIZE], int *endnum)
562 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
563 (laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
565 int curr = *c;
566 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
567 inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits;
568 int8_t etype = (laarr[curr].extLength >> 30);
570 if (blen == 1)
572 else if (!offset || blen == offset + 1)
574 laarr[curr+2] = laarr[curr+1];
575 laarr[curr+1] = laarr[curr];
577 else
579 laarr[curr+3] = laarr[curr+1];
580 laarr[curr+2] = laarr[curr+1] = laarr[curr];
583 if (offset)
585 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
587 udf_free_blocks(inode->i_sb, inode, laarr[curr].extLocation, 0, offset);
588 laarr[curr].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
589 (offset << inode->i_sb->s_blocksize_bits);
590 laarr[curr].extLocation.logicalBlockNum = 0;
591 laarr[curr].extLocation.partitionReferenceNum = 0;
593 else
594 laarr[curr].extLength = (etype << 30) |
595 (offset << inode->i_sb->s_blocksize_bits);
596 curr ++;
597 (*c) ++;
598 (*endnum) ++;
601 laarr[curr].extLocation.logicalBlockNum = newblocknum;
602 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
603 laarr[curr].extLocation.partitionReferenceNum =
604 UDF_I_LOCATION(inode).partitionReferenceNum;
605 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
606 inode->i_sb->s_blocksize;
607 curr ++;
609 if (blen != offset + 1)
611 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
612 laarr[curr].extLocation.logicalBlockNum += (offset + 1);
613 laarr[curr].extLength = (etype << 30) |
614 ((blen - (offset + 1)) << inode->i_sb->s_blocksize_bits);
615 curr ++;
616 (*endnum) ++;
621 static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
622 kernel_long_ad laarr[EXTENT_MERGE_SIZE], int *endnum)
624 int start, length = 0, currlength = 0, i;
626 if (*endnum >= (c+1))
628 if (!lastblock)
629 return;
630 else
631 start = c;
633 else
635 if ((laarr[c+1].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
637 start = c+1;
638 length = currlength = (((laarr[c+1].extLength & UDF_EXTENT_LENGTH_MASK) +
639 inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits);
641 else
642 start = c;
645 for (i=start+1; i<=*endnum; i++)
647 if (i == *endnum)
649 if (lastblock)
650 length += UDF_DEFAULT_PREALLOC_BLOCKS;
652 else if ((laarr[i].extLength >> 30) == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
653 length += (((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
654 inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits);
655 else
656 break;
659 if (length)
661 int next = laarr[start].extLocation.logicalBlockNum +
662 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
663 inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits);
664 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
665 laarr[start].extLocation.partitionReferenceNum,
666 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ? length :
667 UDF_DEFAULT_PREALLOC_BLOCKS) - currlength);
669 if (numalloc)
671 if (start == (c+1))
672 laarr[start].extLength +=
673 (numalloc << inode->i_sb->s_blocksize_bits);
674 else
676 memmove(&laarr[c+2], &laarr[c+1],
677 sizeof(long_ad) * (*endnum - (c+1)));
678 (*endnum) ++;
679 laarr[c+1].extLocation.logicalBlockNum = next;
680 laarr[c+1].extLocation.partitionReferenceNum =
681 laarr[c].extLocation.partitionReferenceNum;
682 laarr[c+1].extLength = EXT_NOT_RECORDED_ALLOCATED |
683 (numalloc << inode->i_sb->s_blocksize_bits);
684 start = c+1;
687 for (i=start+1; numalloc && i<*endnum; i++)
689 int elen = ((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
690 inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits;
692 if (elen > numalloc)
694 laarr[i].extLength -=
695 (numalloc << inode->i_sb->s_blocksize_bits);
696 numalloc = 0;
698 else
700 numalloc -= elen;
701 if (*endnum > (i+1))
702 memmove(&laarr[i], &laarr[i+1],
703 sizeof(long_ad) * (*endnum - (i+1)));
704 i --;
705 (*endnum) --;
708 UDF_I_LENEXTENTS(inode) += numalloc << inode->i_sb->s_blocksize_bits;
713 static void udf_merge_extents(struct inode *inode,
714 kernel_long_ad laarr[EXTENT_MERGE_SIZE], int *endnum)
716 int i;
718 for (i=0; i<(*endnum-1); i++)
720 if ((laarr[i].extLength >> 30) == (laarr[i+1].extLength >> 30))
722 if (((laarr[i].extLength >> 30) == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
723 ((laarr[i+1].extLocation.logicalBlockNum - laarr[i].extLocation.logicalBlockNum) ==
724 (((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
725 inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits)))
727 if (((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
728 (laarr[i+1].extLength & UDF_EXTENT_LENGTH_MASK) +
729 inode->i_sb->s_blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK)
731 laarr[i+1].extLength = (laarr[i+1].extLength -
732 (laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
733 UDF_EXTENT_LENGTH_MASK) & ~(inode->i_sb->s_blocksize-1);
734 laarr[i].extLength = (laarr[i].extLength & UDF_EXTENT_FLAG_MASK) +
735 (UDF_EXTENT_LENGTH_MASK + 1) - inode->i_sb->s_blocksize;
736 laarr[i+1].extLocation.logicalBlockNum =
737 laarr[i].extLocation.logicalBlockNum +
738 ((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) >>
739 inode->i_sb->s_blocksize_bits);
741 else
743 laarr[i].extLength = laarr[i+1].extLength +
744 (((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
745 inode->i_sb->s_blocksize - 1) & ~(inode->i_sb->s_blocksize-1));
746 if (*endnum > (i+2))
747 memmove(&laarr[i+1], &laarr[i+2],
748 sizeof(long_ad) * (*endnum - (i+2)));
749 i --;
750 (*endnum) --;
754 else if (((laarr[i].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
755 ((laarr[i+1].extLength >> 30) == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)))
757 udf_free_blocks(inode->i_sb, inode, laarr[i].extLocation, 0,
758 ((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
759 inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits);
760 laarr[i].extLocation.logicalBlockNum = 0;
761 laarr[i].extLocation.partitionReferenceNum = 0;
763 if (((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
764 (laarr[i+1].extLength & UDF_EXTENT_LENGTH_MASK) +
765 inode->i_sb->s_blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK)
767 laarr[i+1].extLength = (laarr[i+1].extLength -
768 (laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
769 UDF_EXTENT_LENGTH_MASK) & ~(inode->i_sb->s_blocksize-1);
770 laarr[i].extLength = (laarr[i].extLength & UDF_EXTENT_FLAG_MASK) +
771 (UDF_EXTENT_LENGTH_MASK + 1) - inode->i_sb->s_blocksize;
773 else
775 laarr[i].extLength = laarr[i+1].extLength +
776 (((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
777 inode->i_sb->s_blocksize - 1) & ~(inode->i_sb->s_blocksize-1));
778 if (*endnum > (i+2))
779 memmove(&laarr[i+1], &laarr[i+2],
780 sizeof(long_ad) * (*endnum - (i+2)));
781 i --;
782 (*endnum) --;
785 else if ((laarr[i].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
787 udf_free_blocks(inode->i_sb, inode, laarr[i].extLocation, 0,
788 ((laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) +
789 inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits);
790 laarr[i].extLocation.logicalBlockNum = 0;
791 laarr[i].extLocation.partitionReferenceNum = 0;
792 laarr[i].extLength = (laarr[i].extLength & UDF_EXTENT_LENGTH_MASK) |
793 EXT_NOT_RECORDED_NOT_ALLOCATED;
798 static void udf_update_extents(struct inode *inode,
799 kernel_long_ad laarr[EXTENT_MERGE_SIZE], int startnum, int endnum,
800 kernel_lb_addr pbloc, uint32_t pextoffset, struct buffer_head **pbh)
802 int start = 0, i;
803 kernel_lb_addr tmploc;
804 uint32_t tmplen;
806 if (startnum > endnum)
808 for (i=0; i<(startnum-endnum); i++)
810 udf_delete_aext(inode, pbloc, pextoffset, laarr[i].extLocation,
811 laarr[i].extLength, *pbh);
814 else if (startnum < endnum)
816 for (i=0; i<(endnum-startnum); i++)
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 ++;
826 for (i=start; i<endnum; i++)
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);
834 struct buffer_head * udf_bread(struct inode * inode, int block,
835 int create, int * err)
837 struct buffer_head * bh = NULL;
839 bh = udf_getblk(inode, block, create, err);
840 if (!bh)
841 return NULL;
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;
854 void udf_truncate(struct inode * inode)
856 int offset;
857 int err;
859 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
860 S_ISLNK(inode->i_mode)))
861 return;
862 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
863 return;
865 lock_kernel();
866 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
868 if (inode->i_sb->s_blocksize < (udf_file_entry_alloc_offset(inode) +
869 inode->i_size))
871 udf_expand_file_adinicb(inode, inode->i_size, &err);
872 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
874 inode->i_size = UDF_I_LENALLOC(inode);
875 unlock_kernel();
876 return;
878 else
879 udf_truncate_extents(inode);
881 else
883 offset = inode->i_size & (inode->i_sb->s_blocksize - 1);
884 memset(UDF_I_DATA(inode) + UDF_I_LENEATTR(inode) + offset, 0x00, inode->i_sb->s_blocksize - offset - udf_file_entry_alloc_offset(inode));
885 UDF_I_LENALLOC(inode) = inode->i_size;
888 else
890 block_truncate_page(inode->i_mapping, inode->i_size, udf_get_block);
891 udf_truncate_extents(inode);
894 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
895 if (IS_SYNC(inode))
896 udf_sync_inode (inode);
897 else
898 mark_inode_dirty(inode);
899 unlock_kernel();
902 static void
903 __udf_read_inode(struct inode *inode)
905 struct buffer_head *bh = NULL;
906 struct fileEntry *fe;
907 uint16_t ident;
910 * Set defaults, but the inode is still incomplete!
911 * Note: get_new_inode() sets the following on a new inode:
912 * i_sb = sb
913 * i_no = ino
914 * i_flags = sb->s_flags
915 * i_state = 0
916 * clean_inode(): zero fills and sets
917 * i_count = 1
918 * i_nlink = 1
919 * i_op = NULL;
921 inode->i_blksize = PAGE_SIZE;
923 bh = udf_read_ptagged(inode->i_sb, UDF_I_LOCATION(inode), 0, &ident);
925 if (!bh)
927 printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed !bh\n",
928 inode->i_ino);
929 make_bad_inode(inode);
930 return;
933 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
934 ident != TAG_IDENT_USE)
936 printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed ident=%d\n",
937 inode->i_ino, ident);
938 udf_release_data(bh);
939 make_bad_inode(inode);
940 return;
943 fe = (struct fileEntry *)bh->b_data;
945 if (le16_to_cpu(fe->icbTag.strategyType) == 4096)
947 struct buffer_head *ibh = NULL, *nbh = NULL;
948 struct indirectEntry *ie;
950 ibh = udf_read_ptagged(inode->i_sb, UDF_I_LOCATION(inode), 1, &ident);
951 if (ident == TAG_IDENT_IE)
953 if (ibh)
955 kernel_lb_addr loc;
956 ie = (struct indirectEntry *)ibh->b_data;
958 loc = lelb_to_cpu(ie->indirectICB.extLocation);
960 if (ie->indirectICB.extLength &&
961 (nbh = udf_read_ptagged(inode->i_sb, loc, 0, &ident)))
963 if (ident == TAG_IDENT_FE ||
964 ident == TAG_IDENT_EFE)
966 memcpy(&UDF_I_LOCATION(inode), &loc, sizeof(kernel_lb_addr));
967 udf_release_data(bh);
968 udf_release_data(ibh);
969 udf_release_data(nbh);
970 __udf_read_inode(inode);
971 return;
973 else
975 udf_release_data(nbh);
976 udf_release_data(ibh);
979 else
980 udf_release_data(ibh);
983 else
984 udf_release_data(ibh);
986 else if (le16_to_cpu(fe->icbTag.strategyType) != 4)
988 printk(KERN_ERR "udf: unsupported strategy type: %d\n",
989 le16_to_cpu(fe->icbTag.strategyType));
990 udf_release_data(bh);
991 make_bad_inode(inode);
992 return;
994 udf_fill_inode(inode, bh);
995 udf_release_data(bh);
998 static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1000 struct fileEntry *fe;
1001 struct extendedFileEntry *efe;
1002 time_t convtime;
1003 long convtime_usec;
1004 int offset;
1006 fe = (struct fileEntry *)bh->b_data;
1007 efe = (struct extendedFileEntry *)bh->b_data;
1009 if (le16_to_cpu(fe->icbTag.strategyType) == 4)
1010 UDF_I_STRAT4096(inode) = 0;
1011 else /* if (le16_to_cpu(fe->icbTag.strategyType) == 4096) */
1012 UDF_I_STRAT4096(inode) = 1;
1014 UDF_I_ALLOCTYPE(inode) = le16_to_cpu(fe->icbTag.flags) & ICBTAG_FLAG_AD_MASK;
1015 UDF_I_UNIQUE(inode) = 0;
1016 UDF_I_LENEATTR(inode) = 0;
1017 UDF_I_LENEXTENTS(inode) = 0;
1018 UDF_I_LENALLOC(inode) = 0;
1019 UDF_I_NEXT_ALLOC_BLOCK(inode) = 0;
1020 UDF_I_NEXT_ALLOC_GOAL(inode) = 0;
1021 if (le16_to_cpu(fe->descTag.tagIdent) == TAG_IDENT_EFE)
1023 UDF_I_EFE(inode) = 1;
1024 UDF_I_USE(inode) = 0;
1025 UDF_I_DATA(inode) = kmalloc(inode->i_sb->s_blocksize - sizeof(struct extendedFileEntry), GFP_KERNEL);
1026 memcpy(UDF_I_DATA(inode), bh->b_data + sizeof(struct extendedFileEntry), inode->i_sb->s_blocksize - sizeof(struct extendedFileEntry));
1028 else if (le16_to_cpu(fe->descTag.tagIdent) == TAG_IDENT_FE)
1030 UDF_I_EFE(inode) = 0;
1031 UDF_I_USE(inode) = 0;
1032 UDF_I_DATA(inode) = kmalloc(inode->i_sb->s_blocksize - sizeof(struct fileEntry), GFP_KERNEL);
1033 memcpy(UDF_I_DATA(inode), bh->b_data + sizeof(struct fileEntry), inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1035 else if (le16_to_cpu(fe->descTag.tagIdent) == TAG_IDENT_USE)
1037 UDF_I_EFE(inode) = 0;
1038 UDF_I_USE(inode) = 1;
1039 UDF_I_LENALLOC(inode) =
1040 le32_to_cpu(
1041 ((struct unallocSpaceEntry *)bh->b_data)->lengthAllocDescs);
1042 UDF_I_DATA(inode) = kmalloc(inode->i_sb->s_blocksize - sizeof(struct unallocSpaceEntry), GFP_KERNEL);
1043 memcpy(UDF_I_DATA(inode), bh->b_data + sizeof(struct unallocSpaceEntry), inode->i_sb->s_blocksize - sizeof(struct unallocSpaceEntry));
1044 return;
1047 inode->i_uid = le32_to_cpu(fe->uid);
1048 if ( inode->i_uid == -1 ) inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
1050 inode->i_gid = le32_to_cpu(fe->gid);
1051 if ( inode->i_gid == -1 ) inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
1053 inode->i_nlink = le16_to_cpu(fe->fileLinkCount);
1054 if (!inode->i_nlink)
1055 inode->i_nlink = 1;
1057 inode->i_size = le64_to_cpu(fe->informationLength);
1058 UDF_I_LENEXTENTS(inode) = inode->i_size;
1060 inode->i_mode = udf_convert_permissions(fe);
1061 inode->i_mode &= ~UDF_SB(inode->i_sb)->s_umask;
1063 if (UDF_I_EFE(inode) == 0)
1065 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
1066 (inode->i_sb->s_blocksize_bits - 9);
1068 if ( udf_stamp_to_time(&convtime, &convtime_usec,
1069 lets_to_cpu(fe->accessTime)) )
1071 inode->i_atime.tv_sec = convtime;
1072 inode->i_atime.tv_nsec = convtime_usec * 1000;
1074 else
1076 inode->i_atime = UDF_SB_RECORDTIME(inode->i_sb);
1079 if ( udf_stamp_to_time(&convtime, &convtime_usec,
1080 lets_to_cpu(fe->modificationTime)) )
1082 inode->i_mtime.tv_sec = convtime;
1083 inode->i_mtime.tv_nsec = convtime_usec * 1000;
1085 else
1087 inode->i_mtime = UDF_SB_RECORDTIME(inode->i_sb);
1090 if ( udf_stamp_to_time(&convtime, &convtime_usec,
1091 lets_to_cpu(fe->attrTime)) )
1093 inode->i_ctime.tv_sec = convtime;
1094 inode->i_ctime.tv_nsec = convtime_usec * 1000;
1096 else
1098 inode->i_ctime = UDF_SB_RECORDTIME(inode->i_sb);
1101 UDF_I_UNIQUE(inode) = le64_to_cpu(fe->uniqueID);
1102 UDF_I_LENEATTR(inode) = le32_to_cpu(fe->lengthExtendedAttr);
1103 UDF_I_LENALLOC(inode) = le32_to_cpu(fe->lengthAllocDescs);
1104 offset = sizeof(struct fileEntry) + UDF_I_LENEATTR(inode);
1106 else
1108 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
1109 (inode->i_sb->s_blocksize_bits - 9);
1111 if ( udf_stamp_to_time(&convtime, &convtime_usec,
1112 lets_to_cpu(efe->accessTime)) )
1114 inode->i_atime.tv_sec = convtime;
1115 inode->i_atime.tv_nsec = convtime_usec * 1000;
1117 else
1119 inode->i_atime = UDF_SB_RECORDTIME(inode->i_sb);
1122 if ( udf_stamp_to_time(&convtime, &convtime_usec,
1123 lets_to_cpu(efe->modificationTime)) )
1125 inode->i_mtime.tv_sec = convtime;
1126 inode->i_mtime.tv_nsec = convtime_usec * 1000;
1128 else
1130 inode->i_mtime = UDF_SB_RECORDTIME(inode->i_sb);
1133 if ( udf_stamp_to_time(&convtime, &convtime_usec,
1134 lets_to_cpu(efe->createTime)) )
1136 UDF_I_CRTIME(inode).tv_sec = convtime;
1137 UDF_I_CRTIME(inode).tv_nsec = convtime_usec * 1000;
1139 else
1141 UDF_I_CRTIME(inode) = UDF_SB_RECORDTIME(inode->i_sb);
1144 if ( udf_stamp_to_time(&convtime, &convtime_usec,
1145 lets_to_cpu(efe->attrTime)) )
1147 inode->i_ctime.tv_sec = convtime;
1148 inode->i_ctime.tv_nsec = convtime_usec * 1000;
1150 else
1152 inode->i_ctime = UDF_SB_RECORDTIME(inode->i_sb);
1155 UDF_I_UNIQUE(inode) = le64_to_cpu(efe->uniqueID);
1156 UDF_I_LENEATTR(inode) = le32_to_cpu(efe->lengthExtendedAttr);
1157 UDF_I_LENALLOC(inode) = le32_to_cpu(efe->lengthAllocDescs);
1158 offset = sizeof(struct extendedFileEntry) + UDF_I_LENEATTR(inode);
1161 switch (fe->icbTag.fileType)
1163 case ICBTAG_FILE_TYPE_DIRECTORY:
1165 inode->i_op = &udf_dir_inode_operations;
1166 inode->i_fop = &udf_dir_operations;
1167 inode->i_mode |= S_IFDIR;
1168 inode->i_nlink ++;
1169 break;
1171 case ICBTAG_FILE_TYPE_REALTIME:
1172 case ICBTAG_FILE_TYPE_REGULAR:
1173 case ICBTAG_FILE_TYPE_UNDEF:
1175 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_IN_ICB)
1176 inode->i_data.a_ops = &udf_adinicb_aops;
1177 else
1178 inode->i_data.a_ops = &udf_aops;
1179 inode->i_op = &udf_file_inode_operations;
1180 inode->i_fop = &udf_file_operations;
1181 inode->i_mode |= S_IFREG;
1182 break;
1184 case ICBTAG_FILE_TYPE_BLOCK:
1186 inode->i_mode |= S_IFBLK;
1187 break;
1189 case ICBTAG_FILE_TYPE_CHAR:
1191 inode->i_mode |= S_IFCHR;
1192 break;
1194 case ICBTAG_FILE_TYPE_FIFO:
1196 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1197 break;
1199 case ICBTAG_FILE_TYPE_SOCKET:
1201 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1202 break;
1204 case ICBTAG_FILE_TYPE_SYMLINK:
1206 inode->i_data.a_ops = &udf_symlink_aops;
1207 inode->i_op = &page_symlink_inode_operations;
1208 inode->i_mode = S_IFLNK|S_IRWXUGO;
1209 break;
1211 default:
1213 printk(KERN_ERR "udf: udf_fill_inode(ino %ld) failed unknown file type=%d\n",
1214 inode->i_ino, fe->icbTag.fileType);
1215 make_bad_inode(inode);
1216 return;
1219 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
1221 struct deviceSpec *dsea =
1222 (struct deviceSpec *)
1223 udf_get_extendedattr(inode, 12, 1);
1225 if (dsea)
1227 init_special_inode(inode, inode->i_mode, MKDEV(
1228 le32_to_cpu(dsea->majorDeviceIdent),
1229 le32_to_cpu(dsea->minorDeviceIdent)));
1230 /* Developer ID ??? */
1232 else
1234 make_bad_inode(inode);
1239 static mode_t
1240 udf_convert_permissions(struct fileEntry *fe)
1242 mode_t mode;
1243 uint32_t permissions;
1244 uint32_t flags;
1246 permissions = le32_to_cpu(fe->permissions);
1247 flags = le16_to_cpu(fe->icbTag.flags);
1249 mode = (( permissions ) & S_IRWXO) |
1250 (( permissions >> 2 ) & S_IRWXG) |
1251 (( permissions >> 4 ) & S_IRWXU) |
1252 (( flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1253 (( flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1254 (( flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
1256 return mode;
1260 * udf_write_inode
1262 * PURPOSE
1263 * Write out the specified inode.
1265 * DESCRIPTION
1266 * This routine is called whenever an inode is synced.
1267 * Currently this routine is just a placeholder.
1269 * HISTORY
1270 * July 1, 1997 - Andrew E. Mileski
1271 * Written, tested, and released.
1274 int udf_write_inode(struct inode * inode, int sync)
1276 int ret;
1277 lock_kernel();
1278 ret = udf_update_inode(inode, sync);
1279 unlock_kernel();
1280 return ret;
1283 int udf_sync_inode(struct inode * inode)
1285 return udf_update_inode(inode, 1);
1288 static int
1289 udf_update_inode(struct inode *inode, int do_sync)
1291 struct buffer_head *bh = NULL;
1292 struct fileEntry *fe;
1293 struct extendedFileEntry *efe;
1294 uint32_t udfperms;
1295 uint16_t icbflags;
1296 uint16_t crclen;
1297 int i;
1298 kernel_timestamp cpu_time;
1299 int err = 0;
1301 bh = udf_tread(inode->i_sb,
1302 udf_get_lb_pblock(inode->i_sb, UDF_I_LOCATION(inode), 0));
1304 if (!bh)
1306 udf_debug("bread failure\n");
1307 return -EIO;
1310 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
1312 fe = (struct fileEntry *)bh->b_data;
1313 efe = (struct extendedFileEntry *)bh->b_data;
1315 if (le16_to_cpu(fe->descTag.tagIdent) == TAG_IDENT_USE)
1317 struct unallocSpaceEntry *use =
1318 (struct unallocSpaceEntry *)bh->b_data;
1320 use->lengthAllocDescs = cpu_to_le32(UDF_I_LENALLOC(inode));
1321 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry), UDF_I_DATA(inode), inode->i_sb->s_blocksize - sizeof(struct unallocSpaceEntry));
1322 crclen = sizeof(struct unallocSpaceEntry) + UDF_I_LENALLOC(inode) -
1323 sizeof(tag);
1324 use->descTag.tagLocation = cpu_to_le32(UDF_I_LOCATION(inode).logicalBlockNum);
1325 use->descTag.descCRCLength = cpu_to_le16(crclen);
1326 use->descTag.descCRC = cpu_to_le16(udf_crc((char *)use + sizeof(tag), crclen, 0));
1328 use->descTag.tagChecksum = 0;
1329 for (i=0; i<16; i++)
1330 if (i != 4)
1331 use->descTag.tagChecksum += ((uint8_t *)&(use->descTag))[i];
1333 mark_buffer_dirty(bh);
1334 udf_release_data(bh);
1335 return err;
1338 if (inode->i_uid != UDF_SB(inode->i_sb)->s_uid)
1339 fe->uid = cpu_to_le32(inode->i_uid);
1341 if (inode->i_gid != UDF_SB(inode->i_sb)->s_gid)
1342 fe->gid = cpu_to_le32(inode->i_gid);
1344 udfperms = ((inode->i_mode & S_IRWXO) ) |
1345 ((inode->i_mode & S_IRWXG) << 2) |
1346 ((inode->i_mode & S_IRWXU) << 4);
1348 udfperms |= (le32_to_cpu(fe->permissions) &
1349 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1350 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1351 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
1352 fe->permissions = cpu_to_le32(udfperms);
1354 if (S_ISDIR(inode->i_mode))
1355 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1356 else
1357 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1359 fe->informationLength = cpu_to_le64(inode->i_size);
1361 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
1363 regid *eid;
1364 struct deviceSpec *dsea =
1365 (struct deviceSpec *)
1366 udf_get_extendedattr(inode, 12, 1);
1368 if (!dsea)
1370 dsea = (struct deviceSpec *)
1371 udf_add_extendedattr(inode,
1372 sizeof(struct deviceSpec) +
1373 sizeof(regid), 12, 0x3);
1374 dsea->attrType = cpu_to_le32(12);
1375 dsea->attrSubtype = 1;
1376 dsea->attrLength = cpu_to_le32(sizeof(struct deviceSpec) +
1377 sizeof(regid));
1378 dsea->impUseLength = cpu_to_le32(sizeof(regid));
1380 eid = (regid *)dsea->impUse;
1381 memset(eid, 0, sizeof(regid));
1382 strcpy(eid->ident, UDF_ID_DEVELOPER);
1383 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1384 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1385 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1386 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1389 if (UDF_I_EFE(inode) == 0)
1391 memcpy(bh->b_data + sizeof(struct fileEntry), UDF_I_DATA(inode), inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1392 fe->logicalBlocksRecorded = cpu_to_le64(
1393 (inode->i_blocks + (1 << (inode->i_sb->s_blocksize_bits - 9)) - 1) >>
1394 (inode->i_sb->s_blocksize_bits - 9));
1396 if (udf_time_to_stamp(&cpu_time, inode->i_atime))
1397 fe->accessTime = cpu_to_lets(cpu_time);
1398 if (udf_time_to_stamp(&cpu_time, inode->i_mtime))
1399 fe->modificationTime = cpu_to_lets(cpu_time);
1400 if (udf_time_to_stamp(&cpu_time, inode->i_ctime))
1401 fe->attrTime = cpu_to_lets(cpu_time);
1402 memset(&(fe->impIdent), 0, sizeof(regid));
1403 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1404 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1405 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1406 fe->uniqueID = cpu_to_le64(UDF_I_UNIQUE(inode));
1407 fe->lengthExtendedAttr = cpu_to_le32(UDF_I_LENEATTR(inode));
1408 fe->lengthAllocDescs = cpu_to_le32(UDF_I_LENALLOC(inode));
1409 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1410 crclen = sizeof(struct fileEntry);
1412 else
1414 memcpy(bh->b_data + sizeof(struct extendedFileEntry), UDF_I_DATA(inode), inode->i_sb->s_blocksize - sizeof(struct extendedFileEntry));
1415 efe->objectSize = cpu_to_le64(inode->i_size);
1416 efe->logicalBlocksRecorded = cpu_to_le64(
1417 (inode->i_blocks + (1 << (inode->i_sb->s_blocksize_bits - 9)) - 1) >>
1418 (inode->i_sb->s_blocksize_bits - 9));
1420 if (UDF_I_CRTIME(inode).tv_sec > inode->i_atime.tv_sec ||
1421 (UDF_I_CRTIME(inode).tv_sec == inode->i_atime.tv_sec &&
1422 UDF_I_CRTIME(inode).tv_nsec > inode->i_atime.tv_nsec))
1424 UDF_I_CRTIME(inode) = inode->i_atime;
1426 if (UDF_I_CRTIME(inode).tv_sec > inode->i_mtime.tv_sec ||
1427 (UDF_I_CRTIME(inode).tv_sec == inode->i_mtime.tv_sec &&
1428 UDF_I_CRTIME(inode).tv_nsec > inode->i_mtime.tv_nsec))
1430 UDF_I_CRTIME(inode) = inode->i_mtime;
1432 if (UDF_I_CRTIME(inode).tv_sec > inode->i_ctime.tv_sec ||
1433 (UDF_I_CRTIME(inode).tv_sec == inode->i_ctime.tv_sec &&
1434 UDF_I_CRTIME(inode).tv_nsec > inode->i_ctime.tv_nsec))
1436 UDF_I_CRTIME(inode) = inode->i_ctime;
1439 if (udf_time_to_stamp(&cpu_time, inode->i_atime))
1440 efe->accessTime = cpu_to_lets(cpu_time);
1441 if (udf_time_to_stamp(&cpu_time, inode->i_mtime))
1442 efe->modificationTime = cpu_to_lets(cpu_time);
1443 if (udf_time_to_stamp(&cpu_time, UDF_I_CRTIME(inode)))
1444 efe->createTime = cpu_to_lets(cpu_time);
1445 if (udf_time_to_stamp(&cpu_time, inode->i_ctime))
1446 efe->attrTime = cpu_to_lets(cpu_time);
1448 memset(&(efe->impIdent), 0, sizeof(regid));
1449 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1450 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1451 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1452 efe->uniqueID = cpu_to_le64(UDF_I_UNIQUE(inode));
1453 efe->lengthExtendedAttr = cpu_to_le32(UDF_I_LENEATTR(inode));
1454 efe->lengthAllocDescs = cpu_to_le32(UDF_I_LENALLOC(inode));
1455 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1456 crclen = sizeof(struct extendedFileEntry);
1458 if (UDF_I_STRAT4096(inode))
1460 fe->icbTag.strategyType = cpu_to_le16(4096);
1461 fe->icbTag.strategyParameter = cpu_to_le16(1);
1462 fe->icbTag.numEntries = cpu_to_le16(2);
1464 else
1466 fe->icbTag.strategyType = cpu_to_le16(4);
1467 fe->icbTag.numEntries = cpu_to_le16(1);
1470 if (S_ISDIR(inode->i_mode))
1471 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1472 else if (S_ISREG(inode->i_mode))
1473 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1474 else if (S_ISLNK(inode->i_mode))
1475 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1476 else if (S_ISBLK(inode->i_mode))
1477 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1478 else if (S_ISCHR(inode->i_mode))
1479 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1480 else if (S_ISFIFO(inode->i_mode))
1481 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1482 else if (S_ISSOCK(inode->i_mode))
1483 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1485 icbflags = UDF_I_ALLOCTYPE(inode) |
1486 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1487 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1488 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1489 (le16_to_cpu(fe->icbTag.flags) &
1490 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1491 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
1493 fe->icbTag.flags = cpu_to_le16(icbflags);
1494 if (UDF_SB_UDFREV(inode->i_sb) >= 0x0200)
1495 fe->descTag.descVersion = cpu_to_le16(3);
1496 else
1497 fe->descTag.descVersion = cpu_to_le16(2);
1498 fe->descTag.tagSerialNum = cpu_to_le16(UDF_SB_SERIALNUM(inode->i_sb));
1499 fe->descTag.tagLocation = cpu_to_le32(UDF_I_LOCATION(inode).logicalBlockNum);
1500 crclen += UDF_I_LENEATTR(inode) + UDF_I_LENALLOC(inode) - sizeof(tag);
1501 fe->descTag.descCRCLength = cpu_to_le16(crclen);
1502 fe->descTag.descCRC = cpu_to_le16(udf_crc((char *)fe + sizeof(tag), crclen, 0));
1504 fe->descTag.tagChecksum = 0;
1505 for (i=0; i<16; i++)
1506 if (i != 4)
1507 fe->descTag.tagChecksum += ((uint8_t *)&(fe->descTag))[i];
1509 /* write the data blocks */
1510 mark_buffer_dirty(bh);
1511 if (do_sync)
1513 sync_dirty_buffer(bh);
1514 if (buffer_req(bh) && !buffer_uptodate(bh))
1516 printk("IO error syncing udf inode [%s:%08lx]\n",
1517 inode->i_sb->s_id, inode->i_ino);
1518 err = -EIO;
1521 udf_release_data(bh);
1522 return err;
1525 struct inode *
1526 udf_iget(struct super_block *sb, kernel_lb_addr ino)
1528 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1529 struct inode *inode = iget_locked(sb, block);
1531 if (!inode)
1532 return NULL;
1534 if (inode->i_state & I_NEW) {
1535 memcpy(&UDF_I_LOCATION(inode), &ino, sizeof(kernel_lb_addr));
1536 __udf_read_inode(inode);
1537 unlock_new_inode(inode);
1540 if (is_bad_inode(inode))
1541 goto out_iput;
1543 if (ino.logicalBlockNum >= UDF_SB_PARTLEN(sb, ino.partitionReferenceNum)) {
1544 udf_debug("block=%d, partition=%d out of range\n",
1545 ino.logicalBlockNum, ino.partitionReferenceNum);
1546 make_bad_inode(inode);
1547 goto out_iput;
1550 return inode;
1552 out_iput:
1553 iput(inode);
1554 return NULL;
1557 int8_t udf_add_aext(struct inode *inode, kernel_lb_addr *bloc, int *extoffset,
1558 kernel_lb_addr eloc, uint32_t elen, struct buffer_head **bh, int inc)
1560 int adsize;
1561 short_ad *sad = NULL;
1562 long_ad *lad = NULL;
1563 struct allocExtDesc *aed;
1564 int8_t etype;
1565 uint8_t *ptr;
1567 if (!*bh)
1568 ptr = UDF_I_DATA(inode) + *extoffset - udf_file_entry_alloc_offset(inode) + UDF_I_LENEATTR(inode);
1569 else
1570 ptr = (*bh)->b_data + *extoffset;
1572 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
1573 adsize = sizeof(short_ad);
1574 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
1575 adsize = sizeof(long_ad);
1576 else
1577 return -1;
1579 if (*extoffset + (2 * adsize) > inode->i_sb->s_blocksize)
1581 char *sptr, *dptr;
1582 struct buffer_head *nbh;
1583 int err, loffset;
1584 kernel_lb_addr obloc = *bloc;
1586 if (!(bloc->logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1587 obloc.partitionReferenceNum, obloc.logicalBlockNum, &err)))
1589 return -1;
1591 if (!(nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
1592 *bloc, 0))))
1594 return -1;
1596 lock_buffer(nbh);
1597 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1598 set_buffer_uptodate(nbh);
1599 unlock_buffer(nbh);
1600 mark_buffer_dirty_inode(nbh, inode);
1602 aed = (struct allocExtDesc *)(nbh->b_data);
1603 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
1604 aed->previousAllocExtLocation = cpu_to_le32(obloc.logicalBlockNum);
1605 if (*extoffset + adsize > inode->i_sb->s_blocksize)
1607 loffset = *extoffset;
1608 aed->lengthAllocDescs = cpu_to_le32(adsize);
1609 sptr = ptr - adsize;
1610 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1611 memcpy(dptr, sptr, adsize);
1612 *extoffset = sizeof(struct allocExtDesc) + adsize;
1614 else
1616 loffset = *extoffset + adsize;
1617 aed->lengthAllocDescs = cpu_to_le32(0);
1618 sptr = ptr;
1619 *extoffset = sizeof(struct allocExtDesc);
1621 if (*bh)
1623 aed = (struct allocExtDesc *)(*bh)->b_data;
1624 aed->lengthAllocDescs =
1625 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) + adsize);
1627 else
1629 UDF_I_LENALLOC(inode) += adsize;
1630 mark_inode_dirty(inode);
1633 if (UDF_SB_UDFREV(inode->i_sb) >= 0x0200)
1634 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
1635 bloc->logicalBlockNum, sizeof(tag));
1636 else
1637 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
1638 bloc->logicalBlockNum, sizeof(tag));
1639 switch (UDF_I_ALLOCTYPE(inode))
1641 case ICBTAG_FLAG_AD_SHORT:
1643 sad = (short_ad *)sptr;
1644 sad->extLength = cpu_to_le32(
1645 EXT_NEXT_EXTENT_ALLOCDECS |
1646 inode->i_sb->s_blocksize);
1647 sad->extPosition = cpu_to_le32(bloc->logicalBlockNum);
1648 break;
1650 case ICBTAG_FLAG_AD_LONG:
1652 lad = (long_ad *)sptr;
1653 lad->extLength = cpu_to_le32(
1654 EXT_NEXT_EXTENT_ALLOCDECS |
1655 inode->i_sb->s_blocksize);
1656 lad->extLocation = cpu_to_lelb(*bloc);
1657 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1658 break;
1661 if (*bh)
1663 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
1664 udf_update_tag((*bh)->b_data, loffset);
1665 else
1666 udf_update_tag((*bh)->b_data, sizeof(struct allocExtDesc));
1667 mark_buffer_dirty_inode(*bh, inode);
1668 udf_release_data(*bh);
1670 else
1671 mark_inode_dirty(inode);
1672 *bh = nbh;
1675 etype = udf_write_aext(inode, *bloc, extoffset, eloc, elen, *bh, inc);
1677 if (!*bh)
1679 UDF_I_LENALLOC(inode) += adsize;
1680 mark_inode_dirty(inode);
1682 else
1684 aed = (struct allocExtDesc *)(*bh)->b_data;
1685 aed->lengthAllocDescs =
1686 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) + adsize);
1687 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
1688 udf_update_tag((*bh)->b_data, *extoffset + (inc ? 0 : adsize));
1689 else
1690 udf_update_tag((*bh)->b_data, sizeof(struct allocExtDesc));
1691 mark_buffer_dirty_inode(*bh, inode);
1694 return etype;
1697 int8_t udf_write_aext(struct inode *inode, kernel_lb_addr bloc, int *extoffset,
1698 kernel_lb_addr eloc, uint32_t elen, struct buffer_head *bh, int inc)
1700 int adsize;
1701 uint8_t *ptr;
1703 if (!bh)
1704 ptr = UDF_I_DATA(inode) + *extoffset - udf_file_entry_alloc_offset(inode) + UDF_I_LENEATTR(inode);
1705 else
1707 ptr = bh->b_data + *extoffset;
1708 atomic_inc(&bh->b_count);
1711 switch (UDF_I_ALLOCTYPE(inode))
1713 case ICBTAG_FLAG_AD_SHORT:
1715 short_ad *sad = (short_ad *)ptr;
1716 sad->extLength = cpu_to_le32(elen);
1717 sad->extPosition = cpu_to_le32(eloc.logicalBlockNum);
1718 adsize = sizeof(short_ad);
1719 break;
1721 case ICBTAG_FLAG_AD_LONG:
1723 long_ad *lad = (long_ad *)ptr;
1724 lad->extLength = cpu_to_le32(elen);
1725 lad->extLocation = cpu_to_lelb(eloc);
1726 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1727 adsize = sizeof(long_ad);
1728 break;
1730 default:
1731 return -1;
1734 if (bh)
1736 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
1738 struct allocExtDesc *aed = (struct allocExtDesc *)(bh)->b_data;
1739 udf_update_tag((bh)->b_data,
1740 le32_to_cpu(aed->lengthAllocDescs) + sizeof(struct allocExtDesc));
1742 mark_buffer_dirty_inode(bh, inode);
1743 udf_release_data(bh);
1745 else
1746 mark_inode_dirty(inode);
1748 if (inc)
1749 *extoffset += adsize;
1750 return (elen >> 30);
1753 int8_t udf_next_aext(struct inode *inode, kernel_lb_addr *bloc, int *extoffset,
1754 kernel_lb_addr *eloc, uint32_t *elen, struct buffer_head **bh, int inc)
1756 int8_t etype;
1758 while ((etype = udf_current_aext(inode, bloc, extoffset, eloc, elen, bh, inc)) ==
1759 (EXT_NEXT_EXTENT_ALLOCDECS >> 30))
1761 *bloc = *eloc;
1762 *extoffset = sizeof(struct allocExtDesc);
1763 udf_release_data(*bh);
1764 if (!(*bh = udf_tread(inode->i_sb, udf_get_lb_pblock(inode->i_sb, *bloc, 0))))
1766 udf_debug("reading block %d failed!\n",
1767 udf_get_lb_pblock(inode->i_sb, *bloc, 0));
1768 return -1;
1772 return etype;
1775 int8_t udf_current_aext(struct inode *inode, kernel_lb_addr *bloc, int *extoffset,
1776 kernel_lb_addr *eloc, uint32_t *elen, struct buffer_head **bh, int inc)
1778 int alen;
1779 int8_t etype;
1780 uint8_t *ptr;
1782 if (!*bh)
1784 if (!(*extoffset))
1785 *extoffset = udf_file_entry_alloc_offset(inode);
1786 ptr = UDF_I_DATA(inode) + *extoffset - udf_file_entry_alloc_offset(inode) + UDF_I_LENEATTR(inode);
1787 alen = udf_file_entry_alloc_offset(inode) + UDF_I_LENALLOC(inode);
1789 else
1791 if (!(*extoffset))
1792 *extoffset = sizeof(struct allocExtDesc);
1793 ptr = (*bh)->b_data + *extoffset;
1794 alen = sizeof(struct allocExtDesc) + le32_to_cpu(((struct allocExtDesc *)(*bh)->b_data)->lengthAllocDescs);
1797 switch (UDF_I_ALLOCTYPE(inode))
1799 case ICBTAG_FLAG_AD_SHORT:
1801 short_ad *sad;
1803 if (!(sad = udf_get_fileshortad(ptr, alen, extoffset, inc)))
1804 return -1;
1806 etype = le32_to_cpu(sad->extLength) >> 30;
1807 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
1808 eloc->partitionReferenceNum = UDF_I_LOCATION(inode).partitionReferenceNum;
1809 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
1810 break;
1812 case ICBTAG_FLAG_AD_LONG:
1814 long_ad *lad;
1816 if (!(lad = udf_get_filelongad(ptr, alen, extoffset, inc)))
1817 return -1;
1819 etype = le32_to_cpu(lad->extLength) >> 30;
1820 *eloc = lelb_to_cpu(lad->extLocation);
1821 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
1822 break;
1824 default:
1826 udf_debug("alloc_type = %d unsupported\n", UDF_I_ALLOCTYPE(inode));
1827 return -1;
1831 return etype;
1834 static int8_t
1835 udf_insert_aext(struct inode *inode, kernel_lb_addr bloc, int extoffset,
1836 kernel_lb_addr neloc, uint32_t nelen, struct buffer_head *bh)
1838 kernel_lb_addr oeloc;
1839 uint32_t oelen;
1840 int8_t etype;
1842 if (bh)
1843 atomic_inc(&bh->b_count);
1845 while ((etype = udf_next_aext(inode, &bloc, &extoffset, &oeloc, &oelen, &bh, 0)) != -1)
1847 udf_write_aext(inode, bloc, &extoffset, neloc, nelen, bh, 1);
1849 neloc = oeloc;
1850 nelen = (etype << 30) | oelen;
1852 udf_add_aext(inode, &bloc, &extoffset, neloc, nelen, &bh, 1);
1853 udf_release_data(bh);
1854 return (nelen >> 30);
1857 int8_t udf_delete_aext(struct inode *inode, kernel_lb_addr nbloc, int nextoffset,
1858 kernel_lb_addr eloc, uint32_t elen, struct buffer_head *nbh)
1860 struct buffer_head *obh;
1861 kernel_lb_addr obloc;
1862 int oextoffset, adsize;
1863 int8_t etype;
1864 struct allocExtDesc *aed;
1866 if (nbh)
1868 atomic_inc(&nbh->b_count);
1869 atomic_inc(&nbh->b_count);
1872 if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_SHORT)
1873 adsize = sizeof(short_ad);
1874 else if (UDF_I_ALLOCTYPE(inode) == ICBTAG_FLAG_AD_LONG)
1875 adsize = sizeof(long_ad);
1876 else
1877 adsize = 0;
1879 obh = nbh;
1880 obloc = nbloc;
1881 oextoffset = nextoffset;
1883 if (udf_next_aext(inode, &nbloc, &nextoffset, &eloc, &elen, &nbh, 1) == -1)
1884 return -1;
1886 while ((etype = udf_next_aext(inode, &nbloc, &nextoffset, &eloc, &elen, &nbh, 1)) != -1)
1888 udf_write_aext(inode, obloc, &oextoffset, eloc, (etype << 30) | elen, obh, 1);
1889 if (obh != nbh)
1891 obloc = nbloc;
1892 udf_release_data(obh);
1893 atomic_inc(&nbh->b_count);
1894 obh = nbh;
1895 oextoffset = nextoffset - adsize;
1898 memset(&eloc, 0x00, sizeof(kernel_lb_addr));
1899 elen = 0;
1901 if (nbh != obh)
1903 udf_free_blocks(inode->i_sb, inode, nbloc, 0, 1);
1904 udf_write_aext(inode, obloc, &oextoffset, eloc, elen, obh, 1);
1905 udf_write_aext(inode, obloc, &oextoffset, eloc, elen, obh, 1);
1906 if (!obh)
1908 UDF_I_LENALLOC(inode) -= (adsize * 2);
1909 mark_inode_dirty(inode);
1911 else
1913 aed = (struct allocExtDesc *)(obh)->b_data;
1914 aed->lengthAllocDescs =
1915 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) - (2*adsize));
1916 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
1917 udf_update_tag((obh)->b_data, oextoffset - (2*adsize));
1918 else
1919 udf_update_tag((obh)->b_data, sizeof(struct allocExtDesc));
1920 mark_buffer_dirty_inode(obh, inode);
1923 else
1925 udf_write_aext(inode, obloc, &oextoffset, eloc, elen, obh, 1);
1926 if (!obh)
1928 UDF_I_LENALLOC(inode) -= adsize;
1929 mark_inode_dirty(inode);
1931 else
1933 aed = (struct allocExtDesc *)(obh)->b_data;
1934 aed->lengthAllocDescs =
1935 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) - adsize);
1936 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || UDF_SB_UDFREV(inode->i_sb) >= 0x0201)
1937 udf_update_tag((obh)->b_data, oextoffset - adsize);
1938 else
1939 udf_update_tag((obh)->b_data, sizeof(struct allocExtDesc));
1940 mark_buffer_dirty_inode(obh, inode);
1944 udf_release_data(nbh);
1945 udf_release_data(obh);
1946 return (elen >> 30);
1949 int8_t inode_bmap(struct inode *inode, int block, kernel_lb_addr *bloc, uint32_t *extoffset,
1950 kernel_lb_addr *eloc, uint32_t *elen, uint32_t *offset, struct buffer_head **bh)
1952 uint64_t lbcount = 0, bcount = (uint64_t)block << inode->i_sb->s_blocksize_bits;
1953 int8_t etype;
1955 if (block < 0)
1957 printk(KERN_ERR "udf: inode_bmap: block < 0\n");
1958 return -1;
1961 *extoffset = 0;
1962 *elen = 0;
1963 *bloc = UDF_I_LOCATION(inode);
1967 if ((etype = udf_next_aext(inode, bloc, extoffset, eloc, elen, bh, 1)) == -1)
1969 *offset = bcount - lbcount;
1970 UDF_I_LENEXTENTS(inode) = lbcount;
1971 return -1;
1973 lbcount += *elen;
1974 } while (lbcount <= bcount);
1976 *offset = bcount + *elen - lbcount;
1978 return etype;
1981 long udf_block_map(struct inode *inode, long block)
1983 kernel_lb_addr eloc, bloc;
1984 uint32_t offset, extoffset, elen;
1985 struct buffer_head *bh = NULL;
1986 int ret;
1988 lock_kernel();
1990 if (inode_bmap(inode, block, &bloc, &extoffset, &eloc, &elen, &offset, &bh) == (EXT_RECORDED_ALLOCATED >> 30))
1991 ret = udf_get_lb_pblock(inode->i_sb, eloc, offset >> inode->i_sb->s_blocksize_bits);
1992 else
1993 ret = 0;
1995 unlock_kernel();
1996 udf_release_data(bh);
1998 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
1999 return udf_fixed_to_variable(ret);
2000 else
2001 return ret;