USB: ueagle: fix Coding Styles
[linux-2.6.git] / fs / udf / inode.c
blob8a3fbd177cab342000164e0a934cc41c5bdec30e
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
23 * and udf_read_inode
24 * 12/12/98 rewrote udf_block_map to handle next extents and descs across
25 * block boundaries (which is not actually allowed)
26 * 12/20/98 added support for strategy 4096
27 * 03/07/99 rewrote udf_block_map (again)
28 * New funcs, inode_bmap, udf_next_aext
29 * 04/19/99 Support for writing device EA's for major/minor #
32 #include "udfdecl.h"
33 #include <linux/mm.h>
34 #include <linux/smp_lock.h>
35 #include <linux/module.h>
36 #include <linux/pagemap.h>
37 #include <linux/buffer_head.h>
38 #include <linux/writeback.h>
39 #include <linux/quotaops.h>
40 #include <linux/slab.h>
41 #include <linux/crc-itu-t.h>
43 #include "udf_i.h"
44 #include "udf_sb.h"
46 MODULE_AUTHOR("Ben Fennema");
47 MODULE_DESCRIPTION("Universal Disk Format Filesystem");
48 MODULE_LICENSE("GPL");
50 #define EXTENT_MERGE_SIZE 5
52 static mode_t udf_convert_permissions(struct fileEntry *);
53 static int udf_update_inode(struct inode *, int);
54 static void udf_fill_inode(struct inode *, struct buffer_head *);
55 static int udf_alloc_i_data(struct inode *inode, size_t size);
56 static struct buffer_head *inode_getblk(struct inode *, sector_t, int *,
57 sector_t *, int *);
58 static int8_t udf_insert_aext(struct inode *, struct extent_position,
59 struct kernel_lb_addr, uint32_t);
60 static void udf_split_extents(struct inode *, int *, int, int,
61 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
62 static void udf_prealloc_extents(struct inode *, int, int,
63 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
64 static void udf_merge_extents(struct inode *,
65 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
66 static void udf_update_extents(struct inode *,
67 struct kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
68 struct extent_position *);
69 static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
72 void udf_delete_inode(struct inode *inode)
74 if (!is_bad_inode(inode))
75 dquot_initialize(inode);
77 truncate_inode_pages(&inode->i_data, 0);
79 if (is_bad_inode(inode))
80 goto no_delete;
82 inode->i_size = 0;
83 udf_truncate(inode);
84 lock_kernel();
86 udf_update_inode(inode, IS_SYNC(inode));
87 udf_free_inode(inode);
89 unlock_kernel();
90 return;
92 no_delete:
93 clear_inode(inode);
97 * If we are going to release inode from memory, we truncate last inode extent
98 * to proper length. We could use drop_inode() but it's called under inode_lock
99 * and thus we cannot mark inode dirty there. We use clear_inode() but we have
100 * to make sure to write inode as it's not written automatically.
102 void udf_clear_inode(struct inode *inode)
104 struct udf_inode_info *iinfo = UDF_I(inode);
106 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
107 inode->i_size != iinfo->i_lenExtents) {
108 printk(KERN_WARNING "UDF-fs (%s): Inode %lu (mode %o) has "
109 "inode size %llu different from extent length %llu. "
110 "Filesystem need not be standards compliant.\n",
111 inode->i_sb->s_id, inode->i_ino, inode->i_mode,
112 (unsigned long long)inode->i_size,
113 (unsigned long long)iinfo->i_lenExtents);
116 dquot_drop(inode);
117 kfree(iinfo->i_ext.i_data);
118 iinfo->i_ext.i_data = NULL;
121 static int udf_writepage(struct page *page, struct writeback_control *wbc)
123 return block_write_full_page(page, udf_get_block, wbc);
126 static int udf_readpage(struct file *file, struct page *page)
128 return block_read_full_page(page, udf_get_block);
131 static int udf_write_begin(struct file *file, struct address_space *mapping,
132 loff_t pos, unsigned len, unsigned flags,
133 struct page **pagep, void **fsdata)
135 *pagep = NULL;
136 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
137 udf_get_block);
140 static sector_t udf_bmap(struct address_space *mapping, sector_t block)
142 return generic_block_bmap(mapping, block, udf_get_block);
145 const struct address_space_operations udf_aops = {
146 .readpage = udf_readpage,
147 .writepage = udf_writepage,
148 .sync_page = block_sync_page,
149 .write_begin = udf_write_begin,
150 .write_end = generic_write_end,
151 .bmap = udf_bmap,
154 void udf_expand_file_adinicb(struct inode *inode, int newsize, int *err)
156 struct page *page;
157 char *kaddr;
158 struct udf_inode_info *iinfo = UDF_I(inode);
159 struct writeback_control udf_wbc = {
160 .sync_mode = WB_SYNC_NONE,
161 .nr_to_write = 1,
164 /* from now on we have normal address_space methods */
165 inode->i_data.a_ops = &udf_aops;
167 if (!iinfo->i_lenAlloc) {
168 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
169 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
170 else
171 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
172 mark_inode_dirty(inode);
173 return;
176 page = grab_cache_page(inode->i_mapping, 0);
177 BUG_ON(!PageLocked(page));
179 if (!PageUptodate(page)) {
180 kaddr = kmap(page);
181 memset(kaddr + iinfo->i_lenAlloc, 0x00,
182 PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
183 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
184 iinfo->i_lenAlloc);
185 flush_dcache_page(page);
186 SetPageUptodate(page);
187 kunmap(page);
189 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
190 iinfo->i_lenAlloc);
191 iinfo->i_lenAlloc = 0;
192 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
193 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
194 else
195 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
197 inode->i_data.a_ops->writepage(page, &udf_wbc);
198 page_cache_release(page);
200 mark_inode_dirty(inode);
203 struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
204 int *err)
206 int newblock;
207 struct buffer_head *dbh = NULL;
208 struct kernel_lb_addr eloc;
209 uint8_t alloctype;
210 struct extent_position epos;
212 struct udf_fileident_bh sfibh, dfibh;
213 loff_t f_pos = udf_ext0_offset(inode);
214 int size = udf_ext0_offset(inode) + inode->i_size;
215 struct fileIdentDesc cfi, *sfi, *dfi;
216 struct udf_inode_info *iinfo = UDF_I(inode);
218 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
219 alloctype = ICBTAG_FLAG_AD_SHORT;
220 else
221 alloctype = ICBTAG_FLAG_AD_LONG;
223 if (!inode->i_size) {
224 iinfo->i_alloc_type = alloctype;
225 mark_inode_dirty(inode);
226 return NULL;
229 /* alloc block, and copy data to it */
230 *block = udf_new_block(inode->i_sb, inode,
231 iinfo->i_location.partitionReferenceNum,
232 iinfo->i_location.logicalBlockNum, err);
233 if (!(*block))
234 return NULL;
235 newblock = udf_get_pblock(inode->i_sb, *block,
236 iinfo->i_location.partitionReferenceNum,
238 if (!newblock)
239 return NULL;
240 dbh = udf_tgetblk(inode->i_sb, newblock);
241 if (!dbh)
242 return NULL;
243 lock_buffer(dbh);
244 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
245 set_buffer_uptodate(dbh);
246 unlock_buffer(dbh);
247 mark_buffer_dirty_inode(dbh, inode);
249 sfibh.soffset = sfibh.eoffset =
250 f_pos & (inode->i_sb->s_blocksize - 1);
251 sfibh.sbh = sfibh.ebh = NULL;
252 dfibh.soffset = dfibh.eoffset = 0;
253 dfibh.sbh = dfibh.ebh = dbh;
254 while (f_pos < size) {
255 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
256 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
257 NULL, NULL, NULL);
258 if (!sfi) {
259 brelse(dbh);
260 return NULL;
262 iinfo->i_alloc_type = alloctype;
263 sfi->descTag.tagLocation = cpu_to_le32(*block);
264 dfibh.soffset = dfibh.eoffset;
265 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
266 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
267 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
268 sfi->fileIdent +
269 le16_to_cpu(sfi->lengthOfImpUse))) {
270 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
271 brelse(dbh);
272 return NULL;
275 mark_buffer_dirty_inode(dbh, inode);
277 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
278 iinfo->i_lenAlloc);
279 iinfo->i_lenAlloc = 0;
280 eloc.logicalBlockNum = *block;
281 eloc.partitionReferenceNum =
282 iinfo->i_location.partitionReferenceNum;
283 iinfo->i_lenExtents = inode->i_size;
284 epos.bh = NULL;
285 epos.block = iinfo->i_location;
286 epos.offset = udf_file_entry_alloc_offset(inode);
287 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
288 /* UniqueID stuff */
290 brelse(epos.bh);
291 mark_inode_dirty(inode);
292 return dbh;
295 static int udf_get_block(struct inode *inode, sector_t block,
296 struct buffer_head *bh_result, int create)
298 int err, new;
299 struct buffer_head *bh;
300 sector_t phys = 0;
301 struct udf_inode_info *iinfo;
303 if (!create) {
304 phys = udf_block_map(inode, block);
305 if (phys)
306 map_bh(bh_result, inode->i_sb, phys);
307 return 0;
310 err = -EIO;
311 new = 0;
312 bh = NULL;
314 lock_kernel();
316 iinfo = UDF_I(inode);
317 if (block == iinfo->i_next_alloc_block + 1) {
318 iinfo->i_next_alloc_block++;
319 iinfo->i_next_alloc_goal++;
322 err = 0;
324 bh = inode_getblk(inode, block, &err, &phys, &new);
325 BUG_ON(bh);
326 if (err)
327 goto abort;
328 BUG_ON(!phys);
330 if (new)
331 set_buffer_new(bh_result);
332 map_bh(bh_result, inode->i_sb, phys);
334 abort:
335 unlock_kernel();
336 return err;
339 static struct buffer_head *udf_getblk(struct inode *inode, long block,
340 int create, int *err)
342 struct buffer_head *bh;
343 struct buffer_head dummy;
345 dummy.b_state = 0;
346 dummy.b_blocknr = -1000;
347 *err = udf_get_block(inode, block, &dummy, create);
348 if (!*err && buffer_mapped(&dummy)) {
349 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
350 if (buffer_new(&dummy)) {
351 lock_buffer(bh);
352 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
353 set_buffer_uptodate(bh);
354 unlock_buffer(bh);
355 mark_buffer_dirty_inode(bh, inode);
357 return bh;
360 return NULL;
363 /* Extend the file by 'blocks' blocks, return the number of extents added */
364 int udf_extend_file(struct inode *inode, struct extent_position *last_pos,
365 struct kernel_long_ad *last_ext, sector_t blocks)
367 sector_t add;
368 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
369 struct super_block *sb = inode->i_sb;
370 struct kernel_lb_addr prealloc_loc = {};
371 int prealloc_len = 0;
372 struct udf_inode_info *iinfo;
374 /* The previous extent is fake and we should not extend by anything
375 * - there's nothing to do... */
376 if (!blocks && fake)
377 return 0;
379 iinfo = UDF_I(inode);
380 /* Round the last extent up to a multiple of block size */
381 if (last_ext->extLength & (sb->s_blocksize - 1)) {
382 last_ext->extLength =
383 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
384 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
385 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
386 iinfo->i_lenExtents =
387 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
388 ~(sb->s_blocksize - 1);
391 /* Last extent are just preallocated blocks? */
392 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
393 EXT_NOT_RECORDED_ALLOCATED) {
394 /* Save the extent so that we can reattach it to the end */
395 prealloc_loc = last_ext->extLocation;
396 prealloc_len = last_ext->extLength;
397 /* Mark the extent as a hole */
398 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
399 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
400 last_ext->extLocation.logicalBlockNum = 0;
401 last_ext->extLocation.partitionReferenceNum = 0;
404 /* Can we merge with the previous extent? */
405 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
406 EXT_NOT_RECORDED_NOT_ALLOCATED) {
407 add = ((1 << 30) - sb->s_blocksize -
408 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
409 sb->s_blocksize_bits;
410 if (add > blocks)
411 add = blocks;
412 blocks -= add;
413 last_ext->extLength += add << sb->s_blocksize_bits;
416 if (fake) {
417 udf_add_aext(inode, last_pos, &last_ext->extLocation,
418 last_ext->extLength, 1);
419 count++;
420 } else
421 udf_write_aext(inode, last_pos, &last_ext->extLocation,
422 last_ext->extLength, 1);
424 /* Managed to do everything necessary? */
425 if (!blocks)
426 goto out;
428 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
429 last_ext->extLocation.logicalBlockNum = 0;
430 last_ext->extLocation.partitionReferenceNum = 0;
431 add = (1 << (30-sb->s_blocksize_bits)) - 1;
432 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
433 (add << sb->s_blocksize_bits);
435 /* Create enough extents to cover the whole hole */
436 while (blocks > add) {
437 blocks -= add;
438 if (udf_add_aext(inode, last_pos, &last_ext->extLocation,
439 last_ext->extLength, 1) == -1)
440 return -1;
441 count++;
443 if (blocks) {
444 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
445 (blocks << sb->s_blocksize_bits);
446 if (udf_add_aext(inode, last_pos, &last_ext->extLocation,
447 last_ext->extLength, 1) == -1)
448 return -1;
449 count++;
452 out:
453 /* Do we have some preallocated blocks saved? */
454 if (prealloc_len) {
455 if (udf_add_aext(inode, last_pos, &prealloc_loc,
456 prealloc_len, 1) == -1)
457 return -1;
458 last_ext->extLocation = prealloc_loc;
459 last_ext->extLength = prealloc_len;
460 count++;
463 /* last_pos should point to the last written extent... */
464 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
465 last_pos->offset -= sizeof(struct short_ad);
466 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
467 last_pos->offset -= sizeof(struct long_ad);
468 else
469 return -1;
471 return count;
474 static struct buffer_head *inode_getblk(struct inode *inode, sector_t block,
475 int *err, sector_t *phys, int *new)
477 static sector_t last_block;
478 struct buffer_head *result = NULL;
479 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
480 struct extent_position prev_epos, cur_epos, next_epos;
481 int count = 0, startnum = 0, endnum = 0;
482 uint32_t elen = 0, tmpelen;
483 struct kernel_lb_addr eloc, tmpeloc;
484 int c = 1;
485 loff_t lbcount = 0, b_off = 0;
486 uint32_t newblocknum, newblock;
487 sector_t offset = 0;
488 int8_t etype;
489 struct udf_inode_info *iinfo = UDF_I(inode);
490 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
491 int lastblock = 0;
493 prev_epos.offset = udf_file_entry_alloc_offset(inode);
494 prev_epos.block = iinfo->i_location;
495 prev_epos.bh = NULL;
496 cur_epos = next_epos = prev_epos;
497 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
499 /* find the extent which contains the block we are looking for.
500 alternate between laarr[0] and laarr[1] for locations of the
501 current extent, and the previous extent */
502 do {
503 if (prev_epos.bh != cur_epos.bh) {
504 brelse(prev_epos.bh);
505 get_bh(cur_epos.bh);
506 prev_epos.bh = cur_epos.bh;
508 if (cur_epos.bh != next_epos.bh) {
509 brelse(cur_epos.bh);
510 get_bh(next_epos.bh);
511 cur_epos.bh = next_epos.bh;
514 lbcount += elen;
516 prev_epos.block = cur_epos.block;
517 cur_epos.block = next_epos.block;
519 prev_epos.offset = cur_epos.offset;
520 cur_epos.offset = next_epos.offset;
522 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
523 if (etype == -1)
524 break;
526 c = !c;
528 laarr[c].extLength = (etype << 30) | elen;
529 laarr[c].extLocation = eloc;
531 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
532 pgoal = eloc.logicalBlockNum +
533 ((elen + inode->i_sb->s_blocksize - 1) >>
534 inode->i_sb->s_blocksize_bits);
536 count++;
537 } while (lbcount + elen <= b_off);
539 b_off -= lbcount;
540 offset = b_off >> inode->i_sb->s_blocksize_bits;
542 * Move prev_epos and cur_epos into indirect extent if we are at
543 * the pointer to it
545 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
546 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
548 /* if the extent is allocated and recorded, return the block
549 if the extent is not a multiple of the blocksize, round up */
551 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
552 if (elen & (inode->i_sb->s_blocksize - 1)) {
553 elen = EXT_RECORDED_ALLOCATED |
554 ((elen + inode->i_sb->s_blocksize - 1) &
555 ~(inode->i_sb->s_blocksize - 1));
556 etype = udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
558 brelse(prev_epos.bh);
559 brelse(cur_epos.bh);
560 brelse(next_epos.bh);
561 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
562 *phys = newblock;
563 return NULL;
566 last_block = block;
567 /* Are we beyond EOF? */
568 if (etype == -1) {
569 int ret;
571 if (count) {
572 if (c)
573 laarr[0] = laarr[1];
574 startnum = 1;
575 } else {
576 /* Create a fake extent when there's not one */
577 memset(&laarr[0].extLocation, 0x00,
578 sizeof(struct kernel_lb_addr));
579 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
580 /* Will udf_extend_file() create real extent from
581 a fake one? */
582 startnum = (offset > 0);
584 /* Create extents for the hole between EOF and offset */
585 ret = udf_extend_file(inode, &prev_epos, laarr, offset);
586 if (ret == -1) {
587 brelse(prev_epos.bh);
588 brelse(cur_epos.bh);
589 brelse(next_epos.bh);
590 /* We don't really know the error here so we just make
591 * something up */
592 *err = -ENOSPC;
593 return NULL;
595 c = 0;
596 offset = 0;
597 count += ret;
598 /* We are not covered by a preallocated extent? */
599 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
600 EXT_NOT_RECORDED_ALLOCATED) {
601 /* Is there any real extent? - otherwise we overwrite
602 * the fake one... */
603 if (count)
604 c = !c;
605 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
606 inode->i_sb->s_blocksize;
607 memset(&laarr[c].extLocation, 0x00,
608 sizeof(struct kernel_lb_addr));
609 count++;
610 endnum++;
612 endnum = c + 1;
613 lastblock = 1;
614 } else {
615 endnum = startnum = ((count > 2) ? 2 : count);
617 /* if the current extent is in position 0,
618 swap it with the previous */
619 if (!c && count != 1) {
620 laarr[2] = laarr[0];
621 laarr[0] = laarr[1];
622 laarr[1] = laarr[2];
623 c = 1;
626 /* if the current block is located in an extent,
627 read the next extent */
628 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
629 if (etype != -1) {
630 laarr[c + 1].extLength = (etype << 30) | elen;
631 laarr[c + 1].extLocation = eloc;
632 count++;
633 startnum++;
634 endnum++;
635 } else
636 lastblock = 1;
639 /* if the current extent is not recorded but allocated, get the
640 * block in the extent corresponding to the requested block */
641 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
642 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
643 else { /* otherwise, allocate a new block */
644 if (iinfo->i_next_alloc_block == block)
645 goal = iinfo->i_next_alloc_goal;
647 if (!goal) {
648 if (!(goal = pgoal)) /* XXX: what was intended here? */
649 goal = iinfo->i_location.logicalBlockNum + 1;
652 newblocknum = udf_new_block(inode->i_sb, inode,
653 iinfo->i_location.partitionReferenceNum,
654 goal, err);
655 if (!newblocknum) {
656 brelse(prev_epos.bh);
657 *err = -ENOSPC;
658 return NULL;
660 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
663 /* if the extent the requsted block is located in contains multiple
664 * blocks, split the extent into at most three extents. blocks prior
665 * to requested block, requested block, and blocks after requested
666 * block */
667 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
669 #ifdef UDF_PREALLOCATE
670 /* We preallocate blocks only for regular files. It also makes sense
671 * for directories but there's a problem when to drop the
672 * preallocation. We might use some delayed work for that but I feel
673 * it's overengineering for a filesystem like UDF. */
674 if (S_ISREG(inode->i_mode))
675 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
676 #endif
678 /* merge any continuous blocks in laarr */
679 udf_merge_extents(inode, laarr, &endnum);
681 /* write back the new extents, inserting new extents if the new number
682 * of extents is greater than the old number, and deleting extents if
683 * the new number of extents is less than the old number */
684 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
686 brelse(prev_epos.bh);
688 newblock = udf_get_pblock(inode->i_sb, newblocknum,
689 iinfo->i_location.partitionReferenceNum, 0);
690 if (!newblock)
691 return NULL;
692 *phys = newblock;
693 *err = 0;
694 *new = 1;
695 iinfo->i_next_alloc_block = block;
696 iinfo->i_next_alloc_goal = newblocknum;
697 inode->i_ctime = current_fs_time(inode->i_sb);
699 if (IS_SYNC(inode))
700 udf_sync_inode(inode);
701 else
702 mark_inode_dirty(inode);
704 return result;
707 static void udf_split_extents(struct inode *inode, int *c, int offset,
708 int newblocknum,
709 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
710 int *endnum)
712 unsigned long blocksize = inode->i_sb->s_blocksize;
713 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
715 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
716 (laarr[*c].extLength >> 30) ==
717 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
718 int curr = *c;
719 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
720 blocksize - 1) >> blocksize_bits;
721 int8_t etype = (laarr[curr].extLength >> 30);
723 if (blen == 1)
725 else if (!offset || blen == offset + 1) {
726 laarr[curr + 2] = laarr[curr + 1];
727 laarr[curr + 1] = laarr[curr];
728 } else {
729 laarr[curr + 3] = laarr[curr + 1];
730 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
733 if (offset) {
734 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
735 udf_free_blocks(inode->i_sb, inode,
736 &laarr[curr].extLocation,
737 0, offset);
738 laarr[curr].extLength =
739 EXT_NOT_RECORDED_NOT_ALLOCATED |
740 (offset << blocksize_bits);
741 laarr[curr].extLocation.logicalBlockNum = 0;
742 laarr[curr].extLocation.
743 partitionReferenceNum = 0;
744 } else
745 laarr[curr].extLength = (etype << 30) |
746 (offset << blocksize_bits);
747 curr++;
748 (*c)++;
749 (*endnum)++;
752 laarr[curr].extLocation.logicalBlockNum = newblocknum;
753 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
754 laarr[curr].extLocation.partitionReferenceNum =
755 UDF_I(inode)->i_location.partitionReferenceNum;
756 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
757 blocksize;
758 curr++;
760 if (blen != offset + 1) {
761 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
762 laarr[curr].extLocation.logicalBlockNum +=
763 offset + 1;
764 laarr[curr].extLength = (etype << 30) |
765 ((blen - (offset + 1)) << blocksize_bits);
766 curr++;
767 (*endnum)++;
772 static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
773 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
774 int *endnum)
776 int start, length = 0, currlength = 0, i;
778 if (*endnum >= (c + 1)) {
779 if (!lastblock)
780 return;
781 else
782 start = c;
783 } else {
784 if ((laarr[c + 1].extLength >> 30) ==
785 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
786 start = c + 1;
787 length = currlength =
788 (((laarr[c + 1].extLength &
789 UDF_EXTENT_LENGTH_MASK) +
790 inode->i_sb->s_blocksize - 1) >>
791 inode->i_sb->s_blocksize_bits);
792 } else
793 start = c;
796 for (i = start + 1; i <= *endnum; i++) {
797 if (i == *endnum) {
798 if (lastblock)
799 length += UDF_DEFAULT_PREALLOC_BLOCKS;
800 } else if ((laarr[i].extLength >> 30) ==
801 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
802 length += (((laarr[i].extLength &
803 UDF_EXTENT_LENGTH_MASK) +
804 inode->i_sb->s_blocksize - 1) >>
805 inode->i_sb->s_blocksize_bits);
806 } else
807 break;
810 if (length) {
811 int next = laarr[start].extLocation.logicalBlockNum +
812 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
813 inode->i_sb->s_blocksize - 1) >>
814 inode->i_sb->s_blocksize_bits);
815 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
816 laarr[start].extLocation.partitionReferenceNum,
817 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
818 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
819 currlength);
820 if (numalloc) {
821 if (start == (c + 1))
822 laarr[start].extLength +=
823 (numalloc <<
824 inode->i_sb->s_blocksize_bits);
825 else {
826 memmove(&laarr[c + 2], &laarr[c + 1],
827 sizeof(struct long_ad) * (*endnum - (c + 1)));
828 (*endnum)++;
829 laarr[c + 1].extLocation.logicalBlockNum = next;
830 laarr[c + 1].extLocation.partitionReferenceNum =
831 laarr[c].extLocation.
832 partitionReferenceNum;
833 laarr[c + 1].extLength =
834 EXT_NOT_RECORDED_ALLOCATED |
835 (numalloc <<
836 inode->i_sb->s_blocksize_bits);
837 start = c + 1;
840 for (i = start + 1; numalloc && i < *endnum; i++) {
841 int elen = ((laarr[i].extLength &
842 UDF_EXTENT_LENGTH_MASK) +
843 inode->i_sb->s_blocksize - 1) >>
844 inode->i_sb->s_blocksize_bits;
846 if (elen > numalloc) {
847 laarr[i].extLength -=
848 (numalloc <<
849 inode->i_sb->s_blocksize_bits);
850 numalloc = 0;
851 } else {
852 numalloc -= elen;
853 if (*endnum > (i + 1))
854 memmove(&laarr[i],
855 &laarr[i + 1],
856 sizeof(struct long_ad) *
857 (*endnum - (i + 1)));
858 i--;
859 (*endnum)--;
862 UDF_I(inode)->i_lenExtents +=
863 numalloc << inode->i_sb->s_blocksize_bits;
868 static void udf_merge_extents(struct inode *inode,
869 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
870 int *endnum)
872 int i;
873 unsigned long blocksize = inode->i_sb->s_blocksize;
874 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
876 for (i = 0; i < (*endnum - 1); i++) {
877 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
878 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
880 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
881 (((li->extLength >> 30) ==
882 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
883 ((lip1->extLocation.logicalBlockNum -
884 li->extLocation.logicalBlockNum) ==
885 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
886 blocksize - 1) >> blocksize_bits)))) {
888 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
889 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
890 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
891 lip1->extLength = (lip1->extLength -
892 (li->extLength &
893 UDF_EXTENT_LENGTH_MASK) +
894 UDF_EXTENT_LENGTH_MASK) &
895 ~(blocksize - 1);
896 li->extLength = (li->extLength &
897 UDF_EXTENT_FLAG_MASK) +
898 (UDF_EXTENT_LENGTH_MASK + 1) -
899 blocksize;
900 lip1->extLocation.logicalBlockNum =
901 li->extLocation.logicalBlockNum +
902 ((li->extLength &
903 UDF_EXTENT_LENGTH_MASK) >>
904 blocksize_bits);
905 } else {
906 li->extLength = lip1->extLength +
907 (((li->extLength &
908 UDF_EXTENT_LENGTH_MASK) +
909 blocksize - 1) & ~(blocksize - 1));
910 if (*endnum > (i + 2))
911 memmove(&laarr[i + 1], &laarr[i + 2],
912 sizeof(struct long_ad) *
913 (*endnum - (i + 2)));
914 i--;
915 (*endnum)--;
917 } else if (((li->extLength >> 30) ==
918 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
919 ((lip1->extLength >> 30) ==
920 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
921 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
922 ((li->extLength &
923 UDF_EXTENT_LENGTH_MASK) +
924 blocksize - 1) >> blocksize_bits);
925 li->extLocation.logicalBlockNum = 0;
926 li->extLocation.partitionReferenceNum = 0;
928 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
929 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
930 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
931 lip1->extLength = (lip1->extLength -
932 (li->extLength &
933 UDF_EXTENT_LENGTH_MASK) +
934 UDF_EXTENT_LENGTH_MASK) &
935 ~(blocksize - 1);
936 li->extLength = (li->extLength &
937 UDF_EXTENT_FLAG_MASK) +
938 (UDF_EXTENT_LENGTH_MASK + 1) -
939 blocksize;
940 } else {
941 li->extLength = lip1->extLength +
942 (((li->extLength &
943 UDF_EXTENT_LENGTH_MASK) +
944 blocksize - 1) & ~(blocksize - 1));
945 if (*endnum > (i + 2))
946 memmove(&laarr[i + 1], &laarr[i + 2],
947 sizeof(struct long_ad) *
948 (*endnum - (i + 2)));
949 i--;
950 (*endnum)--;
952 } else if ((li->extLength >> 30) ==
953 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
954 udf_free_blocks(inode->i_sb, inode,
955 &li->extLocation, 0,
956 ((li->extLength &
957 UDF_EXTENT_LENGTH_MASK) +
958 blocksize - 1) >> blocksize_bits);
959 li->extLocation.logicalBlockNum = 0;
960 li->extLocation.partitionReferenceNum = 0;
961 li->extLength = (li->extLength &
962 UDF_EXTENT_LENGTH_MASK) |
963 EXT_NOT_RECORDED_NOT_ALLOCATED;
968 static void udf_update_extents(struct inode *inode,
969 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
970 int startnum, int endnum,
971 struct extent_position *epos)
973 int start = 0, i;
974 struct kernel_lb_addr tmploc;
975 uint32_t tmplen;
977 if (startnum > endnum) {
978 for (i = 0; i < (startnum - endnum); i++)
979 udf_delete_aext(inode, *epos, laarr[i].extLocation,
980 laarr[i].extLength);
981 } else if (startnum < endnum) {
982 for (i = 0; i < (endnum - startnum); i++) {
983 udf_insert_aext(inode, *epos, laarr[i].extLocation,
984 laarr[i].extLength);
985 udf_next_aext(inode, epos, &laarr[i].extLocation,
986 &laarr[i].extLength, 1);
987 start++;
991 for (i = start; i < endnum; i++) {
992 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
993 udf_write_aext(inode, epos, &laarr[i].extLocation,
994 laarr[i].extLength, 1);
998 struct buffer_head *udf_bread(struct inode *inode, int block,
999 int create, int *err)
1001 struct buffer_head *bh = NULL;
1003 bh = udf_getblk(inode, block, create, err);
1004 if (!bh)
1005 return NULL;
1007 if (buffer_uptodate(bh))
1008 return bh;
1010 ll_rw_block(READ, 1, &bh);
1012 wait_on_buffer(bh);
1013 if (buffer_uptodate(bh))
1014 return bh;
1016 brelse(bh);
1017 *err = -EIO;
1018 return NULL;
1021 void udf_truncate(struct inode *inode)
1023 int offset;
1024 int err;
1025 struct udf_inode_info *iinfo;
1027 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1028 S_ISLNK(inode->i_mode)))
1029 return;
1030 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1031 return;
1033 lock_kernel();
1034 iinfo = UDF_I(inode);
1035 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1036 if (inode->i_sb->s_blocksize <
1037 (udf_file_entry_alloc_offset(inode) +
1038 inode->i_size)) {
1039 udf_expand_file_adinicb(inode, inode->i_size, &err);
1040 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1041 inode->i_size = iinfo->i_lenAlloc;
1042 unlock_kernel();
1043 return;
1044 } else
1045 udf_truncate_extents(inode);
1046 } else {
1047 offset = inode->i_size & (inode->i_sb->s_blocksize - 1);
1048 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + offset,
1049 0x00, inode->i_sb->s_blocksize -
1050 offset - udf_file_entry_alloc_offset(inode));
1051 iinfo->i_lenAlloc = inode->i_size;
1053 } else {
1054 block_truncate_page(inode->i_mapping, inode->i_size,
1055 udf_get_block);
1056 udf_truncate_extents(inode);
1059 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1060 if (IS_SYNC(inode))
1061 udf_sync_inode(inode);
1062 else
1063 mark_inode_dirty(inode);
1064 unlock_kernel();
1067 static void __udf_read_inode(struct inode *inode)
1069 struct buffer_head *bh = NULL;
1070 struct fileEntry *fe;
1071 uint16_t ident;
1072 struct udf_inode_info *iinfo = UDF_I(inode);
1075 * Set defaults, but the inode is still incomplete!
1076 * Note: get_new_inode() sets the following on a new inode:
1077 * i_sb = sb
1078 * i_no = ino
1079 * i_flags = sb->s_flags
1080 * i_state = 0
1081 * clean_inode(): zero fills and sets
1082 * i_count = 1
1083 * i_nlink = 1
1084 * i_op = NULL;
1086 bh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 0, &ident);
1087 if (!bh) {
1088 printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed !bh\n",
1089 inode->i_ino);
1090 make_bad_inode(inode);
1091 return;
1094 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
1095 ident != TAG_IDENT_USE) {
1096 printk(KERN_ERR "udf: udf_read_inode(ino %ld) "
1097 "failed ident=%d\n", inode->i_ino, ident);
1098 brelse(bh);
1099 make_bad_inode(inode);
1100 return;
1103 fe = (struct fileEntry *)bh->b_data;
1105 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
1106 struct buffer_head *ibh;
1108 ibh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 1,
1109 &ident);
1110 if (ident == TAG_IDENT_IE && ibh) {
1111 struct buffer_head *nbh = NULL;
1112 struct kernel_lb_addr loc;
1113 struct indirectEntry *ie;
1115 ie = (struct indirectEntry *)ibh->b_data;
1116 loc = lelb_to_cpu(ie->indirectICB.extLocation);
1118 if (ie->indirectICB.extLength &&
1119 (nbh = udf_read_ptagged(inode->i_sb, &loc, 0,
1120 &ident))) {
1121 if (ident == TAG_IDENT_FE ||
1122 ident == TAG_IDENT_EFE) {
1123 memcpy(&iinfo->i_location,
1124 &loc,
1125 sizeof(struct kernel_lb_addr));
1126 brelse(bh);
1127 brelse(ibh);
1128 brelse(nbh);
1129 __udf_read_inode(inode);
1130 return;
1132 brelse(nbh);
1135 brelse(ibh);
1136 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
1137 printk(KERN_ERR "udf: unsupported strategy type: %d\n",
1138 le16_to_cpu(fe->icbTag.strategyType));
1139 brelse(bh);
1140 make_bad_inode(inode);
1141 return;
1143 udf_fill_inode(inode, bh);
1145 brelse(bh);
1148 static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1150 struct fileEntry *fe;
1151 struct extendedFileEntry *efe;
1152 int offset;
1153 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1154 struct udf_inode_info *iinfo = UDF_I(inode);
1156 fe = (struct fileEntry *)bh->b_data;
1157 efe = (struct extendedFileEntry *)bh->b_data;
1159 if (fe->icbTag.strategyType == cpu_to_le16(4))
1160 iinfo->i_strat4096 = 0;
1161 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
1162 iinfo->i_strat4096 = 1;
1164 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
1165 ICBTAG_FLAG_AD_MASK;
1166 iinfo->i_unique = 0;
1167 iinfo->i_lenEAttr = 0;
1168 iinfo->i_lenExtents = 0;
1169 iinfo->i_lenAlloc = 0;
1170 iinfo->i_next_alloc_block = 0;
1171 iinfo->i_next_alloc_goal = 0;
1172 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
1173 iinfo->i_efe = 1;
1174 iinfo->i_use = 0;
1175 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1176 sizeof(struct extendedFileEntry))) {
1177 make_bad_inode(inode);
1178 return;
1180 memcpy(iinfo->i_ext.i_data,
1181 bh->b_data + sizeof(struct extendedFileEntry),
1182 inode->i_sb->s_blocksize -
1183 sizeof(struct extendedFileEntry));
1184 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
1185 iinfo->i_efe = 0;
1186 iinfo->i_use = 0;
1187 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1188 sizeof(struct fileEntry))) {
1189 make_bad_inode(inode);
1190 return;
1192 memcpy(iinfo->i_ext.i_data,
1193 bh->b_data + sizeof(struct fileEntry),
1194 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1195 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
1196 iinfo->i_efe = 0;
1197 iinfo->i_use = 1;
1198 iinfo->i_lenAlloc = le32_to_cpu(
1199 ((struct unallocSpaceEntry *)bh->b_data)->
1200 lengthAllocDescs);
1201 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1202 sizeof(struct unallocSpaceEntry))) {
1203 make_bad_inode(inode);
1204 return;
1206 memcpy(iinfo->i_ext.i_data,
1207 bh->b_data + sizeof(struct unallocSpaceEntry),
1208 inode->i_sb->s_blocksize -
1209 sizeof(struct unallocSpaceEntry));
1210 return;
1213 inode->i_uid = le32_to_cpu(fe->uid);
1214 if (inode->i_uid == -1 ||
1215 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1216 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
1217 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
1219 inode->i_gid = le32_to_cpu(fe->gid);
1220 if (inode->i_gid == -1 ||
1221 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1222 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
1223 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
1225 inode->i_nlink = le16_to_cpu(fe->fileLinkCount);
1226 if (!inode->i_nlink)
1227 inode->i_nlink = 1;
1229 inode->i_size = le64_to_cpu(fe->informationLength);
1230 iinfo->i_lenExtents = inode->i_size;
1232 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
1233 sbi->s_fmode != UDF_INVALID_MODE)
1234 inode->i_mode = sbi->s_fmode;
1235 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
1236 sbi->s_dmode != UDF_INVALID_MODE)
1237 inode->i_mode = sbi->s_dmode;
1238 else
1239 inode->i_mode = udf_convert_permissions(fe);
1240 inode->i_mode &= ~sbi->s_umask;
1242 if (iinfo->i_efe == 0) {
1243 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
1244 (inode->i_sb->s_blocksize_bits - 9);
1246 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
1247 inode->i_atime = sbi->s_record_time;
1249 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1250 fe->modificationTime))
1251 inode->i_mtime = sbi->s_record_time;
1253 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
1254 inode->i_ctime = sbi->s_record_time;
1256 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1257 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1258 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1259 offset = sizeof(struct fileEntry) + iinfo->i_lenEAttr;
1260 } else {
1261 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
1262 (inode->i_sb->s_blocksize_bits - 9);
1264 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
1265 inode->i_atime = sbi->s_record_time;
1267 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1268 efe->modificationTime))
1269 inode->i_mtime = sbi->s_record_time;
1271 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
1272 iinfo->i_crtime = sbi->s_record_time;
1274 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
1275 inode->i_ctime = sbi->s_record_time;
1277 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1278 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1279 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
1280 offset = sizeof(struct extendedFileEntry) +
1281 iinfo->i_lenEAttr;
1284 switch (fe->icbTag.fileType) {
1285 case ICBTAG_FILE_TYPE_DIRECTORY:
1286 inode->i_op = &udf_dir_inode_operations;
1287 inode->i_fop = &udf_dir_operations;
1288 inode->i_mode |= S_IFDIR;
1289 inc_nlink(inode);
1290 break;
1291 case ICBTAG_FILE_TYPE_REALTIME:
1292 case ICBTAG_FILE_TYPE_REGULAR:
1293 case ICBTAG_FILE_TYPE_UNDEF:
1294 case ICBTAG_FILE_TYPE_VAT20:
1295 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1296 inode->i_data.a_ops = &udf_adinicb_aops;
1297 else
1298 inode->i_data.a_ops = &udf_aops;
1299 inode->i_op = &udf_file_inode_operations;
1300 inode->i_fop = &udf_file_operations;
1301 inode->i_mode |= S_IFREG;
1302 break;
1303 case ICBTAG_FILE_TYPE_BLOCK:
1304 inode->i_mode |= S_IFBLK;
1305 break;
1306 case ICBTAG_FILE_TYPE_CHAR:
1307 inode->i_mode |= S_IFCHR;
1308 break;
1309 case ICBTAG_FILE_TYPE_FIFO:
1310 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1311 break;
1312 case ICBTAG_FILE_TYPE_SOCKET:
1313 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1314 break;
1315 case ICBTAG_FILE_TYPE_SYMLINK:
1316 inode->i_data.a_ops = &udf_symlink_aops;
1317 inode->i_op = &udf_symlink_inode_operations;
1318 inode->i_mode = S_IFLNK | S_IRWXUGO;
1319 break;
1320 case ICBTAG_FILE_TYPE_MAIN:
1321 udf_debug("METADATA FILE-----\n");
1322 break;
1323 case ICBTAG_FILE_TYPE_MIRROR:
1324 udf_debug("METADATA MIRROR FILE-----\n");
1325 break;
1326 case ICBTAG_FILE_TYPE_BITMAP:
1327 udf_debug("METADATA BITMAP FILE-----\n");
1328 break;
1329 default:
1330 printk(KERN_ERR "udf: udf_fill_inode(ino %ld) failed unknown "
1331 "file type=%d\n", inode->i_ino,
1332 fe->icbTag.fileType);
1333 make_bad_inode(inode);
1334 return;
1336 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1337 struct deviceSpec *dsea =
1338 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1339 if (dsea) {
1340 init_special_inode(inode, inode->i_mode,
1341 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1342 le32_to_cpu(dsea->minorDeviceIdent)));
1343 /* Developer ID ??? */
1344 } else
1345 make_bad_inode(inode);
1349 static int udf_alloc_i_data(struct inode *inode, size_t size)
1351 struct udf_inode_info *iinfo = UDF_I(inode);
1352 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
1354 if (!iinfo->i_ext.i_data) {
1355 printk(KERN_ERR "udf:udf_alloc_i_data (ino %ld) "
1356 "no free memory\n", inode->i_ino);
1357 return -ENOMEM;
1360 return 0;
1363 static mode_t udf_convert_permissions(struct fileEntry *fe)
1365 mode_t mode;
1366 uint32_t permissions;
1367 uint32_t flags;
1369 permissions = le32_to_cpu(fe->permissions);
1370 flags = le16_to_cpu(fe->icbTag.flags);
1372 mode = ((permissions) & S_IRWXO) |
1373 ((permissions >> 2) & S_IRWXG) |
1374 ((permissions >> 4) & S_IRWXU) |
1375 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1376 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1377 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
1379 return mode;
1382 int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
1384 int ret;
1386 lock_kernel();
1387 ret = udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1388 unlock_kernel();
1390 return ret;
1393 int udf_sync_inode(struct inode *inode)
1395 return udf_update_inode(inode, 1);
1398 static int udf_update_inode(struct inode *inode, int do_sync)
1400 struct buffer_head *bh = NULL;
1401 struct fileEntry *fe;
1402 struct extendedFileEntry *efe;
1403 uint32_t udfperms;
1404 uint16_t icbflags;
1405 uint16_t crclen;
1406 int err = 0;
1407 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1408 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1409 struct udf_inode_info *iinfo = UDF_I(inode);
1411 bh = udf_tgetblk(inode->i_sb,
1412 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
1413 if (!bh) {
1414 udf_debug("getblk failure\n");
1415 return -ENOMEM;
1418 lock_buffer(bh);
1419 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1420 fe = (struct fileEntry *)bh->b_data;
1421 efe = (struct extendedFileEntry *)bh->b_data;
1423 if (iinfo->i_use) {
1424 struct unallocSpaceEntry *use =
1425 (struct unallocSpaceEntry *)bh->b_data;
1427 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1428 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1429 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
1430 sizeof(struct unallocSpaceEntry));
1431 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1432 use->descTag.tagLocation =
1433 cpu_to_le32(iinfo->i_location.logicalBlockNum);
1434 crclen = sizeof(struct unallocSpaceEntry) +
1435 iinfo->i_lenAlloc - sizeof(struct tag);
1436 use->descTag.descCRCLength = cpu_to_le16(crclen);
1437 use->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)use +
1438 sizeof(struct tag),
1439 crclen));
1440 use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
1442 goto out;
1445 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1446 fe->uid = cpu_to_le32(-1);
1447 else
1448 fe->uid = cpu_to_le32(inode->i_uid);
1450 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1451 fe->gid = cpu_to_le32(-1);
1452 else
1453 fe->gid = cpu_to_le32(inode->i_gid);
1455 udfperms = ((inode->i_mode & S_IRWXO)) |
1456 ((inode->i_mode & S_IRWXG) << 2) |
1457 ((inode->i_mode & S_IRWXU) << 4);
1459 udfperms |= (le32_to_cpu(fe->permissions) &
1460 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1461 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1462 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
1463 fe->permissions = cpu_to_le32(udfperms);
1465 if (S_ISDIR(inode->i_mode))
1466 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1467 else
1468 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1470 fe->informationLength = cpu_to_le64(inode->i_size);
1472 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1473 struct regid *eid;
1474 struct deviceSpec *dsea =
1475 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1476 if (!dsea) {
1477 dsea = (struct deviceSpec *)
1478 udf_add_extendedattr(inode,
1479 sizeof(struct deviceSpec) +
1480 sizeof(struct regid), 12, 0x3);
1481 dsea->attrType = cpu_to_le32(12);
1482 dsea->attrSubtype = 1;
1483 dsea->attrLength = cpu_to_le32(
1484 sizeof(struct deviceSpec) +
1485 sizeof(struct regid));
1486 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
1488 eid = (struct regid *)dsea->impUse;
1489 memset(eid, 0, sizeof(struct regid));
1490 strcpy(eid->ident, UDF_ID_DEVELOPER);
1491 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1492 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1493 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1494 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1497 if (iinfo->i_efe == 0) {
1498 memcpy(bh->b_data + sizeof(struct fileEntry),
1499 iinfo->i_ext.i_data,
1500 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1501 fe->logicalBlocksRecorded = cpu_to_le64(
1502 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1503 (blocksize_bits - 9));
1505 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1506 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1507 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
1508 memset(&(fe->impIdent), 0, sizeof(struct regid));
1509 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1510 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1511 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1512 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1513 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1514 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1515 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1516 crclen = sizeof(struct fileEntry);
1517 } else {
1518 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1519 iinfo->i_ext.i_data,
1520 inode->i_sb->s_blocksize -
1521 sizeof(struct extendedFileEntry));
1522 efe->objectSize = cpu_to_le64(inode->i_size);
1523 efe->logicalBlocksRecorded = cpu_to_le64(
1524 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1525 (blocksize_bits - 9));
1527 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1528 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1529 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1530 iinfo->i_crtime = inode->i_atime;
1532 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1533 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1534 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1535 iinfo->i_crtime = inode->i_mtime;
1537 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1538 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1539 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1540 iinfo->i_crtime = inode->i_ctime;
1542 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1543 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1544 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1545 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
1547 memset(&(efe->impIdent), 0, sizeof(struct regid));
1548 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1549 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1550 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1551 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1552 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1553 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1554 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1555 crclen = sizeof(struct extendedFileEntry);
1557 if (iinfo->i_strat4096) {
1558 fe->icbTag.strategyType = cpu_to_le16(4096);
1559 fe->icbTag.strategyParameter = cpu_to_le16(1);
1560 fe->icbTag.numEntries = cpu_to_le16(2);
1561 } else {
1562 fe->icbTag.strategyType = cpu_to_le16(4);
1563 fe->icbTag.numEntries = cpu_to_le16(1);
1566 if (S_ISDIR(inode->i_mode))
1567 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1568 else if (S_ISREG(inode->i_mode))
1569 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1570 else if (S_ISLNK(inode->i_mode))
1571 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1572 else if (S_ISBLK(inode->i_mode))
1573 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1574 else if (S_ISCHR(inode->i_mode))
1575 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1576 else if (S_ISFIFO(inode->i_mode))
1577 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1578 else if (S_ISSOCK(inode->i_mode))
1579 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1581 icbflags = iinfo->i_alloc_type |
1582 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1583 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1584 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1585 (le16_to_cpu(fe->icbTag.flags) &
1586 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1587 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
1589 fe->icbTag.flags = cpu_to_le16(icbflags);
1590 if (sbi->s_udfrev >= 0x0200)
1591 fe->descTag.descVersion = cpu_to_le16(3);
1592 else
1593 fe->descTag.descVersion = cpu_to_le16(2);
1594 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
1595 fe->descTag.tagLocation = cpu_to_le32(
1596 iinfo->i_location.logicalBlockNum);
1597 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
1598 fe->descTag.descCRCLength = cpu_to_le16(crclen);
1599 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
1600 crclen));
1601 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
1603 out:
1604 set_buffer_uptodate(bh);
1605 unlock_buffer(bh);
1607 /* write the data blocks */
1608 mark_buffer_dirty(bh);
1609 if (do_sync) {
1610 sync_dirty_buffer(bh);
1611 if (buffer_write_io_error(bh)) {
1612 printk(KERN_WARNING "IO error syncing udf inode "
1613 "[%s:%08lx]\n", inode->i_sb->s_id,
1614 inode->i_ino);
1615 err = -EIO;
1618 brelse(bh);
1620 return err;
1623 struct inode *udf_iget(struct super_block *sb, struct kernel_lb_addr *ino)
1625 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1626 struct inode *inode = iget_locked(sb, block);
1628 if (!inode)
1629 return NULL;
1631 if (inode->i_state & I_NEW) {
1632 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
1633 __udf_read_inode(inode);
1634 unlock_new_inode(inode);
1637 if (is_bad_inode(inode))
1638 goto out_iput;
1640 if (ino->logicalBlockNum >= UDF_SB(sb)->
1641 s_partmaps[ino->partitionReferenceNum].s_partition_len) {
1642 udf_debug("block=%d, partition=%d out of range\n",
1643 ino->logicalBlockNum, ino->partitionReferenceNum);
1644 make_bad_inode(inode);
1645 goto out_iput;
1648 return inode;
1650 out_iput:
1651 iput(inode);
1652 return NULL;
1655 int8_t udf_add_aext(struct inode *inode, struct extent_position *epos,
1656 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1658 int adsize;
1659 struct short_ad *sad = NULL;
1660 struct long_ad *lad = NULL;
1661 struct allocExtDesc *aed;
1662 int8_t etype;
1663 uint8_t *ptr;
1664 struct udf_inode_info *iinfo = UDF_I(inode);
1666 if (!epos->bh)
1667 ptr = iinfo->i_ext.i_data + epos->offset -
1668 udf_file_entry_alloc_offset(inode) +
1669 iinfo->i_lenEAttr;
1670 else
1671 ptr = epos->bh->b_data + epos->offset;
1673 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1674 adsize = sizeof(struct short_ad);
1675 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1676 adsize = sizeof(struct long_ad);
1677 else
1678 return -1;
1680 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
1681 unsigned char *sptr, *dptr;
1682 struct buffer_head *nbh;
1683 int err, loffset;
1684 struct kernel_lb_addr obloc = epos->block;
1686 epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1687 obloc.partitionReferenceNum,
1688 obloc.logicalBlockNum, &err);
1689 if (!epos->block.logicalBlockNum)
1690 return -1;
1691 nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
1692 &epos->block,
1693 0));
1694 if (!nbh)
1695 return -1;
1696 lock_buffer(nbh);
1697 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1698 set_buffer_uptodate(nbh);
1699 unlock_buffer(nbh);
1700 mark_buffer_dirty_inode(nbh, inode);
1702 aed = (struct allocExtDesc *)(nbh->b_data);
1703 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
1704 aed->previousAllocExtLocation =
1705 cpu_to_le32(obloc.logicalBlockNum);
1706 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
1707 loffset = epos->offset;
1708 aed->lengthAllocDescs = cpu_to_le32(adsize);
1709 sptr = ptr - adsize;
1710 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1711 memcpy(dptr, sptr, adsize);
1712 epos->offset = sizeof(struct allocExtDesc) + adsize;
1713 } else {
1714 loffset = epos->offset + adsize;
1715 aed->lengthAllocDescs = cpu_to_le32(0);
1716 sptr = ptr;
1717 epos->offset = sizeof(struct allocExtDesc);
1719 if (epos->bh) {
1720 aed = (struct allocExtDesc *)epos->bh->b_data;
1721 le32_add_cpu(&aed->lengthAllocDescs, adsize);
1722 } else {
1723 iinfo->i_lenAlloc += adsize;
1724 mark_inode_dirty(inode);
1727 if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
1728 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
1729 epos->block.logicalBlockNum, sizeof(struct tag));
1730 else
1731 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
1732 epos->block.logicalBlockNum, sizeof(struct tag));
1733 switch (iinfo->i_alloc_type) {
1734 case ICBTAG_FLAG_AD_SHORT:
1735 sad = (struct short_ad *)sptr;
1736 sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1737 inode->i_sb->s_blocksize);
1738 sad->extPosition =
1739 cpu_to_le32(epos->block.logicalBlockNum);
1740 break;
1741 case ICBTAG_FLAG_AD_LONG:
1742 lad = (struct long_ad *)sptr;
1743 lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1744 inode->i_sb->s_blocksize);
1745 lad->extLocation = cpu_to_lelb(epos->block);
1746 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1747 break;
1749 if (epos->bh) {
1750 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1751 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1752 udf_update_tag(epos->bh->b_data, loffset);
1753 else
1754 udf_update_tag(epos->bh->b_data,
1755 sizeof(struct allocExtDesc));
1756 mark_buffer_dirty_inode(epos->bh, inode);
1757 brelse(epos->bh);
1758 } else {
1759 mark_inode_dirty(inode);
1761 epos->bh = nbh;
1764 etype = udf_write_aext(inode, epos, eloc, elen, inc);
1766 if (!epos->bh) {
1767 iinfo->i_lenAlloc += adsize;
1768 mark_inode_dirty(inode);
1769 } else {
1770 aed = (struct allocExtDesc *)epos->bh->b_data;
1771 le32_add_cpu(&aed->lengthAllocDescs, adsize);
1772 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1773 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1774 udf_update_tag(epos->bh->b_data,
1775 epos->offset + (inc ? 0 : adsize));
1776 else
1777 udf_update_tag(epos->bh->b_data,
1778 sizeof(struct allocExtDesc));
1779 mark_buffer_dirty_inode(epos->bh, inode);
1782 return etype;
1785 int8_t udf_write_aext(struct inode *inode, struct extent_position *epos,
1786 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1788 int adsize;
1789 uint8_t *ptr;
1790 struct short_ad *sad;
1791 struct long_ad *lad;
1792 struct udf_inode_info *iinfo = UDF_I(inode);
1794 if (!epos->bh)
1795 ptr = iinfo->i_ext.i_data + epos->offset -
1796 udf_file_entry_alloc_offset(inode) +
1797 iinfo->i_lenEAttr;
1798 else
1799 ptr = epos->bh->b_data + epos->offset;
1801 switch (iinfo->i_alloc_type) {
1802 case ICBTAG_FLAG_AD_SHORT:
1803 sad = (struct short_ad *)ptr;
1804 sad->extLength = cpu_to_le32(elen);
1805 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
1806 adsize = sizeof(struct short_ad);
1807 break;
1808 case ICBTAG_FLAG_AD_LONG:
1809 lad = (struct long_ad *)ptr;
1810 lad->extLength = cpu_to_le32(elen);
1811 lad->extLocation = cpu_to_lelb(*eloc);
1812 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1813 adsize = sizeof(struct long_ad);
1814 break;
1815 default:
1816 return -1;
1819 if (epos->bh) {
1820 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1821 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
1822 struct allocExtDesc *aed =
1823 (struct allocExtDesc *)epos->bh->b_data;
1824 udf_update_tag(epos->bh->b_data,
1825 le32_to_cpu(aed->lengthAllocDescs) +
1826 sizeof(struct allocExtDesc));
1828 mark_buffer_dirty_inode(epos->bh, inode);
1829 } else {
1830 mark_inode_dirty(inode);
1833 if (inc)
1834 epos->offset += adsize;
1836 return (elen >> 30);
1839 int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
1840 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
1842 int8_t etype;
1844 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
1845 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
1846 int block;
1847 epos->block = *eloc;
1848 epos->offset = sizeof(struct allocExtDesc);
1849 brelse(epos->bh);
1850 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
1851 epos->bh = udf_tread(inode->i_sb, block);
1852 if (!epos->bh) {
1853 udf_debug("reading block %d failed!\n", block);
1854 return -1;
1858 return etype;
1861 int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
1862 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
1864 int alen;
1865 int8_t etype;
1866 uint8_t *ptr;
1867 struct short_ad *sad;
1868 struct long_ad *lad;
1869 struct udf_inode_info *iinfo = UDF_I(inode);
1871 if (!epos->bh) {
1872 if (!epos->offset)
1873 epos->offset = udf_file_entry_alloc_offset(inode);
1874 ptr = iinfo->i_ext.i_data + epos->offset -
1875 udf_file_entry_alloc_offset(inode) +
1876 iinfo->i_lenEAttr;
1877 alen = udf_file_entry_alloc_offset(inode) +
1878 iinfo->i_lenAlloc;
1879 } else {
1880 if (!epos->offset)
1881 epos->offset = sizeof(struct allocExtDesc);
1882 ptr = epos->bh->b_data + epos->offset;
1883 alen = sizeof(struct allocExtDesc) +
1884 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
1885 lengthAllocDescs);
1888 switch (iinfo->i_alloc_type) {
1889 case ICBTAG_FLAG_AD_SHORT:
1890 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
1891 if (!sad)
1892 return -1;
1893 etype = le32_to_cpu(sad->extLength) >> 30;
1894 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
1895 eloc->partitionReferenceNum =
1896 iinfo->i_location.partitionReferenceNum;
1897 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
1898 break;
1899 case ICBTAG_FLAG_AD_LONG:
1900 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
1901 if (!lad)
1902 return -1;
1903 etype = le32_to_cpu(lad->extLength) >> 30;
1904 *eloc = lelb_to_cpu(lad->extLocation);
1905 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
1906 break;
1907 default:
1908 udf_debug("alloc_type = %d unsupported\n",
1909 iinfo->i_alloc_type);
1910 return -1;
1913 return etype;
1916 static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
1917 struct kernel_lb_addr neloc, uint32_t nelen)
1919 struct kernel_lb_addr oeloc;
1920 uint32_t oelen;
1921 int8_t etype;
1923 if (epos.bh)
1924 get_bh(epos.bh);
1926 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
1927 udf_write_aext(inode, &epos, &neloc, nelen, 1);
1928 neloc = oeloc;
1929 nelen = (etype << 30) | oelen;
1931 udf_add_aext(inode, &epos, &neloc, nelen, 1);
1932 brelse(epos.bh);
1934 return (nelen >> 30);
1937 int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
1938 struct kernel_lb_addr eloc, uint32_t elen)
1940 struct extent_position oepos;
1941 int adsize;
1942 int8_t etype;
1943 struct allocExtDesc *aed;
1944 struct udf_inode_info *iinfo;
1946 if (epos.bh) {
1947 get_bh(epos.bh);
1948 get_bh(epos.bh);
1951 iinfo = UDF_I(inode);
1952 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1953 adsize = sizeof(struct short_ad);
1954 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1955 adsize = sizeof(struct long_ad);
1956 else
1957 adsize = 0;
1959 oepos = epos;
1960 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
1961 return -1;
1963 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
1964 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
1965 if (oepos.bh != epos.bh) {
1966 oepos.block = epos.block;
1967 brelse(oepos.bh);
1968 get_bh(epos.bh);
1969 oepos.bh = epos.bh;
1970 oepos.offset = epos.offset - adsize;
1973 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
1974 elen = 0;
1976 if (epos.bh != oepos.bh) {
1977 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
1978 udf_write_aext(inode, &oepos, &eloc, elen, 1);
1979 udf_write_aext(inode, &oepos, &eloc, elen, 1);
1980 if (!oepos.bh) {
1981 iinfo->i_lenAlloc -= (adsize * 2);
1982 mark_inode_dirty(inode);
1983 } else {
1984 aed = (struct allocExtDesc *)oepos.bh->b_data;
1985 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
1986 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1987 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1988 udf_update_tag(oepos.bh->b_data,
1989 oepos.offset - (2 * adsize));
1990 else
1991 udf_update_tag(oepos.bh->b_data,
1992 sizeof(struct allocExtDesc));
1993 mark_buffer_dirty_inode(oepos.bh, inode);
1995 } else {
1996 udf_write_aext(inode, &oepos, &eloc, elen, 1);
1997 if (!oepos.bh) {
1998 iinfo->i_lenAlloc -= adsize;
1999 mark_inode_dirty(inode);
2000 } else {
2001 aed = (struct allocExtDesc *)oepos.bh->b_data;
2002 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
2003 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2004 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2005 udf_update_tag(oepos.bh->b_data,
2006 epos.offset - adsize);
2007 else
2008 udf_update_tag(oepos.bh->b_data,
2009 sizeof(struct allocExtDesc));
2010 mark_buffer_dirty_inode(oepos.bh, inode);
2014 brelse(epos.bh);
2015 brelse(oepos.bh);
2017 return (elen >> 30);
2020 int8_t inode_bmap(struct inode *inode, sector_t block,
2021 struct extent_position *pos, struct kernel_lb_addr *eloc,
2022 uint32_t *elen, sector_t *offset)
2024 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2025 loff_t lbcount = 0, bcount =
2026 (loff_t) block << blocksize_bits;
2027 int8_t etype;
2028 struct udf_inode_info *iinfo;
2030 iinfo = UDF_I(inode);
2031 pos->offset = 0;
2032 pos->block = iinfo->i_location;
2033 pos->bh = NULL;
2034 *elen = 0;
2036 do {
2037 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2038 if (etype == -1) {
2039 *offset = (bcount - lbcount) >> blocksize_bits;
2040 iinfo->i_lenExtents = lbcount;
2041 return -1;
2043 lbcount += *elen;
2044 } while (lbcount <= bcount);
2046 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
2048 return etype;
2051 long udf_block_map(struct inode *inode, sector_t block)
2053 struct kernel_lb_addr eloc;
2054 uint32_t elen;
2055 sector_t offset;
2056 struct extent_position epos = {};
2057 int ret;
2059 lock_kernel();
2061 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2062 (EXT_RECORDED_ALLOCATED >> 30))
2063 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
2064 else
2065 ret = 0;
2067 unlock_kernel();
2068 brelse(epos.bh);
2070 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2071 return udf_fixed_to_variable(ret);
2072 else
2073 return ret;