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
ntfs_check_bad_char(const __le16 * wc,unsigned int wc_len)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
ntfs_check_bad_windows_name(struct ntfs_volume * vol,const __le16 * wc,unsigned int wc_len)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 */
ntfs_lookup(struct inode * dir_ino,struct dentry * dent,unsigned int flags)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
ntfs_sd_add_everyone(struct ntfs_inode * ni)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
__ntfs_create(struct mnt_idmap * idmap,struct inode * dir,__le16 * name,u8 name_len,mode_t mode,dev_t dev,__le16 * target,int target_len)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
ntfs_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,bool excl)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
ntfs_check_unlinkable_dir(struct ntfs_attr_search_ctx * ctx,struct file_name_attr * fn)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
ntfs_test_inode_attr(struct inode * vi,void * data)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 */
ntfs_delete(struct ntfs_inode * ni,struct ntfs_inode * dir_ni,__le16 * name,u8 name_len,bool need_lock)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 if (!S_ISDIR(VFS_I(ni)->i_mode))
949 drop_nlink(VFS_I(ni));
950
951 mark_mft_record_dirty(ni);
952 if (looking_for_dos_name) {
953 looking_for_dos_name = false;
954 looking_for_win32_name = true;
955 ntfs_attr_reinit_search_ctx(actx);
956 goto search;
957 }
958
959 /*
960 * For directories, Drop VFS nlink only when mft record link count
961 * becomes zero. Because we fixes VFS nlink to 1 for directories.
962 */
963 if (S_ISDIR(VFS_I(ni)->i_mode) && !le16_to_cpu(ni_mrec->link_count))
964 drop_nlink(VFS_I(ni));
965
966 /*
967 * If hard link count is not equal to zero then we are done. In other
968 * case there are no reference to this inode left, so we should free all
969 * non-resident attributes and mark all MFT record as not in use.
970 */
971 if (ni_mrec->link_count == 0) {
972 NInoSetBeingDeleted(ni);
973 ntfs_delete_reparse_index(ni);
974 ntfs_delete_object_id_index(ni);
975 link_count_zero = true;
976 }
977
978 ntfs_attr_put_search_ctx(actx);
979 if (need_lock == true) {
980 mutex_unlock(&dir_ni->mrec_lock);
981 mutex_unlock(&ni->mrec_lock);
982 }
983
984 /*
985 * If hard link count is not equal to zero then we are done. In other
986 * case there are no reference to this inode left, so we should free all
987 * non-resident attributes and mark all MFT record as not in use.
988 */
989 if (link_count_zero == true) {
990 struct inode *attr_vi;
991
992 while ((attr_vi = ilookup5(sb, ni->mft_no, ntfs_test_inode_attr,
993 (void *)(uintptr_t)ni->mft_no)) != NULL) {
994 clear_nlink(attr_vi);
995 iput(attr_vi);
996 }
997 }
998 ntfs_debug("Done.\n");
999 return 0;
1000 err_out:
1001 ntfs_attr_put_search_ctx(actx);
1002 if (need_lock) {
1003 mutex_unlock(&dir_ni->mrec_lock);
1004 mutex_unlock(&ni->mrec_lock);
1005 }
1006 return err;
1007 }
1008
ntfs_unlink(struct inode * dir,struct dentry * dentry)1009 static int ntfs_unlink(struct inode *dir, struct dentry *dentry)
1010 {
1011 struct inode *vi = dentry->d_inode;
1012 struct super_block *sb = dir->i_sb;
1013 struct ntfs_volume *vol = NTFS_SB(sb);
1014 int err = 0;
1015 struct ntfs_inode *ni = NTFS_I(vi);
1016 __le16 *uname = NULL;
1017 int uname_len;
1018
1019 if (NVolShutdown(vol))
1020 return -EIO;
1021
1022 uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len,
1023 &uname, NTFS_MAX_NAME_LEN);
1024 if (uname_len < 0) {
1025 if (uname_len != -ENAMETOOLONG)
1026 ntfs_error(sb, "Failed to convert name to Unicode.");
1027 return -ENOMEM;
1028 }
1029
1030 err = ntfs_check_bad_windows_name(vol, uname, uname_len);
1031 if (err) {
1032 kmem_cache_free(ntfs_name_cache, uname);
1033 return err;
1034 }
1035
1036 if (!(vol->vol_flags & VOLUME_IS_DIRTY))
1037 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1038
1039 err = ntfs_delete(ni, NTFS_I(dir), uname, uname_len, true);
1040 if (err)
1041 goto out;
1042
1043 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
1044 mark_inode_dirty(dir);
1045 inode_set_ctime_to_ts(vi, inode_get_ctime(dir));
1046 if (vi->i_nlink)
1047 mark_inode_dirty(vi);
1048 out:
1049 kmem_cache_free(ntfs_name_cache, uname);
1050 return err;
1051 }
1052
ntfs_mkdir(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)1053 static struct dentry *ntfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
1054 struct dentry *dentry, umode_t mode)
1055 {
1056 struct super_block *sb = dir->i_sb;
1057 struct ntfs_volume *vol = NTFS_SB(sb);
1058 int err = 0;
1059 struct ntfs_inode *ni;
1060 __le16 *uname;
1061 int uname_len;
1062
1063 if (NVolShutdown(vol))
1064 return ERR_PTR(-EIO);
1065
1066 uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len,
1067 &uname, NTFS_MAX_NAME_LEN);
1068 if (uname_len < 0) {
1069 if (uname_len != -ENAMETOOLONG)
1070 ntfs_error(sb, "Failed to convert name to unicode.");
1071 return ERR_PTR(-ENOMEM);
1072 }
1073
1074 err = ntfs_check_bad_windows_name(vol, uname, uname_len);
1075 if (err) {
1076 kmem_cache_free(ntfs_name_cache, uname);
1077 return ERR_PTR(err);
1078 }
1079
1080 if (!(vol->vol_flags & VOLUME_IS_DIRTY))
1081 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1082
1083 ni = __ntfs_create(idmap, dir, uname, uname_len, S_IFDIR | mode, 0, NULL, 0);
1084 kmem_cache_free(ntfs_name_cache, uname);
1085 if (IS_ERR(ni)) {
1086 err = PTR_ERR(ni);
1087 return ERR_PTR(err);
1088 }
1089
1090 d_instantiate_new(dentry, VFS_I(ni));
1091 return NULL;
1092 }
1093
ntfs_rmdir(struct inode * dir,struct dentry * dentry)1094 static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)
1095 {
1096 struct inode *vi = dentry->d_inode;
1097 struct super_block *sb = dir->i_sb;
1098 struct ntfs_volume *vol = NTFS_SB(sb);
1099 int err = 0;
1100 struct ntfs_inode *ni;
1101 __le16 *uname = NULL;
1102 int uname_len;
1103
1104 if (NVolShutdown(vol))
1105 return -EIO;
1106
1107 ni = NTFS_I(vi);
1108 uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len,
1109 &uname, NTFS_MAX_NAME_LEN);
1110 if (uname_len < 0) {
1111 if (uname_len != -ENAMETOOLONG)
1112 ntfs_error(sb, "Failed to convert name to unicode.");
1113 return -ENOMEM;
1114 }
1115
1116 err = ntfs_check_bad_windows_name(vol, uname, uname_len);
1117 if (err) {
1118 kmem_cache_free(ntfs_name_cache, uname);
1119 return err;
1120 }
1121
1122 if (!(vol->vol_flags & VOLUME_IS_DIRTY))
1123 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1124
1125 err = ntfs_delete(ni, NTFS_I(dir), uname, uname_len, true);
1126 if (err)
1127 goto out;
1128
1129 inode_set_mtime_to_ts(vi, inode_set_atime_to_ts(vi, current_time(vi)));
1130 out:
1131 kmem_cache_free(ntfs_name_cache, uname);
1132 return err;
1133 }
1134
1135 /*
1136 * __ntfs_link - create hard link for file or directory
1137 * @ni: ntfs inode for object to create hard link
1138 * @dir_ni: ntfs inode for directory in which new link should be placed
1139 * @name: unicode name of the new link
1140 * @name_len: length of the name in unicode characters
1141 *
1142 * Create a new hard link. This involves adding an entry to the directory
1143 * index and adding a new FILE_NAME attribute to the target inode.
1144 *
1145 * Return 0 on success and -errno on error.
1146 */
__ntfs_link(struct ntfs_inode * ni,struct ntfs_inode * dir_ni,__le16 * name,u8 name_len)1147 static int __ntfs_link(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,
1148 __le16 *name, u8 name_len)
1149 {
1150 struct super_block *sb;
1151 struct inode *vi = VFS_I(ni);
1152 struct file_name_attr *fn = NULL;
1153 int fn_len, err = 0;
1154 struct mft_record *dir_mrec = NULL, *ni_mrec = NULL;
1155
1156 ntfs_debug("Entering.\n");
1157
1158 sb = dir_ni->vol->sb;
1159 if (NInoBeingDeleted(dir_ni) || NInoBeingDeleted(ni))
1160 return -ENOENT;
1161
1162 ni_mrec = map_mft_record(ni);
1163 if (IS_ERR(ni_mrec)) {
1164 err = -EIO;
1165 goto err_out;
1166 }
1167
1168 if (le16_to_cpu(ni_mrec->link_count) == 0) {
1169 err = -ENOENT;
1170 goto err_out;
1171 }
1172
1173 /* Create FILE_NAME attribute. */
1174 fn_len = sizeof(struct file_name_attr) + name_len * sizeof(__le16);
1175
1176 fn = kzalloc(fn_len, GFP_NOFS);
1177 if (!fn) {
1178 err = -ENOMEM;
1179 goto err_out;
1180 }
1181
1182 dir_mrec = map_mft_record(dir_ni);
1183 if (IS_ERR(dir_mrec)) {
1184 err = -EIO;
1185 goto err_out;
1186 }
1187
1188 fn->parent_directory = MK_LE_MREF(dir_ni->mft_no,
1189 le16_to_cpu(dir_mrec->sequence_number));
1190 unmap_mft_record(dir_ni);
1191 fn->file_name_length = name_len;
1192 fn->file_name_type = FILE_NAME_POSIX;
1193 fn->file_attributes = ni->flags;
1194 if (ni_mrec->flags & MFT_RECORD_IS_DIRECTORY) {
1195 fn->file_attributes |= FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT;
1196 fn->allocated_size = fn->data_size = 0;
1197 } else {
1198 if (NInoSparse(ni) || NInoCompressed(ni))
1199 fn->allocated_size =
1200 cpu_to_le64(ni->itype.compressed.size);
1201 else
1202 fn->allocated_size = cpu_to_le64(ni->allocated_size);
1203 fn->data_size = cpu_to_le64(ni->data_size);
1204 }
1205 if (NVolHideDotFiles(dir_ni->vol) && name_len > 0 && name[0] == cpu_to_le16('.'))
1206 fn->file_attributes |= FILE_ATTR_HIDDEN;
1207
1208 fn->creation_time = utc2ntfs(ni->i_crtime);
1209 fn->last_data_change_time = utc2ntfs(inode_get_mtime(vi));
1210 fn->last_mft_change_time = utc2ntfs(inode_get_ctime(vi));
1211 fn->last_access_time = utc2ntfs(inode_get_atime(vi));
1212 memcpy(fn->file_name, name, name_len * sizeof(__le16));
1213
1214 /* Add FILE_NAME attribute to index. */
1215 err = ntfs_index_add_filename(dir_ni, fn, MK_MREF(ni->mft_no,
1216 le16_to_cpu(ni_mrec->sequence_number)));
1217 if (err) {
1218 ntfs_error(sb, "Failed to add filename to the index");
1219 goto err_out;
1220 }
1221 /* Add FILE_NAME attribute to inode. */
1222 err = ntfs_attr_add(ni, AT_FILE_NAME, AT_UNNAMED, 0, (u8 *)fn, fn_len);
1223 if (err) {
1224 ntfs_error(sb, "Failed to add FILE_NAME attribute.\n");
1225 /* Try to remove just added attribute from index. */
1226 if (ntfs_index_remove(dir_ni, fn, fn_len))
1227 goto rollback_failed;
1228 goto err_out;
1229 }
1230 /* Increment hard links count. */
1231 ni_mrec->link_count = cpu_to_le16(le16_to_cpu(ni_mrec->link_count) + 1);
1232 if (!S_ISDIR(vi->i_mode))
1233 inc_nlink(VFS_I(ni));
1234
1235 /* Done! */
1236 mark_mft_record_dirty(ni);
1237 kfree(fn);
1238 unmap_mft_record(ni);
1239
1240 ntfs_debug("Done.\n");
1241
1242 return 0;
1243 rollback_failed:
1244 ntfs_error(sb, "Rollback failed. Leaving inconsistent metadata.\n");
1245 err_out:
1246 kfree(fn);
1247 if (!IS_ERR_OR_NULL(ni_mrec))
1248 unmap_mft_record(ni);
1249 return err;
1250 }
1251
ntfs_rename(struct mnt_idmap * idmap,struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)1252 static int ntfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
1253 struct dentry *old_dentry, struct inode *new_dir,
1254 struct dentry *new_dentry, unsigned int flags)
1255 {
1256 struct inode *old_inode, *new_inode = NULL;
1257 int err = 0;
1258 int is_dir;
1259 struct super_block *sb = old_dir->i_sb;
1260 __le16 *uname_new = NULL;
1261 __le16 *uname_old = NULL;
1262 int new_name_len;
1263 int old_name_len;
1264 struct ntfs_volume *vol = NTFS_SB(sb);
1265 struct ntfs_inode *old_ni, *new_ni = NULL;
1266 struct ntfs_inode *old_dir_ni = NTFS_I(old_dir), *new_dir_ni = NTFS_I(new_dir);
1267
1268 if (NVolShutdown(old_dir_ni->vol))
1269 return -EIO;
1270
1271 if (flags & (RENAME_EXCHANGE | RENAME_WHITEOUT))
1272 return -EINVAL;
1273
1274 new_name_len = ntfs_nlstoucs(NTFS_I(new_dir)->vol, new_dentry->d_name.name,
1275 new_dentry->d_name.len, &uname_new,
1276 NTFS_MAX_NAME_LEN);
1277 if (new_name_len < 0) {
1278 if (new_name_len != -ENAMETOOLONG)
1279 ntfs_error(sb, "Failed to convert name to unicode.");
1280 return -ENOMEM;
1281 }
1282
1283 err = ntfs_check_bad_windows_name(vol, uname_new, new_name_len);
1284 if (err) {
1285 kmem_cache_free(ntfs_name_cache, uname_new);
1286 return err;
1287 }
1288
1289 old_name_len = ntfs_nlstoucs(NTFS_I(old_dir)->vol, old_dentry->d_name.name,
1290 old_dentry->d_name.len, &uname_old,
1291 NTFS_MAX_NAME_LEN);
1292 if (old_name_len < 0) {
1293 kmem_cache_free(ntfs_name_cache, uname_new);
1294 if (old_name_len != -ENAMETOOLONG)
1295 ntfs_error(sb, "Failed to convert name to unicode.");
1296 return -ENOMEM;
1297 }
1298
1299 old_inode = old_dentry->d_inode;
1300 new_inode = new_dentry->d_inode;
1301 old_ni = NTFS_I(old_inode);
1302
1303 if (!(vol->vol_flags & VOLUME_IS_DIRTY))
1304 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1305
1306 mutex_lock_nested(&old_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
1307 mutex_lock_nested(&old_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
1308
1309 if (NInoBeingDeleted(old_ni) || NInoBeingDeleted(old_dir_ni)) {
1310 err = -ENOENT;
1311 goto unlock_old;
1312 }
1313
1314 is_dir = S_ISDIR(old_inode->i_mode);
1315
1316 if (new_inode) {
1317 new_ni = NTFS_I(new_inode);
1318 mutex_lock_nested(&new_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL_2);
1319 if (old_dir != new_dir) {
1320 mutex_lock_nested(&new_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT_2);
1321 if (NInoBeingDeleted(new_dir_ni)) {
1322 err = -ENOENT;
1323 goto err_out;
1324 }
1325 }
1326
1327 if (NInoBeingDeleted(new_ni)) {
1328 err = -ENOENT;
1329 goto err_out;
1330 }
1331
1332 if (is_dir) {
1333 struct mft_record *ni_mrec;
1334
1335 ni_mrec = map_mft_record(NTFS_I(new_inode));
1336 if (IS_ERR(ni_mrec)) {
1337 err = -EIO;
1338 goto err_out;
1339 }
1340 err = ntfs_check_empty_dir(NTFS_I(new_inode), ni_mrec);
1341 unmap_mft_record(NTFS_I(new_inode));
1342 if (err)
1343 goto err_out;
1344 }
1345
1346 err = ntfs_delete(new_ni, new_dir_ni, uname_new, new_name_len, false);
1347 if (err)
1348 goto err_out;
1349 } else {
1350 if (old_dir != new_dir) {
1351 mutex_lock_nested(&new_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT_2);
1352 if (NInoBeingDeleted(new_dir_ni)) {
1353 err = -ENOENT;
1354 goto err_out;
1355 }
1356 }
1357 }
1358
1359 err = __ntfs_link(old_ni, new_dir_ni, uname_new, new_name_len);
1360 if (err)
1361 goto err_out;
1362
1363 err = ntfs_delete(old_ni, old_dir_ni, uname_old, old_name_len, false);
1364 if (err) {
1365 int err2;
1366
1367 ntfs_error(sb, "Failed to delete old ntfs inode(%llu) in old dir, err : %d\n",
1368 old_ni->mft_no, err);
1369 err2 = ntfs_delete(old_ni, new_dir_ni, uname_new, new_name_len, false);
1370 if (err2)
1371 ntfs_error(sb, "Failed to delete old ntfs inode in new dir, err : %d\n",
1372 err2);
1373 goto err_out;
1374 }
1375
1376 simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry);
1377 mark_inode_dirty(old_inode);
1378 mark_inode_dirty(old_dir);
1379 if (old_dir != new_dir)
1380 mark_inode_dirty(new_dir);
1381 if (new_inode)
1382 mark_inode_dirty(old_inode);
1383
1384 inode_inc_iversion(new_dir);
1385
1386 err_out:
1387 if (old_dir != new_dir)
1388 mutex_unlock(&new_dir_ni->mrec_lock);
1389 if (new_inode)
1390 mutex_unlock(&new_ni->mrec_lock);
1391
1392 unlock_old:
1393 mutex_unlock(&old_dir_ni->mrec_lock);
1394 mutex_unlock(&old_ni->mrec_lock);
1395 if (uname_new)
1396 kmem_cache_free(ntfs_name_cache, uname_new);
1397 if (uname_old)
1398 kmem_cache_free(ntfs_name_cache, uname_old);
1399
1400 return err;
1401 }
1402
ntfs_symlink(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,const char * symname)1403 static int ntfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
1404 struct dentry *dentry, const char *symname)
1405 {
1406 struct super_block *sb = dir->i_sb;
1407 struct ntfs_volume *vol = NTFS_SB(sb);
1408 struct inode *vi;
1409 int err = 0;
1410 struct ntfs_inode *ni;
1411 __le16 *usrc;
1412 __le16 *utarget;
1413 int usrc_len;
1414 int utarget_len;
1415 int symlen = strlen(symname);
1416
1417 if (NVolShutdown(vol))
1418 return -EIO;
1419
1420 usrc_len = ntfs_nlstoucs(vol, dentry->d_name.name,
1421 dentry->d_name.len, &usrc, NTFS_MAX_NAME_LEN);
1422 if (usrc_len < 0) {
1423 if (usrc_len != -ENAMETOOLONG)
1424 ntfs_error(sb, "Failed to convert name to Unicode.");
1425 err = -ENOMEM;
1426 goto out;
1427 }
1428
1429 err = ntfs_check_bad_windows_name(vol, usrc, usrc_len);
1430 if (err) {
1431 kmem_cache_free(ntfs_name_cache, usrc);
1432 goto out;
1433 }
1434
1435 utarget_len = ntfs_nlstoucs(vol, symname, symlen, &utarget,
1436 PATH_MAX);
1437 if (utarget_len < 0) {
1438 if (utarget_len != -ENAMETOOLONG)
1439 ntfs_error(sb, "Failed to convert target name to Unicode.");
1440 err = -ENOMEM;
1441 kmem_cache_free(ntfs_name_cache, usrc);
1442 goto out;
1443 }
1444
1445 if (!(vol->vol_flags & VOLUME_IS_DIRTY))
1446 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1447
1448 ni = __ntfs_create(idmap, dir, usrc, usrc_len, S_IFLNK | 0777, 0,
1449 utarget, utarget_len);
1450 kmem_cache_free(ntfs_name_cache, usrc);
1451 kvfree(utarget);
1452 if (IS_ERR(ni)) {
1453 err = PTR_ERR(ni);
1454 goto out;
1455 }
1456
1457 vi = VFS_I(ni);
1458 vi->i_size = symlen;
1459 d_instantiate_new(dentry, vi);
1460 out:
1461 return err;
1462 }
1463
ntfs_mknod(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev)1464 static int ntfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
1465 struct dentry *dentry, umode_t mode, dev_t rdev)
1466 {
1467 struct super_block *sb = dir->i_sb;
1468 struct ntfs_volume *vol = NTFS_SB(sb);
1469 int err = 0;
1470 struct ntfs_inode *ni;
1471 __le16 *uname = NULL;
1472 int uname_len;
1473
1474 if (NVolShutdown(vol))
1475 return -EIO;
1476
1477 uname_len = ntfs_nlstoucs(vol, dentry->d_name.name,
1478 dentry->d_name.len, &uname, NTFS_MAX_NAME_LEN);
1479 if (uname_len < 0) {
1480 if (uname_len != -ENAMETOOLONG)
1481 ntfs_error(sb, "Failed to convert name to Unicode.");
1482 return -ENOMEM;
1483 }
1484
1485 err = ntfs_check_bad_windows_name(vol, uname, uname_len);
1486 if (err) {
1487 kmem_cache_free(ntfs_name_cache, uname);
1488 return err;
1489 }
1490
1491 if (!(vol->vol_flags & VOLUME_IS_DIRTY))
1492 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1493
1494 switch (mode & S_IFMT) {
1495 case S_IFCHR:
1496 case S_IFBLK:
1497 ni = __ntfs_create(idmap, dir, uname, uname_len,
1498 mode, rdev, NULL, 0);
1499 break;
1500 default:
1501 ni = __ntfs_create(idmap, dir, uname, uname_len,
1502 mode, 0, NULL, 0);
1503 }
1504
1505 kmem_cache_free(ntfs_name_cache, uname);
1506 if (IS_ERR(ni)) {
1507 err = PTR_ERR(ni);
1508 goto out;
1509 }
1510
1511 d_instantiate_new(dentry, VFS_I(ni));
1512 out:
1513 return err;
1514 }
1515
ntfs_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)1516 static int ntfs_link(struct dentry *old_dentry, struct inode *dir,
1517 struct dentry *dentry)
1518 {
1519 struct inode *vi = old_dentry->d_inode;
1520 struct super_block *sb = vi->i_sb;
1521 struct ntfs_volume *vol = NTFS_SB(sb);
1522 __le16 *uname = NULL;
1523 int uname_len;
1524 int err;
1525 struct ntfs_inode *ni = NTFS_I(vi), *dir_ni = NTFS_I(dir);
1526
1527 if (NVolShutdown(vol))
1528 return -EIO;
1529
1530 uname_len = ntfs_nlstoucs(vol, dentry->d_name.name,
1531 dentry->d_name.len, &uname, NTFS_MAX_NAME_LEN);
1532 if (uname_len < 0) {
1533 if (uname_len != -ENAMETOOLONG)
1534 ntfs_error(sb, "Failed to convert name to unicode.");
1535 err = -ENOMEM;
1536 goto out;
1537 }
1538
1539 if (!(vol->vol_flags & VOLUME_IS_DIRTY))
1540 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1541
1542 ihold(vi);
1543 mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
1544 mutex_lock_nested(&dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
1545 err = __ntfs_link(NTFS_I(vi), NTFS_I(dir), uname, uname_len);
1546 if (err) {
1547 mutex_unlock(&dir_ni->mrec_lock);
1548 mutex_unlock(&ni->mrec_lock);
1549 iput(vi);
1550 pr_err("failed to create link, err = %d\n", err);
1551 goto out;
1552 }
1553
1554 inode_inc_iversion(dir);
1555 simple_inode_init_ts(dir);
1556
1557 inode_inc_iversion(vi);
1558 simple_inode_init_ts(vi);
1559
1560 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
1561 d_instantiate(dentry, vi);
1562 mutex_unlock(&dir_ni->mrec_lock);
1563 mutex_unlock(&ni->mrec_lock);
1564
1565 out:
1566 kfree(uname);
1567 return err;
1568 }
1569
1570 /*
1571 * Inode operations for directories.
1572 */
1573 const struct inode_operations ntfs_dir_inode_ops = {
1574 .lookup = ntfs_lookup, /* VFS: Lookup directory. */
1575 .create = ntfs_create,
1576 .unlink = ntfs_unlink,
1577 .mkdir = ntfs_mkdir,
1578 .rmdir = ntfs_rmdir,
1579 .rename = ntfs_rename,
1580 .get_acl = ntfs_get_acl,
1581 .set_acl = ntfs_set_acl,
1582 .listxattr = ntfs_listxattr,
1583 .setattr = ntfs_setattr,
1584 .getattr = ntfs_getattr,
1585 .symlink = ntfs_symlink,
1586 .mknod = ntfs_mknod,
1587 .link = ntfs_link,
1588 };
1589
1590 /*
1591 * ntfs_get_parent - find the dentry of the parent of a given directory dentry
1592 * @child_dent: dentry of the directory whose parent directory to find
1593 *
1594 * Find the dentry for the parent directory of the directory specified by the
1595 * dentry @child_dent. This function is called from
1596 * fs/exportfs/expfs.c::find_exported_dentry() which in turn is called from the
1597 * default ->decode_fh() which is export_decode_fh() in the same file.
1598 *
1599 * Note: ntfs_get_parent() is called with @d_inode(child_dent)->i_mutex down.
1600 *
1601 * Return the dentry of the parent directory on success or the error code on
1602 * error (IS_ERR() is true).
1603 */
ntfs_get_parent(struct dentry * child_dent)1604 static struct dentry *ntfs_get_parent(struct dentry *child_dent)
1605 {
1606 struct inode *vi = d_inode(child_dent);
1607 struct ntfs_inode *ni = NTFS_I(vi);
1608 struct mft_record *mrec;
1609 struct ntfs_attr_search_ctx *ctx;
1610 struct attr_record *attr;
1611 struct file_name_attr *fn;
1612 unsigned long parent_ino;
1613 int err;
1614
1615 ntfs_debug("Entering for inode 0x%llx.", ni->mft_no);
1616 /* Get the mft record of the inode belonging to the child dentry. */
1617 mrec = map_mft_record(ni);
1618 if (IS_ERR(mrec))
1619 return ERR_CAST(mrec);
1620 /* Find the first file name attribute in the mft record. */
1621 ctx = ntfs_attr_get_search_ctx(ni, mrec);
1622 if (unlikely(!ctx)) {
1623 unmap_mft_record(ni);
1624 return ERR_PTR(-ENOMEM);
1625 }
1626 try_next:
1627 err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, CASE_SENSITIVE, 0, NULL,
1628 0, ctx);
1629 if (unlikely(err)) {
1630 ntfs_attr_put_search_ctx(ctx);
1631 unmap_mft_record(ni);
1632 if (err == -ENOENT)
1633 ntfs_error(vi->i_sb,
1634 "Inode 0x%llx does not have a file name attribute. Run chkdsk.",
1635 ni->mft_no);
1636 return ERR_PTR(err);
1637 }
1638 attr = ctx->attr;
1639 if (unlikely(attr->non_resident))
1640 goto try_next;
1641 fn = (struct file_name_attr *)((u8 *)attr +
1642 le16_to_cpu(attr->data.resident.value_offset));
1643 if (unlikely((u8 *)fn + le32_to_cpu(attr->data.resident.value_length) >
1644 (u8 *)attr + le32_to_cpu(attr->length)))
1645 goto try_next;
1646 /* Get the inode number of the parent directory. */
1647 parent_ino = MREF_LE(fn->parent_directory);
1648 /* Release the search context and the mft record of the child. */
1649 ntfs_attr_put_search_ctx(ctx);
1650 unmap_mft_record(ni);
1651
1652 return d_obtain_alias(ntfs_iget(vi->i_sb, parent_ino));
1653 }
1654
ntfs_nfs_get_inode(struct super_block * sb,u64 ino,u32 generation)1655 static struct inode *ntfs_nfs_get_inode(struct super_block *sb,
1656 u64 ino, u32 generation)
1657 {
1658 struct inode *inode;
1659
1660 inode = ntfs_iget(sb, ino);
1661 if (!IS_ERR(inode)) {
1662 if (inode->i_generation != generation) {
1663 iput(inode);
1664 inode = ERR_PTR(-ESTALE);
1665 }
1666 }
1667
1668 return inode;
1669 }
1670
ntfs_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)1671 static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1672 int fh_len, int fh_type)
1673 {
1674 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1675 ntfs_nfs_get_inode);
1676 }
1677
ntfs_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)1678 static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
1679 int fh_len, int fh_type)
1680 {
1681 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1682 ntfs_nfs_get_inode);
1683 }
1684
1685 /*
1686 * Export operations allowing NFS exporting of mounted NTFS partitions.
1687 */
1688 const struct export_operations ntfs_export_ops = {
1689 .encode_fh = generic_encode_ino32_fh,
1690 .get_parent = ntfs_get_parent, /* Find the parent of a given directory. */
1691 .fh_to_dentry = ntfs_fh_to_dentry,
1692 .fh_to_parent = ntfs_fh_to_parent,
1693 };
1694