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