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