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