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/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> 41 #include <linux/bio.h> 42 43 #include "udf_i.h" 44 #include "udf_sb.h" 45 46 #define EXTENT_MERGE_SIZE 5 47 48 #define FE_MAPPED_PERMS (FE_PERM_U_READ | FE_PERM_U_WRITE | FE_PERM_U_EXEC | \ 49 FE_PERM_G_READ | FE_PERM_G_WRITE | FE_PERM_G_EXEC | \ 50 FE_PERM_O_READ | FE_PERM_O_WRITE | FE_PERM_O_EXEC) 51 52 #define FE_DELETE_PERMS (FE_PERM_U_DELETE | FE_PERM_G_DELETE | \ 53 FE_PERM_O_DELETE) 54 55 struct udf_map_rq; 56 57 static umode_t udf_convert_permissions(struct fileEntry *); 58 static int udf_update_inode(struct inode *, int); 59 static int udf_sync_inode(struct inode *inode); 60 static int udf_alloc_i_data(struct inode *inode, size_t size); 61 static int inode_getblk(struct inode *inode, struct udf_map_rq *map); 62 static int udf_insert_aext(struct inode *, struct extent_position, 63 struct kernel_lb_addr, uint32_t); 64 static void udf_split_extents(struct inode *, int *, int, udf_pblk_t, 65 struct kernel_long_ad *, int *); 66 static void udf_prealloc_extents(struct inode *, int, int, 67 struct kernel_long_ad *, int *); 68 static void udf_merge_extents(struct inode *, struct kernel_long_ad *, int *); 69 static int udf_update_extents(struct inode *, struct kernel_long_ad *, int, 70 int, struct extent_position *); 71 static int udf_get_block_wb(struct inode *inode, sector_t block, 72 struct buffer_head *bh_result, int create); 73 74 static void __udf_clear_extent_cache(struct inode *inode) 75 { 76 struct udf_inode_info *iinfo = UDF_I(inode); 77 78 if (iinfo->cached_extent.lstart != -1) { 79 brelse(iinfo->cached_extent.epos.bh); 80 iinfo->cached_extent.lstart = -1; 81 } 82 } 83 84 /* Invalidate extent cache */ 85 static void udf_clear_extent_cache(struct inode *inode) 86 { 87 struct udf_inode_info *iinfo = UDF_I(inode); 88 89 spin_lock(&iinfo->i_extent_cache_lock); 90 __udf_clear_extent_cache(inode); 91 spin_unlock(&iinfo->i_extent_cache_lock); 92 } 93 94 /* Return contents of extent cache */ 95 static int udf_read_extent_cache(struct inode *inode, loff_t bcount, 96 loff_t *lbcount, struct extent_position *pos) 97 { 98 struct udf_inode_info *iinfo = UDF_I(inode); 99 int ret = 0; 100 101 spin_lock(&iinfo->i_extent_cache_lock); 102 if ((iinfo->cached_extent.lstart <= bcount) && 103 (iinfo->cached_extent.lstart != -1)) { 104 /* Cache hit */ 105 *lbcount = iinfo->cached_extent.lstart; 106 memcpy(pos, &iinfo->cached_extent.epos, 107 sizeof(struct extent_position)); 108 if (pos->bh) 109 get_bh(pos->bh); 110 ret = 1; 111 } 112 spin_unlock(&iinfo->i_extent_cache_lock); 113 return ret; 114 } 115 116 /* Add extent to extent cache */ 117 static void udf_update_extent_cache(struct inode *inode, loff_t estart, 118 struct extent_position *pos) 119 { 120 struct udf_inode_info *iinfo = UDF_I(inode); 121 122 spin_lock(&iinfo->i_extent_cache_lock); 123 /* Invalidate previously cached extent */ 124 __udf_clear_extent_cache(inode); 125 if (pos->bh) 126 get_bh(pos->bh); 127 memcpy(&iinfo->cached_extent.epos, pos, sizeof(*pos)); 128 iinfo->cached_extent.lstart = estart; 129 switch (iinfo->i_alloc_type) { 130 case ICBTAG_FLAG_AD_SHORT: 131 iinfo->cached_extent.epos.offset -= sizeof(struct short_ad); 132 break; 133 case ICBTAG_FLAG_AD_LONG: 134 iinfo->cached_extent.epos.offset -= sizeof(struct long_ad); 135 break; 136 } 137 spin_unlock(&iinfo->i_extent_cache_lock); 138 } 139 140 void udf_evict_inode(struct inode *inode) 141 { 142 struct udf_inode_info *iinfo = UDF_I(inode); 143 int want_delete = 0; 144 145 if (!is_bad_inode(inode)) { 146 if (!inode->i_nlink) { 147 want_delete = 1; 148 udf_setsize(inode, 0); 149 udf_update_inode(inode, IS_SYNC(inode)); 150 } 151 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB && 152 inode->i_size != iinfo->i_lenExtents) { 153 udf_warn(inode->i_sb, 154 "Inode %lu (mode %o) has inode size %llu different from extent length %llu. Filesystem need not be standards compliant.\n", 155 inode->i_ino, inode->i_mode, 156 (unsigned long long)inode->i_size, 157 (unsigned long long)iinfo->i_lenExtents); 158 } 159 } 160 truncate_inode_pages_final(&inode->i_data); 161 invalidate_inode_buffers(inode); 162 clear_inode(inode); 163 kfree(iinfo->i_data); 164 iinfo->i_data = NULL; 165 udf_clear_extent_cache(inode); 166 if (want_delete) { 167 udf_free_inode(inode); 168 } 169 } 170 171 static void udf_write_failed(struct address_space *mapping, loff_t to) 172 { 173 struct inode *inode = mapping->host; 174 struct udf_inode_info *iinfo = UDF_I(inode); 175 loff_t isize = inode->i_size; 176 177 if (to > isize) { 178 truncate_pagecache(inode, isize); 179 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) { 180 down_write(&iinfo->i_data_sem); 181 udf_clear_extent_cache(inode); 182 udf_truncate_extents(inode); 183 up_write(&iinfo->i_data_sem); 184 } 185 } 186 } 187 188 static int udf_adinicb_writepage(struct page *page, 189 struct writeback_control *wbc, void *data) 190 { 191 struct inode *inode = page->mapping->host; 192 char *kaddr; 193 struct udf_inode_info *iinfo = UDF_I(inode); 194 195 BUG_ON(!PageLocked(page)); 196 197 kaddr = kmap_atomic(page); 198 memcpy(iinfo->i_data + iinfo->i_lenEAttr, kaddr, i_size_read(inode)); 199 SetPageUptodate(page); 200 kunmap_atomic(kaddr); 201 unlock_page(page); 202 mark_inode_dirty(inode); 203 204 return 0; 205 } 206 207 int udf_writepages(struct address_space *mapping, struct writeback_control *wbc) 208 { 209 struct inode *inode = mapping->host; 210 struct udf_inode_info *iinfo = UDF_I(inode); 211 212 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) 213 return mpage_writepages(mapping, wbc, udf_get_block_wb); 214 return write_cache_pages(mapping, wbc, udf_adinicb_writepage, NULL); 215 } 216 217 int udf_read_folio(struct file *file, struct folio *folio) 218 { 219 struct udf_inode_info *iinfo = UDF_I(file_inode(file)); 220 221 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) { 222 udf_adinicb_readpage(&folio->page); 223 folio_unlock(folio); 224 return 0; 225 } 226 return mpage_read_folio(folio, udf_get_block); 227 } 228 229 static void udf_readahead(struct readahead_control *rac) 230 { 231 mpage_readahead(rac, udf_get_block); 232 } 233 234 int udf_write_begin(struct file *file, struct address_space *mapping, 235 loff_t pos, unsigned len, 236 struct page **pagep, void **fsdata) 237 { 238 struct udf_inode_info *iinfo = UDF_I(file_inode(file)); 239 struct page *page; 240 int ret; 241 242 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) { 243 ret = block_write_begin(mapping, pos, len, pagep, 244 udf_get_block); 245 if (unlikely(ret)) 246 udf_write_failed(mapping, pos + len); 247 return ret; 248 } 249 if (WARN_ON_ONCE(pos >= PAGE_SIZE)) 250 return -EIO; 251 page = grab_cache_page_write_begin(mapping, 0); 252 if (!page) 253 return -ENOMEM; 254 *pagep = page; 255 if (!PageUptodate(page)) 256 udf_adinicb_readpage(page); 257 return 0; 258 } 259 260 ssize_t udf_direct_IO(struct kiocb *iocb, struct iov_iter *iter) 261 { 262 struct file *file = iocb->ki_filp; 263 struct address_space *mapping = file->f_mapping; 264 struct inode *inode = mapping->host; 265 size_t count = iov_iter_count(iter); 266 ssize_t ret; 267 268 /* Fallback to buffered IO for in-ICB files */ 269 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) 270 return 0; 271 ret = blockdev_direct_IO(iocb, inode, iter, udf_get_block); 272 if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE)) 273 udf_write_failed(mapping, iocb->ki_pos + count); 274 return ret; 275 } 276 277 static sector_t udf_bmap(struct address_space *mapping, sector_t block) 278 { 279 return generic_block_bmap(mapping, block, udf_get_block); 280 } 281 282 const struct address_space_operations udf_aops = { 283 .dirty_folio = block_dirty_folio, 284 .invalidate_folio = block_invalidate_folio, 285 .read_folio = udf_read_folio, 286 .readahead = udf_readahead, 287 .writepages = udf_writepages, 288 .write_begin = udf_write_begin, 289 .write_end = generic_write_end, 290 .direct_IO = udf_direct_IO, 291 .bmap = udf_bmap, 292 .migrate_folio = buffer_migrate_folio, 293 }; 294 295 /* 296 * Expand file stored in ICB to a normal one-block-file 297 * 298 * This function requires i_mutex held 299 */ 300 int udf_expand_file_adinicb(struct inode *inode) 301 { 302 struct page *page; 303 char *kaddr; 304 struct udf_inode_info *iinfo = UDF_I(inode); 305 int err; 306 307 WARN_ON_ONCE(!inode_is_locked(inode)); 308 if (!iinfo->i_lenAlloc) { 309 down_write(&iinfo->i_data_sem); 310 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD)) 311 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT; 312 else 313 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG; 314 /* from now on we have normal address_space methods */ 315 inode->i_data.a_ops = &udf_aops; 316 up_write(&iinfo->i_data_sem); 317 mark_inode_dirty(inode); 318 return 0; 319 } 320 321 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS); 322 if (!page) 323 return -ENOMEM; 324 325 if (!PageUptodate(page)) { 326 kaddr = kmap_atomic(page); 327 memset(kaddr + iinfo->i_lenAlloc, 0x00, 328 PAGE_SIZE - iinfo->i_lenAlloc); 329 memcpy(kaddr, iinfo->i_data + iinfo->i_lenEAttr, 330 iinfo->i_lenAlloc); 331 flush_dcache_page(page); 332 SetPageUptodate(page); 333 kunmap_atomic(kaddr); 334 } 335 down_write(&iinfo->i_data_sem); 336 memset(iinfo->i_data + iinfo->i_lenEAttr, 0x00, 337 iinfo->i_lenAlloc); 338 iinfo->i_lenAlloc = 0; 339 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD)) 340 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT; 341 else 342 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG; 343 /* from now on we have normal address_space methods */ 344 inode->i_data.a_ops = &udf_aops; 345 set_page_dirty(page); 346 unlock_page(page); 347 up_write(&iinfo->i_data_sem); 348 err = filemap_fdatawrite(inode->i_mapping); 349 if (err) { 350 /* Restore everything back so that we don't lose data... */ 351 lock_page(page); 352 down_write(&iinfo->i_data_sem); 353 kaddr = kmap_atomic(page); 354 memcpy(iinfo->i_data + iinfo->i_lenEAttr, kaddr, inode->i_size); 355 kunmap_atomic(kaddr); 356 unlock_page(page); 357 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB; 358 inode->i_data.a_ops = &udf_adinicb_aops; 359 iinfo->i_lenAlloc = inode->i_size; 360 up_write(&iinfo->i_data_sem); 361 } 362 put_page(page); 363 mark_inode_dirty(inode); 364 365 return err; 366 } 367 368 #define UDF_MAP_CREATE 0x01 /* Mapping can allocate new blocks */ 369 #define UDF_MAP_NOPREALLOC 0x02 /* Do not preallocate blocks */ 370 371 #define UDF_BLK_MAPPED 0x01 /* Block was successfully mapped */ 372 #define UDF_BLK_NEW 0x02 /* Block was freshly allocated */ 373 374 struct udf_map_rq { 375 sector_t lblk; 376 udf_pblk_t pblk; 377 int iflags; /* UDF_MAP_ flags determining behavior */ 378 int oflags; /* UDF_BLK_ flags reporting results */ 379 }; 380 381 static int udf_map_block(struct inode *inode, struct udf_map_rq *map) 382 { 383 int err; 384 struct udf_inode_info *iinfo = UDF_I(inode); 385 386 map->oflags = 0; 387 if (!(map->iflags & UDF_MAP_CREATE)) { 388 struct kernel_lb_addr eloc; 389 uint32_t elen; 390 sector_t offset; 391 struct extent_position epos = {}; 392 393 down_read(&iinfo->i_data_sem); 394 if (inode_bmap(inode, map->lblk, &epos, &eloc, &elen, &offset) 395 == (EXT_RECORDED_ALLOCATED >> 30)) { 396 map->pblk = udf_get_lb_pblock(inode->i_sb, &eloc, 397 offset); 398 map->oflags |= UDF_BLK_MAPPED; 399 } 400 up_read(&iinfo->i_data_sem); 401 brelse(epos.bh); 402 403 return 0; 404 } 405 406 down_write(&iinfo->i_data_sem); 407 /* 408 * Block beyond EOF and prealloc extents? Just discard preallocation 409 * as it is not useful and complicates things. 410 */ 411 if (((loff_t)map->lblk) << inode->i_blkbits >= iinfo->i_lenExtents) 412 udf_discard_prealloc(inode); 413 udf_clear_extent_cache(inode); 414 err = inode_getblk(inode, map); 415 up_write(&iinfo->i_data_sem); 416 return err; 417 } 418 419 static int __udf_get_block(struct inode *inode, sector_t block, 420 struct buffer_head *bh_result, int flags) 421 { 422 int err; 423 struct udf_map_rq map = { 424 .lblk = block, 425 .iflags = flags, 426 }; 427 428 err = udf_map_block(inode, &map); 429 if (err < 0) 430 return err; 431 if (map.oflags & UDF_BLK_MAPPED) { 432 map_bh(bh_result, inode->i_sb, map.pblk); 433 if (map.oflags & UDF_BLK_NEW) 434 set_buffer_new(bh_result); 435 } 436 return 0; 437 } 438 439 int udf_get_block(struct inode *inode, sector_t block, 440 struct buffer_head *bh_result, int create) 441 { 442 int flags = create ? UDF_MAP_CREATE : 0; 443 444 /* 445 * We preallocate blocks only for regular files. It also makes sense 446 * for directories but there's a problem when to drop the 447 * preallocation. We might use some delayed work for that but I feel 448 * it's overengineering for a filesystem like UDF. 449 */ 450 if (!S_ISREG(inode->i_mode)) 451 flags |= UDF_MAP_NOPREALLOC; 452 return __udf_get_block(inode, block, bh_result, flags); 453 } 454 455 /* 456 * We shouldn't be allocating blocks on page writeback since we allocate them 457 * on page fault. We can spot dirty buffers without allocated blocks though 458 * when truncate expands file. These however don't have valid data so we can 459 * safely ignore them. So never allocate blocks from page writeback. 460 */ 461 static int udf_get_block_wb(struct inode *inode, sector_t block, 462 struct buffer_head *bh_result, int create) 463 { 464 return __udf_get_block(inode, block, bh_result, 0); 465 } 466 467 /* Extend the file with new blocks totaling 'new_block_bytes', 468 * return the number of extents added 469 */ 470 static int udf_do_extend_file(struct inode *inode, 471 struct extent_position *last_pos, 472 struct kernel_long_ad *last_ext, 473 loff_t new_block_bytes) 474 { 475 uint32_t add; 476 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK); 477 struct super_block *sb = inode->i_sb; 478 struct udf_inode_info *iinfo; 479 int err; 480 481 /* The previous extent is fake and we should not extend by anything 482 * - there's nothing to do... */ 483 if (!new_block_bytes && fake) 484 return 0; 485 486 iinfo = UDF_I(inode); 487 /* Round the last extent up to a multiple of block size */ 488 if (last_ext->extLength & (sb->s_blocksize - 1)) { 489 last_ext->extLength = 490 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) | 491 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) + 492 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1)); 493 iinfo->i_lenExtents = 494 (iinfo->i_lenExtents + sb->s_blocksize - 1) & 495 ~(sb->s_blocksize - 1); 496 } 497 498 add = 0; 499 /* Can we merge with the previous extent? */ 500 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) == 501 EXT_NOT_RECORDED_NOT_ALLOCATED) { 502 add = (1 << 30) - sb->s_blocksize - 503 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK); 504 if (add > new_block_bytes) 505 add = new_block_bytes; 506 new_block_bytes -= add; 507 last_ext->extLength += add; 508 } 509 510 if (fake) { 511 err = udf_add_aext(inode, last_pos, &last_ext->extLocation, 512 last_ext->extLength, 1); 513 if (err < 0) 514 goto out_err; 515 count++; 516 } else { 517 struct kernel_lb_addr tmploc; 518 uint32_t tmplen; 519 520 udf_write_aext(inode, last_pos, &last_ext->extLocation, 521 last_ext->extLength, 1); 522 523 /* 524 * We've rewritten the last extent. If we are going to add 525 * more extents, we may need to enter possible following 526 * empty indirect extent. 527 */ 528 if (new_block_bytes) 529 udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0); 530 } 531 iinfo->i_lenExtents += add; 532 533 /* Managed to do everything necessary? */ 534 if (!new_block_bytes) 535 goto out; 536 537 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */ 538 last_ext->extLocation.logicalBlockNum = 0; 539 last_ext->extLocation.partitionReferenceNum = 0; 540 add = (1 << 30) - sb->s_blocksize; 541 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | add; 542 543 /* Create enough extents to cover the whole hole */ 544 while (new_block_bytes > add) { 545 new_block_bytes -= add; 546 err = udf_add_aext(inode, last_pos, &last_ext->extLocation, 547 last_ext->extLength, 1); 548 if (err) 549 goto out_err; 550 iinfo->i_lenExtents += add; 551 count++; 552 } 553 if (new_block_bytes) { 554 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | 555 new_block_bytes; 556 err = udf_add_aext(inode, last_pos, &last_ext->extLocation, 557 last_ext->extLength, 1); 558 if (err) 559 goto out_err; 560 iinfo->i_lenExtents += new_block_bytes; 561 count++; 562 } 563 564 out: 565 /* last_pos should point to the last written extent... */ 566 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) 567 last_pos->offset -= sizeof(struct short_ad); 568 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) 569 last_pos->offset -= sizeof(struct long_ad); 570 else 571 return -EIO; 572 573 return count; 574 out_err: 575 /* Remove extents we've created so far */ 576 udf_clear_extent_cache(inode); 577 udf_truncate_extents(inode); 578 return err; 579 } 580 581 /* Extend the final block of the file to final_block_len bytes */ 582 static void udf_do_extend_final_block(struct inode *inode, 583 struct extent_position *last_pos, 584 struct kernel_long_ad *last_ext, 585 uint32_t new_elen) 586 { 587 uint32_t added_bytes; 588 589 /* 590 * Extent already large enough? It may be already rounded up to block 591 * size... 592 */ 593 if (new_elen <= (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) 594 return; 595 added_bytes = new_elen - (last_ext->extLength & UDF_EXTENT_LENGTH_MASK); 596 last_ext->extLength += added_bytes; 597 UDF_I(inode)->i_lenExtents += added_bytes; 598 599 udf_write_aext(inode, last_pos, &last_ext->extLocation, 600 last_ext->extLength, 1); 601 } 602 603 static int udf_extend_file(struct inode *inode, loff_t newsize) 604 { 605 606 struct extent_position epos; 607 struct kernel_lb_addr eloc; 608 uint32_t elen; 609 int8_t etype; 610 struct super_block *sb = inode->i_sb; 611 sector_t first_block = newsize >> sb->s_blocksize_bits, offset; 612 loff_t new_elen; 613 int adsize; 614 struct udf_inode_info *iinfo = UDF_I(inode); 615 struct kernel_long_ad extent; 616 int err = 0; 617 bool within_last_ext; 618 619 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) 620 adsize = sizeof(struct short_ad); 621 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) 622 adsize = sizeof(struct long_ad); 623 else 624 BUG(); 625 626 down_write(&iinfo->i_data_sem); 627 /* 628 * When creating hole in file, just don't bother with preserving 629 * preallocation. It likely won't be very useful anyway. 630 */ 631 udf_discard_prealloc(inode); 632 633 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset); 634 within_last_ext = (etype != -1); 635 /* We don't expect extents past EOF... */ 636 WARN_ON_ONCE(within_last_ext && 637 elen > ((loff_t)offset + 1) << inode->i_blkbits); 638 639 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) || 640 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) { 641 /* File has no extents at all or has empty last 642 * indirect extent! Create a fake extent... */ 643 extent.extLocation.logicalBlockNum = 0; 644 extent.extLocation.partitionReferenceNum = 0; 645 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED; 646 } else { 647 epos.offset -= adsize; 648 etype = udf_next_aext(inode, &epos, &extent.extLocation, 649 &extent.extLength, 0); 650 extent.extLength |= etype << 30; 651 } 652 653 new_elen = ((loff_t)offset << inode->i_blkbits) | 654 (newsize & (sb->s_blocksize - 1)); 655 656 /* File has extent covering the new size (could happen when extending 657 * inside a block)? 658 */ 659 if (within_last_ext) { 660 /* Extending file within the last file block */ 661 udf_do_extend_final_block(inode, &epos, &extent, new_elen); 662 } else { 663 err = udf_do_extend_file(inode, &epos, &extent, new_elen); 664 } 665 666 if (err < 0) 667 goto out; 668 err = 0; 669 out: 670 brelse(epos.bh); 671 up_write(&iinfo->i_data_sem); 672 return err; 673 } 674 675 static int inode_getblk(struct inode *inode, struct udf_map_rq *map) 676 { 677 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE]; 678 struct extent_position prev_epos, cur_epos, next_epos; 679 int count = 0, startnum = 0, endnum = 0; 680 uint32_t elen = 0, tmpelen; 681 struct kernel_lb_addr eloc, tmpeloc; 682 int c = 1; 683 loff_t lbcount = 0, b_off = 0; 684 udf_pblk_t newblocknum; 685 sector_t offset = 0; 686 int8_t etype; 687 struct udf_inode_info *iinfo = UDF_I(inode); 688 udf_pblk_t goal = 0, pgoal = iinfo->i_location.logicalBlockNum; 689 int lastblock = 0; 690 bool isBeyondEOF; 691 int ret = 0; 692 693 prev_epos.offset = udf_file_entry_alloc_offset(inode); 694 prev_epos.block = iinfo->i_location; 695 prev_epos.bh = NULL; 696 cur_epos = next_epos = prev_epos; 697 b_off = (loff_t)map->lblk << inode->i_sb->s_blocksize_bits; 698 699 /* find the extent which contains the block we are looking for. 700 alternate between laarr[0] and laarr[1] for locations of the 701 current extent, and the previous extent */ 702 do { 703 if (prev_epos.bh != cur_epos.bh) { 704 brelse(prev_epos.bh); 705 get_bh(cur_epos.bh); 706 prev_epos.bh = cur_epos.bh; 707 } 708 if (cur_epos.bh != next_epos.bh) { 709 brelse(cur_epos.bh); 710 get_bh(next_epos.bh); 711 cur_epos.bh = next_epos.bh; 712 } 713 714 lbcount += elen; 715 716 prev_epos.block = cur_epos.block; 717 cur_epos.block = next_epos.block; 718 719 prev_epos.offset = cur_epos.offset; 720 cur_epos.offset = next_epos.offset; 721 722 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1); 723 if (etype == -1) 724 break; 725 726 c = !c; 727 728 laarr[c].extLength = (etype << 30) | elen; 729 laarr[c].extLocation = eloc; 730 731 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) 732 pgoal = eloc.logicalBlockNum + 733 ((elen + inode->i_sb->s_blocksize - 1) >> 734 inode->i_sb->s_blocksize_bits); 735 736 count++; 737 } while (lbcount + elen <= b_off); 738 739 b_off -= lbcount; 740 offset = b_off >> inode->i_sb->s_blocksize_bits; 741 /* 742 * Move prev_epos and cur_epos into indirect extent if we are at 743 * the pointer to it 744 */ 745 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0); 746 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0); 747 748 /* if the extent is allocated and recorded, return the block 749 if the extent is not a multiple of the blocksize, round up */ 750 751 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) { 752 if (elen & (inode->i_sb->s_blocksize - 1)) { 753 elen = EXT_RECORDED_ALLOCATED | 754 ((elen + inode->i_sb->s_blocksize - 1) & 755 ~(inode->i_sb->s_blocksize - 1)); 756 iinfo->i_lenExtents = 757 ALIGN(iinfo->i_lenExtents, 758 inode->i_sb->s_blocksize); 759 udf_write_aext(inode, &cur_epos, &eloc, elen, 1); 760 } 761 map->oflags = UDF_BLK_MAPPED; 762 map->pblk = udf_get_lb_pblock(inode->i_sb, &eloc, offset); 763 goto out_free; 764 } 765 766 /* Are we beyond EOF and preallocated extent? */ 767 if (etype == -1) { 768 loff_t hole_len; 769 770 isBeyondEOF = true; 771 if (count) { 772 if (c) 773 laarr[0] = laarr[1]; 774 startnum = 1; 775 } else { 776 /* Create a fake extent when there's not one */ 777 memset(&laarr[0].extLocation, 0x00, 778 sizeof(struct kernel_lb_addr)); 779 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED; 780 /* Will udf_do_extend_file() create real extent from 781 a fake one? */ 782 startnum = (offset > 0); 783 } 784 /* Create extents for the hole between EOF and offset */ 785 hole_len = (loff_t)offset << inode->i_blkbits; 786 ret = udf_do_extend_file(inode, &prev_epos, laarr, hole_len); 787 if (ret < 0) 788 goto out_free; 789 c = 0; 790 offset = 0; 791 count += ret; 792 /* 793 * Is there any real extent? - otherwise we overwrite the fake 794 * one... 795 */ 796 if (count) 797 c = !c; 798 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | 799 inode->i_sb->s_blocksize; 800 memset(&laarr[c].extLocation, 0x00, 801 sizeof(struct kernel_lb_addr)); 802 count++; 803 endnum = c + 1; 804 lastblock = 1; 805 } else { 806 isBeyondEOF = false; 807 endnum = startnum = ((count > 2) ? 2 : count); 808 809 /* if the current extent is in position 0, 810 swap it with the previous */ 811 if (!c && count != 1) { 812 laarr[2] = laarr[0]; 813 laarr[0] = laarr[1]; 814 laarr[1] = laarr[2]; 815 c = 1; 816 } 817 818 /* if the current block is located in an extent, 819 read the next extent */ 820 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0); 821 if (etype != -1) { 822 laarr[c + 1].extLength = (etype << 30) | elen; 823 laarr[c + 1].extLocation = eloc; 824 count++; 825 startnum++; 826 endnum++; 827 } else 828 lastblock = 1; 829 } 830 831 /* if the current extent is not recorded but allocated, get the 832 * block in the extent corresponding to the requested block */ 833 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30)) 834 newblocknum = laarr[c].extLocation.logicalBlockNum + offset; 835 else { /* otherwise, allocate a new block */ 836 if (iinfo->i_next_alloc_block == map->lblk) 837 goal = iinfo->i_next_alloc_goal; 838 839 if (!goal) { 840 if (!(goal = pgoal)) /* XXX: what was intended here? */ 841 goal = iinfo->i_location.logicalBlockNum + 1; 842 } 843 844 newblocknum = udf_new_block(inode->i_sb, inode, 845 iinfo->i_location.partitionReferenceNum, 846 goal, &ret); 847 if (!newblocknum) 848 goto out_free; 849 if (isBeyondEOF) 850 iinfo->i_lenExtents += inode->i_sb->s_blocksize; 851 } 852 853 /* if the extent the requsted block is located in contains multiple 854 * blocks, split the extent into at most three extents. blocks prior 855 * to requested block, requested block, and blocks after requested 856 * block */ 857 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum); 858 859 if (!(map->iflags & UDF_MAP_NOPREALLOC)) 860 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum); 861 862 /* merge any continuous blocks in laarr */ 863 udf_merge_extents(inode, laarr, &endnum); 864 865 /* write back the new extents, inserting new extents if the new number 866 * of extents is greater than the old number, and deleting extents if 867 * the new number of extents is less than the old number */ 868 ret = udf_update_extents(inode, laarr, startnum, endnum, &prev_epos); 869 if (ret < 0) 870 goto out_free; 871 872 map->pblk = udf_get_pblock(inode->i_sb, newblocknum, 873 iinfo->i_location.partitionReferenceNum, 0); 874 if (!map->pblk) { 875 ret = -EFSCORRUPTED; 876 goto out_free; 877 } 878 map->oflags = UDF_BLK_NEW | UDF_BLK_MAPPED; 879 iinfo->i_next_alloc_block = map->lblk + 1; 880 iinfo->i_next_alloc_goal = newblocknum + 1; 881 inode->i_ctime = current_time(inode); 882 883 if (IS_SYNC(inode)) 884 udf_sync_inode(inode); 885 else 886 mark_inode_dirty(inode); 887 ret = 0; 888 out_free: 889 brelse(prev_epos.bh); 890 brelse(cur_epos.bh); 891 brelse(next_epos.bh); 892 return ret; 893 } 894 895 static void udf_split_extents(struct inode *inode, int *c, int offset, 896 udf_pblk_t newblocknum, 897 struct kernel_long_ad *laarr, int *endnum) 898 { 899 unsigned long blocksize = inode->i_sb->s_blocksize; 900 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits; 901 902 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) || 903 (laarr[*c].extLength >> 30) == 904 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) { 905 int curr = *c; 906 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) + 907 blocksize - 1) >> blocksize_bits; 908 int8_t etype = (laarr[curr].extLength >> 30); 909 910 if (blen == 1) 911 ; 912 else if (!offset || blen == offset + 1) { 913 laarr[curr + 2] = laarr[curr + 1]; 914 laarr[curr + 1] = laarr[curr]; 915 } else { 916 laarr[curr + 3] = laarr[curr + 1]; 917 laarr[curr + 2] = laarr[curr + 1] = laarr[curr]; 918 } 919 920 if (offset) { 921 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) { 922 udf_free_blocks(inode->i_sb, inode, 923 &laarr[curr].extLocation, 924 0, offset); 925 laarr[curr].extLength = 926 EXT_NOT_RECORDED_NOT_ALLOCATED | 927 (offset << blocksize_bits); 928 laarr[curr].extLocation.logicalBlockNum = 0; 929 laarr[curr].extLocation. 930 partitionReferenceNum = 0; 931 } else 932 laarr[curr].extLength = (etype << 30) | 933 (offset << blocksize_bits); 934 curr++; 935 (*c)++; 936 (*endnum)++; 937 } 938 939 laarr[curr].extLocation.logicalBlockNum = newblocknum; 940 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) 941 laarr[curr].extLocation.partitionReferenceNum = 942 UDF_I(inode)->i_location.partitionReferenceNum; 943 laarr[curr].extLength = EXT_RECORDED_ALLOCATED | 944 blocksize; 945 curr++; 946 947 if (blen != offset + 1) { 948 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) 949 laarr[curr].extLocation.logicalBlockNum += 950 offset + 1; 951 laarr[curr].extLength = (etype << 30) | 952 ((blen - (offset + 1)) << blocksize_bits); 953 curr++; 954 (*endnum)++; 955 } 956 } 957 } 958 959 static void udf_prealloc_extents(struct inode *inode, int c, int lastblock, 960 struct kernel_long_ad *laarr, 961 int *endnum) 962 { 963 int start, length = 0, currlength = 0, i; 964 965 if (*endnum >= (c + 1)) { 966 if (!lastblock) 967 return; 968 else 969 start = c; 970 } else { 971 if ((laarr[c + 1].extLength >> 30) == 972 (EXT_NOT_RECORDED_ALLOCATED >> 30)) { 973 start = c + 1; 974 length = currlength = 975 (((laarr[c + 1].extLength & 976 UDF_EXTENT_LENGTH_MASK) + 977 inode->i_sb->s_blocksize - 1) >> 978 inode->i_sb->s_blocksize_bits); 979 } else 980 start = c; 981 } 982 983 for (i = start + 1; i <= *endnum; i++) { 984 if (i == *endnum) { 985 if (lastblock) 986 length += UDF_DEFAULT_PREALLOC_BLOCKS; 987 } else if ((laarr[i].extLength >> 30) == 988 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) { 989 length += (((laarr[i].extLength & 990 UDF_EXTENT_LENGTH_MASK) + 991 inode->i_sb->s_blocksize - 1) >> 992 inode->i_sb->s_blocksize_bits); 993 } else 994 break; 995 } 996 997 if (length) { 998 int next = laarr[start].extLocation.logicalBlockNum + 999 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) + 1000 inode->i_sb->s_blocksize - 1) >> 1001 inode->i_sb->s_blocksize_bits); 1002 int numalloc = udf_prealloc_blocks(inode->i_sb, inode, 1003 laarr[start].extLocation.partitionReferenceNum, 1004 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ? 1005 length : UDF_DEFAULT_PREALLOC_BLOCKS) - 1006 currlength); 1007 if (numalloc) { 1008 if (start == (c + 1)) 1009 laarr[start].extLength += 1010 (numalloc << 1011 inode->i_sb->s_blocksize_bits); 1012 else { 1013 memmove(&laarr[c + 2], &laarr[c + 1], 1014 sizeof(struct long_ad) * (*endnum - (c + 1))); 1015 (*endnum)++; 1016 laarr[c + 1].extLocation.logicalBlockNum = next; 1017 laarr[c + 1].extLocation.partitionReferenceNum = 1018 laarr[c].extLocation. 1019 partitionReferenceNum; 1020 laarr[c + 1].extLength = 1021 EXT_NOT_RECORDED_ALLOCATED | 1022 (numalloc << 1023 inode->i_sb->s_blocksize_bits); 1024 start = c + 1; 1025 } 1026 1027 for (i = start + 1; numalloc && i < *endnum; i++) { 1028 int elen = ((laarr[i].extLength & 1029 UDF_EXTENT_LENGTH_MASK) + 1030 inode->i_sb->s_blocksize - 1) >> 1031 inode->i_sb->s_blocksize_bits; 1032 1033 if (elen > numalloc) { 1034 laarr[i].extLength -= 1035 (numalloc << 1036 inode->i_sb->s_blocksize_bits); 1037 numalloc = 0; 1038 } else { 1039 numalloc -= elen; 1040 if (*endnum > (i + 1)) 1041 memmove(&laarr[i], 1042 &laarr[i + 1], 1043 sizeof(struct long_ad) * 1044 (*endnum - (i + 1))); 1045 i--; 1046 (*endnum)--; 1047 } 1048 } 1049 UDF_I(inode)->i_lenExtents += 1050 numalloc << inode->i_sb->s_blocksize_bits; 1051 } 1052 } 1053 } 1054 1055 static void udf_merge_extents(struct inode *inode, struct kernel_long_ad *laarr, 1056 int *endnum) 1057 { 1058 int i; 1059 unsigned long blocksize = inode->i_sb->s_blocksize; 1060 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits; 1061 1062 for (i = 0; i < (*endnum - 1); i++) { 1063 struct kernel_long_ad *li /*l[i]*/ = &laarr[i]; 1064 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1]; 1065 1066 if (((li->extLength >> 30) == (lip1->extLength >> 30)) && 1067 (((li->extLength >> 30) == 1068 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) || 1069 ((lip1->extLocation.logicalBlockNum - 1070 li->extLocation.logicalBlockNum) == 1071 (((li->extLength & UDF_EXTENT_LENGTH_MASK) + 1072 blocksize - 1) >> blocksize_bits)))) { 1073 1074 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) + 1075 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) + 1076 blocksize - 1) <= UDF_EXTENT_LENGTH_MASK) { 1077 li->extLength = lip1->extLength + 1078 (((li->extLength & 1079 UDF_EXTENT_LENGTH_MASK) + 1080 blocksize - 1) & ~(blocksize - 1)); 1081 if (*endnum > (i + 2)) 1082 memmove(&laarr[i + 1], &laarr[i + 2], 1083 sizeof(struct long_ad) * 1084 (*endnum - (i + 2))); 1085 i--; 1086 (*endnum)--; 1087 } 1088 } else if (((li->extLength >> 30) == 1089 (EXT_NOT_RECORDED_ALLOCATED >> 30)) && 1090 ((lip1->extLength >> 30) == 1091 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) { 1092 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0, 1093 ((li->extLength & 1094 UDF_EXTENT_LENGTH_MASK) + 1095 blocksize - 1) >> blocksize_bits); 1096 li->extLocation.logicalBlockNum = 0; 1097 li->extLocation.partitionReferenceNum = 0; 1098 1099 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) + 1100 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) + 1101 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) { 1102 lip1->extLength = (lip1->extLength - 1103 (li->extLength & 1104 UDF_EXTENT_LENGTH_MASK) + 1105 UDF_EXTENT_LENGTH_MASK) & 1106 ~(blocksize - 1); 1107 li->extLength = (li->extLength & 1108 UDF_EXTENT_FLAG_MASK) + 1109 (UDF_EXTENT_LENGTH_MASK + 1) - 1110 blocksize; 1111 } else { 1112 li->extLength = lip1->extLength + 1113 (((li->extLength & 1114 UDF_EXTENT_LENGTH_MASK) + 1115 blocksize - 1) & ~(blocksize - 1)); 1116 if (*endnum > (i + 2)) 1117 memmove(&laarr[i + 1], &laarr[i + 2], 1118 sizeof(struct long_ad) * 1119 (*endnum - (i + 2))); 1120 i--; 1121 (*endnum)--; 1122 } 1123 } else if ((li->extLength >> 30) == 1124 (EXT_NOT_RECORDED_ALLOCATED >> 30)) { 1125 udf_free_blocks(inode->i_sb, inode, 1126 &li->extLocation, 0, 1127 ((li->extLength & 1128 UDF_EXTENT_LENGTH_MASK) + 1129 blocksize - 1) >> blocksize_bits); 1130 li->extLocation.logicalBlockNum = 0; 1131 li->extLocation.partitionReferenceNum = 0; 1132 li->extLength = (li->extLength & 1133 UDF_EXTENT_LENGTH_MASK) | 1134 EXT_NOT_RECORDED_NOT_ALLOCATED; 1135 } 1136 } 1137 } 1138 1139 static int udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr, 1140 int startnum, int endnum, 1141 struct extent_position *epos) 1142 { 1143 int start = 0, i; 1144 struct kernel_lb_addr tmploc; 1145 uint32_t tmplen; 1146 int err; 1147 1148 if (startnum > endnum) { 1149 for (i = 0; i < (startnum - endnum); i++) 1150 udf_delete_aext(inode, *epos); 1151 } else if (startnum < endnum) { 1152 for (i = 0; i < (endnum - startnum); i++) { 1153 err = udf_insert_aext(inode, *epos, 1154 laarr[i].extLocation, 1155 laarr[i].extLength); 1156 /* 1157 * If we fail here, we are likely corrupting the extent 1158 * list and leaking blocks. At least stop early to 1159 * limit the damage. 1160 */ 1161 if (err < 0) 1162 return err; 1163 udf_next_aext(inode, epos, &laarr[i].extLocation, 1164 &laarr[i].extLength, 1); 1165 start++; 1166 } 1167 } 1168 1169 for (i = start; i < endnum; i++) { 1170 udf_next_aext(inode, epos, &tmploc, &tmplen, 0); 1171 udf_write_aext(inode, epos, &laarr[i].extLocation, 1172 laarr[i].extLength, 1); 1173 } 1174 return 0; 1175 } 1176 1177 struct buffer_head *udf_bread(struct inode *inode, udf_pblk_t block, 1178 int create, int *err) 1179 { 1180 struct buffer_head *bh = NULL; 1181 struct udf_map_rq map = { 1182 .lblk = block, 1183 .iflags = UDF_MAP_NOPREALLOC | (create ? UDF_MAP_CREATE : 0), 1184 }; 1185 1186 *err = udf_map_block(inode, &map); 1187 if (*err || !(map.oflags & UDF_BLK_MAPPED)) 1188 return NULL; 1189 1190 bh = sb_getblk(inode->i_sb, map.pblk); 1191 if (!bh) { 1192 *err = -ENOMEM; 1193 return NULL; 1194 } 1195 if (map.oflags & UDF_BLK_NEW) { 1196 lock_buffer(bh); 1197 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize); 1198 set_buffer_uptodate(bh); 1199 unlock_buffer(bh); 1200 mark_buffer_dirty_inode(bh, inode); 1201 return bh; 1202 } 1203 1204 if (bh_read(bh, 0) >= 0) 1205 return bh; 1206 1207 brelse(bh); 1208 *err = -EIO; 1209 return NULL; 1210 } 1211 1212 int udf_setsize(struct inode *inode, loff_t newsize) 1213 { 1214 int err = 0; 1215 struct udf_inode_info *iinfo; 1216 unsigned int bsize = i_blocksize(inode); 1217 1218 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 1219 S_ISLNK(inode->i_mode))) 1220 return -EINVAL; 1221 if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 1222 return -EPERM; 1223 1224 filemap_invalidate_lock(inode->i_mapping); 1225 iinfo = UDF_I(inode); 1226 if (newsize > inode->i_size) { 1227 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) { 1228 if (bsize >= 1229 (udf_file_entry_alloc_offset(inode) + newsize)) { 1230 down_write(&iinfo->i_data_sem); 1231 iinfo->i_lenAlloc = newsize; 1232 up_write(&iinfo->i_data_sem); 1233 goto set_size; 1234 } 1235 err = udf_expand_file_adinicb(inode); 1236 if (err) 1237 goto out_unlock; 1238 } 1239 err = udf_extend_file(inode, newsize); 1240 if (err) 1241 goto out_unlock; 1242 set_size: 1243 truncate_setsize(inode, newsize); 1244 } else { 1245 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) { 1246 down_write(&iinfo->i_data_sem); 1247 udf_clear_extent_cache(inode); 1248 memset(iinfo->i_data + iinfo->i_lenEAttr + newsize, 1249 0x00, bsize - newsize - 1250 udf_file_entry_alloc_offset(inode)); 1251 iinfo->i_lenAlloc = newsize; 1252 truncate_setsize(inode, newsize); 1253 up_write(&iinfo->i_data_sem); 1254 goto update_time; 1255 } 1256 err = block_truncate_page(inode->i_mapping, newsize, 1257 udf_get_block); 1258 if (err) 1259 goto out_unlock; 1260 truncate_setsize(inode, newsize); 1261 down_write(&iinfo->i_data_sem); 1262 udf_clear_extent_cache(inode); 1263 err = udf_truncate_extents(inode); 1264 up_write(&iinfo->i_data_sem); 1265 if (err) 1266 goto out_unlock; 1267 } 1268 update_time: 1269 inode->i_mtime = inode->i_ctime = current_time(inode); 1270 if (IS_SYNC(inode)) 1271 udf_sync_inode(inode); 1272 else 1273 mark_inode_dirty(inode); 1274 out_unlock: 1275 filemap_invalidate_unlock(inode->i_mapping); 1276 return err; 1277 } 1278 1279 /* 1280 * Maximum length of linked list formed by ICB hierarchy. The chosen number is 1281 * arbitrary - just that we hopefully don't limit any real use of rewritten 1282 * inode on write-once media but avoid looping for too long on corrupted media. 1283 */ 1284 #define UDF_MAX_ICB_NESTING 1024 1285 1286 static int udf_read_inode(struct inode *inode, bool hidden_inode) 1287 { 1288 struct buffer_head *bh = NULL; 1289 struct fileEntry *fe; 1290 struct extendedFileEntry *efe; 1291 uint16_t ident; 1292 struct udf_inode_info *iinfo = UDF_I(inode); 1293 struct udf_sb_info *sbi = UDF_SB(inode->i_sb); 1294 struct kernel_lb_addr *iloc = &iinfo->i_location; 1295 unsigned int link_count; 1296 unsigned int indirections = 0; 1297 int bs = inode->i_sb->s_blocksize; 1298 int ret = -EIO; 1299 uint32_t uid, gid; 1300 1301 reread: 1302 if (iloc->partitionReferenceNum >= sbi->s_partitions) { 1303 udf_debug("partition reference: %u > logical volume partitions: %u\n", 1304 iloc->partitionReferenceNum, sbi->s_partitions); 1305 return -EIO; 1306 } 1307 1308 if (iloc->logicalBlockNum >= 1309 sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) { 1310 udf_debug("block=%u, partition=%u out of range\n", 1311 iloc->logicalBlockNum, iloc->partitionReferenceNum); 1312 return -EIO; 1313 } 1314 1315 /* 1316 * Set defaults, but the inode is still incomplete! 1317 * Note: get_new_inode() sets the following on a new inode: 1318 * i_sb = sb 1319 * i_no = ino 1320 * i_flags = sb->s_flags 1321 * i_state = 0 1322 * clean_inode(): zero fills and sets 1323 * i_count = 1 1324 * i_nlink = 1 1325 * i_op = NULL; 1326 */ 1327 bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident); 1328 if (!bh) { 1329 udf_err(inode->i_sb, "(ino %lu) failed !bh\n", inode->i_ino); 1330 return -EIO; 1331 } 1332 1333 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE && 1334 ident != TAG_IDENT_USE) { 1335 udf_err(inode->i_sb, "(ino %lu) failed ident=%u\n", 1336 inode->i_ino, ident); 1337 goto out; 1338 } 1339 1340 fe = (struct fileEntry *)bh->b_data; 1341 efe = (struct extendedFileEntry *)bh->b_data; 1342 1343 if (fe->icbTag.strategyType == cpu_to_le16(4096)) { 1344 struct buffer_head *ibh; 1345 1346 ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident); 1347 if (ident == TAG_IDENT_IE && ibh) { 1348 struct kernel_lb_addr loc; 1349 struct indirectEntry *ie; 1350 1351 ie = (struct indirectEntry *)ibh->b_data; 1352 loc = lelb_to_cpu(ie->indirectICB.extLocation); 1353 1354 if (ie->indirectICB.extLength) { 1355 brelse(ibh); 1356 memcpy(&iinfo->i_location, &loc, 1357 sizeof(struct kernel_lb_addr)); 1358 if (++indirections > UDF_MAX_ICB_NESTING) { 1359 udf_err(inode->i_sb, 1360 "too many ICBs in ICB hierarchy" 1361 " (max %d supported)\n", 1362 UDF_MAX_ICB_NESTING); 1363 goto out; 1364 } 1365 brelse(bh); 1366 goto reread; 1367 } 1368 } 1369 brelse(ibh); 1370 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) { 1371 udf_err(inode->i_sb, "unsupported strategy type: %u\n", 1372 le16_to_cpu(fe->icbTag.strategyType)); 1373 goto out; 1374 } 1375 if (fe->icbTag.strategyType == cpu_to_le16(4)) 1376 iinfo->i_strat4096 = 0; 1377 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */ 1378 iinfo->i_strat4096 = 1; 1379 1380 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) & 1381 ICBTAG_FLAG_AD_MASK; 1382 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_SHORT && 1383 iinfo->i_alloc_type != ICBTAG_FLAG_AD_LONG && 1384 iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) { 1385 ret = -EIO; 1386 goto out; 1387 } 1388 iinfo->i_hidden = hidden_inode; 1389 iinfo->i_unique = 0; 1390 iinfo->i_lenEAttr = 0; 1391 iinfo->i_lenExtents = 0; 1392 iinfo->i_lenAlloc = 0; 1393 iinfo->i_next_alloc_block = 0; 1394 iinfo->i_next_alloc_goal = 0; 1395 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) { 1396 iinfo->i_efe = 1; 1397 iinfo->i_use = 0; 1398 ret = udf_alloc_i_data(inode, bs - 1399 sizeof(struct extendedFileEntry)); 1400 if (ret) 1401 goto out; 1402 memcpy(iinfo->i_data, 1403 bh->b_data + sizeof(struct extendedFileEntry), 1404 bs - sizeof(struct extendedFileEntry)); 1405 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) { 1406 iinfo->i_efe = 0; 1407 iinfo->i_use = 0; 1408 ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry)); 1409 if (ret) 1410 goto out; 1411 memcpy(iinfo->i_data, 1412 bh->b_data + sizeof(struct fileEntry), 1413 bs - sizeof(struct fileEntry)); 1414 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) { 1415 iinfo->i_efe = 0; 1416 iinfo->i_use = 1; 1417 iinfo->i_lenAlloc = le32_to_cpu( 1418 ((struct unallocSpaceEntry *)bh->b_data)-> 1419 lengthAllocDescs); 1420 ret = udf_alloc_i_data(inode, bs - 1421 sizeof(struct unallocSpaceEntry)); 1422 if (ret) 1423 goto out; 1424 memcpy(iinfo->i_data, 1425 bh->b_data + sizeof(struct unallocSpaceEntry), 1426 bs - sizeof(struct unallocSpaceEntry)); 1427 return 0; 1428 } 1429 1430 ret = -EIO; 1431 read_lock(&sbi->s_cred_lock); 1432 uid = le32_to_cpu(fe->uid); 1433 if (uid == UDF_INVALID_ID || 1434 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET)) 1435 inode->i_uid = sbi->s_uid; 1436 else 1437 i_uid_write(inode, uid); 1438 1439 gid = le32_to_cpu(fe->gid); 1440 if (gid == UDF_INVALID_ID || 1441 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET)) 1442 inode->i_gid = sbi->s_gid; 1443 else 1444 i_gid_write(inode, gid); 1445 1446 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY && 1447 sbi->s_fmode != UDF_INVALID_MODE) 1448 inode->i_mode = sbi->s_fmode; 1449 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY && 1450 sbi->s_dmode != UDF_INVALID_MODE) 1451 inode->i_mode = sbi->s_dmode; 1452 else 1453 inode->i_mode = udf_convert_permissions(fe); 1454 inode->i_mode &= ~sbi->s_umask; 1455 iinfo->i_extraPerms = le32_to_cpu(fe->permissions) & ~FE_MAPPED_PERMS; 1456 1457 read_unlock(&sbi->s_cred_lock); 1458 1459 link_count = le16_to_cpu(fe->fileLinkCount); 1460 if (!link_count) { 1461 if (!hidden_inode) { 1462 ret = -ESTALE; 1463 goto out; 1464 } 1465 link_count = 1; 1466 } 1467 set_nlink(inode, link_count); 1468 1469 inode->i_size = le64_to_cpu(fe->informationLength); 1470 iinfo->i_lenExtents = inode->i_size; 1471 1472 if (iinfo->i_efe == 0) { 1473 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) << 1474 (inode->i_sb->s_blocksize_bits - 9); 1475 1476 udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime); 1477 udf_disk_stamp_to_time(&inode->i_mtime, fe->modificationTime); 1478 udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime); 1479 1480 iinfo->i_unique = le64_to_cpu(fe->uniqueID); 1481 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr); 1482 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs); 1483 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint); 1484 iinfo->i_streamdir = 0; 1485 iinfo->i_lenStreams = 0; 1486 } else { 1487 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) << 1488 (inode->i_sb->s_blocksize_bits - 9); 1489 1490 udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime); 1491 udf_disk_stamp_to_time(&inode->i_mtime, efe->modificationTime); 1492 udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime); 1493 udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime); 1494 1495 iinfo->i_unique = le64_to_cpu(efe->uniqueID); 1496 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr); 1497 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs); 1498 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint); 1499 1500 /* Named streams */ 1501 iinfo->i_streamdir = (efe->streamDirectoryICB.extLength != 0); 1502 iinfo->i_locStreamdir = 1503 lelb_to_cpu(efe->streamDirectoryICB.extLocation); 1504 iinfo->i_lenStreams = le64_to_cpu(efe->objectSize); 1505 if (iinfo->i_lenStreams >= inode->i_size) 1506 iinfo->i_lenStreams -= inode->i_size; 1507 else 1508 iinfo->i_lenStreams = 0; 1509 } 1510 inode->i_generation = iinfo->i_unique; 1511 1512 /* 1513 * Sanity check length of allocation descriptors and extended attrs to 1514 * avoid integer overflows 1515 */ 1516 if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs) 1517 goto out; 1518 /* Now do exact checks */ 1519 if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs) 1520 goto out; 1521 /* Sanity checks for files in ICB so that we don't get confused later */ 1522 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) { 1523 /* 1524 * For file in ICB data is stored in allocation descriptor 1525 * so sizes should match 1526 */ 1527 if (iinfo->i_lenAlloc != inode->i_size) 1528 goto out; 1529 /* File in ICB has to fit in there... */ 1530 if (inode->i_size > bs - udf_file_entry_alloc_offset(inode)) 1531 goto out; 1532 } 1533 1534 switch (fe->icbTag.fileType) { 1535 case ICBTAG_FILE_TYPE_DIRECTORY: 1536 inode->i_op = &udf_dir_inode_operations; 1537 inode->i_fop = &udf_dir_operations; 1538 inode->i_mode |= S_IFDIR; 1539 inc_nlink(inode); 1540 break; 1541 case ICBTAG_FILE_TYPE_REALTIME: 1542 case ICBTAG_FILE_TYPE_REGULAR: 1543 case ICBTAG_FILE_TYPE_UNDEF: 1544 case ICBTAG_FILE_TYPE_VAT20: 1545 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) 1546 inode->i_data.a_ops = &udf_adinicb_aops; 1547 else 1548 inode->i_data.a_ops = &udf_aops; 1549 inode->i_op = &udf_file_inode_operations; 1550 inode->i_fop = &udf_file_operations; 1551 inode->i_mode |= S_IFREG; 1552 break; 1553 case ICBTAG_FILE_TYPE_BLOCK: 1554 inode->i_mode |= S_IFBLK; 1555 break; 1556 case ICBTAG_FILE_TYPE_CHAR: 1557 inode->i_mode |= S_IFCHR; 1558 break; 1559 case ICBTAG_FILE_TYPE_FIFO: 1560 init_special_inode(inode, inode->i_mode | S_IFIFO, 0); 1561 break; 1562 case ICBTAG_FILE_TYPE_SOCKET: 1563 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0); 1564 break; 1565 case ICBTAG_FILE_TYPE_SYMLINK: 1566 inode->i_data.a_ops = &udf_symlink_aops; 1567 inode->i_op = &udf_symlink_inode_operations; 1568 inode_nohighmem(inode); 1569 inode->i_mode = S_IFLNK | 0777; 1570 break; 1571 case ICBTAG_FILE_TYPE_MAIN: 1572 udf_debug("METADATA FILE-----\n"); 1573 break; 1574 case ICBTAG_FILE_TYPE_MIRROR: 1575 udf_debug("METADATA MIRROR FILE-----\n"); 1576 break; 1577 case ICBTAG_FILE_TYPE_BITMAP: 1578 udf_debug("METADATA BITMAP FILE-----\n"); 1579 break; 1580 default: 1581 udf_err(inode->i_sb, "(ino %lu) failed unknown file type=%u\n", 1582 inode->i_ino, fe->icbTag.fileType); 1583 goto out; 1584 } 1585 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 1586 struct deviceSpec *dsea = 1587 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1); 1588 if (dsea) { 1589 init_special_inode(inode, inode->i_mode, 1590 MKDEV(le32_to_cpu(dsea->majorDeviceIdent), 1591 le32_to_cpu(dsea->minorDeviceIdent))); 1592 /* Developer ID ??? */ 1593 } else 1594 goto out; 1595 } 1596 ret = 0; 1597 out: 1598 brelse(bh); 1599 return ret; 1600 } 1601 1602 static int udf_alloc_i_data(struct inode *inode, size_t size) 1603 { 1604 struct udf_inode_info *iinfo = UDF_I(inode); 1605 iinfo->i_data = kmalloc(size, GFP_KERNEL); 1606 if (!iinfo->i_data) 1607 return -ENOMEM; 1608 return 0; 1609 } 1610 1611 static umode_t udf_convert_permissions(struct fileEntry *fe) 1612 { 1613 umode_t mode; 1614 uint32_t permissions; 1615 uint32_t flags; 1616 1617 permissions = le32_to_cpu(fe->permissions); 1618 flags = le16_to_cpu(fe->icbTag.flags); 1619 1620 mode = ((permissions) & 0007) | 1621 ((permissions >> 2) & 0070) | 1622 ((permissions >> 4) & 0700) | 1623 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) | 1624 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) | 1625 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0); 1626 1627 return mode; 1628 } 1629 1630 void udf_update_extra_perms(struct inode *inode, umode_t mode) 1631 { 1632 struct udf_inode_info *iinfo = UDF_I(inode); 1633 1634 /* 1635 * UDF 2.01 sec. 3.3.3.3 Note 2: 1636 * In Unix, delete permission tracks write 1637 */ 1638 iinfo->i_extraPerms &= ~FE_DELETE_PERMS; 1639 if (mode & 0200) 1640 iinfo->i_extraPerms |= FE_PERM_U_DELETE; 1641 if (mode & 0020) 1642 iinfo->i_extraPerms |= FE_PERM_G_DELETE; 1643 if (mode & 0002) 1644 iinfo->i_extraPerms |= FE_PERM_O_DELETE; 1645 } 1646 1647 int udf_write_inode(struct inode *inode, struct writeback_control *wbc) 1648 { 1649 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL); 1650 } 1651 1652 static int udf_sync_inode(struct inode *inode) 1653 { 1654 return udf_update_inode(inode, 1); 1655 } 1656 1657 static void udf_adjust_time(struct udf_inode_info *iinfo, struct timespec64 time) 1658 { 1659 if (iinfo->i_crtime.tv_sec > time.tv_sec || 1660 (iinfo->i_crtime.tv_sec == time.tv_sec && 1661 iinfo->i_crtime.tv_nsec > time.tv_nsec)) 1662 iinfo->i_crtime = time; 1663 } 1664 1665 static int udf_update_inode(struct inode *inode, int do_sync) 1666 { 1667 struct buffer_head *bh = NULL; 1668 struct fileEntry *fe; 1669 struct extendedFileEntry *efe; 1670 uint64_t lb_recorded; 1671 uint32_t udfperms; 1672 uint16_t icbflags; 1673 uint16_t crclen; 1674 int err = 0; 1675 struct udf_sb_info *sbi = UDF_SB(inode->i_sb); 1676 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits; 1677 struct udf_inode_info *iinfo = UDF_I(inode); 1678 1679 bh = sb_getblk(inode->i_sb, 1680 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0)); 1681 if (!bh) { 1682 udf_debug("getblk failure\n"); 1683 return -EIO; 1684 } 1685 1686 lock_buffer(bh); 1687 memset(bh->b_data, 0, inode->i_sb->s_blocksize); 1688 fe = (struct fileEntry *)bh->b_data; 1689 efe = (struct extendedFileEntry *)bh->b_data; 1690 1691 if (iinfo->i_use) { 1692 struct unallocSpaceEntry *use = 1693 (struct unallocSpaceEntry *)bh->b_data; 1694 1695 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc); 1696 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry), 1697 iinfo->i_data, inode->i_sb->s_blocksize - 1698 sizeof(struct unallocSpaceEntry)); 1699 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE); 1700 crclen = sizeof(struct unallocSpaceEntry); 1701 1702 goto finish; 1703 } 1704 1705 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET)) 1706 fe->uid = cpu_to_le32(UDF_INVALID_ID); 1707 else 1708 fe->uid = cpu_to_le32(i_uid_read(inode)); 1709 1710 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET)) 1711 fe->gid = cpu_to_le32(UDF_INVALID_ID); 1712 else 1713 fe->gid = cpu_to_le32(i_gid_read(inode)); 1714 1715 udfperms = ((inode->i_mode & 0007)) | 1716 ((inode->i_mode & 0070) << 2) | 1717 ((inode->i_mode & 0700) << 4); 1718 1719 udfperms |= iinfo->i_extraPerms; 1720 fe->permissions = cpu_to_le32(udfperms); 1721 1722 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0) 1723 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1); 1724 else { 1725 if (iinfo->i_hidden) 1726 fe->fileLinkCount = cpu_to_le16(0); 1727 else 1728 fe->fileLinkCount = cpu_to_le16(inode->i_nlink); 1729 } 1730 1731 fe->informationLength = cpu_to_le64(inode->i_size); 1732 1733 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 1734 struct regid *eid; 1735 struct deviceSpec *dsea = 1736 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1); 1737 if (!dsea) { 1738 dsea = (struct deviceSpec *) 1739 udf_add_extendedattr(inode, 1740 sizeof(struct deviceSpec) + 1741 sizeof(struct regid), 12, 0x3); 1742 dsea->attrType = cpu_to_le32(12); 1743 dsea->attrSubtype = 1; 1744 dsea->attrLength = cpu_to_le32( 1745 sizeof(struct deviceSpec) + 1746 sizeof(struct regid)); 1747 dsea->impUseLength = cpu_to_le32(sizeof(struct regid)); 1748 } 1749 eid = (struct regid *)dsea->impUse; 1750 memset(eid, 0, sizeof(*eid)); 1751 strcpy(eid->ident, UDF_ID_DEVELOPER); 1752 eid->identSuffix[0] = UDF_OS_CLASS_UNIX; 1753 eid->identSuffix[1] = UDF_OS_ID_LINUX; 1754 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode)); 1755 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode)); 1756 } 1757 1758 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) 1759 lb_recorded = 0; /* No extents => no blocks! */ 1760 else 1761 lb_recorded = 1762 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >> 1763 (blocksize_bits - 9); 1764 1765 if (iinfo->i_efe == 0) { 1766 memcpy(bh->b_data + sizeof(struct fileEntry), 1767 iinfo->i_data, 1768 inode->i_sb->s_blocksize - sizeof(struct fileEntry)); 1769 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded); 1770 1771 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime); 1772 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime); 1773 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime); 1774 memset(&(fe->impIdent), 0, sizeof(struct regid)); 1775 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER); 1776 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX; 1777 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX; 1778 fe->uniqueID = cpu_to_le64(iinfo->i_unique); 1779 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr); 1780 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc); 1781 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint); 1782 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE); 1783 crclen = sizeof(struct fileEntry); 1784 } else { 1785 memcpy(bh->b_data + sizeof(struct extendedFileEntry), 1786 iinfo->i_data, 1787 inode->i_sb->s_blocksize - 1788 sizeof(struct extendedFileEntry)); 1789 efe->objectSize = 1790 cpu_to_le64(inode->i_size + iinfo->i_lenStreams); 1791 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded); 1792 1793 if (iinfo->i_streamdir) { 1794 struct long_ad *icb_lad = &efe->streamDirectoryICB; 1795 1796 icb_lad->extLocation = 1797 cpu_to_lelb(iinfo->i_locStreamdir); 1798 icb_lad->extLength = 1799 cpu_to_le32(inode->i_sb->s_blocksize); 1800 } 1801 1802 udf_adjust_time(iinfo, inode->i_atime); 1803 udf_adjust_time(iinfo, inode->i_mtime); 1804 udf_adjust_time(iinfo, inode->i_ctime); 1805 1806 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime); 1807 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime); 1808 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime); 1809 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime); 1810 1811 memset(&(efe->impIdent), 0, sizeof(efe->impIdent)); 1812 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER); 1813 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX; 1814 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX; 1815 efe->uniqueID = cpu_to_le64(iinfo->i_unique); 1816 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr); 1817 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc); 1818 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint); 1819 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE); 1820 crclen = sizeof(struct extendedFileEntry); 1821 } 1822 1823 finish: 1824 if (iinfo->i_strat4096) { 1825 fe->icbTag.strategyType = cpu_to_le16(4096); 1826 fe->icbTag.strategyParameter = cpu_to_le16(1); 1827 fe->icbTag.numEntries = cpu_to_le16(2); 1828 } else { 1829 fe->icbTag.strategyType = cpu_to_le16(4); 1830 fe->icbTag.numEntries = cpu_to_le16(1); 1831 } 1832 1833 if (iinfo->i_use) 1834 fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE; 1835 else if (S_ISDIR(inode->i_mode)) 1836 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY; 1837 else if (S_ISREG(inode->i_mode)) 1838 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR; 1839 else if (S_ISLNK(inode->i_mode)) 1840 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK; 1841 else if (S_ISBLK(inode->i_mode)) 1842 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK; 1843 else if (S_ISCHR(inode->i_mode)) 1844 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR; 1845 else if (S_ISFIFO(inode->i_mode)) 1846 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO; 1847 else if (S_ISSOCK(inode->i_mode)) 1848 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET; 1849 1850 icbflags = iinfo->i_alloc_type | 1851 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) | 1852 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) | 1853 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) | 1854 (le16_to_cpu(fe->icbTag.flags) & 1855 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID | 1856 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY)); 1857 1858 fe->icbTag.flags = cpu_to_le16(icbflags); 1859 if (sbi->s_udfrev >= 0x0200) 1860 fe->descTag.descVersion = cpu_to_le16(3); 1861 else 1862 fe->descTag.descVersion = cpu_to_le16(2); 1863 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number); 1864 fe->descTag.tagLocation = cpu_to_le32( 1865 iinfo->i_location.logicalBlockNum); 1866 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag); 1867 fe->descTag.descCRCLength = cpu_to_le16(crclen); 1868 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag), 1869 crclen)); 1870 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag); 1871 1872 set_buffer_uptodate(bh); 1873 unlock_buffer(bh); 1874 1875 /* write the data blocks */ 1876 mark_buffer_dirty(bh); 1877 if (do_sync) { 1878 sync_dirty_buffer(bh); 1879 if (buffer_write_io_error(bh)) { 1880 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n", 1881 inode->i_ino); 1882 err = -EIO; 1883 } 1884 } 1885 brelse(bh); 1886 1887 return err; 1888 } 1889 1890 struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino, 1891 bool hidden_inode) 1892 { 1893 unsigned long block = udf_get_lb_pblock(sb, ino, 0); 1894 struct inode *inode = iget_locked(sb, block); 1895 int err; 1896 1897 if (!inode) 1898 return ERR_PTR(-ENOMEM); 1899 1900 if (!(inode->i_state & I_NEW)) { 1901 if (UDF_I(inode)->i_hidden != hidden_inode) { 1902 iput(inode); 1903 return ERR_PTR(-EFSCORRUPTED); 1904 } 1905 return inode; 1906 } 1907 1908 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr)); 1909 err = udf_read_inode(inode, hidden_inode); 1910 if (err < 0) { 1911 iget_failed(inode); 1912 return ERR_PTR(err); 1913 } 1914 unlock_new_inode(inode); 1915 1916 return inode; 1917 } 1918 1919 int udf_setup_indirect_aext(struct inode *inode, udf_pblk_t block, 1920 struct extent_position *epos) 1921 { 1922 struct super_block *sb = inode->i_sb; 1923 struct buffer_head *bh; 1924 struct allocExtDesc *aed; 1925 struct extent_position nepos; 1926 struct kernel_lb_addr neloc; 1927 int ver, adsize; 1928 1929 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT) 1930 adsize = sizeof(struct short_ad); 1931 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG) 1932 adsize = sizeof(struct long_ad); 1933 else 1934 return -EIO; 1935 1936 neloc.logicalBlockNum = block; 1937 neloc.partitionReferenceNum = epos->block.partitionReferenceNum; 1938 1939 bh = sb_getblk(sb, udf_get_lb_pblock(sb, &neloc, 0)); 1940 if (!bh) 1941 return -EIO; 1942 lock_buffer(bh); 1943 memset(bh->b_data, 0x00, sb->s_blocksize); 1944 set_buffer_uptodate(bh); 1945 unlock_buffer(bh); 1946 mark_buffer_dirty_inode(bh, inode); 1947 1948 aed = (struct allocExtDesc *)(bh->b_data); 1949 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) { 1950 aed->previousAllocExtLocation = 1951 cpu_to_le32(epos->block.logicalBlockNum); 1952 } 1953 aed->lengthAllocDescs = cpu_to_le32(0); 1954 if (UDF_SB(sb)->s_udfrev >= 0x0200) 1955 ver = 3; 1956 else 1957 ver = 2; 1958 udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block, 1959 sizeof(struct tag)); 1960 1961 nepos.block = neloc; 1962 nepos.offset = sizeof(struct allocExtDesc); 1963 nepos.bh = bh; 1964 1965 /* 1966 * Do we have to copy current last extent to make space for indirect 1967 * one? 1968 */ 1969 if (epos->offset + adsize > sb->s_blocksize) { 1970 struct kernel_lb_addr cp_loc; 1971 uint32_t cp_len; 1972 int cp_type; 1973 1974 epos->offset -= adsize; 1975 cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0); 1976 cp_len |= ((uint32_t)cp_type) << 30; 1977 1978 __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1); 1979 udf_write_aext(inode, epos, &nepos.block, 1980 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDESCS, 0); 1981 } else { 1982 __udf_add_aext(inode, epos, &nepos.block, 1983 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDESCS, 0); 1984 } 1985 1986 brelse(epos->bh); 1987 *epos = nepos; 1988 1989 return 0; 1990 } 1991 1992 /* 1993 * Append extent at the given position - should be the first free one in inode 1994 * / indirect extent. This function assumes there is enough space in the inode 1995 * or indirect extent. Use udf_add_aext() if you didn't check for this before. 1996 */ 1997 int __udf_add_aext(struct inode *inode, struct extent_position *epos, 1998 struct kernel_lb_addr *eloc, uint32_t elen, int inc) 1999 { 2000 struct udf_inode_info *iinfo = UDF_I(inode); 2001 struct allocExtDesc *aed; 2002 int adsize; 2003 2004 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) 2005 adsize = sizeof(struct short_ad); 2006 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) 2007 adsize = sizeof(struct long_ad); 2008 else 2009 return -EIO; 2010 2011 if (!epos->bh) { 2012 WARN_ON(iinfo->i_lenAlloc != 2013 epos->offset - udf_file_entry_alloc_offset(inode)); 2014 } else { 2015 aed = (struct allocExtDesc *)epos->bh->b_data; 2016 WARN_ON(le32_to_cpu(aed->lengthAllocDescs) != 2017 epos->offset - sizeof(struct allocExtDesc)); 2018 WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize); 2019 } 2020 2021 udf_write_aext(inode, epos, eloc, elen, inc); 2022 2023 if (!epos->bh) { 2024 iinfo->i_lenAlloc += adsize; 2025 mark_inode_dirty(inode); 2026 } else { 2027 aed = (struct allocExtDesc *)epos->bh->b_data; 2028 le32_add_cpu(&aed->lengthAllocDescs, adsize); 2029 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || 2030 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) 2031 udf_update_tag(epos->bh->b_data, 2032 epos->offset + (inc ? 0 : adsize)); 2033 else 2034 udf_update_tag(epos->bh->b_data, 2035 sizeof(struct allocExtDesc)); 2036 mark_buffer_dirty_inode(epos->bh, inode); 2037 } 2038 2039 return 0; 2040 } 2041 2042 /* 2043 * Append extent at given position - should be the first free one in inode 2044 * / indirect extent. Takes care of allocating and linking indirect blocks. 2045 */ 2046 int udf_add_aext(struct inode *inode, struct extent_position *epos, 2047 struct kernel_lb_addr *eloc, uint32_t elen, int inc) 2048 { 2049 int adsize; 2050 struct super_block *sb = inode->i_sb; 2051 2052 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT) 2053 adsize = sizeof(struct short_ad); 2054 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG) 2055 adsize = sizeof(struct long_ad); 2056 else 2057 return -EIO; 2058 2059 if (epos->offset + (2 * adsize) > sb->s_blocksize) { 2060 int err; 2061 udf_pblk_t new_block; 2062 2063 new_block = udf_new_block(sb, NULL, 2064 epos->block.partitionReferenceNum, 2065 epos->block.logicalBlockNum, &err); 2066 if (!new_block) 2067 return -ENOSPC; 2068 2069 err = udf_setup_indirect_aext(inode, new_block, epos); 2070 if (err) 2071 return err; 2072 } 2073 2074 return __udf_add_aext(inode, epos, eloc, elen, inc); 2075 } 2076 2077 void udf_write_aext(struct inode *inode, struct extent_position *epos, 2078 struct kernel_lb_addr *eloc, uint32_t elen, int inc) 2079 { 2080 int adsize; 2081 uint8_t *ptr; 2082 struct short_ad *sad; 2083 struct long_ad *lad; 2084 struct udf_inode_info *iinfo = UDF_I(inode); 2085 2086 if (!epos->bh) 2087 ptr = iinfo->i_data + epos->offset - 2088 udf_file_entry_alloc_offset(inode) + 2089 iinfo->i_lenEAttr; 2090 else 2091 ptr = epos->bh->b_data + epos->offset; 2092 2093 switch (iinfo->i_alloc_type) { 2094 case ICBTAG_FLAG_AD_SHORT: 2095 sad = (struct short_ad *)ptr; 2096 sad->extLength = cpu_to_le32(elen); 2097 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum); 2098 adsize = sizeof(struct short_ad); 2099 break; 2100 case ICBTAG_FLAG_AD_LONG: 2101 lad = (struct long_ad *)ptr; 2102 lad->extLength = cpu_to_le32(elen); 2103 lad->extLocation = cpu_to_lelb(*eloc); 2104 memset(lad->impUse, 0x00, sizeof(lad->impUse)); 2105 adsize = sizeof(struct long_ad); 2106 break; 2107 default: 2108 return; 2109 } 2110 2111 if (epos->bh) { 2112 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || 2113 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) { 2114 struct allocExtDesc *aed = 2115 (struct allocExtDesc *)epos->bh->b_data; 2116 udf_update_tag(epos->bh->b_data, 2117 le32_to_cpu(aed->lengthAllocDescs) + 2118 sizeof(struct allocExtDesc)); 2119 } 2120 mark_buffer_dirty_inode(epos->bh, inode); 2121 } else { 2122 mark_inode_dirty(inode); 2123 } 2124 2125 if (inc) 2126 epos->offset += adsize; 2127 } 2128 2129 /* 2130 * Only 1 indirect extent in a row really makes sense but allow upto 16 in case 2131 * someone does some weird stuff. 2132 */ 2133 #define UDF_MAX_INDIR_EXTS 16 2134 2135 int8_t udf_next_aext(struct inode *inode, struct extent_position *epos, 2136 struct kernel_lb_addr *eloc, uint32_t *elen, int inc) 2137 { 2138 int8_t etype; 2139 unsigned int indirections = 0; 2140 2141 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) == 2142 (EXT_NEXT_EXTENT_ALLOCDESCS >> 30)) { 2143 udf_pblk_t block; 2144 2145 if (++indirections > UDF_MAX_INDIR_EXTS) { 2146 udf_err(inode->i_sb, 2147 "too many indirect extents in inode %lu\n", 2148 inode->i_ino); 2149 return -1; 2150 } 2151 2152 epos->block = *eloc; 2153 epos->offset = sizeof(struct allocExtDesc); 2154 brelse(epos->bh); 2155 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0); 2156 epos->bh = sb_bread(inode->i_sb, block); 2157 if (!epos->bh) { 2158 udf_debug("reading block %u failed!\n", block); 2159 return -1; 2160 } 2161 } 2162 2163 return etype; 2164 } 2165 2166 int8_t udf_current_aext(struct inode *inode, struct extent_position *epos, 2167 struct kernel_lb_addr *eloc, uint32_t *elen, int inc) 2168 { 2169 int alen; 2170 int8_t etype; 2171 uint8_t *ptr; 2172 struct short_ad *sad; 2173 struct long_ad *lad; 2174 struct udf_inode_info *iinfo = UDF_I(inode); 2175 2176 if (!epos->bh) { 2177 if (!epos->offset) 2178 epos->offset = udf_file_entry_alloc_offset(inode); 2179 ptr = iinfo->i_data + epos->offset - 2180 udf_file_entry_alloc_offset(inode) + 2181 iinfo->i_lenEAttr; 2182 alen = udf_file_entry_alloc_offset(inode) + 2183 iinfo->i_lenAlloc; 2184 } else { 2185 if (!epos->offset) 2186 epos->offset = sizeof(struct allocExtDesc); 2187 ptr = epos->bh->b_data + epos->offset; 2188 alen = sizeof(struct allocExtDesc) + 2189 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)-> 2190 lengthAllocDescs); 2191 } 2192 2193 switch (iinfo->i_alloc_type) { 2194 case ICBTAG_FLAG_AD_SHORT: 2195 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc); 2196 if (!sad) 2197 return -1; 2198 etype = le32_to_cpu(sad->extLength) >> 30; 2199 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition); 2200 eloc->partitionReferenceNum = 2201 iinfo->i_location.partitionReferenceNum; 2202 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK; 2203 break; 2204 case ICBTAG_FLAG_AD_LONG: 2205 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc); 2206 if (!lad) 2207 return -1; 2208 etype = le32_to_cpu(lad->extLength) >> 30; 2209 *eloc = lelb_to_cpu(lad->extLocation); 2210 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK; 2211 break; 2212 default: 2213 udf_debug("alloc_type = %u unsupported\n", iinfo->i_alloc_type); 2214 return -1; 2215 } 2216 2217 return etype; 2218 } 2219 2220 static int udf_insert_aext(struct inode *inode, struct extent_position epos, 2221 struct kernel_lb_addr neloc, uint32_t nelen) 2222 { 2223 struct kernel_lb_addr oeloc; 2224 uint32_t oelen; 2225 int8_t etype; 2226 int err; 2227 2228 if (epos.bh) 2229 get_bh(epos.bh); 2230 2231 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) { 2232 udf_write_aext(inode, &epos, &neloc, nelen, 1); 2233 neloc = oeloc; 2234 nelen = (etype << 30) | oelen; 2235 } 2236 err = udf_add_aext(inode, &epos, &neloc, nelen, 1); 2237 brelse(epos.bh); 2238 2239 return err; 2240 } 2241 2242 int8_t udf_delete_aext(struct inode *inode, struct extent_position epos) 2243 { 2244 struct extent_position oepos; 2245 int adsize; 2246 int8_t etype; 2247 struct allocExtDesc *aed; 2248 struct udf_inode_info *iinfo; 2249 struct kernel_lb_addr eloc; 2250 uint32_t elen; 2251 2252 if (epos.bh) { 2253 get_bh(epos.bh); 2254 get_bh(epos.bh); 2255 } 2256 2257 iinfo = UDF_I(inode); 2258 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) 2259 adsize = sizeof(struct short_ad); 2260 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) 2261 adsize = sizeof(struct long_ad); 2262 else 2263 adsize = 0; 2264 2265 oepos = epos; 2266 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1) 2267 return -1; 2268 2269 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) { 2270 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1); 2271 if (oepos.bh != epos.bh) { 2272 oepos.block = epos.block; 2273 brelse(oepos.bh); 2274 get_bh(epos.bh); 2275 oepos.bh = epos.bh; 2276 oepos.offset = epos.offset - adsize; 2277 } 2278 } 2279 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr)); 2280 elen = 0; 2281 2282 if (epos.bh != oepos.bh) { 2283 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1); 2284 udf_write_aext(inode, &oepos, &eloc, elen, 1); 2285 udf_write_aext(inode, &oepos, &eloc, elen, 1); 2286 if (!oepos.bh) { 2287 iinfo->i_lenAlloc -= (adsize * 2); 2288 mark_inode_dirty(inode); 2289 } else { 2290 aed = (struct allocExtDesc *)oepos.bh->b_data; 2291 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize)); 2292 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || 2293 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) 2294 udf_update_tag(oepos.bh->b_data, 2295 oepos.offset - (2 * adsize)); 2296 else 2297 udf_update_tag(oepos.bh->b_data, 2298 sizeof(struct allocExtDesc)); 2299 mark_buffer_dirty_inode(oepos.bh, inode); 2300 } 2301 } else { 2302 udf_write_aext(inode, &oepos, &eloc, elen, 1); 2303 if (!oepos.bh) { 2304 iinfo->i_lenAlloc -= adsize; 2305 mark_inode_dirty(inode); 2306 } else { 2307 aed = (struct allocExtDesc *)oepos.bh->b_data; 2308 le32_add_cpu(&aed->lengthAllocDescs, -adsize); 2309 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || 2310 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) 2311 udf_update_tag(oepos.bh->b_data, 2312 epos.offset - adsize); 2313 else 2314 udf_update_tag(oepos.bh->b_data, 2315 sizeof(struct allocExtDesc)); 2316 mark_buffer_dirty_inode(oepos.bh, inode); 2317 } 2318 } 2319 2320 brelse(epos.bh); 2321 brelse(oepos.bh); 2322 2323 return (elen >> 30); 2324 } 2325 2326 int8_t inode_bmap(struct inode *inode, sector_t block, 2327 struct extent_position *pos, struct kernel_lb_addr *eloc, 2328 uint32_t *elen, sector_t *offset) 2329 { 2330 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits; 2331 loff_t lbcount = 0, bcount = (loff_t) block << blocksize_bits; 2332 int8_t etype; 2333 struct udf_inode_info *iinfo; 2334 2335 iinfo = UDF_I(inode); 2336 if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) { 2337 pos->offset = 0; 2338 pos->block = iinfo->i_location; 2339 pos->bh = NULL; 2340 } 2341 *elen = 0; 2342 do { 2343 etype = udf_next_aext(inode, pos, eloc, elen, 1); 2344 if (etype == -1) { 2345 *offset = (bcount - lbcount) >> blocksize_bits; 2346 iinfo->i_lenExtents = lbcount; 2347 return -1; 2348 } 2349 lbcount += *elen; 2350 } while (lbcount <= bcount); 2351 /* update extent cache */ 2352 udf_update_extent_cache(inode, lbcount - *elen, pos); 2353 *offset = (bcount + *elen - lbcount) >> blocksize_bits; 2354 2355 return etype; 2356 } 2357