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