xref: /linux/fs/ntfs/namei.c (revision f09c78bd6242599a3e6ebb6c88b1d857cc981147)
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 			goto err_out;
477 	} else
478 #endif
479 	{
480 		vi->i_flags |= S_NOSEC;
481 	}
482 
483 	if (uid_valid(vol->uid))
484 		vi->i_uid = vol->uid;
485 
486 	if (gid_valid(vol->gid))
487 		vi->i_gid = vol->gid;
488 
489 	/*
490 	 * Set the file size to 0, the ntfs inode sizes are set to 0 by
491 	 * the call to ntfs_init_big_inode() below.
492 	 */
493 	vi->i_size = 0;
494 	vi->i_blocks = 0;
495 
496 	inode_inc_iversion(vi);
497 
498 	simple_inode_init_ts(vi);
499 	ni->i_crtime = inode_get_ctime(vi);
500 
501 	inode_set_mtime_to_ts(dir, ni->i_crtime);
502 	inode_set_ctime_to_ts(dir, ni->i_crtime);
503 	mark_inode_dirty(dir);
504 
505 	err = ntfs_mft_record_alloc(dir_ni->vol, mode, &ni, NULL,
506 			&ni_mrec, -1);
507 	if (err) {
508 		iput(vi);
509 		return ERR_PTR(err);
510 	}
511 
512 	/*
513 	 * Prevent iget and writeback from finding this inode.
514 	 * Caller must call d_instantiate_new instead of d_instantiate.
515 	 */
516 	spin_lock(&vi->i_lock);
517 	inode_state_set(vi, I_NEW | I_CREATING);
518 	spin_unlock(&vi->i_lock);
519 
520 	/* Add the inode to the inode hash for the superblock. */
521 	vi->i_ino = (unsigned long)ni->mft_no;
522 	inode_set_iversion(vi, 1);
523 	insert_inode_hash(vi);
524 
525 	mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
526 	mutex_lock_nested(&dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
527 	if (NInoBeingDeleted(dir_ni)) {
528 		err = -ENOENT;
529 		goto err_out;
530 	}
531 
532 	dni_mrec = map_mft_record(dir_ni);
533 	if (IS_ERR(dni_mrec)) {
534 		ntfs_error(dir_ni->vol->sb, "failed to map mft record for file 0x%llx.\n",
535 			   dir_ni->mft_no);
536 		err = -EIO;
537 		goto err_out;
538 	}
539 	parent_mft_ref = MK_LE_MREF(dir_ni->mft_no,
540 				    le16_to_cpu(dni_mrec->sequence_number));
541 	unmap_mft_record(dir_ni);
542 
543 	/*
544 	 * Create STANDARD_INFORMATION attribute. Write STANDARD_INFORMATION
545 	 * version 1.2, windows will upgrade it to version 3 if needed.
546 	 */
547 	si_len = offsetof(struct standard_information, file_attributes) +
548 		sizeof(__le32) + 12;
549 	si = kzalloc(si_len, GFP_NOFS);
550 	if (!si) {
551 		err = -ENOMEM;
552 		goto err_out;
553 	}
554 
555 	si->creation_time = si->last_data_change_time = utc2ntfs(ni->i_crtime);
556 	si->last_mft_change_time = si->last_access_time = si->creation_time;
557 
558 	if (!S_ISREG(mode) && !S_ISDIR(mode))
559 		si->file_attributes = FILE_ATTR_SYSTEM;
560 
561 	/* Add STANDARD_INFORMATION to inode. */
562 	err = ntfs_attr_add(ni, AT_STANDARD_INFORMATION, AT_UNNAMED, 0, (u8 *)si,
563 			si_len);
564 	if (err) {
565 		ntfs_error(sb, "Failed to add STANDARD_INFORMATION attribute.\n");
566 		goto err_out;
567 	}
568 
569 	err = ntfs_sd_add_everyone(ni);
570 	if (err)
571 		goto err_out;
572 	rollback_sd = true;
573 
574 	if (S_ISDIR(mode)) {
575 		struct index_root *ir = NULL;
576 		struct index_entry *ie;
577 		int ir_len, index_len;
578 
579 		/* Create struct index_root attribute. */
580 		index_len = sizeof(struct index_header) + sizeof(struct index_entry_header);
581 		ir_len = offsetof(struct index_root, index) + index_len;
582 		ir = kzalloc(ir_len, GFP_NOFS);
583 		if (!ir) {
584 			err = -ENOMEM;
585 			goto err_out;
586 		}
587 		ir->type = AT_FILE_NAME;
588 		ir->collation_rule = COLLATION_FILE_NAME;
589 		ir->index_block_size = cpu_to_le32(ni->vol->index_record_size);
590 		if (ni->vol->cluster_size <= ni->vol->index_record_size)
591 			ir->clusters_per_index_block =
592 				NTFS_B_TO_CLU(vol, ni->vol->index_record_size);
593 		else
594 			ir->clusters_per_index_block =
595 				ni->vol->index_record_size >> NTFS_BLOCK_SIZE_BITS;
596 		ir->index.entries_offset = cpu_to_le32(sizeof(struct index_header));
597 		ir->index.index_length = cpu_to_le32(index_len);
598 		ir->index.allocated_size = cpu_to_le32(index_len);
599 		ie = (struct index_entry *)((u8 *)ir + sizeof(struct index_root));
600 		ie->length = cpu_to_le16(sizeof(struct index_entry_header));
601 		ie->key_length = 0;
602 		ie->flags = INDEX_ENTRY_END;
603 
604 		/* Add struct index_root attribute to inode. */
605 		err = ntfs_attr_add(ni, AT_INDEX_ROOT, I30, 4, (u8 *)ir, ir_len);
606 		if (err) {
607 			kfree(ir);
608 			ntfs_error(vi->i_sb, "Failed to add struct index_root attribute.\n");
609 			goto err_out;
610 		}
611 		kfree(ir);
612 		err = ntfs_attr_open(ni, AT_INDEX_ROOT, I30, 4);
613 		if (err)
614 			goto err_out;
615 	} else {
616 		/* Add DATA attribute to inode. */
617 		err = ntfs_attr_add(ni, AT_DATA, AT_UNNAMED, 0, NULL, 0);
618 		if (err) {
619 			ntfs_error(dir_ni->vol->sb, "Failed to add DATA attribute.\n");
620 			goto err_out;
621 		}
622 		rollback_data = true;
623 
624 		err = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0);
625 		if (err)
626 			goto err_out;
627 
628 		if (S_ISLNK(mode)) {
629 			if (NVolSymlinkNative(vol))
630 				err = ntfs_reparse_set_native_symlink(ni, target, target_len);
631 			else
632 				err = ntfs_reparse_set_wsl_symlink(ni, target, target_len);
633 			if (!err)
634 				rollback_reparse = true;
635 		} else if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode) ||
636 			   S_ISFIFO(mode)) {
637 			si->file_attributes = FILE_ATTRIBUTE_RECALL_ON_OPEN;
638 			ni->flags = FILE_ATTRIBUTE_RECALL_ON_OPEN;
639 			err = ntfs_reparse_set_wsl_not_symlink(ni, mode);
640 			if (!err)
641 				rollback_reparse = true;
642 		}
643 		if (err)
644 			goto err_out;
645 	}
646 
647 	err = ntfs_ea_set_wsl_inode(vi, dev, &ea_size,
648 			NTFS_EA_UID | NTFS_EA_GID | NTFS_EA_MODE);
649 	if (err)
650 		goto err_out;
651 
652 	/* Create FILE_NAME attribute. */
653 	fn_len = sizeof(struct file_name_attr) + name_len * sizeof(__le16);
654 	fn = kzalloc(fn_len, GFP_NOFS);
655 	if (!fn) {
656 		err = -ENOMEM;
657 		goto err_out;
658 	}
659 
660 	fn->file_attributes |= ni->flags;
661 	fn->parent_directory = parent_mft_ref;
662 	fn->file_name_length = name_len;
663 	fn->file_name_type = FILE_NAME_POSIX;
664 	fn->type.ea.packed_ea_size = ea_size;
665 	if (S_ISDIR(mode)) {
666 		fn->file_attributes = FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT;
667 		fn->allocated_size = fn->data_size = 0;
668 	} else {
669 		fn->data_size = cpu_to_le64(ni->data_size);
670 		fn->allocated_size = cpu_to_le64(ni->allocated_size);
671 	}
672 	if (!S_ISREG(mode) && !S_ISDIR(mode)) {
673 		fn->file_attributes = FILE_ATTR_SYSTEM;
674 		if (rollback_reparse)
675 			fn->file_attributes |= FILE_ATTR_REPARSE_POINT;
676 	}
677 	if (NVolHideDotFiles(vol) && name_len > 0 && name[0] == cpu_to_le16('.'))
678 		fn->file_attributes |= FILE_ATTR_HIDDEN;
679 	fn->creation_time = fn->last_data_change_time = utc2ntfs(ni->i_crtime);
680 	fn->last_mft_change_time = fn->last_access_time = fn->creation_time;
681 	memcpy(fn->file_name, name, name_len * sizeof(__le16));
682 
683 	/* Add FILE_NAME attribute to inode. */
684 	err = ntfs_attr_add(ni, AT_FILE_NAME, AT_UNNAMED, 0, (u8 *)fn, fn_len);
685 	if (err) {
686 		ntfs_error(sb, "Failed to add FILE_NAME attribute.\n");
687 		goto err_out;
688 	}
689 
690 	child_mft_ref = MK_MREF(ni->mft_no,
691 				le16_to_cpu(ni_mrec->sequence_number));
692 	/* Set hard links count and directory flag. */
693 	ni_mrec->link_count = cpu_to_le16(1);
694 	mark_mft_record_dirty(ni);
695 
696 	/* Add FILE_NAME attribute to index. */
697 	err = ntfs_index_add_filename(dir_ni, fn, child_mft_ref);
698 	if (err) {
699 		ntfs_debug("Failed to add entry to the index");
700 		goto err_out;
701 	}
702 
703 	unmap_mft_record(ni);
704 	mutex_unlock(&dir_ni->mrec_lock);
705 	mutex_unlock(&ni->mrec_lock);
706 
707 	ni->flags = fn->file_attributes |
708 		    (ni->flags & FILE_ATTRIBUTE_RECALL_ON_OPEN);
709 	/* Set the sequence number. */
710 	vi->i_generation = ni->seq_no;
711 	set_nlink(vi, 1);
712 	ntfs_set_vfs_operations(vi, mode, dev);
713 
714 	/* Done! */
715 	kfree(fn);
716 	kfree(si);
717 	ntfs_debug("Done.\n");
718 	return ni;
719 
720 err_out:
721 	if (rollback_sd) {
722 		int rollback_err;
723 
724 		rollback_err = ntfs_attr_remove(ni,
725 				AT_SECURITY_DESCRIPTOR, AT_UNNAMED, 0,
726 				&rollback_sd_vi);
727 		if (rollback_err)
728 			ntfs_error(sb,
729 				"Failed to roll back security descriptor.\n");
730 	}
731 
732 	if (rollback_data) {
733 		int rollback_err;
734 
735 		rollback_err = ntfs_attr_remove(ni, AT_DATA, AT_UNNAMED,
736 				0, &rollback_data_vi);
737 		if (rollback_err)
738 			ntfs_error(sb, "Failed to roll back DATA attribute.\n");
739 	}
740 
741 	if (rollback_reparse)
742 		ntfs_delete_reparse_index(ni);
743 	/*
744 	 * Free extent MFT records (should not exist any with current
745 	 * ntfs_create implementation, but for any case if something will be
746 	 * changed in the future).
747 	 */
748 	while (ni->nr_extents != 0) {
749 		int err2;
750 
751 		err2 = ntfs_mft_record_free(ni->vol, *(ni->ext.extent_ntfs_inos));
752 		if (err2)
753 			ntfs_error(sb,
754 				"Failed to free extent MFT record. Leaving inconsistent metadata.\n");
755 		ntfs_inode_close(*(ni->ext.extent_ntfs_inos));
756 	}
757 	if (ntfs_mft_record_free(ni->vol, ni))
758 		ntfs_error(sb,
759 			"Failed to free MFT record. Leaving inconsistent metadata. Run chkdsk.\n");
760 	unmap_mft_record(ni);
761 	kfree(fn);
762 	kfree(si);
763 
764 	mutex_unlock(&dir_ni->mrec_lock);
765 	mutex_unlock(&ni->mrec_lock);
766 
767 	if (rollback_data_vi)
768 		iput(rollback_data_vi);
769 	if (rollback_sd_vi)
770 		iput(rollback_sd_vi);
771 	remove_inode_hash(vi);
772 	discard_new_inode(vi);
773 	return ERR_PTR(err);
774 }
775 
776 static int ntfs_create(const struct mnt_idmap *idmap, struct inode *dir,
777 		struct dentry *dentry, umode_t mode)
778 {
779 	struct ntfs_volume *vol = NTFS_SB(dir->i_sb);
780 	struct ntfs_inode *ni;
781 	struct inode *stream_vi = NULL;
782 	__le16 *uname, *base_name = NULL, *stream_name = NULL;
783 	int uname_len, stream_len, path_result, err;
784 
785 	if (NVolShutdown(vol))
786 		return -EIO;
787 
788 	path_result = ntfs_stream_path_names(vol, &dentry->d_name, &base_name,
789 			&uname_len, &stream_name, &stream_len);
790 	if (path_result) {
791 		if (path_result < 0)
792 			return path_result;
793 		stream_vi = ntfs_create_named_stream(idmap, dir, base_name,
794 				uname_len, stream_name, stream_len);
795 		if (IS_ERR(stream_vi)) {
796 			err = PTR_ERR(stream_vi);
797 			goto out_free_stream_names;
798 		}
799 		kmem_cache_free(ntfs_name_cache, stream_name);
800 		kmem_cache_free(ntfs_name_cache, base_name);
801 		d_instantiate(dentry, stream_vi);
802 		return 0;
803 
804 out_free_stream_names:
805 		kmem_cache_free(ntfs_name_cache, stream_name);
806 		kmem_cache_free(ntfs_name_cache, base_name);
807 		return err;
808 	}
809 
810 	uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len,
811 				  &uname, NTFS_MAX_NAME_LEN);
812 	if (uname_len < 0) {
813 		if (uname_len != -ENAMETOOLONG)
814 			ntfs_error(vol->sb, "Failed to convert name to unicode.");
815 		return uname_len;
816 	}
817 
818 	err = ntfs_check_bad_windows_name(vol, uname, uname_len);
819 	if (err) {
820 		kmem_cache_free(ntfs_name_cache, uname);
821 		return err;
822 	}
823 
824 	ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
825 
826 	ni = __ntfs_create(idmap, dir, uname, uname_len, S_IFREG | mode, 0,
827 			NULL, 0);
828 	kmem_cache_free(ntfs_name_cache, uname);
829 	if (IS_ERR(ni))
830 		return PTR_ERR(ni);
831 
832 	d_instantiate_new(dentry, VFS_I(ni));
833 
834 	return 0;
835 }
836 
837 static int ntfs_check_unlinkable_dir(struct ntfs_attr_search_ctx *ctx, struct file_name_attr *fn)
838 {
839 	int link_count;
840 	int ret;
841 	struct ntfs_inode *ni = ctx->base_ntfs_ino ? ctx->base_ntfs_ino : ctx->ntfs_ino;
842 	struct mft_record *ni_mrec = ctx->base_mrec ? ctx->base_mrec : ctx->mrec;
843 
844 	ret = ntfs_check_empty_dir(ni, ni_mrec);
845 	if (!ret || ret != -ENOTEMPTY)
846 		return ret;
847 
848 	link_count = le16_to_cpu(ni_mrec->link_count);
849 	/*
850 	 * Directory is non-empty, so we can unlink only if there is more than
851 	 * one "real" hard link, i.e. links aren't different DOS and WIN32 names
852 	 */
853 	if ((link_count == 1) ||
854 	    (link_count == 2 && fn->file_name_type == FILE_NAME_DOS)) {
855 		ret = -ENOTEMPTY;
856 		ntfs_debug("Non-empty directory without hard links\n");
857 		goto no_hardlink;
858 	}
859 
860 	ret = 0;
861 no_hardlink:
862 	return ret;
863 }
864 
865 static int ntfs_test_inode_attr(struct inode *vi, void *data)
866 {
867 	struct ntfs_inode *ni = NTFS_I(vi);
868 	u64 mft_no = (u64)(uintptr_t)data;
869 
870 	if (ni->mft_no != mft_no)
871 		return 0;
872 	if (NInoAttr(ni) || ni->nr_extents == -1)
873 		return 1;
874 	return 0;
875 }
876 
877 static void ntfs_cleanup_deleted_inode(struct ntfs_inode *ni)
878 {
879 	struct inode *attr_vi;
880 	struct super_block *sb = VFS_I(ni)->i_sb;
881 
882 	if (!NInoBeingDeleted(ni))
883 		return;
884 
885 	while ((attr_vi = ilookup5(sb, ni->mft_no, ntfs_test_inode_attr,
886 			(void *)(uintptr_t)ni->mft_no))) {
887 		struct ntfs_inode *attr_ni = NTFS_I(attr_vi);
888 
889 		if (ntfs_inode_is_named_stream(attr_ni)) {
890 			if (atomic_read(&attr_ni->stream_open_count) > 0)
891 				NInoSetStreamUnlinked(attr_ni);
892 			remove_inode_hash(attr_vi);
893 		}
894 		clear_nlink(attr_vi);
895 		iput(attr_vi);
896 	}
897 }
898 
899 /*
900  * ntfs_delete - delete file or directory from ntfs volume
901  * @ni:         ntfs inode for object to delte
902  * @dir_ni:     ntfs inode for directory in which delete object
903  * @name:       unicode name of the object to delete
904  * @name_len:   length of the name in unicode characters
905  * @need_lock:  whether mrec lock is needed or not
906  *
907  * Delete the specified name from the directory index @dir_ni and decrement
908  * the link count of the target inode @ni.
909  *
910  * Return 0 on success and -errno on error.
911  */
912 static int ntfs_delete(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,
913 		__le16 *name, u8 name_len, bool need_lock)
914 {
915 	struct ntfs_attr_search_ctx *actx = NULL;
916 	struct file_name_attr *fn = NULL;
917 	bool looking_for_dos_name = false, looking_for_win32_name = false;
918 	bool case_sensitive_match = true;
919 	bool link_count_zero = false;
920 	int err = 0;
921 	struct mft_record *ni_mrec;
922 	struct super_block *sb;
923 
924 	ntfs_debug("Entering.\n");
925 
926 	if (need_lock == true) {
927 		mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
928 		mutex_lock_nested(&dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
929 	}
930 
931 	sb = dir_ni->vol->sb;
932 
933 	if (ni->nr_extents == -1)
934 		ni = ni->ext.base_ntfs_ino;
935 	if (dir_ni->nr_extents == -1)
936 		dir_ni = dir_ni->ext.base_ntfs_ino;
937 	/*
938 	 * Search for FILE_NAME attribute with such name. If it's in POSIX or
939 	 * WIN32_AND_DOS namespace, then simply remove it from index and inode.
940 	 * If filename in DOS or in WIN32 namespace, then remove DOS name first,
941 	 * only then remove WIN32 name.
942 	 */
943 	actx = ntfs_attr_get_search_ctx(ni, NULL);
944 	if (!actx) {
945 		ntfs_error(sb, "%s, Failed to get search context", __func__);
946 		if (need_lock) {
947 			mutex_unlock(&dir_ni->mrec_lock);
948 			mutex_unlock(&ni->mrec_lock);
949 		}
950 		return -ENOMEM;
951 	}
952 search:
953 	while ((err = ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, CASE_SENSITIVE,
954 				0, NULL, 0, actx)) == 0) {
955 #ifdef DEBUG
956 		unsigned char *s;
957 #endif
958 		bool case_sensitive = IGNORE_CASE;
959 
960 		fn = (struct file_name_attr *)((u8 *)actx->attr +
961 				le16_to_cpu(actx->attr->data.resident.value_offset));
962 #ifdef DEBUG
963 		s = ntfs_attr_name_get(ni->vol, fn->file_name, fn->file_name_length);
964 		ntfs_debug("name: '%s'  type: %d  dos: %d  win32: %d case: %d\n",
965 				s, fn->file_name_type,
966 				looking_for_dos_name, looking_for_win32_name,
967 				case_sensitive_match);
968 		ntfs_attr_name_free(&s);
969 #endif
970 		if (looking_for_dos_name) {
971 			if (fn->file_name_type == FILE_NAME_DOS)
972 				break;
973 			continue;
974 		}
975 		if (looking_for_win32_name) {
976 			if  (fn->file_name_type == FILE_NAME_WIN32)
977 				break;
978 			continue;
979 		}
980 
981 		/* Ignore hard links from other directories */
982 		if (dir_ni->mft_no != MREF_LE(fn->parent_directory)) {
983 			ntfs_debug("MFT record numbers don't match (%llu != %lu)\n",
984 					dir_ni->mft_no,
985 					MREF_LE(fn->parent_directory));
986 			continue;
987 		}
988 
989 		if (fn->file_name_type == FILE_NAME_POSIX || case_sensitive_match)
990 			case_sensitive = CASE_SENSITIVE;
991 
992 		if (ntfs_names_are_equal(fn->file_name, fn->file_name_length,
993 					name, name_len, case_sensitive,
994 					ni->vol->upcase, ni->vol->upcase_len)) {
995 			if (fn->file_name_type == FILE_NAME_WIN32) {
996 				looking_for_dos_name = true;
997 				ntfs_attr_reinit_search_ctx(actx);
998 				continue;
999 			}
1000 			if (fn->file_name_type == FILE_NAME_DOS)
1001 				looking_for_dos_name = true;
1002 			break;
1003 		}
1004 	}
1005 	if (err) {
1006 		/*
1007 		 * If case sensitive search failed, then try once again
1008 		 * ignoring case.
1009 		 */
1010 		if (err == -ENOENT && case_sensitive_match) {
1011 			case_sensitive_match = false;
1012 			ntfs_attr_reinit_search_ctx(actx);
1013 			goto search;
1014 		}
1015 		goto err_out;
1016 	}
1017 
1018 	err = ntfs_check_unlinkable_dir(actx, fn);
1019 	if (err)
1020 		goto err_out;
1021 
1022 	err = ntfs_index_remove(dir_ni, fn, le32_to_cpu(actx->attr->data.resident.value_length));
1023 	if (err)
1024 		goto err_out;
1025 
1026 	err = ntfs_attr_record_rm(actx);
1027 	if (err)
1028 		goto err_out;
1029 
1030 	ni_mrec = actx->base_mrec ? actx->base_mrec : actx->mrec;
1031 	ni_mrec->link_count = cpu_to_le16(le16_to_cpu(ni_mrec->link_count) - 1);
1032 	if (!S_ISDIR(VFS_I(ni)->i_mode))
1033 		drop_nlink(VFS_I(ni));
1034 
1035 	mark_mft_record_dirty(ni);
1036 	if (looking_for_dos_name) {
1037 		looking_for_dos_name = false;
1038 		looking_for_win32_name = true;
1039 		ntfs_attr_reinit_search_ctx(actx);
1040 		goto search;
1041 	}
1042 
1043 	/*
1044 	 * For directories, Drop VFS nlink only when mft record link count
1045 	 * becomes zero. Because we fixes VFS nlink to 1 for directories.
1046 	 */
1047 	if (S_ISDIR(VFS_I(ni)->i_mode) && !le16_to_cpu(ni_mrec->link_count))
1048 		drop_nlink(VFS_I(ni));
1049 
1050 	/*
1051 	 * If hard link count is not equal to zero then we are done. In other
1052 	 * case there are no reference to this inode left, so we should free all
1053 	 * non-resident attributes and mark all MFT record as not in use.
1054 	 */
1055 	if (ni_mrec->link_count == 0) {
1056 		NInoSetBeingDeleted(ni);
1057 		ntfs_delete_reparse_index(ni);
1058 		ntfs_delete_object_id_index(ni);
1059 		link_count_zero = true;
1060 	}
1061 
1062 	ntfs_attr_put_search_ctx(actx);
1063 	if (need_lock == true) {
1064 		mutex_unlock(&dir_ni->mrec_lock);
1065 		mutex_unlock(&ni->mrec_lock);
1066 		if (link_count_zero)
1067 			ntfs_cleanup_deleted_inode(ni);
1068 	}
1069 
1070 	ntfs_debug("Done.\n");
1071 	return 0;
1072 err_out:
1073 	ntfs_attr_put_search_ctx(actx);
1074 	if (need_lock) {
1075 		mutex_unlock(&dir_ni->mrec_lock);
1076 		mutex_unlock(&ni->mrec_lock);
1077 	}
1078 	return err;
1079 }
1080 
1081 static int ntfs_unlink(struct inode *dir, struct dentry *dentry)
1082 {
1083 	struct inode *vi = dentry->d_inode;
1084 	struct super_block *sb = dir->i_sb;
1085 	struct ntfs_volume *vol = NTFS_SB(sb);
1086 	int err = 0;
1087 	struct ntfs_inode *ni = NTFS_I(vi);
1088 	__le16 *uname = NULL;
1089 	int uname_len;
1090 
1091 	if (NVolShutdown(vol))
1092 		return -EIO;
1093 
1094 	if (ntfs_stream_path_has_colon(vol, &dentry->d_name))
1095 		return ntfs_unlink_named_stream(dir, dentry);
1096 	if (NInoAttr(ni))
1097 		return -EOPNOTSUPP;
1098 
1099 	uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len,
1100 				  &uname, NTFS_MAX_NAME_LEN);
1101 	if (uname_len < 0) {
1102 		if (uname_len != -ENAMETOOLONG)
1103 			ntfs_error(sb, "Failed to convert name to Unicode.");
1104 		return -ENOMEM;
1105 	}
1106 
1107 	err = ntfs_check_bad_windows_name(vol, uname, uname_len);
1108 	if (err) {
1109 		kmem_cache_free(ntfs_name_cache, uname);
1110 		return err;
1111 	}
1112 
1113 	ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1114 
1115 	err = ntfs_delete(ni, NTFS_I(dir), uname, uname_len, true);
1116 	if (err)
1117 		goto out;
1118 
1119 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
1120 	mark_inode_dirty(dir);
1121 	inode_set_ctime_to_ts(vi, inode_get_ctime(dir));
1122 	if (vi->i_nlink)
1123 		mark_inode_dirty(vi);
1124 out:
1125 	kmem_cache_free(ntfs_name_cache, uname);
1126 	return err;
1127 }
1128 
1129 static struct dentry *ntfs_mkdir(const struct mnt_idmap *idmap, struct inode *dir,
1130 		struct dentry *dentry, umode_t mode)
1131 {
1132 	struct super_block *sb = dir->i_sb;
1133 	struct ntfs_volume *vol = NTFS_SB(sb);
1134 	int err = 0;
1135 	struct ntfs_inode *ni;
1136 	__le16 *uname;
1137 	int uname_len;
1138 
1139 	if (NVolShutdown(vol))
1140 		return ERR_PTR(-EIO);
1141 
1142 	err = ntfs_stream_path_check(vol, &dentry->d_name);
1143 	if (err)
1144 		return ERR_PTR(err);
1145 
1146 	uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len,
1147 				  &uname, NTFS_MAX_NAME_LEN);
1148 	if (uname_len < 0) {
1149 		if (uname_len != -ENAMETOOLONG)
1150 			ntfs_error(sb, "Failed to convert name to unicode.");
1151 		return ERR_PTR(-ENOMEM);
1152 	}
1153 
1154 	err = ntfs_check_bad_windows_name(vol, uname, uname_len);
1155 	if (err) {
1156 		kmem_cache_free(ntfs_name_cache, uname);
1157 		return ERR_PTR(err);
1158 	}
1159 
1160 	ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1161 
1162 	ni = __ntfs_create(idmap, dir, uname, uname_len, mode, 0,
1163 			NULL, 0);
1164 	kmem_cache_free(ntfs_name_cache, uname);
1165 	if (IS_ERR(ni)) {
1166 		err = PTR_ERR(ni);
1167 		return ERR_PTR(err);
1168 	}
1169 
1170 	d_instantiate_new(dentry, VFS_I(ni));
1171 	return NULL;
1172 }
1173 
1174 static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)
1175 {
1176 	struct inode *vi = dentry->d_inode;
1177 	struct super_block *sb = dir->i_sb;
1178 	struct ntfs_volume *vol = NTFS_SB(sb);
1179 	int err = 0;
1180 	struct ntfs_inode *ni;
1181 	__le16 *uname = NULL;
1182 	int uname_len;
1183 
1184 	if (NVolShutdown(vol))
1185 		return -EIO;
1186 
1187 	err = ntfs_stream_path_check(vol, &dentry->d_name);
1188 	if (err)
1189 		return err;
1190 
1191 	ni = NTFS_I(vi);
1192 	uname_len = ntfs_nlstoucs(vol, dentry->d_name.name, dentry->d_name.len,
1193 				  &uname, NTFS_MAX_NAME_LEN);
1194 	if (uname_len < 0) {
1195 		if (uname_len != -ENAMETOOLONG)
1196 			ntfs_error(sb, "Failed to convert name to unicode.");
1197 		return -ENOMEM;
1198 	}
1199 
1200 	err = ntfs_check_bad_windows_name(vol, uname, uname_len);
1201 	if (err) {
1202 		kmem_cache_free(ntfs_name_cache, uname);
1203 		return err;
1204 	}
1205 
1206 	ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1207 
1208 	err = ntfs_delete(ni, NTFS_I(dir), uname, uname_len, true);
1209 	if (err)
1210 		goto out;
1211 
1212 	inode_set_mtime_to_ts(vi, inode_set_atime_to_ts(vi, current_time(vi)));
1213 out:
1214 	kmem_cache_free(ntfs_name_cache, uname);
1215 	return err;
1216 }
1217 
1218 /*
1219  * __ntfs_link - create hard link for file or directory
1220  * @ni:		ntfs inode for object to create hard link
1221  * @dir_ni:	ntfs inode for directory in which new link should be placed
1222  * @name:	unicode name of the new link
1223  * @name_len:	length of the name in unicode characters
1224  *
1225  * Create a new hard link. This involves adding an entry to the directory
1226  * index and adding a new FILE_NAME attribute to the target inode.
1227  *
1228  * Return 0 on success and -errno on error.
1229  */
1230 static int __ntfs_link(struct ntfs_inode *ni, struct ntfs_inode *dir_ni,
1231 		__le16 *name, u8 name_len)
1232 {
1233 	struct super_block *sb;
1234 	struct inode *vi = VFS_I(ni);
1235 	struct file_name_attr *fn = NULL;
1236 	int fn_len, err = 0;
1237 	struct mft_record *dir_mrec = NULL, *ni_mrec = NULL;
1238 
1239 	ntfs_debug("Entering.\n");
1240 
1241 	sb = dir_ni->vol->sb;
1242 	if (NInoBeingDeleted(dir_ni) || NInoBeingDeleted(ni))
1243 		return -ENOENT;
1244 
1245 	ni_mrec = map_mft_record(ni);
1246 	if (IS_ERR(ni_mrec)) {
1247 		err = -EIO;
1248 		goto err_out;
1249 	}
1250 
1251 	if (le16_to_cpu(ni_mrec->link_count) == 0) {
1252 		err = -ENOENT;
1253 		goto err_out;
1254 	}
1255 
1256 	/* Create FILE_NAME attribute. */
1257 	fn_len = sizeof(struct file_name_attr) + name_len * sizeof(__le16);
1258 
1259 	fn = kzalloc(fn_len, GFP_NOFS);
1260 	if (!fn) {
1261 		err = -ENOMEM;
1262 		goto err_out;
1263 	}
1264 
1265 	dir_mrec = map_mft_record(dir_ni);
1266 	if (IS_ERR(dir_mrec)) {
1267 		err = -EIO;
1268 		goto err_out;
1269 	}
1270 
1271 	fn->parent_directory = MK_LE_MREF(dir_ni->mft_no,
1272 			le16_to_cpu(dir_mrec->sequence_number));
1273 	unmap_mft_record(dir_ni);
1274 	fn->file_name_length = name_len;
1275 	fn->file_name_type = FILE_NAME_POSIX;
1276 	fn->file_attributes = ni->flags;
1277 	if (ni_mrec->flags & MFT_RECORD_IS_DIRECTORY) {
1278 		fn->file_attributes |= FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT;
1279 		fn->allocated_size = fn->data_size = 0;
1280 	} else {
1281 		if (NInoSparse(ni) || NInoCompressed(ni))
1282 			fn->allocated_size =
1283 				cpu_to_le64(ni->itype.compressed.size);
1284 		else
1285 			fn->allocated_size = cpu_to_le64(ni->allocated_size);
1286 		fn->data_size = cpu_to_le64(ni->data_size);
1287 	}
1288 	if (NVolHideDotFiles(dir_ni->vol) && name_len > 0 && name[0] == cpu_to_le16('.'))
1289 		fn->file_attributes |= FILE_ATTR_HIDDEN;
1290 
1291 	fn->creation_time = utc2ntfs(ni->i_crtime);
1292 	fn->last_data_change_time = utc2ntfs(inode_get_mtime(vi));
1293 	fn->last_mft_change_time = utc2ntfs(inode_get_ctime(vi));
1294 	fn->last_access_time = utc2ntfs(inode_get_atime(vi));
1295 	memcpy(fn->file_name, name, name_len * sizeof(__le16));
1296 
1297 	/* Add FILE_NAME attribute to index. */
1298 	err = ntfs_index_add_filename(dir_ni, fn, MK_MREF(ni->mft_no,
1299 					le16_to_cpu(ni_mrec->sequence_number)));
1300 	if (err) {
1301 		ntfs_error(sb, "Failed to add filename to the index");
1302 		goto err_out;
1303 	}
1304 	/* Add FILE_NAME attribute to inode. */
1305 	err = ntfs_attr_add(ni, AT_FILE_NAME, AT_UNNAMED, 0, (u8 *)fn, fn_len);
1306 	if (err) {
1307 		ntfs_error(sb, "Failed to add FILE_NAME attribute.\n");
1308 		/* Try to remove just added attribute from index. */
1309 		if (ntfs_index_remove(dir_ni, fn, fn_len))
1310 			goto rollback_failed;
1311 		goto err_out;
1312 	}
1313 	/* Increment hard links count. */
1314 	ni_mrec->link_count = cpu_to_le16(le16_to_cpu(ni_mrec->link_count) + 1);
1315 	if (!S_ISDIR(vi->i_mode))
1316 		inc_nlink(VFS_I(ni));
1317 
1318 	/* Done! */
1319 	mark_mft_record_dirty(ni);
1320 	kfree(fn);
1321 	unmap_mft_record(ni);
1322 
1323 	ntfs_debug("Done.\n");
1324 
1325 	return 0;
1326 rollback_failed:
1327 	ntfs_error(sb, "Rollback failed. Leaving inconsistent metadata.\n");
1328 err_out:
1329 	kfree(fn);
1330 	if (!IS_ERR_OR_NULL(ni_mrec))
1331 		unmap_mft_record(ni);
1332 	return err;
1333 }
1334 
1335 static int ntfs_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
1336 		struct dentry *old_dentry, struct inode *new_dir,
1337 		struct dentry *new_dentry, unsigned int flags)
1338 {
1339 	struct inode *old_inode, *new_inode = NULL;
1340 	int err = 0;
1341 	int is_dir;
1342 	struct super_block *sb = old_dir->i_sb;
1343 	__le16 *uname_new = NULL;
1344 	__le16 *uname_old = NULL;
1345 	int new_name_len;
1346 	int old_name_len;
1347 	struct ntfs_volume *vol = NTFS_SB(sb);
1348 	struct ntfs_inode *old_ni, *new_ni = NULL;
1349 	struct ntfs_inode *old_dir_ni = NTFS_I(old_dir), *new_dir_ni = NTFS_I(new_dir);
1350 	bool new_dir_first = false;
1351 
1352 	if (NVolShutdown(old_dir_ni->vol))
1353 		return -EIO;
1354 
1355 	err = ntfs_stream_path_check(vol, &old_dentry->d_name);
1356 	if (err)
1357 		return err;
1358 	err = ntfs_stream_path_check(vol, &new_dentry->d_name);
1359 	if (err)
1360 		return err;
1361 
1362 	if (flags & (RENAME_EXCHANGE | RENAME_WHITEOUT))
1363 		return -EINVAL;
1364 
1365 	new_name_len = ntfs_nlstoucs(NTFS_I(new_dir)->vol, new_dentry->d_name.name,
1366 				     new_dentry->d_name.len, &uname_new,
1367 				     NTFS_MAX_NAME_LEN);
1368 	if (new_name_len < 0) {
1369 		if (new_name_len != -ENAMETOOLONG)
1370 			ntfs_error(sb, "Failed to convert name to unicode.");
1371 		return -ENOMEM;
1372 	}
1373 
1374 	err = ntfs_check_bad_windows_name(vol, uname_new, new_name_len);
1375 	if (err) {
1376 		kmem_cache_free(ntfs_name_cache, uname_new);
1377 		return err;
1378 	}
1379 
1380 	old_name_len = ntfs_nlstoucs(NTFS_I(old_dir)->vol, old_dentry->d_name.name,
1381 				     old_dentry->d_name.len, &uname_old,
1382 				     NTFS_MAX_NAME_LEN);
1383 	if (old_name_len < 0) {
1384 		kmem_cache_free(ntfs_name_cache, uname_new);
1385 		if (old_name_len != -ENAMETOOLONG)
1386 			ntfs_error(sb, "Failed to convert name to unicode.");
1387 		return -ENOMEM;
1388 	}
1389 
1390 	old_inode = old_dentry->d_inode;
1391 	new_inode = new_dentry->d_inode;
1392 	old_ni = NTFS_I(old_inode);
1393 	if (new_inode)
1394 		new_ni = NTFS_I(new_inode);
1395 	if (old_dir != new_dir)
1396 		new_dir_first = is_subdir(new_dentry->d_parent,
1397 					  old_dentry->d_parent);
1398 
1399 	ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1400 
1401 	mutex_lock_nested(&old_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
1402 	if (new_ni)
1403 		mutex_lock_nested(&new_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL_2);
1404 
1405 	if (old_dir == new_dir) {
1406 		mutex_lock_nested(&old_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
1407 	} else if (new_dir_first) {
1408 		mutex_lock_nested(&new_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
1409 		mutex_lock_nested(&old_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT_2);
1410 	} else {
1411 		mutex_lock_nested(&old_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
1412 		mutex_lock_nested(&new_dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT_2);
1413 	}
1414 
1415 	if (NInoBeingDeleted(old_ni) || NInoBeingDeleted(old_dir_ni) ||
1416 	    (new_ni && NInoBeingDeleted(new_ni)) ||
1417 	    (old_dir != new_dir && NInoBeingDeleted(new_dir_ni))) {
1418 		err = -ENOENT;
1419 		goto err_out;
1420 	}
1421 
1422 	is_dir = S_ISDIR(old_inode->i_mode);
1423 
1424 	if (new_inode) {
1425 		if (is_dir) {
1426 			struct mft_record *ni_mrec;
1427 
1428 			ni_mrec = map_mft_record(NTFS_I(new_inode));
1429 			if (IS_ERR(ni_mrec)) {
1430 				err = -EIO;
1431 				goto err_out;
1432 			}
1433 			err = ntfs_check_empty_dir(NTFS_I(new_inode), ni_mrec);
1434 			unmap_mft_record(NTFS_I(new_inode));
1435 			if (err)
1436 				goto err_out;
1437 		}
1438 
1439 		err = ntfs_delete(new_ni, new_dir_ni, uname_new, new_name_len, false);
1440 		if (err)
1441 			goto err_out;
1442 	}
1443 
1444 	err = __ntfs_link(old_ni, new_dir_ni, uname_new, new_name_len);
1445 	if (err)
1446 		goto err_out;
1447 
1448 	err = ntfs_delete(old_ni, old_dir_ni, uname_old, old_name_len, false);
1449 	if (err) {
1450 		int err2;
1451 
1452 		ntfs_error(sb, "Failed to delete old ntfs inode(%llu) in old dir, err : %d\n",
1453 				old_ni->mft_no, err);
1454 		err2 = ntfs_delete(old_ni, new_dir_ni, uname_new, new_name_len, false);
1455 		if (err2)
1456 			ntfs_error(sb, "Failed to delete old ntfs inode in new dir, err : %d\n",
1457 					err2);
1458 		goto err_out;
1459 	}
1460 
1461 	simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry);
1462 	mark_inode_dirty(old_inode);
1463 	mark_inode_dirty(old_dir);
1464 	if (old_dir != new_dir)
1465 		mark_inode_dirty(new_dir);
1466 	if (new_inode)
1467 		mark_inode_dirty(old_inode);
1468 
1469 	inode_inc_iversion(new_dir);
1470 
1471 err_out:
1472 	if (old_dir == new_dir) {
1473 		mutex_unlock(&old_dir_ni->mrec_lock);
1474 	} else if (new_dir_first) {
1475 		mutex_unlock(&old_dir_ni->mrec_lock);
1476 		mutex_unlock(&new_dir_ni->mrec_lock);
1477 	} else {
1478 		mutex_unlock(&new_dir_ni->mrec_lock);
1479 		mutex_unlock(&old_dir_ni->mrec_lock);
1480 	}
1481 	if (new_ni)
1482 		mutex_unlock(&new_ni->mrec_lock);
1483 	mutex_unlock(&old_ni->mrec_lock);
1484 	if (new_ni)
1485 		ntfs_cleanup_deleted_inode(new_ni);
1486 	if (uname_new)
1487 		kmem_cache_free(ntfs_name_cache, uname_new);
1488 	if (uname_old)
1489 		kmem_cache_free(ntfs_name_cache, uname_old);
1490 
1491 	return err;
1492 }
1493 
1494 static int ntfs_symlink(const struct mnt_idmap *idmap, struct inode *dir,
1495 		struct dentry *dentry, const char *symname)
1496 {
1497 	struct super_block *sb = dir->i_sb;
1498 	struct ntfs_volume *vol = NTFS_SB(sb);
1499 	struct inode *vi;
1500 	int err = 0;
1501 	struct ntfs_inode *ni;
1502 	__le16 *usrc;
1503 	int usrc_len;
1504 	int symlen = strlen(symname);
1505 
1506 	if (NVolShutdown(vol))
1507 		return -EIO;
1508 
1509 	err = ntfs_stream_path_check(vol, &dentry->d_name);
1510 	if (err)
1511 		return err;
1512 
1513 	usrc_len = ntfs_nlstoucs(vol, dentry->d_name.name,
1514 				 dentry->d_name.len, &usrc, NTFS_MAX_NAME_LEN);
1515 	if (usrc_len < 0) {
1516 		if (usrc_len != -ENAMETOOLONG)
1517 			ntfs_error(sb, "Failed to convert name to Unicode.");
1518 		err =  -ENOMEM;
1519 		goto out;
1520 	}
1521 
1522 	err = ntfs_check_bad_windows_name(vol, usrc, usrc_len);
1523 	if (err) {
1524 		kmem_cache_free(ntfs_name_cache, usrc);
1525 		goto out;
1526 	}
1527 
1528 	ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1529 
1530 	ni = __ntfs_create(idmap, dir, usrc, usrc_len, S_IFLNK | 0777, 0,
1531 			   symname, symlen);
1532 	kmem_cache_free(ntfs_name_cache, usrc);
1533 	if (IS_ERR(ni)) {
1534 		err = PTR_ERR(ni);
1535 		goto out;
1536 	}
1537 
1538 	vi = VFS_I(ni);
1539 	vi->i_size = symlen;
1540 	d_instantiate_new(dentry, vi);
1541 out:
1542 	return err;
1543 }
1544 
1545 static int ntfs_mknod(const struct mnt_idmap *idmap, struct inode *dir,
1546 		struct dentry *dentry, umode_t mode, dev_t rdev)
1547 {
1548 	struct super_block *sb = dir->i_sb;
1549 	struct ntfs_volume *vol = NTFS_SB(sb);
1550 	int err = 0;
1551 	struct ntfs_inode *ni;
1552 	__le16 *uname = NULL;
1553 	int uname_len;
1554 
1555 	if (NVolShutdown(vol))
1556 		return -EIO;
1557 
1558 	err = ntfs_stream_path_check(vol, &dentry->d_name);
1559 	if (err)
1560 		return err;
1561 
1562 	uname_len = ntfs_nlstoucs(vol, dentry->d_name.name,
1563 			dentry->d_name.len, &uname, NTFS_MAX_NAME_LEN);
1564 	if (uname_len < 0) {
1565 		if (uname_len != -ENAMETOOLONG)
1566 			ntfs_error(sb, "Failed to convert name to Unicode.");
1567 		return -ENOMEM;
1568 	}
1569 
1570 	err = ntfs_check_bad_windows_name(vol, uname, uname_len);
1571 	if (err) {
1572 		kmem_cache_free(ntfs_name_cache, uname);
1573 		return err;
1574 	}
1575 
1576 	ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1577 
1578 	switch (mode & S_IFMT) {
1579 	case S_IFCHR:
1580 	case S_IFBLK:
1581 		ni = __ntfs_create(idmap, dir, uname, uname_len,
1582 				mode, rdev, NULL, 0);
1583 		break;
1584 	default:
1585 		ni = __ntfs_create(idmap, dir, uname, uname_len,
1586 				mode, 0, NULL, 0);
1587 	}
1588 
1589 	kmem_cache_free(ntfs_name_cache, uname);
1590 	if (IS_ERR(ni)) {
1591 		err = PTR_ERR(ni);
1592 		goto out;
1593 	}
1594 
1595 	d_instantiate_new(dentry, VFS_I(ni));
1596 out:
1597 	return err;
1598 }
1599 
1600 static int ntfs_link(struct dentry *old_dentry, struct inode *dir,
1601 		struct dentry *dentry)
1602 {
1603 	struct inode *vi = old_dentry->d_inode;
1604 	struct super_block *sb = vi->i_sb;
1605 	struct ntfs_volume *vol = NTFS_SB(sb);
1606 	__le16 *uname = NULL;
1607 	int uname_len;
1608 	int err;
1609 	struct ntfs_inode *ni = NTFS_I(vi), *dir_ni = NTFS_I(dir);
1610 
1611 	if (NVolShutdown(vol))
1612 		return -EIO;
1613 
1614 	if (NInoAttr(ni))
1615 		return -EOPNOTSUPP;
1616 
1617 	err = ntfs_stream_path_check(vol, &dentry->d_name);
1618 	if (err)
1619 		return err;
1620 
1621 	uname_len = ntfs_nlstoucs(vol, dentry->d_name.name,
1622 			dentry->d_name.len, &uname, NTFS_MAX_NAME_LEN);
1623 	if (uname_len < 0) {
1624 		if (uname_len != -ENAMETOOLONG)
1625 			ntfs_error(sb, "Failed to convert name to unicode.");
1626 		return -ENOMEM;
1627 	}
1628 
1629 	ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1630 
1631 	ihold(vi);
1632 	mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
1633 	mutex_lock_nested(&dir_ni->mrec_lock, NTFS_INODE_MUTEX_PARENT);
1634 	err = __ntfs_link(NTFS_I(vi), NTFS_I(dir), uname, uname_len);
1635 	if (err) {
1636 		mutex_unlock(&dir_ni->mrec_lock);
1637 		mutex_unlock(&ni->mrec_lock);
1638 		iput(vi);
1639 		pr_err("failed to create link, err = %d\n", err);
1640 		goto out;
1641 	}
1642 
1643 	inode_inc_iversion(dir);
1644 	simple_inode_init_ts(dir);
1645 
1646 	inode_inc_iversion(vi);
1647 	simple_inode_init_ts(vi);
1648 
1649 	/* timestamp is already written, so mark_inode_dirty() is unneeded. */
1650 	d_instantiate(dentry, vi);
1651 	mutex_unlock(&dir_ni->mrec_lock);
1652 	mutex_unlock(&ni->mrec_lock);
1653 
1654 out:
1655 	kmem_cache_free(ntfs_name_cache, uname);
1656 	return err;
1657 }
1658 
1659 /*
1660  * Inode operations for directories.
1661  */
1662 const struct inode_operations ntfs_dir_inode_ops = {
1663 	.lookup		= ntfs_lookup,	/* VFS: Lookup directory. */
1664 	.create		= ntfs_create,
1665 	.unlink		= ntfs_unlink,
1666 	.mkdir		= ntfs_mkdir,
1667 	.rmdir		= ntfs_rmdir,
1668 	.rename		= ntfs_rename,
1669 	.get_acl	= ntfs_get_acl,
1670 	.set_acl	= ntfs_set_acl,
1671 	.listxattr	= ntfs_listxattr,
1672 	.setattr	= ntfs_setattr,
1673 	.getattr	= ntfs_getattr,
1674 	.fileattr_get	= ntfs_fileattr_get,
1675 	.fileattr_set	= ntfs_fileattr_set,
1676 	.symlink	= ntfs_symlink,
1677 	.mknod		= ntfs_mknod,
1678 	.link		= ntfs_link,
1679 };
1680 
1681 /*
1682  * ntfs_get_parent - find the dentry of the parent of a given directory dentry
1683  * @child_dent:		dentry of the directory whose parent directory to find
1684  *
1685  * Find the dentry for the parent directory of the directory specified by the
1686  * dentry @child_dent.  This function is called from
1687  * fs/exportfs/expfs.c::find_exported_dentry() which in turn is called from the
1688  * default ->decode_fh() which is export_decode_fh() in the same file.
1689  *
1690  * Note: ntfs_get_parent() is called with @d_inode(child_dent)->i_mutex down.
1691  *
1692  * Return the dentry of the parent directory on success or the error code on
1693  * error (IS_ERR() is true).
1694  */
1695 static struct dentry *ntfs_get_parent(struct dentry *child_dent)
1696 {
1697 	struct inode *vi = d_inode(child_dent);
1698 	struct ntfs_inode *ni = NTFS_I(vi);
1699 	struct mft_record *mrec;
1700 	struct ntfs_attr_search_ctx *ctx;
1701 	struct attr_record *attr;
1702 	struct file_name_attr *fn;
1703 	unsigned long parent_ino;
1704 	int err;
1705 
1706 	ntfs_debug("Entering for inode 0x%llx.", ni->mft_no);
1707 	/* Get the mft record of the inode belonging to the child dentry. */
1708 	mrec = map_mft_record(ni);
1709 	if (IS_ERR(mrec))
1710 		return ERR_CAST(mrec);
1711 	/* Find the first file name attribute in the mft record. */
1712 	ctx = ntfs_attr_get_search_ctx(ni, mrec);
1713 	if (unlikely(!ctx)) {
1714 		unmap_mft_record(ni);
1715 		return ERR_PTR(-ENOMEM);
1716 	}
1717 try_next:
1718 	err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, CASE_SENSITIVE, 0, NULL,
1719 			0, ctx);
1720 	if (unlikely(err)) {
1721 		ntfs_attr_put_search_ctx(ctx);
1722 		unmap_mft_record(ni);
1723 		if (err == -ENOENT)
1724 			ntfs_error(vi->i_sb,
1725 				   "Inode 0x%llx does not have a file name attribute.  Run chkdsk.",
1726 				   ni->mft_no);
1727 		return ERR_PTR(err);
1728 	}
1729 	attr = ctx->attr;
1730 	if (unlikely(attr->non_resident))
1731 		goto try_next;
1732 	fn = (struct file_name_attr *)((u8 *)attr +
1733 			le16_to_cpu(attr->data.resident.value_offset));
1734 	if (unlikely((u8 *)fn + le32_to_cpu(attr->data.resident.value_length) >
1735 	    (u8 *)attr + le32_to_cpu(attr->length)))
1736 		goto try_next;
1737 	/* Get the inode number of the parent directory. */
1738 	parent_ino = MREF_LE(fn->parent_directory);
1739 	/* Release the search context and the mft record of the child. */
1740 	ntfs_attr_put_search_ctx(ctx);
1741 	unmap_mft_record(ni);
1742 
1743 	return d_obtain_alias(ntfs_iget(vi->i_sb, parent_ino));
1744 }
1745 
1746 static struct inode *ntfs_nfs_get_inode(struct super_block *sb,
1747 		u64 ino, u32 generation)
1748 {
1749 	struct inode *inode;
1750 
1751 	inode = ntfs_iget(sb, ino);
1752 	if (!IS_ERR(inode)) {
1753 		if (inode->i_generation != generation) {
1754 			iput(inode);
1755 			inode = ERR_PTR(-ESTALE);
1756 		}
1757 	}
1758 
1759 	return inode;
1760 }
1761 
1762 static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1763 		int fh_len, int fh_type)
1764 {
1765 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1766 				    ntfs_nfs_get_inode);
1767 }
1768 
1769 static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
1770 		int fh_len, int fh_type)
1771 {
1772 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1773 				    ntfs_nfs_get_inode);
1774 }
1775 
1776 static int ntfs_encode_fh(struct inode *inode, u32 *fh, int *max_len,
1777 		struct inode *parent)
1778 {
1779 	if (ntfs_inode_is_named_stream(NTFS_I(inode)))
1780 		return -EOPNOTSUPP;
1781 	return generic_encode_ino32_fh(inode, fh, max_len, parent);
1782 }
1783 
1784 /*
1785  * Export operations allowing NFS exporting of mounted NTFS partitions.
1786  */
1787 const struct export_operations ntfs_export_ops = {
1788 	.encode_fh	= ntfs_encode_fh,
1789 	.get_parent	= ntfs_get_parent,	/* Find the parent of a given directory. */
1790 	.fh_to_dentry	= ntfs_fh_to_dentry,
1791 	.fh_to_parent	= ntfs_fh_to_parent,
1792 };
1793