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)  * dir.c - NTFS kernel directory operations. Part of the Linux-NTFS project.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (c) 2001-2007 Anton Altaparmakov
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * Copyright (c) 2002 Richard Russon
^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/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include "dir.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include "aops.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include "attrib.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include "mft.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include "debug.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)  * The little endian Unicode string $I30 as a global constant.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) ntfschar I30[5] = { cpu_to_le16('$'), cpu_to_le16('I'),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 		cpu_to_le16('3'),	cpu_to_le16('0'), 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  * ntfs_lookup_inode_by_name - find an inode in a directory given its name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  * @dir_ni:	ntfs inode of the directory in which to search for the name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29)  * @uname:	Unicode name for which to search in the directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30)  * @uname_len:	length of the name @uname in Unicode characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31)  * @res:	return the found file name if necessary (see below)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33)  * Look for an inode with name @uname in the directory with inode @dir_ni.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34)  * ntfs_lookup_inode_by_name() walks the contents of the directory looking for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  * the Unicode name. If the name is found in the directory, the corresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  * is a 64-bit number containing the sequence number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39)  * On error, a negative value is returned corresponding to the error code. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40)  * particular if the inode is not found -ENOENT is returned. Note that you
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  * can't just check the return value for being negative, you have to check the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  * inode number for being negative which you can extract using MREC(return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  * value).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  * Note, @uname_len does not include the (optional) terminating NULL character.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47)  * Note, we look for a case sensitive match first but we also look for a case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48)  * insensitive match at the same time. If we find a case insensitive match, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)  * save that for the case that we don't find an exact match, where we return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  * the case insensitive match and setup @res (which we allocate!) with the mft
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  * reference, the file name type, length and with a copy of the little endian
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52)  * Unicode file name itself. If we match a file name which is in the DOS name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53)  * space, we only return the mft reference and file name type in @res.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  * ntfs_lookup() then uses this to find the long file name in the inode itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55)  * This is to avoid polluting the dcache with short file names. We want them to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56)  * work but we don't care for how quickly one can access them. This also fixes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57)  * the dcache aliasing issues.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  * Locking:  - Caller must hold i_mutex on the directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60)  *	     - Each page cache page in the index allocation mapping must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61)  *	       locked whilst being accessed otherwise we may find a corrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62)  *	       page due to it being under ->writepage at the moment which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63)  *	       applies the mst protection fixups before writing out and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64)  *	       removes them again after the write is complete after which it 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65)  *	       unlocks the page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) MFT_REF ntfs_lookup_inode_by_name(ntfs_inode *dir_ni, const ntfschar *uname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 		const int uname_len, ntfs_name **res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	ntfs_volume *vol = dir_ni->vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	struct super_block *sb = vol->sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	MFT_RECORD *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	INDEX_ROOT *ir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	INDEX_ENTRY *ie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	INDEX_ALLOCATION *ia;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	u8 *index_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	u64 mref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	ntfs_attr_search_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	int err, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	VCN vcn, old_vcn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	struct address_space *ia_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	u8 *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	ntfs_name *name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	BUG_ON(!S_ISDIR(VFS_I(dir_ni)->i_mode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	BUG_ON(NInoAttr(dir_ni));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	/* Get hold of the mft record for the directory. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	m = map_mft_record(dir_ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	if (IS_ERR(m)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 		ntfs_error(sb, "map_mft_record() failed with error code %ld.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 				-PTR_ERR(m));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 		return ERR_MREF(PTR_ERR(m));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	ctx = ntfs_attr_get_search_ctx(dir_ni, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	if (unlikely(!ctx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	/* Find the index root attribute in the mft record. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 			0, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 		if (err == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 			ntfs_error(sb, "Index root attribute missing in "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 					"directory inode 0x%lx.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 					dir_ni->mft_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 			err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	/* Get to the index root value (it's been verified in read_inode). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	ir = (INDEX_ROOT*)((u8*)ctx->attr +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 			le16_to_cpu(ctx->attr->data.resident.value_offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	/* The first index entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	ie = (INDEX_ENTRY*)((u8*)&ir->index +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 			le32_to_cpu(ir->index.entries_offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	 * Loop until we exceed valid memory (corruption case) or until we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	 * reach the last entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 		/* Bounds checks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 		if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 				sizeof(INDEX_ENTRY_HEADER) > index_end ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 				(u8*)ie + le16_to_cpu(ie->key_length) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 				index_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 			goto dir_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 		 * The last entry cannot contain a name. It can however contain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 		 * a pointer to a child node in the B+tree so we just break out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 		if (ie->flags & INDEX_ENTRY_END)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 		 * We perform a case sensitive comparison and if that matches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 		 * we are done and return the mft reference of the inode (i.e.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 		 * the inode number together with the sequence number for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 		 * consistency checking). We convert it to cpu format before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 		 * returning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 		if (ntfs_are_names_equal(uname, uname_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 				(ntfschar*)&ie->key.file_name.file_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 				ie->key.file_name.file_name_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 				CASE_SENSITIVE, vol->upcase, vol->upcase_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) found_it:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 			 * We have a perfect match, so we don't need to care
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 			 * about having matched imperfectly before, so we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 			 * free name and set *res to NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 			 * However, if the perfect match is a short file name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 			 * we need to signal this through *res, so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 			 * ntfs_lookup() can fix dcache aliasing issues.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 			 * As an optimization we just reuse an existing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 			 * allocation of *res.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 			if (ie->key.file_name.file_name_type == FILE_NAME_DOS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 				if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 					name = kmalloc(sizeof(ntfs_name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 							GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 					if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 						err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 						goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 					}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 				name->mref = le64_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 						ie->data.dir.indexed_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 				name->type = FILE_NAME_DOS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 				name->len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 				*res = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 				kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 				*res = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 			mref = le64_to_cpu(ie->data.dir.indexed_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 			ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 			unmap_mft_record(dir_ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 			return mref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		 * For a case insensitive mount, we also perform a case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 		 * insensitive comparison (provided the file name is not in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 		 * POSIX namespace). If the comparison matches, and the name is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		 * in the WIN32 namespace, we cache the filename in *res so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 		 * that the caller, ntfs_lookup(), can work on it. If the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 		 * comparison matches, and the name is in the DOS namespace, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		 * only cache the mft reference and the file name type (we set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		 * the name length to zero for simplicity).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		if (!NVolCaseSensitive(vol) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 				ie->key.file_name.file_name_type &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 				ntfs_are_names_equal(uname, uname_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 				(ntfschar*)&ie->key.file_name.file_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 				ie->key.file_name.file_name_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 				IGNORE_CASE, vol->upcase, vol->upcase_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 			int name_size = sizeof(ntfs_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 			u8 type = ie->key.file_name.file_name_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 			u8 len = ie->key.file_name.file_name_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 			/* Only one case insensitive matching name allowed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 			if (name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 				ntfs_error(sb, "Found already allocated name "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 						"in phase 1. Please run chkdsk "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 						"and if that doesn't find any "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 						"errors please report you saw "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 						"this message to "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 						"linux-ntfs-dev@lists."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 						"sourceforge.net.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 				goto dir_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 			if (type != FILE_NAME_DOS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 				name_size += len * sizeof(ntfschar);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 			name = kmalloc(name_size, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 			if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 				err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 				goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 			name->mref = le64_to_cpu(ie->data.dir.indexed_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 			name->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 			if (type != FILE_NAME_DOS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 				name->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 				memcpy(name->name, ie->key.file_name.file_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 						len * sizeof(ntfschar));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 			} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 				name->len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 			*res = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		 * Not a perfect match, need to do full blown collation so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		 * know which way in the B+tree we have to go.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 		rc = ntfs_collate_names(uname, uname_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 				(ntfschar*)&ie->key.file_name.file_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 				ie->key.file_name.file_name_length, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 				IGNORE_CASE, vol->upcase, vol->upcase_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 		 * If uname collates before the name of the current entry, there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		 * is definitely no such name in this index but we might need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 		 * descend into the B+tree so we just break out of the loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		if (rc == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 		/* The names are not equal, continue the search. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		 * Names match with case insensitive comparison, now try the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		 * case sensitive comparison, which is required for proper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		 * collation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		rc = ntfs_collate_names(uname, uname_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 				(ntfschar*)&ie->key.file_name.file_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 				ie->key.file_name.file_name_length, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 				CASE_SENSITIVE, vol->upcase, vol->upcase_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		if (rc == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 		 * Perfect match, this will never happen as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 		 * ntfs_are_names_equal() call will have gotten a match but we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		 * still treat it correctly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 		goto found_it;
^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) 	 * We have finished with this index without success. Check for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	 * presence of a child node and if not present return -ENOENT, unless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	 * we have got a matching name cached in name in which case return the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	 * mft reference associated with it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	if (!(ie->flags & INDEX_ENTRY_NODE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		if (name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 			ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 			unmap_mft_record(dir_ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 			return name->mref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 		ntfs_debug("Entry not found.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	} /* Child node present, descend into it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	/* Consistency check: Verify that an index allocation exists. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	if (!NInoIndexAllocPresent(dir_ni)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		ntfs_error(sb, "No index allocation attribute but index entry "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 				"requires one. Directory inode 0x%lx is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 				"corrupt or driver bug.", dir_ni->mft_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	/* Get the starting vcn of the index_block holding the child node. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	vcn = sle64_to_cpup((sle64*)((u8*)ie + le16_to_cpu(ie->length) - 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	ia_mapping = VFS_I(dir_ni)->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	 * We are done with the index root and the mft record. Release them,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	 * otherwise we deadlock with ntfs_map_page().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	unmap_mft_record(dir_ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	m = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	ctx = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) descend_into_child_node:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	 * Convert vcn to index into the index allocation attribute in units
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	 * of PAGE_SIZE and map the page cache page, reading it from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	 * disk if necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	page = ntfs_map_page(ia_mapping, vcn <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 			dir_ni->itype.index.vcn_size_bits >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	if (IS_ERR(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 		ntfs_error(sb, "Failed to map directory index page, error %ld.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 				-PTR_ERR(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 		err = PTR_ERR(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	kaddr = (u8*)page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) fast_descend_into_child_node:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	/* Get to the index allocation block. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	ia = (INDEX_ALLOCATION*)(kaddr + ((vcn <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 			dir_ni->itype.index.vcn_size_bits) & ~PAGE_MASK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	/* Bounds checks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 		ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 				"inode 0x%lx or driver bug.", dir_ni->mft_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	/* Catch multi sector transfer fixup errors. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	if (unlikely(!ntfs_is_indx_record(ia->magic))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		ntfs_error(sb, "Directory index record with vcn 0x%llx is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 				"corrupt.  Corrupt inode 0x%lx.  Run chkdsk.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 				(unsigned long long)vcn, dir_ni->mft_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	if (sle64_to_cpu(ia->index_block_vcn) != vcn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 				"different from expected VCN (0x%llx). "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 				"Directory inode 0x%lx is corrupt or driver "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 				"bug.", (unsigned long long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 				sle64_to_cpu(ia->index_block_vcn),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 				(unsigned long long)vcn, dir_ni->mft_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	if (le32_to_cpu(ia->index.allocated_size) + 0x18 !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 			dir_ni->itype.index.block_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 		ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 				"0x%lx has a size (%u) differing from the "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 				"directory specified size (%u). Directory "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 				"inode is corrupt or driver bug.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 				(unsigned long long)vcn, dir_ni->mft_no,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 				le32_to_cpu(ia->index.allocated_size) + 0x18,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 				dir_ni->itype.index.block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	index_end = (u8*)ia + dir_ni->itype.index.block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	if (index_end > kaddr + PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 				"0x%lx crosses page boundary. Impossible! "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 				"Cannot access! This is probably a bug in the "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 				"driver.", (unsigned long long)vcn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 				dir_ni->mft_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	if (index_end > (u8*)ia + dir_ni->itype.index.block_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 				"inode 0x%lx exceeds maximum size.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 				(unsigned long long)vcn, dir_ni->mft_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	/* The first index entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	ie = (INDEX_ENTRY*)((u8*)&ia->index +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 			le32_to_cpu(ia->index.entries_offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	 * Iterate similar to above big loop but applied to index buffer, thus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	 * loop until we exceed valid memory (corruption case) or until we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	 * reach the last entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		/* Bounds check. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 		if ((u8*)ie < (u8*)ia || (u8*)ie +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 				sizeof(INDEX_ENTRY_HEADER) > index_end ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 				(u8*)ie + le16_to_cpu(ie->key_length) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 				index_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 			ntfs_error(sb, "Index entry out of bounds in "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 					"directory inode 0x%lx.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 					dir_ni->mft_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 		 * The last entry cannot contain a name. It can however contain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		 * a pointer to a child node in the B+tree so we just break out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		if (ie->flags & INDEX_ENTRY_END)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		 * We perform a case sensitive comparison and if that matches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		 * we are done and return the mft reference of the inode (i.e.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		 * the inode number together with the sequence number for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		 * consistency checking). We convert it to cpu format before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		 * returning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 		if (ntfs_are_names_equal(uname, uname_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 				(ntfschar*)&ie->key.file_name.file_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 				ie->key.file_name.file_name_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 				CASE_SENSITIVE, vol->upcase, vol->upcase_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) found_it2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 			 * We have a perfect match, so we don't need to care
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 			 * about having matched imperfectly before, so we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 			 * free name and set *res to NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 			 * However, if the perfect match is a short file name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 			 * we need to signal this through *res, so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 			 * ntfs_lookup() can fix dcache aliasing issues.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 			 * As an optimization we just reuse an existing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 			 * allocation of *res.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 			if (ie->key.file_name.file_name_type == FILE_NAME_DOS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 				if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 					name = kmalloc(sizeof(ntfs_name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 							GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 					if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 						err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 						goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 					}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 				name->mref = le64_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 						ie->data.dir.indexed_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 				name->type = FILE_NAME_DOS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 				name->len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 				*res = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 				kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 				*res = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 			mref = le64_to_cpu(ie->data.dir.indexed_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 			ntfs_unmap_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 			return mref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		 * For a case insensitive mount, we also perform a case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		 * insensitive comparison (provided the file name is not in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 		 * POSIX namespace). If the comparison matches, and the name is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 		 * in the WIN32 namespace, we cache the filename in *res so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 		 * that the caller, ntfs_lookup(), can work on it. If the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 		 * comparison matches, and the name is in the DOS namespace, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		 * only cache the mft reference and the file name type (we set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		 * the name length to zero for simplicity).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		if (!NVolCaseSensitive(vol) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 				ie->key.file_name.file_name_type &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 				ntfs_are_names_equal(uname, uname_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 				(ntfschar*)&ie->key.file_name.file_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 				ie->key.file_name.file_name_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 				IGNORE_CASE, vol->upcase, vol->upcase_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 			int name_size = sizeof(ntfs_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 			u8 type = ie->key.file_name.file_name_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 			u8 len = ie->key.file_name.file_name_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 			/* Only one case insensitive matching name allowed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 			if (name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 				ntfs_error(sb, "Found already allocated name "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 						"in phase 2. Please run chkdsk "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 						"and if that doesn't find any "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 						"errors please report you saw "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 						"this message to "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 						"linux-ntfs-dev@lists."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 						"sourceforge.net.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 				unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 				ntfs_unmap_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 				goto dir_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 			if (type != FILE_NAME_DOS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 				name_size += len * sizeof(ntfschar);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 			name = kmalloc(name_size, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 			if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 				err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 				goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 			name->mref = le64_to_cpu(ie->data.dir.indexed_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 			name->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 			if (type != FILE_NAME_DOS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 				name->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 				memcpy(name->name, ie->key.file_name.file_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 						len * sizeof(ntfschar));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 			} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 				name->len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 			*res = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 		 * Not a perfect match, need to do full blown collation so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 		 * know which way in the B+tree we have to go.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 		rc = ntfs_collate_names(uname, uname_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 				(ntfschar*)&ie->key.file_name.file_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 				ie->key.file_name.file_name_length, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 				IGNORE_CASE, vol->upcase, vol->upcase_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		 * If uname collates before the name of the current entry, there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 		 * is definitely no such name in this index but we might need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 		 * descend into the B+tree so we just break out of the loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 		if (rc == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		/* The names are not equal, continue the search. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		 * Names match with case insensitive comparison, now try the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		 * case sensitive comparison, which is required for proper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		 * collation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		rc = ntfs_collate_names(uname, uname_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 				(ntfschar*)&ie->key.file_name.file_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 				ie->key.file_name.file_name_length, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 				CASE_SENSITIVE, vol->upcase, vol->upcase_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 		if (rc == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		 * Perfect match, this will never happen as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		 * ntfs_are_names_equal() call will have gotten a match but we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		 * still treat it correctly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 		goto found_it2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	 * We have finished with this index buffer without success. Check for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	 * the presence of a child node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	if (ie->flags & INDEX_ENTRY_NODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		if ((ia->index.flags & NODE_MASK) == LEAF_NODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 			ntfs_error(sb, "Index entry with child node found in "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 					"a leaf node in directory inode 0x%lx.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 					dir_ni->mft_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		/* Child node present, descend into it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		old_vcn = vcn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		vcn = sle64_to_cpup((sle64*)((u8*)ie +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 				le16_to_cpu(ie->length) - 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		if (vcn >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 			/* If vcn is in the same page cache page as old_vcn we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 			 * recycle the mapped page. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 			if (old_vcn << vol->cluster_size_bits >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 					PAGE_SHIFT == vcn <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 					vol->cluster_size_bits >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 					PAGE_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 				goto fast_descend_into_child_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 			ntfs_unmap_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 			goto descend_into_child_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		ntfs_error(sb, "Negative child node vcn in directory inode "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 				"0x%lx.", dir_ni->mft_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	 * No child node present, return -ENOENT, unless we have got a matching
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	 * name cached in name in which case return the mft reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	 * associated with it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	if (name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		ntfs_unmap_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		return name->mref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	ntfs_debug("Entry not found.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) unm_err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	ntfs_unmap_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	if (ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 		ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	if (m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 		unmap_mft_record(dir_ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	if (name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 		*res = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	return ERR_MREF(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) dir_err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	ntfs_error(sb, "Corrupt directory.  Aborting lookup.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) // TODO: (AIA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) // The algorithm embedded in this code will be required for the time when we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) // want to support adding of entries to directories, where we require correct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) // collation of file names in order not to cause corruption of the filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601)  * ntfs_lookup_inode_by_name - find an inode in a directory given its name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602)  * @dir_ni:	ntfs inode of the directory in which to search for the name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603)  * @uname:	Unicode name for which to search in the directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604)  * @uname_len:	length of the name @uname in Unicode characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606)  * Look for an inode with name @uname in the directory with inode @dir_ni.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607)  * ntfs_lookup_inode_by_name() walks the contents of the directory looking for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608)  * the Unicode name. If the name is found in the directory, the corresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609)  * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610)  * is a 64-bit number containing the sequence number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612)  * On error, a negative value is returned corresponding to the error code. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613)  * particular if the inode is not found -ENOENT is returned. Note that you
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614)  * can't just check the return value for being negative, you have to check the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615)  * inode number for being negative which you can extract using MREC(return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616)  * value).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618)  * Note, @uname_len does not include the (optional) terminating NULL character.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) u64 ntfs_lookup_inode_by_name(ntfs_inode *dir_ni, const ntfschar *uname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		const int uname_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	ntfs_volume *vol = dir_ni->vol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	struct super_block *sb = vol->sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	MFT_RECORD *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	INDEX_ROOT *ir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	INDEX_ENTRY *ie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	INDEX_ALLOCATION *ia;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	u8 *index_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	u64 mref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	ntfs_attr_search_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	int err, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	IGNORE_CASE_BOOL ic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	VCN vcn, old_vcn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	struct address_space *ia_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	u8 *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	/* Get hold of the mft record for the directory. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	m = map_mft_record(dir_ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	if (IS_ERR(m)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		ntfs_error(sb, "map_mft_record() failed with error code %ld.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 				-PTR_ERR(m));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 		return ERR_MREF(PTR_ERR(m));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	ctx = ntfs_attr_get_search_ctx(dir_ni, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	if (!ctx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	/* Find the index root attribute in the mft record. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 			0, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		if (err == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 			ntfs_error(sb, "Index root attribute missing in "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 					"directory inode 0x%lx.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 					dir_ni->mft_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 			err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	/* Get to the index root value (it's been verified in read_inode). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	ir = (INDEX_ROOT*)((u8*)ctx->attr +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 			le16_to_cpu(ctx->attr->data.resident.value_offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	/* The first index entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	ie = (INDEX_ENTRY*)((u8*)&ir->index +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 			le32_to_cpu(ir->index.entries_offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	 * Loop until we exceed valid memory (corruption case) or until we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	 * reach the last entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		/* Bounds checks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 				sizeof(INDEX_ENTRY_HEADER) > index_end ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 				(u8*)ie + le16_to_cpu(ie->key_length) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 				index_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 			goto dir_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		 * The last entry cannot contain a name. It can however contain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 		 * a pointer to a child node in the B+tree so we just break out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 		if (ie->flags & INDEX_ENTRY_END)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		 * If the current entry has a name type of POSIX, the name is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		 * case sensitive and not otherwise. This has the effect of us
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 		 * not being able to access any POSIX file names which collate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 		 * after the non-POSIX one when they only differ in case, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 		 * anyone doing screwy stuff like that deserves to burn in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		 * hell... Doing that kind of stuff on NT4 actually causes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		 * corruption on the partition even when using SP6a and Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		 * is not involved at all.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		ic = ie->key.file_name.file_name_type ? IGNORE_CASE :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 				CASE_SENSITIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		 * If the names match perfectly, we are done and return the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		 * mft reference of the inode (i.e. the inode number together
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 		 * with the sequence number for consistency checking. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		 * convert it to cpu format before returning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 		if (ntfs_are_names_equal(uname, uname_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 				(ntfschar*)&ie->key.file_name.file_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 				ie->key.file_name.file_name_length, ic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 				vol->upcase, vol->upcase_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) found_it:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 			mref = le64_to_cpu(ie->data.dir.indexed_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 			ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 			unmap_mft_record(dir_ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 			return mref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 		 * Not a perfect match, need to do full blown collation so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		 * know which way in the B+tree we have to go.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		rc = ntfs_collate_names(uname, uname_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 				(ntfschar*)&ie->key.file_name.file_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 				ie->key.file_name.file_name_length, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 				IGNORE_CASE, vol->upcase, vol->upcase_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		 * If uname collates before the name of the current entry, there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		 * is definitely no such name in this index but we might need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		 * descend into the B+tree so we just break out of the loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		if (rc == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		/* The names are not equal, continue the search. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		 * Names match with case insensitive comparison, now try the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		 * case sensitive comparison, which is required for proper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		 * collation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		rc = ntfs_collate_names(uname, uname_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 				(ntfschar*)&ie->key.file_name.file_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 				ie->key.file_name.file_name_length, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 				CASE_SENSITIVE, vol->upcase, vol->upcase_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		if (rc == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		 * Perfect match, this will never happen as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		 * ntfs_are_names_equal() call will have gotten a match but we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		 * still treat it correctly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		goto found_it;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	 * We have finished with this index without success. Check for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	 * presence of a child node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	if (!(ie->flags & INDEX_ENTRY_NODE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 		/* No child node, return -ENOENT. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	} /* Child node present, descend into it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	/* Consistency check: Verify that an index allocation exists. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	if (!NInoIndexAllocPresent(dir_ni)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		ntfs_error(sb, "No index allocation attribute but index entry "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 				"requires one. Directory inode 0x%lx is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 				"corrupt or driver bug.", dir_ni->mft_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	/* Get the starting vcn of the index_block holding the child node. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	ia_mapping = VFS_I(dir_ni)->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	 * We are done with the index root and the mft record. Release them,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	 * otherwise we deadlock with ntfs_map_page().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	unmap_mft_record(dir_ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	m = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	ctx = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) descend_into_child_node:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	 * Convert vcn to index into the index allocation attribute in units
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	 * of PAGE_SIZE and map the page cache page, reading it from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	 * disk if necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	page = ntfs_map_page(ia_mapping, vcn <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 			dir_ni->itype.index.vcn_size_bits >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	if (IS_ERR(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		ntfs_error(sb, "Failed to map directory index page, error %ld.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 				-PTR_ERR(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		err = PTR_ERR(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	kaddr = (u8*)page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) fast_descend_into_child_node:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	/* Get to the index allocation block. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	ia = (INDEX_ALLOCATION*)(kaddr + ((vcn <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 			dir_ni->itype.index.vcn_size_bits) & ~PAGE_MASK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	/* Bounds checks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	if ((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 				"inode 0x%lx or driver bug.", dir_ni->mft_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	/* Catch multi sector transfer fixup errors. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	if (unlikely(!ntfs_is_indx_record(ia->magic))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 		ntfs_error(sb, "Directory index record with vcn 0x%llx is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 				"corrupt.  Corrupt inode 0x%lx.  Run chkdsk.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 				(unsigned long long)vcn, dir_ni->mft_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	if (sle64_to_cpu(ia->index_block_vcn) != vcn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 				"different from expected VCN (0x%llx). "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 				"Directory inode 0x%lx is corrupt or driver "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 				"bug.", (unsigned long long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 				sle64_to_cpu(ia->index_block_vcn),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 				(unsigned long long)vcn, dir_ni->mft_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	if (le32_to_cpu(ia->index.allocated_size) + 0x18 !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 			dir_ni->itype.index.block_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 		ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 				"0x%lx has a size (%u) differing from the "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 				"directory specified size (%u). Directory "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 				"inode is corrupt or driver bug.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 				(unsigned long long)vcn, dir_ni->mft_no,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 				le32_to_cpu(ia->index.allocated_size) + 0x18,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 				dir_ni->itype.index.block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	index_end = (u8*)ia + dir_ni->itype.index.block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	if (index_end > kaddr + PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 		ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 				"0x%lx crosses page boundary. Impossible! "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 				"Cannot access! This is probably a bug in the "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 				"driver.", (unsigned long long)vcn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 				dir_ni->mft_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	if (index_end > (u8*)ia + dir_ni->itype.index.block_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 				"inode 0x%lx exceeds maximum size.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 				(unsigned long long)vcn, dir_ni->mft_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	/* The first index entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	ie = (INDEX_ENTRY*)((u8*)&ia->index +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 			le32_to_cpu(ia->index.entries_offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	 * Iterate similar to above big loop but applied to index buffer, thus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	 * loop until we exceed valid memory (corruption case) or until we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	 * reach the last entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 		/* Bounds check. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		if ((u8*)ie < (u8*)ia || (u8*)ie +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 				sizeof(INDEX_ENTRY_HEADER) > index_end ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 				(u8*)ie + le16_to_cpu(ie->key_length) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 				index_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 			ntfs_error(sb, "Index entry out of bounds in "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 					"directory inode 0x%lx.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 					dir_ni->mft_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		 * The last entry cannot contain a name. It can however contain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		 * a pointer to a child node in the B+tree so we just break out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 		if (ie->flags & INDEX_ENTRY_END)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		 * If the current entry has a name type of POSIX, the name is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		 * case sensitive and not otherwise. This has the effect of us
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		 * not being able to access any POSIX file names which collate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		 * after the non-POSIX one when they only differ in case, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		 * anyone doing screwy stuff like that deserves to burn in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		 * hell... Doing that kind of stuff on NT4 actually causes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		 * corruption on the partition even when using SP6a and Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		 * is not involved at all.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		ic = ie->key.file_name.file_name_type ? IGNORE_CASE :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 				CASE_SENSITIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		 * If the names match perfectly, we are done and return the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 		 * mft reference of the inode (i.e. the inode number together
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 		 * with the sequence number for consistency checking. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 		 * convert it to cpu format before returning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		if (ntfs_are_names_equal(uname, uname_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 				(ntfschar*)&ie->key.file_name.file_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 				ie->key.file_name.file_name_length, ic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 				vol->upcase, vol->upcase_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) found_it2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 			mref = le64_to_cpu(ie->data.dir.indexed_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 			ntfs_unmap_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 			return mref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 		 * Not a perfect match, need to do full blown collation so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 		 * know which way in the B+tree we have to go.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 		rc = ntfs_collate_names(uname, uname_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 				(ntfschar*)&ie->key.file_name.file_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 				ie->key.file_name.file_name_length, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 				IGNORE_CASE, vol->upcase, vol->upcase_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		 * If uname collates before the name of the current entry, there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		 * is definitely no such name in this index but we might need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 		 * descend into the B+tree so we just break out of the loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		if (rc == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		/* The names are not equal, continue the search. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		 * Names match with case insensitive comparison, now try the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		 * case sensitive comparison, which is required for proper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		 * collation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 		rc = ntfs_collate_names(uname, uname_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 				(ntfschar*)&ie->key.file_name.file_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 				ie->key.file_name.file_name_length, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 				CASE_SENSITIVE, vol->upcase, vol->upcase_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 		if (rc == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		 * Perfect match, this will never happen as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 		 * ntfs_are_names_equal() call will have gotten a match but we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		 * still treat it correctly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		goto found_it2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	 * We have finished with this index buffer without success. Check for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	 * the presence of a child node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	if (ie->flags & INDEX_ENTRY_NODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		if ((ia->index.flags & NODE_MASK) == LEAF_NODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 			ntfs_error(sb, "Index entry with child node found in "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 					"a leaf node in directory inode 0x%lx.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 					dir_ni->mft_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 			goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		/* Child node present, descend into it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		old_vcn = vcn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 		vcn = sle64_to_cpup((u8*)ie + le16_to_cpu(ie->length) - 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		if (vcn >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 			/* If vcn is in the same page cache page as old_vcn we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 			 * recycle the mapped page. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 			if (old_vcn << vol->cluster_size_bits >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 					PAGE_SHIFT == vcn <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 					vol->cluster_size_bits >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 					PAGE_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 				goto fast_descend_into_child_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 			ntfs_unmap_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 			goto descend_into_child_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		ntfs_error(sb, "Negative child node vcn in directory inode "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 				"0x%lx.", dir_ni->mft_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		goto unm_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	/* No child node, return -ENOENT. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	ntfs_debug("Entry not found.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) unm_err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	ntfs_unmap_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	if (ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 		ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	if (m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		unmap_mft_record(dir_ni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	return ERR_MREF(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) dir_err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	ntfs_error(sb, "Corrupt directory. Aborting lookup.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992)  * ntfs_filldir - ntfs specific filldir method
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993)  * @vol:	current ntfs volume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994)  * @ndir:	ntfs inode of current directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995)  * @ia_page:	page in which the index allocation buffer @ie is in resides
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996)  * @ie:		current index entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997)  * @name:	buffer to use for the converted name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998)  * @actor:	what to feed the entries to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)  * Convert the Unicode @name to the loaded NLS and pass it to the @filldir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001)  * callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003)  * If @ia_page is not NULL it is the locked page containing the index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)  * allocation block containing the index entry @ie.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006)  * Note, we drop (and then reacquire) the page lock on @ia_page across the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)  * @filldir() call otherwise we would deadlock with NFSd when it calls ->lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008)  * since ntfs_lookup() will lock the same page.  As an optimization, we do not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)  * retake the lock if we are returning a non-zero value as ntfs_readdir()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010)  * would need to drop the lock immediately anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) static inline int ntfs_filldir(ntfs_volume *vol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		ntfs_inode *ndir, struct page *ia_page, INDEX_ENTRY *ie,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		u8 *name, struct dir_context *actor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	unsigned long mref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	int name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	unsigned dt_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	FILE_NAME_TYPE_FLAGS name_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	name_type = ie->key.file_name.file_name_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	if (name_type == FILE_NAME_DOS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		ntfs_debug("Skipping DOS name space entry.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	if (MREF_LE(ie->data.dir.indexed_file) == FILE_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		ntfs_debug("Skipping root directory self reference entry.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	if (MREF_LE(ie->data.dir.indexed_file) < FILE_first_user &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 			!NVolShowSystemFiles(vol)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		ntfs_debug("Skipping system file.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	name_len = ntfs_ucstonls(vol, (ntfschar*)&ie->key.file_name.file_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 			ie->key.file_name.file_name_length, &name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 			NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	if (name_len <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		ntfs_warning(vol->sb, "Skipping unrepresentable inode 0x%llx.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 				(long long)MREF_LE(ie->data.dir.indexed_file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	if (ie->key.file_name.file_attributes &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 			FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		dt_type = DT_DIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		dt_type = DT_REG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	mref = MREF_LE(ie->data.dir.indexed_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	 * Drop the page lock otherwise we deadlock with NFS when it calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	 * ->lookup since ntfs_lookup() will lock the same page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	if (ia_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		unlock_page(ia_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	ntfs_debug("Calling filldir for %s with len %i, fpos 0x%llx, inode "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 			"0x%lx, DT_%s.", name, name_len, actor->pos, mref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 			dt_type == DT_DIR ? "DIR" : "REG");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	if (!dir_emit(actor, name, name_len, mref, dt_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	/* Relock the page but not if we are aborting ->readdir. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	if (ia_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		lock_page(ia_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067)  * We use the same basic approach as the old NTFS driver, i.e. we parse the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068)  * index root entries and then the index allocation entries that are marked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069)  * as in use in the index bitmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071)  * While this will return the names in random order this doesn't matter for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072)  * ->readdir but OTOH results in a faster ->readdir.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074)  * VFS calls ->readdir without BKL but with i_mutex held. This protects the VFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075)  * parts (e.g. ->f_pos and ->i_size, and it also protects against directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076)  * modifications).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078)  * Locking:  - Caller must hold i_mutex on the directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079)  *	     - Each page cache page in the index allocation mapping must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080)  *	       locked whilst being accessed otherwise we may find a corrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081)  *	       page due to it being under ->writepage at the moment which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082)  *	       applies the mst protection fixups before writing out and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083)  *	       removes them again after the write is complete after which it 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084)  *	       unlocks the page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) static int ntfs_readdir(struct file *file, struct dir_context *actor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	s64 ia_pos, ia_start, prev_ia_pos, bmp_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	loff_t i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	struct inode *bmp_vi, *vdir = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	struct super_block *sb = vdir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	ntfs_inode *ndir = NTFS_I(vdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	ntfs_volume *vol = NTFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	MFT_RECORD *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	INDEX_ROOT *ir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	INDEX_ENTRY *ie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	INDEX_ALLOCATION *ia;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	u8 *name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	int rc, err, ir_pos, cur_bmp_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	struct address_space *ia_mapping, *bmp_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	struct page *bmp_page = NULL, *ia_page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	u8 *kaddr, *bmp, *index_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	ntfs_attr_search_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	ntfs_debug("Entering for inode 0x%lx, fpos 0x%llx.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 			vdir->i_ino, actor->pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	rc = err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	/* Are we at end of dir yet? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	i_size = i_size_read(vdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	if (actor->pos >= i_size + vol->mft_record_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	/* Emulate . and .. for all directories. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	if (!dir_emit_dots(file, actor))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	m = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	ctx = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	 * Allocate a buffer to store the current name being processed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	 * converted to format determined by current NLS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	name = kmalloc(NTFS_MAX_NAME_LEN * NLS_MAX_CHARSET_SIZE + 1, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	if (unlikely(!name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	/* Are we jumping straight into the index allocation attribute? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	if (actor->pos >= vol->mft_record_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 		goto skip_index_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	/* Get hold of the mft record for the directory. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	m = map_mft_record(ndir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	if (IS_ERR(m)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 		err = PTR_ERR(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		m = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	ctx = ntfs_attr_get_search_ctx(ndir, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	if (unlikely(!ctx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	/* Get the offset into the index root attribute. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	ir_pos = (s64)actor->pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	/* Find the index root attribute in the mft record. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE, 0, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 			0, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		ntfs_error(sb, "Index root attribute missing in directory "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 				"inode 0x%lx.", vdir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	 * Copy the index root attribute value to a buffer so that we can put
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	 * the search context and unmap the mft record before calling the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	 * filldir() callback.  We need to do this because of NFSd which calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	 * ->lookup() from its filldir callback() and this causes NTFS to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	 * deadlock as ntfs_lookup() maps the mft record of the directory and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	 * we have got it mapped here already.  The only solution is for us to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	 * unmap the mft record here so that a call to ntfs_lookup() is able to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	 * map the mft record without deadlocking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	rc = le32_to_cpu(ctx->attr->data.resident.value_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	ir = kmalloc(rc, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	if (unlikely(!ir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	/* Copy the index root value (it has been verified in read_inode). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	memcpy(ir, (u8*)ctx->attr +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 			le16_to_cpu(ctx->attr->data.resident.value_offset), rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	unmap_mft_record(ndir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	ctx = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	m = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	/* The first index entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	ie = (INDEX_ENTRY*)((u8*)&ir->index +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 			le32_to_cpu(ir->index.entries_offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	 * Loop until we exceed valid memory (corruption case) or until we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	 * reach the last entry or until filldir tells us it has had enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	 * or signals an error (both covered by the rc test).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		ntfs_debug("In index root, offset 0x%zx.", (u8*)ie - (u8*)ir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 		/* Bounds checks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		if (unlikely((u8*)ie < (u8*)ir || (u8*)ie +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 				sizeof(INDEX_ENTRY_HEADER) > index_end ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 				(u8*)ie + le16_to_cpu(ie->key_length) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 				index_end))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 			goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		/* The last entry cannot contain a name. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 		if (ie->flags & INDEX_ENTRY_END)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		/* Skip index root entry if continuing previous readdir. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		if (ir_pos > (u8*)ie - (u8*)ir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 		/* Advance the position even if going to skip the entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		actor->pos = (u8*)ie - (u8*)ir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		/* Submit the name to the filldir callback. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 		rc = ntfs_filldir(vol, ndir, NULL, ie, name, actor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 			kfree(ir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 			goto abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	/* We are done with the index root and can free the buffer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	kfree(ir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	ir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	/* If there is no index allocation attribute we are finished. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	if (!NInoIndexAllocPresent(ndir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 		goto EOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	/* Advance fpos to the beginning of the index allocation. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	actor->pos = vol->mft_record_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) skip_index_root:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	kaddr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	prev_ia_pos = -1LL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	/* Get the offset into the index allocation attribute. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	ia_pos = (s64)actor->pos - vol->mft_record_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	ia_mapping = vdir->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	ntfs_debug("Inode 0x%lx, getting index bitmap.", vdir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	bmp_vi = ntfs_attr_iget(vdir, AT_BITMAP, I30, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	if (IS_ERR(bmp_vi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 		ntfs_error(sb, "Failed to get bitmap attribute.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		err = PTR_ERR(bmp_vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	bmp_mapping = bmp_vi->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	/* Get the starting bitmap bit position and sanity check it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	bmp_pos = ia_pos >> ndir->itype.index.block_size_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	if (unlikely(bmp_pos >> 3 >= i_size_read(bmp_vi))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 		ntfs_error(sb, "Current index allocation position exceeds "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 				"index bitmap size.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 		goto iput_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 	/* Get the starting bit position in the current bitmap page. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	cur_bmp_pos = bmp_pos & ((PAGE_SIZE * 8) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	bmp_pos &= ~(u64)((PAGE_SIZE * 8) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) get_next_bmp_page:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	ntfs_debug("Reading bitmap with page index 0x%llx, bit ofs 0x%llx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 			(unsigned long long)bmp_pos >> (3 + PAGE_SHIFT),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 			(unsigned long long)bmp_pos &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 			(unsigned long long)((PAGE_SIZE * 8) - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	bmp_page = ntfs_map_page(bmp_mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 			bmp_pos >> (3 + PAGE_SHIFT));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	if (IS_ERR(bmp_page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 		ntfs_error(sb, "Reading index bitmap failed.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 		err = PTR_ERR(bmp_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 		bmp_page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 		goto iput_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	bmp = (u8*)page_address(bmp_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	/* Find next index block in use. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	while (!(bmp[cur_bmp_pos >> 3] & (1 << (cur_bmp_pos & 7)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) find_next_index_buffer:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 		cur_bmp_pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 		 * If we have reached the end of the bitmap page, get the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		 * page, and put away the old one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 		if (unlikely((cur_bmp_pos >> 3) >= PAGE_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 			ntfs_unmap_page(bmp_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 			bmp_pos += PAGE_SIZE * 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 			cur_bmp_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 			goto get_next_bmp_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 		/* If we have reached the end of the bitmap, we are done. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 		if (unlikely(((bmp_pos + cur_bmp_pos) >> 3) >= i_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 			goto unm_EOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 		ia_pos = (bmp_pos + cur_bmp_pos) <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 				ndir->itype.index.block_size_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	ntfs_debug("Handling index buffer 0x%llx.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 			(unsigned long long)bmp_pos + cur_bmp_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	/* If the current index buffer is in the same page we reuse the page. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	if ((prev_ia_pos & (s64)PAGE_MASK) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 			(ia_pos & (s64)PAGE_MASK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 		prev_ia_pos = ia_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		if (likely(ia_page != NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 			unlock_page(ia_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 			ntfs_unmap_page(ia_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 		 * Map the page cache page containing the current ia_pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 		 * reading it from disk if necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 		ia_page = ntfs_map_page(ia_mapping, ia_pos >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 		if (IS_ERR(ia_page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 			ntfs_error(sb, "Reading index allocation data failed.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 			err = PTR_ERR(ia_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 			ia_page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 			goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 		lock_page(ia_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		kaddr = (u8*)page_address(ia_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	/* Get the current index buffer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	ia = (INDEX_ALLOCATION*)(kaddr + (ia_pos & ~PAGE_MASK &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 					  ~(s64)(ndir->itype.index.block_size - 1)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	/* Bounds checks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	if (unlikely((u8*)ia < kaddr || (u8*)ia > kaddr + PAGE_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 		ntfs_error(sb, "Out of bounds check failed. Corrupt directory "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 				"inode 0x%lx or driver bug.", vdir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	/* Catch multi sector transfer fixup errors. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	if (unlikely(!ntfs_is_indx_record(ia->magic))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 		ntfs_error(sb, "Directory index record with vcn 0x%llx is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 				"corrupt.  Corrupt inode 0x%lx.  Run chkdsk.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 				(unsigned long long)ia_pos >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 				ndir->itype.index.vcn_size_bits, vdir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	if (unlikely(sle64_to_cpu(ia->index_block_vcn) != (ia_pos &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 			~(s64)(ndir->itype.index.block_size - 1)) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 			ndir->itype.index.vcn_size_bits)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 		ntfs_error(sb, "Actual VCN (0x%llx) of index buffer is "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 				"different from expected VCN (0x%llx). "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 				"Directory inode 0x%lx is corrupt or driver "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 				"bug. ", (unsigned long long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 				sle64_to_cpu(ia->index_block_vcn),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 				(unsigned long long)ia_pos >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 				ndir->itype.index.vcn_size_bits, vdir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	if (unlikely(le32_to_cpu(ia->index.allocated_size) + 0x18 !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 			ndir->itype.index.block_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 				"0x%lx has a size (%u) differing from the "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 				"directory specified size (%u). Directory "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 				"inode is corrupt or driver bug.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 				(unsigned long long)ia_pos >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 				ndir->itype.index.vcn_size_bits, vdir->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 				le32_to_cpu(ia->index.allocated_size) + 0x18,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 				ndir->itype.index.block_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	index_end = (u8*)ia + ndir->itype.index.block_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	if (unlikely(index_end > kaddr + PAGE_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 		ntfs_error(sb, "Index buffer (VCN 0x%llx) of directory inode "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 				"0x%lx crosses page boundary. Impossible! "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 				"Cannot access! This is probably a bug in the "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 				"driver.", (unsigned long long)ia_pos >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 				ndir->itype.index.vcn_size_bits, vdir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	ia_start = ia_pos & ~(s64)(ndir->itype.index.block_size - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	if (unlikely(index_end > (u8*)ia + ndir->itype.index.block_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		ntfs_error(sb, "Size of index buffer (VCN 0x%llx) of directory "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 				"inode 0x%lx exceeds maximum size.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 				(unsigned long long)ia_pos >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 				ndir->itype.index.vcn_size_bits, vdir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 		goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 	/* The first index entry in this index buffer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	ie = (INDEX_ENTRY*)((u8*)&ia->index +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 			le32_to_cpu(ia->index.entries_offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	 * Loop until we exceed valid memory (corruption case) or until we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	 * reach the last entry or until filldir tells us it has had enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	 * or signals an error (both covered by the rc test).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 		ntfs_debug("In index allocation, offset 0x%llx.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 				(unsigned long long)ia_start +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 				(unsigned long long)((u8*)ie - (u8*)ia));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 		/* Bounds checks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 		if (unlikely((u8*)ie < (u8*)ia || (u8*)ie +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 				sizeof(INDEX_ENTRY_HEADER) > index_end ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 				(u8*)ie + le16_to_cpu(ie->key_length) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 				index_end))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 			goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 		/* The last entry cannot contain a name. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 		if (ie->flags & INDEX_ENTRY_END)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 		/* Skip index block entry if continuing previous readdir. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 		if (ia_pos - ia_start > (u8*)ie - (u8*)ia)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 		/* Advance the position even if going to skip the entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 		actor->pos = (u8*)ie - (u8*)ia +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 				(sle64_to_cpu(ia->index_block_vcn) <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 				ndir->itype.index.vcn_size_bits) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 				vol->mft_record_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 		 * Submit the name to the @filldir callback.  Note,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 		 * ntfs_filldir() drops the lock on @ia_page but it retakes it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 		 * before returning, unless a non-zero value is returned in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		 * which case the page is left unlocked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		rc = ntfs_filldir(vol, ndir, ia_page, ie, name, actor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 			/* @ia_page is already unlocked in this case. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 			ntfs_unmap_page(ia_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 			ntfs_unmap_page(bmp_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 			iput(bmp_vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 			goto abort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	goto find_next_index_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) unm_EOD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	if (ia_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 		unlock_page(ia_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 		ntfs_unmap_page(ia_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	ntfs_unmap_page(bmp_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	iput(bmp_vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) EOD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	/* We are finished, set fpos to EOD. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	actor->pos = i_size + vol->mft_record_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) abort:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	if (bmp_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 		ntfs_unmap_page(bmp_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) iput_err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 		iput(bmp_vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	if (ia_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 		unlock_page(ia_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 		ntfs_unmap_page(ia_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	kfree(ir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	if (ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		ntfs_attr_put_search_ctx(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	if (m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 		unmap_mft_record(ndir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	ntfs_debug("Failed. Returning error code %i.", -err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436)  * ntfs_dir_open - called when an inode is about to be opened
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437)  * @vi:		inode to be opened
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438)  * @filp:	file structure describing the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440)  * Limit directory size to the page cache limit on architectures where unsigned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441)  * long is 32-bits. This is the most we can do for now without overflowing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442)  * page cache page index. Doing it this way means we don't run into problems
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443)  * because of existing too large directories. It would be better to allow the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444)  * user to read the accessible part of the directory but I doubt very much
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445)  * anyone is going to hit this check on a 32-bit architecture, so there is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446)  * point in adding the extra complexity required to support this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448)  * On 64-bit architectures, the check is hopefully optimized away by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449)  * compiler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) static int ntfs_dir_open(struct inode *vi, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	if (sizeof(unsigned long) < 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 		if (i_size_read(vi) > MAX_LFS_FILESIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 			return -EFBIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) #ifdef NTFS_RW
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463)  * ntfs_dir_fsync - sync a directory to disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464)  * @filp:	directory to be synced
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465)  * @dentry:	dentry describing the directory to sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466)  * @datasync:	if non-zero only flush user data and not metadata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468)  * Data integrity sync of a directory to disk.  Used for fsync, fdatasync, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469)  * msync system calls.  This function is based on file.c::ntfs_file_fsync().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471)  * Write the mft record and all associated extent mft records as well as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472)  * $INDEX_ALLOCATION and $BITMAP attributes and then sync the block device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474)  * If @datasync is true, we do not wait on the inode(s) to be written out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475)  * but we always wait on the page cache pages to be written out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477)  * Note: In the past @filp could be NULL so we ignore it as we don't need it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478)  * anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480)  * Locking: Caller must hold i_mutex on the inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482)  * TODO: We should probably also write all attribute/index inodes associated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483)  * with this inode but since we have no simple way of getting to them we ignore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484)  * this problem for now.  We do write the $BITMAP attribute if it is present
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485)  * which is the important one for a directory so things are not too bad.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) static int ntfs_dir_fsync(struct file *filp, loff_t start, loff_t end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 			  int datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	struct inode *bmp_vi, *vi = filp->f_mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	int err, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	ntfs_attr na;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	err = file_write_and_wait_range(filp, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	inode_lock(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	BUG_ON(!S_ISDIR(vi->i_mode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 	/* If the bitmap attribute inode is in memory sync it, too. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	na.mft_no = vi->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	na.type = AT_BITMAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	na.name = I30;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	na.name_len = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 	bmp_vi = ilookup5(vi->i_sb, vi->i_ino, ntfs_test_inode, &na);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	if (bmp_vi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509)  		write_inode_now(bmp_vi, !datasync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 		iput(bmp_vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	ret = __ntfs_write_inode(vi, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	write_inode_now(vi, !datasync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	err = sync_blockdev(vi->i_sb->s_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	if (unlikely(err && !ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 		ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	if (likely(!ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 		ntfs_debug("Done.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 		ntfs_warning(vi->i_sb, "Failed to f%ssync inode 0x%lx.  Error "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 				"%u.", datasync ? "data" : "", vi->i_ino, -ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	inode_unlock(vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) #endif /* NTFS_RW */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) const struct file_operations ntfs_dir_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	.llseek		= generic_file_llseek,	/* Seek inside directory. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	.read		= generic_read_dir,	/* Return -EISDIR. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 	.iterate	= ntfs_readdir,		/* Read directory contents. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) #ifdef NTFS_RW
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	.fsync		= ntfs_dir_fsync,	/* Sync a directory to disk. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) #endif /* NTFS_RW */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	/*.ioctl	= ,*/			/* Perform function on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 						   mounted filesystem. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	.open		= ntfs_dir_open,	/* Open directory. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) };