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