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