xref: /OK3568_Linux_fs/kernel/fs/ntfs/namei.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * namei.c - NTFS kernel directory inode operations. Part of the Linux-NTFS
4*4882a593Smuzhiyun  *	     project.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (c) 2001-2006 Anton Altaparmakov
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/dcache.h>
10*4882a593Smuzhiyun #include <linux/exportfs.h>
11*4882a593Smuzhiyun #include <linux/security.h>
12*4882a593Smuzhiyun #include <linux/slab.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include "attrib.h"
15*4882a593Smuzhiyun #include "debug.h"
16*4882a593Smuzhiyun #include "dir.h"
17*4882a593Smuzhiyun #include "mft.h"
18*4882a593Smuzhiyun #include "ntfs.h"
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun /**
21*4882a593Smuzhiyun  * ntfs_lookup - find the inode represented by a dentry in a directory inode
22*4882a593Smuzhiyun  * @dir_ino:	directory inode in which to look for the inode
23*4882a593Smuzhiyun  * @dent:	dentry representing the inode to look for
24*4882a593Smuzhiyun  * @flags:	lookup flags
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * In short, ntfs_lookup() looks for the inode represented by the dentry @dent
27*4882a593Smuzhiyun  * in the directory inode @dir_ino and if found attaches the inode to the
28*4882a593Smuzhiyun  * dentry @dent.
29*4882a593Smuzhiyun  *
30*4882a593Smuzhiyun  * In more detail, the dentry @dent specifies which inode to look for by
31*4882a593Smuzhiyun  * supplying the name of the inode in @dent->d_name.name. ntfs_lookup()
32*4882a593Smuzhiyun  * converts the name to Unicode and walks the contents of the directory inode
33*4882a593Smuzhiyun  * @dir_ino looking for the converted Unicode name. If the name is found in the
34*4882a593Smuzhiyun  * directory, the corresponding inode is loaded by calling ntfs_iget() on its
35*4882a593Smuzhiyun  * inode number and the inode is associated with the dentry @dent via a call to
36*4882a593Smuzhiyun  * d_splice_alias().
37*4882a593Smuzhiyun  *
38*4882a593Smuzhiyun  * If the name is not found in the directory, a NULL inode is inserted into the
39*4882a593Smuzhiyun  * dentry @dent via a call to d_add(). The dentry is then termed a negative
40*4882a593Smuzhiyun  * dentry.
41*4882a593Smuzhiyun  *
42*4882a593Smuzhiyun  * Only if an actual error occurs, do we return an error via ERR_PTR().
43*4882a593Smuzhiyun  *
44*4882a593Smuzhiyun  * In order to handle the case insensitivity issues of NTFS with regards to the
45*4882a593Smuzhiyun  * dcache and the dcache requiring only one dentry per directory, we deal with
46*4882a593Smuzhiyun  * dentry aliases that only differ in case in ->ntfs_lookup() while maintaining
47*4882a593Smuzhiyun  * a case sensitive dcache. This means that we get the full benefit of dcache
48*4882a593Smuzhiyun  * speed when the file/directory is looked up with the same case as returned by
49*4882a593Smuzhiyun  * ->ntfs_readdir() but that a lookup for any other case (or for the short file
50*4882a593Smuzhiyun  * name) will not find anything in dcache and will enter ->ntfs_lookup()
51*4882a593Smuzhiyun  * instead, where we search the directory for a fully matching file name
52*4882a593Smuzhiyun  * (including case) and if that is not found, we search for a file name that
53*4882a593Smuzhiyun  * matches with different case and if that has non-POSIX semantics we return
54*4882a593Smuzhiyun  * that. We actually do only one search (case sensitive) and keep tabs on
55*4882a593Smuzhiyun  * whether we have found a case insensitive match in the process.
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  * To simplify matters for us, we do not treat the short vs long filenames as
58*4882a593Smuzhiyun  * two hard links but instead if the lookup matches a short filename, we
59*4882a593Smuzhiyun  * return the dentry for the corresponding long filename instead.
60*4882a593Smuzhiyun  *
61*4882a593Smuzhiyun  * There are three cases we need to distinguish here:
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  * 1) @dent perfectly matches (i.e. including case) a directory entry with a
64*4882a593Smuzhiyun  *    file name in the WIN32 or POSIX namespaces. In this case
65*4882a593Smuzhiyun  *    ntfs_lookup_inode_by_name() will return with name set to NULL and we
66*4882a593Smuzhiyun  *    just d_splice_alias() @dent.
67*4882a593Smuzhiyun  * 2) @dent matches (not including case) a directory entry with a file name in
68*4882a593Smuzhiyun  *    the WIN32 namespace. In this case ntfs_lookup_inode_by_name() will return
69*4882a593Smuzhiyun  *    with name set to point to a kmalloc()ed ntfs_name structure containing
70*4882a593Smuzhiyun  *    the properly cased little endian Unicode name. We convert the name to the
71*4882a593Smuzhiyun  *    current NLS code page, search if a dentry with this name already exists
72*4882a593Smuzhiyun  *    and if so return that instead of @dent.  At this point things are
73*4882a593Smuzhiyun  *    complicated by the possibility of 'disconnected' dentries due to NFS
74*4882a593Smuzhiyun  *    which we deal with appropriately (see the code comments).  The VFS will
75*4882a593Smuzhiyun  *    then destroy the old @dent and use the one we returned.  If a dentry is
76*4882a593Smuzhiyun  *    not found, we allocate a new one, d_splice_alias() it, and return it as
77*4882a593Smuzhiyun  *    above.
78*4882a593Smuzhiyun  * 3) @dent matches either perfectly or not (i.e. we don't care about case) a
79*4882a593Smuzhiyun  *    directory entry with a file name in the DOS namespace. In this case
80*4882a593Smuzhiyun  *    ntfs_lookup_inode_by_name() will return with name set to point to a
81*4882a593Smuzhiyun  *    kmalloc()ed ntfs_name structure containing the mft reference (cpu endian)
82*4882a593Smuzhiyun  *    of the inode. We use the mft reference to read the inode and to find the
83*4882a593Smuzhiyun  *    file name in the WIN32 namespace corresponding to the matched short file
84*4882a593Smuzhiyun  *    name. We then convert the name to the current NLS code page, and proceed
85*4882a593Smuzhiyun  *    searching for a dentry with this name, etc, as in case 2), above.
86*4882a593Smuzhiyun  *
87*4882a593Smuzhiyun  * Locking: Caller must hold i_mutex on the directory.
88*4882a593Smuzhiyun  */
ntfs_lookup(struct inode * dir_ino,struct dentry * dent,unsigned int flags)89*4882a593Smuzhiyun static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent,
90*4882a593Smuzhiyun 		unsigned int flags)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	ntfs_volume *vol = NTFS_SB(dir_ino->i_sb);
93*4882a593Smuzhiyun 	struct inode *dent_inode;
94*4882a593Smuzhiyun 	ntfschar *uname;
95*4882a593Smuzhiyun 	ntfs_name *name = NULL;
96*4882a593Smuzhiyun 	MFT_REF mref;
97*4882a593Smuzhiyun 	unsigned long dent_ino;
98*4882a593Smuzhiyun 	int uname_len;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	ntfs_debug("Looking up %pd in directory inode 0x%lx.",
101*4882a593Smuzhiyun 			dent, dir_ino->i_ino);
102*4882a593Smuzhiyun 	/* Convert the name of the dentry to Unicode. */
103*4882a593Smuzhiyun 	uname_len = ntfs_nlstoucs(vol, dent->d_name.name, dent->d_name.len,
104*4882a593Smuzhiyun 			&uname);
105*4882a593Smuzhiyun 	if (uname_len < 0) {
106*4882a593Smuzhiyun 		if (uname_len != -ENAMETOOLONG)
107*4882a593Smuzhiyun 			ntfs_error(vol->sb, "Failed to convert name to "
108*4882a593Smuzhiyun 					"Unicode.");
109*4882a593Smuzhiyun 		return ERR_PTR(uname_len);
110*4882a593Smuzhiyun 	}
111*4882a593Smuzhiyun 	mref = ntfs_lookup_inode_by_name(NTFS_I(dir_ino), uname, uname_len,
112*4882a593Smuzhiyun 			&name);
113*4882a593Smuzhiyun 	kmem_cache_free(ntfs_name_cache, uname);
114*4882a593Smuzhiyun 	if (!IS_ERR_MREF(mref)) {
115*4882a593Smuzhiyun 		dent_ino = MREF(mref);
116*4882a593Smuzhiyun 		ntfs_debug("Found inode 0x%lx. Calling ntfs_iget.", dent_ino);
117*4882a593Smuzhiyun 		dent_inode = ntfs_iget(vol->sb, dent_ino);
118*4882a593Smuzhiyun 		if (!IS_ERR(dent_inode)) {
119*4882a593Smuzhiyun 			/* Consistency check. */
120*4882a593Smuzhiyun 			if (is_bad_inode(dent_inode) || MSEQNO(mref) ==
121*4882a593Smuzhiyun 					NTFS_I(dent_inode)->seq_no ||
122*4882a593Smuzhiyun 					dent_ino == FILE_MFT) {
123*4882a593Smuzhiyun 				/* Perfect WIN32/POSIX match. -- Case 1. */
124*4882a593Smuzhiyun 				if (!name) {
125*4882a593Smuzhiyun 					ntfs_debug("Done.  (Case 1.)");
126*4882a593Smuzhiyun 					return d_splice_alias(dent_inode, dent);
127*4882a593Smuzhiyun 				}
128*4882a593Smuzhiyun 				/*
129*4882a593Smuzhiyun 				 * We are too indented.  Handle imperfect
130*4882a593Smuzhiyun 				 * matches and short file names further below.
131*4882a593Smuzhiyun 				 */
132*4882a593Smuzhiyun 				goto handle_name;
133*4882a593Smuzhiyun 			}
134*4882a593Smuzhiyun 			ntfs_error(vol->sb, "Found stale reference to inode "
135*4882a593Smuzhiyun 					"0x%lx (reference sequence number = "
136*4882a593Smuzhiyun 					"0x%x, inode sequence number = 0x%x), "
137*4882a593Smuzhiyun 					"returning -EIO. Run chkdsk.",
138*4882a593Smuzhiyun 					dent_ino, MSEQNO(mref),
139*4882a593Smuzhiyun 					NTFS_I(dent_inode)->seq_no);
140*4882a593Smuzhiyun 			iput(dent_inode);
141*4882a593Smuzhiyun 			dent_inode = ERR_PTR(-EIO);
142*4882a593Smuzhiyun 		} else
143*4882a593Smuzhiyun 			ntfs_error(vol->sb, "ntfs_iget(0x%lx) failed with "
144*4882a593Smuzhiyun 					"error code %li.", dent_ino,
145*4882a593Smuzhiyun 					PTR_ERR(dent_inode));
146*4882a593Smuzhiyun 		kfree(name);
147*4882a593Smuzhiyun 		/* Return the error code. */
148*4882a593Smuzhiyun 		return ERR_CAST(dent_inode);
149*4882a593Smuzhiyun 	}
150*4882a593Smuzhiyun 	/* It is guaranteed that @name is no longer allocated at this point. */
151*4882a593Smuzhiyun 	if (MREF_ERR(mref) == -ENOENT) {
152*4882a593Smuzhiyun 		ntfs_debug("Entry was not found, adding negative dentry.");
153*4882a593Smuzhiyun 		/* The dcache will handle negative entries. */
154*4882a593Smuzhiyun 		d_add(dent, NULL);
155*4882a593Smuzhiyun 		ntfs_debug("Done.");
156*4882a593Smuzhiyun 		return NULL;
157*4882a593Smuzhiyun 	}
158*4882a593Smuzhiyun 	ntfs_error(vol->sb, "ntfs_lookup_ino_by_name() failed with error "
159*4882a593Smuzhiyun 			"code %i.", -MREF_ERR(mref));
160*4882a593Smuzhiyun 	return ERR_PTR(MREF_ERR(mref));
161*4882a593Smuzhiyun 	// TODO: Consider moving this lot to a separate function! (AIA)
162*4882a593Smuzhiyun handle_name:
163*4882a593Smuzhiyun    {
164*4882a593Smuzhiyun 	MFT_RECORD *m;
165*4882a593Smuzhiyun 	ntfs_attr_search_ctx *ctx;
166*4882a593Smuzhiyun 	ntfs_inode *ni = NTFS_I(dent_inode);
167*4882a593Smuzhiyun 	int err;
168*4882a593Smuzhiyun 	struct qstr nls_name;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	nls_name.name = NULL;
171*4882a593Smuzhiyun 	if (name->type != FILE_NAME_DOS) {			/* Case 2. */
172*4882a593Smuzhiyun 		ntfs_debug("Case 2.");
173*4882a593Smuzhiyun 		nls_name.len = (unsigned)ntfs_ucstonls(vol,
174*4882a593Smuzhiyun 				(ntfschar*)&name->name, name->len,
175*4882a593Smuzhiyun 				(unsigned char**)&nls_name.name, 0);
176*4882a593Smuzhiyun 		kfree(name);
177*4882a593Smuzhiyun 	} else /* if (name->type == FILE_NAME_DOS) */ {		/* Case 3. */
178*4882a593Smuzhiyun 		FILE_NAME_ATTR *fn;
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 		ntfs_debug("Case 3.");
181*4882a593Smuzhiyun 		kfree(name);
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 		/* Find the WIN32 name corresponding to the matched DOS name. */
184*4882a593Smuzhiyun 		ni = NTFS_I(dent_inode);
185*4882a593Smuzhiyun 		m = map_mft_record(ni);
186*4882a593Smuzhiyun 		if (IS_ERR(m)) {
187*4882a593Smuzhiyun 			err = PTR_ERR(m);
188*4882a593Smuzhiyun 			m = NULL;
189*4882a593Smuzhiyun 			ctx = NULL;
190*4882a593Smuzhiyun 			goto err_out;
191*4882a593Smuzhiyun 		}
192*4882a593Smuzhiyun 		ctx = ntfs_attr_get_search_ctx(ni, m);
193*4882a593Smuzhiyun 		if (unlikely(!ctx)) {
194*4882a593Smuzhiyun 			err = -ENOMEM;
195*4882a593Smuzhiyun 			goto err_out;
196*4882a593Smuzhiyun 		}
197*4882a593Smuzhiyun 		do {
198*4882a593Smuzhiyun 			ATTR_RECORD *a;
199*4882a593Smuzhiyun 			u32 val_len;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 			err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0,
202*4882a593Smuzhiyun 					NULL, 0, ctx);
203*4882a593Smuzhiyun 			if (unlikely(err)) {
204*4882a593Smuzhiyun 				ntfs_error(vol->sb, "Inode corrupt: No WIN32 "
205*4882a593Smuzhiyun 						"namespace counterpart to DOS "
206*4882a593Smuzhiyun 						"file name. Run chkdsk.");
207*4882a593Smuzhiyun 				if (err == -ENOENT)
208*4882a593Smuzhiyun 					err = -EIO;
209*4882a593Smuzhiyun 				goto err_out;
210*4882a593Smuzhiyun 			}
211*4882a593Smuzhiyun 			/* Consistency checks. */
212*4882a593Smuzhiyun 			a = ctx->attr;
213*4882a593Smuzhiyun 			if (a->non_resident || a->flags)
214*4882a593Smuzhiyun 				goto eio_err_out;
215*4882a593Smuzhiyun 			val_len = le32_to_cpu(a->data.resident.value_length);
216*4882a593Smuzhiyun 			if (le16_to_cpu(a->data.resident.value_offset) +
217*4882a593Smuzhiyun 					val_len > le32_to_cpu(a->length))
218*4882a593Smuzhiyun 				goto eio_err_out;
219*4882a593Smuzhiyun 			fn = (FILE_NAME_ATTR*)((u8*)ctx->attr + le16_to_cpu(
220*4882a593Smuzhiyun 					ctx->attr->data.resident.value_offset));
221*4882a593Smuzhiyun 			if ((u32)(fn->file_name_length * sizeof(ntfschar) +
222*4882a593Smuzhiyun 					sizeof(FILE_NAME_ATTR)) > val_len)
223*4882a593Smuzhiyun 				goto eio_err_out;
224*4882a593Smuzhiyun 		} while (fn->file_name_type != FILE_NAME_WIN32);
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 		/* Convert the found WIN32 name to current NLS code page. */
227*4882a593Smuzhiyun 		nls_name.len = (unsigned)ntfs_ucstonls(vol,
228*4882a593Smuzhiyun 				(ntfschar*)&fn->file_name, fn->file_name_length,
229*4882a593Smuzhiyun 				(unsigned char**)&nls_name.name, 0);
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 		ntfs_attr_put_search_ctx(ctx);
232*4882a593Smuzhiyun 		unmap_mft_record(ni);
233*4882a593Smuzhiyun 	}
234*4882a593Smuzhiyun 	m = NULL;
235*4882a593Smuzhiyun 	ctx = NULL;
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	/* Check if a conversion error occurred. */
238*4882a593Smuzhiyun 	if ((signed)nls_name.len < 0) {
239*4882a593Smuzhiyun 		err = (signed)nls_name.len;
240*4882a593Smuzhiyun 		goto err_out;
241*4882a593Smuzhiyun 	}
242*4882a593Smuzhiyun 	nls_name.hash = full_name_hash(dent, nls_name.name, nls_name.len);
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	dent = d_add_ci(dent, dent_inode, &nls_name);
245*4882a593Smuzhiyun 	kfree(nls_name.name);
246*4882a593Smuzhiyun 	return dent;
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun eio_err_out:
249*4882a593Smuzhiyun 	ntfs_error(vol->sb, "Illegal file name attribute. Run chkdsk.");
250*4882a593Smuzhiyun 	err = -EIO;
251*4882a593Smuzhiyun err_out:
252*4882a593Smuzhiyun 	if (ctx)
253*4882a593Smuzhiyun 		ntfs_attr_put_search_ctx(ctx);
254*4882a593Smuzhiyun 	if (m)
255*4882a593Smuzhiyun 		unmap_mft_record(ni);
256*4882a593Smuzhiyun 	iput(dent_inode);
257*4882a593Smuzhiyun 	ntfs_error(vol->sb, "Failed, returning error code %i.", err);
258*4882a593Smuzhiyun 	return ERR_PTR(err);
259*4882a593Smuzhiyun    }
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun /**
263*4882a593Smuzhiyun  * Inode operations for directories.
264*4882a593Smuzhiyun  */
265*4882a593Smuzhiyun const struct inode_operations ntfs_dir_inode_ops = {
266*4882a593Smuzhiyun 	.lookup	= ntfs_lookup,	/* VFS: Lookup directory. */
267*4882a593Smuzhiyun };
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun /**
270*4882a593Smuzhiyun  * ntfs_get_parent - find the dentry of the parent of a given directory dentry
271*4882a593Smuzhiyun  * @child_dent:		dentry of the directory whose parent directory to find
272*4882a593Smuzhiyun  *
273*4882a593Smuzhiyun  * Find the dentry for the parent directory of the directory specified by the
274*4882a593Smuzhiyun  * dentry @child_dent.  This function is called from
275*4882a593Smuzhiyun  * fs/exportfs/expfs.c::find_exported_dentry() which in turn is called from the
276*4882a593Smuzhiyun  * default ->decode_fh() which is export_decode_fh() in the same file.
277*4882a593Smuzhiyun  *
278*4882a593Smuzhiyun  * The code is based on the ext3 ->get_parent() implementation found in
279*4882a593Smuzhiyun  * fs/ext3/namei.c::ext3_get_parent().
280*4882a593Smuzhiyun  *
281*4882a593Smuzhiyun  * Note: ntfs_get_parent() is called with @d_inode(child_dent)->i_mutex down.
282*4882a593Smuzhiyun  *
283*4882a593Smuzhiyun  * Return the dentry of the parent directory on success or the error code on
284*4882a593Smuzhiyun  * error (IS_ERR() is true).
285*4882a593Smuzhiyun  */
ntfs_get_parent(struct dentry * child_dent)286*4882a593Smuzhiyun static struct dentry *ntfs_get_parent(struct dentry *child_dent)
287*4882a593Smuzhiyun {
288*4882a593Smuzhiyun 	struct inode *vi = d_inode(child_dent);
289*4882a593Smuzhiyun 	ntfs_inode *ni = NTFS_I(vi);
290*4882a593Smuzhiyun 	MFT_RECORD *mrec;
291*4882a593Smuzhiyun 	ntfs_attr_search_ctx *ctx;
292*4882a593Smuzhiyun 	ATTR_RECORD *attr;
293*4882a593Smuzhiyun 	FILE_NAME_ATTR *fn;
294*4882a593Smuzhiyun 	unsigned long parent_ino;
295*4882a593Smuzhiyun 	int err;
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
298*4882a593Smuzhiyun 	/* Get the mft record of the inode belonging to the child dentry. */
299*4882a593Smuzhiyun 	mrec = map_mft_record(ni);
300*4882a593Smuzhiyun 	if (IS_ERR(mrec))
301*4882a593Smuzhiyun 		return ERR_CAST(mrec);
302*4882a593Smuzhiyun 	/* Find the first file name attribute in the mft record. */
303*4882a593Smuzhiyun 	ctx = ntfs_attr_get_search_ctx(ni, mrec);
304*4882a593Smuzhiyun 	if (unlikely(!ctx)) {
305*4882a593Smuzhiyun 		unmap_mft_record(ni);
306*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
307*4882a593Smuzhiyun 	}
308*4882a593Smuzhiyun try_next:
309*4882a593Smuzhiyun 	err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, CASE_SENSITIVE, 0, NULL,
310*4882a593Smuzhiyun 			0, ctx);
311*4882a593Smuzhiyun 	if (unlikely(err)) {
312*4882a593Smuzhiyun 		ntfs_attr_put_search_ctx(ctx);
313*4882a593Smuzhiyun 		unmap_mft_record(ni);
314*4882a593Smuzhiyun 		if (err == -ENOENT)
315*4882a593Smuzhiyun 			ntfs_error(vi->i_sb, "Inode 0x%lx does not have a "
316*4882a593Smuzhiyun 					"file name attribute.  Run chkdsk.",
317*4882a593Smuzhiyun 					vi->i_ino);
318*4882a593Smuzhiyun 		return ERR_PTR(err);
319*4882a593Smuzhiyun 	}
320*4882a593Smuzhiyun 	attr = ctx->attr;
321*4882a593Smuzhiyun 	if (unlikely(attr->non_resident))
322*4882a593Smuzhiyun 		goto try_next;
323*4882a593Smuzhiyun 	fn = (FILE_NAME_ATTR *)((u8 *)attr +
324*4882a593Smuzhiyun 			le16_to_cpu(attr->data.resident.value_offset));
325*4882a593Smuzhiyun 	if (unlikely((u8 *)fn + le32_to_cpu(attr->data.resident.value_length) >
326*4882a593Smuzhiyun 			(u8*)attr + le32_to_cpu(attr->length)))
327*4882a593Smuzhiyun 		goto try_next;
328*4882a593Smuzhiyun 	/* Get the inode number of the parent directory. */
329*4882a593Smuzhiyun 	parent_ino = MREF_LE(fn->parent_directory);
330*4882a593Smuzhiyun 	/* Release the search context and the mft record of the child. */
331*4882a593Smuzhiyun 	ntfs_attr_put_search_ctx(ctx);
332*4882a593Smuzhiyun 	unmap_mft_record(ni);
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	return d_obtain_alias(ntfs_iget(vi->i_sb, parent_ino));
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun 
ntfs_nfs_get_inode(struct super_block * sb,u64 ino,u32 generation)337*4882a593Smuzhiyun static struct inode *ntfs_nfs_get_inode(struct super_block *sb,
338*4882a593Smuzhiyun 		u64 ino, u32 generation)
339*4882a593Smuzhiyun {
340*4882a593Smuzhiyun 	struct inode *inode;
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	inode = ntfs_iget(sb, ino);
343*4882a593Smuzhiyun 	if (!IS_ERR(inode)) {
344*4882a593Smuzhiyun 		if (is_bad_inode(inode) || inode->i_generation != generation) {
345*4882a593Smuzhiyun 			iput(inode);
346*4882a593Smuzhiyun 			inode = ERR_PTR(-ESTALE);
347*4882a593Smuzhiyun 		}
348*4882a593Smuzhiyun 	}
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	return inode;
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun 
ntfs_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)353*4882a593Smuzhiyun static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
354*4882a593Smuzhiyun 		int fh_len, int fh_type)
355*4882a593Smuzhiyun {
356*4882a593Smuzhiyun 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
357*4882a593Smuzhiyun 				    ntfs_nfs_get_inode);
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun 
ntfs_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)360*4882a593Smuzhiyun static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
361*4882a593Smuzhiyun 		int fh_len, int fh_type)
362*4882a593Smuzhiyun {
363*4882a593Smuzhiyun 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
364*4882a593Smuzhiyun 				    ntfs_nfs_get_inode);
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun /**
368*4882a593Smuzhiyun  * Export operations allowing NFS exporting of mounted NTFS partitions.
369*4882a593Smuzhiyun  *
370*4882a593Smuzhiyun  * We use the default ->encode_fh() for now.  Note that they
371*4882a593Smuzhiyun  * use 32 bits to store the inode number which is an unsigned long so on 64-bit
372*4882a593Smuzhiyun  * architectures is usually 64 bits so it would all fail horribly on huge
373*4882a593Smuzhiyun  * volumes.  I guess we need to define our own encode and decode fh functions
374*4882a593Smuzhiyun  * that store 64-bit inode numbers at some point but for now we will ignore the
375*4882a593Smuzhiyun  * problem...
376*4882a593Smuzhiyun  *
377*4882a593Smuzhiyun  * We also use the default ->get_name() helper (used by ->decode_fh() via
378*4882a593Smuzhiyun  * fs/exportfs/expfs.c::find_exported_dentry()) as that is completely fs
379*4882a593Smuzhiyun  * independent.
380*4882a593Smuzhiyun  *
381*4882a593Smuzhiyun  * The default ->get_parent() just returns -EACCES so we have to provide our
382*4882a593Smuzhiyun  * own and the default ->get_dentry() is incompatible with NTFS due to not
383*4882a593Smuzhiyun  * allowing the inode number 0 which is used in NTFS for the system file $MFT
384*4882a593Smuzhiyun  * and due to using iget() whereas NTFS needs ntfs_iget().
385*4882a593Smuzhiyun  */
386*4882a593Smuzhiyun const struct export_operations ntfs_export_ops = {
387*4882a593Smuzhiyun 	.get_parent	= ntfs_get_parent,	/* Find the parent of a given
388*4882a593Smuzhiyun 						   directory. */
389*4882a593Smuzhiyun 	.fh_to_dentry	= ntfs_fh_to_dentry,
390*4882a593Smuzhiyun 	.fh_to_parent	= ntfs_fh_to_parent,
391*4882a593Smuzhiyun };
392