Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

ntfs: update inode operations

Add extent inode loading via ntfs_extent_inode_open() and
ntfs_inode_attach_all_extents().

Allow dynamic creation of with ntfs_inode_add_attrlist() when
the base MFT record overflows.

Introduce ntfs_inode_free_space() to move attributes out of
the base record on demand.

Implement direct attribute I/O through ntfs_inode_attr_pread() and
ntfs_inode_attr_pwrite().

Implement .create, .unlink, .mkdir, .rmdir, .rename, .symlink, .mknod,
.link callbacks.

Introduce ntfs_non_resident_dealloc_clusters() to free clusters of
non-resident attributes during inode eviction.

Add ntfs_drop_big_inode() logic to safely truncate and deallocate
clusters.

Acked-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Hyunchul Lee <hyc.lee@gmail.com>
Signed-off-by: Namjae Jeon <linkinjeon@kernel.org>

+3641 -1620
+2193 -1475
fs/ntfs/inode.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-or-later 2 2 /* 3 - * inode.c - NTFS kernel inode handling. 3 + * NTFS kernel inode handling. 4 4 * 5 5 * Copyright (c) 2001-2014 Anton Altaparmakov and Tuxera Inc. 6 + * Copyright (c) 2025 LG Electronics Co., Ltd. 6 7 */ 7 8 8 - #include <linux/buffer_head.h> 9 - #include <linux/fs.h> 10 - #include <linux/mm.h> 11 - #include <linux/mount.h> 12 - #include <linux/mutex.h> 13 - #include <linux/pagemap.h> 14 - #include <linux/quotaops.h> 15 - #include <linux/slab.h> 16 - #include <linux/log2.h> 9 + #include <linux/writeback.h> 10 + #include <linux/seq_file.h> 17 11 18 - #include "aops.h" 19 - #include "attrib.h" 20 - #include "bitmap.h" 21 - #include "dir.h" 22 - #include "debug.h" 23 - #include "inode.h" 24 12 #include "lcnalloc.h" 25 - #include "malloc.h" 26 - #include "mft.h" 27 13 #include "time.h" 28 14 #include "ntfs.h" 15 + #include "index.h" 16 + #include "attrlist.h" 17 + #include "reparse.h" 18 + #include "ea.h" 19 + #include "attrib.h" 20 + #include "iomap.h" 21 + #include "object_id.h" 29 22 30 - /** 23 + /* 31 24 * ntfs_test_inode - compare two (possibly fake) inodes for equality 32 25 * @vi: vfs inode which to test 33 26 * @data: data which is being tested with ··· 38 45 */ 39 46 int ntfs_test_inode(struct inode *vi, void *data) 40 47 { 41 - ntfs_attr *na = (ntfs_attr *)data; 42 - ntfs_inode *ni; 48 + struct ntfs_attr *na = data; 49 + struct ntfs_inode *ni = NTFS_I(vi); 43 50 44 51 if (vi->i_ino != na->mft_no) 45 52 return 0; 46 - ni = NTFS_I(vi); 53 + 47 54 /* If !NInoAttr(ni), @vi is a normal file or directory inode. */ 48 55 if (likely(!NInoAttr(ni))) { 49 56 /* If not looking for a normal inode this is a mismatch. */ ··· 56 63 if (ni->name_len != na->name_len) 57 64 return 0; 58 65 if (na->name_len && memcmp(ni->name, na->name, 59 - na->name_len * sizeof(ntfschar))) 66 + na->name_len * sizeof(__le16))) 67 + return 0; 68 + if (!ni->ext.base_ntfs_ino) 60 69 return 0; 61 70 } 71 + 62 72 /* Match! */ 63 73 return 1; 64 74 } 65 75 66 - /** 76 + /* 67 77 * ntfs_init_locked_inode - initialize an inode 68 78 * @vi: vfs inode to initialize 69 79 * @data: data which to initialize @vi to ··· 79 83 * respectively. Although that is not strictly necessary as 80 84 * ntfs_read_locked_inode() will fill them in later. 81 85 * 82 - * Return 0 on success and -errno on error. 86 + * Return 0 on success and error. 83 87 * 84 88 * NOTE: This function runs with the inode->i_lock spin lock held so it is not 85 89 * allowed to sleep. (Hence the GFP_ATOMIC allocation.) 86 90 */ 87 91 static int ntfs_init_locked_inode(struct inode *vi, void *data) 88 92 { 89 - ntfs_attr *na = (ntfs_attr *)data; 90 - ntfs_inode *ni = NTFS_I(vi); 93 + struct ntfs_attr *na = data; 94 + struct ntfs_inode *ni = NTFS_I(vi); 91 95 92 96 vi->i_ino = na->mft_no; 93 97 94 - ni->type = na->type; 95 98 if (na->type == AT_INDEX_ALLOCATION) 96 99 NInoSetMstProtected(ni); 100 + else 101 + ni->type = na->type; 97 102 98 103 ni->name = na->name; 99 104 ni->name_len = na->name_len; 105 + ni->folio = NULL; 106 + atomic_set(&ni->count, 1); 100 107 101 108 /* If initializing a normal inode, we are done. */ 102 - if (likely(na->type == AT_UNUSED)) { 103 - BUG_ON(na->name); 104 - BUG_ON(na->name_len); 109 + if (likely(na->type == AT_UNUSED)) 105 110 return 0; 106 - } 107 111 108 112 /* It is a fake inode. */ 109 113 NInoSetAttr(ni); ··· 118 122 if (na->name_len && na->name != I30) { 119 123 unsigned int i; 120 124 121 - BUG_ON(!na->name); 122 - i = na->name_len * sizeof(ntfschar); 123 - ni->name = kmalloc(i + sizeof(ntfschar), GFP_ATOMIC); 125 + i = na->name_len * sizeof(__le16); 126 + ni->name = kmalloc(i + sizeof(__le16), GFP_ATOMIC); 124 127 if (!ni->name) 125 128 return -ENOMEM; 126 129 memcpy(ni->name, na->name, i); ··· 133 138 static int ntfs_read_locked_index_inode(struct inode *base_vi, 134 139 struct inode *vi); 135 140 136 - /** 141 + /* 137 142 * ntfs_iget - obtain a struct inode corresponding to a specific normal inode 138 143 * @sb: super block of mounted volume 139 144 * @mft_no: mft record number / inode number to obtain ··· 153 158 { 154 159 struct inode *vi; 155 160 int err; 156 - ntfs_attr na; 161 + struct ntfs_attr na; 157 162 158 163 na.mft_no = mft_no; 159 164 na.type = AT_UNUSED; ··· 168 173 err = 0; 169 174 170 175 /* If this is a freshly allocated inode, need to read it now. */ 171 - if (vi->i_state & I_NEW) { 176 + if (inode_state_read_once(vi) & I_NEW) { 172 177 err = ntfs_read_locked_inode(vi); 173 178 unlock_new_inode(vi); 174 179 } ··· 183 188 return vi; 184 189 } 185 190 186 - /** 191 + /* 187 192 * ntfs_attr_iget - obtain a struct inode corresponding to an attribute 188 193 * @base_vi: vfs base inode containing the attribute 189 194 * @type: attribute type ··· 206 211 * value with IS_ERR() and if true, the function failed and the error code is 207 212 * obtained from PTR_ERR(). 208 213 */ 209 - struct inode *ntfs_attr_iget(struct inode *base_vi, ATTR_TYPE type, 210 - ntfschar *name, u32 name_len) 214 + struct inode *ntfs_attr_iget(struct inode *base_vi, __le32 type, 215 + __le16 *name, u32 name_len) 211 216 { 212 217 struct inode *vi; 213 218 int err; 214 - ntfs_attr na; 219 + struct ntfs_attr na; 215 220 216 221 /* Make sure no one calls ntfs_attr_iget() for indices. */ 217 - BUG_ON(type == AT_INDEX_ALLOCATION); 222 + WARN_ON(type == AT_INDEX_ALLOCATION); 218 223 219 224 na.mft_no = base_vi->i_ino; 220 225 na.type = type; ··· 225 230 ntfs_init_locked_inode, &na); 226 231 if (unlikely(!vi)) 227 232 return ERR_PTR(-ENOMEM); 228 - 229 233 err = 0; 230 234 231 235 /* If this is a freshly allocated inode, need to read it now. */ 232 - if (vi->i_state & I_NEW) { 236 + if (inode_state_read_once(vi) & I_NEW) { 233 237 err = ntfs_read_locked_attr_inode(base_vi, vi); 234 238 unlock_new_inode(vi); 235 239 } ··· 244 250 return vi; 245 251 } 246 252 247 - /** 253 + /* 248 254 * ntfs_index_iget - obtain a struct inode corresponding to an index 249 255 * @base_vi: vfs base inode containing the index related attributes 250 256 * @name: Unicode name of the index ··· 263 269 * value with IS_ERR() and if true, the function failed and the error code is 264 270 * obtained from PTR_ERR(). 265 271 */ 266 - struct inode *ntfs_index_iget(struct inode *base_vi, ntfschar *name, 272 + struct inode *ntfs_index_iget(struct inode *base_vi, __le16 *name, 267 273 u32 name_len) 268 274 { 269 275 struct inode *vi; 270 276 int err; 271 - ntfs_attr na; 277 + struct ntfs_attr na; 272 278 273 279 na.mft_no = base_vi->i_ino; 274 280 na.type = AT_INDEX_ALLOCATION; ··· 283 289 err = 0; 284 290 285 291 /* If this is a freshly allocated inode, need to read it now. */ 286 - if (vi->i_state & I_NEW) { 292 + if (inode_state_read_once(vi) & I_NEW) { 287 293 err = ntfs_read_locked_index_inode(base_vi, vi); 288 294 unlock_new_inode(vi); 289 295 } ··· 301 307 302 308 struct inode *ntfs_alloc_big_inode(struct super_block *sb) 303 309 { 304 - ntfs_inode *ni; 310 + struct ntfs_inode *ni; 305 311 306 312 ntfs_debug("Entering."); 307 313 ni = alloc_inode_sb(sb, ntfs_big_inode_cache, GFP_NOFS); 308 314 if (likely(ni != NULL)) { 309 315 ni->state = 0; 316 + ni->type = 0; 317 + ni->mft_no = 0; 310 318 return VFS_I(ni); 311 319 } 312 320 ntfs_error(sb, "Allocation of NTFS big inode structure failed."); ··· 320 324 kmem_cache_free(ntfs_big_inode_cache, NTFS_I(inode)); 321 325 } 322 326 323 - static inline ntfs_inode *ntfs_alloc_extent_inode(void) 327 + static int ntfs_non_resident_dealloc_clusters(struct ntfs_inode *ni) 324 328 { 325 - ntfs_inode *ni; 329 + struct super_block *sb = ni->vol->sb; 330 + struct ntfs_attr_search_ctx *actx; 331 + int err = 0; 332 + 333 + actx = ntfs_attr_get_search_ctx(ni, NULL); 334 + if (!actx) 335 + return -ENOMEM; 336 + WARN_ON(actx->mrec->link_count != 0); 337 + 338 + /** 339 + * ntfs_truncate_vfs cannot be called in evict() context due 340 + * to some limitations, which are the @ni vfs inode is marked 341 + * with I_FREEING, and etc. 342 + */ 343 + if (NInoRunlistDirty(ni)) { 344 + err = ntfs_cluster_free_from_rl(ni->vol, ni->runlist.rl); 345 + if (err) 346 + ntfs_error(sb, 347 + "Failed to free clusters. Leaving inconsistent metadata.\n"); 348 + } 349 + 350 + while ((err = ntfs_attrs_walk(actx)) == 0) { 351 + if (actx->attr->non_resident && 352 + (!NInoRunlistDirty(ni) || actx->attr->type != AT_DATA)) { 353 + struct runlist_element *rl; 354 + size_t new_rl_count; 355 + 356 + rl = ntfs_mapping_pairs_decompress(ni->vol, actx->attr, NULL, 357 + &new_rl_count); 358 + if (IS_ERR(rl)) { 359 + err = PTR_ERR(rl); 360 + ntfs_error(sb, 361 + "Failed to decompress runlist. Leaving inconsistent metadata.\n"); 362 + continue; 363 + } 364 + 365 + err = ntfs_cluster_free_from_rl(ni->vol, rl); 366 + if (err) 367 + ntfs_error(sb, 368 + "Failed to free attribute clusters. Leaving inconsistent metadata.\n"); 369 + kvfree(rl); 370 + } 371 + } 372 + 373 + ntfs_release_dirty_clusters(ni->vol, ni->i_dealloc_clusters); 374 + ntfs_attr_put_search_ctx(actx); 375 + return err; 376 + } 377 + 378 + int ntfs_drop_big_inode(struct inode *inode) 379 + { 380 + struct ntfs_inode *ni = NTFS_I(inode); 381 + 382 + if (!inode_unhashed(inode) && inode_state_read_once(inode) & I_SYNC) { 383 + if (ni->type == AT_DATA || ni->type == AT_INDEX_ALLOCATION) { 384 + if (!inode->i_nlink) { 385 + struct ntfs_inode *ni = NTFS_I(inode); 386 + 387 + if (ni->data_size == 0) 388 + return 0; 389 + 390 + /* To avoid evict_inode call simultaneously */ 391 + atomic_inc(&inode->i_count); 392 + spin_unlock(&inode->i_lock); 393 + 394 + truncate_setsize(VFS_I(ni), 0); 395 + ntfs_truncate_vfs(VFS_I(ni), 0, 1); 396 + 397 + sb_start_intwrite(inode->i_sb); 398 + i_size_write(inode, 0); 399 + ni->allocated_size = ni->initialized_size = ni->data_size = 0; 400 + 401 + truncate_inode_pages_final(inode->i_mapping); 402 + sb_end_intwrite(inode->i_sb); 403 + 404 + spin_lock(&inode->i_lock); 405 + atomic_dec(&inode->i_count); 406 + } 407 + } 408 + return 0; 409 + } 410 + 411 + return inode_generic_drop(inode); 412 + } 413 + 414 + static inline struct ntfs_inode *ntfs_alloc_extent_inode(void) 415 + { 416 + struct ntfs_inode *ni; 326 417 327 418 ntfs_debug("Entering."); 328 419 ni = kmem_cache_alloc(ntfs_inode_cache, GFP_NOFS); ··· 421 338 return NULL; 422 339 } 423 340 424 - static void ntfs_destroy_extent_inode(ntfs_inode *ni) 341 + static void ntfs_destroy_extent_inode(struct ntfs_inode *ni) 425 342 { 426 343 ntfs_debug("Entering."); 427 - BUG_ON(ni->page); 344 + 428 345 if (!atomic_dec_and_test(&ni->count)) 429 - BUG(); 346 + WARN_ON(1); 347 + if (ni->folio) 348 + folio_put(ni->folio); 349 + kfree(ni->mrec); 430 350 kmem_cache_free(ntfs_inode_cache, ni); 431 351 } 352 + 353 + static struct lock_class_key attr_inode_mrec_lock_class; 354 + static struct lock_class_key attr_list_inode_mrec_lock_class; 432 355 433 356 /* 434 357 * The attribute runlist lock has separate locking rules from the ··· 442 353 */ 443 354 static struct lock_class_key attr_list_rl_lock_class; 444 355 445 - /** 356 + /* 446 357 * __ntfs_init_inode - initialize ntfs specific part of an inode 447 358 * @sb: super block of mounted volume 448 359 * @ni: freshly allocated ntfs inode which to initialize ··· 451 362 * 452 363 * NOTE: ni->mft_no, ni->state, ni->type, ni->name, and ni->name_len are left 453 364 * untouched. Make sure to initialize them elsewhere. 454 - * 455 - * Return zero on success and -ENOMEM on error. 456 365 */ 457 - void __ntfs_init_inode(struct super_block *sb, ntfs_inode *ni) 366 + void __ntfs_init_inode(struct super_block *sb, struct ntfs_inode *ni) 458 367 { 459 368 ntfs_debug("Entering."); 460 369 rwlock_init(&ni->size_lock); ··· 462 375 ni->vol = NTFS_SB(sb); 463 376 ntfs_init_runlist(&ni->runlist); 464 377 mutex_init(&ni->mrec_lock); 465 - ni->page = NULL; 466 - ni->page_ofs = 0; 378 + if (ni->type == AT_ATTRIBUTE_LIST) { 379 + lockdep_set_class(&ni->mrec_lock, 380 + &attr_list_inode_mrec_lock_class); 381 + lockdep_set_class(&ni->runlist.lock, 382 + &attr_list_rl_lock_class); 383 + } else if (NInoAttr(ni)) { 384 + lockdep_set_class(&ni->mrec_lock, 385 + &attr_inode_mrec_lock_class); 386 + } 387 + 388 + ni->folio = NULL; 389 + ni->folio_ofs = 0; 390 + ni->mrec = NULL; 467 391 ni->attr_list_size = 0; 468 392 ni->attr_list = NULL; 469 - ntfs_init_runlist(&ni->attr_list_rl); 470 - lockdep_set_class(&ni->attr_list_rl.lock, 471 - &attr_list_rl_lock_class); 472 393 ni->itype.index.block_size = 0; 473 394 ni->itype.index.vcn_size = 0; 474 395 ni->itype.index.collation_rule = 0; ··· 485 390 mutex_init(&ni->extent_lock); 486 391 ni->nr_extents = 0; 487 392 ni->ext.base_ntfs_ino = NULL; 393 + ni->flags = 0; 394 + ni->mft_lcn[0] = LCN_RL_NOT_MAPPED; 395 + ni->mft_lcn_count = 0; 396 + ni->target = NULL; 397 + ni->i_dealloc_clusters = 0; 488 398 } 489 399 490 400 /* ··· 499 399 */ 500 400 static struct lock_class_key extent_inode_mrec_lock_key; 501 401 502 - inline ntfs_inode *ntfs_new_extent_inode(struct super_block *sb, 402 + inline struct ntfs_inode *ntfs_new_extent_inode(struct super_block *sb, 503 403 unsigned long mft_no) 504 404 { 505 - ntfs_inode *ni = ntfs_alloc_extent_inode(); 405 + struct ntfs_inode *ni = ntfs_alloc_extent_inode(); 506 406 507 407 ntfs_debug("Entering."); 508 408 if (likely(ni != NULL)) { ··· 516 416 return ni; 517 417 } 518 418 519 - /** 419 + /* 520 420 * ntfs_is_extended_system_file - check if a file is in the $Extend directory 521 421 * @ctx: initialized attribute search context 522 422 * ··· 525 425 * directory. 526 426 * 527 427 * Return values: 428 + * 3: file is $ObjId in $Extend directory 429 + * 2: file is $Reparse in $Extend directory 528 430 * 1: file is in $Extend directory 529 431 * 0: file is not in $Extend directory 530 432 * -errno: failed to determine if the file is in the $Extend directory 531 433 */ 532 - static int ntfs_is_extended_system_file(ntfs_attr_search_ctx *ctx) 434 + static int ntfs_is_extended_system_file(struct ntfs_attr_search_ctx *ctx) 533 435 { 534 436 int nr_links, err; 535 437 ··· 544 442 /* Loop through all hard links. */ 545 443 while (!(err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0, NULL, 0, 546 444 ctx))) { 547 - FILE_NAME_ATTR *file_name_attr; 548 - ATTR_RECORD *attr = ctx->attr; 445 + struct file_name_attr *file_name_attr; 446 + struct attr_record *attr = ctx->attr; 549 447 u8 *p, *p2; 550 448 551 449 nr_links--; ··· 553 451 * Maximum sanity checking as we are called on an inode that 554 452 * we suspect might be corrupt. 555 453 */ 556 - p = (u8*)attr + le32_to_cpu(attr->length); 557 - if (p < (u8*)ctx->mrec || (u8*)p > (u8*)ctx->mrec + 454 + p = (u8 *)attr + le32_to_cpu(attr->length); 455 + if (p < (u8 *)ctx->mrec || (u8 *)p > (u8 *)ctx->mrec + 558 456 le32_to_cpu(ctx->mrec->bytes_in_use)) { 559 457 err_corrupt_attr: 560 - ntfs_error(ctx->ntfs_ino->vol->sb, "Corrupt file name " 561 - "attribute. You should run chkdsk."); 458 + ntfs_error(ctx->ntfs_ino->vol->sb, 459 + "Corrupt file name attribute. You should run chkdsk."); 562 460 return -EIO; 563 461 } 564 462 if (attr->non_resident) { 565 - ntfs_error(ctx->ntfs_ino->vol->sb, "Non-resident file " 566 - "name. You should run chkdsk."); 463 + ntfs_error(ctx->ntfs_ino->vol->sb, 464 + "Non-resident file name. You should run chkdsk."); 567 465 return -EIO; 568 466 } 569 467 if (attr->flags) { 570 - ntfs_error(ctx->ntfs_ino->vol->sb, "File name with " 571 - "invalid flags. You should run " 572 - "chkdsk."); 468 + ntfs_error(ctx->ntfs_ino->vol->sb, 469 + "File name with invalid flags. You should run chkdsk."); 573 470 return -EIO; 574 471 } 575 472 if (!(attr->data.resident.flags & RESIDENT_ATTR_IS_INDEXED)) { 576 - ntfs_error(ctx->ntfs_ino->vol->sb, "Unindexed file " 577 - "name. You should run chkdsk."); 473 + ntfs_error(ctx->ntfs_ino->vol->sb, 474 + "Unindexed file name. You should run chkdsk."); 578 475 return -EIO; 579 476 } 580 - file_name_attr = (FILE_NAME_ATTR*)((u8*)attr + 477 + file_name_attr = (struct file_name_attr *)((u8 *)attr + 581 478 le16_to_cpu(attr->data.resident.value_offset)); 582 479 p2 = (u8 *)file_name_attr + le32_to_cpu(attr->data.resident.value_length); 583 - if (p2 < (u8*)attr || p2 > p) 480 + if (p2 < (u8 *)attr || p2 > p) 584 481 goto err_corrupt_attr; 585 482 /* This attribute is ok, but is it in the $Extend directory? */ 586 - if (MREF_LE(file_name_attr->parent_directory) == FILE_Extend) 483 + if (MREF_LE(file_name_attr->parent_directory) == FILE_Extend) { 484 + unsigned char *s; 485 + 486 + s = ntfs_attr_name_get(ctx->ntfs_ino->vol, 487 + file_name_attr->file_name, 488 + file_name_attr->file_name_length); 489 + if (!s) 490 + return 1; 491 + if (!strcmp("$Reparse", s)) { 492 + ntfs_attr_name_free(&s); 493 + return 2; /* it's reparse point file */ 494 + } 495 + if (!strcmp("$ObjId", s)) { 496 + ntfs_attr_name_free(&s); 497 + return 3; /* it's object id file */ 498 + } 499 + ntfs_attr_name_free(&s); 587 500 return 1; /* YES, it's an extended system file. */ 501 + } 588 502 } 589 503 if (unlikely(err != -ENOENT)) 590 504 return err; 591 505 if (unlikely(nr_links)) { 592 - ntfs_error(ctx->ntfs_ino->vol->sb, "Inode hard link count " 593 - "doesn't match number of name attributes. You " 594 - "should run chkdsk."); 506 + ntfs_error(ctx->ntfs_ino->vol->sb, 507 + "Inode hard link count doesn't match number of name attributes. You should run chkdsk."); 595 508 return -EIO; 596 509 } 597 510 return 0; /* NO, it is not an extended system file. */ 598 511 } 599 512 600 - /** 513 + static struct lock_class_key ntfs_dir_inval_lock_key; 514 + 515 + void ntfs_set_vfs_operations(struct inode *inode, mode_t mode, dev_t dev) 516 + { 517 + if (S_ISDIR(mode)) { 518 + if (!NInoAttr(NTFS_I(inode))) { 519 + inode->i_op = &ntfs_dir_inode_ops; 520 + inode->i_fop = &ntfs_dir_ops; 521 + } 522 + inode->i_mapping->a_ops = &ntfs_aops; 523 + lockdep_set_class(&inode->i_mapping->invalidate_lock, 524 + &ntfs_dir_inval_lock_key); 525 + } else if (S_ISLNK(mode)) { 526 + inode->i_op = &ntfs_symlink_inode_operations; 527 + inode->i_mapping->a_ops = &ntfs_aops; 528 + } else if (S_ISCHR(mode) || S_ISBLK(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) { 529 + inode->i_op = &ntfs_special_inode_operations; 530 + init_special_inode(inode, inode->i_mode, dev); 531 + } else { 532 + if (!NInoAttr(NTFS_I(inode))) { 533 + inode->i_op = &ntfs_file_inode_ops; 534 + inode->i_fop = &ntfs_file_ops; 535 + } 536 + if (inode->i_ino == FILE_MFT) 537 + inode->i_mapping->a_ops = &ntfs_mft_aops; 538 + else 539 + inode->i_mapping->a_ops = &ntfs_aops; 540 + } 541 + } 542 + 543 + /* 601 544 * ntfs_read_locked_inode - read an inode from its device 602 545 * @vi: inode to read 603 546 * ··· 665 518 * we need to do that using the IS_* macros defined in include/linux/fs.h. 666 519 * In any case ntfs_read_locked_inode() has nothing to do with i_flags. 667 520 * 668 - * Return 0 on success and -errno on error. In the error case, the inode will 669 - * have had make_bad_inode() executed on it. 521 + * Return 0 on success and -errno on error. 670 522 */ 671 523 static int ntfs_read_locked_inode(struct inode *vi) 672 524 { 673 - ntfs_volume *vol = NTFS_SB(vi->i_sb); 674 - ntfs_inode *ni; 675 - struct inode *bvi; 676 - MFT_RECORD *m; 677 - ATTR_RECORD *a; 678 - STANDARD_INFORMATION *si; 679 - ntfs_attr_search_ctx *ctx; 525 + struct ntfs_volume *vol = NTFS_SB(vi->i_sb); 526 + struct ntfs_inode *ni; 527 + struct mft_record *m; 528 + struct attr_record *a; 529 + struct standard_information *si; 530 + struct ntfs_attr_search_ctx *ctx; 680 531 int err = 0; 532 + __le16 *name = I30; 533 + unsigned int name_len = 4, flags = 0; 534 + int extend_sys = 0; 535 + dev_t dev = 0; 536 + bool vol_err = true; 681 537 682 538 ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino); 683 539 684 - /* Setup the generic vfs inode parts now. */ 685 - vi->i_uid = vol->uid; 686 - vi->i_gid = vol->gid; 687 - vi->i_mode = 0; 540 + if (uid_valid(vol->uid)) { 541 + vi->i_uid = vol->uid; 542 + flags |= NTFS_VOL_UID; 543 + } else 544 + vi->i_uid = GLOBAL_ROOT_UID; 545 + 546 + if (gid_valid(vol->gid)) { 547 + vi->i_gid = vol->gid; 548 + flags |= NTFS_VOL_GID; 549 + } else 550 + vi->i_gid = GLOBAL_ROOT_GID; 551 + 552 + vi->i_mode = 0777; 688 553 689 554 /* 690 555 * Initialize the ntfs specific part of @vi special casing ··· 711 552 err = PTR_ERR(m); 712 553 goto err_out; 713 554 } 555 + 714 556 ctx = ntfs_attr_get_search_ctx(ni, m); 715 557 if (!ctx) { 716 558 err = -ENOMEM; ··· 719 559 } 720 560 721 561 if (!(m->flags & MFT_RECORD_IN_USE)) { 722 - ntfs_error(vi->i_sb, "Inode is not in use!"); 562 + err = -ENOENT; 563 + vol_err = false; 723 564 goto unm_err_out; 724 565 } 566 + 725 567 if (m->base_mft_record) { 726 568 ntfs_error(vi->i_sb, "Inode is an extent inode!"); 727 569 goto unm_err_out; ··· 732 570 /* Transfer information from mft record into vfs and ntfs inodes. */ 733 571 vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number); 734 572 735 - /* 736 - * FIXME: Keep in mind that link_count is two for files which have both 737 - * a long file name and a short file name as separate entries, so if 738 - * we are hiding short file names this will be too high. Either we need 739 - * to account for the short file names by subtracting them or we need 740 - * to make sure we delete files even though i_nlink is not zero which 741 - * might be tricky due to vfs interactions. Need to think about this 742 - * some more when implementing the unlink command. 743 - */ 573 + if (le16_to_cpu(m->link_count) < 1) { 574 + ntfs_error(vi->i_sb, "Inode link count is 0!"); 575 + goto unm_err_out; 576 + } 744 577 set_nlink(vi, le16_to_cpu(m->link_count)); 745 - /* 746 - * FIXME: Reparse points can have the directory bit set even though 747 - * they would be S_IFLNK. Need to deal with this further below when we 748 - * implement reparse points / symbolic links but it will do for now. 749 - * Also if not a directory, it could be something else, rather than 750 - * a regular file. But again, will do for now. 751 - */ 752 - /* Everyone gets all permissions. */ 753 - vi->i_mode |= S_IRWXUGO; 578 + 754 579 /* If read-only, no one gets write permissions. */ 755 580 if (IS_RDONLY(vi)) 756 - vi->i_mode &= ~S_IWUGO; 757 - if (m->flags & MFT_RECORD_IS_DIRECTORY) { 758 - vi->i_mode |= S_IFDIR; 759 - /* 760 - * Apply the directory permissions mask set in the mount 761 - * options. 762 - */ 763 - vi->i_mode &= ~vol->dmask; 764 - /* Things break without this kludge! */ 765 - if (vi->i_nlink > 1) 766 - set_nlink(vi, 1); 767 - } else { 768 - vi->i_mode |= S_IFREG; 769 - /* Apply the file permissions mask set in the mount options. */ 770 - vi->i_mode &= ~vol->fmask; 771 - } 581 + vi->i_mode &= ~0222; 582 + 772 583 /* 773 584 * Find the standard information attribute in the mft record. At this 774 585 * stage we haven't setup the attribute list stuff yet, so this could 775 586 * in fact fail if the standard information is in an extent record, but 776 587 * I don't think this actually ever happens. 777 588 */ 589 + ntfs_attr_reinit_search_ctx(ctx); 778 590 err = ntfs_attr_lookup(AT_STANDARD_INFORMATION, NULL, 0, 0, 0, NULL, 0, 779 591 ctx); 780 592 if (unlikely(err)) { 781 - if (err == -ENOENT) { 782 - /* 783 - * TODO: We should be performing a hot fix here (if the 784 - * recover mount option is set) by creating a new 785 - * attribute. 786 - */ 787 - ntfs_error(vi->i_sb, "$STANDARD_INFORMATION attribute " 788 - "is missing."); 789 - } 593 + if (err == -ENOENT) 594 + ntfs_error(vi->i_sb, "$STANDARD_INFORMATION attribute is missing."); 790 595 goto unm_err_out; 791 596 } 792 597 a = ctx->attr; ··· 764 635 ntfs_error(vi->i_sb, "Corrupt standard information attribute in inode."); 765 636 goto unm_err_out; 766 637 } 767 - si = (STANDARD_INFORMATION*)((u8*)a + 638 + si = (struct standard_information *)((u8 *)a + 768 639 le16_to_cpu(a->data.resident.value_offset)); 769 640 770 641 /* Transfer information from the standard information into vi. */ ··· 777 648 * mtime is the last change of the data within the file. Not changed 778 649 * when only metadata is changed, e.g. a rename doesn't affect mtime. 779 650 */ 651 + ni->i_crtime = ntfs2utc(si->creation_time); 652 + 780 653 inode_set_mtime_to_ts(vi, ntfs2utc(si->last_data_change_time)); 781 654 /* 782 655 * ctime is the last change of the metadata of the file. This obviously ··· 791 660 * for example but changed whenever the file is written to. 792 661 */ 793 662 inode_set_atime_to_ts(vi, ntfs2utc(si->last_access_time)); 663 + ni->flags = si->file_attributes; 794 664 795 665 /* Find the attribute list attribute if present. */ 796 666 ntfs_attr_reinit_search_ctx(ctx); 797 667 err = ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, 0, 0, NULL, 0, ctx); 798 668 if (err) { 799 669 if (unlikely(err != -ENOENT)) { 800 - ntfs_error(vi->i_sb, "Failed to lookup attribute list " 801 - "attribute."); 670 + ntfs_error(vi->i_sb, "Failed to lookup attribute list attribute."); 802 671 goto unm_err_out; 803 672 } 804 - } else /* if (!err) */ { 673 + } else { 805 674 if (vi->i_ino == FILE_MFT) 806 675 goto skip_attr_list_load; 807 676 ntfs_debug("Attribute list found in inode 0x%lx.", vi->i_ino); 808 677 NInoSetAttrList(ni); 809 678 a = ctx->attr; 810 679 if (a->flags & ATTR_COMPRESSION_MASK) { 811 - ntfs_error(vi->i_sb, "Attribute list attribute is " 812 - "compressed."); 680 + ntfs_error(vi->i_sb, 681 + "Attribute list attribute is compressed."); 813 682 goto unm_err_out; 814 683 } 815 684 if (a->flags & ATTR_IS_ENCRYPTED || 816 685 a->flags & ATTR_IS_SPARSE) { 817 686 if (a->non_resident) { 818 - ntfs_error(vi->i_sb, "Non-resident attribute " 819 - "list attribute is encrypted/" 820 - "sparse."); 687 + ntfs_error(vi->i_sb, 688 + "Non-resident attribute list attribute is encrypted/sparse."); 821 689 goto unm_err_out; 822 690 } 823 - ntfs_warning(vi->i_sb, "Resident attribute list " 824 - "attribute in inode 0x%lx is marked " 825 - "encrypted/sparse which is not true. " 826 - "However, Windows allows this and " 827 - "chkdsk does not detect or correct it " 828 - "so we will just ignore the invalid " 829 - "flags and pretend they are not set.", 830 - vi->i_ino); 691 + ntfs_warning(vi->i_sb, 692 + "Resident attribute list attribute in inode 0x%lx is marked encrypted/sparse which is not true. However, Windows allows this and chkdsk does not detect or correct it so we will just ignore the invalid flags and pretend they are not set.", 693 + vi->i_ino); 831 694 } 832 695 /* Now allocate memory for the attribute list. */ 833 696 ni->attr_list_size = (u32)ntfs_attr_size(a); 834 - ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size); 697 + if (!ni->attr_list_size) { 698 + ntfs_error(vi->i_sb, "Attr_list_size is zero"); 699 + goto unm_err_out; 700 + } 701 + ni->attr_list = kvzalloc(ni->attr_list_size, GFP_NOFS); 835 702 if (!ni->attr_list) { 836 - ntfs_error(vi->i_sb, "Not enough memory to allocate " 837 - "buffer for attribute list."); 703 + ntfs_error(vi->i_sb, 704 + "Not enough memory to allocate buffer for attribute list."); 838 705 err = -ENOMEM; 839 706 goto unm_err_out; 840 707 } 841 708 if (a->non_resident) { 842 709 NInoSetAttrListNonResident(ni); 843 710 if (a->data.non_resident.lowest_vcn) { 844 - ntfs_error(vi->i_sb, "Attribute list has non " 845 - "zero lowest_vcn."); 711 + ntfs_error(vi->i_sb, "Attribute list has non zero lowest_vcn."); 846 712 goto unm_err_out; 847 713 } 848 - /* 849 - * Setup the runlist. No need for locking as we have 850 - * exclusive access to the inode at this time. 851 - */ 852 - ni->attr_list_rl.rl = ntfs_mapping_pairs_decompress(vol, 853 - a, NULL); 854 - if (IS_ERR(ni->attr_list_rl.rl)) { 855 - err = PTR_ERR(ni->attr_list_rl.rl); 856 - ni->attr_list_rl.rl = NULL; 857 - ntfs_error(vi->i_sb, "Mapping pairs " 858 - "decompression failed."); 859 - goto unm_err_out; 860 - } 714 + 861 715 /* Now load the attribute list. */ 862 - if ((err = load_attribute_list(vol, &ni->attr_list_rl, 863 - ni->attr_list, ni->attr_list_size, 864 - sle64_to_cpu(a->data.non_resident. 865 - initialized_size)))) { 866 - ntfs_error(vi->i_sb, "Failed to load " 867 - "attribute list attribute."); 716 + err = load_attribute_list(ni, ni->attr_list, ni->attr_list_size); 717 + if (err) { 718 + ntfs_error(vi->i_sb, "Failed to load attribute list attribute."); 868 719 goto unm_err_out; 869 720 } 870 721 } else /* if (!a->non_resident) */ { 871 - if ((u8*)a + le16_to_cpu(a->data.resident.value_offset) 722 + if ((u8 *)a + le16_to_cpu(a->data.resident.value_offset) 872 723 + le32_to_cpu( 873 724 a->data.resident.value_length) > 874 - (u8*)ctx->mrec + vol->mft_record_size) { 875 - ntfs_error(vi->i_sb, "Corrupt attribute list " 876 - "in inode."); 725 + (u8 *)ctx->mrec + vol->mft_record_size) { 726 + ntfs_error(vi->i_sb, "Corrupt attribute list in inode."); 877 727 goto unm_err_out; 878 728 } 879 729 /* Now copy the attribute list. */ 880 - memcpy(ni->attr_list, (u8*)a + le16_to_cpu( 730 + memcpy(ni->attr_list, (u8 *)a + le16_to_cpu( 881 731 a->data.resident.value_offset), 882 732 le32_to_cpu( 883 733 a->data.resident.value_length)); 884 734 } 885 735 } 886 736 skip_attr_list_load: 737 + err = ntfs_attr_lookup(AT_EA_INFORMATION, NULL, 0, 0, 0, NULL, 0, ctx); 738 + if (!err) 739 + NInoSetHasEA(ni); 740 + 741 + ntfs_ea_get_wsl_inode(vi, &dev, flags); 742 + 743 + if (m->flags & MFT_RECORD_IS_DIRECTORY) { 744 + vi->i_mode |= S_IFDIR; 745 + /* 746 + * Apply the directory permissions mask set in the mount 747 + * options. 748 + */ 749 + vi->i_mode &= ~vol->dmask; 750 + /* Things break without this kludge! */ 751 + if (vi->i_nlink > 1) 752 + set_nlink(vi, 1); 753 + } else { 754 + if (ni->flags & FILE_ATTR_REPARSE_POINT) { 755 + unsigned int mode; 756 + 757 + mode = ntfs_make_symlink(ni); 758 + if (mode) 759 + vi->i_mode |= mode; 760 + else { 761 + vi->i_mode &= ~S_IFLNK; 762 + vi->i_mode |= S_IFREG; 763 + } 764 + } else 765 + vi->i_mode |= S_IFREG; 766 + /* Apply the file permissions mask set in the mount options. */ 767 + vi->i_mode &= ~vol->fmask; 768 + } 769 + 887 770 /* 888 771 * If an attribute list is present we now have the attribute list value 889 772 * in ntfs_ino->attr_list and it is ntfs_ino->attr_list_size bytes. 890 773 */ 891 774 if (S_ISDIR(vi->i_mode)) { 892 - loff_t bvi_size; 893 - ntfs_inode *bni; 894 - INDEX_ROOT *ir; 775 + struct index_root *ir; 895 776 u8 *ir_end, *index_end; 896 777 778 + view_index_meta: 897 779 /* It is a directory, find index root attribute. */ 898 780 ntfs_attr_reinit_search_ctx(ctx); 899 - err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 781 + err = ntfs_attr_lookup(AT_INDEX_ROOT, name, name_len, CASE_SENSITIVE, 900 782 0, NULL, 0, ctx); 901 783 if (unlikely(err)) { 902 - if (err == -ENOENT) { 903 - // FIXME: File is corrupt! Hot-fix with empty 904 - // index root attribute if recovery option is 905 - // set. 906 - ntfs_error(vi->i_sb, "$INDEX_ROOT attribute " 907 - "is missing."); 908 - } 784 + if (err == -ENOENT) 785 + ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is missing."); 909 786 goto unm_err_out; 910 787 } 911 788 a = ctx->attr; 912 789 /* Set up the state. */ 913 790 if (unlikely(a->non_resident)) { 914 - ntfs_error(vol->sb, "$INDEX_ROOT attribute is not " 915 - "resident."); 791 + ntfs_error(vol->sb, 792 + "$INDEX_ROOT attribute is not resident."); 916 793 goto unm_err_out; 917 794 } 918 795 /* Ensure the attribute name is placed before the value. */ 919 796 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >= 920 797 le16_to_cpu(a->data.resident.value_offset)))) { 921 - ntfs_error(vol->sb, "$INDEX_ROOT attribute name is " 922 - "placed after the attribute value."); 798 + ntfs_error(vol->sb, 799 + "$INDEX_ROOT attribute name is placed after the attribute value."); 923 800 goto unm_err_out; 924 801 } 925 802 /* ··· 936 797 * encrypted. However index root cannot be both compressed and 937 798 * encrypted. 938 799 */ 939 - if (a->flags & ATTR_COMPRESSION_MASK) 800 + if (a->flags & ATTR_COMPRESSION_MASK) { 940 801 NInoSetCompressed(ni); 802 + ni->flags |= FILE_ATTR_COMPRESSED; 803 + } 941 804 if (a->flags & ATTR_IS_ENCRYPTED) { 942 805 if (a->flags & ATTR_COMPRESSION_MASK) { 943 - ntfs_error(vi->i_sb, "Found encrypted and " 944 - "compressed attribute."); 806 + ntfs_error(vi->i_sb, "Found encrypted and compressed attribute."); 945 807 goto unm_err_out; 946 808 } 947 809 NInoSetEncrypted(ni); 810 + ni->flags |= FILE_ATTR_ENCRYPTED; 948 811 } 949 - if (a->flags & ATTR_IS_SPARSE) 812 + if (a->flags & ATTR_IS_SPARSE) { 950 813 NInoSetSparse(ni); 951 - ir = (INDEX_ROOT*)((u8*)a + 814 + ni->flags |= FILE_ATTR_SPARSE_FILE; 815 + } 816 + ir = (struct index_root *)((u8 *)a + 952 817 le16_to_cpu(a->data.resident.value_offset)); 953 - ir_end = (u8*)ir + le32_to_cpu(a->data.resident.value_length); 954 - if (ir_end > (u8*)ctx->mrec + vol->mft_record_size) { 955 - ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is " 956 - "corrupt."); 818 + ir_end = (u8 *)ir + le32_to_cpu(a->data.resident.value_length); 819 + if (ir_end > (u8 *)ctx->mrec + vol->mft_record_size) { 820 + ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is corrupt."); 957 821 goto unm_err_out; 958 822 } 959 - index_end = (u8*)&ir->index + 823 + index_end = (u8 *)&ir->index + 960 824 le32_to_cpu(ir->index.index_length); 961 825 if (index_end > ir_end) { 962 826 ntfs_error(vi->i_sb, "Directory index is corrupt."); 963 827 goto unm_err_out; 964 828 } 965 - if (ir->type != AT_FILE_NAME) { 966 - ntfs_error(vi->i_sb, "Indexed attribute is not " 967 - "$FILE_NAME."); 968 - goto unm_err_out; 829 + 830 + if (extend_sys) { 831 + if (ir->type) { 832 + ntfs_error(vi->i_sb, "Indexed attribute is not zero."); 833 + goto unm_err_out; 834 + } 835 + } else { 836 + if (ir->type != AT_FILE_NAME) { 837 + ntfs_error(vi->i_sb, "Indexed attribute is not $FILE_NAME."); 838 + goto unm_err_out; 839 + } 840 + 841 + if (ir->collation_rule != COLLATION_FILE_NAME) { 842 + ntfs_error(vi->i_sb, 843 + "Index collation rule is not COLLATION_FILE_NAME."); 844 + goto unm_err_out; 845 + } 969 846 } 970 - if (ir->collation_rule != COLLATION_FILE_NAME) { 971 - ntfs_error(vi->i_sb, "Index collation rule is not " 972 - "COLLATION_FILE_NAME."); 973 - goto unm_err_out; 974 - } 847 + 975 848 ni->itype.index.collation_rule = ir->collation_rule; 976 849 ni->itype.index.block_size = le32_to_cpu(ir->index_block_size); 977 850 if (ni->itype.index.block_size & 978 851 (ni->itype.index.block_size - 1)) { 979 - ntfs_error(vi->i_sb, "Index block size (%u) is not a " 980 - "power of two.", 852 + ntfs_error(vi->i_sb, "Index block size (%u) is not a power of two.", 981 853 ni->itype.index.block_size); 982 854 goto unm_err_out; 983 855 } 984 856 if (ni->itype.index.block_size > PAGE_SIZE) { 985 - ntfs_error(vi->i_sb, "Index block size (%u) > " 986 - "PAGE_SIZE (%ld) is not " 987 - "supported. Sorry.", 988 - ni->itype.index.block_size, 989 - PAGE_SIZE); 857 + ntfs_error(vi->i_sb, 858 + "Index block size (%u) > PAGE_SIZE (%ld) is not supported.", 859 + ni->itype.index.block_size, 860 + PAGE_SIZE); 990 861 err = -EOPNOTSUPP; 991 862 goto unm_err_out; 992 863 } 993 864 if (ni->itype.index.block_size < NTFS_BLOCK_SIZE) { 994 - ntfs_error(vi->i_sb, "Index block size (%u) < " 995 - "NTFS_BLOCK_SIZE (%i) is not " 996 - "supported. Sorry.", 997 - ni->itype.index.block_size, 998 - NTFS_BLOCK_SIZE); 865 + ntfs_error(vi->i_sb, 866 + "Index block size (%u) < NTFS_BLOCK_SIZE (%i) is not supported.", 867 + ni->itype.index.block_size, 868 + NTFS_BLOCK_SIZE); 999 869 err = -EOPNOTSUPP; 1000 870 goto unm_err_out; 1001 871 } ··· 1020 872 } 1021 873 1022 874 /* Setup the index allocation attribute, even if not present. */ 1023 - NInoSetMstProtected(ni); 1024 - ni->type = AT_INDEX_ALLOCATION; 1025 - ni->name = I30; 1026 - ni->name_len = 4; 1027 - 1028 - if (!(ir->index.flags & LARGE_INDEX)) { 1029 - /* No index allocation. */ 1030 - vi->i_size = ni->initialized_size = 1031 - ni->allocated_size = 0; 1032 - /* We are done with the mft record, so we release it. */ 1033 - ntfs_attr_put_search_ctx(ctx); 1034 - unmap_mft_record(ni); 1035 - m = NULL; 1036 - ctx = NULL; 1037 - goto skip_large_dir_stuff; 1038 - } /* LARGE_INDEX: Index allocation present. Setup state. */ 1039 - NInoSetIndexAllocPresent(ni); 1040 - /* Find index allocation attribute. */ 1041 - ntfs_attr_reinit_search_ctx(ctx); 1042 - err = ntfs_attr_lookup(AT_INDEX_ALLOCATION, I30, 4, 1043 - CASE_SENSITIVE, 0, NULL, 0, ctx); 1044 - if (unlikely(err)) { 1045 - if (err == -ENOENT) 1046 - ntfs_error(vi->i_sb, "$INDEX_ALLOCATION " 1047 - "attribute is not present but " 1048 - "$INDEX_ROOT indicated it is."); 1049 - else 1050 - ntfs_error(vi->i_sb, "Failed to lookup " 1051 - "$INDEX_ALLOCATION " 1052 - "attribute."); 1053 - goto unm_err_out; 1054 - } 1055 - a = ctx->attr; 1056 - if (!a->non_resident) { 1057 - ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute " 1058 - "is resident."); 1059 - goto unm_err_out; 1060 - } 1061 - /* 1062 - * Ensure the attribute name is placed before the mapping pairs 1063 - * array. 1064 - */ 1065 - if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >= 1066 - le16_to_cpu( 1067 - a->data.non_resident.mapping_pairs_offset)))) { 1068 - ntfs_error(vol->sb, "$INDEX_ALLOCATION attribute name " 1069 - "is placed after the mapping pairs " 1070 - "array."); 1071 - goto unm_err_out; 1072 - } 1073 - if (a->flags & ATTR_IS_ENCRYPTED) { 1074 - ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute " 1075 - "is encrypted."); 1076 - goto unm_err_out; 1077 - } 1078 - if (a->flags & ATTR_IS_SPARSE) { 1079 - ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute " 1080 - "is sparse."); 1081 - goto unm_err_out; 1082 - } 1083 - if (a->flags & ATTR_COMPRESSION_MASK) { 1084 - ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute " 1085 - "is compressed."); 1086 - goto unm_err_out; 1087 - } 1088 - if (a->data.non_resident.lowest_vcn) { 1089 - ntfs_error(vi->i_sb, "First extent of " 1090 - "$INDEX_ALLOCATION attribute has non " 1091 - "zero lowest_vcn."); 1092 - goto unm_err_out; 1093 - } 1094 - vi->i_size = sle64_to_cpu(a->data.non_resident.data_size); 1095 - ni->initialized_size = sle64_to_cpu( 1096 - a->data.non_resident.initialized_size); 1097 - ni->allocated_size = sle64_to_cpu( 1098 - a->data.non_resident.allocated_size); 1099 - /* 1100 - * We are done with the mft record, so we release it. Otherwise 1101 - * we would deadlock in ntfs_attr_iget(). 1102 - */ 875 + ni->type = AT_INDEX_ROOT; 876 + ni->name = name; 877 + ni->name_len = name_len; 878 + vi->i_size = ni->initialized_size = ni->data_size = 879 + le32_to_cpu(a->data.resident.value_length); 880 + ni->allocated_size = (ni->data_size + 7) & ~7; 881 + /* We are done with the mft record, so we release it. */ 1103 882 ntfs_attr_put_search_ctx(ctx); 1104 883 unmap_mft_record(ni); 1105 884 m = NULL; 1106 885 ctx = NULL; 1107 - /* Get the index bitmap attribute inode. */ 1108 - bvi = ntfs_attr_iget(vi, AT_BITMAP, I30, 4); 1109 - if (IS_ERR(bvi)) { 1110 - ntfs_error(vi->i_sb, "Failed to get bitmap attribute."); 1111 - err = PTR_ERR(bvi); 1112 - goto unm_err_out; 1113 - } 1114 - bni = NTFS_I(bvi); 1115 - if (NInoCompressed(bni) || NInoEncrypted(bni) || 1116 - NInoSparse(bni)) { 1117 - ntfs_error(vi->i_sb, "$BITMAP attribute is compressed " 1118 - "and/or encrypted and/or sparse."); 1119 - goto iput_unm_err_out; 1120 - } 1121 - /* Consistency check bitmap size vs. index allocation size. */ 1122 - bvi_size = i_size_read(bvi); 1123 - if ((bvi_size << 3) < (vi->i_size >> 1124 - ni->itype.index.block_size_bits)) { 1125 - ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) " 1126 - "for index allocation (0x%llx).", 1127 - bvi_size << 3, vi->i_size); 1128 - goto iput_unm_err_out; 1129 - } 1130 - /* No longer need the bitmap attribute inode. */ 1131 - iput(bvi); 1132 - skip_large_dir_stuff: 1133 886 /* Setup the operations for this inode. */ 1134 - vi->i_op = &ntfs_dir_inode_ops; 1135 - vi->i_fop = &ntfs_dir_ops; 1136 - vi->i_mapping->a_ops = &ntfs_mst_aops; 887 + ntfs_set_vfs_operations(vi, S_IFDIR, 0); 888 + if (ir->index.flags & LARGE_INDEX) 889 + NInoSetIndexAllocPresent(ni); 1137 890 } else { 1138 891 /* It is a file. */ 1139 892 ntfs_attr_reinit_search_ctx(ctx); 1140 893 1141 894 /* Setup the data attribute, even if not present. */ 1142 895 ni->type = AT_DATA; 1143 - ni->name = NULL; 896 + ni->name = AT_UNNAMED; 1144 897 ni->name_len = 0; 1145 898 1146 899 /* Find first extent of the unnamed data attribute. */ ··· 1050 1001 vi->i_size = ni->initialized_size = 1051 1002 ni->allocated_size = 0; 1052 1003 if (err != -ENOENT) { 1053 - ntfs_error(vi->i_sb, "Failed to lookup $DATA " 1054 - "attribute."); 1004 + ntfs_error(vi->i_sb, "Failed to lookup $DATA attribute."); 1055 1005 goto unm_err_out; 1056 1006 } 1057 1007 /* ··· 1068 1020 * name of this inode from the mft record as the name 1069 1021 * contains the back reference to the parent directory. 1070 1022 */ 1071 - if (ntfs_is_extended_system_file(ctx) > 0) 1023 + extend_sys = ntfs_is_extended_system_file(ctx); 1024 + if (extend_sys > 0) { 1025 + if (m->flags & MFT_RECORD_IS_VIEW_INDEX) { 1026 + if (extend_sys == 2) { 1027 + name = reparse_index_name; 1028 + name_len = 2; 1029 + goto view_index_meta; 1030 + } else if (extend_sys == 3) { 1031 + name = objid_index_name; 1032 + name_len = 2; 1033 + goto view_index_meta; 1034 + } 1035 + } 1072 1036 goto no_data_attr_special_case; 1073 - // FIXME: File is corrupt! Hot-fix with empty data 1074 - // attribute if recovery option is set. 1075 - ntfs_error(vi->i_sb, "$DATA attribute is missing."); 1037 + } 1038 + 1039 + err = extend_sys; 1040 + ntfs_error(vi->i_sb, "$DATA attribute is missing, err : %d", err); 1076 1041 goto unm_err_out; 1077 1042 } 1078 1043 a = ctx->attr; ··· 1093 1032 if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE)) { 1094 1033 if (a->flags & ATTR_COMPRESSION_MASK) { 1095 1034 NInoSetCompressed(ni); 1035 + ni->flags |= FILE_ATTR_COMPRESSED; 1096 1036 if (vol->cluster_size > 4096) { 1097 - ntfs_error(vi->i_sb, "Found " 1098 - "compressed data but " 1099 - "compression is " 1100 - "disabled due to " 1101 - "cluster size (%i) > " 1102 - "4kiB.", 1103 - vol->cluster_size); 1037 + ntfs_error(vi->i_sb, 1038 + "Found compressed data but compression is disabled due to cluster size (%i) > 4kiB.", 1039 + vol->cluster_size); 1104 1040 goto unm_err_out; 1105 1041 } 1106 1042 if ((a->flags & ATTR_COMPRESSION_MASK) 1107 1043 != ATTR_IS_COMPRESSED) { 1108 - ntfs_error(vi->i_sb, "Found unknown " 1109 - "compression method " 1110 - "or corrupt file."); 1044 + ntfs_error(vi->i_sb, 1045 + "Found unknown compression method or corrupt file."); 1111 1046 goto unm_err_out; 1112 1047 } 1113 1048 } 1114 - if (a->flags & ATTR_IS_SPARSE) 1049 + if (a->flags & ATTR_IS_SPARSE) { 1115 1050 NInoSetSparse(ni); 1051 + ni->flags |= FILE_ATTR_SPARSE_FILE; 1052 + } 1116 1053 } 1117 1054 if (a->flags & ATTR_IS_ENCRYPTED) { 1118 1055 if (NInoCompressed(ni)) { 1119 - ntfs_error(vi->i_sb, "Found encrypted and " 1120 - "compressed data."); 1056 + ntfs_error(vi->i_sb, "Found encrypted and compressed data."); 1121 1057 goto unm_err_out; 1122 1058 } 1123 1059 NInoSetEncrypted(ni); 1060 + ni->flags |= FILE_ATTR_ENCRYPTED; 1124 1061 } 1125 1062 if (a->non_resident) { 1126 1063 NInoSetNonResident(ni); 1127 1064 if (NInoCompressed(ni) || NInoSparse(ni)) { 1128 - if (NInoCompressed(ni) && a->data.non_resident. 1129 - compression_unit != 4) { 1130 - ntfs_error(vi->i_sb, "Found " 1131 - "non-standard " 1132 - "compression unit (%u " 1133 - "instead of 4). " 1134 - "Cannot handle this.", 1135 - a->data.non_resident. 1136 - compression_unit); 1065 + if (NInoCompressed(ni) && 1066 + a->data.non_resident.compression_unit != 4) { 1067 + ntfs_error(vi->i_sb, 1068 + "Found non-standard compression unit (%u instead of 4). Cannot handle this.", 1069 + a->data.non_resident.compression_unit); 1137 1070 err = -EOPNOTSUPP; 1138 1071 goto unm_err_out; 1139 1072 } 1073 + 1074 + if (NInoSparse(ni) && 1075 + a->data.non_resident.compression_unit && 1076 + a->data.non_resident.compression_unit != 1077 + vol->sparse_compression_unit) { 1078 + ntfs_error(vi->i_sb, 1079 + "Found non-standard compression unit (%u instead of 0 or %d). Cannot handle this.", 1080 + a->data.non_resident.compression_unit, 1081 + vol->sparse_compression_unit); 1082 + err = -EOPNOTSUPP; 1083 + goto unm_err_out; 1084 + } 1085 + 1086 + 1140 1087 if (a->data.non_resident.compression_unit) { 1141 1088 ni->itype.compressed.block_size = 1U << 1142 - (a->data.non_resident. 1143 - compression_unit + 1089 + (a->data.non_resident.compression_unit + 1144 1090 vol->cluster_size_bits); 1145 1091 ni->itype.compressed.block_size_bits = 1146 - ffs(ni->itype. 1147 - compressed. 1148 - block_size) - 1; 1092 + ffs(ni->itype.compressed.block_size) - 1; 1149 1093 ni->itype.compressed.block_clusters = 1150 - 1U << a->data. 1151 - non_resident. 1152 - compression_unit; 1094 + 1U << a->data.non_resident.compression_unit; 1153 1095 } else { 1154 1096 ni->itype.compressed.block_size = 0; 1155 1097 ni->itype.compressed.block_size_bits = ··· 1160 1096 ni->itype.compressed.block_clusters = 1161 1097 0; 1162 1098 } 1163 - ni->itype.compressed.size = sle64_to_cpu( 1164 - a->data.non_resident. 1165 - compressed_size); 1099 + ni->itype.compressed.size = le64_to_cpu( 1100 + a->data.non_resident.compressed_size); 1166 1101 } 1167 1102 if (a->data.non_resident.lowest_vcn) { 1168 - ntfs_error(vi->i_sb, "First extent of $DATA " 1169 - "attribute has non zero " 1170 - "lowest_vcn."); 1103 + ntfs_error(vi->i_sb, 1104 + "First extent of $DATA attribute has non zero lowest_vcn."); 1171 1105 goto unm_err_out; 1172 1106 } 1173 - vi->i_size = sle64_to_cpu( 1174 - a->data.non_resident.data_size); 1175 - ni->initialized_size = sle64_to_cpu( 1176 - a->data.non_resident.initialized_size); 1177 - ni->allocated_size = sle64_to_cpu( 1178 - a->data.non_resident.allocated_size); 1107 + vi->i_size = ni->data_size = le64_to_cpu(a->data.non_resident.data_size); 1108 + ni->initialized_size = le64_to_cpu(a->data.non_resident.initialized_size); 1109 + ni->allocated_size = le64_to_cpu(a->data.non_resident.allocated_size); 1179 1110 } else { /* Resident attribute. */ 1180 - vi->i_size = ni->initialized_size = le32_to_cpu( 1111 + vi->i_size = ni->data_size = ni->initialized_size = le32_to_cpu( 1181 1112 a->data.resident.value_length); 1182 1113 ni->allocated_size = le32_to_cpu(a->length) - 1183 1114 le16_to_cpu( 1184 1115 a->data.resident.value_offset); 1185 1116 if (vi->i_size > ni->allocated_size) { 1186 - ntfs_error(vi->i_sb, "Resident data attribute " 1187 - "is corrupt (size exceeds " 1188 - "allocation)."); 1117 + ntfs_error(vi->i_sb, 1118 + "Resident data attribute is corrupt (size exceeds allocation)."); 1189 1119 goto unm_err_out; 1190 1120 } 1191 1121 } ··· 1190 1132 m = NULL; 1191 1133 ctx = NULL; 1192 1134 /* Setup the operations for this inode. */ 1193 - vi->i_op = &ntfs_file_inode_ops; 1194 - vi->i_fop = &ntfs_file_ops; 1195 - vi->i_mapping->a_ops = &ntfs_normal_aops; 1196 - if (NInoMstProtected(ni)) 1197 - vi->i_mapping->a_ops = &ntfs_mst_aops; 1198 - else if (NInoCompressed(ni)) 1199 - vi->i_mapping->a_ops = &ntfs_compressed_aops; 1135 + ntfs_set_vfs_operations(vi, vi->i_mode, dev); 1200 1136 } 1137 + 1138 + if (NVolSysImmutable(vol) && (ni->flags & FILE_ATTR_SYSTEM) && 1139 + !S_ISFIFO(vi->i_mode) && !S_ISSOCK(vi->i_mode) && !S_ISLNK(vi->i_mode)) 1140 + vi->i_flags |= S_IMMUTABLE; 1141 + 1201 1142 /* 1202 1143 * The number of 512-byte blocks used on disk (for stat). This is in so 1203 1144 * far inaccurate as it doesn't account for any named streams or other ··· 1212 1155 vi->i_blocks = ni->itype.compressed.size >> 9; 1213 1156 else 1214 1157 vi->i_blocks = ni->allocated_size >> 9; 1158 + 1215 1159 ntfs_debug("Done."); 1216 1160 return 0; 1217 - iput_unm_err_out: 1218 - iput(bvi); 1219 1161 unm_err_out: 1220 1162 if (!err) 1221 1163 err = -EIO; ··· 1223 1167 if (m) 1224 1168 unmap_mft_record(ni); 1225 1169 err_out: 1226 - ntfs_error(vol->sb, "Failed with error code %i. Marking corrupt " 1227 - "inode 0x%lx as bad. Run chkdsk.", err, vi->i_ino); 1228 - make_bad_inode(vi); 1229 - if (err != -EOPNOTSUPP && err != -ENOMEM) 1170 + if (err != -EOPNOTSUPP && err != -ENOMEM && vol_err == true) { 1171 + ntfs_error(vol->sb, 1172 + "Failed with error code %i. Marking corrupt inode 0x%lx as bad. Run chkdsk.", 1173 + err, vi->i_ino); 1230 1174 NVolSetErrors(vol); 1175 + } 1231 1176 return err; 1232 1177 } 1233 1178 1234 - /** 1179 + /* 1235 1180 * ntfs_read_locked_attr_inode - read an attribute inode from its base inode 1236 1181 * @base_vi: base inode 1237 1182 * @vi: attribute inode to read ··· 1249 1192 * A: i_state has I_NEW set, hence the inode is locked, also 1250 1193 * i_count is set to 1, so it is not going to go away 1251 1194 * 1252 - * Return 0 on success and -errno on error. In the error case, the inode will 1253 - * have had make_bad_inode() executed on it. 1195 + * Return 0 on success and -errno on error. 1254 1196 * 1255 1197 * Note this cannot be called for AT_INDEX_ALLOCATION. 1256 1198 */ 1257 1199 static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi) 1258 1200 { 1259 - ntfs_volume *vol = NTFS_SB(vi->i_sb); 1260 - ntfs_inode *ni, *base_ni; 1261 - MFT_RECORD *m; 1262 - ATTR_RECORD *a; 1263 - ntfs_attr_search_ctx *ctx; 1201 + struct ntfs_volume *vol = NTFS_SB(vi->i_sb); 1202 + struct ntfs_inode *ni = NTFS_I(vi), *base_ni = NTFS_I(base_vi); 1203 + struct mft_record *m; 1204 + struct attr_record *a; 1205 + struct ntfs_attr_search_ctx *ctx; 1264 1206 int err = 0; 1265 1207 1266 1208 ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino); 1267 1209 1268 1210 ntfs_init_big_inode(vi); 1269 - 1270 - ni = NTFS_I(vi); 1271 - base_ni = NTFS_I(base_vi); 1272 1211 1273 1212 /* Just mirror the values from the base inode. */ 1274 1213 vi->i_uid = base_vi->i_uid; ··· 1297 1244 if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE)) { 1298 1245 if (a->flags & ATTR_COMPRESSION_MASK) { 1299 1246 NInoSetCompressed(ni); 1247 + ni->flags |= FILE_ATTR_COMPRESSED; 1300 1248 if ((ni->type != AT_DATA) || (ni->type == AT_DATA && 1301 1249 ni->name_len)) { 1302 - ntfs_error(vi->i_sb, "Found compressed " 1303 - "non-data or named data " 1304 - "attribute. Please report " 1305 - "you saw this message to " 1306 - "linux-ntfs-dev@lists." 1307 - "sourceforge.net"); 1250 + ntfs_error(vi->i_sb, 1251 + "Found compressed non-data or named data attribute."); 1308 1252 goto unm_err_out; 1309 1253 } 1310 1254 if (vol->cluster_size > 4096) { 1311 - ntfs_error(vi->i_sb, "Found compressed " 1312 - "attribute but compression is " 1313 - "disabled due to cluster size " 1314 - "(%i) > 4kiB.", 1315 - vol->cluster_size); 1255 + ntfs_error(vi->i_sb, 1256 + "Found compressed attribute but compression is disabled due to cluster size (%i) > 4kiB.", 1257 + vol->cluster_size); 1316 1258 goto unm_err_out; 1317 1259 } 1318 1260 if ((a->flags & ATTR_COMPRESSION_MASK) != 1319 1261 ATTR_IS_COMPRESSED) { 1320 - ntfs_error(vi->i_sb, "Found unknown " 1321 - "compression method."); 1262 + ntfs_error(vi->i_sb, "Found unknown compression method."); 1322 1263 goto unm_err_out; 1323 1264 } 1324 1265 } ··· 1321 1274 * to compress all files. 1322 1275 */ 1323 1276 if (NInoMstProtected(ni) && ni->type != AT_INDEX_ROOT) { 1324 - ntfs_error(vi->i_sb, "Found mst protected attribute " 1325 - "but the attribute is %s. Please " 1326 - "report you saw this message to " 1327 - "linux-ntfs-dev@lists.sourceforge.net", 1328 - NInoCompressed(ni) ? "compressed" : 1329 - "sparse"); 1277 + ntfs_error(vi->i_sb, 1278 + "Found mst protected attribute but the attribute is %s.", 1279 + NInoCompressed(ni) ? "compressed" : "sparse"); 1330 1280 goto unm_err_out; 1331 1281 } 1332 - if (a->flags & ATTR_IS_SPARSE) 1282 + if (a->flags & ATTR_IS_SPARSE) { 1333 1283 NInoSetSparse(ni); 1284 + ni->flags |= FILE_ATTR_SPARSE_FILE; 1285 + } 1334 1286 } 1335 1287 if (a->flags & ATTR_IS_ENCRYPTED) { 1336 1288 if (NInoCompressed(ni)) { 1337 - ntfs_error(vi->i_sb, "Found encrypted and compressed " 1338 - "data."); 1289 + ntfs_error(vi->i_sb, "Found encrypted and compressed data."); 1339 1290 goto unm_err_out; 1340 1291 } 1341 1292 /* ··· 1341 1296 * encrypt all files. 1342 1297 */ 1343 1298 if (NInoMstProtected(ni) && ni->type != AT_INDEX_ROOT) { 1344 - ntfs_error(vi->i_sb, "Found mst protected attribute " 1345 - "but the attribute is encrypted. " 1346 - "Please report you saw this message " 1347 - "to linux-ntfs-dev@lists.sourceforge." 1348 - "net"); 1299 + ntfs_error(vi->i_sb, 1300 + "Found mst protected attribute but the attribute is encrypted."); 1349 1301 goto unm_err_out; 1350 1302 } 1351 1303 if (ni->type != AT_DATA) { 1352 - ntfs_error(vi->i_sb, "Found encrypted non-data " 1353 - "attribute."); 1304 + ntfs_error(vi->i_sb, 1305 + "Found encrypted non-data attribute."); 1354 1306 goto unm_err_out; 1355 1307 } 1356 1308 NInoSetEncrypted(ni); 1309 + ni->flags |= FILE_ATTR_ENCRYPTED; 1357 1310 } 1358 1311 if (!a->non_resident) { 1359 1312 /* Ensure the attribute name is placed before the value. */ 1360 1313 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >= 1361 1314 le16_to_cpu(a->data.resident.value_offset)))) { 1362 - ntfs_error(vol->sb, "Attribute name is placed after " 1363 - "the attribute value."); 1315 + ntfs_error(vol->sb, 1316 + "Attribute name is placed after the attribute value."); 1364 1317 goto unm_err_out; 1365 1318 } 1366 1319 if (NInoMstProtected(ni)) { 1367 - ntfs_error(vi->i_sb, "Found mst protected attribute " 1368 - "but the attribute is resident. " 1369 - "Please report you saw this message to " 1370 - "linux-ntfs-dev@lists.sourceforge.net"); 1320 + ntfs_error(vi->i_sb, 1321 + "Found mst protected attribute but the attribute is resident."); 1371 1322 goto unm_err_out; 1372 1323 } 1373 - vi->i_size = ni->initialized_size = le32_to_cpu( 1324 + vi->i_size = ni->initialized_size = ni->data_size = le32_to_cpu( 1374 1325 a->data.resident.value_length); 1375 1326 ni->allocated_size = le32_to_cpu(a->length) - 1376 1327 le16_to_cpu(a->data.resident.value_offset); 1377 1328 if (vi->i_size > ni->allocated_size) { 1378 - ntfs_error(vi->i_sb, "Resident attribute is corrupt " 1379 - "(size exceeds allocation)."); 1329 + ntfs_error(vi->i_sb, 1330 + "Resident attribute is corrupt (size exceeds allocation)."); 1380 1331 goto unm_err_out; 1381 1332 } 1382 1333 } else { ··· 1384 1343 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >= 1385 1344 le16_to_cpu( 1386 1345 a->data.non_resident.mapping_pairs_offset)))) { 1387 - ntfs_error(vol->sb, "Attribute name is placed after " 1388 - "the mapping pairs array."); 1346 + ntfs_error(vol->sb, 1347 + "Attribute name is placed after the mapping pairs array."); 1389 1348 goto unm_err_out; 1390 1349 } 1391 1350 if (NInoCompressed(ni) || NInoSparse(ni)) { 1392 - if (NInoCompressed(ni) && a->data.non_resident. 1393 - compression_unit != 4) { 1394 - ntfs_error(vi->i_sb, "Found non-standard " 1395 - "compression unit (%u instead " 1396 - "of 4). Cannot handle this.", 1397 - a->data.non_resident. 1398 - compression_unit); 1351 + if (NInoCompressed(ni) && a->data.non_resident.compression_unit != 4) { 1352 + ntfs_error(vi->i_sb, 1353 + "Found non-standard compression unit (%u instead of 4). Cannot handle this.", 1354 + a->data.non_resident.compression_unit); 1399 1355 err = -EOPNOTSUPP; 1400 1356 goto unm_err_out; 1401 1357 } 1402 1358 if (a->data.non_resident.compression_unit) { 1403 1359 ni->itype.compressed.block_size = 1U << 1404 - (a->data.non_resident. 1405 - compression_unit + 1360 + (a->data.non_resident.compression_unit + 1406 1361 vol->cluster_size_bits); 1407 1362 ni->itype.compressed.block_size_bits = 1408 - ffs(ni->itype.compressed. 1409 - block_size) - 1; 1363 + ffs(ni->itype.compressed.block_size) - 1; 1410 1364 ni->itype.compressed.block_clusters = 1U << 1411 - a->data.non_resident. 1412 - compression_unit; 1365 + a->data.non_resident.compression_unit; 1413 1366 } else { 1414 1367 ni->itype.compressed.block_size = 0; 1415 1368 ni->itype.compressed.block_size_bits = 0; 1416 1369 ni->itype.compressed.block_clusters = 0; 1417 1370 } 1418 - ni->itype.compressed.size = sle64_to_cpu( 1371 + ni->itype.compressed.size = le64_to_cpu( 1419 1372 a->data.non_resident.compressed_size); 1420 1373 } 1421 1374 if (a->data.non_resident.lowest_vcn) { 1422 - ntfs_error(vi->i_sb, "First extent of attribute has " 1423 - "non-zero lowest_vcn."); 1375 + ntfs_error(vi->i_sb, "First extent of attribute has non-zero lowest_vcn."); 1424 1376 goto unm_err_out; 1425 1377 } 1426 - vi->i_size = sle64_to_cpu(a->data.non_resident.data_size); 1427 - ni->initialized_size = sle64_to_cpu( 1428 - a->data.non_resident.initialized_size); 1429 - ni->allocated_size = sle64_to_cpu( 1430 - a->data.non_resident.allocated_size); 1378 + vi->i_size = ni->data_size = le64_to_cpu(a->data.non_resident.data_size); 1379 + ni->initialized_size = le64_to_cpu(a->data.non_resident.initialized_size); 1380 + ni->allocated_size = le64_to_cpu(a->data.non_resident.allocated_size); 1431 1381 } 1432 - vi->i_mapping->a_ops = &ntfs_normal_aops; 1433 - if (NInoMstProtected(ni)) 1434 - vi->i_mapping->a_ops = &ntfs_mst_aops; 1435 - else if (NInoCompressed(ni)) 1436 - vi->i_mapping->a_ops = &ntfs_compressed_aops; 1382 + vi->i_mapping->a_ops = &ntfs_aops; 1437 1383 if ((NInoCompressed(ni) || NInoSparse(ni)) && ni->type != AT_INDEX_ROOT) 1438 1384 vi->i_blocks = ni->itype.compressed.size >> 9; 1439 1385 else ··· 1429 1401 * Make sure the base inode does not go away and attach it to the 1430 1402 * attribute inode. 1431 1403 */ 1432 - igrab(base_vi); 1404 + if (!igrab(base_vi)) { 1405 + err = -ENOENT; 1406 + goto unm_err_out; 1407 + } 1433 1408 ni->ext.base_ntfs_ino = base_ni; 1434 1409 ni->nr_extents = -1; 1435 1410 ··· 1449 1418 ntfs_attr_put_search_ctx(ctx); 1450 1419 unmap_mft_record(base_ni); 1451 1420 err_out: 1452 - ntfs_error(vol->sb, "Failed with error code %i while reading attribute " 1453 - "inode (mft_no 0x%lx, type 0x%x, name_len %i). " 1454 - "Marking corrupt inode and base inode 0x%lx as bad. " 1455 - "Run chkdsk.", err, vi->i_ino, ni->type, ni->name_len, 1421 + if (err != -ENOENT) 1422 + ntfs_error(vol->sb, 1423 + "Failed with error code %i while reading attribute inode (mft_no 0x%lx, type 0x%x, name_len %i). Marking corrupt inode and base inode 0x%lx as bad. Run chkdsk.", 1424 + err, vi->i_ino, ni->type, ni->name_len, 1456 1425 base_vi->i_ino); 1457 - make_bad_inode(vi); 1458 - if (err != -ENOMEM) 1426 + if (err != -ENOENT && err != -ENOMEM) 1459 1427 NVolSetErrors(vol); 1460 1428 return err; 1461 1429 } 1462 1430 1463 - /** 1431 + /* 1464 1432 * ntfs_read_locked_index_inode - read an index inode from its base inode 1465 1433 * @base_vi: base inode 1466 1434 * @vi: index inode to read ··· 1489 1459 * A: i_state has I_NEW set, hence the inode is locked, also 1490 1460 * i_count is set to 1, so it is not going to go away 1491 1461 * 1492 - * Return 0 on success and -errno on error. In the error case, the inode will 1493 - * have had make_bad_inode() executed on it. 1462 + * Return 0 on success and -errno on error. 1494 1463 */ 1495 1464 static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi) 1496 1465 { 1497 1466 loff_t bvi_size; 1498 - ntfs_volume *vol = NTFS_SB(vi->i_sb); 1499 - ntfs_inode *ni, *base_ni, *bni; 1467 + struct ntfs_volume *vol = NTFS_SB(vi->i_sb); 1468 + struct ntfs_inode *ni = NTFS_I(vi), *base_ni = NTFS_I(base_vi), *bni; 1500 1469 struct inode *bvi; 1501 - MFT_RECORD *m; 1502 - ATTR_RECORD *a; 1503 - ntfs_attr_search_ctx *ctx; 1504 - INDEX_ROOT *ir; 1470 + struct mft_record *m; 1471 + struct attr_record *a; 1472 + struct ntfs_attr_search_ctx *ctx; 1473 + struct index_root *ir; 1505 1474 u8 *ir_end, *index_end; 1506 1475 int err = 0; 1507 1476 1508 1477 ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino); 1478 + lockdep_assert_held(&base_ni->mrec_lock); 1479 + 1509 1480 ntfs_init_big_inode(vi); 1510 - ni = NTFS_I(vi); 1511 - base_ni = NTFS_I(base_vi); 1512 1481 /* Just mirror the values from the base inode. */ 1513 1482 vi->i_uid = base_vi->i_uid; 1514 1483 vi->i_gid = base_vi->i_gid; ··· 1534 1505 CASE_SENSITIVE, 0, NULL, 0, ctx); 1535 1506 if (unlikely(err)) { 1536 1507 if (err == -ENOENT) 1537 - ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is " 1538 - "missing."); 1508 + ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is missing."); 1539 1509 goto unm_err_out; 1540 1510 } 1541 1511 a = ctx->attr; ··· 1546 1518 /* Ensure the attribute name is placed before the value. */ 1547 1519 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >= 1548 1520 le16_to_cpu(a->data.resident.value_offset)))) { 1549 - ntfs_error(vol->sb, "$INDEX_ROOT attribute name is placed " 1550 - "after the attribute value."); 1521 + ntfs_error(vol->sb, 1522 + "$INDEX_ROOT attribute name is placed after the attribute value."); 1551 1523 goto unm_err_out; 1552 1524 } 1553 - /* 1554 - * Compressed/encrypted/sparse index root is not allowed, except for 1555 - * directories of course but those are not dealt with here. 1556 - */ 1557 - if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_ENCRYPTED | 1558 - ATTR_IS_SPARSE)) { 1559 - ntfs_error(vi->i_sb, "Found compressed/encrypted/sparse index " 1560 - "root attribute."); 1561 - goto unm_err_out; 1562 - } 1563 - ir = (INDEX_ROOT*)((u8*)a + le16_to_cpu(a->data.resident.value_offset)); 1564 - ir_end = (u8*)ir + le32_to_cpu(a->data.resident.value_length); 1565 - if (ir_end > (u8*)ctx->mrec + vol->mft_record_size) { 1525 + 1526 + ir = (struct index_root *)((u8 *)a + le16_to_cpu(a->data.resident.value_offset)); 1527 + ir_end = (u8 *)ir + le32_to_cpu(a->data.resident.value_length); 1528 + if (ir_end > (u8 *)ctx->mrec + vol->mft_record_size) { 1566 1529 ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is corrupt."); 1567 1530 goto unm_err_out; 1568 1531 } 1569 - index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length); 1532 + index_end = (u8 *)&ir->index + le32_to_cpu(ir->index.index_length); 1570 1533 if (index_end > ir_end) { 1571 1534 ntfs_error(vi->i_sb, "Index is corrupt."); 1572 1535 goto unm_err_out; 1573 1536 } 1574 - if (ir->type) { 1575 - ntfs_error(vi->i_sb, "Index type is not 0 (type is 0x%x).", 1576 - le32_to_cpu(ir->type)); 1577 - goto unm_err_out; 1578 - } 1537 + 1579 1538 ni->itype.index.collation_rule = ir->collation_rule; 1580 1539 ntfs_debug("Index collation rule is 0x%x.", 1581 1540 le32_to_cpu(ir->collation_rule)); 1582 1541 ni->itype.index.block_size = le32_to_cpu(ir->index_block_size); 1583 1542 if (!is_power_of_2(ni->itype.index.block_size)) { 1584 - ntfs_error(vi->i_sb, "Index block size (%u) is not a power of " 1585 - "two.", ni->itype.index.block_size); 1543 + ntfs_error(vi->i_sb, "Index block size (%u) is not a power of two.", 1544 + ni->itype.index.block_size); 1586 1545 goto unm_err_out; 1587 1546 } 1588 1547 if (ni->itype.index.block_size > PAGE_SIZE) { 1589 - ntfs_error(vi->i_sb, "Index block size (%u) > PAGE_SIZE " 1590 - "(%ld) is not supported. Sorry.", 1548 + ntfs_error(vi->i_sb, "Index block size (%u) > PAGE_SIZE (%ld) is not supported.", 1591 1549 ni->itype.index.block_size, PAGE_SIZE); 1592 1550 err = -EOPNOTSUPP; 1593 1551 goto unm_err_out; 1594 1552 } 1595 1553 if (ni->itype.index.block_size < NTFS_BLOCK_SIZE) { 1596 - ntfs_error(vi->i_sb, "Index block size (%u) < NTFS_BLOCK_SIZE " 1597 - "(%i) is not supported. Sorry.", 1554 + ntfs_error(vi->i_sb, 1555 + "Index block size (%u) < NTFS_BLOCK_SIZE (%i) is not supported.", 1598 1556 ni->itype.index.block_size, NTFS_BLOCK_SIZE); 1599 1557 err = -EOPNOTSUPP; 1600 1558 goto unm_err_out; ··· 1594 1580 ni->itype.index.vcn_size = vol->sector_size; 1595 1581 ni->itype.index.vcn_size_bits = vol->sector_size_bits; 1596 1582 } 1597 - /* Check for presence of index allocation attribute. */ 1598 - if (!(ir->index.flags & LARGE_INDEX)) { 1599 - /* No index allocation. */ 1600 - vi->i_size = ni->initialized_size = ni->allocated_size = 0; 1601 - /* We are done with the mft record, so we release it. */ 1602 - ntfs_attr_put_search_ctx(ctx); 1603 - unmap_mft_record(base_ni); 1604 - m = NULL; 1605 - ctx = NULL; 1606 - goto skip_large_index_stuff; 1607 - } /* LARGE_INDEX: Index allocation present. Setup state. */ 1608 - NInoSetIndexAllocPresent(ni); 1583 + 1609 1584 /* Find index allocation attribute. */ 1610 1585 ntfs_attr_reinit_search_ctx(ctx); 1611 1586 err = ntfs_attr_lookup(AT_INDEX_ALLOCATION, ni->name, ni->name_len, 1612 1587 CASE_SENSITIVE, 0, NULL, 0, ctx); 1613 1588 if (unlikely(err)) { 1614 - if (err == -ENOENT) 1615 - ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is " 1616 - "not present but $INDEX_ROOT " 1617 - "indicated it is."); 1618 - else 1619 - ntfs_error(vi->i_sb, "Failed to lookup " 1620 - "$INDEX_ALLOCATION attribute."); 1589 + if (err == -ENOENT) { 1590 + /* No index allocation. */ 1591 + vi->i_size = ni->initialized_size = ni->allocated_size = 0; 1592 + /* We are done with the mft record, so we release it. */ 1593 + ntfs_attr_put_search_ctx(ctx); 1594 + unmap_mft_record(base_ni); 1595 + m = NULL; 1596 + ctx = NULL; 1597 + goto skip_large_index_stuff; 1598 + } else 1599 + ntfs_error(vi->i_sb, "Failed to lookup $INDEX_ALLOCATION attribute."); 1621 1600 goto unm_err_out; 1622 1601 } 1602 + NInoSetIndexAllocPresent(ni); 1603 + NInoSetNonResident(ni); 1604 + ni->type = AT_INDEX_ALLOCATION; 1605 + 1623 1606 a = ctx->attr; 1624 1607 if (!a->non_resident) { 1625 - ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is " 1626 - "resident."); 1608 + ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is resident."); 1627 1609 goto unm_err_out; 1628 1610 } 1629 1611 /* 1630 1612 * Ensure the attribute name is placed before the mapping pairs array. 1631 1613 */ 1632 1614 if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >= 1633 - le16_to_cpu( 1634 - a->data.non_resident.mapping_pairs_offset)))) { 1635 - ntfs_error(vol->sb, "$INDEX_ALLOCATION attribute name is " 1636 - "placed after the mapping pairs array."); 1615 + le16_to_cpu(a->data.non_resident.mapping_pairs_offset)))) { 1616 + ntfs_error(vol->sb, 1617 + "$INDEX_ALLOCATION attribute name is placed after the mapping pairs array."); 1637 1618 goto unm_err_out; 1638 1619 } 1639 1620 if (a->flags & ATTR_IS_ENCRYPTED) { 1640 - ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is " 1641 - "encrypted."); 1621 + ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is encrypted."); 1642 1622 goto unm_err_out; 1643 1623 } 1644 1624 if (a->flags & ATTR_IS_SPARSE) { ··· 1640 1632 goto unm_err_out; 1641 1633 } 1642 1634 if (a->flags & ATTR_COMPRESSION_MASK) { 1643 - ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is " 1644 - "compressed."); 1635 + ntfs_error(vi->i_sb, 1636 + "$INDEX_ALLOCATION attribute is compressed."); 1645 1637 goto unm_err_out; 1646 1638 } 1647 1639 if (a->data.non_resident.lowest_vcn) { 1648 - ntfs_error(vi->i_sb, "First extent of $INDEX_ALLOCATION " 1649 - "attribute has non zero lowest_vcn."); 1640 + ntfs_error(vi->i_sb, 1641 + "First extent of $INDEX_ALLOCATION attribute has non zero lowest_vcn."); 1650 1642 goto unm_err_out; 1651 1643 } 1652 - vi->i_size = sle64_to_cpu(a->data.non_resident.data_size); 1653 - ni->initialized_size = sle64_to_cpu( 1654 - a->data.non_resident.initialized_size); 1655 - ni->allocated_size = sle64_to_cpu(a->data.non_resident.allocated_size); 1644 + vi->i_size = ni->data_size = le64_to_cpu(a->data.non_resident.data_size); 1645 + ni->initialized_size = le64_to_cpu(a->data.non_resident.initialized_size); 1646 + ni->allocated_size = le64_to_cpu(a->data.non_resident.allocated_size); 1656 1647 /* 1657 1648 * We are done with the mft record, so we release it. Otherwise 1658 1649 * we would deadlock in ntfs_attr_iget(). ··· 1670 1663 bni = NTFS_I(bvi); 1671 1664 if (NInoCompressed(bni) || NInoEncrypted(bni) || 1672 1665 NInoSparse(bni)) { 1673 - ntfs_error(vi->i_sb, "$BITMAP attribute is compressed and/or " 1674 - "encrypted and/or sparse."); 1666 + ntfs_error(vi->i_sb, 1667 + "$BITMAP attribute is compressed and/or encrypted and/or sparse."); 1675 1668 goto iput_unm_err_out; 1676 1669 } 1677 1670 /* Consistency check bitmap size vs. index allocation size. */ 1678 1671 bvi_size = i_size_read(bvi); 1679 1672 if ((bvi_size << 3) < (vi->i_size >> ni->itype.index.block_size_bits)) { 1680 - ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) for " 1681 - "index allocation (0x%llx).", bvi_size << 3, 1682 - vi->i_size); 1673 + ntfs_error(vi->i_sb, 1674 + "Index bitmap too small (0x%llx) for index allocation (0x%llx).", 1675 + bvi_size << 3, vi->i_size); 1683 1676 goto iput_unm_err_out; 1684 1677 } 1685 1678 iput(bvi); 1686 1679 skip_large_index_stuff: 1687 1680 /* Setup the operations for this index inode. */ 1688 - vi->i_mapping->a_ops = &ntfs_mst_aops; 1681 + ntfs_set_vfs_operations(vi, S_IFDIR, 0); 1689 1682 vi->i_blocks = ni->allocated_size >> 9; 1690 1683 /* 1691 1684 * Make sure the base inode doesn't go away and attach it to the 1692 1685 * index inode. 1693 1686 */ 1694 - igrab(base_vi); 1687 + if (!igrab(base_vi)) 1688 + goto unm_err_out; 1695 1689 ni->ext.base_ntfs_ino = base_ni; 1696 1690 ni->nr_extents = -1; 1697 1691 ··· 1708 1700 if (m) 1709 1701 unmap_mft_record(base_ni); 1710 1702 err_out: 1711 - ntfs_error(vi->i_sb, "Failed with error code %i while reading index " 1712 - "inode (mft_no 0x%lx, name_len %i.", err, vi->i_ino, 1713 - ni->name_len); 1714 - make_bad_inode(vi); 1703 + ntfs_error(vi->i_sb, 1704 + "Failed with error code %i while reading index inode (mft_no 0x%lx, name_len %i.", 1705 + err, vi->i_ino, ni->name_len); 1715 1706 if (err != -EOPNOTSUPP && err != -ENOMEM) 1716 1707 NVolSetErrors(vol); 1717 1708 return err; 1709 + } 1710 + 1711 + /* 1712 + * load_attribute_list_mount - load an attribute list into memory 1713 + * @vol: ntfs volume from which to read 1714 + * @rl: runlist of the attribute list 1715 + * @al_start: destination buffer 1716 + * @size: size of the destination buffer in bytes 1717 + * @initialized_size: initialized size of the attribute list 1718 + * 1719 + * Walk the runlist @rl and load all clusters from it copying them into 1720 + * the linear buffer @al. The maximum number of bytes copied to @al is @size 1721 + * bytes. Note, @size does not need to be a multiple of the cluster size. If 1722 + * @initialized_size is less than @size, the region in @al between 1723 + * @initialized_size and @size will be zeroed and not read from disk. 1724 + * 1725 + * Return 0 on success or -errno on error. 1726 + */ 1727 + static int load_attribute_list_mount(struct ntfs_volume *vol, 1728 + struct runlist_element *rl, u8 *al_start, const s64 size, 1729 + const s64 initialized_size) 1730 + { 1731 + s64 lcn; 1732 + u8 *al = al_start; 1733 + u8 *al_end = al + initialized_size; 1734 + struct super_block *sb; 1735 + int err = 0; 1736 + loff_t rl_byte_off, rl_byte_len; 1737 + 1738 + ntfs_debug("Entering."); 1739 + if (!vol || !rl || !al || size <= 0 || initialized_size < 0 || 1740 + initialized_size > size) 1741 + return -EINVAL; 1742 + if (!initialized_size) { 1743 + memset(al, 0, size); 1744 + return 0; 1745 + } 1746 + sb = vol->sb; 1747 + 1748 + /* Read all clusters specified by the runlist one run at a time. */ 1749 + while (rl->length) { 1750 + lcn = ntfs_rl_vcn_to_lcn(rl, rl->vcn); 1751 + ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.", 1752 + (unsigned long long)rl->vcn, 1753 + (unsigned long long)lcn); 1754 + /* The attribute list cannot be sparse. */ 1755 + if (lcn < 0) { 1756 + ntfs_error(sb, "ntfs_rl_vcn_to_lcn() failed. Cannot read attribute list."); 1757 + goto err_out; 1758 + } 1759 + 1760 + rl_byte_off = ntfs_cluster_to_bytes(vol, lcn); 1761 + rl_byte_len = ntfs_cluster_to_bytes(vol, rl->length); 1762 + 1763 + if (al + rl_byte_len > al_end) 1764 + rl_byte_len = al_end - al; 1765 + 1766 + err = ntfs_bdev_read(sb->s_bdev, al, rl_byte_off, 1767 + round_up(rl_byte_len, SECTOR_SIZE)); 1768 + if (err) { 1769 + ntfs_error(sb, "Cannot read attribute list."); 1770 + goto err_out; 1771 + } 1772 + 1773 + if (al + rl_byte_len >= al_end) { 1774 + if (initialized_size < size) 1775 + goto initialize; 1776 + goto done; 1777 + } 1778 + 1779 + al += rl_byte_len; 1780 + rl++; 1781 + } 1782 + if (initialized_size < size) { 1783 + initialize: 1784 + memset(al_start + initialized_size, 0, size - initialized_size); 1785 + } 1786 + done: 1787 + return err; 1788 + /* Real overflow! */ 1789 + ntfs_error(sb, "Attribute list buffer overflow. Read attribute list is truncated."); 1790 + err_out: 1791 + err = -EIO; 1792 + goto done; 1718 1793 } 1719 1794 1720 1795 /* ··· 1809 1718 */ 1810 1719 static struct lock_class_key mft_ni_runlist_lock_key, mft_ni_mrec_lock_key; 1811 1720 1812 - /** 1721 + /* 1813 1722 * ntfs_read_inode_mount - special read_inode for mount time use only 1814 1723 * @vi: inode to read 1815 1724 * ··· 1837 1746 */ 1838 1747 int ntfs_read_inode_mount(struct inode *vi) 1839 1748 { 1840 - VCN next_vcn, last_vcn, highest_vcn; 1841 - s64 block; 1749 + s64 next_vcn, last_vcn, highest_vcn; 1842 1750 struct super_block *sb = vi->i_sb; 1843 - ntfs_volume *vol = NTFS_SB(sb); 1844 - struct buffer_head *bh; 1845 - ntfs_inode *ni; 1846 - MFT_RECORD *m = NULL; 1847 - ATTR_RECORD *a; 1848 - ntfs_attr_search_ctx *ctx; 1751 + struct ntfs_volume *vol = NTFS_SB(sb); 1752 + struct ntfs_inode *ni = NTFS_I(vi); 1753 + struct mft_record *m = NULL; 1754 + struct attr_record *a; 1755 + struct ntfs_attr_search_ctx *ctx; 1849 1756 unsigned int i, nr_blocks; 1850 1757 int err; 1758 + size_t new_rl_count; 1851 1759 1852 1760 ntfs_debug("Entering."); 1853 1761 1854 1762 /* Initialize the ntfs specific part of @vi. */ 1855 1763 ntfs_init_big_inode(vi); 1856 1764 1857 - ni = NTFS_I(vi); 1858 1765 1859 1766 /* Setup the data attribute. It is special as it is mst protected. */ 1860 1767 NInoSetNonResident(ni); 1861 1768 NInoSetMstProtected(ni); 1862 1769 NInoSetSparseDisabled(ni); 1863 1770 ni->type = AT_DATA; 1864 - ni->name = NULL; 1771 + ni->name = AT_UNNAMED; 1865 1772 ni->name_len = 0; 1866 1773 /* 1867 1774 * This sets up our little cheat allowing us to reuse the async read io ··· 1877 1788 vol->mft_record_size); 1878 1789 goto err_out; 1879 1790 } 1791 + 1880 1792 i = vol->mft_record_size; 1881 1793 if (i < sb->s_blocksize) 1882 1794 i = sb->s_blocksize; 1883 - m = (MFT_RECORD*)ntfs_malloc_nofs(i); 1795 + 1796 + m = kzalloc(i, GFP_NOFS); 1884 1797 if (!m) { 1885 1798 ntfs_error(sb, "Failed to allocate buffer for $MFT record 0."); 1886 1799 goto err_out; 1887 1800 } 1888 1801 1889 1802 /* Determine the first block of the $MFT/$DATA attribute. */ 1890 - block = vol->mft_lcn << vol->cluster_size_bits >> 1891 - sb->s_blocksize_bits; 1892 - nr_blocks = vol->mft_record_size >> sb->s_blocksize_bits; 1803 + nr_blocks = ntfs_bytes_to_sector(vol, vol->mft_record_size); 1893 1804 if (!nr_blocks) 1894 1805 nr_blocks = 1; 1895 1806 1896 1807 /* Load $MFT/$DATA's first mft record. */ 1897 - for (i = 0; i < nr_blocks; i++) { 1898 - bh = sb_bread(sb, block++); 1899 - if (!bh) { 1900 - ntfs_error(sb, "Device read failed."); 1901 - goto err_out; 1902 - } 1903 - memcpy((char*)m + (i << sb->s_blocksize_bits), bh->b_data, 1904 - sb->s_blocksize); 1905 - brelse(bh); 1808 + err = ntfs_bdev_read(sb->s_bdev, (char *)m, 1809 + ntfs_cluster_to_bytes(vol, vol->mft_lcn), i); 1810 + if (err) { 1811 + ntfs_error(sb, "Device read failed."); 1812 + goto err_out; 1906 1813 } 1907 1814 1908 1815 if (le32_to_cpu(m->bytes_allocated) != vol->mft_record_size) { ··· 1908 1823 } 1909 1824 1910 1825 /* Apply the mst fixups. */ 1911 - if (post_read_mst_fixup((NTFS_RECORD*)m, vol->mft_record_size)) { 1912 - /* FIXME: Try to use the $MFTMirr now. */ 1826 + if (post_read_mst_fixup((struct ntfs_record *)m, vol->mft_record_size)) { 1913 1827 ntfs_error(sb, "MST fixup failed. $MFT is corrupt."); 1914 1828 goto err_out; 1915 1829 } 1916 1830 1917 - /* Sanity check offset to the first attribute */ 1918 - if (le16_to_cpu(m->attrs_offset) >= le32_to_cpu(m->bytes_allocated)) { 1919 - ntfs_error(sb, "Incorrect mft offset to the first attribute %u in superblock.", 1920 - le16_to_cpu(m->attrs_offset)); 1831 + if (ntfs_mft_record_check(vol, m, FILE_MFT)) { 1832 + ntfs_error(sb, "ntfs_mft_record_check failed. $MFT is corrupt."); 1921 1833 goto err_out; 1922 1834 } 1923 1835 ··· 1922 1840 vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number); 1923 1841 1924 1842 /* Provides read_folio() for map_mft_record(). */ 1925 - vi->i_mapping->a_ops = &ntfs_mst_aops; 1843 + vi->i_mapping->a_ops = &ntfs_mft_aops; 1926 1844 1927 1845 ctx = ntfs_attr_get_search_ctx(ni, m); 1928 1846 if (!ctx) { ··· 1934 1852 err = ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, 0, 0, NULL, 0, ctx); 1935 1853 if (err) { 1936 1854 if (unlikely(err != -ENOENT)) { 1937 - ntfs_error(sb, "Failed to lookup attribute list " 1938 - "attribute. You should run chkdsk."); 1855 + ntfs_error(sb, 1856 + "Failed to lookup attribute list attribute. You should run chkdsk."); 1939 1857 goto put_err_out; 1940 1858 } 1941 1859 } else /* if (!err) */ { 1942 - ATTR_LIST_ENTRY *al_entry, *next_al_entry; 1860 + struct attr_list_entry *al_entry, *next_al_entry; 1943 1861 u8 *al_end; 1944 - static const char *es = " Not allowed. $MFT is corrupt. " 1945 - "You should run chkdsk."; 1862 + static const char *es = " Not allowed. $MFT is corrupt. You should run chkdsk."; 1946 1863 1947 1864 ntfs_debug("Attribute list attribute found in $MFT."); 1948 1865 NInoSetAttrList(ni); 1949 1866 a = ctx->attr; 1950 1867 if (a->flags & ATTR_COMPRESSION_MASK) { 1951 - ntfs_error(sb, "Attribute list attribute is " 1952 - "compressed.%s", es); 1868 + ntfs_error(sb, 1869 + "Attribute list attribute is compressed.%s", 1870 + es); 1953 1871 goto put_err_out; 1954 1872 } 1955 1873 if (a->flags & ATTR_IS_ENCRYPTED || 1956 1874 a->flags & ATTR_IS_SPARSE) { 1957 1875 if (a->non_resident) { 1958 - ntfs_error(sb, "Non-resident attribute list " 1959 - "attribute is encrypted/" 1960 - "sparse.%s", es); 1876 + ntfs_error(sb, 1877 + "Non-resident attribute list attribute is encrypted/sparse.%s", 1878 + es); 1961 1879 goto put_err_out; 1962 1880 } 1963 - ntfs_warning(sb, "Resident attribute list attribute " 1964 - "in $MFT system file is marked " 1965 - "encrypted/sparse which is not true. " 1966 - "However, Windows allows this and " 1967 - "chkdsk does not detect or correct it " 1968 - "so we will just ignore the invalid " 1969 - "flags and pretend they are not set."); 1881 + ntfs_warning(sb, 1882 + "Resident attribute list attribute in $MFT system file is marked encrypted/sparse which is not true. However, Windows allows this and chkdsk does not detect or correct it so we will just ignore the invalid flags and pretend they are not set."); 1970 1883 } 1971 1884 /* Now allocate memory for the attribute list. */ 1972 1885 ni->attr_list_size = (u32)ntfs_attr_size(a); ··· 1969 1892 ntfs_error(sb, "Attr_list_size is zero"); 1970 1893 goto put_err_out; 1971 1894 } 1972 - ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size); 1895 + ni->attr_list = kvzalloc(round_up(ni->attr_list_size, SECTOR_SIZE), 1896 + GFP_NOFS); 1973 1897 if (!ni->attr_list) { 1974 - ntfs_error(sb, "Not enough memory to allocate buffer " 1975 - "for attribute list."); 1898 + ntfs_error(sb, "Not enough memory to allocate buffer for attribute list."); 1976 1899 goto put_err_out; 1977 1900 } 1978 1901 if (a->non_resident) { 1902 + struct runlist_element *rl; 1903 + size_t new_rl_count; 1904 + 1979 1905 NInoSetAttrListNonResident(ni); 1980 1906 if (a->data.non_resident.lowest_vcn) { 1981 - ntfs_error(sb, "Attribute list has non zero " 1982 - "lowest_vcn. $MFT is corrupt. " 1983 - "You should run chkdsk."); 1907 + ntfs_error(sb, 1908 + "Attribute list has non zero lowest_vcn. $MFT is corrupt. You should run chkdsk."); 1984 1909 goto put_err_out; 1985 1910 } 1986 - /* Setup the runlist. */ 1987 - ni->attr_list_rl.rl = ntfs_mapping_pairs_decompress(vol, 1988 - a, NULL); 1989 - if (IS_ERR(ni->attr_list_rl.rl)) { 1990 - err = PTR_ERR(ni->attr_list_rl.rl); 1991 - ni->attr_list_rl.rl = NULL; 1992 - ntfs_error(sb, "Mapping pairs decompression " 1993 - "failed with error code %i.", 1994 - -err); 1911 + 1912 + rl = ntfs_mapping_pairs_decompress(vol, a, NULL, &new_rl_count); 1913 + if (IS_ERR(rl)) { 1914 + err = PTR_ERR(rl); 1915 + ntfs_error(sb, 1916 + "Mapping pairs decompression failed with error code %i.", 1917 + -err); 1995 1918 goto put_err_out; 1996 1919 } 1997 - /* Now load the attribute list. */ 1998 - if ((err = load_attribute_list(vol, &ni->attr_list_rl, 1999 - ni->attr_list, ni->attr_list_size, 2000 - sle64_to_cpu(a->data. 2001 - non_resident.initialized_size)))) { 2002 - ntfs_error(sb, "Failed to load attribute list " 2003 - "attribute with error code %i.", 2004 - -err); 1920 + 1921 + err = load_attribute_list_mount(vol, rl, ni->attr_list, ni->attr_list_size, 1922 + le64_to_cpu(a->data.non_resident.initialized_size)); 1923 + kvfree(rl); 1924 + if (err) { 1925 + ntfs_error(sb, 1926 + "Failed to load attribute list with error code %i.", 1927 + -err); 2005 1928 goto put_err_out; 2006 1929 } 2007 1930 } else /* if (!ctx.attr->non_resident) */ { 2008 - if ((u8*)a + le16_to_cpu( 1931 + if ((u8 *)a + le16_to_cpu( 2009 1932 a->data.resident.value_offset) + 2010 - le32_to_cpu( 2011 - a->data.resident.value_length) > 2012 - (u8*)ctx->mrec + vol->mft_record_size) { 2013 - ntfs_error(sb, "Corrupt attribute list " 2014 - "attribute."); 1933 + le32_to_cpu(a->data.resident.value_length) > 1934 + (u8 *)ctx->mrec + vol->mft_record_size) { 1935 + ntfs_error(sb, "Corrupt attribute list attribute."); 2015 1936 goto put_err_out; 2016 1937 } 2017 1938 /* Now copy the attribute list. */ 2018 - memcpy(ni->attr_list, (u8*)a + le16_to_cpu( 1939 + memcpy(ni->attr_list, (u8 *)a + le16_to_cpu( 2019 1940 a->data.resident.value_offset), 2020 - le32_to_cpu( 2021 - a->data.resident.value_length)); 1941 + le32_to_cpu(a->data.resident.value_length)); 2022 1942 } 2023 1943 /* The attribute list is now setup in memory. */ 2024 - /* 2025 - * FIXME: I don't know if this case is actually possible. 2026 - * According to logic it is not possible but I have seen too 2027 - * many weird things in MS software to rely on logic... Thus we 2028 - * perform a manual search and make sure the first $MFT/$DATA 2029 - * extent is in the base inode. If it is not we abort with an 2030 - * error and if we ever see a report of this error we will need 2031 - * to do some magic in order to have the necessary mft record 2032 - * loaded and in the right place in the page cache. But 2033 - * hopefully logic will prevail and this never happens... 2034 - */ 2035 - al_entry = (ATTR_LIST_ENTRY*)ni->attr_list; 2036 - al_end = (u8*)al_entry + ni->attr_list_size; 1944 + al_entry = (struct attr_list_entry *)ni->attr_list; 1945 + al_end = (u8 *)al_entry + ni->attr_list_size; 2037 1946 for (;; al_entry = next_al_entry) { 2038 1947 /* Out of bounds check. */ 2039 - if ((u8*)al_entry < ni->attr_list || 2040 - (u8*)al_entry > al_end) 1948 + if ((u8 *)al_entry < ni->attr_list || 1949 + (u8 *)al_entry > al_end) 2041 1950 goto em_put_err_out; 2042 1951 /* Catch the end of the attribute list. */ 2043 - if ((u8*)al_entry == al_end) 1952 + if ((u8 *)al_entry == al_end) 2044 1953 goto em_put_err_out; 2045 1954 if (!al_entry->length) 2046 1955 goto em_put_err_out; 2047 - if ((u8*)al_entry + 6 > al_end || (u8*)al_entry + 2048 - le16_to_cpu(al_entry->length) > al_end) 1956 + if ((u8 *)al_entry + 6 > al_end || 1957 + (u8 *)al_entry + le16_to_cpu(al_entry->length) > al_end) 2049 1958 goto em_put_err_out; 2050 - next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry + 1959 + next_al_entry = (struct attr_list_entry *)((u8 *)al_entry + 2051 1960 le16_to_cpu(al_entry->length)); 2052 1961 if (le32_to_cpu(al_entry->type) > le32_to_cpu(AT_DATA)) 2053 1962 goto em_put_err_out; 2054 - if (AT_DATA != al_entry->type) 1963 + if (al_entry->type != AT_DATA) 2055 1964 continue; 2056 1965 /* We want an unnamed attribute. */ 2057 1966 if (al_entry->name_length) ··· 2048 1985 /* First entry has to be in the base mft record. */ 2049 1986 if (MREF_LE(al_entry->mft_reference) != vi->i_ino) { 2050 1987 /* MFT references do not match, logic fails. */ 2051 - ntfs_error(sb, "BUG: The first $DATA extent " 2052 - "of $MFT is not in the base " 2053 - "mft record. Please report " 2054 - "you saw this message to " 2055 - "linux-ntfs-dev@lists." 2056 - "sourceforge.net"); 1988 + ntfs_error(sb, 1989 + "BUG: The first $DATA extent of $MFT is not in the base mft record."); 2057 1990 goto put_err_out; 2058 1991 } else { 2059 1992 /* Sequence numbers must match. */ ··· 2069 2010 next_vcn = last_vcn = highest_vcn = 0; 2070 2011 while (!(err = ntfs_attr_lookup(AT_DATA, NULL, 0, 0, next_vcn, NULL, 0, 2071 2012 ctx))) { 2072 - runlist_element *nrl; 2013 + struct runlist_element *nrl; 2073 2014 2074 2015 /* Cache the current attribute. */ 2075 2016 a = ctx->attr; 2076 2017 /* $MFT must be non-resident. */ 2077 2018 if (!a->non_resident) { 2078 - ntfs_error(sb, "$MFT must be non-resident but a " 2079 - "resident extent was found. $MFT is " 2080 - "corrupt. Run chkdsk."); 2019 + ntfs_error(sb, 2020 + "$MFT must be non-resident but a resident extent was found. $MFT is corrupt. Run chkdsk."); 2081 2021 goto put_err_out; 2082 2022 } 2083 2023 /* $MFT must be uncompressed and unencrypted. */ 2084 2024 if (a->flags & ATTR_COMPRESSION_MASK || 2085 2025 a->flags & ATTR_IS_ENCRYPTED || 2086 2026 a->flags & ATTR_IS_SPARSE) { 2087 - ntfs_error(sb, "$MFT must be uncompressed, " 2088 - "non-sparse, and unencrypted but a " 2089 - "compressed/sparse/encrypted extent " 2090 - "was found. $MFT is corrupt. Run " 2091 - "chkdsk."); 2027 + ntfs_error(sb, 2028 + "$MFT must be uncompressed, non-sparse, and unencrypted but a compressed/sparse/encrypted extent was found. $MFT is corrupt. Run chkdsk."); 2092 2029 goto put_err_out; 2093 2030 } 2094 2031 /* ··· 2093 2038 * as we have exclusive access to the inode at this time and we 2094 2039 * are a mount in progress task, too. 2095 2040 */ 2096 - nrl = ntfs_mapping_pairs_decompress(vol, a, ni->runlist.rl); 2041 + nrl = ntfs_mapping_pairs_decompress(vol, a, &ni->runlist, 2042 + &new_rl_count); 2097 2043 if (IS_ERR(nrl)) { 2098 - ntfs_error(sb, "ntfs_mapping_pairs_decompress() " 2099 - "failed with error code %ld. $MFT is " 2100 - "corrupt.", PTR_ERR(nrl)); 2044 + ntfs_error(sb, 2045 + "ntfs_mapping_pairs_decompress() failed with error code %ld.", 2046 + PTR_ERR(nrl)); 2101 2047 goto put_err_out; 2102 2048 } 2103 2049 ni->runlist.rl = nrl; 2050 + ni->runlist.count = new_rl_count; 2104 2051 2105 2052 /* Are we in the first extent? */ 2106 2053 if (!next_vcn) { 2107 2054 if (a->data.non_resident.lowest_vcn) { 2108 - ntfs_error(sb, "First extent of $DATA " 2109 - "attribute has non zero " 2110 - "lowest_vcn. $MFT is corrupt. " 2111 - "You should run chkdsk."); 2055 + ntfs_error(sb, 2056 + "First extent of $DATA attribute has non zero lowest_vcn. $MFT is corrupt. You should run chkdsk."); 2112 2057 goto put_err_out; 2113 2058 } 2114 2059 /* Get the last vcn in the $DATA attribute. */ 2115 - last_vcn = sle64_to_cpu( 2116 - a->data.non_resident.allocated_size) 2117 - >> vol->cluster_size_bits; 2060 + last_vcn = ntfs_bytes_to_cluster(vol, 2061 + le64_to_cpu(a->data.non_resident.allocated_size)); 2118 2062 /* Fill in the inode size. */ 2119 - vi->i_size = sle64_to_cpu( 2120 - a->data.non_resident.data_size); 2121 - ni->initialized_size = sle64_to_cpu( 2122 - a->data.non_resident.initialized_size); 2123 - ni->allocated_size = sle64_to_cpu( 2124 - a->data.non_resident.allocated_size); 2063 + vi->i_size = le64_to_cpu(a->data.non_resident.data_size); 2064 + ni->initialized_size = le64_to_cpu(a->data.non_resident.initialized_size); 2065 + ni->allocated_size = le64_to_cpu(a->data.non_resident.allocated_size); 2125 2066 /* 2126 2067 * Verify the number of mft records does not exceed 2127 2068 * 2^32 - 1. ··· 2146 2095 * ntfs_read_inode() on extents of $MFT/$DATA. But lets 2147 2096 * hope this never happens... 2148 2097 */ 2149 - ntfs_read_locked_inode(vi); 2150 - if (is_bad_inode(vi)) { 2151 - ntfs_error(sb, "ntfs_read_inode() of $MFT " 2152 - "failed. BUG or corrupt $MFT. " 2153 - "Run chkdsk and if no errors " 2154 - "are found, please report you " 2155 - "saw this message to " 2156 - "linux-ntfs-dev@lists." 2157 - "sourceforge.net"); 2098 + err = ntfs_read_locked_inode(vi); 2099 + if (err) { 2100 + ntfs_error(sb, "ntfs_read_inode() of $MFT failed.\n"); 2158 2101 ntfs_attr_put_search_ctx(ctx); 2159 2102 /* Revert to the safe super operations. */ 2160 - ntfs_free(m); 2103 + kfree(m); 2161 2104 return -1; 2162 2105 } 2163 2106 /* ··· 2169 2124 } 2170 2125 2171 2126 /* Get the lowest vcn for the next extent. */ 2172 - highest_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn); 2127 + highest_vcn = le64_to_cpu(a->data.non_resident.highest_vcn); 2173 2128 next_vcn = highest_vcn + 1; 2174 2129 2175 2130 /* Only one extent or error, which we catch below. */ ··· 2177 2132 break; 2178 2133 2179 2134 /* Avoid endless loops due to corruption. */ 2180 - if (next_vcn < sle64_to_cpu( 2181 - a->data.non_resident.lowest_vcn)) { 2182 - ntfs_error(sb, "$MFT has corrupt attribute list " 2183 - "attribute. Run chkdsk."); 2135 + if (next_vcn < le64_to_cpu(a->data.non_resident.lowest_vcn)) { 2136 + ntfs_error(sb, "$MFT has corrupt attribute list attribute. Run chkdsk."); 2184 2137 goto put_err_out; 2185 2138 } 2186 2139 } 2187 2140 if (err != -ENOENT) { 2188 - ntfs_error(sb, "Failed to lookup $MFT/$DATA attribute extent. " 2189 - "$MFT is corrupt. Run chkdsk."); 2141 + ntfs_error(sb, "Failed to lookup $MFT/$DATA attribute extent. Run chkdsk.\n"); 2190 2142 goto put_err_out; 2191 2143 } 2192 2144 if (!a) { 2193 - ntfs_error(sb, "$MFT/$DATA attribute not found. $MFT is " 2194 - "corrupt. Run chkdsk."); 2145 + ntfs_error(sb, "$MFT/$DATA attribute not found. $MFT is corrupt. Run chkdsk."); 2195 2146 goto put_err_out; 2196 2147 } 2197 2148 if (highest_vcn && highest_vcn != last_vcn - 1) { 2198 - ntfs_error(sb, "Failed to load the complete runlist for " 2199 - "$MFT/$DATA. Driver bug or corrupt $MFT. " 2200 - "Run chkdsk."); 2149 + ntfs_error(sb, "Failed to load the complete runlist for $MFT/$DATA. Run chkdsk."); 2201 2150 ntfs_debug("highest_vcn = 0x%llx, last_vcn - 1 = 0x%llx", 2202 2151 (unsigned long long)highest_vcn, 2203 2152 (unsigned long long)last_vcn - 1); ··· 2199 2160 } 2200 2161 ntfs_attr_put_search_ctx(ctx); 2201 2162 ntfs_debug("Done."); 2202 - ntfs_free(m); 2163 + kfree(m); 2203 2164 2204 2165 /* 2205 2166 * Split the locking rules of the MFT inode from the ··· 2211 2172 return 0; 2212 2173 2213 2174 em_put_err_out: 2214 - ntfs_error(sb, "Couldn't find first extent of $DATA attribute in " 2215 - "attribute list. $MFT is corrupt. Run chkdsk."); 2175 + ntfs_error(sb, 2176 + "Couldn't find first extent of $DATA attribute in attribute list. $MFT is corrupt. Run chkdsk."); 2216 2177 put_err_out: 2217 2178 ntfs_attr_put_search_ctx(ctx); 2218 2179 err_out: 2219 2180 ntfs_error(sb, "Failed. Marking inode as bad."); 2220 - make_bad_inode(vi); 2221 - ntfs_free(m); 2181 + kfree(m); 2222 2182 return -1; 2223 2183 } 2224 2184 2225 - static void __ntfs_clear_inode(ntfs_inode *ni) 2185 + static void __ntfs_clear_inode(struct ntfs_inode *ni) 2226 2186 { 2227 2187 /* Free all alocated memory. */ 2228 - down_write(&ni->runlist.lock); 2229 - if (ni->runlist.rl) { 2230 - ntfs_free(ni->runlist.rl); 2188 + if (NInoNonResident(ni) && ni->runlist.rl) { 2189 + kvfree(ni->runlist.rl); 2231 2190 ni->runlist.rl = NULL; 2232 2191 } 2233 - up_write(&ni->runlist.lock); 2234 2192 2235 2193 if (ni->attr_list) { 2236 - ntfs_free(ni->attr_list); 2194 + kvfree(ni->attr_list); 2237 2195 ni->attr_list = NULL; 2238 2196 } 2239 2197 2240 - down_write(&ni->attr_list_rl.lock); 2241 - if (ni->attr_list_rl.rl) { 2242 - ntfs_free(ni->attr_list_rl.rl); 2243 - ni->attr_list_rl.rl = NULL; 2244 - } 2245 - up_write(&ni->attr_list_rl.lock); 2246 - 2247 - if (ni->name_len && ni->name != I30) { 2248 - /* Catch bugs... */ 2249 - BUG_ON(!ni->name); 2198 + if (ni->name_len && ni->name != I30 && 2199 + ni->name != reparse_index_name && 2200 + ni->name != objid_index_name) { 2201 + WARN_ON(!ni->name); 2250 2202 kfree(ni->name); 2251 2203 } 2252 2204 } 2253 2205 2254 - void ntfs_clear_extent_inode(ntfs_inode *ni) 2206 + void ntfs_clear_extent_inode(struct ntfs_inode *ni) 2255 2207 { 2256 2208 ntfs_debug("Entering for inode 0x%lx.", ni->mft_no); 2257 2209 2258 - BUG_ON(NInoAttr(ni)); 2259 - BUG_ON(ni->nr_extents != -1); 2260 - 2261 - #ifdef NTFS_RW 2262 - if (NInoDirty(ni)) { 2263 - if (!is_bad_inode(VFS_I(ni->ext.base_ntfs_ino))) 2264 - ntfs_error(ni->vol->sb, "Clearing dirty extent inode! " 2265 - "Losing data! This is a BUG!!!"); 2266 - // FIXME: Do something!!! 2267 - } 2268 - #endif /* NTFS_RW */ 2210 + WARN_ON(NInoAttr(ni)); 2211 + WARN_ON(ni->nr_extents != -1); 2269 2212 2270 2213 __ntfs_clear_inode(ni); 2271 - 2272 - /* Bye, bye... */ 2273 2214 ntfs_destroy_extent_inode(ni); 2274 2215 } 2275 2216 2276 - /** 2217 + static int ntfs_delete_base_inode(struct ntfs_inode *ni) 2218 + { 2219 + struct super_block *sb = ni->vol->sb; 2220 + int err; 2221 + 2222 + if (NInoAttr(ni) || ni->nr_extents == -1) 2223 + return 0; 2224 + 2225 + err = ntfs_non_resident_dealloc_clusters(ni); 2226 + 2227 + /* 2228 + * Deallocate extent mft records and free extent inodes. 2229 + * No need to lock as no one else has a reference. 2230 + */ 2231 + while (ni->nr_extents) { 2232 + err = ntfs_mft_record_free(ni->vol, *(ni->ext.extent_ntfs_inos)); 2233 + if (err) 2234 + ntfs_error(sb, 2235 + "Failed to free extent MFT record. Leaving inconsistent metadata.\n"); 2236 + ntfs_inode_close(*(ni->ext.extent_ntfs_inos)); 2237 + } 2238 + 2239 + /* Deallocate base mft record */ 2240 + err = ntfs_mft_record_free(ni->vol, ni); 2241 + if (err) 2242 + ntfs_error(sb, "Failed to free base MFT record. Leaving inconsistent metadata.\n"); 2243 + return err; 2244 + } 2245 + 2246 + /* 2277 2247 * ntfs_evict_big_inode - clean up the ntfs specific part of an inode 2278 2248 * @vi: vfs inode pending annihilation 2279 2249 * ··· 2294 2246 */ 2295 2247 void ntfs_evict_big_inode(struct inode *vi) 2296 2248 { 2297 - ntfs_inode *ni = NTFS_I(vi); 2249 + struct ntfs_inode *ni = NTFS_I(vi); 2298 2250 2299 2251 truncate_inode_pages_final(&vi->i_data); 2300 - clear_inode(vi); 2301 2252 2302 - #ifdef NTFS_RW 2253 + if (!vi->i_nlink) { 2254 + if (!NInoAttr(ni)) { 2255 + /* Never called with extent inodes */ 2256 + WARN_ON(ni->nr_extents == -1); 2257 + ntfs_delete_base_inode(ni); 2258 + } 2259 + goto release; 2260 + } 2261 + 2303 2262 if (NInoDirty(ni)) { 2304 - bool was_bad = (is_bad_inode(vi)); 2305 - 2306 2263 /* Committing the inode also commits all extent inodes. */ 2307 2264 ntfs_commit_inode(vi); 2308 2265 2309 - if (!was_bad && (is_bad_inode(vi) || NInoDirty(ni))) { 2310 - ntfs_error(vi->i_sb, "Failed to commit dirty inode " 2311 - "0x%lx. Losing data!", vi->i_ino); 2312 - // FIXME: Do something!!! 2266 + if (NInoDirty(ni)) { 2267 + ntfs_debug("Failed to commit dirty inode 0x%lx. Losing data!", 2268 + vi->i_ino); 2269 + NInoClearAttrListDirty(ni); 2270 + NInoClearDirty(ni); 2313 2271 } 2314 2272 } 2315 - #endif /* NTFS_RW */ 2316 2273 2317 2274 /* No need to lock at this stage as no one else has a reference. */ 2318 2275 if (ni->nr_extents > 0) { 2319 2276 int i; 2320 2277 2321 - for (i = 0; i < ni->nr_extents; i++) 2322 - ntfs_clear_extent_inode(ni->ext.extent_ntfs_inos[i]); 2323 - kfree(ni->ext.extent_ntfs_inos); 2278 + for (i = 0; i < ni->nr_extents; i++) { 2279 + if (ni->ext.extent_ntfs_inos[i]) 2280 + ntfs_clear_extent_inode(ni->ext.extent_ntfs_inos[i]); 2281 + } 2282 + ni->nr_extents = 0; 2283 + kvfree(ni->ext.extent_ntfs_inos); 2324 2284 } 2325 2285 2286 + release: 2287 + clear_inode(vi); 2326 2288 __ntfs_clear_inode(ni); 2327 2289 2328 2290 if (NInoAttr(ni)) { ··· 2343 2285 ni->ext.base_ntfs_ino = NULL; 2344 2286 } 2345 2287 } 2346 - BUG_ON(ni->page); 2288 + 2347 2289 if (!atomic_dec_and_test(&ni->count)) 2348 - BUG(); 2349 - return; 2290 + WARN_ON(1); 2291 + if (ni->folio) 2292 + folio_put(ni->folio); 2293 + kfree(ni->mrec); 2294 + kvfree(ni->target); 2350 2295 } 2351 2296 2352 - /** 2297 + /* 2353 2298 * ntfs_show_options - show mount options in /proc/mounts 2354 2299 * @sf: seq_file in which to write our mount options 2355 2300 * @root: root of the mounted tree whose mount options to display ··· 2364 2303 */ 2365 2304 int ntfs_show_options(struct seq_file *sf, struct dentry *root) 2366 2305 { 2367 - ntfs_volume *vol = NTFS_SB(root->d_sb); 2306 + struct ntfs_volume *vol = NTFS_SB(root->d_sb); 2368 2307 int i; 2369 2308 2370 - seq_printf(sf, ",uid=%i", from_kuid_munged(&init_user_ns, vol->uid)); 2371 - seq_printf(sf, ",gid=%i", from_kgid_munged(&init_user_ns, vol->gid)); 2309 + if (uid_valid(vol->uid)) 2310 + seq_printf(sf, ",uid=%i", from_kuid_munged(&init_user_ns, vol->uid)); 2311 + if (gid_valid(vol->gid)) 2312 + seq_printf(sf, ",gid=%i", from_kgid_munged(&init_user_ns, vol->gid)); 2372 2313 if (vol->fmask == vol->dmask) 2373 2314 seq_printf(sf, ",umask=0%o", vol->fmask); 2374 2315 else { 2375 2316 seq_printf(sf, ",fmask=0%o", vol->fmask); 2376 2317 seq_printf(sf, ",dmask=0%o", vol->dmask); 2377 2318 } 2378 - seq_printf(sf, ",nls=%s", vol->nls_map->charset); 2319 + seq_printf(sf, ",iocharset=%s", vol->nls_map->charset); 2379 2320 if (NVolCaseSensitive(vol)) 2380 - seq_printf(sf, ",case_sensitive"); 2321 + seq_puts(sf, ",case_sensitive"); 2322 + else 2323 + seq_puts(sf, ",nocase"); 2381 2324 if (NVolShowSystemFiles(vol)) 2382 - seq_printf(sf, ",show_sys_files"); 2383 - if (!NVolSparseEnabled(vol)) 2384 - seq_printf(sf, ",disable_sparse"); 2325 + seq_puts(sf, ",show_sys_files,showmeta"); 2385 2326 for (i = 0; on_errors_arr[i].val; i++) { 2386 - if (on_errors_arr[i].val & vol->on_errors) 2327 + if (on_errors_arr[i].val == vol->on_errors) 2387 2328 seq_printf(sf, ",errors=%s", on_errors_arr[i].str); 2388 2329 } 2389 2330 seq_printf(sf, ",mft_zone_multiplier=%i", vol->mft_zone_multiplier); 2331 + if (NVolSysImmutable(vol)) 2332 + seq_puts(sf, ",sys_immutable"); 2333 + if (!NVolShowHiddenFiles(vol)) 2334 + seq_puts(sf, ",nohidden"); 2335 + if (NVolHideDotFiles(vol)) 2336 + seq_puts(sf, ",hide_dot_files"); 2337 + if (NVolCheckWindowsNames(vol)) 2338 + seq_puts(sf, ",windows_names"); 2339 + if (NVolDiscard(vol)) 2340 + seq_puts(sf, ",discard"); 2341 + if (NVolDisableSparse(vol)) 2342 + seq_puts(sf, ",disable_sparse"); 2343 + if (vol->sb->s_flags & SB_POSIXACL) 2344 + seq_puts(sf, ",acl"); 2390 2345 return 0; 2391 2346 } 2392 2347 2393 - #ifdef NTFS_RW 2394 - 2395 - static const char *es = " Leaving inconsistent metadata. Unmount and run " 2396 - "chkdsk."; 2397 - 2398 - /** 2399 - * ntfs_truncate - called when the i_size of an ntfs inode is changed 2400 - * @vi: inode for which the i_size was changed 2401 - * 2402 - * We only support i_size changes for normal files at present, i.e. not 2403 - * compressed and not encrypted. This is enforced in ntfs_setattr(), see 2404 - * below. 2405 - * 2406 - * The kernel guarantees that @vi is a regular file (S_ISREG() is true) and 2407 - * that the change is allowed. 2408 - * 2409 - * This implies for us that @vi is a file inode rather than a directory, index, 2410 - * or attribute inode as well as that @vi is a base inode. 2411 - * 2412 - * Returns 0 on success or -errno on error. 2413 - * 2414 - * Called with ->i_mutex held. 2415 - */ 2416 - int ntfs_truncate(struct inode *vi) 2348 + int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset, 2349 + const loff_t new_size, bool bsync) 2417 2350 { 2418 - s64 new_size, old_size, nr_freed, new_alloc_size, old_alloc_size; 2419 - VCN highest_vcn; 2351 + struct ntfs_inode *ni = NTFS_I(vi); 2352 + loff_t old_init_size; 2420 2353 unsigned long flags; 2421 - ntfs_inode *base_ni, *ni = NTFS_I(vi); 2422 - ntfs_volume *vol = ni->vol; 2423 - ntfs_attr_search_ctx *ctx; 2424 - MFT_RECORD *m; 2425 - ATTR_RECORD *a; 2426 - const char *te = " Leaving file length out of sync with i_size."; 2427 - int err, mp_size, size_change, alloc_change; 2428 - 2429 - ntfs_debug("Entering for inode 0x%lx.", vi->i_ino); 2430 - BUG_ON(NInoAttr(ni)); 2431 - BUG_ON(S_ISDIR(vi->i_mode)); 2432 - BUG_ON(NInoMstProtected(ni)); 2433 - BUG_ON(ni->nr_extents < 0); 2434 - retry_truncate: 2435 - /* 2436 - * Lock the runlist for writing and map the mft record to ensure it is 2437 - * safe to mess with the attribute runlist and sizes. 2438 - */ 2439 - down_write(&ni->runlist.lock); 2440 - if (!NInoAttr(ni)) 2441 - base_ni = ni; 2442 - else 2443 - base_ni = ni->ext.base_ntfs_ino; 2444 - m = map_mft_record(base_ni); 2445 - if (IS_ERR(m)) { 2446 - err = PTR_ERR(m); 2447 - ntfs_error(vi->i_sb, "Failed to map mft record for inode 0x%lx " 2448 - "(error code %d).%s", vi->i_ino, err, te); 2449 - ctx = NULL; 2450 - m = NULL; 2451 - goto old_bad_out; 2452 - } 2453 - ctx = ntfs_attr_get_search_ctx(base_ni, m); 2454 - if (unlikely(!ctx)) { 2455 - ntfs_error(vi->i_sb, "Failed to allocate a search context for " 2456 - "inode 0x%lx (not enough memory).%s", 2457 - vi->i_ino, te); 2458 - err = -ENOMEM; 2459 - goto old_bad_out; 2460 - } 2461 - err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 2462 - CASE_SENSITIVE, 0, NULL, 0, ctx); 2463 - if (unlikely(err)) { 2464 - if (err == -ENOENT) { 2465 - ntfs_error(vi->i_sb, "Open attribute is missing from " 2466 - "mft record. Inode 0x%lx is corrupt. " 2467 - "Run chkdsk.%s", vi->i_ino, te); 2468 - err = -EIO; 2469 - } else 2470 - ntfs_error(vi->i_sb, "Failed to lookup attribute in " 2471 - "inode 0x%lx (error code %d).%s", 2472 - vi->i_ino, err, te); 2473 - goto old_bad_out; 2474 - } 2475 - m = ctx->mrec; 2476 - a = ctx->attr; 2477 - /* 2478 - * The i_size of the vfs inode is the new size for the attribute value. 2479 - */ 2480 - new_size = i_size_read(vi); 2481 - /* The current size of the attribute value is the old size. */ 2482 - old_size = ntfs_attr_size(a); 2483 - /* Calculate the new allocated size. */ 2484 - if (NInoNonResident(ni)) 2485 - new_alloc_size = (new_size + vol->cluster_size - 1) & 2486 - ~(s64)vol->cluster_size_mask; 2487 - else 2488 - new_alloc_size = (new_size + 7) & ~7; 2489 - /* The current allocated size is the old allocated size. */ 2490 - read_lock_irqsave(&ni->size_lock, flags); 2491 - old_alloc_size = ni->allocated_size; 2492 - read_unlock_irqrestore(&ni->size_lock, flags); 2493 - /* 2494 - * The change in the file size. This will be 0 if no change, >0 if the 2495 - * size is growing, and <0 if the size is shrinking. 2496 - */ 2497 - size_change = -1; 2498 - if (new_size - old_size >= 0) { 2499 - size_change = 1; 2500 - if (new_size == old_size) 2501 - size_change = 0; 2502 - } 2503 - /* As above for the allocated size. */ 2504 - alloc_change = -1; 2505 - if (new_alloc_size - old_alloc_size >= 0) { 2506 - alloc_change = 1; 2507 - if (new_alloc_size == old_alloc_size) 2508 - alloc_change = 0; 2509 - } 2510 - /* 2511 - * If neither the size nor the allocation are being changed there is 2512 - * nothing to do. 2513 - */ 2514 - if (!size_change && !alloc_change) 2515 - goto unm_done; 2516 - /* If the size is changing, check if new size is allowed in $AttrDef. */ 2517 - if (size_change) { 2518 - err = ntfs_attr_size_bounds_check(vol, ni->type, new_size); 2519 - if (unlikely(err)) { 2520 - if (err == -ERANGE) { 2521 - ntfs_error(vol->sb, "Truncate would cause the " 2522 - "inode 0x%lx to %simum size " 2523 - "for its attribute type " 2524 - "(0x%x). Aborting truncate.", 2525 - vi->i_ino, 2526 - new_size > old_size ? "exceed " 2527 - "the max" : "go under the min", 2528 - le32_to_cpu(ni->type)); 2529 - err = -EFBIG; 2530 - } else { 2531 - ntfs_error(vol->sb, "Inode 0x%lx has unknown " 2532 - "attribute type 0x%x. " 2533 - "Aborting truncate.", 2534 - vi->i_ino, 2535 - le32_to_cpu(ni->type)); 2536 - err = -EIO; 2537 - } 2538 - /* Reset the vfs inode size to the old size. */ 2539 - i_size_write(vi, old_size); 2540 - goto err_out; 2541 - } 2542 - } 2543 - if (NInoCompressed(ni) || NInoEncrypted(ni)) { 2544 - ntfs_warning(vi->i_sb, "Changes in inode size are not " 2545 - "supported yet for %s files, ignoring.", 2546 - NInoCompressed(ni) ? "compressed" : 2547 - "encrypted"); 2548 - err = -EOPNOTSUPP; 2549 - goto bad_out; 2550 - } 2551 - if (a->non_resident) 2552 - goto do_non_resident_truncate; 2553 - BUG_ON(NInoNonResident(ni)); 2554 - /* Resize the attribute record to best fit the new attribute size. */ 2555 - if (new_size < vol->mft_record_size && 2556 - !ntfs_resident_attr_value_resize(m, a, new_size)) { 2557 - /* The resize succeeded! */ 2558 - flush_dcache_mft_record_page(ctx->ntfs_ino); 2559 - mark_mft_record_dirty(ctx->ntfs_ino); 2560 - write_lock_irqsave(&ni->size_lock, flags); 2561 - /* Update the sizes in the ntfs inode and all is done. */ 2562 - ni->allocated_size = le32_to_cpu(a->length) - 2563 - le16_to_cpu(a->data.resident.value_offset); 2564 - /* 2565 - * Note ntfs_resident_attr_value_resize() has already done any 2566 - * necessary data clearing in the attribute record. When the 2567 - * file is being shrunk vmtruncate() will already have cleared 2568 - * the top part of the last partial page, i.e. since this is 2569 - * the resident case this is the page with index 0. However, 2570 - * when the file is being expanded, the page cache page data 2571 - * between the old data_size, i.e. old_size, and the new_size 2572 - * has not been zeroed. Fortunately, we do not need to zero it 2573 - * either since on one hand it will either already be zero due 2574 - * to both read_folio and writepage clearing partial page data 2575 - * beyond i_size in which case there is nothing to do or in the 2576 - * case of the file being mmap()ped at the same time, POSIX 2577 - * specifies that the behaviour is unspecified thus we do not 2578 - * have to do anything. This means that in our implementation 2579 - * in the rare case that the file is mmap()ped and a write 2580 - * occurred into the mmap()ped region just beyond the file size 2581 - * and writepage has not yet been called to write out the page 2582 - * (which would clear the area beyond the file size) and we now 2583 - * extend the file size to incorporate this dirty region 2584 - * outside the file size, a write of the page would result in 2585 - * this data being written to disk instead of being cleared. 2586 - * Given both POSIX and the Linux mmap(2) man page specify that 2587 - * this corner case is undefined, we choose to leave it like 2588 - * that as this is much simpler for us as we cannot lock the 2589 - * relevant page now since we are holding too many ntfs locks 2590 - * which would result in a lock reversal deadlock. 2591 - */ 2592 - ni->initialized_size = new_size; 2593 - write_unlock_irqrestore(&ni->size_lock, flags); 2594 - goto unm_done; 2595 - } 2596 - /* If the above resize failed, this must be an attribute extension. */ 2597 - BUG_ON(size_change < 0); 2598 - /* 2599 - * We have to drop all the locks so we can call 2600 - * ntfs_attr_make_non_resident(). This could be optimised by try- 2601 - * locking the first page cache page and only if that fails dropping 2602 - * the locks, locking the page, and redoing all the locking and 2603 - * lookups. While this would be a huge optimisation, it is not worth 2604 - * it as this is definitely a slow code path as it only ever can happen 2605 - * once for any given file. 2606 - */ 2607 - ntfs_attr_put_search_ctx(ctx); 2608 - unmap_mft_record(base_ni); 2609 - up_write(&ni->runlist.lock); 2610 - /* 2611 - * Not enough space in the mft record, try to make the attribute 2612 - * non-resident and if successful restart the truncation process. 2613 - */ 2614 - err = ntfs_attr_make_non_resident(ni, old_size); 2615 - if (likely(!err)) 2616 - goto retry_truncate; 2617 - /* 2618 - * Could not make non-resident. If this is due to this not being 2619 - * permitted for this attribute type or there not being enough space, 2620 - * try to make other attributes non-resident. Otherwise fail. 2621 - */ 2622 - if (unlikely(err != -EPERM && err != -ENOSPC)) { 2623 - ntfs_error(vol->sb, "Cannot truncate inode 0x%lx, attribute " 2624 - "type 0x%x, because the conversion from " 2625 - "resident to non-resident attribute failed " 2626 - "with error code %i.", vi->i_ino, 2627 - (unsigned)le32_to_cpu(ni->type), err); 2628 - if (err != -ENOMEM) 2629 - err = -EIO; 2630 - goto conv_err_out; 2631 - } 2632 - /* TODO: Not implemented from here, abort. */ 2633 - if (err == -ENOSPC) 2634 - ntfs_error(vol->sb, "Not enough space in the mft record/on " 2635 - "disk for the non-resident attribute value. " 2636 - "This case is not implemented yet."); 2637 - else /* if (err == -EPERM) */ 2638 - ntfs_error(vol->sb, "This attribute type may not be " 2639 - "non-resident. This case is not implemented " 2640 - "yet."); 2641 - err = -EOPNOTSUPP; 2642 - goto conv_err_out; 2643 - #if 0 2644 - // TODO: Attempt to make other attributes non-resident. 2645 - if (!err) 2646 - goto do_resident_extend; 2647 - /* 2648 - * Both the attribute list attribute and the standard information 2649 - * attribute must remain in the base inode. Thus, if this is one of 2650 - * these attributes, we have to try to move other attributes out into 2651 - * extent mft records instead. 2652 - */ 2653 - if (ni->type == AT_ATTRIBUTE_LIST || 2654 - ni->type == AT_STANDARD_INFORMATION) { 2655 - // TODO: Attempt to move other attributes into extent mft 2656 - // records. 2657 - err = -EOPNOTSUPP; 2658 - if (!err) 2659 - goto do_resident_extend; 2660 - goto err_out; 2661 - } 2662 - // TODO: Attempt to move this attribute to an extent mft record, but 2663 - // only if it is not already the only attribute in an mft record in 2664 - // which case there would be nothing to gain. 2665 - err = -EOPNOTSUPP; 2666 - if (!err) 2667 - goto do_resident_extend; 2668 - /* There is nothing we can do to make enough space. )-: */ 2669 - goto err_out; 2670 - #endif 2671 - do_non_resident_truncate: 2672 - BUG_ON(!NInoNonResident(ni)); 2673 - if (alloc_change < 0) { 2674 - highest_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn); 2675 - if (highest_vcn > 0 && 2676 - old_alloc_size >> vol->cluster_size_bits > 2677 - highest_vcn + 1) { 2678 - /* 2679 - * This attribute has multiple extents. Not yet 2680 - * supported. 2681 - */ 2682 - ntfs_error(vol->sb, "Cannot truncate inode 0x%lx, " 2683 - "attribute type 0x%x, because the " 2684 - "attribute is highly fragmented (it " 2685 - "consists of multiple extents) and " 2686 - "this case is not implemented yet.", 2687 - vi->i_ino, 2688 - (unsigned)le32_to_cpu(ni->type)); 2689 - err = -EOPNOTSUPP; 2690 - goto bad_out; 2691 - } 2692 - } 2693 - /* 2694 - * If the size is shrinking, need to reduce the initialized_size and 2695 - * the data_size before reducing the allocation. 2696 - */ 2697 - if (size_change < 0) { 2698 - /* 2699 - * Make the valid size smaller (i_size is already up-to-date). 2700 - */ 2701 - write_lock_irqsave(&ni->size_lock, flags); 2702 - if (new_size < ni->initialized_size) { 2703 - ni->initialized_size = new_size; 2704 - a->data.non_resident.initialized_size = 2705 - cpu_to_sle64(new_size); 2706 - } 2707 - a->data.non_resident.data_size = cpu_to_sle64(new_size); 2708 - write_unlock_irqrestore(&ni->size_lock, flags); 2709 - flush_dcache_mft_record_page(ctx->ntfs_ino); 2710 - mark_mft_record_dirty(ctx->ntfs_ino); 2711 - /* If the allocated size is not changing, we are done. */ 2712 - if (!alloc_change) 2713 - goto unm_done; 2714 - /* 2715 - * If the size is shrinking it makes no sense for the 2716 - * allocation to be growing. 2717 - */ 2718 - BUG_ON(alloc_change > 0); 2719 - } else /* if (size_change >= 0) */ { 2720 - /* 2721 - * The file size is growing or staying the same but the 2722 - * allocation can be shrinking, growing or staying the same. 2723 - */ 2724 - if (alloc_change > 0) { 2725 - /* 2726 - * We need to extend the allocation and possibly update 2727 - * the data size. If we are updating the data size, 2728 - * since we are not touching the initialized_size we do 2729 - * not need to worry about the actual data on disk. 2730 - * And as far as the page cache is concerned, there 2731 - * will be no pages beyond the old data size and any 2732 - * partial region in the last page between the old and 2733 - * new data size (or the end of the page if the new 2734 - * data size is outside the page) does not need to be 2735 - * modified as explained above for the resident 2736 - * attribute truncate case. To do this, we simply drop 2737 - * the locks we hold and leave all the work to our 2738 - * friendly helper ntfs_attr_extend_allocation(). 2739 - */ 2740 - ntfs_attr_put_search_ctx(ctx); 2741 - unmap_mft_record(base_ni); 2742 - up_write(&ni->runlist.lock); 2743 - err = ntfs_attr_extend_allocation(ni, new_size, 2744 - size_change > 0 ? new_size : -1, -1); 2745 - /* 2746 - * ntfs_attr_extend_allocation() will have done error 2747 - * output already. 2748 - */ 2749 - goto done; 2750 - } 2751 - if (!alloc_change) 2752 - goto alloc_done; 2753 - } 2754 - /* alloc_change < 0 */ 2755 - /* Free the clusters. */ 2756 - nr_freed = ntfs_cluster_free(ni, new_alloc_size >> 2757 - vol->cluster_size_bits, -1, ctx); 2758 - m = ctx->mrec; 2759 - a = ctx->attr; 2760 - if (unlikely(nr_freed < 0)) { 2761 - ntfs_error(vol->sb, "Failed to release cluster(s) (error code " 2762 - "%lli). Unmount and run chkdsk to recover " 2763 - "the lost cluster(s).", (long long)nr_freed); 2764 - NVolSetErrors(vol); 2765 - nr_freed = 0; 2766 - } 2767 - /* Truncate the runlist. */ 2768 - err = ntfs_rl_truncate_nolock(vol, &ni->runlist, 2769 - new_alloc_size >> vol->cluster_size_bits); 2770 - /* 2771 - * If the runlist truncation failed and/or the search context is no 2772 - * longer valid, we cannot resize the attribute record or build the 2773 - * mapping pairs array thus we mark the inode bad so that no access to 2774 - * the freed clusters can happen. 2775 - */ 2776 - if (unlikely(err || IS_ERR(m))) { 2777 - ntfs_error(vol->sb, "Failed to %s (error code %li).%s", 2778 - IS_ERR(m) ? 2779 - "restore attribute search context" : 2780 - "truncate attribute runlist", 2781 - IS_ERR(m) ? PTR_ERR(m) : err, es); 2782 - err = -EIO; 2783 - goto bad_out; 2784 - } 2785 - /* Get the size for the shrunk mapping pairs array for the runlist. */ 2786 - mp_size = ntfs_get_size_for_mapping_pairs(vol, ni->runlist.rl, 0, -1); 2787 - if (unlikely(mp_size <= 0)) { 2788 - ntfs_error(vol->sb, "Cannot shrink allocation of inode 0x%lx, " 2789 - "attribute type 0x%x, because determining the " 2790 - "size for the mapping pairs failed with error " 2791 - "code %i.%s", vi->i_ino, 2792 - (unsigned)le32_to_cpu(ni->type), mp_size, es); 2793 - err = -EIO; 2794 - goto bad_out; 2795 - } 2796 - /* 2797 - * Shrink the attribute record for the new mapping pairs array. Note, 2798 - * this cannot fail since we are making the attribute smaller thus by 2799 - * definition there is enough space to do so. 2800 - */ 2801 - err = ntfs_attr_record_resize(m, a, mp_size + 2802 - le16_to_cpu(a->data.non_resident.mapping_pairs_offset)); 2803 - BUG_ON(err); 2804 - /* 2805 - * Generate the mapping pairs array directly into the attribute record. 2806 - */ 2807 - err = ntfs_mapping_pairs_build(vol, (u8*)a + 2808 - le16_to_cpu(a->data.non_resident.mapping_pairs_offset), 2809 - mp_size, ni->runlist.rl, 0, -1, NULL); 2810 - if (unlikely(err)) { 2811 - ntfs_error(vol->sb, "Cannot shrink allocation of inode 0x%lx, " 2812 - "attribute type 0x%x, because building the " 2813 - "mapping pairs failed with error code %i.%s", 2814 - vi->i_ino, (unsigned)le32_to_cpu(ni->type), 2815 - err, es); 2816 - err = -EIO; 2817 - goto bad_out; 2818 - } 2819 - /* Update the allocated/compressed size as well as the highest vcn. */ 2820 - a->data.non_resident.highest_vcn = cpu_to_sle64((new_alloc_size >> 2821 - vol->cluster_size_bits) - 1); 2822 - write_lock_irqsave(&ni->size_lock, flags); 2823 - ni->allocated_size = new_alloc_size; 2824 - a->data.non_resident.allocated_size = cpu_to_sle64(new_alloc_size); 2825 - if (NInoSparse(ni) || NInoCompressed(ni)) { 2826 - if (nr_freed) { 2827 - ni->itype.compressed.size -= nr_freed << 2828 - vol->cluster_size_bits; 2829 - BUG_ON(ni->itype.compressed.size < 0); 2830 - a->data.non_resident.compressed_size = cpu_to_sle64( 2831 - ni->itype.compressed.size); 2832 - vi->i_blocks = ni->itype.compressed.size >> 9; 2833 - } 2834 - } else 2835 - vi->i_blocks = new_alloc_size >> 9; 2836 - write_unlock_irqrestore(&ni->size_lock, flags); 2837 - /* 2838 - * We have shrunk the allocation. If this is a shrinking truncate we 2839 - * have already dealt with the initialized_size and the data_size above 2840 - * and we are done. If the truncate is only changing the allocation 2841 - * and not the data_size, we are also done. If this is an extending 2842 - * truncate, need to extend the data_size now which is ensured by the 2843 - * fact that @size_change is positive. 2844 - */ 2845 - alloc_done: 2846 - /* 2847 - * If the size is growing, need to update it now. If it is shrinking, 2848 - * we have already updated it above (before the allocation change). 2849 - */ 2850 - if (size_change > 0) 2851 - a->data.non_resident.data_size = cpu_to_sle64(new_size); 2852 - /* Ensure the modified mft record is written out. */ 2853 - flush_dcache_mft_record_page(ctx->ntfs_ino); 2854 - mark_mft_record_dirty(ctx->ntfs_ino); 2855 - unm_done: 2856 - ntfs_attr_put_search_ctx(ctx); 2857 - unmap_mft_record(base_ni); 2858 - up_write(&ni->runlist.lock); 2859 - done: 2860 - /* Update the mtime and ctime on the base inode. */ 2861 - /* normally ->truncate shouldn't update ctime or mtime, 2862 - * but ntfs did before so it got a copy & paste version 2863 - * of file_update_time. one day someone should fix this 2864 - * for real. 2865 - */ 2866 - if (!IS_NOCMTIME(VFS_I(base_ni)) && !IS_RDONLY(VFS_I(base_ni))) { 2867 - struct timespec64 now = current_time(VFS_I(base_ni)); 2868 - struct timespec64 ctime = inode_get_ctime(VFS_I(base_ni)); 2869 - struct timespec64 mtime = inode_get_mtime(VFS_I(base_ni)); 2870 - int sync_it = 0; 2871 - 2872 - if (!timespec64_equal(&mtime, &now) || 2873 - !timespec64_equal(&ctime, &now)) 2874 - sync_it = 1; 2875 - inode_set_ctime_to_ts(VFS_I(base_ni), now); 2876 - inode_set_mtime_to_ts(VFS_I(base_ni), now); 2877 - 2878 - if (sync_it) 2879 - mark_inode_dirty_sync(VFS_I(base_ni)); 2880 - } 2881 - 2882 - if (likely(!err)) { 2883 - NInoClearTruncateFailed(ni); 2884 - ntfs_debug("Done."); 2885 - } 2886 - return err; 2887 - old_bad_out: 2888 - old_size = -1; 2889 - bad_out: 2890 - if (err != -ENOMEM && err != -EOPNOTSUPP) 2891 - NVolSetErrors(vol); 2892 - if (err != -EOPNOTSUPP) 2893 - NInoSetTruncateFailed(ni); 2894 - else if (old_size >= 0) 2895 - i_size_write(vi, old_size); 2896 - err_out: 2897 - if (ctx) 2898 - ntfs_attr_put_search_ctx(ctx); 2899 - if (m) 2900 - unmap_mft_record(base_ni); 2901 - up_write(&ni->runlist.lock); 2902 - out: 2903 - ntfs_debug("Failed. Returning error code %i.", err); 2904 - return err; 2905 - conv_err_out: 2906 - if (err != -ENOMEM && err != -EOPNOTSUPP) 2907 - NVolSetErrors(vol); 2908 - if (err != -EOPNOTSUPP) 2909 - NInoSetTruncateFailed(ni); 2910 - else 2911 - i_size_write(vi, old_size); 2912 - goto out; 2913 - } 2914 - 2915 - /** 2916 - * ntfs_truncate_vfs - wrapper for ntfs_truncate() that has no return value 2917 - * @vi: inode for which the i_size was changed 2918 - * 2919 - * Wrapper for ntfs_truncate() that has no return value. 2920 - * 2921 - * See ntfs_truncate() description above for details. 2922 - */ 2923 - #ifdef NTFS_RW 2924 - void ntfs_truncate_vfs(struct inode *vi) { 2925 - ntfs_truncate(vi); 2926 - } 2927 - #endif 2928 - 2929 - /** 2930 - * ntfs_setattr - called from notify_change() when an attribute is being changed 2931 - * @idmap: idmap of the mount the inode was found from 2932 - * @dentry: dentry whose attributes to change 2933 - * @attr: structure describing the attributes and the changes 2934 - * 2935 - * We have to trap VFS attempts to truncate the file described by @dentry as 2936 - * soon as possible, because we do not implement changes in i_size yet. So we 2937 - * abort all i_size changes here. 2938 - * 2939 - * We also abort all changes of user, group, and mode as we do not implement 2940 - * the NTFS ACLs yet. 2941 - * 2942 - * Called with ->i_mutex held. 2943 - */ 2944 - int ntfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 2945 - struct iattr *attr) 2946 - { 2947 - struct inode *vi = d_inode(dentry); 2948 2354 int err; 2949 - unsigned int ia_valid = attr->ia_valid; 2950 2355 2951 - err = setattr_prepare(&nop_mnt_idmap, dentry, attr); 2356 + read_lock_irqsave(&ni->size_lock, flags); 2357 + old_init_size = ni->initialized_size; 2358 + read_unlock_irqrestore(&ni->size_lock, flags); 2359 + 2360 + if (!NInoNonResident(ni)) 2361 + return -EINVAL; 2362 + if (old_init_size >= new_size) 2363 + return 0; 2364 + 2365 + err = ntfs_attr_map_whole_runlist(ni); 2952 2366 if (err) 2953 - goto out; 2954 - /* We do not support NTFS ACLs yet. */ 2955 - if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE)) { 2956 - ntfs_warning(vi->i_sb, "Changes in user/group/mode are not " 2957 - "supported yet, ignoring."); 2958 - err = -EOPNOTSUPP; 2959 - goto out; 2367 + return err; 2368 + 2369 + if (!NInoCompressed(ni) && old_init_size < offset) { 2370 + err = iomap_zero_range(vi, old_init_size, 2371 + offset - old_init_size, 2372 + NULL, &ntfs_seek_iomap_ops, 2373 + &ntfs_iomap_folio_ops, NULL); 2374 + if (err) 2375 + return err; 2376 + if (bsync) 2377 + err = filemap_write_and_wait_range(vi->i_mapping, 2378 + old_init_size, 2379 + offset - 1); 2960 2380 } 2961 - if (ia_valid & ATTR_SIZE) { 2962 - if (attr->ia_size != i_size_read(vi)) { 2963 - ntfs_inode *ni = NTFS_I(vi); 2964 - /* 2965 - * FIXME: For now we do not support resizing of 2966 - * compressed or encrypted files yet. 2967 - */ 2968 - if (NInoCompressed(ni) || NInoEncrypted(ni)) { 2969 - ntfs_warning(vi->i_sb, "Changes in inode size " 2970 - "are not supported yet for " 2971 - "%s files, ignoring.", 2972 - NInoCompressed(ni) ? 2973 - "compressed" : "encrypted"); 2974 - err = -EOPNOTSUPP; 2975 - } else { 2976 - truncate_setsize(vi, attr->ia_size); 2977 - ntfs_truncate_vfs(vi); 2978 - } 2979 - if (err || ia_valid == ATTR_SIZE) 2980 - goto out; 2981 - } else { 2982 - /* 2983 - * We skipped the truncate but must still update 2984 - * timestamps. 2985 - */ 2986 - ia_valid |= ATTR_MTIME | ATTR_CTIME; 2987 - } 2988 - } 2989 - if (ia_valid & ATTR_ATIME) 2990 - inode_set_atime_to_ts(vi, attr->ia_atime); 2991 - if (ia_valid & ATTR_MTIME) 2992 - inode_set_mtime_to_ts(vi, attr->ia_mtime); 2993 - if (ia_valid & ATTR_CTIME) 2994 - inode_set_ctime_to_ts(vi, attr->ia_ctime); 2995 - mark_inode_dirty(vi); 2996 - out: 2381 + 2382 + 2383 + mutex_lock(&ni->mrec_lock); 2384 + err = ntfs_attr_set_initialized_size(ni, new_size); 2385 + mutex_unlock(&ni->mrec_lock); 2386 + if (err) 2387 + truncate_setsize(vi, old_init_size); 2997 2388 return err; 2998 2389 } 2999 2390 3000 - /** 3001 - * __ntfs_write_inode - write out a dirty inode 3002 - * @vi: inode to write out 3003 - * @sync: if true, write out synchronously 3004 - * 3005 - * Write out a dirty inode to disk including any extent inodes if present. 3006 - * 3007 - * If @sync is true, commit the inode to disk and wait for io completion. This 3008 - * is done using write_mft_record(). 3009 - * 3010 - * If @sync is false, just schedule the write to happen but do not wait for i/o 3011 - * completion. In 2.6 kernels, scheduling usually happens just by virtue of 3012 - * marking the page (and in this case mft record) dirty but we do not implement 3013 - * this yet as write_mft_record() largely ignores the @sync parameter and 3014 - * always performs synchronous writes. 3015 - * 3016 - * Return 0 on success and -errno on error. 3017 - */ 3018 - int __ntfs_write_inode(struct inode *vi, int sync) 2391 + int ntfs_truncate_vfs(struct inode *vi, loff_t new_size, loff_t i_size) 3019 2392 { 3020 - sle64 nt; 3021 - ntfs_inode *ni = NTFS_I(vi); 3022 - ntfs_attr_search_ctx *ctx; 3023 - MFT_RECORD *m; 3024 - STANDARD_INFORMATION *si; 2393 + struct ntfs_inode *ni = NTFS_I(vi); 2394 + int err; 2395 + 2396 + mutex_lock(&ni->mrec_lock); 2397 + err = __ntfs_attr_truncate_vfs(ni, new_size, i_size); 2398 + mutex_unlock(&ni->mrec_lock); 2399 + if (err < 0) 2400 + return err; 2401 + 2402 + inode_set_mtime_to_ts(vi, inode_set_ctime_current(vi)); 2403 + return 0; 2404 + } 2405 + 2406 + /* 2407 + * ntfs_inode_sync_standard_information - update standard information attribute 2408 + * @vi: inode to update standard information 2409 + * @m: mft record 2410 + * 2411 + * Return 0 on success or -errno on error. 2412 + */ 2413 + static int ntfs_inode_sync_standard_information(struct inode *vi, struct mft_record *m) 2414 + { 2415 + struct ntfs_inode *ni = NTFS_I(vi); 2416 + struct ntfs_attr_search_ctx *ctx; 2417 + struct standard_information *si; 2418 + __le64 nt; 3025 2419 int err = 0; 3026 2420 bool modified = false; 3027 2421 3028 - ntfs_debug("Entering for %sinode 0x%lx.", NInoAttr(ni) ? "attr " : "", 3029 - vi->i_ino); 3030 - /* 3031 - * Dirty attribute inodes are written via their real inodes so just 3032 - * clean them here. Access time updates are taken care off when the 3033 - * real inode is written. 3034 - */ 3035 - if (NInoAttr(ni)) { 3036 - NInoClearDirty(ni); 3037 - ntfs_debug("Done."); 3038 - return 0; 3039 - } 3040 - /* Map, pin, and lock the mft record belonging to the inode. */ 3041 - m = map_mft_record(ni); 3042 - if (IS_ERR(m)) { 3043 - err = PTR_ERR(m); 3044 - goto err_out; 3045 - } 3046 2422 /* Update the access times in the standard information attribute. */ 3047 2423 ctx = ntfs_attr_get_search_ctx(ni, m); 3048 - if (unlikely(!ctx)) { 3049 - err = -ENOMEM; 3050 - goto unm_err_out; 3051 - } 2424 + if (unlikely(!ctx)) 2425 + return -ENOMEM; 3052 2426 err = ntfs_attr_lookup(AT_STANDARD_INFORMATION, NULL, 0, 3053 2427 CASE_SENSITIVE, 0, NULL, 0, ctx); 3054 2428 if (unlikely(err)) { 3055 2429 ntfs_attr_put_search_ctx(ctx); 3056 - goto unm_err_out; 2430 + return err; 3057 2431 } 3058 - si = (STANDARD_INFORMATION*)((u8*)ctx->attr + 2432 + si = (struct standard_information *)((u8 *)ctx->attr + 3059 2433 le16_to_cpu(ctx->attr->data.resident.value_offset)); 2434 + if (si->file_attributes != ni->flags) { 2435 + si->file_attributes = ni->flags; 2436 + modified = true; 2437 + } 2438 + 2439 + /* Update the creation times if they have changed. */ 2440 + nt = utc2ntfs(ni->i_crtime); 2441 + if (si->creation_time != nt) { 2442 + ntfs_debug("Updating creation time for inode 0x%lx: old = 0x%llx, new = 0x%llx", 2443 + vi->i_ino, le64_to_cpu(si->creation_time), 2444 + le64_to_cpu(nt)); 2445 + si->creation_time = nt; 2446 + modified = true; 2447 + } 2448 + 3060 2449 /* Update the access times if they have changed. */ 3061 2450 nt = utc2ntfs(inode_get_mtime(vi)); 3062 2451 if (si->last_data_change_time != nt) { 3063 - ntfs_debug("Updating mtime for inode 0x%lx: old = 0x%llx, " 3064 - "new = 0x%llx", vi->i_ino, (long long) 3065 - sle64_to_cpu(si->last_data_change_time), 3066 - (long long)sle64_to_cpu(nt)); 2452 + ntfs_debug("Updating mtime for inode 0x%lx: old = 0x%llx, new = 0x%llx", 2453 + vi->i_ino, le64_to_cpu(si->last_data_change_time), 2454 + le64_to_cpu(nt)); 3067 2455 si->last_data_change_time = nt; 3068 2456 modified = true; 3069 2457 } 2458 + 3070 2459 nt = utc2ntfs(inode_get_ctime(vi)); 3071 2460 if (si->last_mft_change_time != nt) { 3072 - ntfs_debug("Updating ctime for inode 0x%lx: old = 0x%llx, " 3073 - "new = 0x%llx", vi->i_ino, (long long) 3074 - sle64_to_cpu(si->last_mft_change_time), 3075 - (long long)sle64_to_cpu(nt)); 2461 + ntfs_debug("Updating ctime for inode 0x%lx: old = 0x%llx, new = 0x%llx", 2462 + vi->i_ino, le64_to_cpu(si->last_mft_change_time), 2463 + le64_to_cpu(nt)); 3076 2464 si->last_mft_change_time = nt; 3077 2465 modified = true; 3078 2466 } 3079 2467 nt = utc2ntfs(inode_get_atime(vi)); 3080 2468 if (si->last_access_time != nt) { 3081 - ntfs_debug("Updating atime for inode 0x%lx: old = 0x%llx, " 3082 - "new = 0x%llx", vi->i_ino, 3083 - (long long)sle64_to_cpu(si->last_access_time), 3084 - (long long)sle64_to_cpu(nt)); 2469 + ntfs_debug("Updating atime for inode 0x%lx: old = 0x%llx, new = 0x%llx", 2470 + vi->i_ino, 2471 + le64_to_cpu(si->last_access_time), 2472 + le64_to_cpu(nt)); 3085 2473 si->last_access_time = nt; 3086 2474 modified = true; 3087 2475 } 2476 + 3088 2477 /* 3089 2478 * If we just modified the standard information attribute we need to 3090 2479 * mark the mft record it is in dirty. We do this manually so that ··· 2545 3034 * and the base mft record may actually not have been modified so it 2546 3035 * might not need to be written out. 2547 3036 * NOTE: It is not a problem when the inode for $MFT itself is being 2548 - * written out as mark_ntfs_record_dirty() will only set I_DIRTY_PAGES 2549 - * on the $MFT inode and hence __ntfs_write_inode() will not be 3037 + * written out as ntfs_mft_mark_dirty() will only set I_DIRTY_PAGES 3038 + * on the $MFT inode and hence ntfs_write_inode() will not be 2550 3039 * re-invoked because of it which in turn is ok since the dirtied mft 2551 3040 * record will be cleaned and written out to disk below, i.e. before 2552 3041 * this function returns. 2553 3042 */ 2554 - if (modified) { 2555 - flush_dcache_mft_record_page(ctx->ntfs_ino); 2556 - if (!NInoTestSetDirty(ctx->ntfs_ino)) 2557 - mark_ntfs_record_dirty(ctx->ntfs_ino->page, 2558 - ctx->ntfs_ino->page_ofs); 2559 - } 3043 + if (modified) 3044 + NInoSetDirty(ctx->ntfs_ino); 2560 3045 ntfs_attr_put_search_ctx(ctx); 3046 + 3047 + return err; 3048 + } 3049 + 3050 + /* 3051 + * ntfs_inode_sync_filename - update FILE_NAME attributes 3052 + * @ni: ntfs inode to update FILE_NAME attributes 3053 + * 3054 + * Update all FILE_NAME attributes for inode @ni in the index. 3055 + * 3056 + * Return 0 on success or error. 3057 + */ 3058 + int ntfs_inode_sync_filename(struct ntfs_inode *ni) 3059 + { 3060 + struct inode *index_vi; 3061 + struct super_block *sb = VFS_I(ni)->i_sb; 3062 + struct ntfs_attr_search_ctx *ctx = NULL; 3063 + struct ntfs_index_context *ictx; 3064 + struct ntfs_inode *index_ni; 3065 + struct file_name_attr *fn; 3066 + struct file_name_attr *fnx; 3067 + struct reparse_point *rpp; 3068 + __le32 reparse_tag; 3069 + int err = 0; 3070 + unsigned long flags; 3071 + 3072 + ntfs_debug("Entering for inode %lld\n", (long long)ni->mft_no); 3073 + 3074 + ctx = ntfs_attr_get_search_ctx(ni, NULL); 3075 + if (!ctx) 3076 + return -ENOMEM; 3077 + 3078 + /* Collect the reparse tag, if any */ 3079 + reparse_tag = cpu_to_le32(0); 3080 + if (ni->flags & FILE_ATTR_REPARSE_POINT) { 3081 + if (!ntfs_attr_lookup(AT_REPARSE_POINT, NULL, 3082 + 0, CASE_SENSITIVE, 0, NULL, 0, ctx)) { 3083 + rpp = (struct reparse_point *)((u8 *)ctx->attr + 3084 + le16_to_cpu(ctx->attr->data.resident.value_offset)); 3085 + reparse_tag = rpp->reparse_tag; 3086 + } 3087 + ntfs_attr_reinit_search_ctx(ctx); 3088 + } 3089 + 3090 + /* Walk through all FILE_NAME attributes and update them. */ 3091 + while (!(err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0, NULL, 0, ctx))) { 3092 + fn = (struct file_name_attr *)((u8 *)ctx->attr + 3093 + le16_to_cpu(ctx->attr->data.resident.value_offset)); 3094 + if (MREF_LE(fn->parent_directory) == ni->mft_no) 3095 + continue; 3096 + 3097 + index_vi = ntfs_iget(sb, MREF_LE(fn->parent_directory)); 3098 + if (IS_ERR(index_vi)) { 3099 + ntfs_error(sb, "Failed to open inode %lld with index", 3100 + (long long)MREF_LE(fn->parent_directory)); 3101 + continue; 3102 + } 3103 + 3104 + index_ni = NTFS_I(index_vi); 3105 + 3106 + mutex_lock_nested(&index_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT); 3107 + if (NInoBeingDeleted(ni)) { 3108 + iput(index_vi); 3109 + mutex_unlock(&index_ni->mrec_lock); 3110 + continue; 3111 + } 3112 + 3113 + ictx = ntfs_index_ctx_get(index_ni, I30, 4); 3114 + if (!ictx) { 3115 + ntfs_error(sb, "Failed to get index ctx, inode %lld", 3116 + (long long)index_ni->mft_no); 3117 + iput(index_vi); 3118 + mutex_unlock(&index_ni->mrec_lock); 3119 + continue; 3120 + } 3121 + 3122 + err = ntfs_index_lookup(fn, sizeof(struct file_name_attr), ictx); 3123 + if (err) { 3124 + ntfs_debug("Index lookup failed, inode %lld", 3125 + (long long)index_ni->mft_no); 3126 + ntfs_index_ctx_put(ictx); 3127 + iput(index_vi); 3128 + mutex_unlock(&index_ni->mrec_lock); 3129 + continue; 3130 + } 3131 + /* Update flags and file size. */ 3132 + fnx = (struct file_name_attr *)ictx->data; 3133 + fnx->file_attributes = 3134 + (fnx->file_attributes & ~FILE_ATTR_VALID_FLAGS) | 3135 + (ni->flags & FILE_ATTR_VALID_FLAGS); 3136 + if (ctx->mrec->flags & MFT_RECORD_IS_DIRECTORY) 3137 + fnx->data_size = fnx->allocated_size = 0; 3138 + else { 3139 + read_lock_irqsave(&ni->size_lock, flags); 3140 + if (NInoSparse(ni) || NInoCompressed(ni)) 3141 + fnx->allocated_size = cpu_to_le64(ni->itype.compressed.size); 3142 + else 3143 + fnx->allocated_size = cpu_to_le64(ni->allocated_size); 3144 + fnx->data_size = cpu_to_le64(ni->data_size); 3145 + 3146 + /* 3147 + * The file name record has also to be fixed if some 3148 + * attribute update implied the unnamed data to be 3149 + * made non-resident 3150 + */ 3151 + fn->allocated_size = fnx->allocated_size; 3152 + fn->data_size = fnx->data_size; 3153 + read_unlock_irqrestore(&ni->size_lock, flags); 3154 + } 3155 + 3156 + /* update or clear the reparse tag in the index */ 3157 + fnx->type.rp.reparse_point_tag = reparse_tag; 3158 + fnx->creation_time = fn->creation_time; 3159 + fnx->last_data_change_time = fn->last_data_change_time; 3160 + fnx->last_mft_change_time = fn->last_mft_change_time; 3161 + fnx->last_access_time = fn->last_access_time; 3162 + ntfs_index_entry_mark_dirty(ictx); 3163 + ntfs_icx_ib_sync_write(ictx); 3164 + NInoSetDirty(ctx->ntfs_ino); 3165 + ntfs_index_ctx_put(ictx); 3166 + mutex_unlock(&index_ni->mrec_lock); 3167 + iput(index_vi); 3168 + } 3169 + /* Check for real error occurred. */ 3170 + if (err != -ENOENT) { 3171 + ntfs_error(sb, "Attribute lookup failed, err : %d, inode %lld", err, 3172 + (long long)ni->mft_no); 3173 + } else 3174 + err = 0; 3175 + 3176 + ntfs_attr_put_search_ctx(ctx); 3177 + return err; 3178 + } 3179 + 3180 + int ntfs_get_block_mft_record(struct ntfs_inode *mft_ni, struct ntfs_inode *ni) 3181 + { 3182 + s64 vcn; 3183 + struct runlist_element *rl; 3184 + 3185 + if (ni->mft_lcn[0] != LCN_RL_NOT_MAPPED) 3186 + return 0; 3187 + 3188 + vcn = (s64)ni->mft_no << mft_ni->vol->mft_record_size_bits >> 3189 + mft_ni->vol->cluster_size_bits; 3190 + 3191 + rl = mft_ni->runlist.rl; 3192 + if (!rl) { 3193 + ntfs_error(mft_ni->vol->sb, "$MFT runlist is not present"); 3194 + return -EIO; 3195 + } 3196 + 3197 + /* Seek to element containing target vcn. */ 3198 + while (rl->length && rl[1].vcn <= vcn) 3199 + rl++; 3200 + ni->mft_lcn[0] = ntfs_rl_vcn_to_lcn(rl, vcn); 3201 + ni->mft_lcn_count = 1; 3202 + 3203 + if (mft_ni->vol->cluster_size < mft_ni->vol->mft_record_size && 3204 + (rl->length - (vcn - rl->vcn)) <= 1) { 3205 + rl++; 3206 + ni->mft_lcn[1] = ntfs_rl_vcn_to_lcn(rl, vcn + 1); 3207 + ni->mft_lcn_count++; 3208 + } 3209 + return 0; 3210 + } 3211 + 3212 + /* 3213 + * __ntfs_write_inode - write out a dirty inode 3214 + * @vi: inode to write out 3215 + * @sync: if true, write out synchronously 3216 + * 3217 + * Write out a dirty inode to disk including any extent inodes if present. 3218 + * 3219 + * If @sync is true, commit the inode to disk and wait for io completion. This 3220 + * is done using write_mft_record(). 3221 + * 3222 + * If @sync is false, just schedule the write to happen but do not wait for i/o 3223 + * completion. 3224 + * 3225 + * Return 0 on success and -errno on error. 3226 + */ 3227 + int __ntfs_write_inode(struct inode *vi, int sync) 3228 + { 3229 + struct ntfs_inode *ni = NTFS_I(vi); 3230 + struct ntfs_inode *mft_ni = NTFS_I(ni->vol->mft_ino); 3231 + struct mft_record *m; 3232 + int err = 0; 3233 + bool need_iput = false; 3234 + 3235 + ntfs_debug("Entering for %sinode 0x%lx.", NInoAttr(ni) ? "attr " : "", 3236 + vi->i_ino); 3237 + 3238 + if (NVolShutdown(ni->vol)) 3239 + return -EIO; 3240 + 3241 + /* 3242 + * Dirty attribute inodes are written via their real inodes so just 3243 + * clean them here. Access time updates are taken care off when the 3244 + * real inode is written. 3245 + */ 3246 + if (NInoAttr(ni) || ni->nr_extents == -1) { 3247 + NInoClearDirty(ni); 3248 + ntfs_debug("Done."); 3249 + return 0; 3250 + } 3251 + 3252 + /* igrab prevents vi from being evicted while mrec_lock is hold. */ 3253 + if (igrab(vi) != NULL) 3254 + need_iput = true; 3255 + 3256 + mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 3257 + /* Map, pin, and lock the mft record belonging to the inode. */ 3258 + m = map_mft_record(ni); 3259 + if (IS_ERR(m)) { 3260 + mutex_unlock(&ni->mrec_lock); 3261 + err = PTR_ERR(m); 3262 + goto err_out; 3263 + } 3264 + 3265 + if (NInoNonResident(ni) && NInoRunlistDirty(ni)) { 3266 + down_write(&ni->runlist.lock); 3267 + err = ntfs_attr_update_mapping_pairs(ni, 0); 3268 + if (!err) 3269 + NInoClearRunlistDirty(ni); 3270 + up_write(&ni->runlist.lock); 3271 + } 3272 + 3273 + err = ntfs_inode_sync_standard_information(vi, m); 3274 + if (err) 3275 + goto unm_err_out; 3276 + 3277 + /* 3278 + * when being umounted and inodes are evicted, write_inode() 3279 + * is called with all inodes being marked with I_FREEING. 3280 + * then ntfs_inode_sync_filename() waits infinitly because 3281 + * of ntfs_iget. This situation happens only where sync_filesysem() 3282 + * from umount fails because of a disk unplug and etc. 3283 + * the absent of SB_ACTIVE means umounting. 3284 + */ 3285 + if ((vi->i_sb->s_flags & SB_ACTIVE) && NInoTestClearFileNameDirty(ni)) 3286 + ntfs_inode_sync_filename(ni); 3287 + 2561 3288 /* Now the access times are updated, write the base mft record. */ 2562 - if (NInoDirty(ni)) 3289 + if (NInoDirty(ni)) { 3290 + down_read(&mft_ni->runlist.lock); 3291 + err = ntfs_get_block_mft_record(mft_ni, ni); 3292 + up_read(&mft_ni->runlist.lock); 3293 + if (err) 3294 + goto unm_err_out; 3295 + 2563 3296 err = write_mft_record(ni, m, sync); 3297 + if (err) 3298 + ntfs_error(vi->i_sb, "write_mft_record failed, err : %d\n", err); 3299 + } 3300 + unmap_mft_record(ni); 3301 + 3302 + /* Map any unmapped extent mft records with LCNs. */ 3303 + down_read(&mft_ni->runlist.lock); 3304 + mutex_lock(&ni->extent_lock); 3305 + if (ni->nr_extents > 0) { 3306 + int i; 3307 + 3308 + for (i = 0; i < ni->nr_extents; i++) { 3309 + err = ntfs_get_block_mft_record(mft_ni, 3310 + ni->ext.extent_ntfs_inos[i]); 3311 + if (err) { 3312 + mutex_unlock(&ni->extent_lock); 3313 + up_read(&mft_ni->runlist.lock); 3314 + mutex_unlock(&ni->mrec_lock); 3315 + goto err_out; 3316 + } 3317 + } 3318 + } 3319 + mutex_unlock(&ni->extent_lock); 3320 + up_read(&mft_ni->runlist.lock); 3321 + 2564 3322 /* Write all attached extent mft records. */ 2565 3323 mutex_lock(&ni->extent_lock); 2566 3324 if (ni->nr_extents > 0) { 2567 - ntfs_inode **extent_nis = ni->ext.extent_ntfs_inos; 3325 + struct ntfs_inode **extent_nis = ni->ext.extent_ntfs_inos; 2568 3326 int i; 2569 3327 2570 3328 ntfs_debug("Writing %i extent inodes.", ni->nr_extents); 2571 3329 for (i = 0; i < ni->nr_extents; i++) { 2572 - ntfs_inode *tni = extent_nis[i]; 3330 + struct ntfs_inode *tni = extent_nis[i]; 2573 3331 2574 3332 if (NInoDirty(tni)) { 2575 - MFT_RECORD *tm = map_mft_record(tni); 3333 + struct mft_record *tm; 2576 3334 int ret; 2577 3335 3336 + mutex_lock(&tni->mrec_lock); 3337 + tm = map_mft_record(tni); 2578 3338 if (IS_ERR(tm)) { 3339 + mutex_unlock(&tni->mrec_lock); 2579 3340 if (!err || err == -ENOMEM) 2580 3341 err = PTR_ERR(tm); 2581 3342 continue; 2582 3343 } 3344 + 2583 3345 ret = write_mft_record(tni, tm, sync); 2584 3346 unmap_mft_record(tni); 3347 + mutex_unlock(&tni->mrec_lock); 3348 + 2585 3349 if (unlikely(ret)) { 2586 3350 if (!err || err == -ENOMEM) 2587 3351 err = ret; ··· 2865 3079 } 2866 3080 } 2867 3081 mutex_unlock(&ni->extent_lock); 2868 - unmap_mft_record(ni); 3082 + mutex_unlock(&ni->mrec_lock); 3083 + 2869 3084 if (unlikely(err)) 2870 3085 goto err_out; 3086 + if (need_iput) 3087 + iput(vi); 2871 3088 ntfs_debug("Done."); 2872 3089 return 0; 2873 3090 unm_err_out: 2874 3091 unmap_mft_record(ni); 3092 + mutex_unlock(&ni->mrec_lock); 2875 3093 err_out: 2876 - if (err == -ENOMEM) { 2877 - ntfs_warning(vi->i_sb, "Not enough memory to write inode. " 2878 - "Marking the inode dirty again, so the VFS " 2879 - "retries later."); 3094 + if (err == -ENOMEM) 2880 3095 mark_inode_dirty(vi); 2881 - } else { 3096 + else { 2882 3097 ntfs_error(vi->i_sb, "Failed (error %i): Run chkdsk.", -err); 2883 3098 NVolSetErrors(ni->vol); 2884 3099 } 3100 + if (need_iput) 3101 + iput(vi); 2885 3102 return err; 2886 3103 } 2887 3104 2888 - #endif /* NTFS_RW */ 3105 + /* 3106 + * ntfs_extent_inode_open - load an extent inode and attach it to its base 3107 + * @base_ni: base ntfs inode 3108 + * @mref: mft reference of the extent inode to load (in little endian) 3109 + * 3110 + * First check if the extent inode @mref is already attached to the base ntfs 3111 + * inode @base_ni, and if so, return a pointer to the attached extent inode. 3112 + * 3113 + * If the extent inode is not already attached to the base inode, allocate an 3114 + * ntfs_inode structure and initialize it for the given inode @mref. @mref 3115 + * specifies the inode number / mft record to read, including the sequence 3116 + * number, which can be 0 if no sequence number checking is to be performed. 3117 + * 3118 + * Then, allocate a buffer for the mft record, read the mft record from the 3119 + * volume @base_ni->vol, and attach it to the ntfs_inode structure (->mrec). 3120 + * The mft record is mst deprotected and sanity checked for validity and we 3121 + * abort if deprotection or checks fail. 3122 + * 3123 + * Finally attach the ntfs inode to its base inode @base_ni and return a 3124 + * pointer to the ntfs_inode structure on success or NULL on error, with errno 3125 + * set to the error code. 3126 + * 3127 + * Note, extent inodes are never closed directly. They are automatically 3128 + * disposed off by the closing of the base inode. 3129 + */ 3130 + static struct ntfs_inode *ntfs_extent_inode_open(struct ntfs_inode *base_ni, 3131 + const __le64 mref) 3132 + { 3133 + u64 mft_no = MREF_LE(mref); 3134 + struct ntfs_inode *ni = NULL; 3135 + struct ntfs_inode **extent_nis; 3136 + int i; 3137 + struct mft_record *ni_mrec; 3138 + struct super_block *sb; 3139 + 3140 + if (!base_ni) 3141 + return NULL; 3142 + 3143 + sb = base_ni->vol->sb; 3144 + ntfs_debug("Opening extent inode %lld (base mft record %lld).\n", 3145 + (unsigned long long)mft_no, 3146 + (unsigned long long)base_ni->mft_no); 3147 + 3148 + /* Is the extent inode already open and attached to the base inode? */ 3149 + if (base_ni->nr_extents > 0) { 3150 + extent_nis = base_ni->ext.extent_ntfs_inos; 3151 + for (i = 0; i < base_ni->nr_extents; i++) { 3152 + u16 seq_no; 3153 + 3154 + ni = extent_nis[i]; 3155 + if (mft_no != ni->mft_no) 3156 + continue; 3157 + ni_mrec = map_mft_record(ni); 3158 + if (IS_ERR(ni_mrec)) { 3159 + ntfs_error(sb, "failed to map mft record for %lu", 3160 + ni->mft_no); 3161 + goto out; 3162 + } 3163 + /* Verify the sequence number if given. */ 3164 + seq_no = MSEQNO_LE(mref); 3165 + if (seq_no && 3166 + seq_no != le16_to_cpu(ni_mrec->sequence_number)) { 3167 + ntfs_error(sb, "Found stale extent mft reference mft=%lld", 3168 + (long long)ni->mft_no); 3169 + unmap_mft_record(ni); 3170 + goto out; 3171 + } 3172 + unmap_mft_record(ni); 3173 + goto out; 3174 + } 3175 + } 3176 + /* Wasn't there, we need to load the extent inode. */ 3177 + ni = ntfs_new_extent_inode(base_ni->vol->sb, mft_no); 3178 + if (!ni) 3179 + goto out; 3180 + 3181 + ni->seq_no = (u16)MSEQNO_LE(mref); 3182 + ni->nr_extents = -1; 3183 + ni->ext.base_ntfs_ino = base_ni; 3184 + /* Attach extent inode to base inode, reallocating memory if needed. */ 3185 + if (!(base_ni->nr_extents & 3)) { 3186 + i = (base_ni->nr_extents + 4) * sizeof(struct ntfs_inode *); 3187 + 3188 + extent_nis = kvzalloc(i, GFP_NOFS); 3189 + if (!extent_nis) 3190 + goto err_out; 3191 + if (base_ni->nr_extents) { 3192 + memcpy(extent_nis, base_ni->ext.extent_ntfs_inos, 3193 + i - 4 * sizeof(struct ntfs_inode *)); 3194 + kvfree(base_ni->ext.extent_ntfs_inos); 3195 + } 3196 + base_ni->ext.extent_ntfs_inos = extent_nis; 3197 + } 3198 + base_ni->ext.extent_ntfs_inos[base_ni->nr_extents++] = ni; 3199 + 3200 + out: 3201 + ntfs_debug("\n"); 3202 + return ni; 3203 + err_out: 3204 + ntfs_destroy_ext_inode(ni); 3205 + ni = NULL; 3206 + goto out; 3207 + } 3208 + 3209 + /* 3210 + * ntfs_inode_attach_all_extents - attach all extents for target inode 3211 + * @ni: opened ntfs inode for which perform attach 3212 + * 3213 + * Return 0 on success and error. 3214 + */ 3215 + int ntfs_inode_attach_all_extents(struct ntfs_inode *ni) 3216 + { 3217 + struct attr_list_entry *ale; 3218 + u64 prev_attached = 0; 3219 + 3220 + if (!ni) { 3221 + ntfs_debug("Invalid arguments.\n"); 3222 + return -EINVAL; 3223 + } 3224 + 3225 + if (NInoAttr(ni)) 3226 + ni = ni->ext.base_ntfs_ino; 3227 + 3228 + ntfs_debug("Entering for inode 0x%llx.\n", (long long) ni->mft_no); 3229 + 3230 + /* Inode haven't got attribute list, thus nothing to attach. */ 3231 + if (!NInoAttrList(ni)) 3232 + return 0; 3233 + 3234 + if (!ni->attr_list) { 3235 + ntfs_debug("Corrupt in-memory struct.\n"); 3236 + return -EINVAL; 3237 + } 3238 + 3239 + /* Walk through attribute list and attach all extents. */ 3240 + ale = (struct attr_list_entry *)ni->attr_list; 3241 + while ((u8 *)ale < ni->attr_list + ni->attr_list_size) { 3242 + if (ni->mft_no != MREF_LE(ale->mft_reference) && 3243 + prev_attached != MREF_LE(ale->mft_reference)) { 3244 + if (!ntfs_extent_inode_open(ni, ale->mft_reference)) { 3245 + ntfs_debug("Couldn't attach extent inode.\n"); 3246 + return -1; 3247 + } 3248 + prev_attached = MREF_LE(ale->mft_reference); 3249 + } 3250 + ale = (struct attr_list_entry *)((u8 *)ale + le16_to_cpu(ale->length)); 3251 + } 3252 + return 0; 3253 + } 3254 + 3255 + /* 3256 + * ntfs_inode_add_attrlist - add attribute list to inode and fill it 3257 + * @ni: opened ntfs inode to which add attribute list 3258 + * 3259 + * Return 0 on success or error. 3260 + */ 3261 + int ntfs_inode_add_attrlist(struct ntfs_inode *ni) 3262 + { 3263 + int err; 3264 + struct ntfs_attr_search_ctx *ctx; 3265 + u8 *al = NULL, *aln; 3266 + int al_len = 0; 3267 + struct attr_list_entry *ale = NULL; 3268 + struct mft_record *ni_mrec; 3269 + u32 attr_al_len; 3270 + 3271 + if (!ni) 3272 + return -EINVAL; 3273 + 3274 + ntfs_debug("inode %llu\n", (unsigned long long) ni->mft_no); 3275 + 3276 + if (NInoAttrList(ni) || ni->nr_extents) { 3277 + ntfs_error(ni->vol->sb, "Inode already has attribute list"); 3278 + return -EEXIST; 3279 + } 3280 + 3281 + ni_mrec = map_mft_record(ni); 3282 + if (IS_ERR(ni_mrec)) 3283 + return -EIO; 3284 + 3285 + /* Form attribute list. */ 3286 + ctx = ntfs_attr_get_search_ctx(ni, ni_mrec); 3287 + if (!ctx) { 3288 + err = -ENOMEM; 3289 + goto err_out; 3290 + } 3291 + 3292 + /* Walk through all attributes. */ 3293 + while (!(err = ntfs_attr_lookup(AT_UNUSED, NULL, 0, 0, 0, NULL, 0, ctx))) { 3294 + int ale_size; 3295 + 3296 + if (ctx->attr->type == AT_ATTRIBUTE_LIST) { 3297 + err = -EIO; 3298 + ntfs_error(ni->vol->sb, "Attribute list already present"); 3299 + goto put_err_out; 3300 + } 3301 + 3302 + ale_size = (sizeof(struct attr_list_entry) + sizeof(__le16) * 3303 + ctx->attr->name_length + 7) & ~7; 3304 + al_len += ale_size; 3305 + 3306 + aln = kvrealloc(al, al_len, GFP_NOFS); 3307 + if (!aln) { 3308 + err = -ENOMEM; 3309 + ntfs_error(ni->vol->sb, "Failed to realloc %d bytes", al_len); 3310 + goto put_err_out; 3311 + } 3312 + ale = (struct attr_list_entry *)(aln + ((u8 *)ale - al)); 3313 + al = aln; 3314 + 3315 + memset(ale, 0, ale_size); 3316 + 3317 + /* Add attribute to attribute list. */ 3318 + ale->type = ctx->attr->type; 3319 + ale->length = cpu_to_le16((sizeof(struct attr_list_entry) + 3320 + sizeof(__le16) * ctx->attr->name_length + 7) & ~7); 3321 + ale->name_length = ctx->attr->name_length; 3322 + ale->name_offset = (u8 *)ale->name - (u8 *)ale; 3323 + if (ctx->attr->non_resident) 3324 + ale->lowest_vcn = 3325 + ctx->attr->data.non_resident.lowest_vcn; 3326 + else 3327 + ale->lowest_vcn = 0; 3328 + ale->mft_reference = MK_LE_MREF(ni->mft_no, 3329 + le16_to_cpu(ni_mrec->sequence_number)); 3330 + ale->instance = ctx->attr->instance; 3331 + memcpy(ale->name, (u8 *)ctx->attr + 3332 + le16_to_cpu(ctx->attr->name_offset), 3333 + ctx->attr->name_length * sizeof(__le16)); 3334 + ale = (struct attr_list_entry *)(al + al_len); 3335 + } 3336 + 3337 + /* Check for real error occurred. */ 3338 + if (err != -ENOENT) { 3339 + ntfs_error(ni->vol->sb, "%s: Attribute lookup failed, inode %lld", 3340 + __func__, (long long)ni->mft_no); 3341 + goto put_err_out; 3342 + } 3343 + 3344 + /* Set in-memory attribute list. */ 3345 + ni->attr_list = al; 3346 + ni->attr_list_size = al_len; 3347 + NInoSetAttrList(ni); 3348 + 3349 + attr_al_len = offsetof(struct attr_record, data.resident.reserved) + 1 + 3350 + ((al_len + 7) & ~7); 3351 + /* Free space if there is not enough it for $ATTRIBUTE_LIST. */ 3352 + if (le32_to_cpu(ni_mrec->bytes_allocated) - 3353 + le32_to_cpu(ni_mrec->bytes_in_use) < attr_al_len) { 3354 + if (ntfs_inode_free_space(ni, (int)attr_al_len)) { 3355 + /* Failed to free space. */ 3356 + err = -ENOSPC; 3357 + ntfs_error(ni->vol->sb, "Failed to free space for attrlist"); 3358 + goto rollback; 3359 + } 3360 + } 3361 + 3362 + /* Add $ATTRIBUTE_LIST to mft record. */ 3363 + err = ntfs_resident_attr_record_add(ni, AT_ATTRIBUTE_LIST, AT_UNNAMED, 0, 3364 + NULL, al_len, 0); 3365 + if (err < 0) { 3366 + ntfs_error(ni->vol->sb, "Couldn't add $ATTRIBUTE_LIST to MFT"); 3367 + goto rollback; 3368 + } 3369 + 3370 + err = ntfs_attrlist_update(ni); 3371 + if (err < 0) 3372 + goto remove_attrlist_record; 3373 + 3374 + ntfs_attr_put_search_ctx(ctx); 3375 + unmap_mft_record(ni); 3376 + return 0; 3377 + 3378 + remove_attrlist_record: 3379 + /* Prevent ntfs_attr_recorm_rm from freeing attribute list. */ 3380 + ni->attr_list = NULL; 3381 + NInoClearAttrList(ni); 3382 + /* Remove $ATTRIBUTE_LIST record. */ 3383 + ntfs_attr_reinit_search_ctx(ctx); 3384 + if (!ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, 3385 + CASE_SENSITIVE, 0, NULL, 0, ctx)) { 3386 + if (ntfs_attr_record_rm(ctx)) 3387 + ntfs_error(ni->vol->sb, "Rollback failed to remove attrlist"); 3388 + } else { 3389 + ntfs_error(ni->vol->sb, "Rollback failed to find attrlist"); 3390 + } 3391 + 3392 + /* Setup back in-memory runlist. */ 3393 + ni->attr_list = al; 3394 + ni->attr_list_size = al_len; 3395 + NInoSetAttrList(ni); 3396 + rollback: 3397 + /* 3398 + * Scan attribute list for attributes that placed not in the base MFT 3399 + * record and move them to it. 3400 + */ 3401 + ntfs_attr_reinit_search_ctx(ctx); 3402 + ale = (struct attr_list_entry *)al; 3403 + while ((u8 *)ale < al + al_len) { 3404 + if (MREF_LE(ale->mft_reference) != ni->mft_no) { 3405 + if (!ntfs_attr_lookup(ale->type, ale->name, 3406 + ale->name_length, 3407 + CASE_SENSITIVE, 3408 + le64_to_cpu(ale->lowest_vcn), 3409 + NULL, 0, ctx)) { 3410 + if (ntfs_attr_record_move_to(ctx, ni)) 3411 + ntfs_error(ni->vol->sb, 3412 + "Rollback failed to move attribute"); 3413 + } else { 3414 + ntfs_error(ni->vol->sb, "Rollback failed to find attr"); 3415 + } 3416 + ntfs_attr_reinit_search_ctx(ctx); 3417 + } 3418 + ale = (struct attr_list_entry *)((u8 *)ale + le16_to_cpu(ale->length)); 3419 + } 3420 + 3421 + /* Remove in-memory attribute list. */ 3422 + ni->attr_list = NULL; 3423 + ni->attr_list_size = 0; 3424 + NInoClearAttrList(ni); 3425 + NInoClearAttrListDirty(ni); 3426 + put_err_out: 3427 + ntfs_attr_put_search_ctx(ctx); 3428 + err_out: 3429 + kvfree(al); 3430 + unmap_mft_record(ni); 3431 + return err; 3432 + } 3433 + 3434 + /* 3435 + * ntfs_inode_close - close an ntfs inode and free all associated memory 3436 + * @ni: ntfs inode to close 3437 + * 3438 + * Make sure the ntfs inode @ni is clean. 3439 + * 3440 + * If the ntfs inode @ni is a base inode, close all associated extent inodes, 3441 + * then deallocate all memory attached to it, and finally free the ntfs inode 3442 + * structure itself. 3443 + * 3444 + * If it is an extent inode, we disconnect it from its base inode before we 3445 + * destroy it. 3446 + * 3447 + * It is OK to pass NULL to this function, it is just noop in this case. 3448 + * 3449 + * Return 0 on success or error. 3450 + */ 3451 + int ntfs_inode_close(struct ntfs_inode *ni) 3452 + { 3453 + int err = -1; 3454 + struct ntfs_inode **tmp_nis; 3455 + struct ntfs_inode *base_ni; 3456 + s32 i; 3457 + 3458 + if (!ni) 3459 + return 0; 3460 + 3461 + ntfs_debug("Entering for inode %lld\n", (long long)ni->mft_no); 3462 + 3463 + /* Is this a base inode with mapped extent inodes? */ 3464 + /* 3465 + * If the inode is an extent inode, disconnect it from the 3466 + * base inode before destroying it. 3467 + */ 3468 + base_ni = ni->ext.base_ntfs_ino; 3469 + for (i = 0; i < base_ni->nr_extents; ++i) { 3470 + tmp_nis = base_ni->ext.extent_ntfs_inos; 3471 + if (tmp_nis[i] != ni) 3472 + continue; 3473 + /* Found it. Disconnect. */ 3474 + memmove(tmp_nis + i, tmp_nis + i + 1, 3475 + (base_ni->nr_extents - i - 1) * 3476 + sizeof(struct ntfs_inode *)); 3477 + /* Buffer should be for multiple of four extents. */ 3478 + if ((--base_ni->nr_extents) & 3) 3479 + break; 3480 + /* 3481 + * ElectricFence is unhappy with realloc(x,0) as free(x) 3482 + * thus we explicitly separate these two cases. 3483 + */ 3484 + if (base_ni->nr_extents) { 3485 + /* Resize the memory buffer. */ 3486 + tmp_nis = kvrealloc(tmp_nis, base_ni->nr_extents * 3487 + sizeof(struct ntfs_inode *), GFP_NOFS); 3488 + /* Ignore errors, they don't really matter. */ 3489 + if (tmp_nis) 3490 + base_ni->ext.extent_ntfs_inos = tmp_nis; 3491 + } else if (tmp_nis) { 3492 + kvfree(tmp_nis); 3493 + base_ni->ext.extent_ntfs_inos = NULL; 3494 + } 3495 + break; 3496 + } 3497 + 3498 + if (NInoDirty(ni)) 3499 + ntfs_error(ni->vol->sb, "Releasing dirty inode %lld!\n", 3500 + (long long)ni->mft_no); 3501 + if (NInoAttrList(ni) && ni->attr_list) 3502 + kvfree(ni->attr_list); 3503 + ntfs_destroy_ext_inode(ni); 3504 + err = 0; 3505 + ntfs_debug("\n"); 3506 + return err; 3507 + } 3508 + 3509 + void ntfs_destroy_ext_inode(struct ntfs_inode *ni) 3510 + { 3511 + ntfs_debug("Entering."); 3512 + if (ni == NULL) 3513 + return; 3514 + 3515 + ntfs_attr_close(ni); 3516 + 3517 + if (NInoDirty(ni)) 3518 + ntfs_error(ni->vol->sb, "Releasing dirty ext inode %lld!\n", 3519 + (long long)ni->mft_no); 3520 + if (NInoAttrList(ni) && ni->attr_list) 3521 + kvfree(ni->attr_list); 3522 + kfree(ni->mrec); 3523 + kmem_cache_free(ntfs_inode_cache, ni); 3524 + } 3525 + 3526 + static struct ntfs_inode *ntfs_inode_base(struct ntfs_inode *ni) 3527 + { 3528 + if (ni->nr_extents == -1) 3529 + return ni->ext.base_ntfs_ino; 3530 + return ni; 3531 + } 3532 + 3533 + static int ntfs_attr_position(__le32 type, struct ntfs_attr_search_ctx *ctx) 3534 + { 3535 + int err; 3536 + 3537 + err = ntfs_attr_lookup(type, NULL, 0, CASE_SENSITIVE, 0, NULL, 3538 + 0, ctx); 3539 + if (err) { 3540 + __le32 atype; 3541 + 3542 + if (err != -ENOENT) 3543 + return err; 3544 + 3545 + atype = ctx->attr->type; 3546 + if (atype == AT_END) 3547 + return -ENOSPC; 3548 + 3549 + /* 3550 + * if ntfs_external_attr_lookup return -ENOENT, ctx->al_entry 3551 + * could point to an attribute in an extent mft record, but 3552 + * ctx->attr and ctx->ntfs_ino always points to an attibute in 3553 + * a base mft record. 3554 + */ 3555 + if (ctx->al_entry && 3556 + MREF_LE(ctx->al_entry->mft_reference) != ctx->ntfs_ino->mft_no) { 3557 + ntfs_attr_reinit_search_ctx(ctx); 3558 + err = ntfs_attr_lookup(atype, NULL, 0, CASE_SENSITIVE, 0, NULL, 3559 + 0, ctx); 3560 + if (err) 3561 + return err; 3562 + } 3563 + } 3564 + return 0; 3565 + } 3566 + 3567 + /* 3568 + * ntfs_inode_free_space - free space in the MFT record of inode 3569 + * @ni: ntfs inode in which MFT record free space 3570 + * @size: amount of space needed to free 3571 + * 3572 + * Return 0 on success or error. 3573 + */ 3574 + int ntfs_inode_free_space(struct ntfs_inode *ni, int size) 3575 + { 3576 + struct ntfs_attr_search_ctx *ctx; 3577 + int freed, err; 3578 + struct mft_record *ni_mrec; 3579 + struct super_block *sb; 3580 + 3581 + if (!ni || size < 0) 3582 + return -EINVAL; 3583 + ntfs_debug("Entering for inode %lld, size %d\n", 3584 + (unsigned long long)ni->mft_no, size); 3585 + 3586 + sb = ni->vol->sb; 3587 + ni_mrec = map_mft_record(ni); 3588 + if (IS_ERR(ni_mrec)) 3589 + return -EIO; 3590 + 3591 + freed = (le32_to_cpu(ni_mrec->bytes_allocated) - 3592 + le32_to_cpu(ni_mrec->bytes_in_use)); 3593 + 3594 + unmap_mft_record(ni); 3595 + 3596 + if (size <= freed) 3597 + return 0; 3598 + 3599 + ctx = ntfs_attr_get_search_ctx(ni, NULL); 3600 + if (!ctx) { 3601 + ntfs_error(sb, "%s, Failed to get search context", __func__); 3602 + return -ENOMEM; 3603 + } 3604 + 3605 + /* 3606 + * Chkdsk complain if $STANDARD_INFORMATION is not in the base MFT 3607 + * record. 3608 + * 3609 + * Also we can't move $ATTRIBUTE_LIST from base MFT_RECORD, so position 3610 + * search context on first attribute after $STANDARD_INFORMATION and 3611 + * $ATTRIBUTE_LIST. 3612 + * 3613 + * Why we reposition instead of simply skip this attributes during 3614 + * enumeration? Because in case we have got only in-memory attribute 3615 + * list ntfs_attr_lookup will fail when it will try to find 3616 + * $ATTRIBUTE_LIST. 3617 + */ 3618 + err = ntfs_attr_position(AT_FILE_NAME, ctx); 3619 + if (err) 3620 + goto put_err_out; 3621 + 3622 + while (1) { 3623 + int record_size; 3624 + 3625 + /* 3626 + * Check whether attribute is from different MFT record. If so, 3627 + * find next, because we don't need such. 3628 + */ 3629 + while (ctx->ntfs_ino->mft_no != ni->mft_no) { 3630 + retry: 3631 + err = ntfs_attr_lookup(AT_UNUSED, NULL, 0, CASE_SENSITIVE, 3632 + 0, NULL, 0, ctx); 3633 + if (err) { 3634 + if (err != -ENOENT) 3635 + ntfs_error(sb, "Attr lookup failed #2"); 3636 + else if (ctx->attr->type == AT_END) 3637 + err = -ENOSPC; 3638 + else 3639 + err = 0; 3640 + 3641 + if (err) 3642 + goto put_err_out; 3643 + } 3644 + } 3645 + 3646 + if (ntfs_inode_base(ctx->ntfs_ino)->mft_no == FILE_MFT && 3647 + ctx->attr->type == AT_DATA) 3648 + goto retry; 3649 + 3650 + if (ctx->attr->type == AT_INDEX_ROOT) 3651 + goto retry; 3652 + 3653 + record_size = le32_to_cpu(ctx->attr->length); 3654 + 3655 + /* Move away attribute. */ 3656 + err = ntfs_attr_record_move_away(ctx, 0); 3657 + if (err) { 3658 + ntfs_error(sb, "Failed to move out attribute #2"); 3659 + break; 3660 + } 3661 + freed += record_size; 3662 + 3663 + /* Check whether we done. */ 3664 + if (size <= freed) { 3665 + ntfs_attr_put_search_ctx(ctx); 3666 + return 0; 3667 + } 3668 + 3669 + /* 3670 + * Reposition to first attribute after $STANDARD_INFORMATION and 3671 + * $ATTRIBUTE_LIST (see comments upwards). 3672 + */ 3673 + ntfs_attr_reinit_search_ctx(ctx); 3674 + err = ntfs_attr_position(AT_FILE_NAME, ctx); 3675 + if (err) 3676 + break; 3677 + } 3678 + put_err_out: 3679 + ntfs_attr_put_search_ctx(ctx); 3680 + if (err == -ENOSPC) 3681 + ntfs_debug("No attributes left that can be moved out.\n"); 3682 + return err; 3683 + } 3684 + 3685 + s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf) 3686 + { 3687 + struct address_space *mapping = vi->i_mapping; 3688 + struct folio *folio; 3689 + struct ntfs_inode *ni = NTFS_I(vi); 3690 + s64 isize; 3691 + u32 attr_len, total = 0, offset; 3692 + pgoff_t index; 3693 + int err = 0; 3694 + 3695 + WARN_ON(!NInoAttr(ni)); 3696 + if (!count) 3697 + return 0; 3698 + 3699 + mutex_lock(&ni->mrec_lock); 3700 + isize = i_size_read(vi); 3701 + if (pos > isize) { 3702 + mutex_unlock(&ni->mrec_lock); 3703 + return -EINVAL; 3704 + } 3705 + if (pos + count > isize) 3706 + count = isize - pos; 3707 + 3708 + if (!NInoNonResident(ni)) { 3709 + struct ntfs_attr_search_ctx *ctx; 3710 + u8 *attr; 3711 + 3712 + ctx = ntfs_attr_get_search_ctx(ni->ext.base_ntfs_ino, NULL); 3713 + if (!ctx) { 3714 + ntfs_error(vi->i_sb, "Failed to get attr search ctx"); 3715 + err = -ENOMEM; 3716 + mutex_unlock(&ni->mrec_lock); 3717 + goto out; 3718 + } 3719 + 3720 + err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE, 3721 + 0, NULL, 0, ctx); 3722 + if (err) { 3723 + ntfs_error(vi->i_sb, "Failed to look up attr %#x", ni->type); 3724 + ntfs_attr_put_search_ctx(ctx); 3725 + mutex_unlock(&ni->mrec_lock); 3726 + goto out; 3727 + } 3728 + 3729 + attr = (u8 *)ctx->attr + le16_to_cpu(ctx->attr->data.resident.value_offset); 3730 + memcpy(buf, (u8 *)attr + pos, count); 3731 + ntfs_attr_put_search_ctx(ctx); 3732 + mutex_unlock(&ni->mrec_lock); 3733 + return count; 3734 + } 3735 + mutex_unlock(&ni->mrec_lock); 3736 + 3737 + index = pos >> PAGE_SHIFT; 3738 + do { 3739 + /* Update @index and get the next folio. */ 3740 + folio = read_mapping_folio(mapping, index, NULL); 3741 + if (IS_ERR(folio)) 3742 + break; 3743 + 3744 + offset = offset_in_folio(folio, pos); 3745 + attr_len = min_t(size_t, (size_t)count, folio_size(folio) - offset); 3746 + 3747 + folio_lock(folio); 3748 + memcpy_from_folio(buf, folio, offset, attr_len); 3749 + folio_unlock(folio); 3750 + folio_put(folio); 3751 + 3752 + total += attr_len; 3753 + buf += attr_len; 3754 + pos += attr_len; 3755 + count -= attr_len; 3756 + index++; 3757 + } while (count); 3758 + out: 3759 + return err ? (s64)err : total; 3760 + } 3761 + 3762 + static inline int ntfs_enlarge_attribute(struct inode *vi, s64 pos, s64 count, 3763 + struct ntfs_attr_search_ctx *ctx) 3764 + { 3765 + struct ntfs_inode *ni = NTFS_I(vi); 3766 + struct super_block *sb = vi->i_sb; 3767 + int ret; 3768 + 3769 + if (pos + count <= ni->initialized_size) 3770 + return 0; 3771 + 3772 + if (NInoEncrypted(ni) && NInoNonResident(ni)) 3773 + return -EACCES; 3774 + 3775 + if (NInoCompressed(ni)) 3776 + return -EOPNOTSUPP; 3777 + 3778 + if (pos + count > ni->data_size) { 3779 + if (ntfs_attr_truncate(ni, pos + count)) { 3780 + ntfs_debug("Failed to truncate attribute"); 3781 + return -1; 3782 + } 3783 + 3784 + ntfs_attr_reinit_search_ctx(ctx); 3785 + ret = ntfs_attr_lookup(ni->type, 3786 + ni->name, ni->name_len, CASE_SENSITIVE, 3787 + 0, NULL, 0, ctx); 3788 + if (ret) { 3789 + ntfs_error(sb, "Failed to look up attr %#x", ni->type); 3790 + return ret; 3791 + } 3792 + } 3793 + 3794 + if (!NInoNonResident(ni)) { 3795 + if (likely(i_size_read(vi) < ni->data_size)) 3796 + i_size_write(vi, ni->data_size); 3797 + return 0; 3798 + } 3799 + 3800 + if (pos + count > ni->initialized_size) { 3801 + ctx->attr->data.non_resident.initialized_size = cpu_to_le64(pos + count); 3802 + mark_mft_record_dirty(ctx->ntfs_ino); 3803 + ni->initialized_size = pos + count; 3804 + if (i_size_read(vi) < ni->initialized_size) 3805 + i_size_write(vi, ni->initialized_size); 3806 + } 3807 + return 0; 3808 + } 3809 + 3810 + static s64 __ntfs_inode_resident_attr_pwrite(struct inode *vi, 3811 + s64 pos, s64 count, u8 *buf, 3812 + struct ntfs_attr_search_ctx *ctx) 3813 + { 3814 + struct ntfs_inode *ni = NTFS_I(vi); 3815 + struct folio *folio; 3816 + struct address_space *mapping = vi->i_mapping; 3817 + u8 *addr; 3818 + int err = 0; 3819 + 3820 + WARN_ON(NInoNonResident(ni)); 3821 + if (pos + count > PAGE_SIZE) { 3822 + ntfs_error(vi->i_sb, "Out of write into resident attr %#x", ni->type); 3823 + return -EINVAL; 3824 + } 3825 + 3826 + /* Copy to mft record page */ 3827 + addr = (u8 *)ctx->attr + le16_to_cpu(ctx->attr->data.resident.value_offset); 3828 + memcpy(addr + pos, buf, count); 3829 + mark_mft_record_dirty(ctx->ntfs_ino); 3830 + 3831 + /* Keep the first page clean and uptodate */ 3832 + folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN | FGP_NOFS, 3833 + mapping_gfp_mask(mapping)); 3834 + if (IS_ERR(folio)) { 3835 + err = PTR_ERR(folio); 3836 + ntfs_error(vi->i_sb, "Failed to read a page 0 for attr %#x: %d", 3837 + ni->type, err); 3838 + goto out; 3839 + } 3840 + if (!folio_test_uptodate(folio)) 3841 + folio_fill_tail(folio, 0, addr, 3842 + le32_to_cpu(ctx->attr->data.resident.value_length)); 3843 + else 3844 + memcpy_to_folio(folio, offset_in_folio(folio, pos), buf, count); 3845 + folio_mark_uptodate(folio); 3846 + folio_unlock(folio); 3847 + folio_put(folio); 3848 + out: 3849 + return err ? err : count; 3850 + } 3851 + 3852 + static s64 __ntfs_inode_non_resident_attr_pwrite(struct inode *vi, 3853 + s64 pos, s64 count, u8 *buf, 3854 + struct ntfs_attr_search_ctx *ctx, 3855 + bool sync) 3856 + { 3857 + struct ntfs_inode *ni = NTFS_I(vi); 3858 + struct address_space *mapping = vi->i_mapping; 3859 + struct folio *folio; 3860 + pgoff_t index; 3861 + unsigned long offset, length; 3862 + size_t attr_len; 3863 + s64 ret = 0, written = 0; 3864 + 3865 + WARN_ON(!NInoNonResident(ni)); 3866 + 3867 + index = pos >> PAGE_SHIFT; 3868 + while (count) { 3869 + if (count == PAGE_SIZE) { 3870 + folio = __filemap_get_folio(vi->i_mapping, index, 3871 + FGP_CREAT | FGP_LOCK, 3872 + mapping_gfp_mask(mapping)); 3873 + if (IS_ERR(folio)) { 3874 + ret = -ENOMEM; 3875 + break; 3876 + } 3877 + } else { 3878 + folio = read_mapping_folio(mapping, index, NULL); 3879 + if (IS_ERR(folio)) { 3880 + ret = PTR_ERR(folio); 3881 + ntfs_error(vi->i_sb, "Failed to read a page %lu for attr %#x: %ld", 3882 + index, ni->type, PTR_ERR(folio)); 3883 + break; 3884 + } 3885 + 3886 + folio_lock(folio); 3887 + } 3888 + 3889 + if (count == PAGE_SIZE) { 3890 + offset = 0; 3891 + attr_len = count; 3892 + } else { 3893 + offset = offset_in_folio(folio, pos); 3894 + attr_len = min_t(size_t, (size_t)count, folio_size(folio) - offset); 3895 + } 3896 + memcpy_to_folio(folio, offset, buf, attr_len); 3897 + 3898 + if (sync) { 3899 + struct ntfs_volume *vol = ni->vol; 3900 + s64 lcn, lcn_count; 3901 + unsigned int lcn_folio_off = 0; 3902 + struct bio *bio; 3903 + u64 rl_length = 0; 3904 + s64 vcn; 3905 + struct runlist_element *rl; 3906 + 3907 + lcn_count = max_t(s64, 1, ntfs_bytes_to_cluster(vol, attr_len)); 3908 + vcn = ntfs_pidx_to_cluster(vol, folio->index); 3909 + 3910 + do { 3911 + down_write(&ni->runlist.lock); 3912 + rl = ntfs_attr_vcn_to_rl(ni, vcn, &lcn); 3913 + if (IS_ERR(rl)) { 3914 + ret = PTR_ERR(rl); 3915 + up_write(&ni->runlist.lock); 3916 + goto err_unlock_folio; 3917 + } 3918 + 3919 + rl_length = rl->length - (vcn - rl->vcn); 3920 + if (rl_length < lcn_count) { 3921 + lcn_count -= rl_length; 3922 + } else { 3923 + rl_length = lcn_count; 3924 + lcn_count = 0; 3925 + } 3926 + up_write(&ni->runlist.lock); 3927 + 3928 + if (vol->cluster_size_bits > PAGE_SHIFT) { 3929 + lcn_folio_off = folio->index << PAGE_SHIFT; 3930 + lcn_folio_off &= vol->cluster_size_mask; 3931 + } 3932 + 3933 + bio = bio_alloc(vol->sb->s_bdev, 1, REQ_OP_WRITE, 3934 + GFP_NOIO); 3935 + bio->bi_iter.bi_sector = 3936 + ntfs_bytes_to_sector(vol, 3937 + ntfs_cluster_to_bytes(vol, lcn) + 3938 + lcn_folio_off); 3939 + 3940 + length = min_t(unsigned long, 3941 + ntfs_cluster_to_bytes(vol, rl_length), 3942 + folio_size(folio)); 3943 + if (!bio_add_folio(bio, folio, length, offset)) { 3944 + ret = -EIO; 3945 + bio_put(bio); 3946 + goto err_unlock_folio; 3947 + } 3948 + 3949 + submit_bio_wait(bio); 3950 + bio_put(bio); 3951 + vcn += rl_length; 3952 + offset += length; 3953 + } while (lcn_count != 0); 3954 + 3955 + folio_mark_uptodate(folio); 3956 + } else { 3957 + folio_mark_uptodate(folio); 3958 + folio_mark_dirty(folio); 3959 + } 3960 + err_unlock_folio: 3961 + folio_unlock(folio); 3962 + folio_put(folio); 3963 + 3964 + if (ret) 3965 + break; 3966 + 3967 + written += attr_len; 3968 + buf += attr_len; 3969 + pos += attr_len; 3970 + count -= attr_len; 3971 + index++; 3972 + 3973 + cond_resched(); 3974 + } 3975 + 3976 + return ret ? ret : written; 3977 + } 3978 + 3979 + s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, bool sync) 3980 + { 3981 + struct ntfs_inode *ni = NTFS_I(vi); 3982 + struct ntfs_attr_search_ctx *ctx; 3983 + s64 ret; 3984 + 3985 + WARN_ON(!NInoAttr(ni)); 3986 + 3987 + ctx = ntfs_attr_get_search_ctx(ni->ext.base_ntfs_ino, NULL); 3988 + if (!ctx) { 3989 + ntfs_error(vi->i_sb, "Failed to get attr search ctx"); 3990 + return -ENOMEM; 3991 + } 3992 + 3993 + ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE, 3994 + 0, NULL, 0, ctx); 3995 + if (ret) { 3996 + ntfs_attr_put_search_ctx(ctx); 3997 + ntfs_error(vi->i_sb, "Failed to look up attr %#x", ni->type); 3998 + return ret; 3999 + } 4000 + 4001 + mutex_lock(&ni->mrec_lock); 4002 + ret = ntfs_enlarge_attribute(vi, pos, count, ctx); 4003 + mutex_unlock(&ni->mrec_lock); 4004 + if (ret) 4005 + goto out; 4006 + 4007 + if (NInoNonResident(ni)) 4008 + ret = __ntfs_inode_non_resident_attr_pwrite(vi, pos, count, buf, ctx, sync); 4009 + else 4010 + ret = __ntfs_inode_resident_attr_pwrite(vi, pos, count, buf, ctx); 4011 + out: 4012 + ntfs_attr_put_search_ctx(ctx); 4013 + return ret; 4014 + } 4015 + 4016 + struct folio *ntfs_get_locked_folio(struct address_space *mapping, 4017 + pgoff_t index, pgoff_t end_index, struct file_ra_state *ra) 4018 + { 4019 + struct folio *folio; 4020 + 4021 + folio = filemap_lock_folio(mapping, index); 4022 + if (IS_ERR(folio)) { 4023 + if (PTR_ERR(folio) != -ENOENT) 4024 + return folio; 4025 + 4026 + page_cache_sync_readahead(mapping, ra, NULL, index, 4027 + end_index - index); 4028 + folio = read_mapping_folio(mapping, index, NULL); 4029 + if (!IS_ERR(folio)) 4030 + folio_lock(folio); 4031 + } 4032 + 4033 + return folio; 4034 + }
+1448 -145
fs/ntfs/namei.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-or-later 2 2 /* 3 - * namei.c - NTFS kernel directory inode operations. Part of the Linux-NTFS 4 - * project. 3 + * NTFS kernel directory inode operations. 5 4 * 6 5 * Copyright (c) 2001-2006 Anton Altaparmakov 6 + * Copyright (c) 2025 LG Electronics Co., Ltd. 7 7 */ 8 8 9 - #include <linux/dcache.h> 10 9 #include <linux/exportfs.h> 11 - #include <linux/security.h> 12 - #include <linux/slab.h> 10 + #include <linux/iversion.h> 13 11 14 - #include "attrib.h" 15 - #include "debug.h" 16 - #include "dir.h" 17 - #include "mft.h" 18 12 #include "ntfs.h" 13 + #include "time.h" 14 + #include "index.h" 15 + #include "reparse.h" 16 + #include "object_id.h" 17 + #include "ea.h" 19 18 20 - /** 19 + static const __le16 aux_name_le[3] = { 20 + cpu_to_le16('A'), cpu_to_le16('U'), cpu_to_le16('X') 21 + }; 22 + 23 + static const __le16 con_name_le[3] = { 24 + cpu_to_le16('C'), cpu_to_le16('O'), cpu_to_le16('N') 25 + }; 26 + 27 + static const __le16 com_name_le[3] = { 28 + cpu_to_le16('C'), cpu_to_le16('O'), cpu_to_le16('M') 29 + }; 30 + 31 + static const __le16 lpt_name_le[3] = { 32 + cpu_to_le16('L'), cpu_to_le16('P'), cpu_to_le16('T') 33 + }; 34 + 35 + static const __le16 nul_name_le[3] = { 36 + cpu_to_le16('N'), cpu_to_le16('U'), cpu_to_le16('L') 37 + }; 38 + 39 + static const __le16 prn_name_le[3] = { 40 + cpu_to_le16('P'), cpu_to_le16('R'), cpu_to_le16('N') 41 + }; 42 + 43 + static inline int ntfs_check_bad_char(const __le16 *wc, unsigned int wc_len) 44 + { 45 + int i; 46 + 47 + for (i = 0; i < wc_len; i++) { 48 + u16 c = le16_to_cpu(wc[i]); 49 + 50 + if (c < 0x0020 || 51 + c == 0x0022 || c == 0x002A || c == 0x002F || 52 + c == 0x003A || c == 0x003C || c == 0x003E || 53 + c == 0x003F || c == 0x005C || c == 0x007C) 54 + return -EINVAL; 55 + } 56 + 57 + return 0; 58 + } 59 + 60 + static int ntfs_check_bad_windows_name(struct ntfs_volume *vol, 61 + const __le16 *wc, 62 + unsigned int wc_len) 63 + { 64 + if (ntfs_check_bad_char(wc, wc_len)) 65 + return -EINVAL; 66 + 67 + if (!NVolCheckWindowsNames(vol)) 68 + return 0; 69 + 70 + /* Check for trailing space or dot. */ 71 + if (wc_len > 0 && 72 + (wc[wc_len - 1] == cpu_to_le16(' ') || 73 + wc[wc_len - 1] == cpu_to_le16('.'))) 74 + return -EINVAL; 75 + 76 + if (wc_len == 3 || (wc_len > 3 && wc[3] == cpu_to_le16('.'))) { 77 + __le16 *upcase = vol->upcase; 78 + u32 size = vol->upcase_len; 79 + 80 + if (ntfs_are_names_equal(wc, 3, aux_name_le, 3, IGNORE_CASE, upcase, size) || 81 + ntfs_are_names_equal(wc, 3, con_name_le, 3, IGNORE_CASE, upcase, size) || 82 + ntfs_are_names_equal(wc, 3, nul_name_le, 3, IGNORE_CASE, upcase, size) || 83 + ntfs_are_names_equal(wc, 3, prn_name_le, 3, IGNORE_CASE, upcase, size)) 84 + return -EINVAL; 85 + } 86 + 87 + if (wc_len == 4 || (wc_len > 4 && wc[4] == cpu_to_le16('.'))) { 88 + __le16 *upcase = vol->upcase; 89 + u32 size = vol->upcase_len, port; 90 + 91 + if (ntfs_are_names_equal(wc, 3, com_name_le, 3, IGNORE_CASE, upcase, size) || 92 + ntfs_are_names_equal(wc, 3, lpt_name_le, 3, IGNORE_CASE, upcase, size)) { 93 + port = le16_to_cpu(wc[3]); 94 + if (port >= '1' && port <= '9') 95 + return -EINVAL; 96 + } 97 + } 98 + return 0; 99 + } 100 + 101 + /* 21 102 * ntfs_lookup - find the inode represented by a dentry in a directory inode 22 103 * @dir_ino: directory inode in which to look for the inode 23 104 * @dent: dentry representing the inode to look for ··· 170 89 static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent, 171 90 unsigned int flags) 172 91 { 173 - ntfs_volume *vol = NTFS_SB(dir_ino->i_sb); 92 + struct ntfs_volume *vol = NTFS_SB(dir_ino->i_sb); 174 93 struct inode *dent_inode; 175 - ntfschar *uname; 176 - ntfs_name *name = NULL; 177 - MFT_REF mref; 94 + __le16 *uname; 95 + struct ntfs_name *name = NULL; 96 + u64 mref; 178 97 unsigned long dent_ino; 179 98 int uname_len; 180 99 ··· 182 101 dent, dir_ino->i_ino); 183 102 /* Convert the name of the dentry to Unicode. */ 184 103 uname_len = ntfs_nlstoucs(vol, dent->d_name.name, dent->d_name.len, 185 - &uname); 104 + &uname, NTFS_MAX_NAME_LEN); 186 105 if (uname_len < 0) { 187 106 if (uname_len != -ENAMETOOLONG) 188 - ntfs_error(vol->sb, "Failed to convert name to " 189 - "Unicode."); 107 + ntfs_debug("Failed to convert name to Unicode."); 190 108 return ERR_PTR(uname_len); 191 109 } 110 + mutex_lock(&NTFS_I(dir_ino)->mrec_lock); 192 111 mref = ntfs_lookup_inode_by_name(NTFS_I(dir_ino), uname, uname_len, 193 112 &name); 113 + mutex_unlock(&NTFS_I(dir_ino)->mrec_lock); 194 114 kmem_cache_free(ntfs_name_cache, uname); 195 115 if (!IS_ERR_MREF(mref)) { 196 116 dent_ino = MREF(mref); ··· 199 117 dent_inode = ntfs_iget(vol->sb, dent_ino); 200 118 if (!IS_ERR(dent_inode)) { 201 119 /* Consistency check. */ 202 - if (is_bad_inode(dent_inode) || MSEQNO(mref) == 203 - NTFS_I(dent_inode)->seq_no || 204 - dent_ino == FILE_MFT) { 120 + if (MSEQNO(mref) == NTFS_I(dent_inode)->seq_no || 121 + dent_ino == FILE_MFT) { 205 122 /* Perfect WIN32/POSIX match. -- Case 1. */ 206 123 if (!name) { 207 124 ntfs_debug("Done. (Case 1.)"); ··· 212 131 */ 213 132 goto handle_name; 214 133 } 215 - ntfs_error(vol->sb, "Found stale reference to inode " 216 - "0x%lx (reference sequence number = " 217 - "0x%x, inode sequence number = 0x%x), " 218 - "returning -EIO. Run chkdsk.", 219 - dent_ino, MSEQNO(mref), 220 - NTFS_I(dent_inode)->seq_no); 134 + ntfs_error(vol->sb, 135 + "Found stale reference to inode 0x%lx (reference sequence number = 0x%x, inode sequence number = 0x%x), returning -EIO. Run chkdsk.", 136 + dent_ino, MSEQNO(mref), 137 + NTFS_I(dent_inode)->seq_no); 221 138 iput(dent_inode); 222 139 dent_inode = ERR_PTR(-EIO); 223 140 } else 224 - ntfs_error(vol->sb, "ntfs_iget(0x%lx) failed with " 225 - "error code %li.", dent_ino, 226 - PTR_ERR(dent_inode)); 141 + ntfs_error(vol->sb, "ntfs_iget(0x%lx) failed with error code %li.", 142 + dent_ino, PTR_ERR(dent_inode)); 227 143 kfree(name); 228 144 /* Return the error code. */ 229 145 return ERR_CAST(dent_inode); 230 146 } 147 + kfree(name); 231 148 /* It is guaranteed that @name is no longer allocated at this point. */ 232 149 if (MREF_ERR(mref) == -ENOENT) { 233 150 ntfs_debug("Entry was not found, adding negative dentry."); ··· 234 155 ntfs_debug("Done."); 235 156 return NULL; 236 157 } 237 - ntfs_error(vol->sb, "ntfs_lookup_ino_by_name() failed with error " 238 - "code %i.", -MREF_ERR(mref)); 158 + ntfs_error(vol->sb, "ntfs_lookup_ino_by_name() failed with error code %i.", 159 + -MREF_ERR(mref)); 239 160 return ERR_PTR(MREF_ERR(mref)); 240 - // TODO: Consider moving this lot to a separate function! (AIA) 241 161 handle_name: 242 - { 243 - MFT_RECORD *m; 244 - ntfs_attr_search_ctx *ctx; 245 - ntfs_inode *ni = NTFS_I(dent_inode); 246 - int err; 247 - struct qstr nls_name; 162 + { 163 + struct mft_record *m; 164 + struct ntfs_attr_search_ctx *ctx; 165 + struct ntfs_inode *ni = NTFS_I(dent_inode); 166 + int err; 167 + struct qstr nls_name; 248 168 249 - nls_name.name = NULL; 250 - if (name->type != FILE_NAME_DOS) { /* Case 2. */ 251 - ntfs_debug("Case 2."); 252 - nls_name.len = (unsigned)ntfs_ucstonls(vol, 253 - (ntfschar*)&name->name, name->len, 254 - (unsigned char**)&nls_name.name, 0); 255 - kfree(name); 256 - } else /* if (name->type == FILE_NAME_DOS) */ { /* Case 3. */ 257 - FILE_NAME_ATTR *fn; 169 + nls_name.name = NULL; 170 + if (name->type != FILE_NAME_DOS) { /* Case 2. */ 171 + ntfs_debug("Case 2."); 172 + nls_name.len = (unsigned int)ntfs_ucstonls(vol, 173 + (__le16 *)&name->name, name->len, 174 + (unsigned char **)&nls_name.name, 0); 175 + kfree(name); 176 + } else /* if (name->type == FILE_NAME_DOS) */ { /* Case 3. */ 177 + struct file_name_attr *fn; 258 178 259 - ntfs_debug("Case 3."); 260 - kfree(name); 179 + ntfs_debug("Case 3."); 180 + kfree(name); 261 181 262 - /* Find the WIN32 name corresponding to the matched DOS name. */ 263 - ni = NTFS_I(dent_inode); 264 - m = map_mft_record(ni); 265 - if (IS_ERR(m)) { 266 - err = PTR_ERR(m); 267 - m = NULL; 268 - ctx = NULL; 182 + /* Find the WIN32 name corresponding to the matched DOS name. */ 183 + ni = NTFS_I(dent_inode); 184 + m = map_mft_record(ni); 185 + if (IS_ERR(m)) { 186 + err = PTR_ERR(m); 187 + m = NULL; 188 + ctx = NULL; 189 + goto err_out; 190 + } 191 + ctx = ntfs_attr_get_search_ctx(ni, m); 192 + if (unlikely(!ctx)) { 193 + err = -ENOMEM; 194 + goto err_out; 195 + } 196 + do { 197 + struct attr_record *a; 198 + u32 val_len; 199 + 200 + err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0, 201 + NULL, 0, ctx); 202 + if (unlikely(err)) { 203 + ntfs_error(vol->sb, 204 + "Inode corrupt: No WIN32 namespace counterpart to DOS file name. Run chkdsk."); 205 + if (err == -ENOENT) 206 + err = -EIO; 207 + goto err_out; 208 + } 209 + /* Consistency checks. */ 210 + a = ctx->attr; 211 + if (a->non_resident || a->flags) 212 + goto eio_err_out; 213 + val_len = le32_to_cpu(a->data.resident.value_length); 214 + if (le16_to_cpu(a->data.resident.value_offset) + 215 + val_len > le32_to_cpu(a->length)) 216 + goto eio_err_out; 217 + fn = (struct file_name_attr *)((u8 *)ctx->attr + le16_to_cpu( 218 + ctx->attr->data.resident.value_offset)); 219 + if ((u32)(fn->file_name_length * sizeof(__le16) + 220 + sizeof(struct file_name_attr)) > val_len) 221 + goto eio_err_out; 222 + } while (fn->file_name_type != FILE_NAME_WIN32); 223 + 224 + /* Convert the found WIN32 name to current NLS code page. */ 225 + nls_name.len = (unsigned int)ntfs_ucstonls(vol, 226 + (__le16 *)&fn->file_name, fn->file_name_length, 227 + (unsigned char **)&nls_name.name, 0); 228 + 229 + ntfs_attr_put_search_ctx(ctx); 230 + unmap_mft_record(ni); 231 + } 232 + m = NULL; 233 + ctx = NULL; 234 + 235 + /* Check if a conversion error occurred. */ 236 + if ((int)nls_name.len < 0) { 237 + err = (int)nls_name.len; 269 238 goto err_out; 270 239 } 271 - ctx = ntfs_attr_get_search_ctx(ni, m); 272 - if (unlikely(!ctx)) { 240 + nls_name.hash = full_name_hash(dent, nls_name.name, nls_name.len); 241 + 242 + dent = d_add_ci(dent, dent_inode, &nls_name); 243 + kfree(nls_name.name); 244 + return dent; 245 + 246 + eio_err_out: 247 + ntfs_error(vol->sb, "Illegal file name attribute. Run chkdsk."); 248 + err = -EIO; 249 + err_out: 250 + if (ctx) 251 + ntfs_attr_put_search_ctx(ctx); 252 + if (m) 253 + unmap_mft_record(ni); 254 + iput(dent_inode); 255 + ntfs_error(vol->sb, "Failed, returning error code %i.", err); 256 + return ERR_PTR(err); 257 + } 258 + } 259 + 260 + static int ntfs_sd_add_everyone(struct ntfs_inode *ni) 261 + { 262 + struct security_descriptor_relative *sd; 263 + struct ntfs_acl *acl; 264 + struct ntfs_ace *ace; 265 + struct ntfs_sid *sid; 266 + int ret, sd_len; 267 + 268 + /* Create SECURITY_DESCRIPTOR attribute (everyone has full access). */ 269 + /* 270 + * Calculate security descriptor length. We have 2 sub-authorities in 271 + * owner and group SIDs, So add 8 bytes to every SID. 272 + */ 273 + sd_len = sizeof(struct security_descriptor_relative) + 2 * 274 + (sizeof(struct ntfs_sid) + 8) + sizeof(struct ntfs_acl) + 275 + sizeof(struct ntfs_ace) + 4; 276 + sd = kmalloc(sd_len, GFP_NOFS); 277 + if (!sd) 278 + return -1; 279 + 280 + sd->revision = 1; 281 + sd->control = SE_DACL_PRESENT | SE_SELF_RELATIVE; 282 + 283 + sid = (struct ntfs_sid *)((u8 *)sd + sizeof(struct security_descriptor_relative)); 284 + sid->revision = 1; 285 + sid->sub_authority_count = 2; 286 + sid->sub_authority[0] = cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID); 287 + sid->sub_authority[1] = cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS); 288 + sid->identifier_authority.value[5] = 5; 289 + sd->owner = cpu_to_le32((u8 *)sid - (u8 *)sd); 290 + 291 + sid = (struct ntfs_sid *)((u8 *)sid + sizeof(struct ntfs_sid) + 8); 292 + sid->revision = 1; 293 + sid->sub_authority_count = 2; 294 + sid->sub_authority[0] = cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID); 295 + sid->sub_authority[1] = cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS); 296 + sid->identifier_authority.value[5] = 5; 297 + sd->group = cpu_to_le32((u8 *)sid - (u8 *)sd); 298 + 299 + acl = (struct ntfs_acl *)((u8 *)sid + sizeof(struct ntfs_sid) + 8); 300 + acl->revision = 2; 301 + acl->size = cpu_to_le16(sizeof(struct ntfs_acl) + sizeof(struct ntfs_ace) + 4); 302 + acl->ace_count = cpu_to_le16(1); 303 + sd->dacl = cpu_to_le32((u8 *)acl - (u8 *)sd); 304 + 305 + ace = (struct ntfs_ace *)((u8 *)acl + sizeof(struct ntfs_acl)); 306 + ace->type = ACCESS_ALLOWED_ACE_TYPE; 307 + ace->flags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE; 308 + ace->size = cpu_to_le16(sizeof(struct ntfs_ace) + 4); 309 + ace->mask = cpu_to_le32(0x1f01ff); 310 + ace->sid.revision = 1; 311 + ace->sid.sub_authority_count = 1; 312 + ace->sid.sub_authority[0] = 0; 313 + ace->sid.identifier_authority.value[5] = 1; 314 + 315 + ret = ntfs_attr_add(ni, AT_SECURITY_DESCRIPTOR, AT_UNNAMED, 0, (u8 *)sd, 316 + sd_len); 317 + if (ret) 318 + ntfs_error(ni->vol->sb, "Failed to add SECURITY_DESCRIPTOR\n"); 319 + 320 + kfree(sd); 321 + return ret; 322 + } 323 + 324 + static struct ntfs_inode *__ntfs_create(struct mnt_idmap *idmap, struct inode *dir, 325 + __le16 *name, u8 name_len, mode_t mode, dev_t dev, 326 + __le16 *target, int target_len) 327 + { 328 + struct ntfs_inode *dir_ni = NTFS_I(dir); 329 + struct ntfs_volume *vol = dir_ni->vol; 330 + struct ntfs_inode *ni; 331 + bool rollback_data = false, rollback_sd = false, rollback_reparse = false; 332 + struct file_name_attr *fn = NULL; 333 + struct standard_information *si = NULL; 334 + int err = 0, fn_len, si_len; 335 + struct inode *vi; 336 + struct mft_record *ni_mrec, *dni_mrec; 337 + struct super_block *sb = dir_ni->vol->sb; 338 + __le64 parent_mft_ref; 339 + u64 child_mft_ref; 340 + __le16 ea_size; 341 + 342 + vi = new_inode(vol->sb); 343 + if (!vi) 344 + return ERR_PTR(-ENOMEM); 345 + 346 + ntfs_init_big_inode(vi); 347 + ni = NTFS_I(vi); 348 + ni->vol = dir_ni->vol; 349 + ni->name_len = 0; 350 + ni->name = NULL; 351 + 352 + /* 353 + * Set the appropriate mode, attribute type, and name. For 354 + * directories, also setup the index values to the defaults. 355 + */ 356 + if (S_ISDIR(mode)) { 357 + mode &= ~vol->dmask; 358 + 359 + NInoSetMstProtected(ni); 360 + ni->itype.index.block_size = 4096; 361 + ni->itype.index.block_size_bits = ntfs_ffs(4096) - 1; 362 + ni->itype.index.collation_rule = COLLATION_FILE_NAME; 363 + if (vol->cluster_size <= ni->itype.index.block_size) { 364 + ni->itype.index.vcn_size = vol->cluster_size; 365 + ni->itype.index.vcn_size_bits = 366 + vol->cluster_size_bits; 367 + } else { 368 + ni->itype.index.vcn_size = vol->sector_size; 369 + ni->itype.index.vcn_size_bits = 370 + vol->sector_size_bits; 371 + } 372 + } else { 373 + mode &= ~vol->fmask; 374 + } 375 + 376 + if (IS_RDONLY(vi)) 377 + mode &= ~0222; 378 + 379 + inode_init_owner(idmap, vi, dir, mode); 380 + 381 + mode = vi->i_mode; 382 + 383 + #ifdef CONFIG_NTFS_FS_POSIX_ACL 384 + if (!S_ISLNK(mode) && (sb->s_flags & SB_POSIXACL)) { 385 + err = ntfs_init_acl(idmap, vi, dir); 386 + if (err) 387 + goto err_out; 388 + } else 389 + #endif 390 + { 391 + vi->i_flags |= S_NOSEC; 392 + } 393 + 394 + if (uid_valid(vol->uid)) 395 + vi->i_uid = vol->uid; 396 + 397 + if (gid_valid(vol->gid)) 398 + vi->i_gid = vol->gid; 399 + 400 + /* 401 + * Set the file size to 0, the ntfs inode sizes are set to 0 by 402 + * the call to ntfs_init_big_inode() below. 403 + */ 404 + vi->i_size = 0; 405 + vi->i_blocks = 0; 406 + 407 + inode_inc_iversion(vi); 408 + 409 + simple_inode_init_ts(vi); 410 + ni->i_crtime = inode_get_ctime(vi); 411 + 412 + inode_set_mtime_to_ts(dir, ni->i_crtime); 413 + inode_set_ctime_to_ts(dir, ni->i_crtime); 414 + mark_inode_dirty(dir); 415 + 416 + err = ntfs_mft_record_alloc(dir_ni->vol, mode, &ni, NULL, 417 + &ni_mrec); 418 + if (err) { 419 + iput(vi); 420 + return ERR_PTR(err); 421 + } 422 + 423 + /* 424 + * Prevent iget and writeback from finding this inode. 425 + * Caller must call d_instantiate_new instead of d_instantiate. 426 + */ 427 + spin_lock(&vi->i_lock); 428 + inode_state_set(vi, I_NEW | I_CREATING); 429 + spin_unlock(&vi->i_lock); 430 + 431 + /* Add the inode to the inode hash for the superblock. */ 432 + vi->i_ino = ni->mft_no; 433 + inode_set_iversion(vi, 1); 434 + insert_inode_hash(vi); 435 + 436 + mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 437 + mutex_lock_nested(&dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT); 438 + if (NInoBeingDeleted(dir_ni)) { 439 + err = -ENOENT; 440 + goto err_out; 441 + } 442 + 443 + dni_mrec = map_mft_record(dir_ni); 444 + if (IS_ERR(dni_mrec)) { 445 + ntfs_error(dir_ni->vol->sb, "failed to map mft record for file %ld.\n", 446 + dir_ni->mft_no); 447 + err = -EIO; 448 + goto err_out; 449 + } 450 + parent_mft_ref = MK_LE_MREF(dir_ni->mft_no, 451 + le16_to_cpu(dni_mrec->sequence_number)); 452 + unmap_mft_record(dir_ni); 453 + 454 + /* 455 + * Create STANDARD_INFORMATION attribute. Write STANDARD_INFORMATION 456 + * version 1.2, windows will upgrade it to version 3 if needed. 457 + */ 458 + si_len = offsetof(struct standard_information, file_attributes) + 459 + sizeof(__le32) + 12; 460 + si = kzalloc(si_len, GFP_NOFS); 461 + if (!si) { 462 + err = -ENOMEM; 463 + goto err_out; 464 + } 465 + 466 + si->creation_time = si->last_data_change_time = utc2ntfs(ni->i_crtime); 467 + si->last_mft_change_time = si->last_access_time = si->creation_time; 468 + 469 + if (!S_ISREG(mode) && !S_ISDIR(mode)) 470 + si->file_attributes = FILE_ATTR_SYSTEM; 471 + 472 + /* Add STANDARD_INFORMATION to inode. */ 473 + err = ntfs_attr_add(ni, AT_STANDARD_INFORMATION, AT_UNNAMED, 0, (u8 *)si, 474 + si_len); 475 + if (err) { 476 + ntfs_error(sb, "Failed to add STANDARD_INFORMATION attribute.\n"); 477 + goto err_out; 478 + } 479 + 480 + err = ntfs_sd_add_everyone(ni); 481 + if (err) 482 + goto err_out; 483 + rollback_sd = true; 484 + 485 + if (S_ISDIR(mode)) { 486 + struct index_root *ir = NULL; 487 + struct index_entry *ie; 488 + int ir_len, index_len; 489 + 490 + /* Create struct index_root attribute. */ 491 + index_len = sizeof(struct index_header) + sizeof(struct index_entry_header); 492 + ir_len = offsetof(struct index_root, index) + index_len; 493 + ir = kzalloc(ir_len, GFP_NOFS); 494 + if (!ir) { 273 495 err = -ENOMEM; 274 496 goto err_out; 275 497 } 276 - do { 277 - ATTR_RECORD *a; 278 - u32 val_len; 498 + ir->type = AT_FILE_NAME; 499 + ir->collation_rule = COLLATION_FILE_NAME; 500 + ir->index_block_size = cpu_to_le32(ni->vol->index_record_size); 501 + if (ni->vol->cluster_size <= ni->vol->index_record_size) 502 + ir->clusters_per_index_block = 503 + NTFS_B_TO_CLU(vol, ni->vol->index_record_size); 504 + else 505 + ir->clusters_per_index_block = 506 + ni->vol->index_record_size >> ni->vol->sector_size_bits; 507 + ir->index.entries_offset = cpu_to_le32(sizeof(struct index_header)); 508 + ir->index.index_length = cpu_to_le32(index_len); 509 + ir->index.allocated_size = cpu_to_le32(index_len); 510 + ie = (struct index_entry *)((u8 *)ir + sizeof(struct index_root)); 511 + ie->length = cpu_to_le16(sizeof(struct index_entry_header)); 512 + ie->key_length = 0; 513 + ie->flags = INDEX_ENTRY_END; 279 514 280 - err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0, 281 - NULL, 0, ctx); 282 - if (unlikely(err)) { 283 - ntfs_error(vol->sb, "Inode corrupt: No WIN32 " 284 - "namespace counterpart to DOS " 285 - "file name. Run chkdsk."); 286 - if (err == -ENOENT) 287 - err = -EIO; 288 - goto err_out; 289 - } 290 - /* Consistency checks. */ 291 - a = ctx->attr; 292 - if (a->non_resident || a->flags) 293 - goto eio_err_out; 294 - val_len = le32_to_cpu(a->data.resident.value_length); 295 - if (le16_to_cpu(a->data.resident.value_offset) + 296 - val_len > le32_to_cpu(a->length)) 297 - goto eio_err_out; 298 - fn = (FILE_NAME_ATTR*)((u8*)ctx->attr + le16_to_cpu( 299 - ctx->attr->data.resident.value_offset)); 300 - if ((u32)(fn->file_name_length * sizeof(ntfschar) + 301 - sizeof(FILE_NAME_ATTR)) > val_len) 302 - goto eio_err_out; 303 - } while (fn->file_name_type != FILE_NAME_WIN32); 515 + /* Add struct index_root attribute to inode. */ 516 + err = ntfs_attr_add(ni, AT_INDEX_ROOT, I30, 4, (u8 *)ir, ir_len); 517 + if (err) { 518 + kfree(ir); 519 + ntfs_error(vi->i_sb, "Failed to add struct index_root attribute.\n"); 520 + goto err_out; 521 + } 522 + kfree(ir); 523 + err = ntfs_attr_open(ni, AT_INDEX_ROOT, I30, 4); 524 + if (err) 525 + goto err_out; 526 + } else { 527 + /* Add DATA attribute to inode. */ 528 + err = ntfs_attr_add(ni, AT_DATA, AT_UNNAMED, 0, NULL, 0); 529 + if (err) { 530 + ntfs_error(dir_ni->vol->sb, "Failed to add DATA attribute.\n"); 531 + goto err_out; 532 + } 533 + rollback_data = true; 304 534 305 - /* Convert the found WIN32 name to current NLS code page. */ 306 - nls_name.len = (unsigned)ntfs_ucstonls(vol, 307 - (ntfschar*)&fn->file_name, fn->file_name_length, 308 - (unsigned char**)&nls_name.name, 0); 535 + err = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); 536 + if (err) 537 + goto err_out; 309 538 310 - ntfs_attr_put_search_ctx(ctx); 311 - unmap_mft_record(ni); 539 + if (S_ISLNK(mode)) { 540 + err = ntfs_reparse_set_wsl_symlink(ni, target, target_len); 541 + if (!err) 542 + rollback_reparse = true; 543 + } else if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode) || 544 + S_ISFIFO(mode)) { 545 + si->file_attributes = FILE_ATTRIBUTE_RECALL_ON_OPEN; 546 + ni->flags = FILE_ATTRIBUTE_RECALL_ON_OPEN; 547 + err = ntfs_reparse_set_wsl_not_symlink(ni, mode); 548 + if (!err) 549 + rollback_reparse = true; 550 + } 551 + if (err) 552 + goto err_out; 312 553 } 313 - m = NULL; 314 - ctx = NULL; 315 554 316 - /* Check if a conversion error occurred. */ 317 - if ((signed)nls_name.len < 0) { 318 - err = (signed)nls_name.len; 555 + err = ntfs_ea_set_wsl_inode(vi, dev, &ea_size, 556 + NTFS_EA_UID | NTFS_EA_GID | NTFS_EA_MODE); 557 + if (err) 558 + goto err_out; 559 + 560 + /* Create FILE_NAME attribute. */ 561 + fn_len = sizeof(struct file_name_attr) + name_len * sizeof(__le16); 562 + fn = kzalloc(fn_len, GFP_NOFS); 563 + if (!fn) { 564 + err = -ENOMEM; 319 565 goto err_out; 320 566 } 321 - nls_name.hash = full_name_hash(dent, nls_name.name, nls_name.len); 322 567 323 - dent = d_add_ci(dent, dent_inode, &nls_name); 324 - kfree(nls_name.name); 325 - return dent; 568 + fn->file_attributes |= ni->flags; 569 + fn->parent_directory = parent_mft_ref; 570 + fn->file_name_length = name_len; 571 + fn->file_name_type = FILE_NAME_POSIX; 572 + fn->type.ea.packed_ea_size = ea_size; 573 + if (S_ISDIR(mode)) { 574 + fn->file_attributes = FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT; 575 + fn->allocated_size = fn->data_size = 0; 576 + } else { 577 + fn->data_size = cpu_to_le64(ni->data_size); 578 + fn->allocated_size = cpu_to_le64(ni->allocated_size); 579 + } 580 + if (!S_ISREG(mode) && !S_ISDIR(mode)) { 581 + fn->file_attributes = FILE_ATTR_SYSTEM; 582 + if (rollback_reparse) 583 + fn->file_attributes |= FILE_ATTR_REPARSE_POINT; 584 + } 585 + if (NVolHideDotFiles(vol) && name_len > 0 && name[0] == cpu_to_le16('.')) 586 + fn->file_attributes |= FILE_ATTR_HIDDEN; 587 + fn->creation_time = fn->last_data_change_time = utc2ntfs(ni->i_crtime); 588 + fn->last_mft_change_time = fn->last_access_time = fn->creation_time; 589 + memcpy(fn->file_name, name, name_len * sizeof(__le16)); 326 590 327 - eio_err_out: 328 - ntfs_error(vol->sb, "Illegal file name attribute. Run chkdsk."); 329 - err = -EIO; 591 + /* Add FILE_NAME attribute to inode. */ 592 + err = ntfs_attr_add(ni, AT_FILE_NAME, AT_UNNAMED, 0, (u8 *)fn, fn_len); 593 + if (err) { 594 + ntfs_error(sb, "Failed to add FILE_NAME attribute.\n"); 595 + goto err_out; 596 + } 597 + 598 + child_mft_ref = MK_MREF(ni->mft_no, 599 + le16_to_cpu(ni_mrec->sequence_number)); 600 + /* Set hard links count and directory flag. */ 601 + ni_mrec->link_count = cpu_to_le16(1); 602 + mark_mft_record_dirty(ni); 603 + 604 + /* Add FILE_NAME attribute to index. */ 605 + err = ntfs_index_add_filename(dir_ni, fn, child_mft_ref); 606 + if (err) { 607 + ntfs_debug("Failed to add entry to the index"); 608 + goto err_out; 609 + } 610 + 611 + unmap_mft_record(ni); 612 + mutex_unlock(&dir_ni->mrec_lock); 613 + mutex_unlock(&ni->mrec_lock); 614 + 615 + ni->flags = fn->file_attributes; 616 + /* Set the sequence number. */ 617 + vi->i_generation = ni->seq_no; 618 + set_nlink(vi, 1); 619 + ntfs_set_vfs_operations(vi, mode, dev); 620 + 621 + /* Done! */ 622 + kfree(fn); 623 + kfree(si); 624 + ntfs_debug("Done.\n"); 625 + return ni; 626 + 330 627 err_out: 331 - if (ctx) 332 - ntfs_attr_put_search_ctx(ctx); 333 - if (m) 334 - unmap_mft_record(ni); 335 - iput(dent_inode); 336 - ntfs_error(vol->sb, "Failed, returning error code %i.", err); 628 + if (rollback_sd) 629 + ntfs_attr_remove(ni, AT_SECURITY_DESCRIPTOR, AT_UNNAMED, 0); 630 + 631 + if (rollback_data) 632 + ntfs_attr_remove(ni, AT_DATA, AT_UNNAMED, 0); 633 + 634 + if (rollback_reparse) 635 + ntfs_delete_reparse_index(ni); 636 + /* 637 + * Free extent MFT records (should not exist any with current 638 + * ntfs_create implementation, but for any case if something will be 639 + * changed in the future). 640 + */ 641 + while (ni->nr_extents != 0) { 642 + int err2; 643 + 644 + err2 = ntfs_mft_record_free(ni->vol, *(ni->ext.extent_ntfs_inos)); 645 + if (err2) 646 + ntfs_error(sb, 647 + "Failed to free extent MFT record. Leaving inconsistent metadata.\n"); 648 + ntfs_inode_close(*(ni->ext.extent_ntfs_inos)); 649 + } 650 + if (ntfs_mft_record_free(ni->vol, ni)) 651 + ntfs_error(sb, 652 + "Failed to free MFT record. Leaving inconsistent metadata. Run chkdsk.\n"); 653 + unmap_mft_record(ni); 654 + kfree(fn); 655 + kfree(si); 656 + 657 + mutex_unlock(&dir_ni->mrec_lock); 658 + mutex_unlock(&ni->mrec_lock); 659 + 660 + remove_inode_hash(vi); 661 + discard_new_inode(vi); 337 662 return ERR_PTR(err); 338 - } 663 + } 664 + 665 + static int ntfs_create(struct mnt_idmap *idmap, struct inode *dir, 666 + struct dentry *dentry, umode_t mode, bool excl) 667 + { 668 + struct ntfs_volume *vol = NTFS_SB(dir->i_sb); 669 + struct ntfs_inode *ni; 670 + __le16 *uname; 671 + int uname_len, err; 672 + 673 + if (NVolShutdown(vol)) 674 + return -EIO; 675 + 676 + uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len, 677 + &uname, NTFS_MAX_NAME_LEN); 678 + if (uname_len < 0) { 679 + if (uname_len != -ENAMETOOLONG) 680 + ntfs_error(vol->sb, "Failed to convert name to unicode."); 681 + return uname_len; 682 + } 683 + 684 + err = ntfs_check_bad_windows_name(vol, uname, uname_len); 685 + if (err) { 686 + kmem_cache_free(ntfs_name_cache, uname); 687 + return err; 688 + } 689 + 690 + if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 691 + ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 692 + 693 + ni = __ntfs_create(idmap, dir, uname, uname_len, S_IFREG | mode, 0, NULL, 0); 694 + kmem_cache_free(ntfs_name_cache, uname); 695 + if (IS_ERR(ni)) 696 + return PTR_ERR(ni); 697 + 698 + d_instantiate_new(dentry, VFS_I(ni)); 699 + 700 + return 0; 701 + } 702 + 703 + static int ntfs_check_unlinkable_dir(struct ntfs_attr_search_ctx *ctx, struct file_name_attr *fn) 704 + { 705 + int link_count; 706 + int ret; 707 + struct ntfs_inode *ni = ctx->base_ntfs_ino ? ctx->base_ntfs_ino : ctx->ntfs_ino; 708 + struct mft_record *ni_mrec = ctx->base_mrec ? ctx->base_mrec : ctx->mrec; 709 + 710 + ret = ntfs_check_empty_dir(ni, ni_mrec); 711 + if (!ret || ret != -ENOTEMPTY) 712 + return ret; 713 + 714 + link_count = le16_to_cpu(ni_mrec->link_count); 715 + /* 716 + * Directory is non-empty, so we can unlink only if there is more than 717 + * one "real" hard link, i.e. links aren't different DOS and WIN32 names 718 + */ 719 + if ((link_count == 1) || 720 + (link_count == 2 && fn->file_name_type == FILE_NAME_DOS)) { 721 + ret = -ENOTEMPTY; 722 + ntfs_debug("Non-empty directory without hard links\n"); 723 + goto no_hardlink; 724 + } 725 + 726 + ret = 0; 727 + no_hardlink: 728 + return ret; 729 + } 730 + 731 + static int ntfs_test_inode_attr(struct inode *vi, void *data) 732 + { 733 + struct ntfs_inode *ni = NTFS_I(vi); 734 + unsigned long mft_no = (unsigned long)data; 735 + 736 + if (ni->mft_no != mft_no) 737 + return 0; 738 + if (NInoAttr(ni) || ni->nr_extents == -1) 739 + return 1; 740 + else 741 + return 0; 742 + } 743 + 744 + /* 745 + * ntfs_delete - delete file or directory from ntfs volume 746 + * @ni: ntfs inode for object to delte 747 + * @dir_ni: ntfs inode for directory in which delete object 748 + * @name: unicode name of the object to delete 749 + * @name_len: length of the name in unicode characters 750 + * @need_lock: whether mrec lock is needed or not 751 + * 752 + * Delete the specified name from the directory index @dir_ni and decrement 753 + * the link count of the target inode @ni. 754 + * 755 + * Return 0 on success and -errno on error. 756 + */ 757 + static int ntfs_delete(struct ntfs_inode *ni, struct ntfs_inode *dir_ni, 758 + __le16 *name, u8 name_len, bool need_lock) 759 + { 760 + struct ntfs_attr_search_ctx *actx = NULL; 761 + struct file_name_attr *fn = NULL; 762 + bool looking_for_dos_name = false, looking_for_win32_name = false; 763 + bool case_sensitive_match = true; 764 + int err = 0; 765 + struct mft_record *ni_mrec; 766 + struct super_block *sb; 767 + bool link_count_zero = false; 768 + 769 + ntfs_debug("Entering.\n"); 770 + 771 + if (need_lock == true) { 772 + mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 773 + mutex_lock_nested(&dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT); 774 + } 775 + 776 + sb = dir_ni->vol->sb; 777 + 778 + if (ni->nr_extents == -1) 779 + ni = ni->ext.base_ntfs_ino; 780 + if (dir_ni->nr_extents == -1) 781 + dir_ni = dir_ni->ext.base_ntfs_ino; 782 + /* 783 + * Search for FILE_NAME attribute with such name. If it's in POSIX or 784 + * WIN32_AND_DOS namespace, then simply remove it from index and inode. 785 + * If filename in DOS or in WIN32 namespace, then remove DOS name first, 786 + * only then remove WIN32 name. 787 + */ 788 + actx = ntfs_attr_get_search_ctx(ni, NULL); 789 + if (!actx) { 790 + ntfs_error(sb, "%s, Failed to get search context", __func__); 791 + if (need_lock) { 792 + mutex_unlock(&dir_ni->mrec_lock); 793 + mutex_unlock(&ni->mrec_lock); 794 + } 795 + return -ENOMEM; 796 + } 797 + search: 798 + while ((err = ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, CASE_SENSITIVE, 799 + 0, NULL, 0, actx)) == 0) { 800 + #ifdef DEBUG 801 + unsigned char *s; 802 + #endif 803 + bool case_sensitive = IGNORE_CASE; 804 + 805 + fn = (struct file_name_attr *)((u8 *)actx->attr + 806 + le16_to_cpu(actx->attr->data.resident.value_offset)); 807 + #ifdef DEBUG 808 + s = ntfs_attr_name_get(ni->vol, fn->file_name, fn->file_name_length); 809 + ntfs_debug("name: '%s' type: %d dos: %d win32: %d case: %d\n", 810 + s, fn->file_name_type, 811 + looking_for_dos_name, looking_for_win32_name, 812 + case_sensitive_match); 813 + ntfs_attr_name_free(&s); 814 + #endif 815 + if (looking_for_dos_name) { 816 + if (fn->file_name_type == FILE_NAME_DOS) 817 + break; 818 + continue; 819 + } 820 + if (looking_for_win32_name) { 821 + if (fn->file_name_type == FILE_NAME_WIN32) 822 + break; 823 + continue; 824 + } 825 + 826 + /* Ignore hard links from other directories */ 827 + if (dir_ni->mft_no != MREF_LE(fn->parent_directory)) { 828 + ntfs_debug("MFT record numbers don't match (%lu != %lu)\n", 829 + dir_ni->mft_no, 830 + MREF_LE(fn->parent_directory)); 831 + continue; 832 + } 833 + 834 + if (fn->file_name_type == FILE_NAME_POSIX || case_sensitive_match) 835 + case_sensitive = CASE_SENSITIVE; 836 + 837 + if (ntfs_names_are_equal(fn->file_name, fn->file_name_length, 838 + name, name_len, case_sensitive, 839 + ni->vol->upcase, ni->vol->upcase_len)) { 840 + if (fn->file_name_type == FILE_NAME_WIN32) { 841 + looking_for_dos_name = true; 842 + ntfs_attr_reinit_search_ctx(actx); 843 + continue; 844 + } 845 + if (fn->file_name_type == FILE_NAME_DOS) 846 + looking_for_dos_name = true; 847 + break; 848 + } 849 + } 850 + if (err) { 851 + /* 852 + * If case sensitive search failed, then try once again 853 + * ignoring case. 854 + */ 855 + if (err == -ENOENT && case_sensitive_match) { 856 + case_sensitive_match = false; 857 + ntfs_attr_reinit_search_ctx(actx); 858 + goto search; 859 + } 860 + goto err_out; 861 + } 862 + 863 + err = ntfs_check_unlinkable_dir(actx, fn); 864 + if (err) 865 + goto err_out; 866 + 867 + err = ntfs_index_remove(dir_ni, fn, le32_to_cpu(actx->attr->data.resident.value_length)); 868 + if (err) 869 + goto err_out; 870 + 871 + err = ntfs_attr_record_rm(actx); 872 + if (err) 873 + goto err_out; 874 + 875 + ni_mrec = actx->base_mrec ? actx->base_mrec : actx->mrec; 876 + ni_mrec->link_count = cpu_to_le16(le16_to_cpu(ni_mrec->link_count) - 1); 877 + drop_nlink(VFS_I(ni)); 878 + 879 + mark_mft_record_dirty(ni); 880 + if (looking_for_dos_name) { 881 + looking_for_dos_name = false; 882 + looking_for_win32_name = true; 883 + ntfs_attr_reinit_search_ctx(actx); 884 + goto search; 885 + } 886 + 887 + /* 888 + * If hard link count is not equal to zero then we are done. In other 889 + * case there are no reference to this inode left, so we should free all 890 + * non-resident attributes and mark all MFT record as not in use. 891 + */ 892 + if (ni_mrec->link_count == 0) { 893 + NInoSetBeingDeleted(ni); 894 + ntfs_delete_reparse_index(ni); 895 + ntfs_delete_object_id_index(ni); 896 + link_count_zero = true; 897 + } 898 + 899 + ntfs_attr_put_search_ctx(actx); 900 + if (need_lock == true) { 901 + mutex_unlock(&dir_ni->mrec_lock); 902 + mutex_unlock(&ni->mrec_lock); 903 + } 904 + 905 + /* 906 + * If hard link count is not equal to zero then we are done. In other 907 + * case there are no reference to this inode left, so we should free all 908 + * non-resident attributes and mark all MFT record as not in use. 909 + */ 910 + if (link_count_zero == true) { 911 + struct inode *attr_vi; 912 + 913 + while ((attr_vi = ilookup5(sb, ni->mft_no, ntfs_test_inode_attr, 914 + (void *)ni->mft_no)) != NULL) { 915 + clear_nlink(attr_vi); 916 + iput(attr_vi); 917 + } 918 + } 919 + ntfs_debug("Done.\n"); 920 + return 0; 921 + err_out: 922 + ntfs_attr_put_search_ctx(actx); 923 + if (need_lock) { 924 + mutex_unlock(&dir_ni->mrec_lock); 925 + mutex_unlock(&ni->mrec_lock); 926 + } 927 + return err; 928 + } 929 + 930 + static int ntfs_unlink(struct inode *dir, struct dentry *dentry) 931 + { 932 + struct inode *vi = dentry->d_inode; 933 + struct super_block *sb = dir->i_sb; 934 + struct ntfs_volume *vol = NTFS_SB(sb); 935 + int err = 0; 936 + struct ntfs_inode *ni = NTFS_I(vi); 937 + __le16 *uname = NULL; 938 + int uname_len; 939 + 940 + if (NVolShutdown(vol)) 941 + return -EIO; 942 + 943 + uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len, 944 + &uname, NTFS_MAX_NAME_LEN); 945 + if (uname_len < 0) { 946 + if (uname_len != -ENAMETOOLONG) 947 + ntfs_error(sb, "Failed to convert name to Unicode."); 948 + return -ENOMEM; 949 + } 950 + 951 + err = ntfs_check_bad_windows_name(vol, uname, uname_len); 952 + if (err) { 953 + kmem_cache_free(ntfs_name_cache, uname); 954 + return err; 955 + } 956 + 957 + if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 958 + ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 959 + 960 + err = ntfs_delete(ni, NTFS_I(dir), uname, uname_len, true); 961 + if (err) 962 + goto out; 963 + 964 + inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); 965 + mark_inode_dirty(dir); 966 + inode_set_ctime_to_ts(vi, inode_get_ctime(dir)); 967 + if (vi->i_nlink) 968 + mark_inode_dirty(vi); 969 + out: 970 + kmem_cache_free(ntfs_name_cache, uname); 971 + return err; 972 + } 973 + 974 + static struct dentry *ntfs_mkdir(struct mnt_idmap *idmap, struct inode *dir, 975 + struct dentry *dentry, umode_t mode) 976 + { 977 + struct super_block *sb = dir->i_sb; 978 + struct ntfs_volume *vol = NTFS_SB(sb); 979 + int err = 0; 980 + struct ntfs_inode *ni; 981 + __le16 *uname; 982 + int uname_len; 983 + 984 + if (NVolShutdown(vol)) 985 + return ERR_PTR(-EIO); 986 + 987 + uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len, 988 + &uname, NTFS_MAX_NAME_LEN); 989 + if (uname_len < 0) { 990 + if (uname_len != -ENAMETOOLONG) 991 + ntfs_error(sb, "Failed to convert name to unicode."); 992 + return ERR_PTR(-ENOMEM); 993 + } 994 + 995 + err = ntfs_check_bad_windows_name(vol, uname, uname_len); 996 + if (err) { 997 + kmem_cache_free(ntfs_name_cache, uname); 998 + return ERR_PTR(err); 999 + } 1000 + 1001 + if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 1002 + ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 1003 + 1004 + ni = __ntfs_create(idmap, dir, uname, uname_len, S_IFDIR | mode, 0, NULL, 0); 1005 + kmem_cache_free(ntfs_name_cache, uname); 1006 + if (IS_ERR(ni)) { 1007 + err = PTR_ERR(ni); 1008 + return ERR_PTR(err); 1009 + } 1010 + 1011 + d_instantiate_new(dentry, VFS_I(ni)); 1012 + return ERR_PTR(err); 1013 + } 1014 + 1015 + static int ntfs_rmdir(struct inode *dir, struct dentry *dentry) 1016 + { 1017 + struct inode *vi = dentry->d_inode; 1018 + struct super_block *sb = dir->i_sb; 1019 + struct ntfs_volume *vol = NTFS_SB(sb); 1020 + int err = 0; 1021 + struct ntfs_inode *ni; 1022 + __le16 *uname = NULL; 1023 + int uname_len; 1024 + 1025 + if (NVolShutdown(vol)) 1026 + return -EIO; 1027 + 1028 + ni = NTFS_I(vi); 1029 + uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len, 1030 + &uname, NTFS_MAX_NAME_LEN); 1031 + if (uname_len < 0) { 1032 + if (uname_len != -ENAMETOOLONG) 1033 + ntfs_error(sb, "Failed to convert name to unicode."); 1034 + return -ENOMEM; 1035 + } 1036 + 1037 + err = ntfs_check_bad_windows_name(vol, uname, uname_len); 1038 + if (err) { 1039 + kmem_cache_free(ntfs_name_cache, uname); 1040 + return err; 1041 + } 1042 + 1043 + if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 1044 + ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 1045 + 1046 + err = ntfs_delete(ni, NTFS_I(dir), uname, uname_len, true); 1047 + if (err) 1048 + goto out; 1049 + 1050 + inode_set_mtime_to_ts(vi, inode_set_atime_to_ts(vi, current_time(vi))); 1051 + out: 1052 + kmem_cache_free(ntfs_name_cache, uname); 1053 + return err; 1054 + } 1055 + 1056 + /* 1057 + * __ntfs_link - create hard link for file or directory 1058 + * @ni: ntfs inode for object to create hard link 1059 + * @dir_ni: ntfs inode for directory in which new link should be placed 1060 + * @name: unicode name of the new link 1061 + * @name_len: length of the name in unicode characters 1062 + * 1063 + * Create a new hard link. This involves adding an entry to the directory 1064 + * index and adding a new FILE_NAME attribute to the target inode. 1065 + * 1066 + * Return 0 on success and -errno on error. 1067 + */ 1068 + static int __ntfs_link(struct ntfs_inode *ni, struct ntfs_inode *dir_ni, 1069 + __le16 *name, u8 name_len) 1070 + { 1071 + struct super_block *sb; 1072 + struct inode *vi = VFS_I(ni); 1073 + struct file_name_attr *fn = NULL; 1074 + int fn_len, err = 0; 1075 + struct mft_record *dir_mrec = NULL, *ni_mrec = NULL; 1076 + 1077 + ntfs_debug("Entering.\n"); 1078 + 1079 + sb = dir_ni->vol->sb; 1080 + if (NInoBeingDeleted(dir_ni) || NInoBeingDeleted(ni)) 1081 + return -ENOENT; 1082 + 1083 + ni_mrec = map_mft_record(ni); 1084 + if (IS_ERR(ni_mrec)) { 1085 + err = -EIO; 1086 + goto err_out; 1087 + } 1088 + 1089 + if (le16_to_cpu(ni_mrec->link_count) == 0) { 1090 + err = -ENOENT; 1091 + goto err_out; 1092 + } 1093 + 1094 + /* Create FILE_NAME attribute. */ 1095 + fn_len = sizeof(struct file_name_attr) + name_len * sizeof(__le16); 1096 + if (name_len > NTFS_MAX_NAME_LEN) { 1097 + err = -EIO; 1098 + goto err_out; 1099 + } 1100 + fn = kzalloc(fn_len, GFP_NOFS); 1101 + if (!fn) { 1102 + err = -ENOMEM; 1103 + goto err_out; 1104 + } 1105 + 1106 + dir_mrec = map_mft_record(dir_ni); 1107 + if (IS_ERR(dir_mrec)) { 1108 + err = -EIO; 1109 + goto err_out; 1110 + } 1111 + 1112 + fn->parent_directory = MK_LE_MREF(dir_ni->mft_no, 1113 + le16_to_cpu(dir_mrec->sequence_number)); 1114 + unmap_mft_record(dir_ni); 1115 + fn->file_name_length = name_len; 1116 + fn->file_name_type = FILE_NAME_POSIX; 1117 + fn->file_attributes = ni->flags; 1118 + if (ni_mrec->flags & MFT_RECORD_IS_DIRECTORY) { 1119 + fn->file_attributes |= FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT; 1120 + fn->allocated_size = fn->data_size = 0; 1121 + } else { 1122 + if (NInoSparse(ni) || NInoCompressed(ni)) 1123 + fn->allocated_size = 1124 + cpu_to_le64(ni->itype.compressed.size); 1125 + else 1126 + fn->allocated_size = cpu_to_le64(ni->allocated_size); 1127 + fn->data_size = cpu_to_le64(ni->data_size); 1128 + } 1129 + if (NVolHideDotFiles(dir_ni->vol) && name_len > 0 && name[0] == cpu_to_le16('.')) 1130 + fn->file_attributes |= FILE_ATTR_HIDDEN; 1131 + 1132 + fn->creation_time = utc2ntfs(ni->i_crtime); 1133 + fn->last_data_change_time = utc2ntfs(inode_get_mtime(vi)); 1134 + fn->last_mft_change_time = utc2ntfs(inode_get_ctime(vi)); 1135 + fn->last_access_time = utc2ntfs(inode_get_atime(vi)); 1136 + memcpy(fn->file_name, name, name_len * sizeof(__le16)); 1137 + 1138 + /* Add FILE_NAME attribute to index. */ 1139 + err = ntfs_index_add_filename(dir_ni, fn, MK_MREF(ni->mft_no, 1140 + le16_to_cpu(ni_mrec->sequence_number))); 1141 + if (err) { 1142 + ntfs_error(sb, "Failed to add filename to the index"); 1143 + goto err_out; 1144 + } 1145 + /* Add FILE_NAME attribute to inode. */ 1146 + err = ntfs_attr_add(ni, AT_FILE_NAME, AT_UNNAMED, 0, (u8 *)fn, fn_len); 1147 + if (err) { 1148 + ntfs_error(sb, "Failed to add FILE_NAME attribute.\n"); 1149 + /* Try to remove just added attribute from index. */ 1150 + if (ntfs_index_remove(dir_ni, fn, fn_len)) 1151 + goto rollback_failed; 1152 + goto err_out; 1153 + } 1154 + /* Increment hard links count. */ 1155 + ni_mrec->link_count = cpu_to_le16(le16_to_cpu(ni_mrec->link_count) + 1); 1156 + inc_nlink(VFS_I(ni)); 1157 + 1158 + /* Done! */ 1159 + mark_mft_record_dirty(ni); 1160 + kfree(fn); 1161 + unmap_mft_record(ni); 1162 + 1163 + ntfs_debug("Done.\n"); 1164 + 1165 + return 0; 1166 + rollback_failed: 1167 + ntfs_error(sb, "Rollback failed. Leaving inconsistent metadata.\n"); 1168 + err_out: 1169 + kfree(fn); 1170 + if (!IS_ERR_OR_NULL(ni_mrec)) 1171 + unmap_mft_record(ni); 1172 + return err; 1173 + } 1174 + 1175 + static int ntfs_rename(struct mnt_idmap *idmap, struct inode *old_dir, 1176 + struct dentry *old_dentry, struct inode *new_dir, 1177 + struct dentry *new_dentry, unsigned int flags) 1178 + { 1179 + struct inode *old_inode, *new_inode = NULL; 1180 + int err = 0; 1181 + int is_dir; 1182 + struct super_block *sb = old_dir->i_sb; 1183 + __le16 *uname_new = NULL; 1184 + __le16 *uname_old = NULL; 1185 + int new_name_len; 1186 + int old_name_len; 1187 + struct ntfs_volume *vol = NTFS_SB(sb); 1188 + struct ntfs_inode *old_ni, *new_ni = NULL; 1189 + struct ntfs_inode *old_dir_ni = NTFS_I(old_dir), *new_dir_ni = NTFS_I(new_dir); 1190 + 1191 + if (NVolShutdown(old_dir_ni->vol)) 1192 + return -EIO; 1193 + 1194 + if (flags & (RENAME_EXCHANGE | RENAME_WHITEOUT)) 1195 + return -EINVAL; 1196 + 1197 + new_name_len = ntfs_nlstoucs(NTFS_I(new_dir)->vol, new_dentry->d_name.name, 1198 + new_dentry->d_name.len, &uname_new, 1199 + NTFS_MAX_NAME_LEN); 1200 + if (new_name_len < 0) { 1201 + if (new_name_len != -ENAMETOOLONG) 1202 + ntfs_error(sb, "Failed to convert name to unicode."); 1203 + return -ENOMEM; 1204 + } 1205 + 1206 + err = ntfs_check_bad_windows_name(vol, uname_new, new_name_len); 1207 + if (err) { 1208 + kmem_cache_free(ntfs_name_cache, uname_new); 1209 + return err; 1210 + } 1211 + 1212 + old_name_len = ntfs_nlstoucs(NTFS_I(old_dir)->vol, old_dentry->d_name.name, 1213 + old_dentry->d_name.len, &uname_old, 1214 + NTFS_MAX_NAME_LEN); 1215 + if (old_name_len < 0) { 1216 + kmem_cache_free(ntfs_name_cache, uname_new); 1217 + if (old_name_len != -ENAMETOOLONG) 1218 + ntfs_error(sb, "Failed to convert name to unicode."); 1219 + return -ENOMEM; 1220 + } 1221 + 1222 + old_inode = old_dentry->d_inode; 1223 + new_inode = new_dentry->d_inode; 1224 + old_ni = NTFS_I(old_inode); 1225 + 1226 + if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 1227 + ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 1228 + 1229 + mutex_lock_nested(&old_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 1230 + mutex_lock_nested(&old_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT); 1231 + 1232 + if (NInoBeingDeleted(old_ni) || NInoBeingDeleted(old_dir_ni)) { 1233 + err = -ENOENT; 1234 + goto unlock_old; 1235 + } 1236 + 1237 + is_dir = S_ISDIR(old_inode->i_mode); 1238 + 1239 + if (new_inode) { 1240 + new_ni = NTFS_I(new_inode); 1241 + mutex_lock_nested(&new_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL_2); 1242 + if (old_dir != new_dir) { 1243 + mutex_lock_nested(&new_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT_2); 1244 + if (NInoBeingDeleted(new_dir_ni)) { 1245 + err = -ENOENT; 1246 + goto err_out; 1247 + } 1248 + } 1249 + 1250 + if (NInoBeingDeleted(new_ni)) { 1251 + err = -ENOENT; 1252 + goto err_out; 1253 + } 1254 + 1255 + if (is_dir) { 1256 + struct mft_record *ni_mrec; 1257 + 1258 + ni_mrec = map_mft_record(NTFS_I(new_inode)); 1259 + if (IS_ERR(ni_mrec)) { 1260 + err = -EIO; 1261 + goto err_out; 1262 + } 1263 + err = ntfs_check_empty_dir(NTFS_I(new_inode), ni_mrec); 1264 + unmap_mft_record(NTFS_I(new_inode)); 1265 + if (err) 1266 + goto err_out; 1267 + } 1268 + 1269 + err = ntfs_delete(new_ni, new_dir_ni, uname_new, new_name_len, false); 1270 + if (err) 1271 + goto err_out; 1272 + } else { 1273 + if (old_dir != new_dir) { 1274 + mutex_lock_nested(&new_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT_2); 1275 + if (NInoBeingDeleted(new_dir_ni)) { 1276 + err = -ENOENT; 1277 + goto err_out; 1278 + } 1279 + } 1280 + } 1281 + 1282 + err = __ntfs_link(old_ni, new_dir_ni, uname_new, new_name_len); 1283 + if (err) 1284 + goto err_out; 1285 + 1286 + err = ntfs_delete(old_ni, old_dir_ni, uname_old, old_name_len, false); 1287 + if (err) { 1288 + int err2; 1289 + 1290 + ntfs_error(sb, "Failed to delete old ntfs inode(%ld) in old dir, err : %d\n", 1291 + old_ni->mft_no, err); 1292 + err2 = ntfs_delete(old_ni, new_dir_ni, uname_new, new_name_len, false); 1293 + if (err2) 1294 + ntfs_error(sb, "Failed to delete old ntfs inode in new dir, err : %d\n", 1295 + err2); 1296 + goto err_out; 1297 + } 1298 + 1299 + simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry); 1300 + mark_inode_dirty(old_inode); 1301 + mark_inode_dirty(old_dir); 1302 + if (old_dir != new_dir) 1303 + mark_inode_dirty(new_dir); 1304 + if (new_inode) 1305 + mark_inode_dirty(old_inode); 1306 + 1307 + inode_inc_iversion(new_dir); 1308 + 1309 + err_out: 1310 + if (old_dir != new_dir) 1311 + mutex_unlock(&new_dir_ni->mrec_lock); 1312 + if (new_inode) 1313 + mutex_unlock(&new_ni->mrec_lock); 1314 + 1315 + unlock_old: 1316 + mutex_unlock(&old_dir_ni->mrec_lock); 1317 + mutex_unlock(&old_ni->mrec_lock); 1318 + if (uname_new) 1319 + kmem_cache_free(ntfs_name_cache, uname_new); 1320 + if (uname_old) 1321 + kmem_cache_free(ntfs_name_cache, uname_old); 1322 + 1323 + return err; 1324 + } 1325 + 1326 + static int ntfs_symlink(struct mnt_idmap *idmap, struct inode *dir, 1327 + struct dentry *dentry, const char *symname) 1328 + { 1329 + struct super_block *sb = dir->i_sb; 1330 + struct ntfs_volume *vol = NTFS_SB(sb); 1331 + struct inode *vi; 1332 + int err = 0; 1333 + struct ntfs_inode *ni; 1334 + __le16 *usrc; 1335 + __le16 *utarget; 1336 + int usrc_len; 1337 + int utarget_len; 1338 + int symlen = strlen(symname); 1339 + 1340 + if (NVolShutdown(vol)) 1341 + return -EIO; 1342 + 1343 + usrc_len = ntfs_nlstoucs(vol, dentry->d_name.name, 1344 + dentry->d_name.len, &usrc, NTFS_MAX_NAME_LEN); 1345 + if (usrc_len < 0) { 1346 + if (usrc_len != -ENAMETOOLONG) 1347 + ntfs_error(sb, "Failed to convert name to Unicode."); 1348 + err = -ENOMEM; 1349 + goto out; 1350 + } 1351 + 1352 + err = ntfs_check_bad_windows_name(vol, usrc, usrc_len); 1353 + if (err) { 1354 + kmem_cache_free(ntfs_name_cache, usrc); 1355 + goto out; 1356 + } 1357 + 1358 + utarget_len = ntfs_nlstoucs(vol, symname, symlen, &utarget, 1359 + PATH_MAX); 1360 + if (utarget_len < 0) { 1361 + if (utarget_len != -ENAMETOOLONG) 1362 + ntfs_error(sb, "Failed to convert target name to Unicode."); 1363 + err = -ENOMEM; 1364 + kmem_cache_free(ntfs_name_cache, usrc); 1365 + goto out; 1366 + } 1367 + 1368 + if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 1369 + ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 1370 + 1371 + ni = __ntfs_create(idmap, dir, usrc, usrc_len, S_IFLNK | 0777, 0, 1372 + utarget, utarget_len); 1373 + kmem_cache_free(ntfs_name_cache, usrc); 1374 + kvfree(utarget); 1375 + if (IS_ERR(ni)) { 1376 + err = PTR_ERR(ni); 1377 + goto out; 1378 + } 1379 + 1380 + vi = VFS_I(ni); 1381 + vi->i_size = symlen; 1382 + d_instantiate_new(dentry, vi); 1383 + out: 1384 + return err; 1385 + } 1386 + 1387 + static int ntfs_mknod(struct mnt_idmap *idmap, struct inode *dir, 1388 + struct dentry *dentry, umode_t mode, dev_t rdev) 1389 + { 1390 + struct super_block *sb = dir->i_sb; 1391 + struct ntfs_volume *vol = NTFS_SB(sb); 1392 + int err = 0; 1393 + struct ntfs_inode *ni; 1394 + __le16 *uname = NULL; 1395 + int uname_len; 1396 + 1397 + if (NVolShutdown(vol)) 1398 + return -EIO; 1399 + 1400 + uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, 1401 + dentry->d_name.len, &uname, NTFS_MAX_NAME_LEN); 1402 + if (uname_len < 0) { 1403 + if (uname_len != -ENAMETOOLONG) 1404 + ntfs_error(sb, "Failed to convert name to Unicode."); 1405 + return -ENOMEM; 1406 + } 1407 + 1408 + err = ntfs_check_bad_windows_name(vol, uname, uname_len); 1409 + if (err) { 1410 + kmem_cache_free(ntfs_name_cache, uname); 1411 + return err; 1412 + } 1413 + 1414 + if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 1415 + ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 1416 + 1417 + switch (mode & S_IFMT) { 1418 + case S_IFCHR: 1419 + case S_IFBLK: 1420 + ni = __ntfs_create(idmap, dir, uname, uname_len, 1421 + mode, rdev, NULL, 0); 1422 + break; 1423 + default: 1424 + ni = __ntfs_create(idmap, dir, uname, uname_len, 1425 + mode, 0, NULL, 0); 1426 + } 1427 + 1428 + kmem_cache_free(ntfs_name_cache, uname); 1429 + if (IS_ERR(ni)) { 1430 + err = PTR_ERR(ni); 1431 + goto out; 1432 + } 1433 + 1434 + d_instantiate_new(dentry, VFS_I(ni)); 1435 + out: 1436 + return err; 1437 + } 1438 + 1439 + static int ntfs_link(struct dentry *old_dentry, struct inode *dir, 1440 + struct dentry *dentry) 1441 + { 1442 + struct inode *vi = old_dentry->d_inode; 1443 + struct super_block *sb = vi->i_sb; 1444 + struct ntfs_volume *vol = NTFS_SB(sb); 1445 + __le16 *uname = NULL; 1446 + int uname_len; 1447 + int err; 1448 + struct ntfs_inode *ni = NTFS_I(vi), *dir_ni = NTFS_I(dir); 1449 + 1450 + if (NVolShutdown(vol)) 1451 + return -EIO; 1452 + 1453 + uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, 1454 + dentry->d_name.len, &uname, NTFS_MAX_NAME_LEN); 1455 + if (uname_len < 0) { 1456 + if (uname_len != -ENAMETOOLONG) 1457 + ntfs_error(sb, "Failed to convert name to unicode."); 1458 + err = -ENOMEM; 1459 + goto out; 1460 + } 1461 + 1462 + if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 1463 + ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 1464 + 1465 + ihold(vi); 1466 + mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 1467 + mutex_lock_nested(&dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT); 1468 + err = __ntfs_link(NTFS_I(vi), NTFS_I(dir), uname, uname_len); 1469 + if (err) { 1470 + mutex_unlock(&dir_ni->mrec_lock); 1471 + mutex_unlock(&ni->mrec_lock); 1472 + iput(vi); 1473 + pr_err("failed to create link, err = %d\n", err); 1474 + goto out; 1475 + } 1476 + 1477 + inode_inc_iversion(dir); 1478 + simple_inode_init_ts(dir); 1479 + 1480 + inode_inc_iversion(vi); 1481 + simple_inode_init_ts(vi); 1482 + 1483 + /* timestamp is already written, so mark_inode_dirty() is unneeded. */ 1484 + d_instantiate(dentry, vi); 1485 + mutex_unlock(&dir_ni->mrec_lock); 1486 + mutex_unlock(&ni->mrec_lock); 1487 + 1488 + out: 1489 + kfree(uname); 1490 + return err; 339 1491 } 340 1492 341 1493 /* 342 1494 * Inode operations for directories. 343 1495 */ 344 1496 const struct inode_operations ntfs_dir_inode_ops = { 345 - .lookup = ntfs_lookup, /* VFS: Lookup directory. */ 1497 + .lookup = ntfs_lookup, /* VFS: Lookup directory. */ 1498 + .create = ntfs_create, 1499 + .unlink = ntfs_unlink, 1500 + .mkdir = ntfs_mkdir, 1501 + .rmdir = ntfs_rmdir, 1502 + .rename = ntfs_rename, 1503 + .get_acl = ntfs_get_acl, 1504 + .set_acl = ntfs_set_acl, 1505 + .listxattr = ntfs_listxattr, 1506 + .setattr = ntfs_setattr, 1507 + .getattr = ntfs_getattr, 1508 + .symlink = ntfs_symlink, 1509 + .mknod = ntfs_mknod, 1510 + .link = ntfs_link, 346 1511 }; 347 1512 348 - /** 1513 + /* 349 1514 * ntfs_get_parent - find the dentry of the parent of a given directory dentry 350 1515 * @child_dent: dentry of the directory whose parent directory to find 351 1516 * ··· 1597 274 * dentry @child_dent. This function is called from 1598 275 * fs/exportfs/expfs.c::find_exported_dentry() which in turn is called from the 1599 276 * default ->decode_fh() which is export_decode_fh() in the same file. 1600 - * 1601 - * The code is based on the ext3 ->get_parent() implementation found in 1602 - * fs/ext3/namei.c::ext3_get_parent(). 1603 277 * 1604 278 * Note: ntfs_get_parent() is called with @d_inode(child_dent)->i_mutex down. 1605 279 * ··· 1606 286 static struct dentry *ntfs_get_parent(struct dentry *child_dent) 1607 287 { 1608 288 struct inode *vi = d_inode(child_dent); 1609 - ntfs_inode *ni = NTFS_I(vi); 1610 - MFT_RECORD *mrec; 1611 - ntfs_attr_search_ctx *ctx; 1612 - ATTR_RECORD *attr; 1613 - FILE_NAME_ATTR *fn; 289 + struct ntfs_inode *ni = NTFS_I(vi); 290 + struct mft_record *mrec; 291 + struct ntfs_attr_search_ctx *ctx; 292 + struct attr_record *attr; 293 + struct file_name_attr *fn; 1614 294 unsigned long parent_ino; 1615 295 int err; 1616 296 ··· 1632 312 ntfs_attr_put_search_ctx(ctx); 1633 313 unmap_mft_record(ni); 1634 314 if (err == -ENOENT) 1635 - ntfs_error(vi->i_sb, "Inode 0x%lx does not have a " 1636 - "file name attribute. Run chkdsk.", 1637 - vi->i_ino); 315 + ntfs_error(vi->i_sb, 316 + "Inode 0x%lx does not have a file name attribute. Run chkdsk.", 317 + vi->i_ino); 1638 318 return ERR_PTR(err); 1639 319 } 1640 320 attr = ctx->attr; 1641 321 if (unlikely(attr->non_resident)) 1642 322 goto try_next; 1643 - fn = (FILE_NAME_ATTR *)((u8 *)attr + 323 + fn = (struct file_name_attr *)((u8 *)attr + 1644 324 le16_to_cpu(attr->data.resident.value_offset)); 1645 325 if (unlikely((u8 *)fn + le32_to_cpu(attr->data.resident.value_length) > 1646 - (u8*)attr + le32_to_cpu(attr->length))) 326 + (u8 *)attr + le32_to_cpu(attr->length))) 1647 327 goto try_next; 1648 328 /* Get the inode number of the parent directory. */ 1649 329 parent_ino = MREF_LE(fn->parent_directory); ··· 1661 341 1662 342 inode = ntfs_iget(sb, ino); 1663 343 if (!IS_ERR(inode)) { 1664 - if (is_bad_inode(inode) || inode->i_generation != generation) { 344 + if (inode->i_generation != generation) { 1665 345 iput(inode); 1666 346 inode = ERR_PTR(-ESTALE); 1667 347 } ··· 1686 366 1687 367 /* 1688 368 * Export operations allowing NFS exporting of mounted NTFS partitions. 1689 - * 1690 - * We use the default ->encode_fh() for now. Note that they 1691 - * use 32 bits to store the inode number which is an unsigned long so on 64-bit 1692 - * architectures is usually 64 bits so it would all fail horribly on huge 1693 - * volumes. I guess we need to define our own encode and decode fh functions 1694 - * that store 64-bit inode numbers at some point but for now we will ignore the 1695 - * problem... 1696 - * 1697 - * We also use the default ->get_name() helper (used by ->decode_fh() via 1698 - * fs/exportfs/expfs.c::find_exported_dentry()) as that is completely fs 1699 - * independent. 1700 - * 1701 - * The default ->get_parent() just returns -EACCES so we have to provide our 1702 - * own and the default ->get_dentry() is incompatible with NTFS due to not 1703 - * allowing the inode number 0 which is used in NTFS for the system file $MFT 1704 - * and due to using iget() whereas NTFS needs ntfs_iget(). 1705 369 */ 1706 370 const struct export_operations ntfs_export_ops = { 1707 - .encode_fh = generic_encode_ino32_fh, 1708 - .get_parent = ntfs_get_parent, /* Find the parent of a given 1709 - directory. */ 371 + .encode_fh = generic_encode_ino32_fh, 372 + .get_parent = ntfs_get_parent, /* Find the parent of a given directory. */ 1710 373 .fh_to_dentry = ntfs_fh_to_dentry, 1711 374 .fh_to_parent = ntfs_fh_to_parent, 1712 375 };