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 (!NVolCheckWindowsNames(vol))
65 return 0;
66
67 if (ntfs_check_bad_char(wc, wc_len))
68 return -EINVAL;
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 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
ntfs_sd_add_everyone(struct ntfs_inode * ni)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
__ntfs_create(struct mnt_idmap * idmap,struct inode * dir,__le16 * name,u8 name_len,mode_t mode,dev_t dev,const char * target,int target_len)394 static struct ntfs_inode *__ntfs_create(struct mnt_idmap *idmap, struct inode *dir,
395 __le16 *name, u8 name_len, mode_t mode, dev_t dev,
396 const char *target, int target_len)
397 {
398 struct ntfs_inode *dir_ni = NTFS_I(dir);
399 struct ntfs_volume *vol = dir_ni->vol;
400 struct ntfs_inode *ni;
401 bool rollback_data = false, rollback_sd = false, rollback_reparse = false;
402 struct file_name_attr *fn = NULL;
403 struct standard_information *si = NULL;
404 int err = 0, fn_len, si_len;
405 struct inode *vi;
406 struct mft_record *ni_mrec, *dni_mrec;
407 struct super_block *sb = dir_ni->vol->sb;
408 __le64 parent_mft_ref;
409 u64 child_mft_ref;
410 __le16 ea_size;
411
412 vi = new_inode(vol->sb);
413 if (!vi)
414 return ERR_PTR(-ENOMEM);
415
416 ntfs_init_big_inode(vi);
417 ni = NTFS_I(vi);
418 ni->vol = dir_ni->vol;
419 ni->name_len = 0;
420 ni->name = NULL;
421
422 /*
423 * Set the appropriate mode, attribute type, and name. For
424 * directories, also setup the index values to the defaults.
425 */
426 if (S_ISDIR(mode)) {
427 NInoSetMstProtected(ni);
428 ni->itype.index.block_size = 4096;
429 ni->itype.index.block_size_bits = ntfs_ffs(4096) - 1;
430 ni->itype.index.collation_rule = COLLATION_FILE_NAME;
431 if (vol->cluster_size <= ni->itype.index.block_size) {
432 ni->itype.index.vcn_size = vol->cluster_size;
433 ni->itype.index.vcn_size_bits =
434 vol->cluster_size_bits;
435 } else {
436 ni->itype.index.vcn_size = vol->sector_size;
437 ni->itype.index.vcn_size_bits =
438 vol->sector_size_bits;
439 }
440 }
441
442 if (IS_RDONLY(vi))
443 mode &= ~0222;
444
445 inode_init_owner(idmap, vi, dir, mode);
446
447 mode = vi->i_mode;
448
449 #ifdef CONFIG_NTFS_FS_POSIX_ACL
450 if (!S_ISLNK(mode) && (sb->s_flags & SB_POSIXACL)) {
451 err = ntfs_init_acl(idmap, vi, dir);
452 if (err)
453 goto err_out;
454 } else
455 #endif
456 {
457 vi->i_flags |= S_NOSEC;
458 }
459
460 if (uid_valid(vol->uid))
461 vi->i_uid = vol->uid;
462
463 if (gid_valid(vol->gid))
464 vi->i_gid = vol->gid;
465
466 /*
467 * Set the file size to 0, the ntfs inode sizes are set to 0 by
468 * the call to ntfs_init_big_inode() below.
469 */
470 vi->i_size = 0;
471 vi->i_blocks = 0;
472
473 inode_inc_iversion(vi);
474
475 simple_inode_init_ts(vi);
476 ni->i_crtime = inode_get_ctime(vi);
477
478 inode_set_mtime_to_ts(dir, ni->i_crtime);
479 inode_set_ctime_to_ts(dir, ni->i_crtime);
480 mark_inode_dirty(dir);
481
482 err = ntfs_mft_record_alloc(dir_ni->vol, mode, &ni, NULL,
483 &ni_mrec, -1);
484 if (err) {
485 iput(vi);
486 return ERR_PTR(err);
487 }
488
489 /*
490 * Prevent iget and writeback from finding this inode.
491 * Caller must call d_instantiate_new instead of d_instantiate.
492 */
493 spin_lock(&vi->i_lock);
494 inode_state_set(vi, I_NEW | I_CREATING);
495 spin_unlock(&vi->i_lock);
496
497 /* Add the inode to the inode hash for the superblock. */
498 vi->i_ino = (unsigned long)ni->mft_no;
499 inode_set_iversion(vi, 1);
500 insert_inode_hash(vi);
501
502 mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
503 mutex_lock_nested(&dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
504 if (NInoBeingDeleted(dir_ni)) {
505 err = -ENOENT;
506 goto err_out;
507 }
508
509 dni_mrec = map_mft_record(dir_ni);
510 if (IS_ERR(dni_mrec)) {
511 ntfs_error(dir_ni->vol->sb, "failed to map mft record for file 0x%llx.\n",
512 dir_ni->mft_no);
513 err = -EIO;
514 goto err_out;
515 }
516 parent_mft_ref = MK_LE_MREF(dir_ni->mft_no,
517 le16_to_cpu(dni_mrec->sequence_number));
518 unmap_mft_record(dir_ni);
519
520 /*
521 * Create STANDARD_INFORMATION attribute. Write STANDARD_INFORMATION
522 * version 1.2, windows will upgrade it to version 3 if needed.
523 */
524 si_len = offsetof(struct standard_information, file_attributes) +
525 sizeof(__le32) + 12;
526 si = kzalloc(si_len, GFP_NOFS);
527 if (!si) {
528 err = -ENOMEM;
529 goto err_out;
530 }
531
532 si->creation_time = si->last_data_change_time = utc2ntfs(ni->i_crtime);
533 si->last_mft_change_time = si->last_access_time = si->creation_time;
534
535 if (!S_ISREG(mode) && !S_ISDIR(mode))
536 si->file_attributes = FILE_ATTR_SYSTEM;
537
538 /* Add STANDARD_INFORMATION to inode. */
539 err = ntfs_attr_add(ni, AT_STANDARD_INFORMATION, AT_UNNAMED, 0, (u8 *)si,
540 si_len);
541 if (err) {
542 ntfs_error(sb, "Failed to add STANDARD_INFORMATION attribute.\n");
543 goto err_out;
544 }
545
546 err = ntfs_sd_add_everyone(ni);
547 if (err)
548 goto err_out;
549 rollback_sd = true;
550
551 if (S_ISDIR(mode)) {
552 struct index_root *ir = NULL;
553 struct index_entry *ie;
554 int ir_len, index_len;
555
556 /* Create struct index_root attribute. */
557 index_len = sizeof(struct index_header) + sizeof(struct index_entry_header);
558 ir_len = offsetof(struct index_root, index) + index_len;
559 ir = kzalloc(ir_len, GFP_NOFS);
560 if (!ir) {
561 err = -ENOMEM;
562 goto err_out;
563 }
564 ir->type = AT_FILE_NAME;
565 ir->collation_rule = COLLATION_FILE_NAME;
566 ir->index_block_size = cpu_to_le32(ni->vol->index_record_size);
567 if (ni->vol->cluster_size <= ni->vol->index_record_size)
568 ir->clusters_per_index_block =
569 NTFS_B_TO_CLU(vol, ni->vol->index_record_size);
570 else
571 ir->clusters_per_index_block =
572 ni->vol->index_record_size >> ni->vol->sector_size_bits;
573 ir->index.entries_offset = cpu_to_le32(sizeof(struct index_header));
574 ir->index.index_length = cpu_to_le32(index_len);
575 ir->index.allocated_size = cpu_to_le32(index_len);
576 ie = (struct index_entry *)((u8 *)ir + sizeof(struct index_root));
577 ie->length = cpu_to_le16(sizeof(struct index_entry_header));
578 ie->key_length = 0;
579 ie->flags = INDEX_ENTRY_END;
580
581 /* Add struct index_root attribute to inode. */
582 err = ntfs_attr_add(ni, AT_INDEX_ROOT, I30, 4, (u8 *)ir, ir_len);
583 if (err) {
584 kfree(ir);
585 ntfs_error(vi->i_sb, "Failed to add struct index_root attribute.\n");
586 goto err_out;
587 }
588 kfree(ir);
589 err = ntfs_attr_open(ni, AT_INDEX_ROOT, I30, 4);
590 if (err)
591 goto err_out;
592 } else {
593 /* Add DATA attribute to inode. */
594 err = ntfs_attr_add(ni, AT_DATA, AT_UNNAMED, 0, NULL, 0);
595 if (err) {
596 ntfs_error(dir_ni->vol->sb, "Failed to add DATA attribute.\n");
597 goto err_out;
598 }
599 rollback_data = true;
600
601 err = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0);
602 if (err)
603 goto err_out;
604
605 if (S_ISLNK(mode)) {
606 if (NVolSymlinkNative(vol))
607 err = ntfs_reparse_set_native_symlink(ni, target, target_len);
608 else
609 err = ntfs_reparse_set_wsl_symlink(ni, target, target_len);
610 if (!err)
611 rollback_reparse = true;
612 } else if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode) ||
613 S_ISFIFO(mode)) {
614 si->file_attributes = FILE_ATTRIBUTE_RECALL_ON_OPEN;
615 ni->flags = FILE_ATTRIBUTE_RECALL_ON_OPEN;
616 err = ntfs_reparse_set_wsl_not_symlink(ni, mode);
617 if (!err)
618 rollback_reparse = true;
619 }
620 if (err)
621 goto err_out;
622 }
623
624 err = ntfs_ea_set_wsl_inode(vi, dev, &ea_size,
625 NTFS_EA_UID | NTFS_EA_GID | NTFS_EA_MODE);
626 if (err)
627 goto err_out;
628
629 /* Create FILE_NAME attribute. */
630 fn_len = sizeof(struct file_name_attr) + name_len * sizeof(__le16);
631 fn = kzalloc(fn_len, GFP_NOFS);
632 if (!fn) {
633 err = -ENOMEM;
634 goto err_out;
635 }
636
637 fn->file_attributes |= ni->flags;
638 fn->parent_directory = parent_mft_ref;
639 fn->file_name_length = name_len;
640 fn->file_name_type = FILE_NAME_POSIX;
641 fn->type.ea.packed_ea_size = ea_size;
642 if (S_ISDIR(mode)) {
643 fn->file_attributes = FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT;
644 fn->allocated_size = fn->data_size = 0;
645 } else {
646 fn->data_size = cpu_to_le64(ni->data_size);
647 fn->allocated_size = cpu_to_le64(ni->allocated_size);
648 }
649 if (!S_ISREG(mode) && !S_ISDIR(mode)) {
650 fn->file_attributes = FILE_ATTR_SYSTEM;
651 if (rollback_reparse)
652 fn->file_attributes |= FILE_ATTR_REPARSE_POINT;
653 }
654 if (NVolHideDotFiles(vol) && name_len > 0 && name[0] == cpu_to_le16('.'))
655 fn->file_attributes |= FILE_ATTR_HIDDEN;
656 fn->creation_time = fn->last_data_change_time = utc2ntfs(ni->i_crtime);
657 fn->last_mft_change_time = fn->last_access_time = fn->creation_time;
658 memcpy(fn->file_name, name, name_len * sizeof(__le16));
659
660 /* Add FILE_NAME attribute to inode. */
661 err = ntfs_attr_add(ni, AT_FILE_NAME, AT_UNNAMED, 0, (u8 *)fn, fn_len);
662 if (err) {
663 ntfs_error(sb, "Failed to add FILE_NAME attribute.\n");
664 goto err_out;
665 }
666
667 child_mft_ref = MK_MREF(ni->mft_no,
668 le16_to_cpu(ni_mrec->sequence_number));
669 /* Set hard links count and directory flag. */
670 ni_mrec->link_count = cpu_to_le16(1);
671 mark_mft_record_dirty(ni);
672
673 /* Add FILE_NAME attribute to index. */
674 err = ntfs_index_add_filename(dir_ni, fn, child_mft_ref);
675 if (err) {
676 ntfs_debug("Failed to add entry to the index");
677 goto err_out;
678 }
679
680 unmap_mft_record(ni);
681 mutex_unlock(&dir_ni->mrec_lock);
682 mutex_unlock(&ni->mrec_lock);
683
684 ni->flags = fn->file_attributes |
685 (ni->flags & FILE_ATTRIBUTE_RECALL_ON_OPEN);
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
ntfs_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)735 static int ntfs_create(struct mnt_idmap *idmap, struct inode *dir,
736 struct dentry *dentry, umode_t mode)
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
ntfs_check_unlinkable_dir(struct ntfs_attr_search_ctx * ctx,struct file_name_attr * fn)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
ntfs_test_inode_attr(struct inode * vi,void * data)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 */
ntfs_delete(struct ntfs_inode * ni,struct ntfs_inode * dir_ni,__le16 * name,u8 name_len,bool need_lock)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
ntfs_unlink(struct inode * dir,struct dentry * dentry)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
ntfs_mkdir(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)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, 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
ntfs_rmdir(struct inode * dir,struct dentry * dentry)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 */
__ntfs_link(struct ntfs_inode * ni,struct ntfs_inode * dir_ni,__le16 * name,u8 name_len)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
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)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 bool new_dir_first = false;
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 if (new_inode)
1303 new_ni = NTFS_I(new_inode);
1304 if (old_dir != new_dir)
1305 new_dir_first = is_subdir(new_dentry->d_parent,
1306 old_dentry->d_parent);
1307
1308 if (!(vol->vol_flags & VOLUME_IS_DIRTY))
1309 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1310
1311 mutex_lock_nested(&old_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
1312 if (new_ni)
1313 mutex_lock_nested(&new_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL_2);
1314
1315 if (old_dir == new_dir) {
1316 mutex_lock_nested(&old_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
1317 } else if (new_dir_first) {
1318 mutex_lock_nested(&new_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
1319 mutex_lock_nested(&old_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT_2);
1320 } else {
1321 mutex_lock_nested(&old_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
1322 mutex_lock_nested(&new_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT_2);
1323 }
1324
1325 if (NInoBeingDeleted(old_ni) || NInoBeingDeleted(old_dir_ni) ||
1326 (new_ni && NInoBeingDeleted(new_ni)) ||
1327 (old_dir != new_dir && NInoBeingDeleted(new_dir_ni))) {
1328 err = -ENOENT;
1329 goto err_out;
1330 }
1331
1332 is_dir = S_ISDIR(old_inode->i_mode);
1333
1334 if (new_inode) {
1335 if (is_dir) {
1336 struct mft_record *ni_mrec;
1337
1338 ni_mrec = map_mft_record(NTFS_I(new_inode));
1339 if (IS_ERR(ni_mrec)) {
1340 err = -EIO;
1341 goto err_out;
1342 }
1343 err = ntfs_check_empty_dir(NTFS_I(new_inode), ni_mrec);
1344 unmap_mft_record(NTFS_I(new_inode));
1345 if (err)
1346 goto err_out;
1347 }
1348
1349 err = ntfs_delete(new_ni, new_dir_ni, uname_new, new_name_len, false);
1350 if (err)
1351 goto err_out;
1352 }
1353
1354 err = __ntfs_link(old_ni, new_dir_ni, uname_new, new_name_len);
1355 if (err)
1356 goto err_out;
1357
1358 err = ntfs_delete(old_ni, old_dir_ni, uname_old, old_name_len, false);
1359 if (err) {
1360 int err2;
1361
1362 ntfs_error(sb, "Failed to delete old ntfs inode(%llu) in old dir, err : %d\n",
1363 old_ni->mft_no, err);
1364 err2 = ntfs_delete(old_ni, new_dir_ni, uname_new, new_name_len, false);
1365 if (err2)
1366 ntfs_error(sb, "Failed to delete old ntfs inode in new dir, err : %d\n",
1367 err2);
1368 goto err_out;
1369 }
1370
1371 simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry);
1372 mark_inode_dirty(old_inode);
1373 mark_inode_dirty(old_dir);
1374 if (old_dir != new_dir)
1375 mark_inode_dirty(new_dir);
1376 if (new_inode)
1377 mark_inode_dirty(old_inode);
1378
1379 inode_inc_iversion(new_dir);
1380
1381 err_out:
1382 if (old_dir == new_dir) {
1383 mutex_unlock(&old_dir_ni->mrec_lock);
1384 } else if (new_dir_first) {
1385 mutex_unlock(&old_dir_ni->mrec_lock);
1386 mutex_unlock(&new_dir_ni->mrec_lock);
1387 } else {
1388 mutex_unlock(&new_dir_ni->mrec_lock);
1389 mutex_unlock(&old_dir_ni->mrec_lock);
1390 }
1391 if (new_ni)
1392 mutex_unlock(&new_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
ntfs_symlink(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,const char * symname)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 int usrc_len;
1412 int symlen = strlen(symname);
1413
1414 if (NVolShutdown(vol))
1415 return -EIO;
1416
1417 usrc_len = ntfs_nlstoucs(vol, dentry->d_name.name,
1418 dentry->d_name.len, &usrc, NTFS_MAX_NAME_LEN);
1419 if (usrc_len < 0) {
1420 if (usrc_len != -ENAMETOOLONG)
1421 ntfs_error(sb, "Failed to convert name to Unicode.");
1422 err = -ENOMEM;
1423 goto out;
1424 }
1425
1426 err = ntfs_check_bad_windows_name(vol, usrc, usrc_len);
1427 if (err) {
1428 kmem_cache_free(ntfs_name_cache, usrc);
1429 goto out;
1430 }
1431
1432 if (!(vol->vol_flags & VOLUME_IS_DIRTY))
1433 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1434
1435 ni = __ntfs_create(idmap, dir, usrc, usrc_len, S_IFLNK | 0777, 0,
1436 symname, symlen);
1437 kmem_cache_free(ntfs_name_cache, usrc);
1438 if (IS_ERR(ni)) {
1439 err = PTR_ERR(ni);
1440 goto out;
1441 }
1442
1443 vi = VFS_I(ni);
1444 vi->i_size = symlen;
1445 d_instantiate_new(dentry, vi);
1446 out:
1447 return err;
1448 }
1449
ntfs_mknod(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev)1450 static int ntfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
1451 struct dentry *dentry, umode_t mode, dev_t rdev)
1452 {
1453 struct super_block *sb = dir->i_sb;
1454 struct ntfs_volume *vol = NTFS_SB(sb);
1455 int err = 0;
1456 struct ntfs_inode *ni;
1457 __le16 *uname = NULL;
1458 int uname_len;
1459
1460 if (NVolShutdown(vol))
1461 return -EIO;
1462
1463 uname_len = ntfs_nlstoucs(vol, dentry->d_name.name,
1464 dentry->d_name.len, &uname, NTFS_MAX_NAME_LEN);
1465 if (uname_len < 0) {
1466 if (uname_len != -ENAMETOOLONG)
1467 ntfs_error(sb, "Failed to convert name to Unicode.");
1468 return -ENOMEM;
1469 }
1470
1471 err = ntfs_check_bad_windows_name(vol, uname, uname_len);
1472 if (err) {
1473 kmem_cache_free(ntfs_name_cache, uname);
1474 return err;
1475 }
1476
1477 if (!(vol->vol_flags & VOLUME_IS_DIRTY))
1478 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1479
1480 switch (mode & S_IFMT) {
1481 case S_IFCHR:
1482 case S_IFBLK:
1483 ni = __ntfs_create(idmap, dir, uname, uname_len,
1484 mode, rdev, NULL, 0);
1485 break;
1486 default:
1487 ni = __ntfs_create(idmap, dir, uname, uname_len,
1488 mode, 0, NULL, 0);
1489 }
1490
1491 kmem_cache_free(ntfs_name_cache, uname);
1492 if (IS_ERR(ni)) {
1493 err = PTR_ERR(ni);
1494 goto out;
1495 }
1496
1497 d_instantiate_new(dentry, VFS_I(ni));
1498 out:
1499 return err;
1500 }
1501
ntfs_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)1502 static int ntfs_link(struct dentry *old_dentry, struct inode *dir,
1503 struct dentry *dentry)
1504 {
1505 struct inode *vi = old_dentry->d_inode;
1506 struct super_block *sb = vi->i_sb;
1507 struct ntfs_volume *vol = NTFS_SB(sb);
1508 __le16 *uname = NULL;
1509 int uname_len;
1510 int err;
1511 struct ntfs_inode *ni = NTFS_I(vi), *dir_ni = NTFS_I(dir);
1512
1513 if (NVolShutdown(vol))
1514 return -EIO;
1515
1516 uname_len = ntfs_nlstoucs(vol, dentry->d_name.name,
1517 dentry->d_name.len, &uname, NTFS_MAX_NAME_LEN);
1518 if (uname_len < 0) {
1519 if (uname_len != -ENAMETOOLONG)
1520 ntfs_error(sb, "Failed to convert name to unicode.");
1521 return -ENOMEM;
1522 }
1523
1524 if (!(vol->vol_flags & VOLUME_IS_DIRTY))
1525 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1526
1527 ihold(vi);
1528 mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
1529 mutex_lock_nested(&dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
1530 err = __ntfs_link(NTFS_I(vi), NTFS_I(dir), uname, uname_len);
1531 if (err) {
1532 mutex_unlock(&dir_ni->mrec_lock);
1533 mutex_unlock(&ni->mrec_lock);
1534 iput(vi);
1535 pr_err("failed to create link, err = %d\n", err);
1536 goto out;
1537 }
1538
1539 inode_inc_iversion(dir);
1540 simple_inode_init_ts(dir);
1541
1542 inode_inc_iversion(vi);
1543 simple_inode_init_ts(vi);
1544
1545 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
1546 d_instantiate(dentry, vi);
1547 mutex_unlock(&dir_ni->mrec_lock);
1548 mutex_unlock(&ni->mrec_lock);
1549
1550 out:
1551 kmem_cache_free(ntfs_name_cache, uname);
1552 return err;
1553 }
1554
1555 /*
1556 * Inode operations for directories.
1557 */
1558 const struct inode_operations ntfs_dir_inode_ops = {
1559 .lookup = ntfs_lookup, /* VFS: Lookup directory. */
1560 .create = ntfs_create,
1561 .unlink = ntfs_unlink,
1562 .mkdir = ntfs_mkdir,
1563 .rmdir = ntfs_rmdir,
1564 .rename = ntfs_rename,
1565 .get_acl = ntfs_get_acl,
1566 .set_acl = ntfs_set_acl,
1567 .listxattr = ntfs_listxattr,
1568 .setattr = ntfs_setattr,
1569 .getattr = ntfs_getattr,
1570 .symlink = ntfs_symlink,
1571 .mknod = ntfs_mknod,
1572 .link = ntfs_link,
1573 };
1574
1575 /*
1576 * ntfs_get_parent - find the dentry of the parent of a given directory dentry
1577 * @child_dent: dentry of the directory whose parent directory to find
1578 *
1579 * Find the dentry for the parent directory of the directory specified by the
1580 * dentry @child_dent. This function is called from
1581 * fs/exportfs/expfs.c::find_exported_dentry() which in turn is called from the
1582 * default ->decode_fh() which is export_decode_fh() in the same file.
1583 *
1584 * Note: ntfs_get_parent() is called with @d_inode(child_dent)->i_mutex down.
1585 *
1586 * Return the dentry of the parent directory on success or the error code on
1587 * error (IS_ERR() is true).
1588 */
ntfs_get_parent(struct dentry * child_dent)1589 static struct dentry *ntfs_get_parent(struct dentry *child_dent)
1590 {
1591 struct inode *vi = d_inode(child_dent);
1592 struct ntfs_inode *ni = NTFS_I(vi);
1593 struct mft_record *mrec;
1594 struct ntfs_attr_search_ctx *ctx;
1595 struct attr_record *attr;
1596 struct file_name_attr *fn;
1597 unsigned long parent_ino;
1598 int err;
1599
1600 ntfs_debug("Entering for inode 0x%llx.", ni->mft_no);
1601 /* Get the mft record of the inode belonging to the child dentry. */
1602 mrec = map_mft_record(ni);
1603 if (IS_ERR(mrec))
1604 return ERR_CAST(mrec);
1605 /* Find the first file name attribute in the mft record. */
1606 ctx = ntfs_attr_get_search_ctx(ni, mrec);
1607 if (unlikely(!ctx)) {
1608 unmap_mft_record(ni);
1609 return ERR_PTR(-ENOMEM);
1610 }
1611 try_next:
1612 err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, CASE_SENSITIVE, 0, NULL,
1613 0, ctx);
1614 if (unlikely(err)) {
1615 ntfs_attr_put_search_ctx(ctx);
1616 unmap_mft_record(ni);
1617 if (err == -ENOENT)
1618 ntfs_error(vi->i_sb,
1619 "Inode 0x%llx does not have a file name attribute. Run chkdsk.",
1620 ni->mft_no);
1621 return ERR_PTR(err);
1622 }
1623 attr = ctx->attr;
1624 if (unlikely(attr->non_resident))
1625 goto try_next;
1626 fn = (struct file_name_attr *)((u8 *)attr +
1627 le16_to_cpu(attr->data.resident.value_offset));
1628 if (unlikely((u8 *)fn + le32_to_cpu(attr->data.resident.value_length) >
1629 (u8 *)attr + le32_to_cpu(attr->length)))
1630 goto try_next;
1631 /* Get the inode number of the parent directory. */
1632 parent_ino = MREF_LE(fn->parent_directory);
1633 /* Release the search context and the mft record of the child. */
1634 ntfs_attr_put_search_ctx(ctx);
1635 unmap_mft_record(ni);
1636
1637 return d_obtain_alias(ntfs_iget(vi->i_sb, parent_ino));
1638 }
1639
ntfs_nfs_get_inode(struct super_block * sb,u64 ino,u32 generation)1640 static struct inode *ntfs_nfs_get_inode(struct super_block *sb,
1641 u64 ino, u32 generation)
1642 {
1643 struct inode *inode;
1644
1645 inode = ntfs_iget(sb, ino);
1646 if (!IS_ERR(inode)) {
1647 if (inode->i_generation != generation) {
1648 iput(inode);
1649 inode = ERR_PTR(-ESTALE);
1650 }
1651 }
1652
1653 return inode;
1654 }
1655
ntfs_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)1656 static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1657 int fh_len, int fh_type)
1658 {
1659 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1660 ntfs_nfs_get_inode);
1661 }
1662
ntfs_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)1663 static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
1664 int fh_len, int fh_type)
1665 {
1666 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1667 ntfs_nfs_get_inode);
1668 }
1669
1670 /*
1671 * Export operations allowing NFS exporting of mounted NTFS partitions.
1672 */
1673 const struct export_operations ntfs_export_ops = {
1674 .encode_fh = generic_encode_ino32_fh,
1675 .get_parent = ntfs_get_parent, /* Find the parent of a given directory. */
1676 .fh_to_dentry = ntfs_fh_to_dentry,
1677 .fh_to_parent = ntfs_fh_to_parent,
1678 };
1679