xref: /openbmc/linux/fs/ntfs/namei.c (revision aa0b42b7)
1a1d312deSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * namei.c - NTFS kernel directory inode operations. Part of the Linux-NTFS
41da177e4SLinus Torvalds  *	     project.
51da177e4SLinus Torvalds  *
667b1dfe7SAnton Altaparmakov  * Copyright (c) 2001-2006 Anton Altaparmakov
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/dcache.h>
10a5694255SChristoph Hellwig #include <linux/exportfs.h>
111da177e4SLinus Torvalds #include <linux/security.h>
125a0e3ad6STejun Heo #include <linux/slab.h>
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #include "attrib.h"
151da177e4SLinus Torvalds #include "debug.h"
161da177e4SLinus Torvalds #include "dir.h"
171da177e4SLinus Torvalds #include "mft.h"
181da177e4SLinus Torvalds #include "ntfs.h"
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds /**
211da177e4SLinus Torvalds  * ntfs_lookup - find the inode represented by a dentry in a directory inode
221da177e4SLinus Torvalds  * @dir_ino:	directory inode in which to look for the inode
231da177e4SLinus Torvalds  * @dent:	dentry representing the inode to look for
2489076bc3SAl Viro  * @flags:	lookup flags
251da177e4SLinus Torvalds  *
261da177e4SLinus Torvalds  * In short, ntfs_lookup() looks for the inode represented by the dentry @dent
271da177e4SLinus Torvalds  * in the directory inode @dir_ino and if found attaches the inode to the
281da177e4SLinus Torvalds  * dentry @dent.
291da177e4SLinus Torvalds  *
301da177e4SLinus Torvalds  * In more detail, the dentry @dent specifies which inode to look for by
311da177e4SLinus Torvalds  * supplying the name of the inode in @dent->d_name.name. ntfs_lookup()
321da177e4SLinus Torvalds  * converts the name to Unicode and walks the contents of the directory inode
331da177e4SLinus Torvalds  * @dir_ino looking for the converted Unicode name. If the name is found in the
341da177e4SLinus Torvalds  * directory, the corresponding inode is loaded by calling ntfs_iget() on its
351da177e4SLinus Torvalds  * inode number and the inode is associated with the dentry @dent via a call to
361da177e4SLinus Torvalds  * d_splice_alias().
371da177e4SLinus Torvalds  *
381da177e4SLinus Torvalds  * If the name is not found in the directory, a NULL inode is inserted into the
391da177e4SLinus Torvalds  * dentry @dent via a call to d_add(). The dentry is then termed a negative
401da177e4SLinus Torvalds  * dentry.
411da177e4SLinus Torvalds  *
421da177e4SLinus Torvalds  * Only if an actual error occurs, do we return an error via ERR_PTR().
431da177e4SLinus Torvalds  *
441da177e4SLinus Torvalds  * In order to handle the case insensitivity issues of NTFS with regards to the
451da177e4SLinus Torvalds  * dcache and the dcache requiring only one dentry per directory, we deal with
461da177e4SLinus Torvalds  * dentry aliases that only differ in case in ->ntfs_lookup() while maintaining
471da177e4SLinus Torvalds  * a case sensitive dcache. This means that we get the full benefit of dcache
481da177e4SLinus Torvalds  * speed when the file/directory is looked up with the same case as returned by
491da177e4SLinus Torvalds  * ->ntfs_readdir() but that a lookup for any other case (or for the short file
501da177e4SLinus Torvalds  * name) will not find anything in dcache and will enter ->ntfs_lookup()
511da177e4SLinus Torvalds  * instead, where we search the directory for a fully matching file name
521da177e4SLinus Torvalds  * (including case) and if that is not found, we search for a file name that
531da177e4SLinus Torvalds  * matches with different case and if that has non-POSIX semantics we return
541da177e4SLinus Torvalds  * that. We actually do only one search (case sensitive) and keep tabs on
551da177e4SLinus Torvalds  * whether we have found a case insensitive match in the process.
561da177e4SLinus Torvalds  *
571da177e4SLinus Torvalds  * To simplify matters for us, we do not treat the short vs long filenames as
581da177e4SLinus Torvalds  * two hard links but instead if the lookup matches a short filename, we
591da177e4SLinus Torvalds  * return the dentry for the corresponding long filename instead.
601da177e4SLinus Torvalds  *
611da177e4SLinus Torvalds  * There are three cases we need to distinguish here:
621da177e4SLinus Torvalds  *
631da177e4SLinus Torvalds  * 1) @dent perfectly matches (i.e. including case) a directory entry with a
641da177e4SLinus Torvalds  *    file name in the WIN32 or POSIX namespaces. In this case
651da177e4SLinus Torvalds  *    ntfs_lookup_inode_by_name() will return with name set to NULL and we
661da177e4SLinus Torvalds  *    just d_splice_alias() @dent.
671da177e4SLinus Torvalds  * 2) @dent matches (not including case) a directory entry with a file name in
681da177e4SLinus Torvalds  *    the WIN32 namespace. In this case ntfs_lookup_inode_by_name() will return
691da177e4SLinus Torvalds  *    with name set to point to a kmalloc()ed ntfs_name structure containing
701da177e4SLinus Torvalds  *    the properly cased little endian Unicode name. We convert the name to the
711da177e4SLinus Torvalds  *    current NLS code page, search if a dentry with this name already exists
721da177e4SLinus Torvalds  *    and if so return that instead of @dent.  At this point things are
731da177e4SLinus Torvalds  *    complicated by the possibility of 'disconnected' dentries due to NFS
741da177e4SLinus Torvalds  *    which we deal with appropriately (see the code comments).  The VFS will
751da177e4SLinus Torvalds  *    then destroy the old @dent and use the one we returned.  If a dentry is
761da177e4SLinus Torvalds  *    not found, we allocate a new one, d_splice_alias() it, and return it as
771da177e4SLinus Torvalds  *    above.
781da177e4SLinus Torvalds  * 3) @dent matches either perfectly or not (i.e. we don't care about case) a
791da177e4SLinus Torvalds  *    directory entry with a file name in the DOS namespace. In this case
801da177e4SLinus Torvalds  *    ntfs_lookup_inode_by_name() will return with name set to point to a
811da177e4SLinus Torvalds  *    kmalloc()ed ntfs_name structure containing the mft reference (cpu endian)
821da177e4SLinus Torvalds  *    of the inode. We use the mft reference to read the inode and to find the
831da177e4SLinus Torvalds  *    file name in the WIN32 namespace corresponding to the matched short file
841da177e4SLinus Torvalds  *    name. We then convert the name to the current NLS code page, and proceed
851da177e4SLinus Torvalds  *    searching for a dentry with this name, etc, as in case 2), above.
861da177e4SLinus Torvalds  *
871b1dcc1bSJes Sorensen  * Locking: Caller must hold i_mutex on the directory.
881da177e4SLinus Torvalds  */
ntfs_lookup(struct inode * dir_ino,struct dentry * dent,unsigned int flags)891da177e4SLinus Torvalds static struct dentry *ntfs_lookup(struct inode *dir_ino, struct dentry *dent,
9000cd8dd3SAl Viro 		unsigned int flags)
911da177e4SLinus Torvalds {
921da177e4SLinus Torvalds 	ntfs_volume *vol = NTFS_SB(dir_ino->i_sb);
931da177e4SLinus Torvalds 	struct inode *dent_inode;
941da177e4SLinus Torvalds 	ntfschar *uname;
951da177e4SLinus Torvalds 	ntfs_name *name = NULL;
961da177e4SLinus Torvalds 	MFT_REF mref;
971da177e4SLinus Torvalds 	unsigned long dent_ino;
981da177e4SLinus Torvalds 	int uname_len;
991da177e4SLinus Torvalds 
100a455589fSAl Viro 	ntfs_debug("Looking up %pd in directory inode 0x%lx.",
101a455589fSAl Viro 			dent, dir_ino->i_ino);
1021da177e4SLinus Torvalds 	/* Convert the name of the dentry to Unicode. */
1031da177e4SLinus Torvalds 	uname_len = ntfs_nlstoucs(vol, dent->d_name.name, dent->d_name.len,
1041da177e4SLinus Torvalds 			&uname);
1051da177e4SLinus Torvalds 	if (uname_len < 0) {
106834ba600SAnton Altaparmakov 		if (uname_len != -ENAMETOOLONG)
107834ba600SAnton Altaparmakov 			ntfs_error(vol->sb, "Failed to convert name to "
108834ba600SAnton Altaparmakov 					"Unicode.");
1091da177e4SLinus Torvalds 		return ERR_PTR(uname_len);
1101da177e4SLinus Torvalds 	}
1111da177e4SLinus Torvalds 	mref = ntfs_lookup_inode_by_name(NTFS_I(dir_ino), uname, uname_len,
1121da177e4SLinus Torvalds 			&name);
1131da177e4SLinus Torvalds 	kmem_cache_free(ntfs_name_cache, uname);
1141da177e4SLinus Torvalds 	if (!IS_ERR_MREF(mref)) {
1151da177e4SLinus Torvalds 		dent_ino = MREF(mref);
1161da177e4SLinus Torvalds 		ntfs_debug("Found inode 0x%lx. Calling ntfs_iget.", dent_ino);
1171da177e4SLinus Torvalds 		dent_inode = ntfs_iget(vol->sb, dent_ino);
118cc22c800SDenis Efremov 		if (!IS_ERR(dent_inode)) {
1191da177e4SLinus Torvalds 			/* Consistency check. */
1201da177e4SLinus Torvalds 			if (is_bad_inode(dent_inode) || MSEQNO(mref) ==
1211da177e4SLinus Torvalds 					NTFS_I(dent_inode)->seq_no ||
1221da177e4SLinus Torvalds 					dent_ino == FILE_MFT) {
1231da177e4SLinus Torvalds 				/* Perfect WIN32/POSIX match. -- Case 1. */
1241da177e4SLinus Torvalds 				if (!name) {
1251da177e4SLinus Torvalds 					ntfs_debug("Done.  (Case 1.)");
1261da177e4SLinus Torvalds 					return d_splice_alias(dent_inode, dent);
1271da177e4SLinus Torvalds 				}
1281da177e4SLinus Torvalds 				/*
1291da177e4SLinus Torvalds 				 * We are too indented.  Handle imperfect
1301da177e4SLinus Torvalds 				 * matches and short file names further below.
1311da177e4SLinus Torvalds 				 */
1321da177e4SLinus Torvalds 				goto handle_name;
1331da177e4SLinus Torvalds 			}
1341da177e4SLinus Torvalds 			ntfs_error(vol->sb, "Found stale reference to inode "
1351da177e4SLinus Torvalds 					"0x%lx (reference sequence number = "
1361da177e4SLinus Torvalds 					"0x%x, inode sequence number = 0x%x), "
1371da177e4SLinus Torvalds 					"returning -EIO. Run chkdsk.",
1381da177e4SLinus Torvalds 					dent_ino, MSEQNO(mref),
1391da177e4SLinus Torvalds 					NTFS_I(dent_inode)->seq_no);
1401da177e4SLinus Torvalds 			iput(dent_inode);
1411da177e4SLinus Torvalds 			dent_inode = ERR_PTR(-EIO);
1421da177e4SLinus Torvalds 		} else
1431da177e4SLinus Torvalds 			ntfs_error(vol->sb, "ntfs_iget(0x%lx) failed with "
1441da177e4SLinus Torvalds 					"error code %li.", dent_ino,
1451da177e4SLinus Torvalds 					PTR_ERR(dent_inode));
1461da177e4SLinus Torvalds 		kfree(name);
1471da177e4SLinus Torvalds 		/* Return the error code. */
148fee2aa75SKees Cook 		return ERR_CAST(dent_inode);
1491da177e4SLinus Torvalds 	}
150834ba600SAnton Altaparmakov 	/* It is guaranteed that @name is no longer allocated at this point. */
1511da177e4SLinus Torvalds 	if (MREF_ERR(mref) == -ENOENT) {
1521da177e4SLinus Torvalds 		ntfs_debug("Entry was not found, adding negative dentry.");
1531da177e4SLinus Torvalds 		/* The dcache will handle negative entries. */
1541da177e4SLinus Torvalds 		d_add(dent, NULL);
1551da177e4SLinus Torvalds 		ntfs_debug("Done.");
1561da177e4SLinus Torvalds 		return NULL;
1571da177e4SLinus Torvalds 	}
1581da177e4SLinus Torvalds 	ntfs_error(vol->sb, "ntfs_lookup_ino_by_name() failed with error "
1591da177e4SLinus Torvalds 			"code %i.", -MREF_ERR(mref));
1601da177e4SLinus Torvalds 	return ERR_PTR(MREF_ERR(mref));
1611da177e4SLinus Torvalds 	// TODO: Consider moving this lot to a separate function! (AIA)
1621da177e4SLinus Torvalds handle_name:
1631da177e4SLinus Torvalds    {
1641da177e4SLinus Torvalds 	MFT_RECORD *m;
1651da177e4SLinus Torvalds 	ntfs_attr_search_ctx *ctx;
1661da177e4SLinus Torvalds 	ntfs_inode *ni = NTFS_I(dent_inode);
1671da177e4SLinus Torvalds 	int err;
1681da177e4SLinus Torvalds 	struct qstr nls_name;
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 	nls_name.name = NULL;
1711da177e4SLinus Torvalds 	if (name->type != FILE_NAME_DOS) {			/* Case 2. */
1721da177e4SLinus Torvalds 		ntfs_debug("Case 2.");
1731da177e4SLinus Torvalds 		nls_name.len = (unsigned)ntfs_ucstonls(vol,
1741da177e4SLinus Torvalds 				(ntfschar*)&name->name, name->len,
1751da177e4SLinus Torvalds 				(unsigned char**)&nls_name.name, 0);
1761da177e4SLinus Torvalds 		kfree(name);
1771da177e4SLinus Torvalds 	} else /* if (name->type == FILE_NAME_DOS) */ {		/* Case 3. */
1781da177e4SLinus Torvalds 		FILE_NAME_ATTR *fn;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 		ntfs_debug("Case 3.");
1811da177e4SLinus Torvalds 		kfree(name);
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 		/* Find the WIN32 name corresponding to the matched DOS name. */
1841da177e4SLinus Torvalds 		ni = NTFS_I(dent_inode);
1851da177e4SLinus Torvalds 		m = map_mft_record(ni);
1861da177e4SLinus Torvalds 		if (IS_ERR(m)) {
1871da177e4SLinus Torvalds 			err = PTR_ERR(m);
1881da177e4SLinus Torvalds 			m = NULL;
1891da177e4SLinus Torvalds 			ctx = NULL;
1901da177e4SLinus Torvalds 			goto err_out;
1911da177e4SLinus Torvalds 		}
1921da177e4SLinus Torvalds 		ctx = ntfs_attr_get_search_ctx(ni, m);
1931da177e4SLinus Torvalds 		if (unlikely(!ctx)) {
1941da177e4SLinus Torvalds 			err = -ENOMEM;
1951da177e4SLinus Torvalds 			goto err_out;
1961da177e4SLinus Torvalds 		}
1971da177e4SLinus Torvalds 		do {
1981da177e4SLinus Torvalds 			ATTR_RECORD *a;
1991da177e4SLinus Torvalds 			u32 val_len;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 			err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0,
2021da177e4SLinus Torvalds 					NULL, 0, ctx);
2031da177e4SLinus Torvalds 			if (unlikely(err)) {
2041da177e4SLinus Torvalds 				ntfs_error(vol->sb, "Inode corrupt: No WIN32 "
2051da177e4SLinus Torvalds 						"namespace counterpart to DOS "
2061da177e4SLinus Torvalds 						"file name. Run chkdsk.");
2071da177e4SLinus Torvalds 				if (err == -ENOENT)
2081da177e4SLinus Torvalds 					err = -EIO;
2091da177e4SLinus Torvalds 				goto err_out;
2101da177e4SLinus Torvalds 			}
2111da177e4SLinus Torvalds 			/* Consistency checks. */
2121da177e4SLinus Torvalds 			a = ctx->attr;
2131da177e4SLinus Torvalds 			if (a->non_resident || a->flags)
2141da177e4SLinus Torvalds 				goto eio_err_out;
2151da177e4SLinus Torvalds 			val_len = le32_to_cpu(a->data.resident.value_length);
2161da177e4SLinus Torvalds 			if (le16_to_cpu(a->data.resident.value_offset) +
2171da177e4SLinus Torvalds 					val_len > le32_to_cpu(a->length))
2181da177e4SLinus Torvalds 				goto eio_err_out;
2191da177e4SLinus Torvalds 			fn = (FILE_NAME_ATTR*)((u8*)ctx->attr + le16_to_cpu(
2201da177e4SLinus Torvalds 					ctx->attr->data.resident.value_offset));
2211da177e4SLinus Torvalds 			if ((u32)(fn->file_name_length * sizeof(ntfschar) +
2221da177e4SLinus Torvalds 					sizeof(FILE_NAME_ATTR)) > val_len)
2231da177e4SLinus Torvalds 				goto eio_err_out;
2241da177e4SLinus Torvalds 		} while (fn->file_name_type != FILE_NAME_WIN32);
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 		/* Convert the found WIN32 name to current NLS code page. */
2271da177e4SLinus Torvalds 		nls_name.len = (unsigned)ntfs_ucstonls(vol,
2281da177e4SLinus Torvalds 				(ntfschar*)&fn->file_name, fn->file_name_length,
2291da177e4SLinus Torvalds 				(unsigned char**)&nls_name.name, 0);
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 		ntfs_attr_put_search_ctx(ctx);
2321da177e4SLinus Torvalds 		unmap_mft_record(ni);
2331da177e4SLinus Torvalds 	}
2341da177e4SLinus Torvalds 	m = NULL;
2351da177e4SLinus Torvalds 	ctx = NULL;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	/* Check if a conversion error occurred. */
2381da177e4SLinus Torvalds 	if ((signed)nls_name.len < 0) {
2391da177e4SLinus Torvalds 		err = (signed)nls_name.len;
2401da177e4SLinus Torvalds 		goto err_out;
2411da177e4SLinus Torvalds 	}
2428387ff25SLinus Torvalds 	nls_name.hash = full_name_hash(dent, nls_name.name, nls_name.len);
2431da177e4SLinus Torvalds 
24426904217SChristoph Hellwig 	dent = d_add_ci(dent, dent_inode, &nls_name);
2451da177e4SLinus Torvalds 	kfree(nls_name.name);
24626904217SChristoph Hellwig 	return dent;
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds eio_err_out:
2491da177e4SLinus Torvalds 	ntfs_error(vol->sb, "Illegal file name attribute. Run chkdsk.");
2501da177e4SLinus Torvalds 	err = -EIO;
2511da177e4SLinus Torvalds err_out:
2521da177e4SLinus Torvalds 	if (ctx)
2531da177e4SLinus Torvalds 		ntfs_attr_put_search_ctx(ctx);
2541da177e4SLinus Torvalds 	if (m)
2551da177e4SLinus Torvalds 		unmap_mft_record(ni);
2561da177e4SLinus Torvalds 	iput(dent_inode);
2571da177e4SLinus Torvalds 	ntfs_error(vol->sb, "Failed, returning error code %i.", err);
2581da177e4SLinus Torvalds 	return ERR_PTR(err);
2591da177e4SLinus Torvalds    }
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
262*aa0b42b7SRandy Dunlap /*
2631da177e4SLinus Torvalds  * Inode operations for directories.
2641da177e4SLinus Torvalds  */
26592e1d5beSArjan van de Ven const struct inode_operations ntfs_dir_inode_ops = {
2661da177e4SLinus Torvalds 	.lookup	= ntfs_lookup,	/* VFS: Lookup directory. */
2671da177e4SLinus Torvalds };
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds /**
2701da177e4SLinus Torvalds  * ntfs_get_parent - find the dentry of the parent of a given directory dentry
2711da177e4SLinus Torvalds  * @child_dent:		dentry of the directory whose parent directory to find
2721da177e4SLinus Torvalds  *
2731da177e4SLinus Torvalds  * Find the dentry for the parent directory of the directory specified by the
2741da177e4SLinus Torvalds  * dentry @child_dent.  This function is called from
2751da177e4SLinus Torvalds  * fs/exportfs/expfs.c::find_exported_dentry() which in turn is called from the
2761da177e4SLinus Torvalds  * default ->decode_fh() which is export_decode_fh() in the same file.
2771da177e4SLinus Torvalds  *
2781da177e4SLinus Torvalds  * The code is based on the ext3 ->get_parent() implementation found in
2791da177e4SLinus Torvalds  * fs/ext3/namei.c::ext3_get_parent().
2801da177e4SLinus Torvalds  *
2812b0143b5SDavid Howells  * Note: ntfs_get_parent() is called with @d_inode(child_dent)->i_mutex down.
2821da177e4SLinus Torvalds  *
2831da177e4SLinus Torvalds  * Return the dentry of the parent directory on success or the error code on
2841da177e4SLinus Torvalds  * error (IS_ERR() is true).
2851da177e4SLinus Torvalds  */
ntfs_get_parent(struct dentry * child_dent)28641382686SAnton Altaparmakov static struct dentry *ntfs_get_parent(struct dentry *child_dent)
2871da177e4SLinus Torvalds {
2882b0143b5SDavid Howells 	struct inode *vi = d_inode(child_dent);
2891da177e4SLinus Torvalds 	ntfs_inode *ni = NTFS_I(vi);
2901da177e4SLinus Torvalds 	MFT_RECORD *mrec;
2911da177e4SLinus Torvalds 	ntfs_attr_search_ctx *ctx;
2921da177e4SLinus Torvalds 	ATTR_RECORD *attr;
2931da177e4SLinus Torvalds 	FILE_NAME_ATTR *fn;
2941da177e4SLinus Torvalds 	unsigned long parent_ino;
2951da177e4SLinus Torvalds 	int err;
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
2981da177e4SLinus Torvalds 	/* Get the mft record of the inode belonging to the child dentry. */
2991da177e4SLinus Torvalds 	mrec = map_mft_record(ni);
3001da177e4SLinus Torvalds 	if (IS_ERR(mrec))
301995f608eSAl Viro 		return ERR_CAST(mrec);
3021da177e4SLinus Torvalds 	/* Find the first file name attribute in the mft record. */
3031da177e4SLinus Torvalds 	ctx = ntfs_attr_get_search_ctx(ni, mrec);
3041da177e4SLinus Torvalds 	if (unlikely(!ctx)) {
3051da177e4SLinus Torvalds 		unmap_mft_record(ni);
3061da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
3071da177e4SLinus Torvalds 	}
3081da177e4SLinus Torvalds try_next:
3091da177e4SLinus Torvalds 	err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, CASE_SENSITIVE, 0, NULL,
3101da177e4SLinus Torvalds 			0, ctx);
3111da177e4SLinus Torvalds 	if (unlikely(err)) {
3121da177e4SLinus Torvalds 		ntfs_attr_put_search_ctx(ctx);
3131da177e4SLinus Torvalds 		unmap_mft_record(ni);
3141da177e4SLinus Torvalds 		if (err == -ENOENT)
3151da177e4SLinus Torvalds 			ntfs_error(vi->i_sb, "Inode 0x%lx does not have a "
3161da177e4SLinus Torvalds 					"file name attribute.  Run chkdsk.",
3171da177e4SLinus Torvalds 					vi->i_ino);
3181da177e4SLinus Torvalds 		return ERR_PTR(err);
3191da177e4SLinus Torvalds 	}
3201da177e4SLinus Torvalds 	attr = ctx->attr;
3211da177e4SLinus Torvalds 	if (unlikely(attr->non_resident))
3221da177e4SLinus Torvalds 		goto try_next;
3231da177e4SLinus Torvalds 	fn = (FILE_NAME_ATTR *)((u8 *)attr +
3241da177e4SLinus Torvalds 			le16_to_cpu(attr->data.resident.value_offset));
3251da177e4SLinus Torvalds 	if (unlikely((u8 *)fn + le32_to_cpu(attr->data.resident.value_length) >
3261da177e4SLinus Torvalds 			(u8*)attr + le32_to_cpu(attr->length)))
3271da177e4SLinus Torvalds 		goto try_next;
3281da177e4SLinus Torvalds 	/* Get the inode number of the parent directory. */
3291da177e4SLinus Torvalds 	parent_ino = MREF_LE(fn->parent_directory);
3301da177e4SLinus Torvalds 	/* Release the search context and the mft record of the child. */
3311da177e4SLinus Torvalds 	ntfs_attr_put_search_ctx(ctx);
3321da177e4SLinus Torvalds 	unmap_mft_record(ni);
33344003728SChristoph Hellwig 
33444003728SChristoph Hellwig 	return d_obtain_alias(ntfs_iget(vi->i_sb, parent_ino));
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
ntfs_nfs_get_inode(struct super_block * sb,u64 ino,u32 generation)337a3513206SChristoph Hellwig static struct inode *ntfs_nfs_get_inode(struct super_block *sb,
338a3513206SChristoph Hellwig 		u64 ino, u32 generation)
3391da177e4SLinus Torvalds {
340a3513206SChristoph Hellwig 	struct inode *inode;
3411da177e4SLinus Torvalds 
342a3513206SChristoph Hellwig 	inode = ntfs_iget(sb, ino);
343a3513206SChristoph Hellwig 	if (!IS_ERR(inode)) {
344a3513206SChristoph Hellwig 		if (is_bad_inode(inode) || inode->i_generation != generation) {
345a3513206SChristoph Hellwig 			iput(inode);
346a3513206SChristoph Hellwig 			inode = ERR_PTR(-ESTALE);
3471da177e4SLinus Torvalds 		}
3481da177e4SLinus Torvalds 	}
349a3513206SChristoph Hellwig 
350a3513206SChristoph Hellwig 	return inode;
3511da177e4SLinus Torvalds }
352a3513206SChristoph Hellwig 
ntfs_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)353a3513206SChristoph Hellwig static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
354a3513206SChristoph Hellwig 		int fh_len, int fh_type)
355a3513206SChristoph Hellwig {
356a3513206SChristoph Hellwig 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
357a3513206SChristoph Hellwig 				    ntfs_nfs_get_inode);
358a3513206SChristoph Hellwig }
359a3513206SChristoph Hellwig 
ntfs_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)360a3513206SChristoph Hellwig static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
361a3513206SChristoph Hellwig 		int fh_len, int fh_type)
362a3513206SChristoph Hellwig {
363a3513206SChristoph Hellwig 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
364a3513206SChristoph Hellwig 				    ntfs_nfs_get_inode);
3651da177e4SLinus Torvalds }
36641382686SAnton Altaparmakov 
367*aa0b42b7SRandy Dunlap /*
36841382686SAnton Altaparmakov  * Export operations allowing NFS exporting of mounted NTFS partitions.
36941382686SAnton Altaparmakov  *
370a3513206SChristoph Hellwig  * We use the default ->encode_fh() for now.  Note that they
37141382686SAnton Altaparmakov  * use 32 bits to store the inode number which is an unsigned long so on 64-bit
37241382686SAnton Altaparmakov  * architectures is usually 64 bits so it would all fail horribly on huge
37341382686SAnton Altaparmakov  * volumes.  I guess we need to define our own encode and decode fh functions
37441382686SAnton Altaparmakov  * that store 64-bit inode numbers at some point but for now we will ignore the
37541382686SAnton Altaparmakov  * problem...
37641382686SAnton Altaparmakov  *
37741382686SAnton Altaparmakov  * We also use the default ->get_name() helper (used by ->decode_fh() via
37841382686SAnton Altaparmakov  * fs/exportfs/expfs.c::find_exported_dentry()) as that is completely fs
37941382686SAnton Altaparmakov  * independent.
38041382686SAnton Altaparmakov  *
38141382686SAnton Altaparmakov  * The default ->get_parent() just returns -EACCES so we have to provide our
38241382686SAnton Altaparmakov  * own and the default ->get_dentry() is incompatible with NTFS due to not
38341382686SAnton Altaparmakov  * allowing the inode number 0 which is used in NTFS for the system file $MFT
38441382686SAnton Altaparmakov  * and due to using iget() whereas NTFS needs ntfs_iget().
38541382686SAnton Altaparmakov  */
38639655164SChristoph Hellwig const struct export_operations ntfs_export_ops = {
38741382686SAnton Altaparmakov 	.get_parent	= ntfs_get_parent,	/* Find the parent of a given
38841382686SAnton Altaparmakov 						   directory. */
389a3513206SChristoph Hellwig 	.fh_to_dentry	= ntfs_fh_to_dentry,
390a3513206SChristoph Hellwig 	.fh_to_parent	= ntfs_fh_to_parent,
39141382686SAnton Altaparmakov };
392