arm64: kgdb: Fix single-step exception handling oops
[linux/fpc-iii.git] / fs / udf / inode.c
blob9e66d85021fcb38e5e28249138d91ff47a847d9e
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/module.h>
35 #include <linux/pagemap.h>
36 #include <linux/writeback.h>
37 #include <linux/slab.h>
38 #include <linux/crc-itu-t.h>
39 #include <linux/mpage.h>
40 #include <linux/uio.h>
42 #include "udf_i.h"
43 #include "udf_sb.h"
45 MODULE_AUTHOR("Ben Fennema");
46 MODULE_DESCRIPTION("Universal Disk Format Filesystem");
47 MODULE_LICENSE("GPL");
49 #define EXTENT_MERGE_SIZE 5
51 static umode_t udf_convert_permissions(struct fileEntry *);
52 static int udf_update_inode(struct inode *, int);
53 static int udf_sync_inode(struct inode *inode);
54 static int udf_alloc_i_data(struct inode *inode, size_t size);
55 static sector_t inode_getblk(struct inode *, sector_t, int *, int *);
56 static int8_t udf_insert_aext(struct inode *, struct extent_position,
57 struct kernel_lb_addr, uint32_t);
58 static void udf_split_extents(struct inode *, int *, int, int,
59 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
60 static void udf_prealloc_extents(struct inode *, int, int,
61 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
62 static void udf_merge_extents(struct inode *,
63 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
64 static void udf_update_extents(struct inode *,
65 struct 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);
69 static void __udf_clear_extent_cache(struct inode *inode)
71 struct udf_inode_info *iinfo = UDF_I(inode);
73 if (iinfo->cached_extent.lstart != -1) {
74 brelse(iinfo->cached_extent.epos.bh);
75 iinfo->cached_extent.lstart = -1;
79 /* Invalidate extent cache */
80 static void udf_clear_extent_cache(struct inode *inode)
82 struct udf_inode_info *iinfo = UDF_I(inode);
84 spin_lock(&iinfo->i_extent_cache_lock);
85 __udf_clear_extent_cache(inode);
86 spin_unlock(&iinfo->i_extent_cache_lock);
89 /* Return contents of extent cache */
90 static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
91 loff_t *lbcount, struct extent_position *pos)
93 struct udf_inode_info *iinfo = UDF_I(inode);
94 int ret = 0;
96 spin_lock(&iinfo->i_extent_cache_lock);
97 if ((iinfo->cached_extent.lstart <= bcount) &&
98 (iinfo->cached_extent.lstart != -1)) {
99 /* Cache hit */
100 *lbcount = iinfo->cached_extent.lstart;
101 memcpy(pos, &iinfo->cached_extent.epos,
102 sizeof(struct extent_position));
103 if (pos->bh)
104 get_bh(pos->bh);
105 ret = 1;
107 spin_unlock(&iinfo->i_extent_cache_lock);
108 return ret;
111 /* Add extent to extent cache */
112 static void udf_update_extent_cache(struct inode *inode, loff_t estart,
113 struct extent_position *pos, int next_epos)
115 struct udf_inode_info *iinfo = UDF_I(inode);
117 spin_lock(&iinfo->i_extent_cache_lock);
118 /* Invalidate previously cached extent */
119 __udf_clear_extent_cache(inode);
120 if (pos->bh)
121 get_bh(pos->bh);
122 memcpy(&iinfo->cached_extent.epos, pos,
123 sizeof(struct extent_position));
124 iinfo->cached_extent.lstart = estart;
125 if (next_epos)
126 switch (iinfo->i_alloc_type) {
127 case ICBTAG_FLAG_AD_SHORT:
128 iinfo->cached_extent.epos.offset -=
129 sizeof(struct short_ad);
130 break;
131 case ICBTAG_FLAG_AD_LONG:
132 iinfo->cached_extent.epos.offset -=
133 sizeof(struct long_ad);
135 spin_unlock(&iinfo->i_extent_cache_lock);
138 void udf_evict_inode(struct inode *inode)
140 struct udf_inode_info *iinfo = UDF_I(inode);
141 int want_delete = 0;
143 if (!inode->i_nlink && !is_bad_inode(inode)) {
144 want_delete = 1;
145 udf_setsize(inode, 0);
146 udf_update_inode(inode, IS_SYNC(inode));
148 truncate_inode_pages_final(&inode->i_data);
149 invalidate_inode_buffers(inode);
150 clear_inode(inode);
151 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
152 inode->i_size != iinfo->i_lenExtents) {
153 udf_warn(inode->i_sb, "Inode %lu (mode %o) has inode size %llu different from extent length %llu. Filesystem need not be standards compliant.\n",
154 inode->i_ino, inode->i_mode,
155 (unsigned long long)inode->i_size,
156 (unsigned long long)iinfo->i_lenExtents);
158 kfree(iinfo->i_ext.i_data);
159 iinfo->i_ext.i_data = NULL;
160 udf_clear_extent_cache(inode);
161 if (want_delete) {
162 udf_free_inode(inode);
166 static void udf_write_failed(struct address_space *mapping, loff_t to)
168 struct inode *inode = mapping->host;
169 struct udf_inode_info *iinfo = UDF_I(inode);
170 loff_t isize = inode->i_size;
172 if (to > isize) {
173 truncate_pagecache(inode, isize);
174 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
175 down_write(&iinfo->i_data_sem);
176 udf_clear_extent_cache(inode);
177 udf_truncate_extents(inode);
178 up_write(&iinfo->i_data_sem);
183 static int udf_writepage(struct page *page, struct writeback_control *wbc)
185 return block_write_full_page(page, udf_get_block, wbc);
188 static int udf_writepages(struct address_space *mapping,
189 struct writeback_control *wbc)
191 return mpage_writepages(mapping, wbc, udf_get_block);
194 static int udf_readpage(struct file *file, struct page *page)
196 return mpage_readpage(page, udf_get_block);
199 static int udf_readpages(struct file *file, struct address_space *mapping,
200 struct list_head *pages, unsigned nr_pages)
202 return mpage_readpages(mapping, pages, nr_pages, udf_get_block);
205 static int udf_write_begin(struct file *file, struct address_space *mapping,
206 loff_t pos, unsigned len, unsigned flags,
207 struct page **pagep, void **fsdata)
209 int ret;
211 ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
212 if (unlikely(ret))
213 udf_write_failed(mapping, pos + len);
214 return ret;
217 static ssize_t udf_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
219 struct file *file = iocb->ki_filp;
220 struct address_space *mapping = file->f_mapping;
221 struct inode *inode = mapping->host;
222 size_t count = iov_iter_count(iter);
223 ssize_t ret;
225 ret = blockdev_direct_IO(iocb, inode, iter, udf_get_block);
226 if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE))
227 udf_write_failed(mapping, iocb->ki_pos + count);
228 return ret;
231 static sector_t udf_bmap(struct address_space *mapping, sector_t block)
233 return generic_block_bmap(mapping, block, udf_get_block);
236 const struct address_space_operations udf_aops = {
237 .readpage = udf_readpage,
238 .readpages = udf_readpages,
239 .writepage = udf_writepage,
240 .writepages = udf_writepages,
241 .write_begin = udf_write_begin,
242 .write_end = generic_write_end,
243 .direct_IO = udf_direct_IO,
244 .bmap = udf_bmap,
248 * Expand file stored in ICB to a normal one-block-file
250 * This function requires i_data_sem for writing and releases it.
251 * This function requires i_mutex held
253 int udf_expand_file_adinicb(struct inode *inode)
255 struct page *page;
256 char *kaddr;
257 struct udf_inode_info *iinfo = UDF_I(inode);
258 int err;
259 struct writeback_control udf_wbc = {
260 .sync_mode = WB_SYNC_NONE,
261 .nr_to_write = 1,
264 WARN_ON_ONCE(!inode_is_locked(inode));
265 if (!iinfo->i_lenAlloc) {
266 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
267 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
268 else
269 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
270 /* from now on we have normal address_space methods */
271 inode->i_data.a_ops = &udf_aops;
272 up_write(&iinfo->i_data_sem);
273 mark_inode_dirty(inode);
274 return 0;
277 * Release i_data_sem so that we can lock a page - page lock ranks
278 * above i_data_sem. i_mutex still protects us against file changes.
280 up_write(&iinfo->i_data_sem);
282 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
283 if (!page)
284 return -ENOMEM;
286 if (!PageUptodate(page)) {
287 kaddr = kmap(page);
288 memset(kaddr + iinfo->i_lenAlloc, 0x00,
289 PAGE_SIZE - iinfo->i_lenAlloc);
290 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
291 iinfo->i_lenAlloc);
292 flush_dcache_page(page);
293 SetPageUptodate(page);
294 kunmap(page);
296 down_write(&iinfo->i_data_sem);
297 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
298 iinfo->i_lenAlloc);
299 iinfo->i_lenAlloc = 0;
300 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
301 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
302 else
303 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
304 /* from now on we have normal address_space methods */
305 inode->i_data.a_ops = &udf_aops;
306 up_write(&iinfo->i_data_sem);
307 err = inode->i_data.a_ops->writepage(page, &udf_wbc);
308 if (err) {
309 /* Restore everything back so that we don't lose data... */
310 lock_page(page);
311 kaddr = kmap(page);
312 down_write(&iinfo->i_data_sem);
313 memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr,
314 inode->i_size);
315 kunmap(page);
316 unlock_page(page);
317 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
318 inode->i_data.a_ops = &udf_adinicb_aops;
319 up_write(&iinfo->i_data_sem);
321 put_page(page);
322 mark_inode_dirty(inode);
324 return err;
327 struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
328 int *err)
330 int newblock;
331 struct buffer_head *dbh = NULL;
332 struct kernel_lb_addr eloc;
333 uint8_t alloctype;
334 struct extent_position epos;
336 struct udf_fileident_bh sfibh, dfibh;
337 loff_t f_pos = udf_ext0_offset(inode);
338 int size = udf_ext0_offset(inode) + inode->i_size;
339 struct fileIdentDesc cfi, *sfi, *dfi;
340 struct udf_inode_info *iinfo = UDF_I(inode);
342 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
343 alloctype = ICBTAG_FLAG_AD_SHORT;
344 else
345 alloctype = ICBTAG_FLAG_AD_LONG;
347 if (!inode->i_size) {
348 iinfo->i_alloc_type = alloctype;
349 mark_inode_dirty(inode);
350 return NULL;
353 /* alloc block, and copy data to it */
354 *block = udf_new_block(inode->i_sb, inode,
355 iinfo->i_location.partitionReferenceNum,
356 iinfo->i_location.logicalBlockNum, err);
357 if (!(*block))
358 return NULL;
359 newblock = udf_get_pblock(inode->i_sb, *block,
360 iinfo->i_location.partitionReferenceNum,
362 if (!newblock)
363 return NULL;
364 dbh = udf_tgetblk(inode->i_sb, newblock);
365 if (!dbh)
366 return NULL;
367 lock_buffer(dbh);
368 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
369 set_buffer_uptodate(dbh);
370 unlock_buffer(dbh);
371 mark_buffer_dirty_inode(dbh, inode);
373 sfibh.soffset = sfibh.eoffset =
374 f_pos & (inode->i_sb->s_blocksize - 1);
375 sfibh.sbh = sfibh.ebh = NULL;
376 dfibh.soffset = dfibh.eoffset = 0;
377 dfibh.sbh = dfibh.ebh = dbh;
378 while (f_pos < size) {
379 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
380 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
381 NULL, NULL, NULL);
382 if (!sfi) {
383 brelse(dbh);
384 return NULL;
386 iinfo->i_alloc_type = alloctype;
387 sfi->descTag.tagLocation = cpu_to_le32(*block);
388 dfibh.soffset = dfibh.eoffset;
389 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
390 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
391 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
392 sfi->fileIdent +
393 le16_to_cpu(sfi->lengthOfImpUse))) {
394 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
395 brelse(dbh);
396 return NULL;
399 mark_buffer_dirty_inode(dbh, inode);
401 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
402 iinfo->i_lenAlloc);
403 iinfo->i_lenAlloc = 0;
404 eloc.logicalBlockNum = *block;
405 eloc.partitionReferenceNum =
406 iinfo->i_location.partitionReferenceNum;
407 iinfo->i_lenExtents = inode->i_size;
408 epos.bh = NULL;
409 epos.block = iinfo->i_location;
410 epos.offset = udf_file_entry_alloc_offset(inode);
411 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
412 /* UniqueID stuff */
414 brelse(epos.bh);
415 mark_inode_dirty(inode);
416 return dbh;
419 static int udf_get_block(struct inode *inode, sector_t block,
420 struct buffer_head *bh_result, int create)
422 int err, new;
423 sector_t phys = 0;
424 struct udf_inode_info *iinfo;
426 if (!create) {
427 phys = udf_block_map(inode, block);
428 if (phys)
429 map_bh(bh_result, inode->i_sb, phys);
430 return 0;
433 err = -EIO;
434 new = 0;
435 iinfo = UDF_I(inode);
437 down_write(&iinfo->i_data_sem);
438 if (block == iinfo->i_next_alloc_block + 1) {
439 iinfo->i_next_alloc_block++;
440 iinfo->i_next_alloc_goal++;
443 udf_clear_extent_cache(inode);
444 phys = inode_getblk(inode, block, &err, &new);
445 if (!phys)
446 goto abort;
448 if (new)
449 set_buffer_new(bh_result);
450 map_bh(bh_result, inode->i_sb, phys);
452 abort:
453 up_write(&iinfo->i_data_sem);
454 return err;
457 static struct buffer_head *udf_getblk(struct inode *inode, long block,
458 int create, int *err)
460 struct buffer_head *bh;
461 struct buffer_head dummy;
463 dummy.b_state = 0;
464 dummy.b_blocknr = -1000;
465 *err = udf_get_block(inode, block, &dummy, create);
466 if (!*err && buffer_mapped(&dummy)) {
467 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
468 if (buffer_new(&dummy)) {
469 lock_buffer(bh);
470 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
471 set_buffer_uptodate(bh);
472 unlock_buffer(bh);
473 mark_buffer_dirty_inode(bh, inode);
475 return bh;
478 return NULL;
481 /* Extend the file with new blocks totaling 'new_block_bytes',
482 * return the number of extents added
484 static int udf_do_extend_file(struct inode *inode,
485 struct extent_position *last_pos,
486 struct kernel_long_ad *last_ext,
487 loff_t new_block_bytes)
489 uint32_t add;
490 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
491 struct super_block *sb = inode->i_sb;
492 struct kernel_lb_addr prealloc_loc = {};
493 int prealloc_len = 0;
494 struct udf_inode_info *iinfo;
495 int err;
497 /* The previous extent is fake and we should not extend by anything
498 * - there's nothing to do... */
499 if (!new_block_bytes && fake)
500 return 0;
502 iinfo = UDF_I(inode);
503 /* Round the last extent up to a multiple of block size */
504 if (last_ext->extLength & (sb->s_blocksize - 1)) {
505 last_ext->extLength =
506 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
507 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
508 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
509 iinfo->i_lenExtents =
510 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
511 ~(sb->s_blocksize - 1);
514 /* Last extent are just preallocated blocks? */
515 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
516 EXT_NOT_RECORDED_ALLOCATED) {
517 /* Save the extent so that we can reattach it to the end */
518 prealloc_loc = last_ext->extLocation;
519 prealloc_len = last_ext->extLength;
520 /* Mark the extent as a hole */
521 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
522 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
523 last_ext->extLocation.logicalBlockNum = 0;
524 last_ext->extLocation.partitionReferenceNum = 0;
527 /* Can we merge with the previous extent? */
528 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
529 EXT_NOT_RECORDED_NOT_ALLOCATED) {
530 add = (1 << 30) - sb->s_blocksize -
531 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
532 if (add > new_block_bytes)
533 add = new_block_bytes;
534 new_block_bytes -= add;
535 last_ext->extLength += add;
538 if (fake) {
539 udf_add_aext(inode, last_pos, &last_ext->extLocation,
540 last_ext->extLength, 1);
541 count++;
542 } else {
543 struct kernel_lb_addr tmploc;
544 uint32_t tmplen;
546 udf_write_aext(inode, last_pos, &last_ext->extLocation,
547 last_ext->extLength, 1);
549 * We've rewritten the last extent but there may be empty
550 * indirect extent after it - enter it.
552 udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
555 /* Managed to do everything necessary? */
556 if (!new_block_bytes)
557 goto out;
559 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
560 last_ext->extLocation.logicalBlockNum = 0;
561 last_ext->extLocation.partitionReferenceNum = 0;
562 add = (1 << 30) - sb->s_blocksize;
563 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | add;
565 /* Create enough extents to cover the whole hole */
566 while (new_block_bytes > add) {
567 new_block_bytes -= add;
568 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
569 last_ext->extLength, 1);
570 if (err)
571 return err;
572 count++;
574 if (new_block_bytes) {
575 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
576 new_block_bytes;
577 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
578 last_ext->extLength, 1);
579 if (err)
580 return err;
581 count++;
584 out:
585 /* Do we have some preallocated blocks saved? */
586 if (prealloc_len) {
587 err = udf_add_aext(inode, last_pos, &prealloc_loc,
588 prealloc_len, 1);
589 if (err)
590 return err;
591 last_ext->extLocation = prealloc_loc;
592 last_ext->extLength = prealloc_len;
593 count++;
596 /* last_pos should point to the last written extent... */
597 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
598 last_pos->offset -= sizeof(struct short_ad);
599 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
600 last_pos->offset -= sizeof(struct long_ad);
601 else
602 return -EIO;
604 return count;
607 /* Extend the final block of the file to final_block_len bytes */
608 static void udf_do_extend_final_block(struct inode *inode,
609 struct extent_position *last_pos,
610 struct kernel_long_ad *last_ext,
611 uint32_t final_block_len)
613 struct super_block *sb = inode->i_sb;
614 uint32_t added_bytes;
616 added_bytes = final_block_len -
617 (last_ext->extLength & (sb->s_blocksize - 1));
618 last_ext->extLength += added_bytes;
619 UDF_I(inode)->i_lenExtents += added_bytes;
621 udf_write_aext(inode, last_pos, &last_ext->extLocation,
622 last_ext->extLength, 1);
625 static int udf_extend_file(struct inode *inode, loff_t newsize)
628 struct extent_position epos;
629 struct kernel_lb_addr eloc;
630 uint32_t elen;
631 int8_t etype;
632 struct super_block *sb = inode->i_sb;
633 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
634 unsigned long partial_final_block;
635 int adsize;
636 struct udf_inode_info *iinfo = UDF_I(inode);
637 struct kernel_long_ad extent;
638 int err = 0;
639 int within_final_block;
641 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
642 adsize = sizeof(struct short_ad);
643 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
644 adsize = sizeof(struct long_ad);
645 else
646 BUG();
648 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
649 within_final_block = (etype != -1);
651 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
652 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
653 /* File has no extents at all or has empty last
654 * indirect extent! Create a fake extent... */
655 extent.extLocation.logicalBlockNum = 0;
656 extent.extLocation.partitionReferenceNum = 0;
657 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
658 } else {
659 epos.offset -= adsize;
660 etype = udf_next_aext(inode, &epos, &extent.extLocation,
661 &extent.extLength, 0);
662 extent.extLength |= etype << 30;
665 partial_final_block = newsize & (sb->s_blocksize - 1);
667 /* File has extent covering the new size (could happen when extending
668 * inside a block)?
670 if (within_final_block) {
671 /* Extending file within the last file block */
672 udf_do_extend_final_block(inode, &epos, &extent,
673 partial_final_block);
674 } else {
675 loff_t add = ((loff_t)offset << sb->s_blocksize_bits) |
676 partial_final_block;
677 err = udf_do_extend_file(inode, &epos, &extent, add);
680 if (err < 0)
681 goto out;
682 err = 0;
683 iinfo->i_lenExtents = newsize;
684 out:
685 brelse(epos.bh);
686 return err;
689 static sector_t inode_getblk(struct inode *inode, sector_t block,
690 int *err, int *new)
692 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
693 struct extent_position prev_epos, cur_epos, next_epos;
694 int count = 0, startnum = 0, endnum = 0;
695 uint32_t elen = 0, tmpelen;
696 struct kernel_lb_addr eloc, tmpeloc;
697 int c = 1;
698 loff_t lbcount = 0, b_off = 0;
699 uint32_t newblocknum, newblock;
700 sector_t offset = 0;
701 int8_t etype;
702 struct udf_inode_info *iinfo = UDF_I(inode);
703 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
704 int lastblock = 0;
705 bool isBeyondEOF;
707 *err = 0;
708 *new = 0;
709 prev_epos.offset = udf_file_entry_alloc_offset(inode);
710 prev_epos.block = iinfo->i_location;
711 prev_epos.bh = NULL;
712 cur_epos = next_epos = prev_epos;
713 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
715 /* find the extent which contains the block we are looking for.
716 alternate between laarr[0] and laarr[1] for locations of the
717 current extent, and the previous extent */
718 do {
719 if (prev_epos.bh != cur_epos.bh) {
720 brelse(prev_epos.bh);
721 get_bh(cur_epos.bh);
722 prev_epos.bh = cur_epos.bh;
724 if (cur_epos.bh != next_epos.bh) {
725 brelse(cur_epos.bh);
726 get_bh(next_epos.bh);
727 cur_epos.bh = next_epos.bh;
730 lbcount += elen;
732 prev_epos.block = cur_epos.block;
733 cur_epos.block = next_epos.block;
735 prev_epos.offset = cur_epos.offset;
736 cur_epos.offset = next_epos.offset;
738 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
739 if (etype == -1)
740 break;
742 c = !c;
744 laarr[c].extLength = (etype << 30) | elen;
745 laarr[c].extLocation = eloc;
747 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
748 pgoal = eloc.logicalBlockNum +
749 ((elen + inode->i_sb->s_blocksize - 1) >>
750 inode->i_sb->s_blocksize_bits);
752 count++;
753 } while (lbcount + elen <= b_off);
755 b_off -= lbcount;
756 offset = b_off >> inode->i_sb->s_blocksize_bits;
758 * Move prev_epos and cur_epos into indirect extent if we are at
759 * the pointer to it
761 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
762 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
764 /* if the extent is allocated and recorded, return the block
765 if the extent is not a multiple of the blocksize, round up */
767 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
768 if (elen & (inode->i_sb->s_blocksize - 1)) {
769 elen = EXT_RECORDED_ALLOCATED |
770 ((elen + inode->i_sb->s_blocksize - 1) &
771 ~(inode->i_sb->s_blocksize - 1));
772 udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
774 brelse(prev_epos.bh);
775 brelse(cur_epos.bh);
776 brelse(next_epos.bh);
777 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
778 return newblock;
781 /* Are we beyond EOF? */
782 if (etype == -1) {
783 int ret;
784 loff_t hole_len;
785 isBeyondEOF = true;
786 if (count) {
787 if (c)
788 laarr[0] = laarr[1];
789 startnum = 1;
790 } else {
791 /* Create a fake extent when there's not one */
792 memset(&laarr[0].extLocation, 0x00,
793 sizeof(struct kernel_lb_addr));
794 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
795 /* Will udf_do_extend_file() create real extent from
796 a fake one? */
797 startnum = (offset > 0);
799 /* Create extents for the hole between EOF and offset */
800 hole_len = (loff_t)offset << inode->i_blkbits;
801 ret = udf_do_extend_file(inode, &prev_epos, laarr, hole_len);
802 if (ret < 0) {
803 brelse(prev_epos.bh);
804 brelse(cur_epos.bh);
805 brelse(next_epos.bh);
806 *err = ret;
807 return 0;
809 c = 0;
810 offset = 0;
811 count += ret;
812 /* We are not covered by a preallocated extent? */
813 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
814 EXT_NOT_RECORDED_ALLOCATED) {
815 /* Is there any real extent? - otherwise we overwrite
816 * the fake one... */
817 if (count)
818 c = !c;
819 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
820 inode->i_sb->s_blocksize;
821 memset(&laarr[c].extLocation, 0x00,
822 sizeof(struct kernel_lb_addr));
823 count++;
825 endnum = c + 1;
826 lastblock = 1;
827 } else {
828 isBeyondEOF = false;
829 endnum = startnum = ((count > 2) ? 2 : count);
831 /* if the current extent is in position 0,
832 swap it with the previous */
833 if (!c && count != 1) {
834 laarr[2] = laarr[0];
835 laarr[0] = laarr[1];
836 laarr[1] = laarr[2];
837 c = 1;
840 /* if the current block is located in an extent,
841 read the next extent */
842 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
843 if (etype != -1) {
844 laarr[c + 1].extLength = (etype << 30) | elen;
845 laarr[c + 1].extLocation = eloc;
846 count++;
847 startnum++;
848 endnum++;
849 } else
850 lastblock = 1;
853 /* if the current extent is not recorded but allocated, get the
854 * block in the extent corresponding to the requested block */
855 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
856 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
857 else { /* otherwise, allocate a new block */
858 if (iinfo->i_next_alloc_block == block)
859 goal = iinfo->i_next_alloc_goal;
861 if (!goal) {
862 if (!(goal = pgoal)) /* XXX: what was intended here? */
863 goal = iinfo->i_location.logicalBlockNum + 1;
866 newblocknum = udf_new_block(inode->i_sb, inode,
867 iinfo->i_location.partitionReferenceNum,
868 goal, err);
869 if (!newblocknum) {
870 brelse(prev_epos.bh);
871 brelse(cur_epos.bh);
872 brelse(next_epos.bh);
873 *err = -ENOSPC;
874 return 0;
876 if (isBeyondEOF)
877 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
880 /* if the extent the requsted block is located in contains multiple
881 * blocks, split the extent into at most three extents. blocks prior
882 * to requested block, requested block, and blocks after requested
883 * block */
884 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
886 #ifdef UDF_PREALLOCATE
887 /* We preallocate blocks only for regular files. It also makes sense
888 * for directories but there's a problem when to drop the
889 * preallocation. We might use some delayed work for that but I feel
890 * it's overengineering for a filesystem like UDF. */
891 if (S_ISREG(inode->i_mode))
892 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
893 #endif
895 /* merge any continuous blocks in laarr */
896 udf_merge_extents(inode, laarr, &endnum);
898 /* write back the new extents, inserting new extents if the new number
899 * of extents is greater than the old number, and deleting extents if
900 * the new number of extents is less than the old number */
901 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
903 brelse(prev_epos.bh);
904 brelse(cur_epos.bh);
905 brelse(next_epos.bh);
907 newblock = udf_get_pblock(inode->i_sb, newblocknum,
908 iinfo->i_location.partitionReferenceNum, 0);
909 if (!newblock) {
910 *err = -EIO;
911 return 0;
913 *new = 1;
914 iinfo->i_next_alloc_block = block;
915 iinfo->i_next_alloc_goal = newblocknum;
916 inode->i_ctime = current_time(inode);
918 if (IS_SYNC(inode))
919 udf_sync_inode(inode);
920 else
921 mark_inode_dirty(inode);
923 return newblock;
926 static void udf_split_extents(struct inode *inode, int *c, int offset,
927 int newblocknum,
928 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
929 int *endnum)
931 unsigned long blocksize = inode->i_sb->s_blocksize;
932 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
934 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
935 (laarr[*c].extLength >> 30) ==
936 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
937 int curr = *c;
938 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
939 blocksize - 1) >> blocksize_bits;
940 int8_t etype = (laarr[curr].extLength >> 30);
942 if (blen == 1)
944 else if (!offset || blen == offset + 1) {
945 laarr[curr + 2] = laarr[curr + 1];
946 laarr[curr + 1] = laarr[curr];
947 } else {
948 laarr[curr + 3] = laarr[curr + 1];
949 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
952 if (offset) {
953 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
954 udf_free_blocks(inode->i_sb, inode,
955 &laarr[curr].extLocation,
956 0, offset);
957 laarr[curr].extLength =
958 EXT_NOT_RECORDED_NOT_ALLOCATED |
959 (offset << blocksize_bits);
960 laarr[curr].extLocation.logicalBlockNum = 0;
961 laarr[curr].extLocation.
962 partitionReferenceNum = 0;
963 } else
964 laarr[curr].extLength = (etype << 30) |
965 (offset << blocksize_bits);
966 curr++;
967 (*c)++;
968 (*endnum)++;
971 laarr[curr].extLocation.logicalBlockNum = newblocknum;
972 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
973 laarr[curr].extLocation.partitionReferenceNum =
974 UDF_I(inode)->i_location.partitionReferenceNum;
975 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
976 blocksize;
977 curr++;
979 if (blen != offset + 1) {
980 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
981 laarr[curr].extLocation.logicalBlockNum +=
982 offset + 1;
983 laarr[curr].extLength = (etype << 30) |
984 ((blen - (offset + 1)) << blocksize_bits);
985 curr++;
986 (*endnum)++;
991 static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
992 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
993 int *endnum)
995 int start, length = 0, currlength = 0, i;
997 if (*endnum >= (c + 1)) {
998 if (!lastblock)
999 return;
1000 else
1001 start = c;
1002 } else {
1003 if ((laarr[c + 1].extLength >> 30) ==
1004 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1005 start = c + 1;
1006 length = currlength =
1007 (((laarr[c + 1].extLength &
1008 UDF_EXTENT_LENGTH_MASK) +
1009 inode->i_sb->s_blocksize - 1) >>
1010 inode->i_sb->s_blocksize_bits);
1011 } else
1012 start = c;
1015 for (i = start + 1; i <= *endnum; i++) {
1016 if (i == *endnum) {
1017 if (lastblock)
1018 length += UDF_DEFAULT_PREALLOC_BLOCKS;
1019 } else if ((laarr[i].extLength >> 30) ==
1020 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
1021 length += (((laarr[i].extLength &
1022 UDF_EXTENT_LENGTH_MASK) +
1023 inode->i_sb->s_blocksize - 1) >>
1024 inode->i_sb->s_blocksize_bits);
1025 } else
1026 break;
1029 if (length) {
1030 int next = laarr[start].extLocation.logicalBlockNum +
1031 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
1032 inode->i_sb->s_blocksize - 1) >>
1033 inode->i_sb->s_blocksize_bits);
1034 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
1035 laarr[start].extLocation.partitionReferenceNum,
1036 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
1037 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
1038 currlength);
1039 if (numalloc) {
1040 if (start == (c + 1))
1041 laarr[start].extLength +=
1042 (numalloc <<
1043 inode->i_sb->s_blocksize_bits);
1044 else {
1045 memmove(&laarr[c + 2], &laarr[c + 1],
1046 sizeof(struct long_ad) * (*endnum - (c + 1)));
1047 (*endnum)++;
1048 laarr[c + 1].extLocation.logicalBlockNum = next;
1049 laarr[c + 1].extLocation.partitionReferenceNum =
1050 laarr[c].extLocation.
1051 partitionReferenceNum;
1052 laarr[c + 1].extLength =
1053 EXT_NOT_RECORDED_ALLOCATED |
1054 (numalloc <<
1055 inode->i_sb->s_blocksize_bits);
1056 start = c + 1;
1059 for (i = start + 1; numalloc && i < *endnum; i++) {
1060 int elen = ((laarr[i].extLength &
1061 UDF_EXTENT_LENGTH_MASK) +
1062 inode->i_sb->s_blocksize - 1) >>
1063 inode->i_sb->s_blocksize_bits;
1065 if (elen > numalloc) {
1066 laarr[i].extLength -=
1067 (numalloc <<
1068 inode->i_sb->s_blocksize_bits);
1069 numalloc = 0;
1070 } else {
1071 numalloc -= elen;
1072 if (*endnum > (i + 1))
1073 memmove(&laarr[i],
1074 &laarr[i + 1],
1075 sizeof(struct long_ad) *
1076 (*endnum - (i + 1)));
1077 i--;
1078 (*endnum)--;
1081 UDF_I(inode)->i_lenExtents +=
1082 numalloc << inode->i_sb->s_blocksize_bits;
1087 static void udf_merge_extents(struct inode *inode,
1088 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
1089 int *endnum)
1091 int i;
1092 unsigned long blocksize = inode->i_sb->s_blocksize;
1093 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1095 for (i = 0; i < (*endnum - 1); i++) {
1096 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
1097 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
1099 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
1100 (((li->extLength >> 30) ==
1101 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1102 ((lip1->extLocation.logicalBlockNum -
1103 li->extLocation.logicalBlockNum) ==
1104 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1105 blocksize - 1) >> blocksize_bits)))) {
1107 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1108 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1109 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1110 lip1->extLength = (lip1->extLength -
1111 (li->extLength &
1112 UDF_EXTENT_LENGTH_MASK) +
1113 UDF_EXTENT_LENGTH_MASK) &
1114 ~(blocksize - 1);
1115 li->extLength = (li->extLength &
1116 UDF_EXTENT_FLAG_MASK) +
1117 (UDF_EXTENT_LENGTH_MASK + 1) -
1118 blocksize;
1119 lip1->extLocation.logicalBlockNum =
1120 li->extLocation.logicalBlockNum +
1121 ((li->extLength &
1122 UDF_EXTENT_LENGTH_MASK) >>
1123 blocksize_bits);
1124 } else {
1125 li->extLength = lip1->extLength +
1126 (((li->extLength &
1127 UDF_EXTENT_LENGTH_MASK) +
1128 blocksize - 1) & ~(blocksize - 1));
1129 if (*endnum > (i + 2))
1130 memmove(&laarr[i + 1], &laarr[i + 2],
1131 sizeof(struct long_ad) *
1132 (*endnum - (i + 2)));
1133 i--;
1134 (*endnum)--;
1136 } else if (((li->extLength >> 30) ==
1137 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1138 ((lip1->extLength >> 30) ==
1139 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
1140 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
1141 ((li->extLength &
1142 UDF_EXTENT_LENGTH_MASK) +
1143 blocksize - 1) >> blocksize_bits);
1144 li->extLocation.logicalBlockNum = 0;
1145 li->extLocation.partitionReferenceNum = 0;
1147 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1148 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1149 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1150 lip1->extLength = (lip1->extLength -
1151 (li->extLength &
1152 UDF_EXTENT_LENGTH_MASK) +
1153 UDF_EXTENT_LENGTH_MASK) &
1154 ~(blocksize - 1);
1155 li->extLength = (li->extLength &
1156 UDF_EXTENT_FLAG_MASK) +
1157 (UDF_EXTENT_LENGTH_MASK + 1) -
1158 blocksize;
1159 } else {
1160 li->extLength = lip1->extLength +
1161 (((li->extLength &
1162 UDF_EXTENT_LENGTH_MASK) +
1163 blocksize - 1) & ~(blocksize - 1));
1164 if (*endnum > (i + 2))
1165 memmove(&laarr[i + 1], &laarr[i + 2],
1166 sizeof(struct long_ad) *
1167 (*endnum - (i + 2)));
1168 i--;
1169 (*endnum)--;
1171 } else if ((li->extLength >> 30) ==
1172 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1173 udf_free_blocks(inode->i_sb, inode,
1174 &li->extLocation, 0,
1175 ((li->extLength &
1176 UDF_EXTENT_LENGTH_MASK) +
1177 blocksize - 1) >> blocksize_bits);
1178 li->extLocation.logicalBlockNum = 0;
1179 li->extLocation.partitionReferenceNum = 0;
1180 li->extLength = (li->extLength &
1181 UDF_EXTENT_LENGTH_MASK) |
1182 EXT_NOT_RECORDED_NOT_ALLOCATED;
1187 static void udf_update_extents(struct inode *inode,
1188 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
1189 int startnum, int endnum,
1190 struct extent_position *epos)
1192 int start = 0, i;
1193 struct kernel_lb_addr tmploc;
1194 uint32_t tmplen;
1196 if (startnum > endnum) {
1197 for (i = 0; i < (startnum - endnum); i++)
1198 udf_delete_aext(inode, *epos, laarr[i].extLocation,
1199 laarr[i].extLength);
1200 } else if (startnum < endnum) {
1201 for (i = 0; i < (endnum - startnum); i++) {
1202 udf_insert_aext(inode, *epos, laarr[i].extLocation,
1203 laarr[i].extLength);
1204 udf_next_aext(inode, epos, &laarr[i].extLocation,
1205 &laarr[i].extLength, 1);
1206 start++;
1210 for (i = start; i < endnum; i++) {
1211 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
1212 udf_write_aext(inode, epos, &laarr[i].extLocation,
1213 laarr[i].extLength, 1);
1217 struct buffer_head *udf_bread(struct inode *inode, int block,
1218 int create, int *err)
1220 struct buffer_head *bh = NULL;
1222 bh = udf_getblk(inode, block, create, err);
1223 if (!bh)
1224 return NULL;
1226 if (buffer_uptodate(bh))
1227 return bh;
1229 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
1231 wait_on_buffer(bh);
1232 if (buffer_uptodate(bh))
1233 return bh;
1235 brelse(bh);
1236 *err = -EIO;
1237 return NULL;
1240 int udf_setsize(struct inode *inode, loff_t newsize)
1242 int err;
1243 struct udf_inode_info *iinfo;
1244 int bsize = i_blocksize(inode);
1246 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1247 S_ISLNK(inode->i_mode)))
1248 return -EINVAL;
1249 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1250 return -EPERM;
1252 iinfo = UDF_I(inode);
1253 if (newsize > inode->i_size) {
1254 down_write(&iinfo->i_data_sem);
1255 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1256 if (bsize <
1257 (udf_file_entry_alloc_offset(inode) + newsize)) {
1258 err = udf_expand_file_adinicb(inode);
1259 if (err)
1260 return err;
1261 down_write(&iinfo->i_data_sem);
1262 } else {
1263 iinfo->i_lenAlloc = newsize;
1264 goto set_size;
1267 err = udf_extend_file(inode, newsize);
1268 if (err) {
1269 up_write(&iinfo->i_data_sem);
1270 return err;
1272 set_size:
1273 up_write(&iinfo->i_data_sem);
1274 truncate_setsize(inode, newsize);
1275 } else {
1276 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1277 down_write(&iinfo->i_data_sem);
1278 udf_clear_extent_cache(inode);
1279 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1280 0x00, bsize - newsize -
1281 udf_file_entry_alloc_offset(inode));
1282 iinfo->i_lenAlloc = newsize;
1283 truncate_setsize(inode, newsize);
1284 up_write(&iinfo->i_data_sem);
1285 goto update_time;
1287 err = block_truncate_page(inode->i_mapping, newsize,
1288 udf_get_block);
1289 if (err)
1290 return err;
1291 truncate_setsize(inode, newsize);
1292 down_write(&iinfo->i_data_sem);
1293 udf_clear_extent_cache(inode);
1294 udf_truncate_extents(inode);
1295 up_write(&iinfo->i_data_sem);
1297 update_time:
1298 inode->i_mtime = inode->i_ctime = current_time(inode);
1299 if (IS_SYNC(inode))
1300 udf_sync_inode(inode);
1301 else
1302 mark_inode_dirty(inode);
1303 return 0;
1307 * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1308 * arbitrary - just that we hopefully don't limit any real use of rewritten
1309 * inode on write-once media but avoid looping for too long on corrupted media.
1311 #define UDF_MAX_ICB_NESTING 1024
1313 static int udf_read_inode(struct inode *inode, bool hidden_inode)
1315 struct buffer_head *bh = NULL;
1316 struct fileEntry *fe;
1317 struct extendedFileEntry *efe;
1318 uint16_t ident;
1319 struct udf_inode_info *iinfo = UDF_I(inode);
1320 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1321 struct kernel_lb_addr *iloc = &iinfo->i_location;
1322 unsigned int link_count;
1323 unsigned int indirections = 0;
1324 int bs = inode->i_sb->s_blocksize;
1325 int ret = -EIO;
1327 reread:
1328 if (iloc->logicalBlockNum >=
1329 sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
1330 udf_debug("block=%d, partition=%d out of range\n",
1331 iloc->logicalBlockNum, iloc->partitionReferenceNum);
1332 return -EIO;
1336 * Set defaults, but the inode is still incomplete!
1337 * Note: get_new_inode() sets the following on a new inode:
1338 * i_sb = sb
1339 * i_no = ino
1340 * i_flags = sb->s_flags
1341 * i_state = 0
1342 * clean_inode(): zero fills and sets
1343 * i_count = 1
1344 * i_nlink = 1
1345 * i_op = NULL;
1347 bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
1348 if (!bh) {
1349 udf_err(inode->i_sb, "(ino %ld) failed !bh\n", inode->i_ino);
1350 return -EIO;
1353 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
1354 ident != TAG_IDENT_USE) {
1355 udf_err(inode->i_sb, "(ino %ld) failed ident=%d\n",
1356 inode->i_ino, ident);
1357 goto out;
1360 fe = (struct fileEntry *)bh->b_data;
1361 efe = (struct extendedFileEntry *)bh->b_data;
1363 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
1364 struct buffer_head *ibh;
1366 ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
1367 if (ident == TAG_IDENT_IE && ibh) {
1368 struct kernel_lb_addr loc;
1369 struct indirectEntry *ie;
1371 ie = (struct indirectEntry *)ibh->b_data;
1372 loc = lelb_to_cpu(ie->indirectICB.extLocation);
1374 if (ie->indirectICB.extLength) {
1375 brelse(ibh);
1376 memcpy(&iinfo->i_location, &loc,
1377 sizeof(struct kernel_lb_addr));
1378 if (++indirections > UDF_MAX_ICB_NESTING) {
1379 udf_err(inode->i_sb,
1380 "too many ICBs in ICB hierarchy"
1381 " (max %d supported)\n",
1382 UDF_MAX_ICB_NESTING);
1383 goto out;
1385 brelse(bh);
1386 goto reread;
1389 brelse(ibh);
1390 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
1391 udf_err(inode->i_sb, "unsupported strategy type: %d\n",
1392 le16_to_cpu(fe->icbTag.strategyType));
1393 goto out;
1395 if (fe->icbTag.strategyType == cpu_to_le16(4))
1396 iinfo->i_strat4096 = 0;
1397 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
1398 iinfo->i_strat4096 = 1;
1400 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
1401 ICBTAG_FLAG_AD_MASK;
1402 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_SHORT &&
1403 iinfo->i_alloc_type != ICBTAG_FLAG_AD_LONG &&
1404 iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1405 ret = -EIO;
1406 goto out;
1408 iinfo->i_unique = 0;
1409 iinfo->i_lenEAttr = 0;
1410 iinfo->i_lenExtents = 0;
1411 iinfo->i_lenAlloc = 0;
1412 iinfo->i_next_alloc_block = 0;
1413 iinfo->i_next_alloc_goal = 0;
1414 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
1415 iinfo->i_efe = 1;
1416 iinfo->i_use = 0;
1417 ret = udf_alloc_i_data(inode, bs -
1418 sizeof(struct extendedFileEntry));
1419 if (ret)
1420 goto out;
1421 memcpy(iinfo->i_ext.i_data,
1422 bh->b_data + sizeof(struct extendedFileEntry),
1423 bs - sizeof(struct extendedFileEntry));
1424 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
1425 iinfo->i_efe = 0;
1426 iinfo->i_use = 0;
1427 ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
1428 if (ret)
1429 goto out;
1430 memcpy(iinfo->i_ext.i_data,
1431 bh->b_data + sizeof(struct fileEntry),
1432 bs - sizeof(struct fileEntry));
1433 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
1434 iinfo->i_efe = 0;
1435 iinfo->i_use = 1;
1436 iinfo->i_lenAlloc = le32_to_cpu(
1437 ((struct unallocSpaceEntry *)bh->b_data)->
1438 lengthAllocDescs);
1439 ret = udf_alloc_i_data(inode, bs -
1440 sizeof(struct unallocSpaceEntry));
1441 if (ret)
1442 goto out;
1443 memcpy(iinfo->i_ext.i_data,
1444 bh->b_data + sizeof(struct unallocSpaceEntry),
1445 bs - sizeof(struct unallocSpaceEntry));
1446 return 0;
1449 ret = -EIO;
1450 read_lock(&sbi->s_cred_lock);
1451 i_uid_write(inode, le32_to_cpu(fe->uid));
1452 if (!uid_valid(inode->i_uid) ||
1453 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1454 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
1455 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
1457 i_gid_write(inode, le32_to_cpu(fe->gid));
1458 if (!gid_valid(inode->i_gid) ||
1459 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1460 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
1461 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
1463 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
1464 sbi->s_fmode != UDF_INVALID_MODE)
1465 inode->i_mode = sbi->s_fmode;
1466 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
1467 sbi->s_dmode != UDF_INVALID_MODE)
1468 inode->i_mode = sbi->s_dmode;
1469 else
1470 inode->i_mode = udf_convert_permissions(fe);
1471 inode->i_mode &= ~sbi->s_umask;
1472 read_unlock(&sbi->s_cred_lock);
1474 link_count = le16_to_cpu(fe->fileLinkCount);
1475 if (!link_count) {
1476 if (!hidden_inode) {
1477 ret = -ESTALE;
1478 goto out;
1480 link_count = 1;
1482 set_nlink(inode, link_count);
1484 inode->i_size = le64_to_cpu(fe->informationLength);
1485 iinfo->i_lenExtents = inode->i_size;
1487 if (iinfo->i_efe == 0) {
1488 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
1489 (inode->i_sb->s_blocksize_bits - 9);
1491 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
1492 inode->i_atime = sbi->s_record_time;
1494 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1495 fe->modificationTime))
1496 inode->i_mtime = sbi->s_record_time;
1498 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
1499 inode->i_ctime = sbi->s_record_time;
1501 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1502 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1503 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1504 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
1505 } else {
1506 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
1507 (inode->i_sb->s_blocksize_bits - 9);
1509 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
1510 inode->i_atime = sbi->s_record_time;
1512 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1513 efe->modificationTime))
1514 inode->i_mtime = sbi->s_record_time;
1516 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
1517 iinfo->i_crtime = sbi->s_record_time;
1519 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
1520 inode->i_ctime = sbi->s_record_time;
1522 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1523 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1524 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
1525 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
1527 inode->i_generation = iinfo->i_unique;
1530 * Sanity check length of allocation descriptors and extended attrs to
1531 * avoid integer overflows
1533 if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
1534 goto out;
1535 /* Now do exact checks */
1536 if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
1537 goto out;
1538 /* Sanity checks for files in ICB so that we don't get confused later */
1539 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1541 * For file in ICB data is stored in allocation descriptor
1542 * so sizes should match
1544 if (iinfo->i_lenAlloc != inode->i_size)
1545 goto out;
1546 /* File in ICB has to fit in there... */
1547 if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
1548 goto out;
1551 switch (fe->icbTag.fileType) {
1552 case ICBTAG_FILE_TYPE_DIRECTORY:
1553 inode->i_op = &udf_dir_inode_operations;
1554 inode->i_fop = &udf_dir_operations;
1555 inode->i_mode |= S_IFDIR;
1556 inc_nlink(inode);
1557 break;
1558 case ICBTAG_FILE_TYPE_REALTIME:
1559 case ICBTAG_FILE_TYPE_REGULAR:
1560 case ICBTAG_FILE_TYPE_UNDEF:
1561 case ICBTAG_FILE_TYPE_VAT20:
1562 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1563 inode->i_data.a_ops = &udf_adinicb_aops;
1564 else
1565 inode->i_data.a_ops = &udf_aops;
1566 inode->i_op = &udf_file_inode_operations;
1567 inode->i_fop = &udf_file_operations;
1568 inode->i_mode |= S_IFREG;
1569 break;
1570 case ICBTAG_FILE_TYPE_BLOCK:
1571 inode->i_mode |= S_IFBLK;
1572 break;
1573 case ICBTAG_FILE_TYPE_CHAR:
1574 inode->i_mode |= S_IFCHR;
1575 break;
1576 case ICBTAG_FILE_TYPE_FIFO:
1577 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1578 break;
1579 case ICBTAG_FILE_TYPE_SOCKET:
1580 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1581 break;
1582 case ICBTAG_FILE_TYPE_SYMLINK:
1583 inode->i_data.a_ops = &udf_symlink_aops;
1584 inode->i_op = &page_symlink_inode_operations;
1585 inode_nohighmem(inode);
1586 inode->i_mode = S_IFLNK | S_IRWXUGO;
1587 break;
1588 case ICBTAG_FILE_TYPE_MAIN:
1589 udf_debug("METADATA FILE-----\n");
1590 break;
1591 case ICBTAG_FILE_TYPE_MIRROR:
1592 udf_debug("METADATA MIRROR FILE-----\n");
1593 break;
1594 case ICBTAG_FILE_TYPE_BITMAP:
1595 udf_debug("METADATA BITMAP FILE-----\n");
1596 break;
1597 default:
1598 udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n",
1599 inode->i_ino, fe->icbTag.fileType);
1600 goto out;
1602 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1603 struct deviceSpec *dsea =
1604 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1605 if (dsea) {
1606 init_special_inode(inode, inode->i_mode,
1607 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1608 le32_to_cpu(dsea->minorDeviceIdent)));
1609 /* Developer ID ??? */
1610 } else
1611 goto out;
1613 ret = 0;
1614 out:
1615 brelse(bh);
1616 return ret;
1619 static int udf_alloc_i_data(struct inode *inode, size_t size)
1621 struct udf_inode_info *iinfo = UDF_I(inode);
1622 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
1624 if (!iinfo->i_ext.i_data) {
1625 udf_err(inode->i_sb, "(ino %ld) no free memory\n",
1626 inode->i_ino);
1627 return -ENOMEM;
1630 return 0;
1633 static umode_t udf_convert_permissions(struct fileEntry *fe)
1635 umode_t mode;
1636 uint32_t permissions;
1637 uint32_t flags;
1639 permissions = le32_to_cpu(fe->permissions);
1640 flags = le16_to_cpu(fe->icbTag.flags);
1642 mode = ((permissions) & S_IRWXO) |
1643 ((permissions >> 2) & S_IRWXG) |
1644 ((permissions >> 4) & S_IRWXU) |
1645 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1646 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1647 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
1649 return mode;
1652 int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
1654 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1657 static int udf_sync_inode(struct inode *inode)
1659 return udf_update_inode(inode, 1);
1662 static int udf_update_inode(struct inode *inode, int do_sync)
1664 struct buffer_head *bh = NULL;
1665 struct fileEntry *fe;
1666 struct extendedFileEntry *efe;
1667 uint64_t lb_recorded;
1668 uint32_t udfperms;
1669 uint16_t icbflags;
1670 uint16_t crclen;
1671 int err = 0;
1672 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1673 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1674 struct udf_inode_info *iinfo = UDF_I(inode);
1676 bh = udf_tgetblk(inode->i_sb,
1677 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
1678 if (!bh) {
1679 udf_debug("getblk failure\n");
1680 return -EIO;
1683 lock_buffer(bh);
1684 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1685 fe = (struct fileEntry *)bh->b_data;
1686 efe = (struct extendedFileEntry *)bh->b_data;
1688 if (iinfo->i_use) {
1689 struct unallocSpaceEntry *use =
1690 (struct unallocSpaceEntry *)bh->b_data;
1692 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1693 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1694 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
1695 sizeof(struct unallocSpaceEntry));
1696 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1697 crclen = sizeof(struct unallocSpaceEntry);
1699 goto finish;
1702 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1703 fe->uid = cpu_to_le32(-1);
1704 else
1705 fe->uid = cpu_to_le32(i_uid_read(inode));
1707 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1708 fe->gid = cpu_to_le32(-1);
1709 else
1710 fe->gid = cpu_to_le32(i_gid_read(inode));
1712 udfperms = ((inode->i_mode & S_IRWXO)) |
1713 ((inode->i_mode & S_IRWXG) << 2) |
1714 ((inode->i_mode & S_IRWXU) << 4);
1716 udfperms |= (le32_to_cpu(fe->permissions) &
1717 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1718 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1719 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
1720 fe->permissions = cpu_to_le32(udfperms);
1722 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
1723 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1724 else
1725 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1727 fe->informationLength = cpu_to_le64(inode->i_size);
1729 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1730 struct regid *eid;
1731 struct deviceSpec *dsea =
1732 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1733 if (!dsea) {
1734 dsea = (struct deviceSpec *)
1735 udf_add_extendedattr(inode,
1736 sizeof(struct deviceSpec) +
1737 sizeof(struct regid), 12, 0x3);
1738 dsea->attrType = cpu_to_le32(12);
1739 dsea->attrSubtype = 1;
1740 dsea->attrLength = cpu_to_le32(
1741 sizeof(struct deviceSpec) +
1742 sizeof(struct regid));
1743 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
1745 eid = (struct regid *)dsea->impUse;
1746 memset(eid, 0, sizeof(struct regid));
1747 strcpy(eid->ident, UDF_ID_DEVELOPER);
1748 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1749 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1750 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1751 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1754 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1755 lb_recorded = 0; /* No extents => no blocks! */
1756 else
1757 lb_recorded =
1758 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1759 (blocksize_bits - 9);
1761 if (iinfo->i_efe == 0) {
1762 memcpy(bh->b_data + sizeof(struct fileEntry),
1763 iinfo->i_ext.i_data,
1764 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1765 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1767 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1768 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1769 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
1770 memset(&(fe->impIdent), 0, sizeof(struct regid));
1771 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1772 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1773 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1774 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1775 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1776 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1777 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1778 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1779 crclen = sizeof(struct fileEntry);
1780 } else {
1781 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1782 iinfo->i_ext.i_data,
1783 inode->i_sb->s_blocksize -
1784 sizeof(struct extendedFileEntry));
1785 efe->objectSize = cpu_to_le64(inode->i_size);
1786 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1788 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1789 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1790 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1791 iinfo->i_crtime = inode->i_atime;
1793 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1794 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1795 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1796 iinfo->i_crtime = inode->i_mtime;
1798 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1799 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1800 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1801 iinfo->i_crtime = inode->i_ctime;
1803 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1804 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1805 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1806 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
1808 memset(&(efe->impIdent), 0, sizeof(struct regid));
1809 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1810 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1811 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1812 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1813 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1814 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1815 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1816 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1817 crclen = sizeof(struct extendedFileEntry);
1820 finish:
1821 if (iinfo->i_strat4096) {
1822 fe->icbTag.strategyType = cpu_to_le16(4096);
1823 fe->icbTag.strategyParameter = cpu_to_le16(1);
1824 fe->icbTag.numEntries = cpu_to_le16(2);
1825 } else {
1826 fe->icbTag.strategyType = cpu_to_le16(4);
1827 fe->icbTag.numEntries = cpu_to_le16(1);
1830 if (iinfo->i_use)
1831 fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
1832 else if (S_ISDIR(inode->i_mode))
1833 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1834 else if (S_ISREG(inode->i_mode))
1835 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1836 else if (S_ISLNK(inode->i_mode))
1837 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1838 else if (S_ISBLK(inode->i_mode))
1839 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1840 else if (S_ISCHR(inode->i_mode))
1841 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1842 else if (S_ISFIFO(inode->i_mode))
1843 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1844 else if (S_ISSOCK(inode->i_mode))
1845 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1847 icbflags = iinfo->i_alloc_type |
1848 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1849 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1850 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1851 (le16_to_cpu(fe->icbTag.flags) &
1852 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1853 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
1855 fe->icbTag.flags = cpu_to_le16(icbflags);
1856 if (sbi->s_udfrev >= 0x0200)
1857 fe->descTag.descVersion = cpu_to_le16(3);
1858 else
1859 fe->descTag.descVersion = cpu_to_le16(2);
1860 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
1861 fe->descTag.tagLocation = cpu_to_le32(
1862 iinfo->i_location.logicalBlockNum);
1863 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
1864 fe->descTag.descCRCLength = cpu_to_le16(crclen);
1865 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
1866 crclen));
1867 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
1869 set_buffer_uptodate(bh);
1870 unlock_buffer(bh);
1872 /* write the data blocks */
1873 mark_buffer_dirty(bh);
1874 if (do_sync) {
1875 sync_dirty_buffer(bh);
1876 if (buffer_write_io_error(bh)) {
1877 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1878 inode->i_ino);
1879 err = -EIO;
1882 brelse(bh);
1884 return err;
1887 struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
1888 bool hidden_inode)
1890 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1891 struct inode *inode = iget_locked(sb, block);
1892 int err;
1894 if (!inode)
1895 return ERR_PTR(-ENOMEM);
1897 if (!(inode->i_state & I_NEW))
1898 return inode;
1900 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
1901 err = udf_read_inode(inode, hidden_inode);
1902 if (err < 0) {
1903 iget_failed(inode);
1904 return ERR_PTR(err);
1906 unlock_new_inode(inode);
1908 return inode;
1911 int udf_setup_indirect_aext(struct inode *inode, int block,
1912 struct extent_position *epos)
1914 struct super_block *sb = inode->i_sb;
1915 struct buffer_head *bh;
1916 struct allocExtDesc *aed;
1917 struct extent_position nepos;
1918 struct kernel_lb_addr neloc;
1919 int ver, adsize;
1921 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1922 adsize = sizeof(struct short_ad);
1923 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1924 adsize = sizeof(struct long_ad);
1925 else
1926 return -EIO;
1928 neloc.logicalBlockNum = block;
1929 neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
1931 bh = udf_tgetblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
1932 if (!bh)
1933 return -EIO;
1934 lock_buffer(bh);
1935 memset(bh->b_data, 0x00, sb->s_blocksize);
1936 set_buffer_uptodate(bh);
1937 unlock_buffer(bh);
1938 mark_buffer_dirty_inode(bh, inode);
1940 aed = (struct allocExtDesc *)(bh->b_data);
1941 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
1942 aed->previousAllocExtLocation =
1943 cpu_to_le32(epos->block.logicalBlockNum);
1945 aed->lengthAllocDescs = cpu_to_le32(0);
1946 if (UDF_SB(sb)->s_udfrev >= 0x0200)
1947 ver = 3;
1948 else
1949 ver = 2;
1950 udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
1951 sizeof(struct tag));
1953 nepos.block = neloc;
1954 nepos.offset = sizeof(struct allocExtDesc);
1955 nepos.bh = bh;
1958 * Do we have to copy current last extent to make space for indirect
1959 * one?
1961 if (epos->offset + adsize > sb->s_blocksize) {
1962 struct kernel_lb_addr cp_loc;
1963 uint32_t cp_len;
1964 int cp_type;
1966 epos->offset -= adsize;
1967 cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
1968 cp_len |= ((uint32_t)cp_type) << 30;
1970 __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
1971 udf_write_aext(inode, epos, &nepos.block,
1972 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1973 } else {
1974 __udf_add_aext(inode, epos, &nepos.block,
1975 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1978 brelse(epos->bh);
1979 *epos = nepos;
1981 return 0;
1985 * Append extent at the given position - should be the first free one in inode
1986 * / indirect extent. This function assumes there is enough space in the inode
1987 * or indirect extent. Use udf_add_aext() if you didn't check for this before.
1989 int __udf_add_aext(struct inode *inode, struct extent_position *epos,
1990 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1992 struct udf_inode_info *iinfo = UDF_I(inode);
1993 struct allocExtDesc *aed;
1994 int adsize;
1996 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1997 adsize = sizeof(struct short_ad);
1998 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1999 adsize = sizeof(struct long_ad);
2000 else
2001 return -EIO;
2003 if (!epos->bh) {
2004 WARN_ON(iinfo->i_lenAlloc !=
2005 epos->offset - udf_file_entry_alloc_offset(inode));
2006 } else {
2007 aed = (struct allocExtDesc *)epos->bh->b_data;
2008 WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
2009 epos->offset - sizeof(struct allocExtDesc));
2010 WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
2013 udf_write_aext(inode, epos, eloc, elen, inc);
2015 if (!epos->bh) {
2016 iinfo->i_lenAlloc += adsize;
2017 mark_inode_dirty(inode);
2018 } else {
2019 aed = (struct allocExtDesc *)epos->bh->b_data;
2020 le32_add_cpu(&aed->lengthAllocDescs, adsize);
2021 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2022 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2023 udf_update_tag(epos->bh->b_data,
2024 epos->offset + (inc ? 0 : adsize));
2025 else
2026 udf_update_tag(epos->bh->b_data,
2027 sizeof(struct allocExtDesc));
2028 mark_buffer_dirty_inode(epos->bh, inode);
2031 return 0;
2035 * Append extent at given position - should be the first free one in inode
2036 * / indirect extent. Takes care of allocating and linking indirect blocks.
2038 int udf_add_aext(struct inode *inode, struct extent_position *epos,
2039 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2041 int adsize;
2042 struct super_block *sb = inode->i_sb;
2044 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2045 adsize = sizeof(struct short_ad);
2046 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2047 adsize = sizeof(struct long_ad);
2048 else
2049 return -EIO;
2051 if (epos->offset + (2 * adsize) > sb->s_blocksize) {
2052 int err;
2053 int new_block;
2055 new_block = udf_new_block(sb, NULL,
2056 epos->block.partitionReferenceNum,
2057 epos->block.logicalBlockNum, &err);
2058 if (!new_block)
2059 return -ENOSPC;
2061 err = udf_setup_indirect_aext(inode, new_block, epos);
2062 if (err)
2063 return err;
2066 return __udf_add_aext(inode, epos, eloc, elen, inc);
2069 void udf_write_aext(struct inode *inode, struct extent_position *epos,
2070 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2072 int adsize;
2073 uint8_t *ptr;
2074 struct short_ad *sad;
2075 struct long_ad *lad;
2076 struct udf_inode_info *iinfo = UDF_I(inode);
2078 if (!epos->bh)
2079 ptr = iinfo->i_ext.i_data + epos->offset -
2080 udf_file_entry_alloc_offset(inode) +
2081 iinfo->i_lenEAttr;
2082 else
2083 ptr = epos->bh->b_data + epos->offset;
2085 switch (iinfo->i_alloc_type) {
2086 case ICBTAG_FLAG_AD_SHORT:
2087 sad = (struct short_ad *)ptr;
2088 sad->extLength = cpu_to_le32(elen);
2089 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
2090 adsize = sizeof(struct short_ad);
2091 break;
2092 case ICBTAG_FLAG_AD_LONG:
2093 lad = (struct long_ad *)ptr;
2094 lad->extLength = cpu_to_le32(elen);
2095 lad->extLocation = cpu_to_lelb(*eloc);
2096 memset(lad->impUse, 0x00, sizeof(lad->impUse));
2097 adsize = sizeof(struct long_ad);
2098 break;
2099 default:
2100 return;
2103 if (epos->bh) {
2104 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2105 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
2106 struct allocExtDesc *aed =
2107 (struct allocExtDesc *)epos->bh->b_data;
2108 udf_update_tag(epos->bh->b_data,
2109 le32_to_cpu(aed->lengthAllocDescs) +
2110 sizeof(struct allocExtDesc));
2112 mark_buffer_dirty_inode(epos->bh, inode);
2113 } else {
2114 mark_inode_dirty(inode);
2117 if (inc)
2118 epos->offset += adsize;
2122 * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2123 * someone does some weird stuff.
2125 #define UDF_MAX_INDIR_EXTS 16
2127 int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
2128 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2130 int8_t etype;
2131 unsigned int indirections = 0;
2133 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
2134 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
2135 int block;
2137 if (++indirections > UDF_MAX_INDIR_EXTS) {
2138 udf_err(inode->i_sb,
2139 "too many indirect extents in inode %lu\n",
2140 inode->i_ino);
2141 return -1;
2144 epos->block = *eloc;
2145 epos->offset = sizeof(struct allocExtDesc);
2146 brelse(epos->bh);
2147 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
2148 epos->bh = udf_tread(inode->i_sb, block);
2149 if (!epos->bh) {
2150 udf_debug("reading block %d failed!\n", block);
2151 return -1;
2155 return etype;
2158 int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
2159 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2161 int alen;
2162 int8_t etype;
2163 uint8_t *ptr;
2164 struct short_ad *sad;
2165 struct long_ad *lad;
2166 struct udf_inode_info *iinfo = UDF_I(inode);
2168 if (!epos->bh) {
2169 if (!epos->offset)
2170 epos->offset = udf_file_entry_alloc_offset(inode);
2171 ptr = iinfo->i_ext.i_data + epos->offset -
2172 udf_file_entry_alloc_offset(inode) +
2173 iinfo->i_lenEAttr;
2174 alen = udf_file_entry_alloc_offset(inode) +
2175 iinfo->i_lenAlloc;
2176 } else {
2177 if (!epos->offset)
2178 epos->offset = sizeof(struct allocExtDesc);
2179 ptr = epos->bh->b_data + epos->offset;
2180 alen = sizeof(struct allocExtDesc) +
2181 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2182 lengthAllocDescs);
2185 switch (iinfo->i_alloc_type) {
2186 case ICBTAG_FLAG_AD_SHORT:
2187 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2188 if (!sad)
2189 return -1;
2190 etype = le32_to_cpu(sad->extLength) >> 30;
2191 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
2192 eloc->partitionReferenceNum =
2193 iinfo->i_location.partitionReferenceNum;
2194 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2195 break;
2196 case ICBTAG_FLAG_AD_LONG:
2197 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2198 if (!lad)
2199 return -1;
2200 etype = le32_to_cpu(lad->extLength) >> 30;
2201 *eloc = lelb_to_cpu(lad->extLocation);
2202 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2203 break;
2204 default:
2205 udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
2206 return -1;
2209 return etype;
2212 static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
2213 struct kernel_lb_addr neloc, uint32_t nelen)
2215 struct kernel_lb_addr oeloc;
2216 uint32_t oelen;
2217 int8_t etype;
2219 if (epos.bh)
2220 get_bh(epos.bh);
2222 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
2223 udf_write_aext(inode, &epos, &neloc, nelen, 1);
2224 neloc = oeloc;
2225 nelen = (etype << 30) | oelen;
2227 udf_add_aext(inode, &epos, &neloc, nelen, 1);
2228 brelse(epos.bh);
2230 return (nelen >> 30);
2233 int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
2234 struct kernel_lb_addr eloc, uint32_t elen)
2236 struct extent_position oepos;
2237 int adsize;
2238 int8_t etype;
2239 struct allocExtDesc *aed;
2240 struct udf_inode_info *iinfo;
2242 if (epos.bh) {
2243 get_bh(epos.bh);
2244 get_bh(epos.bh);
2247 iinfo = UDF_I(inode);
2248 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2249 adsize = sizeof(struct short_ad);
2250 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2251 adsize = sizeof(struct long_ad);
2252 else
2253 adsize = 0;
2255 oepos = epos;
2256 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
2257 return -1;
2259 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
2260 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
2261 if (oepos.bh != epos.bh) {
2262 oepos.block = epos.block;
2263 brelse(oepos.bh);
2264 get_bh(epos.bh);
2265 oepos.bh = epos.bh;
2266 oepos.offset = epos.offset - adsize;
2269 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
2270 elen = 0;
2272 if (epos.bh != oepos.bh) {
2273 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2274 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2275 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2276 if (!oepos.bh) {
2277 iinfo->i_lenAlloc -= (adsize * 2);
2278 mark_inode_dirty(inode);
2279 } else {
2280 aed = (struct allocExtDesc *)oepos.bh->b_data;
2281 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
2282 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2283 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2284 udf_update_tag(oepos.bh->b_data,
2285 oepos.offset - (2 * adsize));
2286 else
2287 udf_update_tag(oepos.bh->b_data,
2288 sizeof(struct allocExtDesc));
2289 mark_buffer_dirty_inode(oepos.bh, inode);
2291 } else {
2292 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2293 if (!oepos.bh) {
2294 iinfo->i_lenAlloc -= adsize;
2295 mark_inode_dirty(inode);
2296 } else {
2297 aed = (struct allocExtDesc *)oepos.bh->b_data;
2298 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
2299 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2300 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2301 udf_update_tag(oepos.bh->b_data,
2302 epos.offset - adsize);
2303 else
2304 udf_update_tag(oepos.bh->b_data,
2305 sizeof(struct allocExtDesc));
2306 mark_buffer_dirty_inode(oepos.bh, inode);
2310 brelse(epos.bh);
2311 brelse(oepos.bh);
2313 return (elen >> 30);
2316 int8_t inode_bmap(struct inode *inode, sector_t block,
2317 struct extent_position *pos, struct kernel_lb_addr *eloc,
2318 uint32_t *elen, sector_t *offset)
2320 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2321 loff_t lbcount = 0, bcount =
2322 (loff_t) block << blocksize_bits;
2323 int8_t etype;
2324 struct udf_inode_info *iinfo;
2326 iinfo = UDF_I(inode);
2327 if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2328 pos->offset = 0;
2329 pos->block = iinfo->i_location;
2330 pos->bh = NULL;
2332 *elen = 0;
2333 do {
2334 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2335 if (etype == -1) {
2336 *offset = (bcount - lbcount) >> blocksize_bits;
2337 iinfo->i_lenExtents = lbcount;
2338 return -1;
2340 lbcount += *elen;
2341 } while (lbcount <= bcount);
2342 /* update extent cache */
2343 udf_update_extent_cache(inode, lbcount - *elen, pos, 1);
2344 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
2346 return etype;
2349 long udf_block_map(struct inode *inode, sector_t block)
2351 struct kernel_lb_addr eloc;
2352 uint32_t elen;
2353 sector_t offset;
2354 struct extent_position epos = {};
2355 int ret;
2357 down_read(&UDF_I(inode)->i_data_sem);
2359 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2360 (EXT_RECORDED_ALLOCATED >> 30))
2361 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
2362 else
2363 ret = 0;
2365 up_read(&UDF_I(inode)->i_data_sem);
2366 brelse(epos.bh);
2368 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2369 return udf_fixed_to_variable(ret);
2370 else
2371 return ret;