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 (ni->flags & FILE_ATTRIBUTE_RECALL_ON_OPEN); 690 /* Set the sequence number. */ 691 vi->i_generation = ni->seq_no; 692 set_nlink(vi, 1); 693 ntfs_set_vfs_operations(vi, mode, dev); 694 695 /* Done! */ 696 kfree(fn); 697 kfree(si); 698 ntfs_debug("Done.\n"); 699 return ni; 700 701 err_out: 702 if (rollback_sd) 703 ntfs_attr_remove(ni, AT_SECURITY_DESCRIPTOR, AT_UNNAMED, 0); 704 705 if (rollback_data) 706 ntfs_attr_remove(ni, AT_DATA, AT_UNNAMED, 0); 707 708 if (rollback_reparse) 709 ntfs_delete_reparse_index(ni); 710 /* 711 * Free extent MFT records (should not exist any with current 712 * ntfs_create implementation, but for any case if something will be 713 * changed in the future). 714 */ 715 while (ni->nr_extents != 0) { 716 int err2; 717 718 err2 = ntfs_mft_record_free(ni->vol, *(ni->ext.extent_ntfs_inos)); 719 if (err2) 720 ntfs_error(sb, 721 "Failed to free extent MFT record. Leaving inconsistent metadata.\n"); 722 ntfs_inode_close(*(ni->ext.extent_ntfs_inos)); 723 } 724 if (ntfs_mft_record_free(ni->vol, ni)) 725 ntfs_error(sb, 726 "Failed to free MFT record. Leaving inconsistent metadata. Run chkdsk.\n"); 727 unmap_mft_record(ni); 728 kfree(fn); 729 kfree(si); 730 731 mutex_unlock(&dir_ni->mrec_lock); 732 mutex_unlock(&ni->mrec_lock); 733 734 remove_inode_hash(vi); 735 discard_new_inode(vi); 736 return ERR_PTR(err); 737 } 738 739 static int ntfs_create(struct mnt_idmap *idmap, struct inode *dir, 740 struct dentry *dentry, umode_t mode, bool excl) 741 { 742 struct ntfs_volume *vol = NTFS_SB(dir->i_sb); 743 struct ntfs_inode *ni; 744 __le16 *uname; 745 int uname_len, err; 746 747 if (NVolShutdown(vol)) 748 return -EIO; 749 750 uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len, 751 &uname, NTFS_MAX_NAME_LEN); 752 if (uname_len < 0) { 753 if (uname_len != -ENAMETOOLONG) 754 ntfs_error(vol->sb, "Failed to convert name to unicode."); 755 return uname_len; 756 } 757 758 err = ntfs_check_bad_windows_name(vol, uname, uname_len); 759 if (err) { 760 kmem_cache_free(ntfs_name_cache, uname); 761 return err; 762 } 763 764 if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 765 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 766 767 ni = __ntfs_create(idmap, dir, uname, uname_len, S_IFREG | mode, 0, NULL, 0); 768 kmem_cache_free(ntfs_name_cache, uname); 769 if (IS_ERR(ni)) 770 return PTR_ERR(ni); 771 772 d_instantiate_new(dentry, VFS_I(ni)); 773 774 return 0; 775 } 776 777 static int ntfs_check_unlinkable_dir(struct ntfs_attr_search_ctx *ctx, struct file_name_attr *fn) 778 { 779 int link_count; 780 int ret; 781 struct ntfs_inode *ni = ctx->base_ntfs_ino ? ctx->base_ntfs_ino : ctx->ntfs_ino; 782 struct mft_record *ni_mrec = ctx->base_mrec ? ctx->base_mrec : ctx->mrec; 783 784 ret = ntfs_check_empty_dir(ni, ni_mrec); 785 if (!ret || ret != -ENOTEMPTY) 786 return ret; 787 788 link_count = le16_to_cpu(ni_mrec->link_count); 789 /* 790 * Directory is non-empty, so we can unlink only if there is more than 791 * one "real" hard link, i.e. links aren't different DOS and WIN32 names 792 */ 793 if ((link_count == 1) || 794 (link_count == 2 && fn->file_name_type == FILE_NAME_DOS)) { 795 ret = -ENOTEMPTY; 796 ntfs_debug("Non-empty directory without hard links\n"); 797 goto no_hardlink; 798 } 799 800 ret = 0; 801 no_hardlink: 802 return ret; 803 } 804 805 static int ntfs_test_inode_attr(struct inode *vi, void *data) 806 { 807 struct ntfs_inode *ni = NTFS_I(vi); 808 u64 mft_no = (u64)(uintptr_t)data; 809 810 if (ni->mft_no != mft_no) 811 return 0; 812 if (NInoAttr(ni) || ni->nr_extents == -1) 813 return 1; 814 else 815 return 0; 816 } 817 818 /* 819 * ntfs_delete - delete file or directory from ntfs volume 820 * @ni: ntfs inode for object to delte 821 * @dir_ni: ntfs inode for directory in which delete object 822 * @name: unicode name of the object to delete 823 * @name_len: length of the name in unicode characters 824 * @need_lock: whether mrec lock is needed or not 825 * 826 * Delete the specified name from the directory index @dir_ni and decrement 827 * the link count of the target inode @ni. 828 * 829 * Return 0 on success and -errno on error. 830 */ 831 static int ntfs_delete(struct ntfs_inode *ni, struct ntfs_inode *dir_ni, 832 __le16 *name, u8 name_len, bool need_lock) 833 { 834 struct ntfs_attr_search_ctx *actx = NULL; 835 struct file_name_attr *fn = NULL; 836 bool looking_for_dos_name = false, looking_for_win32_name = false; 837 bool case_sensitive_match = true; 838 int err = 0; 839 struct mft_record *ni_mrec; 840 struct super_block *sb; 841 bool link_count_zero = false; 842 843 ntfs_debug("Entering.\n"); 844 845 if (need_lock == true) { 846 mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 847 mutex_lock_nested(&dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT); 848 } 849 850 sb = dir_ni->vol->sb; 851 852 if (ni->nr_extents == -1) 853 ni = ni->ext.base_ntfs_ino; 854 if (dir_ni->nr_extents == -1) 855 dir_ni = dir_ni->ext.base_ntfs_ino; 856 /* 857 * Search for FILE_NAME attribute with such name. If it's in POSIX or 858 * WIN32_AND_DOS namespace, then simply remove it from index and inode. 859 * If filename in DOS or in WIN32 namespace, then remove DOS name first, 860 * only then remove WIN32 name. 861 */ 862 actx = ntfs_attr_get_search_ctx(ni, NULL); 863 if (!actx) { 864 ntfs_error(sb, "%s, Failed to get search context", __func__); 865 if (need_lock) { 866 mutex_unlock(&dir_ni->mrec_lock); 867 mutex_unlock(&ni->mrec_lock); 868 } 869 return -ENOMEM; 870 } 871 search: 872 while ((err = ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, CASE_SENSITIVE, 873 0, NULL, 0, actx)) == 0) { 874 #ifdef DEBUG 875 unsigned char *s; 876 #endif 877 bool case_sensitive = IGNORE_CASE; 878 879 fn = (struct file_name_attr *)((u8 *)actx->attr + 880 le16_to_cpu(actx->attr->data.resident.value_offset)); 881 #ifdef DEBUG 882 s = ntfs_attr_name_get(ni->vol, fn->file_name, fn->file_name_length); 883 ntfs_debug("name: '%s' type: %d dos: %d win32: %d case: %d\n", 884 s, fn->file_name_type, 885 looking_for_dos_name, looking_for_win32_name, 886 case_sensitive_match); 887 ntfs_attr_name_free(&s); 888 #endif 889 if (looking_for_dos_name) { 890 if (fn->file_name_type == FILE_NAME_DOS) 891 break; 892 continue; 893 } 894 if (looking_for_win32_name) { 895 if (fn->file_name_type == FILE_NAME_WIN32) 896 break; 897 continue; 898 } 899 900 /* Ignore hard links from other directories */ 901 if (dir_ni->mft_no != MREF_LE(fn->parent_directory)) { 902 ntfs_debug("MFT record numbers don't match (%llu != %lu)\n", 903 dir_ni->mft_no, 904 MREF_LE(fn->parent_directory)); 905 continue; 906 } 907 908 if (fn->file_name_type == FILE_NAME_POSIX || case_sensitive_match) 909 case_sensitive = CASE_SENSITIVE; 910 911 if (ntfs_names_are_equal(fn->file_name, fn->file_name_length, 912 name, name_len, case_sensitive, 913 ni->vol->upcase, ni->vol->upcase_len)) { 914 if (fn->file_name_type == FILE_NAME_WIN32) { 915 looking_for_dos_name = true; 916 ntfs_attr_reinit_search_ctx(actx); 917 continue; 918 } 919 if (fn->file_name_type == FILE_NAME_DOS) 920 looking_for_dos_name = true; 921 break; 922 } 923 } 924 if (err) { 925 /* 926 * If case sensitive search failed, then try once again 927 * ignoring case. 928 */ 929 if (err == -ENOENT && case_sensitive_match) { 930 case_sensitive_match = false; 931 ntfs_attr_reinit_search_ctx(actx); 932 goto search; 933 } 934 goto err_out; 935 } 936 937 err = ntfs_check_unlinkable_dir(actx, fn); 938 if (err) 939 goto err_out; 940 941 err = ntfs_index_remove(dir_ni, fn, le32_to_cpu(actx->attr->data.resident.value_length)); 942 if (err) 943 goto err_out; 944 945 err = ntfs_attr_record_rm(actx); 946 if (err) 947 goto err_out; 948 949 ni_mrec = actx->base_mrec ? actx->base_mrec : actx->mrec; 950 ni_mrec->link_count = cpu_to_le16(le16_to_cpu(ni_mrec->link_count) - 1); 951 if (!S_ISDIR(VFS_I(ni)->i_mode)) 952 drop_nlink(VFS_I(ni)); 953 954 mark_mft_record_dirty(ni); 955 if (looking_for_dos_name) { 956 looking_for_dos_name = false; 957 looking_for_win32_name = true; 958 ntfs_attr_reinit_search_ctx(actx); 959 goto search; 960 } 961 962 /* 963 * For directories, Drop VFS nlink only when mft record link count 964 * becomes zero. Because we fixes VFS nlink to 1 for directories. 965 */ 966 if (S_ISDIR(VFS_I(ni)->i_mode) && !le16_to_cpu(ni_mrec->link_count)) 967 drop_nlink(VFS_I(ni)); 968 969 /* 970 * If hard link count is not equal to zero then we are done. In other 971 * case there are no reference to this inode left, so we should free all 972 * non-resident attributes and mark all MFT record as not in use. 973 */ 974 if (ni_mrec->link_count == 0) { 975 NInoSetBeingDeleted(ni); 976 ntfs_delete_reparse_index(ni); 977 ntfs_delete_object_id_index(ni); 978 link_count_zero = true; 979 } 980 981 ntfs_attr_put_search_ctx(actx); 982 if (need_lock == true) { 983 mutex_unlock(&dir_ni->mrec_lock); 984 mutex_unlock(&ni->mrec_lock); 985 } 986 987 /* 988 * If hard link count is not equal to zero then we are done. In other 989 * case there are no reference to this inode left, so we should free all 990 * non-resident attributes and mark all MFT record as not in use. 991 */ 992 if (link_count_zero == true) { 993 struct inode *attr_vi; 994 995 while ((attr_vi = ilookup5(sb, ni->mft_no, ntfs_test_inode_attr, 996 (void *)(uintptr_t)ni->mft_no)) != NULL) { 997 clear_nlink(attr_vi); 998 iput(attr_vi); 999 } 1000 } 1001 ntfs_debug("Done.\n"); 1002 return 0; 1003 err_out: 1004 ntfs_attr_put_search_ctx(actx); 1005 if (need_lock) { 1006 mutex_unlock(&dir_ni->mrec_lock); 1007 mutex_unlock(&ni->mrec_lock); 1008 } 1009 return err; 1010 } 1011 1012 static int ntfs_unlink(struct inode *dir, struct dentry *dentry) 1013 { 1014 struct inode *vi = dentry->d_inode; 1015 struct super_block *sb = dir->i_sb; 1016 struct ntfs_volume *vol = NTFS_SB(sb); 1017 int err = 0; 1018 struct ntfs_inode *ni = NTFS_I(vi); 1019 __le16 *uname = NULL; 1020 int uname_len; 1021 1022 if (NVolShutdown(vol)) 1023 return -EIO; 1024 1025 uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len, 1026 &uname, NTFS_MAX_NAME_LEN); 1027 if (uname_len < 0) { 1028 if (uname_len != -ENAMETOOLONG) 1029 ntfs_error(sb, "Failed to convert name to Unicode."); 1030 return -ENOMEM; 1031 } 1032 1033 err = ntfs_check_bad_windows_name(vol, uname, uname_len); 1034 if (err) { 1035 kmem_cache_free(ntfs_name_cache, uname); 1036 return err; 1037 } 1038 1039 if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 1040 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 1041 1042 err = ntfs_delete(ni, NTFS_I(dir), uname, uname_len, true); 1043 if (err) 1044 goto out; 1045 1046 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); 1047 mark_inode_dirty(dir); 1048 inode_set_ctime_to_ts(vi, inode_get_ctime(dir)); 1049 if (vi->i_nlink) 1050 mark_inode_dirty(vi); 1051 out: 1052 kmem_cache_free(ntfs_name_cache, uname); 1053 return err; 1054 } 1055 1056 static struct dentry *ntfs_mkdir(struct mnt_idmap *idmap, struct inode *dir, 1057 struct dentry *dentry, umode_t mode) 1058 { 1059 struct super_block *sb = dir->i_sb; 1060 struct ntfs_volume *vol = NTFS_SB(sb); 1061 int err = 0; 1062 struct ntfs_inode *ni; 1063 __le16 *uname; 1064 int uname_len; 1065 1066 if (NVolShutdown(vol)) 1067 return ERR_PTR(-EIO); 1068 1069 uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len, 1070 &uname, NTFS_MAX_NAME_LEN); 1071 if (uname_len < 0) { 1072 if (uname_len != -ENAMETOOLONG) 1073 ntfs_error(sb, "Failed to convert name to unicode."); 1074 return ERR_PTR(-ENOMEM); 1075 } 1076 1077 err = ntfs_check_bad_windows_name(vol, uname, uname_len); 1078 if (err) { 1079 kmem_cache_free(ntfs_name_cache, uname); 1080 return ERR_PTR(err); 1081 } 1082 1083 if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 1084 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 1085 1086 ni = __ntfs_create(idmap, dir, uname, uname_len, S_IFDIR | mode, 0, NULL, 0); 1087 kmem_cache_free(ntfs_name_cache, uname); 1088 if (IS_ERR(ni)) { 1089 err = PTR_ERR(ni); 1090 return ERR_PTR(err); 1091 } 1092 1093 d_instantiate_new(dentry, VFS_I(ni)); 1094 return NULL; 1095 } 1096 1097 static int ntfs_rmdir(struct inode *dir, struct dentry *dentry) 1098 { 1099 struct inode *vi = dentry->d_inode; 1100 struct super_block *sb = dir->i_sb; 1101 struct ntfs_volume *vol = NTFS_SB(sb); 1102 int err = 0; 1103 struct ntfs_inode *ni; 1104 __le16 *uname = NULL; 1105 int uname_len; 1106 1107 if (NVolShutdown(vol)) 1108 return -EIO; 1109 1110 ni = NTFS_I(vi); 1111 uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len, 1112 &uname, NTFS_MAX_NAME_LEN); 1113 if (uname_len < 0) { 1114 if (uname_len != -ENAMETOOLONG) 1115 ntfs_error(sb, "Failed to convert name to unicode."); 1116 return -ENOMEM; 1117 } 1118 1119 err = ntfs_check_bad_windows_name(vol, uname, uname_len); 1120 if (err) { 1121 kmem_cache_free(ntfs_name_cache, uname); 1122 return err; 1123 } 1124 1125 if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 1126 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 1127 1128 err = ntfs_delete(ni, NTFS_I(dir), uname, uname_len, true); 1129 if (err) 1130 goto out; 1131 1132 inode_set_mtime_to_ts(vi, inode_set_atime_to_ts(vi, current_time(vi))); 1133 out: 1134 kmem_cache_free(ntfs_name_cache, uname); 1135 return err; 1136 } 1137 1138 /* 1139 * __ntfs_link - create hard link for file or directory 1140 * @ni: ntfs inode for object to create hard link 1141 * @dir_ni: ntfs inode for directory in which new link should be placed 1142 * @name: unicode name of the new link 1143 * @name_len: length of the name in unicode characters 1144 * 1145 * Create a new hard link. This involves adding an entry to the directory 1146 * index and adding a new FILE_NAME attribute to the target inode. 1147 * 1148 * Return 0 on success and -errno on error. 1149 */ 1150 static int __ntfs_link(struct ntfs_inode *ni, struct ntfs_inode *dir_ni, 1151 __le16 *name, u8 name_len) 1152 { 1153 struct super_block *sb; 1154 struct inode *vi = VFS_I(ni); 1155 struct file_name_attr *fn = NULL; 1156 int fn_len, err = 0; 1157 struct mft_record *dir_mrec = NULL, *ni_mrec = NULL; 1158 1159 ntfs_debug("Entering.\n"); 1160 1161 sb = dir_ni->vol->sb; 1162 if (NInoBeingDeleted(dir_ni) || NInoBeingDeleted(ni)) 1163 return -ENOENT; 1164 1165 ni_mrec = map_mft_record(ni); 1166 if (IS_ERR(ni_mrec)) { 1167 err = -EIO; 1168 goto err_out; 1169 } 1170 1171 if (le16_to_cpu(ni_mrec->link_count) == 0) { 1172 err = -ENOENT; 1173 goto err_out; 1174 } 1175 1176 /* Create FILE_NAME attribute. */ 1177 fn_len = sizeof(struct file_name_attr) + name_len * sizeof(__le16); 1178 1179 fn = kzalloc(fn_len, GFP_NOFS); 1180 if (!fn) { 1181 err = -ENOMEM; 1182 goto err_out; 1183 } 1184 1185 dir_mrec = map_mft_record(dir_ni); 1186 if (IS_ERR(dir_mrec)) { 1187 err = -EIO; 1188 goto err_out; 1189 } 1190 1191 fn->parent_directory = MK_LE_MREF(dir_ni->mft_no, 1192 le16_to_cpu(dir_mrec->sequence_number)); 1193 unmap_mft_record(dir_ni); 1194 fn->file_name_length = name_len; 1195 fn->file_name_type = FILE_NAME_POSIX; 1196 fn->file_attributes = ni->flags; 1197 if (ni_mrec->flags & MFT_RECORD_IS_DIRECTORY) { 1198 fn->file_attributes |= FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT; 1199 fn->allocated_size = fn->data_size = 0; 1200 } else { 1201 if (NInoSparse(ni) || NInoCompressed(ni)) 1202 fn->allocated_size = 1203 cpu_to_le64(ni->itype.compressed.size); 1204 else 1205 fn->allocated_size = cpu_to_le64(ni->allocated_size); 1206 fn->data_size = cpu_to_le64(ni->data_size); 1207 } 1208 if (NVolHideDotFiles(dir_ni->vol) && name_len > 0 && name[0] == cpu_to_le16('.')) 1209 fn->file_attributes |= FILE_ATTR_HIDDEN; 1210 1211 fn->creation_time = utc2ntfs(ni->i_crtime); 1212 fn->last_data_change_time = utc2ntfs(inode_get_mtime(vi)); 1213 fn->last_mft_change_time = utc2ntfs(inode_get_ctime(vi)); 1214 fn->last_access_time = utc2ntfs(inode_get_atime(vi)); 1215 memcpy(fn->file_name, name, name_len * sizeof(__le16)); 1216 1217 /* Add FILE_NAME attribute to index. */ 1218 err = ntfs_index_add_filename(dir_ni, fn, MK_MREF(ni->mft_no, 1219 le16_to_cpu(ni_mrec->sequence_number))); 1220 if (err) { 1221 ntfs_error(sb, "Failed to add filename to the index"); 1222 goto err_out; 1223 } 1224 /* Add FILE_NAME attribute to inode. */ 1225 err = ntfs_attr_add(ni, AT_FILE_NAME, AT_UNNAMED, 0, (u8 *)fn, fn_len); 1226 if (err) { 1227 ntfs_error(sb, "Failed to add FILE_NAME attribute.\n"); 1228 /* Try to remove just added attribute from index. */ 1229 if (ntfs_index_remove(dir_ni, fn, fn_len)) 1230 goto rollback_failed; 1231 goto err_out; 1232 } 1233 /* Increment hard links count. */ 1234 ni_mrec->link_count = cpu_to_le16(le16_to_cpu(ni_mrec->link_count) + 1); 1235 if (!S_ISDIR(vi->i_mode)) 1236 inc_nlink(VFS_I(ni)); 1237 1238 /* Done! */ 1239 mark_mft_record_dirty(ni); 1240 kfree(fn); 1241 unmap_mft_record(ni); 1242 1243 ntfs_debug("Done.\n"); 1244 1245 return 0; 1246 rollback_failed: 1247 ntfs_error(sb, "Rollback failed. Leaving inconsistent metadata.\n"); 1248 err_out: 1249 kfree(fn); 1250 if (!IS_ERR_OR_NULL(ni_mrec)) 1251 unmap_mft_record(ni); 1252 return err; 1253 } 1254 1255 static int ntfs_rename(struct mnt_idmap *idmap, struct inode *old_dir, 1256 struct dentry *old_dentry, struct inode *new_dir, 1257 struct dentry *new_dentry, unsigned int flags) 1258 { 1259 struct inode *old_inode, *new_inode = NULL; 1260 int err = 0; 1261 int is_dir; 1262 struct super_block *sb = old_dir->i_sb; 1263 __le16 *uname_new = NULL; 1264 __le16 *uname_old = NULL; 1265 int new_name_len; 1266 int old_name_len; 1267 struct ntfs_volume *vol = NTFS_SB(sb); 1268 struct ntfs_inode *old_ni, *new_ni = NULL; 1269 struct ntfs_inode *old_dir_ni = NTFS_I(old_dir), *new_dir_ni = NTFS_I(new_dir); 1270 bool new_dir_first = false; 1271 1272 if (NVolShutdown(old_dir_ni->vol)) 1273 return -EIO; 1274 1275 if (flags & (RENAME_EXCHANGE | RENAME_WHITEOUT)) 1276 return -EINVAL; 1277 1278 new_name_len = ntfs_nlstoucs(NTFS_I(new_dir)->vol, new_dentry->d_name.name, 1279 new_dentry->d_name.len, &uname_new, 1280 NTFS_MAX_NAME_LEN); 1281 if (new_name_len < 0) { 1282 if (new_name_len != -ENAMETOOLONG) 1283 ntfs_error(sb, "Failed to convert name to unicode."); 1284 return -ENOMEM; 1285 } 1286 1287 err = ntfs_check_bad_windows_name(vol, uname_new, new_name_len); 1288 if (err) { 1289 kmem_cache_free(ntfs_name_cache, uname_new); 1290 return err; 1291 } 1292 1293 old_name_len = ntfs_nlstoucs(NTFS_I(old_dir)->vol, old_dentry->d_name.name, 1294 old_dentry->d_name.len, &uname_old, 1295 NTFS_MAX_NAME_LEN); 1296 if (old_name_len < 0) { 1297 kmem_cache_free(ntfs_name_cache, uname_new); 1298 if (old_name_len != -ENAMETOOLONG) 1299 ntfs_error(sb, "Failed to convert name to unicode."); 1300 return -ENOMEM; 1301 } 1302 1303 old_inode = old_dentry->d_inode; 1304 new_inode = new_dentry->d_inode; 1305 old_ni = NTFS_I(old_inode); 1306 if (new_inode) 1307 new_ni = NTFS_I(new_inode); 1308 if (old_dir != new_dir) 1309 new_dir_first = is_subdir(new_dentry->d_parent, 1310 old_dentry->d_parent); 1311 1312 if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 1313 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 1314 1315 mutex_lock_nested(&old_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 1316 if (new_ni) 1317 mutex_lock_nested(&new_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL_2); 1318 1319 if (old_dir == new_dir) { 1320 mutex_lock_nested(&old_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT); 1321 } else if (new_dir_first) { 1322 mutex_lock_nested(&new_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT); 1323 mutex_lock_nested(&old_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT_2); 1324 } else { 1325 mutex_lock_nested(&old_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT); 1326 mutex_lock_nested(&new_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT_2); 1327 } 1328 1329 if (NInoBeingDeleted(old_ni) || NInoBeingDeleted(old_dir_ni) || 1330 (new_ni && NInoBeingDeleted(new_ni)) || 1331 (old_dir != new_dir && NInoBeingDeleted(new_dir_ni))) { 1332 err = -ENOENT; 1333 goto err_out; 1334 } 1335 1336 is_dir = S_ISDIR(old_inode->i_mode); 1337 1338 if (new_inode) { 1339 if (is_dir) { 1340 struct mft_record *ni_mrec; 1341 1342 ni_mrec = map_mft_record(NTFS_I(new_inode)); 1343 if (IS_ERR(ni_mrec)) { 1344 err = -EIO; 1345 goto err_out; 1346 } 1347 err = ntfs_check_empty_dir(NTFS_I(new_inode), ni_mrec); 1348 unmap_mft_record(NTFS_I(new_inode)); 1349 if (err) 1350 goto err_out; 1351 } 1352 1353 err = ntfs_delete(new_ni, new_dir_ni, uname_new, new_name_len, false); 1354 if (err) 1355 goto err_out; 1356 } 1357 1358 err = __ntfs_link(old_ni, new_dir_ni, uname_new, new_name_len); 1359 if (err) 1360 goto err_out; 1361 1362 err = ntfs_delete(old_ni, old_dir_ni, uname_old, old_name_len, false); 1363 if (err) { 1364 int err2; 1365 1366 ntfs_error(sb, "Failed to delete old ntfs inode(%llu) in old dir, err : %d\n", 1367 old_ni->mft_no, err); 1368 err2 = ntfs_delete(old_ni, new_dir_ni, uname_new, new_name_len, false); 1369 if (err2) 1370 ntfs_error(sb, "Failed to delete old ntfs inode in new dir, err : %d\n", 1371 err2); 1372 goto err_out; 1373 } 1374 1375 simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry); 1376 mark_inode_dirty(old_inode); 1377 mark_inode_dirty(old_dir); 1378 if (old_dir != new_dir) 1379 mark_inode_dirty(new_dir); 1380 if (new_inode) 1381 mark_inode_dirty(old_inode); 1382 1383 inode_inc_iversion(new_dir); 1384 1385 err_out: 1386 if (old_dir == new_dir) { 1387 mutex_unlock(&old_dir_ni->mrec_lock); 1388 } else if (new_dir_first) { 1389 mutex_unlock(&old_dir_ni->mrec_lock); 1390 mutex_unlock(&new_dir_ni->mrec_lock); 1391 } else { 1392 mutex_unlock(&new_dir_ni->mrec_lock); 1393 mutex_unlock(&old_dir_ni->mrec_lock); 1394 } 1395 if (new_ni) 1396 mutex_unlock(&new_ni->mrec_lock); 1397 mutex_unlock(&old_ni->mrec_lock); 1398 if (uname_new) 1399 kmem_cache_free(ntfs_name_cache, uname_new); 1400 if (uname_old) 1401 kmem_cache_free(ntfs_name_cache, uname_old); 1402 1403 return err; 1404 } 1405 1406 static int ntfs_symlink(struct mnt_idmap *idmap, struct inode *dir, 1407 struct dentry *dentry, const char *symname) 1408 { 1409 struct super_block *sb = dir->i_sb; 1410 struct ntfs_volume *vol = NTFS_SB(sb); 1411 struct inode *vi; 1412 int err = 0; 1413 struct ntfs_inode *ni; 1414 __le16 *usrc; 1415 int usrc_len; 1416 int symlen = strlen(symname); 1417 1418 if (NVolShutdown(vol)) 1419 return -EIO; 1420 1421 usrc_len = ntfs_nlstoucs(vol, dentry->d_name.name, 1422 dentry->d_name.len, &usrc, NTFS_MAX_NAME_LEN); 1423 if (usrc_len < 0) { 1424 if (usrc_len != -ENAMETOOLONG) 1425 ntfs_error(sb, "Failed to convert name to Unicode."); 1426 err = -ENOMEM; 1427 goto out; 1428 } 1429 1430 err = ntfs_check_bad_windows_name(vol, usrc, usrc_len); 1431 if (err) { 1432 kmem_cache_free(ntfs_name_cache, usrc); 1433 goto out; 1434 } 1435 1436 if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 1437 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 1438 1439 ni = __ntfs_create(idmap, dir, usrc, usrc_len, S_IFLNK | 0777, 0, 1440 symname, symlen); 1441 kmem_cache_free(ntfs_name_cache, usrc); 1442 if (IS_ERR(ni)) { 1443 err = PTR_ERR(ni); 1444 goto out; 1445 } 1446 1447 vi = VFS_I(ni); 1448 vi->i_size = symlen; 1449 d_instantiate_new(dentry, vi); 1450 out: 1451 return err; 1452 } 1453 1454 static int ntfs_mknod(struct mnt_idmap *idmap, struct inode *dir, 1455 struct dentry *dentry, umode_t mode, dev_t rdev) 1456 { 1457 struct super_block *sb = dir->i_sb; 1458 struct ntfs_volume *vol = NTFS_SB(sb); 1459 int err = 0; 1460 struct ntfs_inode *ni; 1461 __le16 *uname = NULL; 1462 int uname_len; 1463 1464 if (NVolShutdown(vol)) 1465 return -EIO; 1466 1467 uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, 1468 dentry->d_name.len, &uname, NTFS_MAX_NAME_LEN); 1469 if (uname_len < 0) { 1470 if (uname_len != -ENAMETOOLONG) 1471 ntfs_error(sb, "Failed to convert name to Unicode."); 1472 return -ENOMEM; 1473 } 1474 1475 err = ntfs_check_bad_windows_name(vol, uname, uname_len); 1476 if (err) { 1477 kmem_cache_free(ntfs_name_cache, uname); 1478 return err; 1479 } 1480 1481 if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 1482 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 1483 1484 switch (mode & S_IFMT) { 1485 case S_IFCHR: 1486 case S_IFBLK: 1487 ni = __ntfs_create(idmap, dir, uname, uname_len, 1488 mode, rdev, NULL, 0); 1489 break; 1490 default: 1491 ni = __ntfs_create(idmap, dir, uname, uname_len, 1492 mode, 0, NULL, 0); 1493 } 1494 1495 kmem_cache_free(ntfs_name_cache, uname); 1496 if (IS_ERR(ni)) { 1497 err = PTR_ERR(ni); 1498 goto out; 1499 } 1500 1501 d_instantiate_new(dentry, VFS_I(ni)); 1502 out: 1503 return err; 1504 } 1505 1506 static int ntfs_link(struct dentry *old_dentry, struct inode *dir, 1507 struct dentry *dentry) 1508 { 1509 struct inode *vi = old_dentry->d_inode; 1510 struct super_block *sb = vi->i_sb; 1511 struct ntfs_volume *vol = NTFS_SB(sb); 1512 __le16 *uname = NULL; 1513 int uname_len; 1514 int err; 1515 struct ntfs_inode *ni = NTFS_I(vi), *dir_ni = NTFS_I(dir); 1516 1517 if (NVolShutdown(vol)) 1518 return -EIO; 1519 1520 uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, 1521 dentry->d_name.len, &uname, NTFS_MAX_NAME_LEN); 1522 if (uname_len < 0) { 1523 if (uname_len != -ENAMETOOLONG) 1524 ntfs_error(sb, "Failed to convert name to unicode."); 1525 return -ENOMEM; 1526 } 1527 1528 if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 1529 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 1530 1531 ihold(vi); 1532 mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 1533 mutex_lock_nested(&dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT); 1534 err = __ntfs_link(NTFS_I(vi), NTFS_I(dir), uname, uname_len); 1535 if (err) { 1536 mutex_unlock(&dir_ni->mrec_lock); 1537 mutex_unlock(&ni->mrec_lock); 1538 iput(vi); 1539 pr_err("failed to create link, err = %d\n", err); 1540 goto out; 1541 } 1542 1543 inode_inc_iversion(dir); 1544 simple_inode_init_ts(dir); 1545 1546 inode_inc_iversion(vi); 1547 simple_inode_init_ts(vi); 1548 1549 /* timestamp is already written, so mark_inode_dirty() is unneeded. */ 1550 d_instantiate(dentry, vi); 1551 mutex_unlock(&dir_ni->mrec_lock); 1552 mutex_unlock(&ni->mrec_lock); 1553 1554 out: 1555 kmem_cache_free(ntfs_name_cache, uname); 1556 return err; 1557 } 1558 1559 /* 1560 * Inode operations for directories. 1561 */ 1562 const struct inode_operations ntfs_dir_inode_ops = { 1563 .lookup = ntfs_lookup, /* VFS: Lookup directory. */ 1564 .create = ntfs_create, 1565 .unlink = ntfs_unlink, 1566 .mkdir = ntfs_mkdir, 1567 .rmdir = ntfs_rmdir, 1568 .rename = ntfs_rename, 1569 .get_acl = ntfs_get_acl, 1570 .set_acl = ntfs_set_acl, 1571 .listxattr = ntfs_listxattr, 1572 .setattr = ntfs_setattr, 1573 .getattr = ntfs_getattr, 1574 .symlink = ntfs_symlink, 1575 .mknod = ntfs_mknod, 1576 .link = ntfs_link, 1577 }; 1578 1579 /* 1580 * ntfs_get_parent - find the dentry of the parent of a given directory dentry 1581 * @child_dent: dentry of the directory whose parent directory to find 1582 * 1583 * Find the dentry for the parent directory of the directory specified by the 1584 * dentry @child_dent. This function is called from 1585 * fs/exportfs/expfs.c::find_exported_dentry() which in turn is called from the 1586 * default ->decode_fh() which is export_decode_fh() in the same file. 1587 * 1588 * Note: ntfs_get_parent() is called with @d_inode(child_dent)->i_mutex down. 1589 * 1590 * Return the dentry of the parent directory on success or the error code on 1591 * error (IS_ERR() is true). 1592 */ 1593 static struct dentry *ntfs_get_parent(struct dentry *child_dent) 1594 { 1595 struct inode *vi = d_inode(child_dent); 1596 struct ntfs_inode *ni = NTFS_I(vi); 1597 struct mft_record *mrec; 1598 struct ntfs_attr_search_ctx *ctx; 1599 struct attr_record *attr; 1600 struct file_name_attr *fn; 1601 unsigned long parent_ino; 1602 int err; 1603 1604 ntfs_debug("Entering for inode 0x%llx.", ni->mft_no); 1605 /* Get the mft record of the inode belonging to the child dentry. */ 1606 mrec = map_mft_record(ni); 1607 if (IS_ERR(mrec)) 1608 return ERR_CAST(mrec); 1609 /* Find the first file name attribute in the mft record. */ 1610 ctx = ntfs_attr_get_search_ctx(ni, mrec); 1611 if (unlikely(!ctx)) { 1612 unmap_mft_record(ni); 1613 return ERR_PTR(-ENOMEM); 1614 } 1615 try_next: 1616 err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, CASE_SENSITIVE, 0, NULL, 1617 0, ctx); 1618 if (unlikely(err)) { 1619 ntfs_attr_put_search_ctx(ctx); 1620 unmap_mft_record(ni); 1621 if (err == -ENOENT) 1622 ntfs_error(vi->i_sb, 1623 "Inode 0x%llx does not have a file name attribute. Run chkdsk.", 1624 ni->mft_no); 1625 return ERR_PTR(err); 1626 } 1627 attr = ctx->attr; 1628 if (unlikely(attr->non_resident)) 1629 goto try_next; 1630 fn = (struct file_name_attr *)((u8 *)attr + 1631 le16_to_cpu(attr->data.resident.value_offset)); 1632 if (unlikely((u8 *)fn + le32_to_cpu(attr->data.resident.value_length) > 1633 (u8 *)attr + le32_to_cpu(attr->length))) 1634 goto try_next; 1635 /* Get the inode number of the parent directory. */ 1636 parent_ino = MREF_LE(fn->parent_directory); 1637 /* Release the search context and the mft record of the child. */ 1638 ntfs_attr_put_search_ctx(ctx); 1639 unmap_mft_record(ni); 1640 1641 return d_obtain_alias(ntfs_iget(vi->i_sb, parent_ino)); 1642 } 1643 1644 static struct inode *ntfs_nfs_get_inode(struct super_block *sb, 1645 u64 ino, u32 generation) 1646 { 1647 struct inode *inode; 1648 1649 inode = ntfs_iget(sb, ino); 1650 if (!IS_ERR(inode)) { 1651 if (inode->i_generation != generation) { 1652 iput(inode); 1653 inode = ERR_PTR(-ESTALE); 1654 } 1655 } 1656 1657 return inode; 1658 } 1659 1660 static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid, 1661 int fh_len, int fh_type) 1662 { 1663 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, 1664 ntfs_nfs_get_inode); 1665 } 1666 1667 static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid, 1668 int fh_len, int fh_type) 1669 { 1670 return generic_fh_to_parent(sb, fid, fh_len, fh_type, 1671 ntfs_nfs_get_inode); 1672 } 1673 1674 /* 1675 * Export operations allowing NFS exporting of mounted NTFS partitions. 1676 */ 1677 const struct export_operations ntfs_export_ops = { 1678 .encode_fh = generic_encode_ino32_fh, 1679 .get_parent = ntfs_get_parent, /* Find the parent of a given directory. */ 1680 .fh_to_dentry = ntfs_fh_to_dentry, 1681 .fh_to_parent = ntfs_fh_to_parent, 1682 }; 1683