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