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