1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * NTFS kernel directory inode operations. 4 * 5 * Copyright (c) 2001-2006 Anton Altaparmakov 6 * Copyright (c) 2025 LG Electronics Co., Ltd. 7 */ 8 9 #include <linux/exportfs.h> 10 #include <linux/iversion.h> 11 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" 18 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 /* 102 * ntfs_lookup - find the inode represented by a dentry in a directory inode 103 * @dir_ino: directory inode in which to look for the inode 104 * @dent: dentry representing the inode to look for 105 * @flags: lookup flags 106 * 107 * In short, ntfs_lookup() looks for the inode represented by the dentry @dent 108 * in the directory inode @dir_ino and if found attaches the inode to the 109 * dentry @dent. 110 * 111 * In more detail, the dentry @dent specifies which inode to look for by 112 * supplying the name of the inode in @dent->d_name.name. ntfs_lookup() 113 * converts the name to Unicode and walks the contents of the directory inode 114 * @dir_ino looking for the converted Unicode name. If the name is found in the 115 * directory, the corresponding inode is loaded by calling ntfs_iget() on its 116 * inode number and the inode is associated with the dentry @dent via a call to 117 * d_splice_alias(). 118 * 119 * If the name is not found in the directory, a NULL inode is inserted into the 120 * dentry @dent via a call to d_add(). The dentry is then termed a negative 121 * dentry. 122 * 123 * Only if an actual error occurs, do we return an error via ERR_PTR(). 124 * 125 * In order to handle the case insensitivity issues of NTFS with regards to the 126 * dcache and the dcache requiring only one dentry per directory, we deal with 127 * dentry aliases that only differ in case in ->ntfs_lookup() while maintaining 128 * a case sensitive dcache. This means that we get the full benefit of dcache 129 * speed when the file/directory is looked up with the same case as returned by 130 * ->ntfs_readdir() but that a lookup for any other case (or for the short file 131 * name) will not find anything in dcache and will enter ->ntfs_lookup() 132 * instead, where we search the directory for a fully matching file name 133 * (including case) and if that is not found, we search for a file name that 134 * matches with different case and if that has non-POSIX semantics we return 135 * that. We actually do only one search (case sensitive) and keep tabs on 136 * whether we have found a case insensitive match in the process. 137 * 138 * To simplify matters for us, we do not treat the short vs long filenames as 139 * two hard links but instead if the lookup matches a short filename, we 140 * return the dentry for the corresponding long filename instead. 141 * 142 * There are three cases we need to distinguish here: 143 * 144 * 1) @dent perfectly matches (i.e. including case) a directory entry with a 145 * file name in the WIN32 or POSIX namespaces. In this case 146 * ntfs_lookup_inode_by_name() will return with name set to NULL and we 147 * just d_splice_alias() @dent. 148 * 2) @dent matches (not including case) a directory entry with a file name in 149 * the WIN32 namespace. In this case ntfs_lookup_inode_by_name() will return 150 * with name set to point to a kmalloc()ed ntfs_name structure containing 151 * the properly cased little endian Unicode name. We convert the name to the 152 * current NLS code page, search if a dentry with this name already exists 153 * and if so return that instead of @dent. At this point things are 154 * complicated by the possibility of 'disconnected' dentries due to NFS 155 * which we deal with appropriately (see the code comments). The VFS will 156 * then destroy the old @dent and use the one we returned. If a dentry is 157 * not found, we allocate a new one, d_splice_alias() it, and return it as 158 * above. 159 * 3) @dent matches either perfectly or not (i.e. we don't care about case) a 160 * directory entry with a file name in the DOS namespace. In this case 161 * ntfs_lookup_inode_by_name() will return with name set to point to a 162 * kmalloc()ed ntfs_name structure containing the mft reference (cpu endian) 163 * of the inode. We use the mft reference to read the inode and to find the 164 * file name in the WIN32 namespace corresponding to the matched short file 165 * name. We then convert the name to the current NLS code page, and proceed 166 * searching for a dentry with this name, etc, as in case 2), above. 167 * 168 * Locking: Caller must hold i_mutex on the directory. 169 */ 170 static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent, 171 unsigned int flags) 172 { 173 struct ntfs_volume *vol = NTFS_SB(dir_ino->i_sb); 174 struct inode *dent_inode; 175 __le16 *uname; 176 struct ntfs_name *name = NULL; 177 u64 mref; 178 unsigned long dent_ino; 179 int uname_len; 180 181 ntfs_debug("Looking up %pd in directory inode 0x%llx.", 182 dent, NTFS_I(dir_ino)->mft_no); 183 /* Convert the name of the dentry to Unicode. */ 184 uname_len = ntfs_nlstoucs(vol, dent->d_name.name, dent->d_name.len, 185 &uname, NTFS_MAX_NAME_LEN); 186 if (uname_len < 0) { 187 if (uname_len != -ENAMETOOLONG) 188 ntfs_debug("Failed to convert name to Unicode."); 189 return ERR_PTR(uname_len); 190 } 191 mutex_lock(&NTFS_I(dir_ino)->mrec_lock); 192 mref = ntfs_lookup_inode_by_name(NTFS_I(dir_ino), uname, uname_len, 193 &name); 194 mutex_unlock(&NTFS_I(dir_ino)->mrec_lock); 195 kmem_cache_free(ntfs_name_cache, uname); 196 if (!IS_ERR_MREF(mref)) { 197 dent_ino = MREF(mref); 198 ntfs_debug("Found inode 0x%lx. Calling ntfs_iget.", dent_ino); 199 dent_inode = ntfs_iget(vol->sb, dent_ino); 200 if (!IS_ERR(dent_inode)) { 201 /* Consistency check. */ 202 if (MSEQNO(mref) == NTFS_I(dent_inode)->seq_no || 203 dent_ino == FILE_MFT) { 204 /* Perfect WIN32/POSIX match. -- Case 1. */ 205 if (!name) { 206 ntfs_debug("Done. (Case 1.)"); 207 return d_splice_alias(dent_inode, dent); 208 } 209 /* 210 * We are too indented. Handle imperfect 211 * matches and short file names further below. 212 */ 213 goto handle_name; 214 } 215 ntfs_error(vol->sb, 216 "Found stale reference to inode 0x%lx (reference sequence number = 0x%x, inode sequence number = 0x%x), returning -EIO. Run chkdsk.", 217 dent_ino, MSEQNO(mref), 218 NTFS_I(dent_inode)->seq_no); 219 iput(dent_inode); 220 dent_inode = ERR_PTR(-EIO); 221 } else 222 ntfs_error(vol->sb, "ntfs_iget(0x%lx) failed with error code %li.", 223 dent_ino, PTR_ERR(dent_inode)); 224 kfree(name); 225 /* Return the error code. */ 226 return ERR_CAST(dent_inode); 227 } 228 kfree(name); 229 /* It is guaranteed that @name is no longer allocated at this point. */ 230 if (MREF_ERR(mref) == -ENOENT) { 231 ntfs_debug("Entry was not found, adding negative dentry."); 232 /* The dcache will handle negative entries. */ 233 ntfs_debug("Done."); 234 return d_splice_alias(NULL, dent); 235 } 236 ntfs_error(vol->sb, "ntfs_lookup_ino_by_name() failed with error code %i.", 237 -MREF_ERR(mref)); 238 return ERR_PTR(MREF_ERR(mref)); 239 handle_name: 240 { 241 struct mft_record *m; 242 struct ntfs_attr_search_ctx *ctx; 243 struct ntfs_inode *ni = NTFS_I(dent_inode); 244 int err; 245 struct qstr nls_name; 246 247 nls_name.name = NULL; 248 if (name->type != FILE_NAME_DOS) { /* Case 2. */ 249 ntfs_debug("Case 2."); 250 nls_name.len = (unsigned int)ntfs_ucstonls(vol, 251 (__le16 *)&name->name, name->len, 252 (unsigned char **)&nls_name.name, 0); 253 kfree(name); 254 } else /* if (name->type == FILE_NAME_DOS) */ { /* Case 3. */ 255 struct file_name_attr *fn; 256 257 ntfs_debug("Case 3."); 258 kfree(name); 259 260 /* Find the WIN32 name corresponding to the matched DOS name. */ 261 ni = NTFS_I(dent_inode); 262 m = map_mft_record(ni); 263 if (IS_ERR(m)) { 264 err = PTR_ERR(m); 265 m = NULL; 266 ctx = NULL; 267 goto err_out; 268 } 269 ctx = ntfs_attr_get_search_ctx(ni, m); 270 if (unlikely(!ctx)) { 271 err = -ENOMEM; 272 goto err_out; 273 } 274 do { 275 struct attr_record *a; 276 277 err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0, 278 NULL, 0, ctx); 279 if (unlikely(err)) { 280 ntfs_error(vol->sb, 281 "Inode corrupt: No WIN32 namespace counterpart to DOS file name. Run chkdsk."); 282 if (err == -ENOENT) 283 err = -EIO; 284 goto err_out; 285 } 286 /* Consistency checks. */ 287 a = ctx->attr; 288 if (a->non_resident || a->flags) 289 goto eio_err_out; 290 fn = (struct file_name_attr *)((u8 *)ctx->attr + le16_to_cpu( 291 ctx->attr->data.resident.value_offset)); 292 } while (fn->file_name_type != FILE_NAME_WIN32); 293 294 /* Convert the found WIN32 name to current NLS code page. */ 295 nls_name.len = (unsigned int)ntfs_ucstonls(vol, 296 (__le16 *)&fn->file_name, fn->file_name_length, 297 (unsigned char **)&nls_name.name, 0); 298 299 ntfs_attr_put_search_ctx(ctx); 300 unmap_mft_record(ni); 301 } 302 m = NULL; 303 ctx = NULL; 304 305 /* Check if a conversion error occurred. */ 306 if ((int)nls_name.len < 0) { 307 err = (int)nls_name.len; 308 goto err_out; 309 } 310 nls_name.hash = full_name_hash(dent, nls_name.name, nls_name.len); 311 312 dent = d_add_ci(dent, dent_inode, &nls_name); 313 kfree(nls_name.name); 314 return dent; 315 316 eio_err_out: 317 ntfs_error(vol->sb, "Illegal file name attribute. Run chkdsk."); 318 err = -EIO; 319 err_out: 320 if (ctx) 321 ntfs_attr_put_search_ctx(ctx); 322 if (m) 323 unmap_mft_record(ni); 324 iput(dent_inode); 325 ntfs_error(vol->sb, "Failed, returning error code %i.", err); 326 return ERR_PTR(err); 327 } 328 } 329 330 static int ntfs_sd_add_everyone(struct ntfs_inode *ni) 331 { 332 struct security_descriptor_relative *sd; 333 struct ntfs_acl *acl; 334 struct ntfs_ace *ace; 335 struct ntfs_sid *sid; 336 int ret, sd_len; 337 338 /* Create SECURITY_DESCRIPTOR attribute (everyone has full access). */ 339 /* 340 * Calculate security descriptor length. We have 2 sub-authorities in 341 * owner and group SIDs, So add 8 bytes to every SID. 342 */ 343 sd_len = sizeof(struct security_descriptor_relative) + 2 * 344 (sizeof(struct ntfs_sid) + 8) + sizeof(struct ntfs_acl) + 345 sizeof(struct ntfs_ace) + 4; 346 sd = kzalloc(sd_len, GFP_NOFS); 347 if (!sd) 348 return -ENOMEM; 349 350 sd->revision = 1; 351 sd->control = SE_DACL_PRESENT | SE_SELF_RELATIVE; 352 353 sid = (struct ntfs_sid *)((u8 *)sd + sizeof(struct security_descriptor_relative)); 354 sid->revision = 1; 355 sid->sub_authority_count = 2; 356 sid->sub_authority[0] = cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID); 357 sid->sub_authority[1] = cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS); 358 sid->identifier_authority.value[5] = 5; 359 sd->owner = cpu_to_le32((u8 *)sid - (u8 *)sd); 360 361 sid = (struct ntfs_sid *)((u8 *)sid + sizeof(struct ntfs_sid) + 8); 362 sid->revision = 1; 363 sid->sub_authority_count = 2; 364 sid->sub_authority[0] = cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID); 365 sid->sub_authority[1] = cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS); 366 sid->identifier_authority.value[5] = 5; 367 sd->group = cpu_to_le32((u8 *)sid - (u8 *)sd); 368 369 acl = (struct ntfs_acl *)((u8 *)sid + sizeof(struct ntfs_sid) + 8); 370 acl->revision = 2; 371 acl->size = cpu_to_le16(sizeof(struct ntfs_acl) + sizeof(struct ntfs_ace) + 4); 372 acl->ace_count = cpu_to_le16(1); 373 sd->dacl = cpu_to_le32((u8 *)acl - (u8 *)sd); 374 375 ace = (struct ntfs_ace *)((u8 *)acl + sizeof(struct ntfs_acl)); 376 ace->type = ACCESS_ALLOWED_ACE_TYPE; 377 ace->flags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE; 378 ace->size = cpu_to_le16(sizeof(struct ntfs_ace) + 4); 379 ace->mask = cpu_to_le32(0x1f01ff); 380 ace->sid.revision = 1; 381 ace->sid.sub_authority_count = 1; 382 ace->sid.sub_authority[0] = 0; 383 ace->sid.identifier_authority.value[5] = 1; 384 385 ret = ntfs_attr_add(ni, AT_SECURITY_DESCRIPTOR, AT_UNNAMED, 0, (u8 *)sd, 386 sd_len); 387 if (ret) 388 ntfs_error(ni->vol->sb, "Failed to add SECURITY_DESCRIPTOR\n"); 389 390 kfree(sd); 391 return ret; 392 } 393 394 static struct ntfs_inode *__ntfs_create(struct mnt_idmap *idmap, struct inode *dir, 395 __le16 *name, u8 name_len, mode_t mode, dev_t dev, 396 const char *target, int target_len) 397 { 398 struct ntfs_inode *dir_ni = NTFS_I(dir); 399 struct ntfs_volume *vol = dir_ni->vol; 400 struct ntfs_inode *ni; 401 bool rollback_data = false, rollback_sd = false, rollback_reparse = false; 402 struct file_name_attr *fn = NULL; 403 struct standard_information *si = NULL; 404 int err = 0, fn_len, si_len; 405 struct inode *vi; 406 struct mft_record *ni_mrec, *dni_mrec; 407 struct super_block *sb = dir_ni->vol->sb; 408 __le64 parent_mft_ref; 409 u64 child_mft_ref; 410 __le16 ea_size; 411 412 vi = new_inode(vol->sb); 413 if (!vi) 414 return ERR_PTR(-ENOMEM); 415 416 ntfs_init_big_inode(vi); 417 ni = NTFS_I(vi); 418 ni->vol = dir_ni->vol; 419 ni->name_len = 0; 420 ni->name = NULL; 421 422 /* 423 * Set the appropriate mode, attribute type, and name. For 424 * directories, also setup the index values to the defaults. 425 */ 426 if (S_ISDIR(mode)) { 427 mode &= ~vol->dmask; 428 429 NInoSetMstProtected(ni); 430 ni->itype.index.block_size = 4096; 431 ni->itype.index.block_size_bits = ntfs_ffs(4096) - 1; 432 ni->itype.index.collation_rule = COLLATION_FILE_NAME; 433 if (vol->cluster_size <= ni->itype.index.block_size) { 434 ni->itype.index.vcn_size = vol->cluster_size; 435 ni->itype.index.vcn_size_bits = 436 vol->cluster_size_bits; 437 } else { 438 ni->itype.index.vcn_size = vol->sector_size; 439 ni->itype.index.vcn_size_bits = 440 vol->sector_size_bits; 441 } 442 } else { 443 mode &= ~vol->fmask; 444 } 445 446 if (IS_RDONLY(vi)) 447 mode &= ~0222; 448 449 inode_init_owner(idmap, vi, dir, mode); 450 451 mode = vi->i_mode; 452 453 #ifdef CONFIG_NTFS_FS_POSIX_ACL 454 if (!S_ISLNK(mode) && (sb->s_flags & SB_POSIXACL)) { 455 err = ntfs_init_acl(idmap, vi, dir); 456 if (err) 457 goto err_out; 458 } else 459 #endif 460 { 461 vi->i_flags |= S_NOSEC; 462 } 463 464 if (uid_valid(vol->uid)) 465 vi->i_uid = vol->uid; 466 467 if (gid_valid(vol->gid)) 468 vi->i_gid = vol->gid; 469 470 /* 471 * Set the file size to 0, the ntfs inode sizes are set to 0 by 472 * the call to ntfs_init_big_inode() below. 473 */ 474 vi->i_size = 0; 475 vi->i_blocks = 0; 476 477 inode_inc_iversion(vi); 478 479 simple_inode_init_ts(vi); 480 ni->i_crtime = inode_get_ctime(vi); 481 482 inode_set_mtime_to_ts(dir, ni->i_crtime); 483 inode_set_ctime_to_ts(dir, ni->i_crtime); 484 mark_inode_dirty(dir); 485 486 err = ntfs_mft_record_alloc(dir_ni->vol, mode, &ni, NULL, 487 &ni_mrec); 488 if (err) { 489 iput(vi); 490 return ERR_PTR(err); 491 } 492 493 /* 494 * Prevent iget and writeback from finding this inode. 495 * Caller must call d_instantiate_new instead of d_instantiate. 496 */ 497 spin_lock(&vi->i_lock); 498 inode_state_set(vi, I_NEW | I_CREATING); 499 spin_unlock(&vi->i_lock); 500 501 /* Add the inode to the inode hash for the superblock. */ 502 vi->i_ino = (unsigned long)ni->mft_no; 503 inode_set_iversion(vi, 1); 504 insert_inode_hash(vi); 505 506 mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 507 mutex_lock_nested(&dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT); 508 if (NInoBeingDeleted(dir_ni)) { 509 err = -ENOENT; 510 goto err_out; 511 } 512 513 dni_mrec = map_mft_record(dir_ni); 514 if (IS_ERR(dni_mrec)) { 515 ntfs_error(dir_ni->vol->sb, "failed to map mft record for file 0x%llx.\n", 516 dir_ni->mft_no); 517 err = -EIO; 518 goto err_out; 519 } 520 parent_mft_ref = MK_LE_MREF(dir_ni->mft_no, 521 le16_to_cpu(dni_mrec->sequence_number)); 522 unmap_mft_record(dir_ni); 523 524 /* 525 * Create STANDARD_INFORMATION attribute. Write STANDARD_INFORMATION 526 * version 1.2, windows will upgrade it to version 3 if needed. 527 */ 528 si_len = offsetof(struct standard_information, file_attributes) + 529 sizeof(__le32) + 12; 530 si = kzalloc(si_len, GFP_NOFS); 531 if (!si) { 532 err = -ENOMEM; 533 goto err_out; 534 } 535 536 si->creation_time = si->last_data_change_time = utc2ntfs(ni->i_crtime); 537 si->last_mft_change_time = si->last_access_time = si->creation_time; 538 539 if (!S_ISREG(mode) && !S_ISDIR(mode)) 540 si->file_attributes = FILE_ATTR_SYSTEM; 541 542 /* Add STANDARD_INFORMATION to inode. */ 543 err = ntfs_attr_add(ni, AT_STANDARD_INFORMATION, AT_UNNAMED, 0, (u8 *)si, 544 si_len); 545 if (err) { 546 ntfs_error(sb, "Failed to add STANDARD_INFORMATION attribute.\n"); 547 goto err_out; 548 } 549 550 err = ntfs_sd_add_everyone(ni); 551 if (err) 552 goto err_out; 553 rollback_sd = true; 554 555 if (S_ISDIR(mode)) { 556 struct index_root *ir = NULL; 557 struct index_entry *ie; 558 int ir_len, index_len; 559 560 /* Create struct index_root attribute. */ 561 index_len = sizeof(struct index_header) + sizeof(struct index_entry_header); 562 ir_len = offsetof(struct index_root, index) + index_len; 563 ir = kzalloc(ir_len, GFP_NOFS); 564 if (!ir) { 565 err = -ENOMEM; 566 goto err_out; 567 } 568 ir->type = AT_FILE_NAME; 569 ir->collation_rule = COLLATION_FILE_NAME; 570 ir->index_block_size = cpu_to_le32(ni->vol->index_record_size); 571 if (ni->vol->cluster_size <= ni->vol->index_record_size) 572 ir->clusters_per_index_block = 573 NTFS_B_TO_CLU(vol, ni->vol->index_record_size); 574 else 575 ir->clusters_per_index_block = 576 ni->vol->index_record_size >> ni->vol->sector_size_bits; 577 ir->index.entries_offset = cpu_to_le32(sizeof(struct index_header)); 578 ir->index.index_length = cpu_to_le32(index_len); 579 ir->index.allocated_size = cpu_to_le32(index_len); 580 ie = (struct index_entry *)((u8 *)ir + sizeof(struct index_root)); 581 ie->length = cpu_to_le16(sizeof(struct index_entry_header)); 582 ie->key_length = 0; 583 ie->flags = INDEX_ENTRY_END; 584 585 /* Add struct index_root attribute to inode. */ 586 err = ntfs_attr_add(ni, AT_INDEX_ROOT, I30, 4, (u8 *)ir, ir_len); 587 if (err) { 588 kfree(ir); 589 ntfs_error(vi->i_sb, "Failed to add struct index_root attribute.\n"); 590 goto err_out; 591 } 592 kfree(ir); 593 err = ntfs_attr_open(ni, AT_INDEX_ROOT, I30, 4); 594 if (err) 595 goto err_out; 596 } else { 597 /* Add DATA attribute to inode. */ 598 err = ntfs_attr_add(ni, AT_DATA, AT_UNNAMED, 0, NULL, 0); 599 if (err) { 600 ntfs_error(dir_ni->vol->sb, "Failed to add DATA attribute.\n"); 601 goto err_out; 602 } 603 rollback_data = true; 604 605 err = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); 606 if (err) 607 goto err_out; 608 609 if (S_ISLNK(mode)) { 610 if (NVolSymlinkNative(vol)) 611 err = ntfs_reparse_set_native_symlink(ni, target, target_len); 612 else 613 err = ntfs_reparse_set_wsl_symlink(ni, target, target_len); 614 if (!err) 615 rollback_reparse = true; 616 } else if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode) || 617 S_ISFIFO(mode)) { 618 si->file_attributes = FILE_ATTRIBUTE_RECALL_ON_OPEN; 619 ni->flags = FILE_ATTRIBUTE_RECALL_ON_OPEN; 620 err = ntfs_reparse_set_wsl_not_symlink(ni, mode); 621 if (!err) 622 rollback_reparse = true; 623 } 624 if (err) 625 goto err_out; 626 } 627 628 err = ntfs_ea_set_wsl_inode(vi, dev, &ea_size, 629 NTFS_EA_UID | NTFS_EA_GID | NTFS_EA_MODE); 630 if (err) 631 goto err_out; 632 633 /* Create FILE_NAME attribute. */ 634 fn_len = sizeof(struct file_name_attr) + name_len * sizeof(__le16); 635 fn = kzalloc(fn_len, GFP_NOFS); 636 if (!fn) { 637 err = -ENOMEM; 638 goto err_out; 639 } 640 641 fn->file_attributes |= ni->flags; 642 fn->parent_directory = parent_mft_ref; 643 fn->file_name_length = name_len; 644 fn->file_name_type = FILE_NAME_POSIX; 645 fn->type.ea.packed_ea_size = ea_size; 646 if (S_ISDIR(mode)) { 647 fn->file_attributes = FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT; 648 fn->allocated_size = fn->data_size = 0; 649 } else { 650 fn->data_size = cpu_to_le64(ni->data_size); 651 fn->allocated_size = cpu_to_le64(ni->allocated_size); 652 } 653 if (!S_ISREG(mode) && !S_ISDIR(mode)) { 654 fn->file_attributes = FILE_ATTR_SYSTEM; 655 if (rollback_reparse) 656 fn->file_attributes |= FILE_ATTR_REPARSE_POINT; 657 } 658 if (NVolHideDotFiles(vol) && name_len > 0 && name[0] == cpu_to_le16('.')) 659 fn->file_attributes |= FILE_ATTR_HIDDEN; 660 fn->creation_time = fn->last_data_change_time = utc2ntfs(ni->i_crtime); 661 fn->last_mft_change_time = fn->last_access_time = fn->creation_time; 662 memcpy(fn->file_name, name, name_len * sizeof(__le16)); 663 664 /* Add FILE_NAME attribute to inode. */ 665 err = ntfs_attr_add(ni, AT_FILE_NAME, AT_UNNAMED, 0, (u8 *)fn, fn_len); 666 if (err) { 667 ntfs_error(sb, "Failed to add FILE_NAME attribute.\n"); 668 goto err_out; 669 } 670 671 child_mft_ref = MK_MREF(ni->mft_no, 672 le16_to_cpu(ni_mrec->sequence_number)); 673 /* Set hard links count and directory flag. */ 674 ni_mrec->link_count = cpu_to_le16(1); 675 mark_mft_record_dirty(ni); 676 677 /* Add FILE_NAME attribute to index. */ 678 err = ntfs_index_add_filename(dir_ni, fn, child_mft_ref); 679 if (err) { 680 ntfs_debug("Failed to add entry to the index"); 681 goto err_out; 682 } 683 684 unmap_mft_record(ni); 685 mutex_unlock(&dir_ni->mrec_lock); 686 mutex_unlock(&ni->mrec_lock); 687 688 ni->flags = fn->file_attributes; 689 /* Set the sequence number. */ 690 vi->i_generation = ni->seq_no; 691 set_nlink(vi, 1); 692 ntfs_set_vfs_operations(vi, mode, dev); 693 694 /* Done! */ 695 kfree(fn); 696 kfree(si); 697 ntfs_debug("Done.\n"); 698 return ni; 699 700 err_out: 701 if (rollback_sd) 702 ntfs_attr_remove(ni, AT_SECURITY_DESCRIPTOR, AT_UNNAMED, 0); 703 704 if (rollback_data) 705 ntfs_attr_remove(ni, AT_DATA, AT_UNNAMED, 0); 706 707 if (rollback_reparse) 708 ntfs_delete_reparse_index(ni); 709 /* 710 * Free extent MFT records (should not exist any with current 711 * ntfs_create implementation, but for any case if something will be 712 * changed in the future). 713 */ 714 while (ni->nr_extents != 0) { 715 int err2; 716 717 err2 = ntfs_mft_record_free(ni->vol, *(ni->ext.extent_ntfs_inos)); 718 if (err2) 719 ntfs_error(sb, 720 "Failed to free extent MFT record. Leaving inconsistent metadata.\n"); 721 ntfs_inode_close(*(ni->ext.extent_ntfs_inos)); 722 } 723 if (ntfs_mft_record_free(ni->vol, ni)) 724 ntfs_error(sb, 725 "Failed to free MFT record. Leaving inconsistent metadata. Run chkdsk.\n"); 726 unmap_mft_record(ni); 727 kfree(fn); 728 kfree(si); 729 730 mutex_unlock(&dir_ni->mrec_lock); 731 mutex_unlock(&ni->mrec_lock); 732 733 remove_inode_hash(vi); 734 discard_new_inode(vi); 735 return ERR_PTR(err); 736 } 737 738 static int ntfs_create(struct mnt_idmap *idmap, struct inode *dir, 739 struct dentry *dentry, umode_t mode, bool excl) 740 { 741 struct ntfs_volume *vol = NTFS_SB(dir->i_sb); 742 struct ntfs_inode *ni; 743 __le16 *uname; 744 int uname_len, err; 745 746 if (NVolShutdown(vol)) 747 return -EIO; 748 749 uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len, 750 &uname, NTFS_MAX_NAME_LEN); 751 if (uname_len < 0) { 752 if (uname_len != -ENAMETOOLONG) 753 ntfs_error(vol->sb, "Failed to convert name to unicode."); 754 return uname_len; 755 } 756 757 err = ntfs_check_bad_windows_name(vol, uname, uname_len); 758 if (err) { 759 kmem_cache_free(ntfs_name_cache, uname); 760 return err; 761 } 762 763 if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 764 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 765 766 ni = __ntfs_create(idmap, dir, uname, uname_len, S_IFREG | mode, 0, NULL, 0); 767 kmem_cache_free(ntfs_name_cache, uname); 768 if (IS_ERR(ni)) 769 return PTR_ERR(ni); 770 771 d_instantiate_new(dentry, VFS_I(ni)); 772 773 return 0; 774 } 775 776 static int ntfs_check_unlinkable_dir(struct ntfs_attr_search_ctx *ctx, struct file_name_attr *fn) 777 { 778 int link_count; 779 int ret; 780 struct ntfs_inode *ni = ctx->base_ntfs_ino ? ctx->base_ntfs_ino : ctx->ntfs_ino; 781 struct mft_record *ni_mrec = ctx->base_mrec ? ctx->base_mrec : ctx->mrec; 782 783 ret = ntfs_check_empty_dir(ni, ni_mrec); 784 if (!ret || ret != -ENOTEMPTY) 785 return ret; 786 787 link_count = le16_to_cpu(ni_mrec->link_count); 788 /* 789 * Directory is non-empty, so we can unlink only if there is more than 790 * one "real" hard link, i.e. links aren't different DOS and WIN32 names 791 */ 792 if ((link_count == 1) || 793 (link_count == 2 && fn->file_name_type == FILE_NAME_DOS)) { 794 ret = -ENOTEMPTY; 795 ntfs_debug("Non-empty directory without hard links\n"); 796 goto no_hardlink; 797 } 798 799 ret = 0; 800 no_hardlink: 801 return ret; 802 } 803 804 static int ntfs_test_inode_attr(struct inode *vi, void *data) 805 { 806 struct ntfs_inode *ni = NTFS_I(vi); 807 u64 mft_no = (u64)(uintptr_t)data; 808 809 if (ni->mft_no != mft_no) 810 return 0; 811 if (NInoAttr(ni) || ni->nr_extents == -1) 812 return 1; 813 else 814 return 0; 815 } 816 817 /* 818 * ntfs_delete - delete file or directory from ntfs volume 819 * @ni: ntfs inode for object to delte 820 * @dir_ni: ntfs inode for directory in which delete object 821 * @name: unicode name of the object to delete 822 * @name_len: length of the name in unicode characters 823 * @need_lock: whether mrec lock is needed or not 824 * 825 * Delete the specified name from the directory index @dir_ni and decrement 826 * the link count of the target inode @ni. 827 * 828 * Return 0 on success and -errno on error. 829 */ 830 static int ntfs_delete(struct ntfs_inode *ni, struct ntfs_inode *dir_ni, 831 __le16 *name, u8 name_len, bool need_lock) 832 { 833 struct ntfs_attr_search_ctx *actx = NULL; 834 struct file_name_attr *fn = NULL; 835 bool looking_for_dos_name = false, looking_for_win32_name = false; 836 bool case_sensitive_match = true; 837 int err = 0; 838 struct mft_record *ni_mrec; 839 struct super_block *sb; 840 bool link_count_zero = false; 841 842 ntfs_debug("Entering.\n"); 843 844 if (need_lock == true) { 845 mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 846 mutex_lock_nested(&dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT); 847 } 848 849 sb = dir_ni->vol->sb; 850 851 if (ni->nr_extents == -1) 852 ni = ni->ext.base_ntfs_ino; 853 if (dir_ni->nr_extents == -1) 854 dir_ni = dir_ni->ext.base_ntfs_ino; 855 /* 856 * Search for FILE_NAME attribute with such name. If it's in POSIX or 857 * WIN32_AND_DOS namespace, then simply remove it from index and inode. 858 * If filename in DOS or in WIN32 namespace, then remove DOS name first, 859 * only then remove WIN32 name. 860 */ 861 actx = ntfs_attr_get_search_ctx(ni, NULL); 862 if (!actx) { 863 ntfs_error(sb, "%s, Failed to get search context", __func__); 864 if (need_lock) { 865 mutex_unlock(&dir_ni->mrec_lock); 866 mutex_unlock(&ni->mrec_lock); 867 } 868 return -ENOMEM; 869 } 870 search: 871 while ((err = ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, CASE_SENSITIVE, 872 0, NULL, 0, actx)) == 0) { 873 #ifdef DEBUG 874 unsigned char *s; 875 #endif 876 bool case_sensitive = IGNORE_CASE; 877 878 fn = (struct file_name_attr *)((u8 *)actx->attr + 879 le16_to_cpu(actx->attr->data.resident.value_offset)); 880 #ifdef DEBUG 881 s = ntfs_attr_name_get(ni->vol, fn->file_name, fn->file_name_length); 882 ntfs_debug("name: '%s' type: %d dos: %d win32: %d case: %d\n", 883 s, fn->file_name_type, 884 looking_for_dos_name, looking_for_win32_name, 885 case_sensitive_match); 886 ntfs_attr_name_free(&s); 887 #endif 888 if (looking_for_dos_name) { 889 if (fn->file_name_type == FILE_NAME_DOS) 890 break; 891 continue; 892 } 893 if (looking_for_win32_name) { 894 if (fn->file_name_type == FILE_NAME_WIN32) 895 break; 896 continue; 897 } 898 899 /* Ignore hard links from other directories */ 900 if (dir_ni->mft_no != MREF_LE(fn->parent_directory)) { 901 ntfs_debug("MFT record numbers don't match (%llu != %lu)\n", 902 dir_ni->mft_no, 903 MREF_LE(fn->parent_directory)); 904 continue; 905 } 906 907 if (fn->file_name_type == FILE_NAME_POSIX || case_sensitive_match) 908 case_sensitive = CASE_SENSITIVE; 909 910 if (ntfs_names_are_equal(fn->file_name, fn->file_name_length, 911 name, name_len, case_sensitive, 912 ni->vol->upcase, ni->vol->upcase_len)) { 913 if (fn->file_name_type == FILE_NAME_WIN32) { 914 looking_for_dos_name = true; 915 ntfs_attr_reinit_search_ctx(actx); 916 continue; 917 } 918 if (fn->file_name_type == FILE_NAME_DOS) 919 looking_for_dos_name = true; 920 break; 921 } 922 } 923 if (err) { 924 /* 925 * If case sensitive search failed, then try once again 926 * ignoring case. 927 */ 928 if (err == -ENOENT && case_sensitive_match) { 929 case_sensitive_match = false; 930 ntfs_attr_reinit_search_ctx(actx); 931 goto search; 932 } 933 goto err_out; 934 } 935 936 err = ntfs_check_unlinkable_dir(actx, fn); 937 if (err) 938 goto err_out; 939 940 err = ntfs_index_remove(dir_ni, fn, le32_to_cpu(actx->attr->data.resident.value_length)); 941 if (err) 942 goto err_out; 943 944 err = ntfs_attr_record_rm(actx); 945 if (err) 946 goto err_out; 947 948 ni_mrec = actx->base_mrec ? actx->base_mrec : actx->mrec; 949 ni_mrec->link_count = cpu_to_le16(le16_to_cpu(ni_mrec->link_count) - 1); 950 if (!S_ISDIR(VFS_I(ni)->i_mode)) 951 drop_nlink(VFS_I(ni)); 952 953 mark_mft_record_dirty(ni); 954 if (looking_for_dos_name) { 955 looking_for_dos_name = false; 956 looking_for_win32_name = true; 957 ntfs_attr_reinit_search_ctx(actx); 958 goto search; 959 } 960 961 /* 962 * For directories, Drop VFS nlink only when mft record link count 963 * becomes zero. Because we fixes VFS nlink to 1 for directories. 964 */ 965 if (S_ISDIR(VFS_I(ni)->i_mode) && !le16_to_cpu(ni_mrec->link_count)) 966 drop_nlink(VFS_I(ni)); 967 968 /* 969 * If hard link count is not equal to zero then we are done. In other 970 * case there are no reference to this inode left, so we should free all 971 * non-resident attributes and mark all MFT record as not in use. 972 */ 973 if (ni_mrec->link_count == 0) { 974 NInoSetBeingDeleted(ni); 975 ntfs_delete_reparse_index(ni); 976 ntfs_delete_object_id_index(ni); 977 link_count_zero = true; 978 } 979 980 ntfs_attr_put_search_ctx(actx); 981 if (need_lock == true) { 982 mutex_unlock(&dir_ni->mrec_lock); 983 mutex_unlock(&ni->mrec_lock); 984 } 985 986 /* 987 * If hard link count is not equal to zero then we are done. In other 988 * case there are no reference to this inode left, so we should free all 989 * non-resident attributes and mark all MFT record as not in use. 990 */ 991 if (link_count_zero == true) { 992 struct inode *attr_vi; 993 994 while ((attr_vi = ilookup5(sb, ni->mft_no, ntfs_test_inode_attr, 995 (void *)(uintptr_t)ni->mft_no)) != NULL) { 996 clear_nlink(attr_vi); 997 iput(attr_vi); 998 } 999 } 1000 ntfs_debug("Done.\n"); 1001 return 0; 1002 err_out: 1003 ntfs_attr_put_search_ctx(actx); 1004 if (need_lock) { 1005 mutex_unlock(&dir_ni->mrec_lock); 1006 mutex_unlock(&ni->mrec_lock); 1007 } 1008 return err; 1009 } 1010 1011 static int ntfs_unlink(struct inode *dir, struct dentry *dentry) 1012 { 1013 struct inode *vi = dentry->d_inode; 1014 struct super_block *sb = dir->i_sb; 1015 struct ntfs_volume *vol = NTFS_SB(sb); 1016 int err = 0; 1017 struct ntfs_inode *ni = NTFS_I(vi); 1018 __le16 *uname = NULL; 1019 int uname_len; 1020 1021 if (NVolShutdown(vol)) 1022 return -EIO; 1023 1024 uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len, 1025 &uname, NTFS_MAX_NAME_LEN); 1026 if (uname_len < 0) { 1027 if (uname_len != -ENAMETOOLONG) 1028 ntfs_error(sb, "Failed to convert name to Unicode."); 1029 return -ENOMEM; 1030 } 1031 1032 err = ntfs_check_bad_windows_name(vol, uname, uname_len); 1033 if (err) { 1034 kmem_cache_free(ntfs_name_cache, uname); 1035 return err; 1036 } 1037 1038 if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 1039 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 1040 1041 err = ntfs_delete(ni, NTFS_I(dir), uname, uname_len, true); 1042 if (err) 1043 goto out; 1044 1045 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); 1046 mark_inode_dirty(dir); 1047 inode_set_ctime_to_ts(vi, inode_get_ctime(dir)); 1048 if (vi->i_nlink) 1049 mark_inode_dirty(vi); 1050 out: 1051 kmem_cache_free(ntfs_name_cache, uname); 1052 return err; 1053 } 1054 1055 static struct dentry *ntfs_mkdir(struct mnt_idmap *idmap, struct inode *dir, 1056 struct dentry *dentry, umode_t mode) 1057 { 1058 struct super_block *sb = dir->i_sb; 1059 struct ntfs_volume *vol = NTFS_SB(sb); 1060 int err = 0; 1061 struct ntfs_inode *ni; 1062 __le16 *uname; 1063 int uname_len; 1064 1065 if (NVolShutdown(vol)) 1066 return ERR_PTR(-EIO); 1067 1068 uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len, 1069 &uname, NTFS_MAX_NAME_LEN); 1070 if (uname_len < 0) { 1071 if (uname_len != -ENAMETOOLONG) 1072 ntfs_error(sb, "Failed to convert name to unicode."); 1073 return ERR_PTR(-ENOMEM); 1074 } 1075 1076 err = ntfs_check_bad_windows_name(vol, uname, uname_len); 1077 if (err) { 1078 kmem_cache_free(ntfs_name_cache, uname); 1079 return ERR_PTR(err); 1080 } 1081 1082 if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 1083 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 1084 1085 ni = __ntfs_create(idmap, dir, uname, uname_len, S_IFDIR | mode, 0, NULL, 0); 1086 kmem_cache_free(ntfs_name_cache, uname); 1087 if (IS_ERR(ni)) { 1088 err = PTR_ERR(ni); 1089 return ERR_PTR(err); 1090 } 1091 1092 d_instantiate_new(dentry, VFS_I(ni)); 1093 return NULL; 1094 } 1095 1096 static int ntfs_rmdir(struct inode *dir, struct dentry *dentry) 1097 { 1098 struct inode *vi = dentry->d_inode; 1099 struct super_block *sb = dir->i_sb; 1100 struct ntfs_volume *vol = NTFS_SB(sb); 1101 int err = 0; 1102 struct ntfs_inode *ni; 1103 __le16 *uname = NULL; 1104 int uname_len; 1105 1106 if (NVolShutdown(vol)) 1107 return -EIO; 1108 1109 ni = NTFS_I(vi); 1110 uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len, 1111 &uname, NTFS_MAX_NAME_LEN); 1112 if (uname_len < 0) { 1113 if (uname_len != -ENAMETOOLONG) 1114 ntfs_error(sb, "Failed to convert name to unicode."); 1115 return -ENOMEM; 1116 } 1117 1118 err = ntfs_check_bad_windows_name(vol, uname, uname_len); 1119 if (err) { 1120 kmem_cache_free(ntfs_name_cache, uname); 1121 return err; 1122 } 1123 1124 if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 1125 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 1126 1127 err = ntfs_delete(ni, NTFS_I(dir), uname, uname_len, true); 1128 if (err) 1129 goto out; 1130 1131 inode_set_mtime_to_ts(vi, inode_set_atime_to_ts(vi, current_time(vi))); 1132 out: 1133 kmem_cache_free(ntfs_name_cache, uname); 1134 return err; 1135 } 1136 1137 /* 1138 * __ntfs_link - create hard link for file or directory 1139 * @ni: ntfs inode for object to create hard link 1140 * @dir_ni: ntfs inode for directory in which new link should be placed 1141 * @name: unicode name of the new link 1142 * @name_len: length of the name in unicode characters 1143 * 1144 * Create a new hard link. This involves adding an entry to the directory 1145 * index and adding a new FILE_NAME attribute to the target inode. 1146 * 1147 * Return 0 on success and -errno on error. 1148 */ 1149 static int __ntfs_link(struct ntfs_inode *ni, struct ntfs_inode *dir_ni, 1150 __le16 *name, u8 name_len) 1151 { 1152 struct super_block *sb; 1153 struct inode *vi = VFS_I(ni); 1154 struct file_name_attr *fn = NULL; 1155 int fn_len, err = 0; 1156 struct mft_record *dir_mrec = NULL, *ni_mrec = NULL; 1157 1158 ntfs_debug("Entering.\n"); 1159 1160 sb = dir_ni->vol->sb; 1161 if (NInoBeingDeleted(dir_ni) || NInoBeingDeleted(ni)) 1162 return -ENOENT; 1163 1164 ni_mrec = map_mft_record(ni); 1165 if (IS_ERR(ni_mrec)) { 1166 err = -EIO; 1167 goto err_out; 1168 } 1169 1170 if (le16_to_cpu(ni_mrec->link_count) == 0) { 1171 err = -ENOENT; 1172 goto err_out; 1173 } 1174 1175 /* Create FILE_NAME attribute. */ 1176 fn_len = sizeof(struct file_name_attr) + name_len * sizeof(__le16); 1177 1178 fn = kzalloc(fn_len, GFP_NOFS); 1179 if (!fn) { 1180 err = -ENOMEM; 1181 goto err_out; 1182 } 1183 1184 dir_mrec = map_mft_record(dir_ni); 1185 if (IS_ERR(dir_mrec)) { 1186 err = -EIO; 1187 goto err_out; 1188 } 1189 1190 fn->parent_directory = MK_LE_MREF(dir_ni->mft_no, 1191 le16_to_cpu(dir_mrec->sequence_number)); 1192 unmap_mft_record(dir_ni); 1193 fn->file_name_length = name_len; 1194 fn->file_name_type = FILE_NAME_POSIX; 1195 fn->file_attributes = ni->flags; 1196 if (ni_mrec->flags & MFT_RECORD_IS_DIRECTORY) { 1197 fn->file_attributes |= FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT; 1198 fn->allocated_size = fn->data_size = 0; 1199 } else { 1200 if (NInoSparse(ni) || NInoCompressed(ni)) 1201 fn->allocated_size = 1202 cpu_to_le64(ni->itype.compressed.size); 1203 else 1204 fn->allocated_size = cpu_to_le64(ni->allocated_size); 1205 fn->data_size = cpu_to_le64(ni->data_size); 1206 } 1207 if (NVolHideDotFiles(dir_ni->vol) && name_len > 0 && name[0] == cpu_to_le16('.')) 1208 fn->file_attributes |= FILE_ATTR_HIDDEN; 1209 1210 fn->creation_time = utc2ntfs(ni->i_crtime); 1211 fn->last_data_change_time = utc2ntfs(inode_get_mtime(vi)); 1212 fn->last_mft_change_time = utc2ntfs(inode_get_ctime(vi)); 1213 fn->last_access_time = utc2ntfs(inode_get_atime(vi)); 1214 memcpy(fn->file_name, name, name_len * sizeof(__le16)); 1215 1216 /* Add FILE_NAME attribute to index. */ 1217 err = ntfs_index_add_filename(dir_ni, fn, MK_MREF(ni->mft_no, 1218 le16_to_cpu(ni_mrec->sequence_number))); 1219 if (err) { 1220 ntfs_error(sb, "Failed to add filename to the index"); 1221 goto err_out; 1222 } 1223 /* Add FILE_NAME attribute to inode. */ 1224 err = ntfs_attr_add(ni, AT_FILE_NAME, AT_UNNAMED, 0, (u8 *)fn, fn_len); 1225 if (err) { 1226 ntfs_error(sb, "Failed to add FILE_NAME attribute.\n"); 1227 /* Try to remove just added attribute from index. */ 1228 if (ntfs_index_remove(dir_ni, fn, fn_len)) 1229 goto rollback_failed; 1230 goto err_out; 1231 } 1232 /* Increment hard links count. */ 1233 ni_mrec->link_count = cpu_to_le16(le16_to_cpu(ni_mrec->link_count) + 1); 1234 if (!S_ISDIR(vi->i_mode)) 1235 inc_nlink(VFS_I(ni)); 1236 1237 /* Done! */ 1238 mark_mft_record_dirty(ni); 1239 kfree(fn); 1240 unmap_mft_record(ni); 1241 1242 ntfs_debug("Done.\n"); 1243 1244 return 0; 1245 rollback_failed: 1246 ntfs_error(sb, "Rollback failed. Leaving inconsistent metadata.\n"); 1247 err_out: 1248 kfree(fn); 1249 if (!IS_ERR_OR_NULL(ni_mrec)) 1250 unmap_mft_record(ni); 1251 return err; 1252 } 1253 1254 static int ntfs_rename(struct mnt_idmap *idmap, struct inode *old_dir, 1255 struct dentry *old_dentry, struct inode *new_dir, 1256 struct dentry *new_dentry, unsigned int flags) 1257 { 1258 struct inode *old_inode, *new_inode = NULL; 1259 int err = 0; 1260 int is_dir; 1261 struct super_block *sb = old_dir->i_sb; 1262 __le16 *uname_new = NULL; 1263 __le16 *uname_old = NULL; 1264 int new_name_len; 1265 int old_name_len; 1266 struct ntfs_volume *vol = NTFS_SB(sb); 1267 struct ntfs_inode *old_ni, *new_ni = NULL; 1268 struct ntfs_inode *old_dir_ni = NTFS_I(old_dir), *new_dir_ni = NTFS_I(new_dir); 1269 1270 if (NVolShutdown(old_dir_ni->vol)) 1271 return -EIO; 1272 1273 if (flags & (RENAME_EXCHANGE | RENAME_WHITEOUT)) 1274 return -EINVAL; 1275 1276 new_name_len = ntfs_nlstoucs(NTFS_I(new_dir)->vol, new_dentry->d_name.name, 1277 new_dentry->d_name.len, &uname_new, 1278 NTFS_MAX_NAME_LEN); 1279 if (new_name_len < 0) { 1280 if (new_name_len != -ENAMETOOLONG) 1281 ntfs_error(sb, "Failed to convert name to unicode."); 1282 return -ENOMEM; 1283 } 1284 1285 err = ntfs_check_bad_windows_name(vol, uname_new, new_name_len); 1286 if (err) { 1287 kmem_cache_free(ntfs_name_cache, uname_new); 1288 return err; 1289 } 1290 1291 old_name_len = ntfs_nlstoucs(NTFS_I(old_dir)->vol, old_dentry->d_name.name, 1292 old_dentry->d_name.len, &uname_old, 1293 NTFS_MAX_NAME_LEN); 1294 if (old_name_len < 0) { 1295 kmem_cache_free(ntfs_name_cache, uname_new); 1296 if (old_name_len != -ENAMETOOLONG) 1297 ntfs_error(sb, "Failed to convert name to unicode."); 1298 return -ENOMEM; 1299 } 1300 1301 old_inode = old_dentry->d_inode; 1302 new_inode = new_dentry->d_inode; 1303 old_ni = NTFS_I(old_inode); 1304 1305 if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 1306 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 1307 1308 mutex_lock_nested(&old_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 1309 mutex_lock_nested(&old_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT); 1310 1311 if (NInoBeingDeleted(old_ni) || NInoBeingDeleted(old_dir_ni)) { 1312 err = -ENOENT; 1313 goto unlock_old; 1314 } 1315 1316 is_dir = S_ISDIR(old_inode->i_mode); 1317 1318 if (new_inode) { 1319 new_ni = NTFS_I(new_inode); 1320 mutex_lock_nested(&new_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL_2); 1321 if (old_dir != new_dir) { 1322 mutex_lock_nested(&new_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT_2); 1323 if (NInoBeingDeleted(new_dir_ni)) { 1324 err = -ENOENT; 1325 goto err_out; 1326 } 1327 } 1328 1329 if (NInoBeingDeleted(new_ni)) { 1330 err = -ENOENT; 1331 goto err_out; 1332 } 1333 1334 if (is_dir) { 1335 struct mft_record *ni_mrec; 1336 1337 ni_mrec = map_mft_record(NTFS_I(new_inode)); 1338 if (IS_ERR(ni_mrec)) { 1339 err = -EIO; 1340 goto err_out; 1341 } 1342 err = ntfs_check_empty_dir(NTFS_I(new_inode), ni_mrec); 1343 unmap_mft_record(NTFS_I(new_inode)); 1344 if (err) 1345 goto err_out; 1346 } 1347 1348 err = ntfs_delete(new_ni, new_dir_ni, uname_new, new_name_len, false); 1349 if (err) 1350 goto err_out; 1351 } else { 1352 if (old_dir != new_dir) { 1353 mutex_lock_nested(&new_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT_2); 1354 if (NInoBeingDeleted(new_dir_ni)) { 1355 err = -ENOENT; 1356 goto err_out; 1357 } 1358 } 1359 } 1360 1361 err = __ntfs_link(old_ni, new_dir_ni, uname_new, new_name_len); 1362 if (err) 1363 goto err_out; 1364 1365 err = ntfs_delete(old_ni, old_dir_ni, uname_old, old_name_len, false); 1366 if (err) { 1367 int err2; 1368 1369 ntfs_error(sb, "Failed to delete old ntfs inode(%llu) in old dir, err : %d\n", 1370 old_ni->mft_no, err); 1371 err2 = ntfs_delete(old_ni, new_dir_ni, uname_new, new_name_len, false); 1372 if (err2) 1373 ntfs_error(sb, "Failed to delete old ntfs inode in new dir, err : %d\n", 1374 err2); 1375 goto err_out; 1376 } 1377 1378 simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry); 1379 mark_inode_dirty(old_inode); 1380 mark_inode_dirty(old_dir); 1381 if (old_dir != new_dir) 1382 mark_inode_dirty(new_dir); 1383 if (new_inode) 1384 mark_inode_dirty(old_inode); 1385 1386 inode_inc_iversion(new_dir); 1387 1388 err_out: 1389 if (old_dir != new_dir) 1390 mutex_unlock(&new_dir_ni->mrec_lock); 1391 if (new_inode) 1392 mutex_unlock(&new_ni->mrec_lock); 1393 1394 unlock_old: 1395 mutex_unlock(&old_dir_ni->mrec_lock); 1396 mutex_unlock(&old_ni->mrec_lock); 1397 if (uname_new) 1398 kmem_cache_free(ntfs_name_cache, uname_new); 1399 if (uname_old) 1400 kmem_cache_free(ntfs_name_cache, uname_old); 1401 1402 return err; 1403 } 1404 1405 static int ntfs_symlink(struct mnt_idmap *idmap, struct inode *dir, 1406 struct dentry *dentry, const char *symname) 1407 { 1408 struct super_block *sb = dir->i_sb; 1409 struct ntfs_volume *vol = NTFS_SB(sb); 1410 struct inode *vi; 1411 int err = 0; 1412 struct ntfs_inode *ni; 1413 __le16 *usrc; 1414 int usrc_len; 1415 int symlen = strlen(symname); 1416 1417 if (NVolShutdown(vol)) 1418 return -EIO; 1419 1420 usrc_len = ntfs_nlstoucs(vol, dentry->d_name.name, 1421 dentry->d_name.len, &usrc, NTFS_MAX_NAME_LEN); 1422 if (usrc_len < 0) { 1423 if (usrc_len != -ENAMETOOLONG) 1424 ntfs_error(sb, "Failed to convert name to Unicode."); 1425 err = -ENOMEM; 1426 goto out; 1427 } 1428 1429 err = ntfs_check_bad_windows_name(vol, usrc, usrc_len); 1430 if (err) { 1431 kmem_cache_free(ntfs_name_cache, usrc); 1432 goto out; 1433 } 1434 1435 if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 1436 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 1437 1438 ni = __ntfs_create(idmap, dir, usrc, usrc_len, S_IFLNK | 0777, 0, 1439 symname, symlen); 1440 kmem_cache_free(ntfs_name_cache, usrc); 1441 if (IS_ERR(ni)) { 1442 err = PTR_ERR(ni); 1443 goto out; 1444 } 1445 1446 vi = VFS_I(ni); 1447 vi->i_size = symlen; 1448 d_instantiate_new(dentry, vi); 1449 out: 1450 return err; 1451 } 1452 1453 static int ntfs_mknod(struct mnt_idmap *idmap, struct inode *dir, 1454 struct dentry *dentry, umode_t mode, dev_t rdev) 1455 { 1456 struct super_block *sb = dir->i_sb; 1457 struct ntfs_volume *vol = NTFS_SB(sb); 1458 int err = 0; 1459 struct ntfs_inode *ni; 1460 __le16 *uname = NULL; 1461 int uname_len; 1462 1463 if (NVolShutdown(vol)) 1464 return -EIO; 1465 1466 uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, 1467 dentry->d_name.len, &uname, NTFS_MAX_NAME_LEN); 1468 if (uname_len < 0) { 1469 if (uname_len != -ENAMETOOLONG) 1470 ntfs_error(sb, "Failed to convert name to Unicode."); 1471 return -ENOMEM; 1472 } 1473 1474 err = ntfs_check_bad_windows_name(vol, uname, uname_len); 1475 if (err) { 1476 kmem_cache_free(ntfs_name_cache, uname); 1477 return err; 1478 } 1479 1480 if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 1481 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 1482 1483 switch (mode & S_IFMT) { 1484 case S_IFCHR: 1485 case S_IFBLK: 1486 ni = __ntfs_create(idmap, dir, uname, uname_len, 1487 mode, rdev, NULL, 0); 1488 break; 1489 default: 1490 ni = __ntfs_create(idmap, dir, uname, uname_len, 1491 mode, 0, NULL, 0); 1492 } 1493 1494 kmem_cache_free(ntfs_name_cache, uname); 1495 if (IS_ERR(ni)) { 1496 err = PTR_ERR(ni); 1497 goto out; 1498 } 1499 1500 d_instantiate_new(dentry, VFS_I(ni)); 1501 out: 1502 return err; 1503 } 1504 1505 static int ntfs_link(struct dentry *old_dentry, struct inode *dir, 1506 struct dentry *dentry) 1507 { 1508 struct inode *vi = old_dentry->d_inode; 1509 struct super_block *sb = vi->i_sb; 1510 struct ntfs_volume *vol = NTFS_SB(sb); 1511 __le16 *uname = NULL; 1512 int uname_len; 1513 int err; 1514 struct ntfs_inode *ni = NTFS_I(vi), *dir_ni = NTFS_I(dir); 1515 1516 if (NVolShutdown(vol)) 1517 return -EIO; 1518 1519 uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, 1520 dentry->d_name.len, &uname, NTFS_MAX_NAME_LEN); 1521 if (uname_len < 0) { 1522 if (uname_len != -ENAMETOOLONG) 1523 ntfs_error(sb, "Failed to convert name to unicode."); 1524 return -ENOMEM; 1525 } 1526 1527 if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 1528 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 1529 1530 ihold(vi); 1531 mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 1532 mutex_lock_nested(&dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT); 1533 err = __ntfs_link(NTFS_I(vi), NTFS_I(dir), uname, uname_len); 1534 if (err) { 1535 mutex_unlock(&dir_ni->mrec_lock); 1536 mutex_unlock(&ni->mrec_lock); 1537 iput(vi); 1538 pr_err("failed to create link, err = %d\n", err); 1539 goto out; 1540 } 1541 1542 inode_inc_iversion(dir); 1543 simple_inode_init_ts(dir); 1544 1545 inode_inc_iversion(vi); 1546 simple_inode_init_ts(vi); 1547 1548 /* timestamp is already written, so mark_inode_dirty() is unneeded. */ 1549 d_instantiate(dentry, vi); 1550 mutex_unlock(&dir_ni->mrec_lock); 1551 mutex_unlock(&ni->mrec_lock); 1552 1553 out: 1554 kmem_cache_free(ntfs_name_cache, uname); 1555 return err; 1556 } 1557 1558 /* 1559 * Inode operations for directories. 1560 */ 1561 const struct inode_operations ntfs_dir_inode_ops = { 1562 .lookup = ntfs_lookup, /* VFS: Lookup directory. */ 1563 .create = ntfs_create, 1564 .unlink = ntfs_unlink, 1565 .mkdir = ntfs_mkdir, 1566 .rmdir = ntfs_rmdir, 1567 .rename = ntfs_rename, 1568 .get_acl = ntfs_get_acl, 1569 .set_acl = ntfs_set_acl, 1570 .listxattr = ntfs_listxattr, 1571 .setattr = ntfs_setattr, 1572 .getattr = ntfs_getattr, 1573 .symlink = ntfs_symlink, 1574 .mknod = ntfs_mknod, 1575 .link = ntfs_link, 1576 }; 1577 1578 /* 1579 * ntfs_get_parent - find the dentry of the parent of a given directory dentry 1580 * @child_dent: dentry of the directory whose parent directory to find 1581 * 1582 * Find the dentry for the parent directory of the directory specified by the 1583 * dentry @child_dent. This function is called from 1584 * fs/exportfs/expfs.c::find_exported_dentry() which in turn is called from the 1585 * default ->decode_fh() which is export_decode_fh() in the same file. 1586 * 1587 * Note: ntfs_get_parent() is called with @d_inode(child_dent)->i_mutex down. 1588 * 1589 * Return the dentry of the parent directory on success or the error code on 1590 * error (IS_ERR() is true). 1591 */ 1592 static struct dentry *ntfs_get_parent(struct dentry *child_dent) 1593 { 1594 struct inode *vi = d_inode(child_dent); 1595 struct ntfs_inode *ni = NTFS_I(vi); 1596 struct mft_record *mrec; 1597 struct ntfs_attr_search_ctx *ctx; 1598 struct attr_record *attr; 1599 struct file_name_attr *fn; 1600 unsigned long parent_ino; 1601 int err; 1602 1603 ntfs_debug("Entering for inode 0x%llx.", ni->mft_no); 1604 /* Get the mft record of the inode belonging to the child dentry. */ 1605 mrec = map_mft_record(ni); 1606 if (IS_ERR(mrec)) 1607 return ERR_CAST(mrec); 1608 /* Find the first file name attribute in the mft record. */ 1609 ctx = ntfs_attr_get_search_ctx(ni, mrec); 1610 if (unlikely(!ctx)) { 1611 unmap_mft_record(ni); 1612 return ERR_PTR(-ENOMEM); 1613 } 1614 try_next: 1615 err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, CASE_SENSITIVE, 0, NULL, 1616 0, ctx); 1617 if (unlikely(err)) { 1618 ntfs_attr_put_search_ctx(ctx); 1619 unmap_mft_record(ni); 1620 if (err == -ENOENT) 1621 ntfs_error(vi->i_sb, 1622 "Inode 0x%llx does not have a file name attribute. Run chkdsk.", 1623 ni->mft_no); 1624 return ERR_PTR(err); 1625 } 1626 attr = ctx->attr; 1627 if (unlikely(attr->non_resident)) 1628 goto try_next; 1629 fn = (struct file_name_attr *)((u8 *)attr + 1630 le16_to_cpu(attr->data.resident.value_offset)); 1631 if (unlikely((u8 *)fn + le32_to_cpu(attr->data.resident.value_length) > 1632 (u8 *)attr + le32_to_cpu(attr->length))) 1633 goto try_next; 1634 /* Get the inode number of the parent directory. */ 1635 parent_ino = MREF_LE(fn->parent_directory); 1636 /* Release the search context and the mft record of the child. */ 1637 ntfs_attr_put_search_ctx(ctx); 1638 unmap_mft_record(ni); 1639 1640 return d_obtain_alias(ntfs_iget(vi->i_sb, parent_ino)); 1641 } 1642 1643 static struct inode *ntfs_nfs_get_inode(struct super_block *sb, 1644 u64 ino, u32 generation) 1645 { 1646 struct inode *inode; 1647 1648 inode = ntfs_iget(sb, ino); 1649 if (!IS_ERR(inode)) { 1650 if (inode->i_generation != generation) { 1651 iput(inode); 1652 inode = ERR_PTR(-ESTALE); 1653 } 1654 } 1655 1656 return inode; 1657 } 1658 1659 static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid, 1660 int fh_len, int fh_type) 1661 { 1662 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, 1663 ntfs_nfs_get_inode); 1664 } 1665 1666 static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid, 1667 int fh_len, int fh_type) 1668 { 1669 return generic_fh_to_parent(sb, fid, fh_len, fh_type, 1670 ntfs_nfs_get_inode); 1671 } 1672 1673 /* 1674 * Export operations allowing NFS exporting of mounted NTFS partitions. 1675 */ 1676 const struct export_operations ntfs_export_ops = { 1677 .encode_fh = generic_encode_ino32_fh, 1678 .get_parent = ntfs_get_parent, /* Find the parent of a given directory. */ 1679 .fh_to_dentry = ntfs_fh_to_dentry, 1680 .fh_to_parent = ntfs_fh_to_parent, 1681 }; 1682