xref: /linux/fs/ntfs/namei.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
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 #include <linux/namei.h>
12 
13 #include "ntfs.h"
14 #include "time.h"
15 #include "index.h"
16 #include "reparse.h"
17 #include "object_id.h"
18 #include "ea.h"
19 #include "stream.h"
20 
21 static const __le16 aux_name_le[3] = {
22 	cpu_to_le16('A'), cpu_to_le16('U'), cpu_to_le16('X')
23 };
24 
25 static const __le16 con_name_le[3] = {
26 	cpu_to_le16('C'), cpu_to_le16('O'), cpu_to_le16('N')
27 };
28 
29 static const __le16 com_name_le[3] = {
30 	cpu_to_le16('C'), cpu_to_le16('O'), cpu_to_le16('M')
31 };
32 
33 static const __le16 lpt_name_le[3] = {
34 	cpu_to_le16('L'), cpu_to_le16('P'), cpu_to_le16('T')
35 };
36 
37 static const __le16 nul_name_le[3] = {
38 	cpu_to_le16('N'), cpu_to_le16('U'), cpu_to_le16('L')
39 };
40 
41 static const __le16 prn_name_le[3] = {
42 	cpu_to_le16('P'), cpu_to_le16('R'), cpu_to_le16('N')
43 };
44 
45 static inline int ntfs_check_bad_char(const __le16 *wc, unsigned int wc_len)
46 {
47 	int i;
48 
49 	for (i = 0; i < wc_len; i++) {
50 		u16 c = le16_to_cpu(wc[i]);
51 
52 		if (c < 0x0020 ||
53 		    c == 0x0022 || c == 0x002A || c == 0x002F ||
54 		    c == 0x003A || c == 0x003C || c == 0x003E ||
55 		    c == 0x003F || c == 0x005C || c == 0x007C)
56 			return -EINVAL;
57 	}
58 
59 	return 0;
60 }
61 
62 /*
63  * ntfs_check_bad_windows_name() - Validate a name against Windows rules
64  * @vol: NTFS volume
65  * @wc: UTF-16LE name
66  * @wc_len: length of @wc in UTF-16 code units
67  *
68  * When Windows-name checks are enabled, reject disallowed characters,
69  * trailing spaces or dots, and reserved DOS device names.
70  *
71  * Return: 0 if valid, or -EINVAL otherwise.
72  */
73 int ntfs_check_bad_windows_name(struct ntfs_volume *vol,
74 				       const __le16 *wc,
75 				       unsigned int wc_len)
76 {
77 	if (!NVolCheckWindowsNames(vol))
78 		return 0;
79 
80 	if (ntfs_check_bad_char(wc, wc_len))
81 		return -EINVAL;
82 
83 	/* Check for trailing space or dot. */
84 	if (wc_len > 0 &&
85 	    (wc[wc_len - 1] == cpu_to_le16(' ') ||
86 	    wc[wc_len - 1] == cpu_to_le16('.')))
87 		return -EINVAL;
88 
89 	if (wc_len == 3 || (wc_len > 3 && wc[3] == cpu_to_le16('.'))) {
90 		__le16 *upcase = vol->upcase;
91 		u32 size = vol->upcase_len;
92 
93 		if (ntfs_are_names_equal(wc, 3, aux_name_le, 3, IGNORE_CASE, upcase, size) ||
94 		    ntfs_are_names_equal(wc, 3, con_name_le, 3, IGNORE_CASE, upcase, size) ||
95 		    ntfs_are_names_equal(wc, 3, nul_name_le, 3, IGNORE_CASE, upcase, size) ||
96 		    ntfs_are_names_equal(wc, 3, prn_name_le, 3, IGNORE_CASE, upcase, size))
97 			return -EINVAL;
98 	}
99 
100 	if (wc_len == 4 || (wc_len > 4 && wc[4] == cpu_to_le16('.'))) {
101 		__le16 *upcase = vol->upcase;
102 		u32 size = vol->upcase_len, port;
103 
104 		if (ntfs_are_names_equal(wc, 3, com_name_le, 3, IGNORE_CASE, upcase, size) ||
105 		    ntfs_are_names_equal(wc, 3, lpt_name_le, 3, IGNORE_CASE, upcase, size)) {
106 			port = le16_to_cpu(wc[3]);
107 			if (port >= '1' && port <= '9')
108 				return -EINVAL;
109 		}
110 	}
111 	return 0;
112 }
113 
114 /*
115  * ntfs_lookup - find the inode represented by a dentry in a directory inode
116  * @dir_ino:	directory inode in which to look for the inode
117  * @dent:	dentry representing the inode to look for
118  * @flags:	lookup flags
119  *
120  * In short, ntfs_lookup() looks for the inode represented by the dentry @dent
121  * in the directory inode @dir_ino and if found attaches the inode to the
122  * dentry @dent.
123  *
124  * In more detail, the dentry @dent specifies which inode to look for by
125  * supplying the name of the inode in @dent->d_name.name. ntfs_lookup()
126  * converts the name to Unicode and walks the contents of the directory inode
127  * @dir_ino looking for the converted Unicode name. If the name is found in the
128  * directory, the corresponding inode is loaded by calling ntfs_iget() on its
129  * inode number and the inode is associated with the dentry @dent via a call to
130  * d_splice_alias().
131  *
132  * If the name is not found in the directory, a NULL inode is inserted into the
133  * dentry @dent via a call to d_add(). The dentry is then termed a negative
134  * dentry.
135  *
136  * Only if an actual error occurs, do we return an error via ERR_PTR().
137  *
138  * In order to handle the case insensitivity issues of NTFS with regards to the
139  * dcache and the dcache requiring only one dentry per directory, we deal with
140  * dentry aliases that only differ in case in ->ntfs_lookup() while maintaining
141  * a case sensitive dcache. This means that we get the full benefit of dcache
142  * speed when the file/directory is looked up with the same case as returned by
143  * ->ntfs_readdir() but that a lookup for any other case (or for the short file
144  * name) will not find anything in dcache and will enter ->ntfs_lookup()
145  * instead, where we search the directory for a fully matching file name
146  * (including case) and if that is not found, we search for a file name that
147  * matches with different case and if that has non-POSIX semantics we return
148  * that. We actually do only one search (case sensitive) and keep tabs on
149  * whether we have found a case insensitive match in the process.
150  *
151  * To simplify matters for us, we do not treat the short vs long filenames as
152  * two hard links but instead if the lookup matches a short filename, we
153  * return the dentry for the corresponding long filename instead.
154  *
155  * There are three cases we need to distinguish here:
156  *
157  * 1) @dent perfectly matches (i.e. including case) a directory entry with a
158  *    file name in the WIN32 or POSIX namespaces. In this case
159  *    ntfs_lookup_inode_by_name() will return with name set to NULL and we
160  *    just d_splice_alias() @dent.
161  * 2) @dent matches (not including case) a directory entry with a file name in
162  *    the WIN32 namespace. In this case ntfs_lookup_inode_by_name() will return
163  *    with name set to point to a kmalloc()ed ntfs_name structure containing
164  *    the properly cased little endian Unicode name. We convert the name to the
165  *    current NLS code page, search if a dentry with this name already exists
166  *    and if so return that instead of @dent.  At this point things are
167  *    complicated by the possibility of 'disconnected' dentries due to NFS
168  *    which we deal with appropriately (see the code comments).  The VFS will
169  *    then destroy the old @dent and use the one we returned.  If a dentry is
170  *    not found, we allocate a new one, d_splice_alias() it, and return it as
171  *    above.
172  * 3) @dent matches either perfectly or not (i.e. we don't care about case) a
173  *    directory entry with a file name in the DOS namespace. In this case
174  *    ntfs_lookup_inode_by_name() will return with name set to point to a
175  *    kmalloc()ed ntfs_name structure containing the mft reference (cpu endian)
176  *    of the inode. We use the mft reference to read the inode and to find the
177  *    file name in the WIN32 namespace corresponding to the matched short file
178  *    name. We then convert the name to the current NLS code page, and proceed
179  *    searching for a dentry with this name, etc, as in case 2), above.
180  *
181  * Locking: Caller must hold i_mutex on the directory.
182  */
183 
184 static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent,
185 		unsigned int flags)
186 {
187 	struct ntfs_volume *vol = NTFS_SB(dir_ino->i_sb);
188 	struct ntfs_stream_path stream_path;
189 	struct inode *dent_inode;
190 	__le16 *uname;
191 	struct ntfs_name *name = NULL;
192 	u64 mref;
193 	unsigned long dent_ino;
194 	int uname_len;
195 	int stream_path_len;
196 
197 	ntfs_debug("Looking up %pd in directory inode 0x%llx.",
198 			dent, NTFS_I(dir_ino)->mft_no);
199 	stream_path_len = ntfs_stream_path_parse(vol, &dent->d_name,
200 			&stream_path);
201 	if (stream_path_len < 0)
202 		return ERR_PTR(stream_path_len);
203 	if (stream_path_len)
204 		return ntfs_lookup_stream(dir_ino, dent, &stream_path);
205 
206 	/* Convert the name of the dentry to Unicode. */
207 	uname_len = ntfs_nlstoucs(vol, dent->d_name.name, dent->d_name.len,
208 				  &uname, NTFS_MAX_NAME_LEN);
209 	if (uname_len < 0) {
210 		if (uname_len != -ENAMETOOLONG)
211 			ntfs_debug("Failed to convert name to Unicode.");
212 		return ERR_PTR(uname_len);
213 	}
214 	mutex_lock(&NTFS_I(dir_ino)->mrec_lock);
215 	mref = ntfs_lookup_inode_by_name(NTFS_I(dir_ino), uname, uname_len,
216 			&name);
217 	mutex_unlock(&NTFS_I(dir_ino)->mrec_lock);
218 	kmem_cache_free(ntfs_name_cache, uname);
219 	if (!IS_ERR_MREF(mref)) {
220 		dent_ino = MREF(mref);
221 		ntfs_debug("Found inode 0x%lx. Calling ntfs_iget.", dent_ino);
222 		dent_inode = ntfs_iget(vol->sb, dent_ino);
223 		if (!IS_ERR(dent_inode)) {
224 			/* Consistency check. */
225 			if (MSEQNO(mref) == NTFS_I(dent_inode)->seq_no ||
226 			    dent_ino == FILE_MFT) {
227 				/* Perfect WIN32/POSIX match. -- Case 1. */
228 				if (!name) {
229 					ntfs_debug("Done.  (Case 1.)");
230 					return d_splice_alias(dent_inode, dent);
231 				}
232 				/*
233 				 * We are too indented.  Handle imperfect
234 				 * matches and short file names further below.
235 				 */
236 				goto handle_name;
237 			}
238 			ntfs_error(vol->sb,
239 				"Found stale reference to inode 0x%lx (reference sequence number = 0x%x, inode sequence number = 0x%x), returning -EIO. Run chkdsk.",
240 				dent_ino, MSEQNO(mref),
241 				NTFS_I(dent_inode)->seq_no);
242 			iput(dent_inode);
243 			dent_inode = ERR_PTR(-EIO);
244 		} else
245 			ntfs_error(vol->sb, "ntfs_iget(0x%lx) failed with error code %li.",
246 					dent_ino, PTR_ERR(dent_inode));
247 		kfree(name);
248 		/* Return the error code. */
249 		return ERR_CAST(dent_inode);
250 	}
251 	kfree(name);
252 	/* It is guaranteed that @name is no longer allocated at this point. */
253 	if (MREF_ERR(mref) == -ENOENT) {
254 		ntfs_debug("Entry was not found, adding negative dentry.");
255 		/* The dcache will handle negative entries. */
256 		ntfs_debug("Done.");
257 		return d_splice_alias(NULL, dent);
258 	}
259 	ntfs_error(vol->sb, "ntfs_lookup_ino_by_name() failed with error code %i.",
260 			-MREF_ERR(mref));
261 	return ERR_PTR(MREF_ERR(mref));
262 handle_name:
263 	{
264 		struct mft_record *m;
265 		struct ntfs_attr_search_ctx *ctx;
266 		struct ntfs_inode *ni = NTFS_I(dent_inode);
267 		int err;
268 		struct qstr nls_name;
269 
270 		nls_name.name = NULL;
271 		if (name->type != FILE_NAME_DOS) {			/* Case 2. */
272 			ntfs_debug("Case 2.");
273 			nls_name.len = (unsigned int)ntfs_ucstonls(vol,
274 					(__le16 *)&name->name, name->len,
275 					(unsigned char **)&nls_name.name, 0);
276 			kfree(name);
277 		} else /* if (name->type == FILE_NAME_DOS) */ {		/* Case 3. */
278 			struct file_name_attr *fn;
279 
280 			ntfs_debug("Case 3.");
281 			kfree(name);
282 
283 			/* Find the WIN32 name corresponding to the matched DOS name. */
284 			ni = NTFS_I(dent_inode);
285 			m = map_mft_record(ni);
286 			if (IS_ERR(m)) {
287 				err = PTR_ERR(m);
288 				m = NULL;
289 				ctx = NULL;
290 				goto err_out;
291 			}
292 			ctx = ntfs_attr_get_search_ctx(ni, m);
293 			if (unlikely(!ctx)) {
294 				err = -ENOMEM;
295 				goto err_out;
296 			}
297 			do {
298 				struct attr_record *a;
299 
300 				err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0,
301 						NULL, 0, ctx);
302 				if (unlikely(err)) {
303 					ntfs_error(vol->sb,
304 						"Inode corrupt: No WIN32 namespace counterpart to DOS file name. Run chkdsk.");
305 					if (err == -ENOENT)
306 						err = -EIO;
307 					goto err_out;
308 				}
309 				/* Consistency checks. */
310 				a = ctx->attr;
311 				if (a->non_resident || a->flags)
312 					goto eio_err_out;
313 				fn = (struct file_name_attr *)((u8 *)ctx->attr + le16_to_cpu(
314 							ctx->attr->data.resident.value_offset));
315 			} while (fn->file_name_type != FILE_NAME_WIN32);
316 
317 			/* Convert the found WIN32 name to current NLS code page. */
318 			nls_name.len = (unsigned int)ntfs_ucstonls(vol,
319 					(__le16 *)&fn->file_name, fn->file_name_length,
320 					(unsigned char **)&nls_name.name, 0);
321 
322 			ntfs_attr_put_search_ctx(ctx);
323 			unmap_mft_record(ni);
324 		}
325 		m = NULL;
326 		ctx = NULL;
327 
328 		/* Check if a conversion error occurred. */
329 		if ((int)nls_name.len < 0) {
330 			err = (int)nls_name.len;
331 			goto err_out;
332 		}
333 		nls_name.hash = full_name_hash(dent, nls_name.name, nls_name.len);
334 
335 		dent = d_add_ci(dent, dent_inode, &nls_name);
336 		kfree(nls_name.name);
337 		return dent;
338 
339 eio_err_out:
340 		ntfs_error(vol->sb, "Illegal file name attribute. Run chkdsk.");
341 		err = -EIO;
342 err_out:
343 		if (ctx)
344 			ntfs_attr_put_search_ctx(ctx);
345 		if (m)
346 			unmap_mft_record(ni);
347 		iput(dent_inode);
348 		ntfs_error(vol->sb, "Failed, returning error code %i.", err);
349 		return ERR_PTR(err);
350 	}
351 }
352 
353 static int ntfs_sd_add_everyone(struct ntfs_inode *ni)
354 {
355 	struct security_descriptor_relative *sd;
356 	struct ntfs_acl *acl;
357 	struct ntfs_ace *ace;
358 	struct ntfs_sid *sid;
359 	int ret, sd_len;
360 
361 	/* Create SECURITY_DESCRIPTOR attribute (everyone has full access). */
362 	/*
363 	 * Calculate security descriptor length. We have 2 sub-authorities in
364 	 * owner and group SIDs, So add 8 bytes to every SID.
365 	 */
366 	sd_len = sizeof(struct security_descriptor_relative) + 2 *
367 		(sizeof(struct ntfs_sid) + 8) + sizeof(struct ntfs_acl) +
368 		sizeof(struct ntfs_ace) + 4;
369 	sd = kzalloc(sd_len, GFP_NOFS);
370 	if (!sd)
371 		return -ENOMEM;
372 
373 	sd->revision = 1;
374 	sd->control = SE_DACL_PRESENT | SE_SELF_RELATIVE;
375 
376 	sid = (struct ntfs_sid *)((u8 *)sd + sizeof(struct security_descriptor_relative));
377 	sid->revision = 1;
378 	sid->sub_authority_count = 2;
379 	sid->sub_authority[0] = cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID);
380 	sid->sub_authority[1] = cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS);
381 	sid->identifier_authority.value[5] = 5;
382 	sd->owner = cpu_to_le32((u8 *)sid - (u8 *)sd);
383 
384 	sid = (struct ntfs_sid *)((u8 *)sid + sizeof(struct ntfs_sid) + 8);
385 	sid->revision = 1;
386 	sid->sub_authority_count = 2;
387 	sid->sub_authority[0] = cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID);
388 	sid->sub_authority[1] = cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS);
389 	sid->identifier_authority.value[5] = 5;
390 	sd->group = cpu_to_le32((u8 *)sid - (u8 *)sd);
391 
392 	acl = (struct ntfs_acl *)((u8 *)sid + sizeof(struct ntfs_sid) + 8);
393 	acl->revision = 2;
394 	acl->size = cpu_to_le16(sizeof(struct ntfs_acl) + sizeof(struct ntfs_ace) + 4);
395 	acl->ace_count = cpu_to_le16(1);
396 	sd->dacl = cpu_to_le32((u8 *)acl - (u8 *)sd);
397 
398 	ace = (struct ntfs_ace *)((u8 *)acl + sizeof(struct ntfs_acl));
399 	ace->type = ACCESS_ALLOWED_ACE_TYPE;
400 	ace->flags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE;
401 	ace->size = cpu_to_le16(sizeof(struct ntfs_ace) + 4);
402 	ace->mask = cpu_to_le32(0x1f01ff);
403 	ace->sid.revision = 1;
404 	ace->sid.sub_authority_count = 1;
405 	ace->sid.sub_authority[0] = 0;
406 	ace->sid.identifier_authority.value[5] = 1;
407 
408 	ret = ntfs_attr_add(ni, AT_SECURITY_DESCRIPTOR, AT_UNNAMED, 0, (u8 *)sd,
409 			sd_len);
410 	if (ret)
411 		ntfs_error(ni->vol->sb, "Failed to add SECURITY_DESCRIPTOR\n");
412 
413 	kfree(sd);
414 	return ret;
415 }
416 
417 static struct ntfs_inode *__ntfs_create(const struct mnt_idmap *idmap, struct inode *dir,
418 		__le16 *name, u8 name_len, mode_t mode, dev_t dev,
419 		const char *target, int target_len)
420 {
421 	struct ntfs_inode *dir_ni = NTFS_I(dir);
422 	struct ntfs_volume *vol = dir_ni->vol;
423 	struct ntfs_inode *ni;
424 	bool rollback_data = false, rollback_sd = false, rollback_reparse = false;
425 	struct file_name_attr *fn = NULL;
426 	struct standard_information *si = NULL;
427 	int err = 0, fn_len, si_len;
428 	struct inode *vi;
429 	struct mft_record *ni_mrec, *dni_mrec;
430 	struct super_block *sb = dir_ni->vol->sb;
431 	struct inode *rollback_data_vi = NULL, *rollback_sd_vi = NULL;
432 	__le64 parent_mft_ref;
433 	u64 child_mft_ref;
434 	__le16 ea_size;
435 
436 	vi = new_inode(vol->sb);
437 	if (!vi)
438 		return ERR_PTR(-ENOMEM);
439 
440 	ntfs_init_big_inode(vi);
441 	ni = NTFS_I(vi);
442 	ni->vol = dir_ni->vol;
443 	ni->name_len = 0;
444 	ni->name = NULL;
445 
446 	/*
447 	 * Set the appropriate mode, attribute type, and name.  For
448 	 * directories, also setup the index values to the defaults.
449 	 */
450 	if (S_ISDIR(mode)) {
451 		NInoSetMstProtected(ni);
452 		ni->itype.index.block_size = 4096;
453 		ni->itype.index.block_size_bits = ntfs_ffs(4096) - 1;
454 		ni->itype.index.collation_rule = COLLATION_FILE_NAME;
455 		if (vol->cluster_size <= ni->itype.index.block_size) {
456 			ni->itype.index.vcn_size = vol->cluster_size;
457 			ni->itype.index.vcn_size_bits =
458 				vol->cluster_size_bits;
459 		} else {
460 			ni->itype.index.vcn_size = NTFS_BLOCK_SIZE;
461 			ni->itype.index.vcn_size_bits = NTFS_BLOCK_SIZE_BITS;
462 		}
463 	}
464 
465 	if (IS_RDONLY(vi))
466 		mode &= ~0222;
467 
468 	inode_init_owner(idmap, vi, dir, mode);
469 
470 	mode = vi->i_mode;
471 
472 #ifdef CONFIG_NTFS_FS_POSIX_ACL
473 	if (!S_ISLNK(mode) && (sb->s_flags & SB_POSIXACL)) {
474 		err = ntfs_init_acl(idmap, vi, dir);
475 		if (err) {
476 			iput(vi);
477 			return ERR_PTR(err);
478 		}
479 	} else
480 #endif
481 	{
482 		vi->i_flags |= S_NOSEC;
483 	}
484 
485 	if (uid_valid(vol->uid))
486 		vi->i_uid = vol->uid;
487 
488 	if (gid_valid(vol->gid))
489 		vi->i_gid = vol->gid;
490 
491 	/*
492 	 * Set the file size to 0, the ntfs inode sizes are set to 0 by
493 	 * the call to ntfs_init_big_inode() below.
494 	 */
495 	vi->i_size = 0;
496 	vi->i_blocks = 0;
497 
498 	inode_inc_iversion(vi);
499 
500 	simple_inode_init_ts(vi);
501 	ni->i_crtime = inode_get_ctime(vi);
502 
503 	inode_set_mtime_to_ts(dir, ni->i_crtime);
504 	inode_set_ctime_to_ts(dir, ni->i_crtime);
505 	mark_inode_dirty(dir);
506 
507 	err = ntfs_mft_record_alloc(dir_ni->vol, mode, &ni, NULL,
508 			&ni_mrec, -1);
509 	if (err) {
510 		iput(vi);
511 		return ERR_PTR(err);
512 	}
513 
514 	/*
515 	 * Prevent iget and writeback from finding this inode.
516 	 * Caller must call d_instantiate_new instead of d_instantiate.
517 	 */
518 	spin_lock(&vi->i_lock);
519 	inode_state_set(vi, I_NEW | I_CREATING);
520 	spin_unlock(&vi->i_lock);
521 
522 	/* Add the inode to the inode hash for the superblock. */
523 	vi->i_ino = (unsigned long)ni->mft_no;
524 	inode_set_iversion(vi, 1);
525 	insert_inode_hash(vi);
526 
527 	mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
528 	mutex_lock_nested(&dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
529 	if (NInoBeingDeleted(dir_ni)) {
530 		err = -ENOENT;
531 		goto err_out;
532 	}
533 
534 	dni_mrec = map_mft_record(dir_ni);
535 	if (IS_ERR(dni_mrec)) {
536 		ntfs_error(dir_ni->vol->sb, "failed to map mft record for file 0x%llx.\n",
537 			   dir_ni->mft_no);
538 		err = -EIO;
539 		goto err_out;
540 	}
541 	parent_mft_ref = MK_LE_MREF(dir_ni->mft_no,
542 				    le16_to_cpu(dni_mrec->sequence_number));
543 	unmap_mft_record(dir_ni);
544 
545 	/*
546 	 * Create STANDARD_INFORMATION attribute. Write STANDARD_INFORMATION
547 	 * version 1.2, windows will upgrade it to version 3 if needed.
548 	 */
549 	si_len = offsetof(struct standard_information, file_attributes) +
550 		sizeof(__le32) + 12;
551 	si = kzalloc(si_len, GFP_NOFS);
552 	if (!si) {
553 		err = -ENOMEM;
554 		goto err_out;
555 	}
556 
557 	si->creation_time = si->last_data_change_time = utc2ntfs(ni->i_crtime);
558 	si->last_mft_change_time = si->last_access_time = si->creation_time;
559 
560 	if (!S_ISREG(mode) && !S_ISDIR(mode))
561 		si->file_attributes = FILE_ATTR_SYSTEM;
562 
563 	/* Add STANDARD_INFORMATION to inode. */
564 	err = ntfs_attr_add(ni, AT_STANDARD_INFORMATION, AT_UNNAMED, 0, (u8 *)si,
565 			si_len);
566 	if (err) {
567 		ntfs_error(sb, "Failed to add STANDARD_INFORMATION attribute.\n");
568 		goto err_out;
569 	}
570 
571 	err = ntfs_sd_add_everyone(ni);
572 	if (err)
573 		goto err_out;
574 	rollback_sd = true;
575 
576 	if (S_ISDIR(mode)) {
577 		struct index_root *ir = NULL;
578 		struct index_entry *ie;
579 		int ir_len, index_len;
580 
581 		/* Create struct index_root attribute. */
582 		index_len = sizeof(struct index_header) + sizeof(struct index_entry_header);
583 		ir_len = offsetof(struct index_root, index) + index_len;
584 		ir = kzalloc(ir_len, GFP_NOFS);
585 		if (!ir) {
586 			err = -ENOMEM;
587 			goto err_out;
588 		}
589 		ir->type = AT_FILE_NAME;
590 		ir->collation_rule = COLLATION_FILE_NAME;
591 		ir->index_block_size = cpu_to_le32(ni->vol->index_record_size);
592 		if (ni->vol->cluster_size <= ni->vol->index_record_size)
593 			ir->clusters_per_index_block =
594 				NTFS_B_TO_CLU(vol, ni->vol->index_record_size);
595 		else
596 			ir->clusters_per_index_block =
597 				ni->vol->index_record_size >> NTFS_BLOCK_SIZE_BITS;
598 		ir->index.entries_offset = cpu_to_le32(sizeof(struct index_header));
599 		ir->index.index_length = cpu_to_le32(index_len);
600 		ir->index.allocated_size = cpu_to_le32(index_len);
601 		ie = (struct index_entry *)((u8 *)ir + sizeof(struct index_root));
602 		ie->length = cpu_to_le16(sizeof(struct index_entry_header));
603 		ie->key_length = 0;
604 		ie->flags = INDEX_ENTRY_END;
605 
606 		/* Add struct index_root attribute to inode. */
607 		err = ntfs_attr_add(ni, AT_INDEX_ROOT, I30, 4, (u8 *)ir, ir_len);
608 		if (err) {
609 			kfree(ir);
610 			ntfs_error(vi->i_sb, "Failed to add struct index_root attribute.\n");
611 			goto err_out;
612 		}
613 		kfree(ir);
614 		err = ntfs_attr_open(ni, AT_INDEX_ROOT, I30, 4);
615 		if (err)
616 			goto err_out;
617 	} else {
618 		/* Add DATA attribute to inode. */
619 		err = ntfs_attr_add(ni, AT_DATA, AT_UNNAMED, 0, NULL, 0);
620 		if (err) {
621 			ntfs_error(dir_ni->vol->sb, "Failed to add DATA attribute.\n");
622 			goto err_out;
623 		}
624 		rollback_data = true;
625 
626 		err = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0);
627 		if (err)
628 			goto err_out;
629 
630 		if (S_ISLNK(mode)) {
631 			if (NVolSymlinkNative(vol))
632 				err = ntfs_reparse_set_native_symlink(ni, target, target_len);
633 			else
634 				err = ntfs_reparse_set_wsl_symlink(ni, target, target_len);
635 			if (!err)
636 				rollback_reparse = true;
637 		} else if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode) ||
638 			   S_ISFIFO(mode)) {
639 			si->file_attributes = FILE_ATTRIBUTE_RECALL_ON_OPEN;
640 			ni->flags = FILE_ATTRIBUTE_RECALL_ON_OPEN;
641 			err = ntfs_reparse_set_wsl_not_symlink(ni, mode);
642 			if (!err)
643 				rollback_reparse = true;
644 		}
645 		if (err)
646 			goto err_out;
647 	}
648 
649 	err = ntfs_ea_set_wsl_inode(vi, dev, &ea_size,
650 			NTFS_EA_UID | NTFS_EA_GID | NTFS_EA_MODE);
651 	if (err)
652 		goto err_out;
653 
654 	/* Create FILE_NAME attribute. */
655 	fn_len = sizeof(struct file_name_attr) + name_len * sizeof(__le16);
656 	fn = kzalloc(fn_len, GFP_NOFS);
657 	if (!fn) {
658 		err = -ENOMEM;
659 		goto err_out;
660 	}
661 
662 	fn->file_attributes |= ni->flags;
663 	fn->parent_directory = parent_mft_ref;
664 	fn->file_name_length = name_len;
665 	fn->file_name_type = FILE_NAME_POSIX;
666 	fn->type.ea.packed_ea_size = ea_size;
667 	if (S_ISDIR(mode)) {
668 		fn->file_attributes = FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT;
669 		fn->allocated_size = fn->data_size = 0;
670 	} else {
671 		fn->data_size = cpu_to_le64(ni->data_size);
672 		fn->allocated_size = cpu_to_le64(ni->allocated_size);
673 	}
674 	if (!S_ISREG(mode) && !S_ISDIR(mode)) {
675 		fn->file_attributes = FILE_ATTR_SYSTEM;
676 		if (rollback_reparse)
677 			fn->file_attributes |= FILE_ATTR_REPARSE_POINT;
678 	}
679 	if (NVolHideDotFiles(vol) && name_len > 0 && name[0] == cpu_to_le16('.'))
680 		fn->file_attributes |= FILE_ATTR_HIDDEN;
681 	fn->creation_time = fn->last_data_change_time = utc2ntfs(ni->i_crtime);
682 	fn->last_mft_change_time = fn->last_access_time = fn->creation_time;
683 	memcpy(fn->file_name, name, name_len * sizeof(__le16));
684 
685 	/* Add FILE_NAME attribute to inode. */
686 	err = ntfs_attr_add(ni, AT_FILE_NAME, AT_UNNAMED, 0, (u8 *)fn, fn_len);
687 	if (err) {
688 		ntfs_error(sb, "Failed to add FILE_NAME attribute.\n");
689 		goto err_out;
690 	}
691 
692 	child_mft_ref = MK_MREF(ni->mft_no,
693 				le16_to_cpu(ni_mrec->sequence_number));
694 	/* Set hard links count and directory flag. */
695 	ni_mrec->link_count = cpu_to_le16(1);
696 	mark_mft_record_dirty(ni);
697 
698 	/* Add FILE_NAME attribute to index. */
699 	err = ntfs_index_add_filename(dir_ni, fn, child_mft_ref);
700 	if (err) {
701 		ntfs_debug("Failed to add entry to the index");
702 		goto err_out;
703 	}
704 
705 	unmap_mft_record(ni);
706 	mutex_unlock(&dir_ni->mrec_lock);
707 	mutex_unlock(&ni->mrec_lock);
708 
709 	ni->flags = fn->file_attributes |
710 		    (ni->flags & FILE_ATTRIBUTE_RECALL_ON_OPEN);
711 	/* Set the sequence number. */
712 	vi->i_generation = ni->seq_no;
713 	set_nlink(vi, 1);
714 	ntfs_set_vfs_operations(vi, mode, dev);
715 
716 	/* Done! */
717 	kfree(fn);
718 	kfree(si);
719 	ntfs_debug("Done.\n");
720 	return ni;
721 
722 err_out:
723 	if (rollback_sd) {
724 		int rollback_err;
725 
726 		rollback_err = ntfs_attr_remove(ni,
727 				AT_SECURITY_DESCRIPTOR, AT_UNNAMED, 0,
728 				&rollback_sd_vi);
729 		if (rollback_err)
730 			ntfs_error(sb,
731 				"Failed to roll back security descriptor.\n");
732 	}
733 
734 	if (rollback_data) {
735 		int rollback_err;
736 
737 		rollback_err = ntfs_attr_remove(ni, AT_DATA, AT_UNNAMED,
738 				0, &rollback_data_vi);
739 		if (rollback_err)
740 			ntfs_error(sb, "Failed to roll back DATA attribute.\n");
741 	}
742 
743 	if (rollback_reparse)
744 		ntfs_delete_reparse_index(ni);
745 	/*
746 	 * Free extent MFT records (should not exist any with current
747 	 * ntfs_create implementation, but for any case if something will be
748 	 * changed in the future).
749 	 */
750 	while (ni->nr_extents != 0) {
751 		int err2;
752 
753 		err2 = ntfs_mft_record_free(ni->vol, *(ni->ext.extent_ntfs_inos));
754 		if (err2)
755 			ntfs_error(sb,
756 				"Failed to free extent MFT record. Leaving inconsistent metadata.\n");
757 		ntfs_inode_close(*(ni->ext.extent_ntfs_inos));
758 	}
759 	if (ntfs_mft_record_free(ni->vol, ni))
760 		ntfs_error(sb,
761 			"Failed to free MFT record. Leaving inconsistent metadata. Run chkdsk.\n");
762 	unmap_mft_record(ni);
763 	kfree(fn);
764 	kfree(si);
765 
766 	mutex_unlock(&dir_ni->mrec_lock);
767 	mutex_unlock(&ni->mrec_lock);
768 
769 	if (rollback_data_vi)
770 		iput(rollback_data_vi);
771 	if (rollback_sd_vi)
772 		iput(rollback_sd_vi);
773 	remove_inode_hash(vi);
774 	discard_new_inode(vi);
775 	return ERR_PTR(err);
776 }
777 
778 static int ntfs_create(const struct mnt_idmap *idmap, struct inode *dir,
779 		struct dentry *dentry, umode_t mode)
780 {
781 	struct ntfs_volume *vol = NTFS_SB(dir->i_sb);
782 	struct ntfs_inode *ni;
783 	struct inode *stream_vi = NULL;
784 	__le16 *uname, *base_name = NULL, *stream_name = NULL;
785 	int uname_len, stream_len, path_result, err;
786 
787 	if (NVolShutdown(vol))
788 		return -EIO;
789 
790 	path_result = ntfs_stream_path_names(vol, &dentry->d_name, &base_name,
791 			&uname_len, &stream_name, &stream_len);
792 	if (path_result) {
793 		if (path_result < 0)
794 			return path_result;
795 		stream_vi = ntfs_create_named_stream(idmap, dir, base_name,
796 				uname_len, stream_name, stream_len);
797 		if (IS_ERR(stream_vi)) {
798 			err = PTR_ERR(stream_vi);
799 			goto out_free_stream_names;
800 		}
801 		kmem_cache_free(ntfs_name_cache, stream_name);
802 		kmem_cache_free(ntfs_name_cache, base_name);
803 		d_instantiate(dentry, stream_vi);
804 		return 0;
805 
806 out_free_stream_names:
807 		kmem_cache_free(ntfs_name_cache, stream_name);
808 		kmem_cache_free(ntfs_name_cache, base_name);
809 		return err;
810 	}
811 
812 	uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len,
813 				  &uname, NTFS_MAX_NAME_LEN);
814 	if (uname_len < 0) {
815 		if (uname_len != -ENAMETOOLONG)
816 			ntfs_error(vol->sb, "Failed to convert name to unicode.");
817 		return uname_len;
818 	}
819 
820 	err = ntfs_check_bad_windows_name(vol, uname, uname_len);
821 	if (err) {
822 		kmem_cache_free(ntfs_name_cache, uname);
823 		return err;
824 	}
825 
826 	ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
827 
828 	ni = __ntfs_create(idmap, dir, uname, uname_len, S_IFREG | mode, 0,
829 			NULL, 0);
830 	kmem_cache_free(ntfs_name_cache, uname);
831 	if (IS_ERR(ni))
832 		return PTR_ERR(ni);
833 
834 	d_instantiate_new(dentry, VFS_I(ni));
835 
836 	return 0;
837 }
838 
839 static int ntfs_check_unlinkable_dir(struct ntfs_attr_search_ctx *ctx, struct file_name_attr *fn)
840 {
841 	int link_count;
842 	int ret;
843 	struct ntfs_inode *ni = ctx->base_ntfs_ino ? ctx->base_ntfs_ino : ctx->ntfs_ino;
844 	struct mft_record *ni_mrec = ctx->base_mrec ? ctx->base_mrec : ctx->mrec;
845 
846 	ret = ntfs_check_empty_dir(ni, ni_mrec);
847 	if (!ret || ret != -ENOTEMPTY)
848 		return ret;
849 
850 	link_count = le16_to_cpu(ni_mrec->link_count);
851 	/*
852 	 * Directory is non-empty, so we can unlink only if there is more than
853 	 * one "real" hard link, i.e. links aren't different DOS and WIN32 names
854 	 */
855 	if ((link_count == 1) ||
856 	    (link_count == 2 && fn->file_name_type == FILE_NAME_DOS)) {
857 		ret = -ENOTEMPTY;
858 		ntfs_debug("Non-empty directory without hard links\n");
859 		goto no_hardlink;
860 	}
861 
862 	ret = 0;
863 no_hardlink:
864 	return ret;
865 }
866 
867 static int ntfs_test_inode_attr(struct inode *vi, void *data)
868 {
869 	struct ntfs_inode *ni = NTFS_I(vi);
870 	u64 mft_no = (u64)(uintptr_t)data;
871 
872 	if (ni->mft_no != mft_no)
873 		return 0;
874 	if (NInoAttr(ni) || ni->nr_extents == -1)
875 		return 1;
876 	return 0;
877 }
878 
879 static void ntfs_cleanup_deleted_inode(struct ntfs_inode *ni)
880 {
881 	struct inode *attr_vi;
882 	struct super_block *sb = VFS_I(ni)->i_sb;
883 
884 	if (!NInoBeingDeleted(ni))
885 		return;
886 
887 	while ((attr_vi = ilookup5(sb, ni->mft_no, ntfs_test_inode_attr,
888 			(void *)(uintptr_t)ni->mft_no))) {
889 		struct ntfs_inode *attr_ni = NTFS_I(attr_vi);
890 
891 		if (ntfs_inode_is_named_stream(attr_ni)) {
892 			if (atomic_read(&attr_ni->stream_open_count) > 0)
893 				NInoSetStreamUnlinked(attr_ni);
894 			remove_inode_hash(attr_vi);
895 		}
896 		clear_nlink(attr_vi);
897 		iput(attr_vi);
898 	}
899 }
900 
901 /*
902  * ntfs_delete - delete file or directory from ntfs volume
903  * @ni:         ntfs inode for object to delte
904  * @dir_ni:     ntfs inode for directory in which delete object
905  * @name:       unicode name of the object to delete
906  * @name_len:   length of the name in unicode characters
907  * @need_lock:  whether mrec lock is needed or not
908  *
909  * Delete the specified name from the directory index @dir_ni and decrement
910  * the link count of the target inode @ni.
911  *
912  * Return 0 on success and -errno on error.
913  */
914 static int ntfs_delete(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,
915 		__le16 *name, u8 name_len, bool need_lock)
916 {
917 	struct ntfs_attr_search_ctx *actx = NULL;
918 	struct file_name_attr *fn = NULL;
919 	bool looking_for_dos_name = false, looking_for_win32_name = false;
920 	bool case_sensitive_match = true;
921 	bool link_count_zero = false;
922 	int err = 0;
923 	struct mft_record *ni_mrec;
924 	struct super_block *sb;
925 
926 	ntfs_debug("Entering.\n");
927 
928 	if (need_lock == true) {
929 		mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
930 		mutex_lock_nested(&dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
931 	}
932 
933 	sb = dir_ni->vol->sb;
934 
935 	if (ni->nr_extents == -1)
936 		ni = ni->ext.base_ntfs_ino;
937 	if (dir_ni->nr_extents == -1)
938 		dir_ni = dir_ni->ext.base_ntfs_ino;
939 	/*
940 	 * Search for FILE_NAME attribute with such name. If it's in POSIX or
941 	 * WIN32_AND_DOS namespace, then simply remove it from index and inode.
942 	 * If filename in DOS or in WIN32 namespace, then remove DOS name first,
943 	 * only then remove WIN32 name.
944 	 */
945 	actx = ntfs_attr_get_search_ctx(ni, NULL);
946 	if (!actx) {
947 		ntfs_error(sb, "%s, Failed to get search context", __func__);
948 		if (need_lock) {
949 			mutex_unlock(&dir_ni->mrec_lock);
950 			mutex_unlock(&ni->mrec_lock);
951 		}
952 		return -ENOMEM;
953 	}
954 search:
955 	while ((err = ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, CASE_SENSITIVE,
956 				0, NULL, 0, actx)) == 0) {
957 #ifdef DEBUG
958 		unsigned char *s;
959 #endif
960 		bool case_sensitive = IGNORE_CASE;
961 
962 		fn = (struct file_name_attr *)((u8 *)actx->attr +
963 				le16_to_cpu(actx->attr->data.resident.value_offset));
964 #ifdef DEBUG
965 		s = ntfs_attr_name_get(ni->vol, fn->file_name, fn->file_name_length);
966 		ntfs_debug("name: '%s'  type: %d  dos: %d  win32: %d case: %d\n",
967 				s, fn->file_name_type,
968 				looking_for_dos_name, looking_for_win32_name,
969 				case_sensitive_match);
970 		ntfs_attr_name_free(&s);
971 #endif
972 		if (looking_for_dos_name) {
973 			if (fn->file_name_type == FILE_NAME_DOS)
974 				break;
975 			continue;
976 		}
977 		if (looking_for_win32_name) {
978 			if  (fn->file_name_type == FILE_NAME_WIN32)
979 				break;
980 			continue;
981 		}
982 
983 		/* Ignore hard links from other directories */
984 		if (dir_ni->mft_no != MREF_LE(fn->parent_directory)) {
985 			ntfs_debug("MFT record numbers don't match (%llu != %lu)\n",
986 					dir_ni->mft_no,
987 					MREF_LE(fn->parent_directory));
988 			continue;
989 		}
990 
991 		if (fn->file_name_type == FILE_NAME_POSIX || case_sensitive_match)
992 			case_sensitive = CASE_SENSITIVE;
993 
994 		if (ntfs_names_are_equal(fn->file_name, fn->file_name_length,
995 					name, name_len, case_sensitive,
996 					ni->vol->upcase, ni->vol->upcase_len)) {
997 			if (fn->file_name_type == FILE_NAME_WIN32) {
998 				looking_for_dos_name = true;
999 				ntfs_attr_reinit_search_ctx(actx);
1000 				continue;
1001 			}
1002 			if (fn->file_name_type == FILE_NAME_DOS)
1003 				looking_for_dos_name = true;
1004 			break;
1005 		}
1006 	}
1007 	if (err) {
1008 		/*
1009 		 * If case sensitive search failed, then try once again
1010 		 * ignoring case.
1011 		 */
1012 		if (err == -ENOENT && case_sensitive_match) {
1013 			case_sensitive_match = false;
1014 			ntfs_attr_reinit_search_ctx(actx);
1015 			goto search;
1016 		}
1017 		goto err_out;
1018 	}
1019 
1020 	err = ntfs_check_unlinkable_dir(actx, fn);
1021 	if (err)
1022 		goto err_out;
1023 
1024 	err = ntfs_index_remove(dir_ni, fn, le32_to_cpu(actx->attr->data.resident.value_length));
1025 	if (err)
1026 		goto err_out;
1027 
1028 	err = ntfs_attr_record_rm(actx);
1029 	if (err)
1030 		goto err_out;
1031 
1032 	ni_mrec = actx->base_mrec ? actx->base_mrec : actx->mrec;
1033 	ni_mrec->link_count = cpu_to_le16(le16_to_cpu(ni_mrec->link_count) - 1);
1034 	if (!S_ISDIR(VFS_I(ni)->i_mode))
1035 		drop_nlink(VFS_I(ni));
1036 
1037 	mark_mft_record_dirty(ni);
1038 	if (looking_for_dos_name) {
1039 		looking_for_dos_name = false;
1040 		looking_for_win32_name = true;
1041 		ntfs_attr_reinit_search_ctx(actx);
1042 		goto search;
1043 	}
1044 
1045 	/*
1046 	 * For directories, Drop VFS nlink only when mft record link count
1047 	 * becomes zero. Because we fixes VFS nlink to 1 for directories.
1048 	 */
1049 	if (S_ISDIR(VFS_I(ni)->i_mode) && !le16_to_cpu(ni_mrec->link_count))
1050 		drop_nlink(VFS_I(ni));
1051 
1052 	/*
1053 	 * If hard link count is not equal to zero then we are done. In other
1054 	 * case there are no reference to this inode left, so we should free all
1055 	 * non-resident attributes and mark all MFT record as not in use.
1056 	 */
1057 	if (ni_mrec->link_count == 0) {
1058 		NInoSetBeingDeleted(ni);
1059 		ntfs_delete_reparse_index(ni);
1060 		ntfs_delete_object_id_index(ni);
1061 		link_count_zero = true;
1062 	}
1063 
1064 	ntfs_attr_put_search_ctx(actx);
1065 	if (need_lock == true) {
1066 		mutex_unlock(&dir_ni->mrec_lock);
1067 		mutex_unlock(&ni->mrec_lock);
1068 		if (link_count_zero)
1069 			ntfs_cleanup_deleted_inode(ni);
1070 	}
1071 
1072 	ntfs_debug("Done.\n");
1073 	return 0;
1074 err_out:
1075 	ntfs_attr_put_search_ctx(actx);
1076 	if (need_lock) {
1077 		mutex_unlock(&dir_ni->mrec_lock);
1078 		mutex_unlock(&ni->mrec_lock);
1079 	}
1080 	return err;
1081 }
1082 
1083 static int ntfs_unlink(struct inode *dir, struct dentry *dentry)
1084 {
1085 	struct inode *vi = dentry->d_inode;
1086 	struct super_block *sb = dir->i_sb;
1087 	struct ntfs_volume *vol = NTFS_SB(sb);
1088 	int err = 0;
1089 	struct ntfs_inode *ni = NTFS_I(vi);
1090 	__le16 *uname = NULL;
1091 	int uname_len;
1092 
1093 	if (NVolShutdown(vol))
1094 		return -EIO;
1095 
1096 	if (ntfs_stream_path_has_colon(vol, &dentry->d_name))
1097 		return ntfs_unlink_named_stream(dir, dentry);
1098 	if (NInoAttr(ni))
1099 		return -EOPNOTSUPP;
1100 
1101 	uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len,
1102 				  &uname, NTFS_MAX_NAME_LEN);
1103 	if (uname_len < 0) {
1104 		if (uname_len != -ENAMETOOLONG)
1105 			ntfs_error(sb, "Failed to convert name to Unicode.");
1106 		return -ENOMEM;
1107 	}
1108 
1109 	err = ntfs_check_bad_windows_name(vol, uname, uname_len);
1110 	if (err) {
1111 		kmem_cache_free(ntfs_name_cache, uname);
1112 		return err;
1113 	}
1114 
1115 	ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1116 
1117 	err = ntfs_delete(ni, NTFS_I(dir), uname, uname_len, true);
1118 	if (err)
1119 		goto out;
1120 
1121 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
1122 	mark_inode_dirty(dir);
1123 	inode_set_ctime_to_ts(vi, inode_get_ctime(dir));
1124 	if (vi->i_nlink)
1125 		mark_inode_dirty(vi);
1126 out:
1127 	kmem_cache_free(ntfs_name_cache, uname);
1128 	return err;
1129 }
1130 
1131 static struct dentry *ntfs_mkdir(const struct mnt_idmap *idmap, struct inode *dir,
1132 		struct dentry *dentry, umode_t mode)
1133 {
1134 	struct super_block *sb = dir->i_sb;
1135 	struct ntfs_volume *vol = NTFS_SB(sb);
1136 	int err = 0;
1137 	struct ntfs_inode *ni;
1138 	__le16 *uname;
1139 	int uname_len;
1140 
1141 	if (NVolShutdown(vol))
1142 		return ERR_PTR(-EIO);
1143 
1144 	err = ntfs_stream_path_check(vol, &dentry->d_name);
1145 	if (err)
1146 		return ERR_PTR(err);
1147 
1148 	uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len,
1149 				  &uname, NTFS_MAX_NAME_LEN);
1150 	if (uname_len < 0) {
1151 		if (uname_len != -ENAMETOOLONG)
1152 			ntfs_error(sb, "Failed to convert name to unicode.");
1153 		return ERR_PTR(-ENOMEM);
1154 	}
1155 
1156 	err = ntfs_check_bad_windows_name(vol, uname, uname_len);
1157 	if (err) {
1158 		kmem_cache_free(ntfs_name_cache, uname);
1159 		return ERR_PTR(err);
1160 	}
1161 
1162 	ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1163 
1164 	ni = __ntfs_create(idmap, dir, uname, uname_len, mode, 0,
1165 			NULL, 0);
1166 	kmem_cache_free(ntfs_name_cache, uname);
1167 	if (IS_ERR(ni)) {
1168 		err = PTR_ERR(ni);
1169 		return ERR_PTR(err);
1170 	}
1171 
1172 	d_instantiate_new(dentry, VFS_I(ni));
1173 	return NULL;
1174 }
1175 
1176 static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)
1177 {
1178 	struct inode *vi = dentry->d_inode;
1179 	struct super_block *sb = dir->i_sb;
1180 	struct ntfs_volume *vol = NTFS_SB(sb);
1181 	int err = 0;
1182 	struct ntfs_inode *ni;
1183 	__le16 *uname = NULL;
1184 	int uname_len;
1185 
1186 	if (NVolShutdown(vol))
1187 		return -EIO;
1188 
1189 	err = ntfs_stream_path_check(vol, &dentry->d_name);
1190 	if (err)
1191 		return err;
1192 
1193 	ni = NTFS_I(vi);
1194 	uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len,
1195 				  &uname, NTFS_MAX_NAME_LEN);
1196 	if (uname_len < 0) {
1197 		if (uname_len != -ENAMETOOLONG)
1198 			ntfs_error(sb, "Failed to convert name to unicode.");
1199 		return -ENOMEM;
1200 	}
1201 
1202 	err = ntfs_check_bad_windows_name(vol, uname, uname_len);
1203 	if (err) {
1204 		kmem_cache_free(ntfs_name_cache, uname);
1205 		return err;
1206 	}
1207 
1208 	ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1209 
1210 	err = ntfs_delete(ni, NTFS_I(dir), uname, uname_len, true);
1211 	if (err)
1212 		goto out;
1213 
1214 	inode_set_mtime_to_ts(vi, inode_set_atime_to_ts(vi, current_time(vi)));
1215 out:
1216 	kmem_cache_free(ntfs_name_cache, uname);
1217 	return err;
1218 }
1219 
1220 /*
1221  * __ntfs_link - create hard link for file or directory
1222  * @ni:		ntfs inode for object to create hard link
1223  * @dir_ni:	ntfs inode for directory in which new link should be placed
1224  * @name:	unicode name of the new link
1225  * @name_len:	length of the name in unicode characters
1226  *
1227  * Create a new hard link. This involves adding an entry to the directory
1228  * index and adding a new FILE_NAME attribute to the target inode.
1229  *
1230  * Return 0 on success and -errno on error.
1231  */
1232 static int __ntfs_link(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,
1233 		__le16 *name, u8 name_len)
1234 {
1235 	struct super_block *sb;
1236 	struct inode *vi = VFS_I(ni);
1237 	struct file_name_attr *fn = NULL;
1238 	int fn_len, err = 0;
1239 	struct mft_record *dir_mrec = NULL, *ni_mrec = NULL;
1240 
1241 	ntfs_debug("Entering.\n");
1242 
1243 	sb = dir_ni->vol->sb;
1244 	if (NInoBeingDeleted(dir_ni) || NInoBeingDeleted(ni))
1245 		return -ENOENT;
1246 
1247 	ni_mrec = map_mft_record(ni);
1248 	if (IS_ERR(ni_mrec)) {
1249 		err = -EIO;
1250 		goto err_out;
1251 	}
1252 
1253 	if (le16_to_cpu(ni_mrec->link_count) == 0) {
1254 		err = -ENOENT;
1255 		goto err_out;
1256 	}
1257 
1258 	/* Create FILE_NAME attribute. */
1259 	fn_len = sizeof(struct file_name_attr) + name_len * sizeof(__le16);
1260 
1261 	fn = kzalloc(fn_len, GFP_NOFS);
1262 	if (!fn) {
1263 		err = -ENOMEM;
1264 		goto err_out;
1265 	}
1266 
1267 	dir_mrec = map_mft_record(dir_ni);
1268 	if (IS_ERR(dir_mrec)) {
1269 		err = -EIO;
1270 		goto err_out;
1271 	}
1272 
1273 	fn->parent_directory = MK_LE_MREF(dir_ni->mft_no,
1274 			le16_to_cpu(dir_mrec->sequence_number));
1275 	unmap_mft_record(dir_ni);
1276 	fn->file_name_length = name_len;
1277 	fn->file_name_type = FILE_NAME_POSIX;
1278 	fn->file_attributes = ni->flags;
1279 	if (ni_mrec->flags & MFT_RECORD_IS_DIRECTORY) {
1280 		fn->file_attributes |= FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT;
1281 		fn->allocated_size = fn->data_size = 0;
1282 	} else {
1283 		if (NInoSparse(ni) || NInoCompressed(ni))
1284 			fn->allocated_size =
1285 				cpu_to_le64(ni->itype.compressed.size);
1286 		else
1287 			fn->allocated_size = cpu_to_le64(ni->allocated_size);
1288 		fn->data_size = cpu_to_le64(ni->data_size);
1289 	}
1290 	if (NVolHideDotFiles(dir_ni->vol) && name_len > 0 && name[0] == cpu_to_le16('.'))
1291 		fn->file_attributes |= FILE_ATTR_HIDDEN;
1292 
1293 	fn->creation_time = utc2ntfs(ni->i_crtime);
1294 	fn->last_data_change_time = utc2ntfs(inode_get_mtime(vi));
1295 	fn->last_mft_change_time = utc2ntfs(inode_get_ctime(vi));
1296 	fn->last_access_time = utc2ntfs(inode_get_atime(vi));
1297 	memcpy(fn->file_name, name, name_len * sizeof(__le16));
1298 
1299 	/* Add FILE_NAME attribute to index. */
1300 	err = ntfs_index_add_filename(dir_ni, fn, MK_MREF(ni->mft_no,
1301 					le16_to_cpu(ni_mrec->sequence_number)));
1302 	if (err) {
1303 		ntfs_error(sb, "Failed to add filename to the index");
1304 		goto err_out;
1305 	}
1306 	/* Add FILE_NAME attribute to inode. */
1307 	err = ntfs_attr_add(ni, AT_FILE_NAME, AT_UNNAMED, 0, (u8 *)fn, fn_len);
1308 	if (err) {
1309 		ntfs_error(sb, "Failed to add FILE_NAME attribute.\n");
1310 		/* Try to remove just added attribute from index. */
1311 		if (ntfs_index_remove(dir_ni, fn, fn_len))
1312 			goto rollback_failed;
1313 		goto err_out;
1314 	}
1315 	/* Increment hard links count. */
1316 	ni_mrec->link_count = cpu_to_le16(le16_to_cpu(ni_mrec->link_count) + 1);
1317 	if (!S_ISDIR(vi->i_mode))
1318 		inc_nlink(VFS_I(ni));
1319 
1320 	/* Done! */
1321 	mark_mft_record_dirty(ni);
1322 	kfree(fn);
1323 	unmap_mft_record(ni);
1324 
1325 	ntfs_debug("Done.\n");
1326 
1327 	return 0;
1328 rollback_failed:
1329 	ntfs_error(sb, "Rollback failed. Leaving inconsistent metadata.\n");
1330 err_out:
1331 	kfree(fn);
1332 	if (!IS_ERR_OR_NULL(ni_mrec))
1333 		unmap_mft_record(ni);
1334 	return err;
1335 }
1336 
1337 static int ntfs_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
1338 		struct dentry *old_dentry, struct inode *new_dir,
1339 		struct dentry *new_dentry, unsigned int flags)
1340 {
1341 	struct inode *old_inode, *new_inode = NULL;
1342 	int err = 0;
1343 	int is_dir;
1344 	struct super_block *sb = old_dir->i_sb;
1345 	__le16 *uname_new = NULL;
1346 	__le16 *uname_old = NULL;
1347 	int new_name_len;
1348 	int old_name_len;
1349 	struct ntfs_volume *vol = NTFS_SB(sb);
1350 	struct ntfs_inode *old_ni, *new_ni = NULL;
1351 	struct ntfs_inode *old_dir_ni = NTFS_I(old_dir), *new_dir_ni = NTFS_I(new_dir);
1352 	bool new_dir_first = false;
1353 
1354 	if (NVolShutdown(old_dir_ni->vol))
1355 		return -EIO;
1356 
1357 	err = ntfs_stream_path_check(vol, &old_dentry->d_name);
1358 	if (err)
1359 		return err;
1360 	err = ntfs_stream_path_check(vol, &new_dentry->d_name);
1361 	if (err)
1362 		return err;
1363 
1364 	if (flags & (RENAME_EXCHANGE | RENAME_WHITEOUT))
1365 		return -EINVAL;
1366 
1367 	new_name_len = ntfs_nlstoucs(NTFS_I(new_dir)->vol, new_dentry->d_name.name,
1368 				     new_dentry->d_name.len, &uname_new,
1369 				     NTFS_MAX_NAME_LEN);
1370 	if (new_name_len < 0) {
1371 		if (new_name_len != -ENAMETOOLONG)
1372 			ntfs_error(sb, "Failed to convert name to unicode.");
1373 		return -ENOMEM;
1374 	}
1375 
1376 	err = ntfs_check_bad_windows_name(vol, uname_new, new_name_len);
1377 	if (err) {
1378 		kmem_cache_free(ntfs_name_cache, uname_new);
1379 		return err;
1380 	}
1381 
1382 	old_name_len = ntfs_nlstoucs(NTFS_I(old_dir)->vol, old_dentry->d_name.name,
1383 				     old_dentry->d_name.len, &uname_old,
1384 				     NTFS_MAX_NAME_LEN);
1385 	if (old_name_len < 0) {
1386 		kmem_cache_free(ntfs_name_cache, uname_new);
1387 		if (old_name_len != -ENAMETOOLONG)
1388 			ntfs_error(sb, "Failed to convert name to unicode.");
1389 		return -ENOMEM;
1390 	}
1391 
1392 	old_inode = old_dentry->d_inode;
1393 	new_inode = new_dentry->d_inode;
1394 	old_ni = NTFS_I(old_inode);
1395 	if (new_inode)
1396 		new_ni = NTFS_I(new_inode);
1397 	if (old_dir != new_dir)
1398 		new_dir_first = is_subdir(new_dentry->d_parent,
1399 					  old_dentry->d_parent);
1400 
1401 	ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1402 
1403 	mutex_lock_nested(&old_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
1404 	if (new_ni)
1405 		mutex_lock_nested(&new_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL_2);
1406 
1407 	if (old_dir == new_dir) {
1408 		mutex_lock_nested(&old_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
1409 	} else if (new_dir_first) {
1410 		mutex_lock_nested(&new_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
1411 		mutex_lock_nested(&old_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT_2);
1412 	} else {
1413 		mutex_lock_nested(&old_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
1414 		mutex_lock_nested(&new_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT_2);
1415 	}
1416 
1417 	if (NInoBeingDeleted(old_ni) || NInoBeingDeleted(old_dir_ni) ||
1418 	    (new_ni && NInoBeingDeleted(new_ni)) ||
1419 	    (old_dir != new_dir && NInoBeingDeleted(new_dir_ni))) {
1420 		err = -ENOENT;
1421 		goto err_out;
1422 	}
1423 
1424 	is_dir = S_ISDIR(old_inode->i_mode);
1425 
1426 	if (new_inode) {
1427 		if (is_dir) {
1428 			struct mft_record *ni_mrec;
1429 
1430 			ni_mrec = map_mft_record(NTFS_I(new_inode));
1431 			if (IS_ERR(ni_mrec)) {
1432 				err = -EIO;
1433 				goto err_out;
1434 			}
1435 			err = ntfs_check_empty_dir(NTFS_I(new_inode), ni_mrec);
1436 			unmap_mft_record(NTFS_I(new_inode));
1437 			if (err)
1438 				goto err_out;
1439 		}
1440 
1441 		err = ntfs_delete(new_ni, new_dir_ni, uname_new, new_name_len, false);
1442 		if (err)
1443 			goto err_out;
1444 	}
1445 
1446 	err = __ntfs_link(old_ni, new_dir_ni, uname_new, new_name_len);
1447 	if (err)
1448 		goto err_out;
1449 
1450 	err = ntfs_delete(old_ni, old_dir_ni, uname_old, old_name_len, false);
1451 	if (err) {
1452 		int err2;
1453 
1454 		ntfs_error(sb, "Failed to delete old ntfs inode(%llu) in old dir, err : %d\n",
1455 				old_ni->mft_no, err);
1456 		err2 = ntfs_delete(old_ni, new_dir_ni, uname_new, new_name_len, false);
1457 		if (err2)
1458 			ntfs_error(sb, "Failed to delete old ntfs inode in new dir, err : %d\n",
1459 					err2);
1460 		goto err_out;
1461 	}
1462 
1463 	simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry);
1464 	mark_inode_dirty(old_inode);
1465 	mark_inode_dirty(old_dir);
1466 	if (old_dir != new_dir)
1467 		mark_inode_dirty(new_dir);
1468 	if (new_inode)
1469 		mark_inode_dirty(old_inode);
1470 
1471 	inode_inc_iversion(new_dir);
1472 
1473 err_out:
1474 	if (old_dir == new_dir) {
1475 		mutex_unlock(&old_dir_ni->mrec_lock);
1476 	} else if (new_dir_first) {
1477 		mutex_unlock(&old_dir_ni->mrec_lock);
1478 		mutex_unlock(&new_dir_ni->mrec_lock);
1479 	} else {
1480 		mutex_unlock(&new_dir_ni->mrec_lock);
1481 		mutex_unlock(&old_dir_ni->mrec_lock);
1482 	}
1483 	if (new_ni)
1484 		mutex_unlock(&new_ni->mrec_lock);
1485 	mutex_unlock(&old_ni->mrec_lock);
1486 	if (new_ni)
1487 		ntfs_cleanup_deleted_inode(new_ni);
1488 	if (uname_new)
1489 		kmem_cache_free(ntfs_name_cache, uname_new);
1490 	if (uname_old)
1491 		kmem_cache_free(ntfs_name_cache, uname_old);
1492 
1493 	return err;
1494 }
1495 
1496 static int ntfs_symlink(const struct mnt_idmap *idmap, struct inode *dir,
1497 		struct dentry *dentry, const char *symname)
1498 {
1499 	struct super_block *sb = dir->i_sb;
1500 	struct ntfs_volume *vol = NTFS_SB(sb);
1501 	struct inode *vi;
1502 	int err = 0;
1503 	struct ntfs_inode *ni;
1504 	__le16 *usrc;
1505 	int usrc_len;
1506 	int symlen = strlen(symname);
1507 
1508 	if (NVolShutdown(vol))
1509 		return -EIO;
1510 
1511 	err = ntfs_stream_path_check(vol, &dentry->d_name);
1512 	if (err)
1513 		return err;
1514 
1515 	usrc_len = ntfs_nlstoucs(vol, dentry->d_name.name,
1516 				 dentry->d_name.len, &usrc, NTFS_MAX_NAME_LEN);
1517 	if (usrc_len < 0) {
1518 		if (usrc_len != -ENAMETOOLONG)
1519 			ntfs_error(sb, "Failed to convert name to Unicode.");
1520 		err =  -ENOMEM;
1521 		goto out;
1522 	}
1523 
1524 	err = ntfs_check_bad_windows_name(vol, usrc, usrc_len);
1525 	if (err) {
1526 		kmem_cache_free(ntfs_name_cache, usrc);
1527 		goto out;
1528 	}
1529 
1530 	ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1531 
1532 	ni = __ntfs_create(idmap, dir, usrc, usrc_len, S_IFLNK | 0777, 0,
1533 			   symname, symlen);
1534 	kmem_cache_free(ntfs_name_cache, usrc);
1535 	if (IS_ERR(ni)) {
1536 		err = PTR_ERR(ni);
1537 		goto out;
1538 	}
1539 
1540 	vi = VFS_I(ni);
1541 	vi->i_size = symlen;
1542 	d_instantiate_new(dentry, vi);
1543 out:
1544 	return err;
1545 }
1546 
1547 static int ntfs_mknod(const struct mnt_idmap *idmap, struct inode *dir,
1548 		struct dentry *dentry, umode_t mode, dev_t rdev)
1549 {
1550 	struct super_block *sb = dir->i_sb;
1551 	struct ntfs_volume *vol = NTFS_SB(sb);
1552 	int err = 0;
1553 	struct ntfs_inode *ni;
1554 	__le16 *uname = NULL;
1555 	int uname_len;
1556 
1557 	if (NVolShutdown(vol))
1558 		return -EIO;
1559 
1560 	err = ntfs_stream_path_check(vol, &dentry->d_name);
1561 	if (err)
1562 		return err;
1563 
1564 	uname_len = ntfs_nlstoucs(vol, dentry->d_name.name,
1565 			dentry->d_name.len, &uname, NTFS_MAX_NAME_LEN);
1566 	if (uname_len < 0) {
1567 		if (uname_len != -ENAMETOOLONG)
1568 			ntfs_error(sb, "Failed to convert name to Unicode.");
1569 		return -ENOMEM;
1570 	}
1571 
1572 	err = ntfs_check_bad_windows_name(vol, uname, uname_len);
1573 	if (err) {
1574 		kmem_cache_free(ntfs_name_cache, uname);
1575 		return err;
1576 	}
1577 
1578 	ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1579 
1580 	switch (mode & S_IFMT) {
1581 	case S_IFCHR:
1582 	case S_IFBLK:
1583 		ni = __ntfs_create(idmap, dir, uname, uname_len,
1584 				mode, rdev, NULL, 0);
1585 		break;
1586 	default:
1587 		ni = __ntfs_create(idmap, dir, uname, uname_len,
1588 				mode, 0, NULL, 0);
1589 	}
1590 
1591 	kmem_cache_free(ntfs_name_cache, uname);
1592 	if (IS_ERR(ni)) {
1593 		err = PTR_ERR(ni);
1594 		goto out;
1595 	}
1596 
1597 	d_instantiate_new(dentry, VFS_I(ni));
1598 out:
1599 	return err;
1600 }
1601 
1602 static int ntfs_link(struct dentry *old_dentry, struct inode *dir,
1603 		struct dentry *dentry)
1604 {
1605 	struct inode *vi = old_dentry->d_inode;
1606 	struct super_block *sb = vi->i_sb;
1607 	struct ntfs_volume *vol = NTFS_SB(sb);
1608 	__le16 *uname = NULL;
1609 	int uname_len;
1610 	int err;
1611 	struct ntfs_inode *ni = NTFS_I(vi), *dir_ni = NTFS_I(dir);
1612 
1613 	if (NVolShutdown(vol))
1614 		return -EIO;
1615 
1616 	if (NInoAttr(ni))
1617 		return -EOPNOTSUPP;
1618 
1619 	err = ntfs_stream_path_check(vol, &dentry->d_name);
1620 	if (err)
1621 		return err;
1622 
1623 	uname_len = ntfs_nlstoucs(vol, dentry->d_name.name,
1624 			dentry->d_name.len, &uname, NTFS_MAX_NAME_LEN);
1625 	if (uname_len < 0) {
1626 		if (uname_len != -ENAMETOOLONG)
1627 			ntfs_error(sb, "Failed to convert name to unicode.");
1628 		return -ENOMEM;
1629 	}
1630 
1631 	ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1632 
1633 	ihold(vi);
1634 	mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
1635 	mutex_lock_nested(&dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
1636 	err = __ntfs_link(NTFS_I(vi), NTFS_I(dir), uname, uname_len);
1637 	if (err) {
1638 		mutex_unlock(&dir_ni->mrec_lock);
1639 		mutex_unlock(&ni->mrec_lock);
1640 		iput(vi);
1641 		pr_err("failed to create link, err = %d\n", err);
1642 		goto out;
1643 	}
1644 
1645 	inode_inc_iversion(dir);
1646 	simple_inode_init_ts(dir);
1647 
1648 	inode_inc_iversion(vi);
1649 	simple_inode_init_ts(vi);
1650 
1651 	/* timestamp is already written, so mark_inode_dirty() is unneeded. */
1652 	d_instantiate(dentry, vi);
1653 	mutex_unlock(&dir_ni->mrec_lock);
1654 	mutex_unlock(&ni->mrec_lock);
1655 
1656 out:
1657 	kmem_cache_free(ntfs_name_cache, uname);
1658 	return err;
1659 }
1660 
1661 /*
1662  * Inode operations for directories.
1663  */
1664 const struct inode_operations ntfs_dir_inode_ops = {
1665 	.lookup		= ntfs_lookup,	/* VFS: Lookup directory. */
1666 	.create		= ntfs_create,
1667 	.unlink		= ntfs_unlink,
1668 	.mkdir		= ntfs_mkdir,
1669 	.rmdir		= ntfs_rmdir,
1670 	.rename		= ntfs_rename,
1671 	.get_acl	= ntfs_get_acl,
1672 	.set_acl	= ntfs_set_acl,
1673 	.listxattr	= ntfs_listxattr,
1674 	.setattr	= ntfs_setattr,
1675 	.getattr	= ntfs_getattr,
1676 	.fileattr_get	= ntfs_fileattr_get,
1677 	.fileattr_set	= ntfs_fileattr_set,
1678 	.symlink	= ntfs_symlink,
1679 	.mknod		= ntfs_mknod,
1680 	.link		= ntfs_link,
1681 };
1682 
1683 /*
1684  * ntfs_get_parent - find the dentry of the parent of a given directory dentry
1685  * @child_dent:		dentry of the directory whose parent directory to find
1686  *
1687  * Find the dentry for the parent directory of the directory specified by the
1688  * dentry @child_dent.  This function is called from
1689  * fs/exportfs/expfs.c::find_exported_dentry() which in turn is called from the
1690  * default ->decode_fh() which is export_decode_fh() in the same file.
1691  *
1692  * Note: ntfs_get_parent() is called with @d_inode(child_dent)->i_mutex down.
1693  *
1694  * Return the dentry of the parent directory on success or the error code on
1695  * error (IS_ERR() is true).
1696  */
1697 static struct dentry *ntfs_get_parent(struct dentry *child_dent)
1698 {
1699 	struct inode *vi = d_inode(child_dent);
1700 	struct ntfs_inode *ni = NTFS_I(vi);
1701 	struct mft_record *mrec;
1702 	struct ntfs_attr_search_ctx *ctx;
1703 	struct attr_record *attr;
1704 	struct file_name_attr *fn;
1705 	unsigned long parent_ino;
1706 	int err;
1707 
1708 	ntfs_debug("Entering for inode 0x%llx.", ni->mft_no);
1709 	/* Get the mft record of the inode belonging to the child dentry. */
1710 	mrec = map_mft_record(ni);
1711 	if (IS_ERR(mrec))
1712 		return ERR_CAST(mrec);
1713 	/* Find the first file name attribute in the mft record. */
1714 	ctx = ntfs_attr_get_search_ctx(ni, mrec);
1715 	if (unlikely(!ctx)) {
1716 		unmap_mft_record(ni);
1717 		return ERR_PTR(-ENOMEM);
1718 	}
1719 try_next:
1720 	err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, CASE_SENSITIVE, 0, NULL,
1721 			0, ctx);
1722 	if (unlikely(err)) {
1723 		ntfs_attr_put_search_ctx(ctx);
1724 		unmap_mft_record(ni);
1725 		if (err == -ENOENT)
1726 			ntfs_error(vi->i_sb,
1727 				   "Inode 0x%llx does not have a file name attribute.  Run chkdsk.",
1728 				   ni->mft_no);
1729 		return ERR_PTR(err);
1730 	}
1731 	attr = ctx->attr;
1732 	if (unlikely(attr->non_resident))
1733 		goto try_next;
1734 	fn = (struct file_name_attr *)((u8 *)attr +
1735 			le16_to_cpu(attr->data.resident.value_offset));
1736 	if (unlikely((u8 *)fn + le32_to_cpu(attr->data.resident.value_length) >
1737 	    (u8 *)attr + le32_to_cpu(attr->length)))
1738 		goto try_next;
1739 	/* Get the inode number of the parent directory. */
1740 	parent_ino = MREF_LE(fn->parent_directory);
1741 	/* Release the search context and the mft record of the child. */
1742 	ntfs_attr_put_search_ctx(ctx);
1743 	unmap_mft_record(ni);
1744 
1745 	return d_obtain_alias(ntfs_iget(vi->i_sb, parent_ino));
1746 }
1747 
1748 static struct inode *ntfs_nfs_get_inode(struct super_block *sb,
1749 		u64 ino, u32 generation)
1750 {
1751 	struct inode *inode;
1752 
1753 	inode = ntfs_iget(sb, ino);
1754 	if (!IS_ERR(inode)) {
1755 		if (inode->i_generation != generation) {
1756 			iput(inode);
1757 			inode = ERR_PTR(-ESTALE);
1758 		}
1759 	}
1760 
1761 	return inode;
1762 }
1763 
1764 static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1765 		int fh_len, int fh_type)
1766 {
1767 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1768 				    ntfs_nfs_get_inode);
1769 }
1770 
1771 static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
1772 		int fh_len, int fh_type)
1773 {
1774 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1775 				    ntfs_nfs_get_inode);
1776 }
1777 
1778 static int ntfs_encode_fh(struct inode *inode, u32 *fh, int *max_len,
1779 		struct inode *parent)
1780 {
1781 	if (ntfs_inode_is_named_stream(NTFS_I(inode)))
1782 		return -EOPNOTSUPP;
1783 	return generic_encode_ino32_fh(inode, fh, max_len, parent);
1784 }
1785 
1786 /*
1787  * Export operations allowing NFS exporting of mounted NTFS partitions.
1788  */
1789 const struct export_operations ntfs_export_ops = {
1790 	.encode_fh	= ntfs_encode_fh,
1791 	.get_parent	= ntfs_get_parent,	/* Find the parent of a given directory. */
1792 	.fh_to_dentry	= ntfs_fh_to_dentry,
1793 	.fh_to_parent	= ntfs_fh_to_parent,
1794 };
1795