Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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