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