| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  *   fs/cifs/readdir.c | 
 | 3 |  * | 
 | 4 |  *   Directory search handling | 
| Steve French | 6dc0f87 | 2007-07-06 23:13:06 +0000 | [diff] [blame] | 5 |  * | 
| Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 6 |  *   Copyright (C) International Business Machines  Corp., 2004, 2008 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 |  *   Author(s): Steve French (sfrench@us.ibm.com) | 
 | 8 |  * | 
 | 9 |  *   This library is free software; you can redistribute it and/or modify | 
 | 10 |  *   it under the terms of the GNU Lesser General Public License as published | 
 | 11 |  *   by the Free Software Foundation; either version 2.1 of the License, or | 
 | 12 |  *   (at your option) any later version. | 
 | 13 |  * | 
 | 14 |  *   This library is distributed in the hope that it will be useful, | 
 | 15 |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 16 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See | 
 | 17 |  *   the GNU Lesser General Public License for more details. | 
 | 18 |  * | 
 | 19 |  *   You should have received a copy of the GNU Lesser General Public License | 
 | 20 |  *   along with this library; if not, write to the Free Software | 
 | 21 |  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 
 | 22 |  */ | 
 | 23 | #include <linux/fs.h> | 
| Dave Kleikamp | 273d81d | 2006-06-01 19:41:23 +0000 | [diff] [blame] | 24 | #include <linux/pagemap.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/stat.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "cifspdu.h" | 
 | 27 | #include "cifsglob.h" | 
 | 28 | #include "cifsproto.h" | 
 | 29 | #include "cifs_unicode.h" | 
 | 30 | #include "cifs_debug.h" | 
 | 31 | #include "cifs_fs_sb.h" | 
 | 32 | #include "cifsfs.h" | 
 | 33 |  | 
| Jeff Layton | f588416 | 2009-04-30 07:18:00 -0400 | [diff] [blame] | 34 | /* | 
 | 35 |  * To be safe - for UCS to UTF-8 with strings loaded with the rare long | 
 | 36 |  * characters alloc more to account for such multibyte target UTF-8 | 
 | 37 |  * characters. | 
 | 38 |  */ | 
 | 39 | #define UNICODE_NAME_MAX ((4 * NAME_MAX) + 2) | 
 | 40 |  | 
| Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 41 | #ifdef CONFIG_CIFS_DEBUG2 | 
 | 42 | static void dump_cifs_file_struct(struct file *file, char *label) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { | 
| Steve French | 6dc0f87 | 2007-07-06 23:13:06 +0000 | [diff] [blame] | 44 | 	struct cifsFileInfo *cf; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 |  | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 46 | 	if (file) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | 		cf = file->private_data; | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 48 | 		if (cf == NULL) { | 
| Steve French | 6dc0f87 | 2007-07-06 23:13:06 +0000 | [diff] [blame] | 49 | 			cFYI(1, ("empty cifs private file data")); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | 			return; | 
 | 51 | 		} | 
| Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 52 | 		if (cf->invalidHandle) | 
| Steve French | 6dc0f87 | 2007-07-06 23:13:06 +0000 | [diff] [blame] | 53 | 			cFYI(1, ("invalid handle")); | 
| Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 54 | 		if (cf->srch_inf.endOfSearch) | 
| Steve French | 6dc0f87 | 2007-07-06 23:13:06 +0000 | [diff] [blame] | 55 | 			cFYI(1, ("end of search")); | 
| Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 56 | 		if (cf->srch_inf.emptyDir) | 
| Steve French | 6dc0f87 | 2007-07-06 23:13:06 +0000 | [diff] [blame] | 57 | 			cFYI(1, ("empty dir")); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | 	} | 
| Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 59 | } | 
| Steve French | 90c81e0 | 2008-02-12 20:32:36 +0000 | [diff] [blame] | 60 | #else | 
 | 61 | static inline void dump_cifs_file_struct(struct file *file, char *label) | 
 | 62 | { | 
 | 63 | } | 
| Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 64 | #endif /* DEBUG2 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 |  | 
| Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 66 | /* | 
 | 67 |  * Find the dentry that matches "name". If there isn't one, create one. If it's | 
 | 68 |  * a negative dentry or the uniqueid changed, then drop it and recreate it. | 
 | 69 |  */ | 
 | 70 | static struct dentry * | 
 | 71 | cifs_readdir_lookup(struct dentry *parent, struct qstr *name, | 
 | 72 | 		    struct cifs_fattr *fattr) | 
 | 73 | { | 
 | 74 | 	struct dentry *dentry, *alias; | 
 | 75 | 	struct inode *inode; | 
 | 76 | 	struct super_block *sb = parent->d_inode->i_sb; | 
 | 77 |  | 
 | 78 | 	cFYI(1, ("For %s", name->name)); | 
 | 79 |  | 
 | 80 | 	dentry = d_lookup(parent, name); | 
 | 81 | 	if (dentry) { | 
 | 82 | 		/* FIXME: check for inode number changes? */ | 
 | 83 | 		if (dentry->d_inode != NULL) | 
 | 84 | 			return dentry; | 
 | 85 | 		d_drop(dentry); | 
 | 86 | 		dput(dentry); | 
 | 87 | 	} | 
 | 88 |  | 
 | 89 | 	dentry = d_alloc(parent, name); | 
 | 90 | 	if (dentry == NULL) | 
 | 91 | 		return NULL; | 
 | 92 |  | 
 | 93 | 	inode = cifs_iget(sb, fattr); | 
 | 94 | 	if (!inode) { | 
 | 95 | 		dput(dentry); | 
 | 96 | 		return NULL; | 
 | 97 | 	} | 
 | 98 |  | 
 | 99 | 	if (CIFS_SB(sb)->tcon->nocase) | 
 | 100 | 		dentry->d_op = &cifs_ci_dentry_ops; | 
 | 101 | 	else | 
 | 102 | 		dentry->d_op = &cifs_dentry_ops; | 
 | 103 |  | 
 | 104 | 	alias = d_materialise_unique(dentry, inode); | 
 | 105 | 	if (alias != NULL) { | 
 | 106 | 		dput(dentry); | 
 | 107 | 		if (IS_ERR(alias)) | 
 | 108 | 			return NULL; | 
 | 109 | 		dentry = alias; | 
 | 110 | 	} | 
 | 111 |  | 
 | 112 | 	return dentry; | 
 | 113 | } | 
 | 114 |  | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 115 | static void | 
 | 116 | cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 118 | 	fattr->cf_uid = cifs_sb->mnt_uid; | 
 | 119 | 	fattr->cf_gid = cifs_sb->mnt_gid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 |  | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 121 | 	if (fattr->cf_cifsattrs & ATTR_DIRECTORY) { | 
 | 122 | 		fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode; | 
 | 123 | 		fattr->cf_dtype = DT_DIR; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | 	} else { | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 125 | 		fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode; | 
 | 126 | 		fattr->cf_dtype = DT_REG; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | 	} | 
 | 128 |  | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 129 | 	if (fattr->cf_cifsattrs & ATTR_READONLY) | 
 | 130 | 		fattr->cf_mode &= ~S_IWUGO; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 |  | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 132 | 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL && | 
 | 133 | 	    fattr->cf_cifsattrs & ATTR_SYSTEM) { | 
 | 134 | 		if (fattr->cf_eof == 0)  { | 
 | 135 | 			fattr->cf_mode &= ~S_IFMT; | 
 | 136 | 			fattr->cf_mode |= S_IFIFO; | 
 | 137 | 			fattr->cf_dtype = DT_FIFO; | 
| Steve French | 3020a1f | 2005-11-18 11:31:10 -0800 | [diff] [blame] | 138 | 		} else { | 
| Jeff Layton | 4468eb3 | 2008-05-22 09:31:40 -0400 | [diff] [blame] | 139 | 			/* | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 140 | 			 * trying to get the type and mode via SFU can be slow, | 
 | 141 | 			 * so just call those regular files for now, and mark | 
 | 142 | 			 * for reval | 
| Jeff Layton | 4468eb3 | 2008-05-22 09:31:40 -0400 | [diff] [blame] | 143 | 			 */ | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 144 | 			fattr->cf_flags |= CIFS_FATTR_NEED_REVAL; | 
| Jeff Layton | 4468eb3 | 2008-05-22 09:31:40 -0400 | [diff] [blame] | 145 | 		} | 
 | 146 | 	} | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 147 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 |  | 
| Steve French | 15dd478 | 2009-09-25 02:24:45 +0000 | [diff] [blame] | 149 | static void | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 150 | cifs_dir_info_to_fattr(struct cifs_fattr *fattr, FILE_DIRECTORY_INFO *info, | 
 | 151 | 		       struct cifs_sb_info *cifs_sb) | 
 | 152 | { | 
 | 153 | 	memset(fattr, 0, sizeof(*fattr)); | 
 | 154 | 	fattr->cf_cifsattrs = le32_to_cpu(info->ExtFileAttributes); | 
 | 155 | 	fattr->cf_eof = le64_to_cpu(info->EndOfFile); | 
 | 156 | 	fattr->cf_bytes = le64_to_cpu(info->AllocationSize); | 
 | 157 | 	fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime); | 
 | 158 | 	fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime); | 
 | 159 | 	fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 |  | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 161 | 	cifs_fill_common_info(fattr, cifs_sb); | 
 | 162 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 |  | 
| Steve French | 15dd478 | 2009-09-25 02:24:45 +0000 | [diff] [blame] | 164 | static void | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 165 | cifs_std_info_to_fattr(struct cifs_fattr *fattr, FIND_FILE_STANDARD_INFO *info, | 
 | 166 | 		       struct cifs_sb_info *cifs_sb) | 
 | 167 | { | 
 | 168 | 	int offset = cifs_sb->tcon->ses->server->timeAdj; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 |  | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 170 | 	memset(fattr, 0, sizeof(*fattr)); | 
 | 171 | 	fattr->cf_atime = cnvrtDosUnixTm(info->LastAccessDate, | 
 | 172 | 					    info->LastAccessTime, offset); | 
 | 173 | 	fattr->cf_ctime = cnvrtDosUnixTm(info->LastWriteDate, | 
 | 174 | 					    info->LastWriteTime, offset); | 
 | 175 | 	fattr->cf_mtime = cnvrtDosUnixTm(info->LastWriteDate, | 
 | 176 | 					    info->LastWriteTime, offset); | 
| Steve French | f3f6ec4 | 2006-01-08 20:12:58 -0800 | [diff] [blame] | 177 |  | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 178 | 	fattr->cf_cifsattrs = le16_to_cpu(info->Attributes); | 
 | 179 | 	fattr->cf_bytes = le32_to_cpu(info->AllocationSize); | 
 | 180 | 	fattr->cf_eof = le32_to_cpu(info->DataSize); | 
| Dave Kleikamp | 273d81d | 2006-06-01 19:41:23 +0000 | [diff] [blame] | 181 |  | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 182 | 	cifs_fill_common_info(fattr, cifs_sb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } | 
 | 184 |  | 
| Steve French | 0e0d2cf | 2009-05-01 05:27:32 +0000 | [diff] [blame] | 185 | /* BB eventually need to add the following helper function to | 
 | 186 |       resolve NT_STATUS_STOPPED_ON_SYMLINK return code when | 
 | 187 |       we try to do FindFirst on (NTFS) directory symlinks */ | 
 | 188 | /* | 
 | 189 | int get_symlink_reparse_path(char *full_path, struct cifs_sb_info *cifs_sb, | 
 | 190 | 			     int xid) | 
 | 191 | { | 
 | 192 | 	__u16 fid; | 
 | 193 | 	int len; | 
 | 194 | 	int oplock = 0; | 
 | 195 | 	int rc; | 
 | 196 | 	struct cifsTconInfo *ptcon = cifs_sb->tcon; | 
 | 197 | 	char *tmpbuffer; | 
 | 198 |  | 
 | 199 | 	rc = CIFSSMBOpen(xid, ptcon, full_path, FILE_OPEN, GENERIC_READ, | 
 | 200 | 			OPEN_REPARSE_POINT, &fid, &oplock, NULL, | 
 | 201 | 			cifs_sb->local_nls, | 
 | 202 | 			cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); | 
 | 203 | 	if (!rc) { | 
 | 204 | 		tmpbuffer = kmalloc(maxpath); | 
 | 205 | 		rc = CIFSSMBQueryReparseLinkInfo(xid, ptcon, full_path, | 
 | 206 | 				tmpbuffer, | 
 | 207 | 				maxpath -1, | 
 | 208 | 				fid, | 
 | 209 | 				cifs_sb->local_nls); | 
 | 210 | 		if (CIFSSMBClose(xid, ptcon, fid)) { | 
 | 211 | 			cFYI(1, ("Error closing temporary reparsepoint open)")); | 
 | 212 | 		} | 
 | 213 | 	} | 
 | 214 | } | 
 | 215 |  */ | 
 | 216 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | static int initiate_cifs_search(const int xid, struct file *file) | 
 | 218 | { | 
 | 219 | 	int rc = 0; | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 220 | 	char *full_path; | 
 | 221 | 	struct cifsFileInfo *cifsFile; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | 	struct cifs_sb_info *cifs_sb; | 
 | 223 | 	struct cifsTconInfo *pTcon; | 
 | 224 |  | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 225 | 	if (file->private_data == NULL) { | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 226 | 		file->private_data = | 
 | 227 | 			kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | 	} | 
 | 229 |  | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 230 | 	if (file->private_data == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | 		return -ENOMEM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | 	cifsFile = file->private_data; | 
| Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 233 | 	cifsFile->invalidHandle = true; | 
 | 234 | 	cifsFile->srch_inf.endOfSearch = false; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 |  | 
| Josef "Jeff" Sipek | e6a0029 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 236 | 	cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 237 | 	if (cifs_sb == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | 		return -EINVAL; | 
 | 239 |  | 
 | 240 | 	pTcon = cifs_sb->tcon; | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 241 | 	if (pTcon == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | 		return -EINVAL; | 
 | 243 |  | 
| Josef "Jeff" Sipek | e6a0029 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 244 | 	full_path = build_path_from_dentry(file->f_path.dentry); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 |  | 
| Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 246 | 	if (full_path == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | 		return -ENOMEM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 |  | 
| Steve French | 966ca92 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 249 | 	cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 |  | 
| Steve French | 75cf6bd | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 251 | ffirst_retry: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | 	/* test for Unix extensions */ | 
| Steve French | c18c842 | 2007-07-18 23:21:09 +0000 | [diff] [blame] | 253 | 	/* but now check for them on the share/mount not on the SMB session */ | 
 | 254 | /*	if (pTcon->ses->capabilities & CAP_UNIX) { */ | 
| Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 255 | 	if (pTcon->unix_ext) | 
| Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 256 | 		cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX; | 
| Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 257 | 	else if ((pTcon->ses->capabilities & | 
| Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 258 | 			(CAP_NT_SMBS | CAP_NT_FIND)) == 0) { | 
 | 259 | 		cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | 	} else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) { | 
 | 261 | 		cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO; | 
 | 262 | 	} else /* not srvinos - BB fixme add check for backlevel? */ { | 
 | 263 | 		cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO; | 
 | 264 | 	} | 
 | 265 |  | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 266 | 	rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls, | 
| Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 267 | 		&cifsFile->netfid, &cifsFile->srch_inf, | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 268 | 		cifs_sb->mnt_cifs_flags & | 
| Steve French | eafe870 | 2005-09-15 21:47:30 -0700 | [diff] [blame] | 269 | 			CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb)); | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 270 | 	if (rc == 0) | 
| Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 271 | 		cifsFile->invalidHandle = false; | 
| Steve French | e836f01 | 2009-05-01 16:20:35 +0000 | [diff] [blame] | 272 | 	/* BB add following call to handle readdir on new NTFS symlink errors | 
| Steve French | 0e0d2cf | 2009-05-01 05:27:32 +0000 | [diff] [blame] | 273 | 	else if STATUS_STOPPED_ON_SYMLINK | 
 | 274 | 		call get_symlink_reparse_path and retry with new path */ | 
 | 275 | 	else if ((rc == -EOPNOTSUPP) && | 
| Steve French | 75cf6bd | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 276 | 		(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) { | 
 | 277 | 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM; | 
 | 278 | 		goto ffirst_retry; | 
 | 279 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | 	kfree(full_path); | 
 | 281 | 	return rc; | 
 | 282 | } | 
 | 283 |  | 
 | 284 | /* return length of unicode string in bytes */ | 
 | 285 | static int cifs_unicode_bytelen(char *str) | 
 | 286 | { | 
 | 287 | 	int len; | 
| Steve French | 63d2583 | 2007-11-05 21:46:10 +0000 | [diff] [blame] | 288 | 	__le16 *ustr = (__le16 *)str; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 |  | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 290 | 	for (len = 0; len <= PATH_MAX; len++) { | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 291 | 		if (ustr[len] == 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | 			return len << 1; | 
 | 293 | 	} | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 294 | 	cFYI(1, ("Unicode string longer than PATH_MAX found")); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | 	return len << 1; | 
 | 296 | } | 
 | 297 |  | 
| Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 298 | static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | { | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 300 | 	char *new_entry; | 
| Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 301 | 	FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 |  | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 303 | 	if (level == SMB_FIND_FILE_INFO_STANDARD) { | 
| Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 304 | 		FIND_FILE_STANDARD_INFO *pfData; | 
| Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 305 | 		pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo; | 
 | 306 |  | 
 | 307 | 		new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) + | 
 | 308 | 				pfData->FileNameLength; | 
 | 309 | 	} else | 
 | 310 | 		new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset); | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 311 | 	cFYI(1, ("new entry %p old entry %p", new_entry, old_entry)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | 	/* validate that new_entry is not past end of SMB */ | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 313 | 	if (new_entry >= end_of_smb) { | 
| Steve French | 09d1db5 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 314 | 		cERROR(1, | 
 | 315 | 		      ("search entry %p began after end of SMB %p old entry %p", | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 316 | 			new_entry, end_of_smb, old_entry)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | 		return NULL; | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 318 | 	} else if (((level == SMB_FIND_FILE_INFO_STANDARD) && | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 319 | 		    (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb)) | 
 | 320 | 		  || ((level != SMB_FIND_FILE_INFO_STANDARD) && | 
| Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 321 | 		   (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb)))  { | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 322 | 		cERROR(1, ("search entry %p extends after end of SMB %p", | 
| Steve French | 09d1db5 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 323 | 			new_entry, end_of_smb)); | 
 | 324 | 		return NULL; | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 325 | 	} else | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | 		return new_entry; | 
 | 327 |  | 
 | 328 | } | 
 | 329 |  | 
 | 330 | #define UNICODE_DOT cpu_to_le16(0x2e) | 
 | 331 |  | 
 | 332 | /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */ | 
 | 333 | static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile) | 
 | 334 | { | 
 | 335 | 	int rc = 0; | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 336 | 	char *filename = NULL; | 
 | 337 | 	int len = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 |  | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 339 | 	if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) { | 
| Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 340 | 		FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | 		filename = &pFindData->FileName[0]; | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 342 | 		if (cfile->srch_inf.unicode) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | 			len = cifs_unicode_bytelen(filename); | 
 | 344 | 		} else { | 
 | 345 | 			/* BB should we make this strnlen of PATH_MAX? */ | 
 | 346 | 			len = strnlen(filename, 5); | 
 | 347 | 		} | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 348 | 	} else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) { | 
| Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 349 | 		FILE_DIRECTORY_INFO *pFindData = | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | 			(FILE_DIRECTORY_INFO *)current_entry; | 
 | 351 | 		filename = &pFindData->FileName[0]; | 
 | 352 | 		len = le32_to_cpu(pFindData->FileNameLength); | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 353 | 	} else if (cfile->srch_inf.info_level == | 
| Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 354 | 			SMB_FIND_FILE_FULL_DIRECTORY_INFO) { | 
| Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 355 | 		FILE_FULL_DIRECTORY_INFO *pFindData = | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | 			(FILE_FULL_DIRECTORY_INFO *)current_entry; | 
 | 357 | 		filename = &pFindData->FileName[0]; | 
 | 358 | 		len = le32_to_cpu(pFindData->FileNameLength); | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 359 | 	} else if (cfile->srch_inf.info_level == | 
| Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 360 | 			SMB_FIND_FILE_ID_FULL_DIR_INFO) { | 
| Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 361 | 		SEARCH_ID_FULL_DIR_INFO *pFindData = | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | 			(SEARCH_ID_FULL_DIR_INFO *)current_entry; | 
 | 363 | 		filename = &pFindData->FileName[0]; | 
 | 364 | 		len = le32_to_cpu(pFindData->FileNameLength); | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 365 | 	} else if (cfile->srch_inf.info_level == | 
| Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 366 | 			SMB_FIND_FILE_BOTH_DIRECTORY_INFO) { | 
| Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 367 | 		FILE_BOTH_DIRECTORY_INFO *pFindData = | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | 			(FILE_BOTH_DIRECTORY_INFO *)current_entry; | 
 | 369 | 		filename = &pFindData->FileName[0]; | 
 | 370 | 		len = le32_to_cpu(pFindData->FileNameLength); | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 371 | 	} else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) { | 
| Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 372 | 		FIND_FILE_STANDARD_INFO *pFindData = | 
| Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 373 | 			(FIND_FILE_STANDARD_INFO *)current_entry; | 
 | 374 | 		filename = &pFindData->FileName[0]; | 
| Steve French | 5ddaa68 | 2006-08-15 13:35:48 +0000 | [diff] [blame] | 375 | 		len = pFindData->FileNameLength; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | 	} else { | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 377 | 		cFYI(1, ("Unknown findfirst level %d", | 
 | 378 | 			 cfile->srch_inf.info_level)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | 	} | 
 | 380 |  | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 381 | 	if (filename) { | 
 | 382 | 		if (cfile->srch_inf.unicode) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | 			__le16 *ufilename = (__le16 *)filename; | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 384 | 			if (len == 2) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | 				/* check for . */ | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 386 | 				if (ufilename[0] == UNICODE_DOT) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | 					rc = 1; | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 388 | 			} else if (len == 4) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | 				/* check for .. */ | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 390 | 				if ((ufilename[0] == UNICODE_DOT) | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 391 | 				   && (ufilename[1] == UNICODE_DOT)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | 					rc = 2; | 
 | 393 | 			} | 
 | 394 | 		} else /* ASCII */ { | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 395 | 			if (len == 1) { | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 396 | 				if (filename[0] == '.') | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | 					rc = 1; | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 398 | 			} else if (len == 2) { | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 399 | 				if ((filename[0] == '.') && (filename[1] == '.')) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | 					rc = 2; | 
 | 401 | 			} | 
 | 402 | 		} | 
 | 403 | 	} | 
 | 404 |  | 
 | 405 | 	return rc; | 
 | 406 | } | 
 | 407 |  | 
| Steve French | eafe870 | 2005-09-15 21:47:30 -0700 | [diff] [blame] | 408 | /* Check if directory that we are searching has changed so we can decide | 
 | 409 |    whether we can use the cached search results from the previous search */ | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 410 | static int is_dir_changed(struct file *file) | 
| Steve French | eafe870 | 2005-09-15 21:47:30 -0700 | [diff] [blame] | 411 | { | 
| Christoph Hellwig | c33f8d3 | 2007-04-02 18:47:20 +0000 | [diff] [blame] | 412 | 	struct inode *inode = file->f_path.dentry->d_inode; | 
 | 413 | 	struct cifsInodeInfo *cifsInfo = CIFS_I(inode); | 
| Steve French | eafe870 | 2005-09-15 21:47:30 -0700 | [diff] [blame] | 414 |  | 
| Christoph Hellwig | c33f8d3 | 2007-04-02 18:47:20 +0000 | [diff] [blame] | 415 | 	if (cifsInfo->time == 0) | 
| Steve French | eafe870 | 2005-09-15 21:47:30 -0700 | [diff] [blame] | 416 | 		return 1; /* directory was changed, perhaps due to unlink */ | 
 | 417 | 	else | 
 | 418 | 		return 0; | 
 | 419 |  | 
 | 420 | } | 
 | 421 |  | 
| Steve French | 0752f15 | 2008-10-07 20:03:33 +0000 | [diff] [blame] | 422 | static int cifs_save_resume_key(const char *current_entry, | 
 | 423 | 	struct cifsFileInfo *cifsFile) | 
 | 424 | { | 
 | 425 | 	int rc = 0; | 
 | 426 | 	unsigned int len = 0; | 
 | 427 | 	__u16 level; | 
 | 428 | 	char *filename; | 
 | 429 |  | 
 | 430 | 	if ((cifsFile == NULL) || (current_entry == NULL)) | 
 | 431 | 		return -EINVAL; | 
 | 432 |  | 
 | 433 | 	level = cifsFile->srch_inf.info_level; | 
 | 434 |  | 
 | 435 | 	if (level == SMB_FIND_FILE_UNIX) { | 
 | 436 | 		FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry; | 
 | 437 |  | 
 | 438 | 		filename = &pFindData->FileName[0]; | 
 | 439 | 		if (cifsFile->srch_inf.unicode) { | 
 | 440 | 			len = cifs_unicode_bytelen(filename); | 
 | 441 | 		} else { | 
 | 442 | 			/* BB should we make this strnlen of PATH_MAX? */ | 
 | 443 | 			len = strnlen(filename, PATH_MAX); | 
 | 444 | 		} | 
 | 445 | 		cifsFile->srch_inf.resume_key = pFindData->ResumeKey; | 
 | 446 | 	} else if (level == SMB_FIND_FILE_DIRECTORY_INFO) { | 
 | 447 | 		FILE_DIRECTORY_INFO *pFindData = | 
 | 448 | 			(FILE_DIRECTORY_INFO *)current_entry; | 
 | 449 | 		filename = &pFindData->FileName[0]; | 
 | 450 | 		len = le32_to_cpu(pFindData->FileNameLength); | 
 | 451 | 		cifsFile->srch_inf.resume_key = pFindData->FileIndex; | 
 | 452 | 	} else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) { | 
 | 453 | 		FILE_FULL_DIRECTORY_INFO *pFindData = | 
 | 454 | 			(FILE_FULL_DIRECTORY_INFO *)current_entry; | 
 | 455 | 		filename = &pFindData->FileName[0]; | 
 | 456 | 		len = le32_to_cpu(pFindData->FileNameLength); | 
 | 457 | 		cifsFile->srch_inf.resume_key = pFindData->FileIndex; | 
 | 458 | 	} else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) { | 
 | 459 | 		SEARCH_ID_FULL_DIR_INFO *pFindData = | 
 | 460 | 			(SEARCH_ID_FULL_DIR_INFO *)current_entry; | 
 | 461 | 		filename = &pFindData->FileName[0]; | 
 | 462 | 		len = le32_to_cpu(pFindData->FileNameLength); | 
 | 463 | 		cifsFile->srch_inf.resume_key = pFindData->FileIndex; | 
 | 464 | 	} else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) { | 
 | 465 | 		FILE_BOTH_DIRECTORY_INFO *pFindData = | 
 | 466 | 			(FILE_BOTH_DIRECTORY_INFO *)current_entry; | 
 | 467 | 		filename = &pFindData->FileName[0]; | 
 | 468 | 		len = le32_to_cpu(pFindData->FileNameLength); | 
 | 469 | 		cifsFile->srch_inf.resume_key = pFindData->FileIndex; | 
 | 470 | 	} else if (level == SMB_FIND_FILE_INFO_STANDARD) { | 
 | 471 | 		FIND_FILE_STANDARD_INFO *pFindData = | 
 | 472 | 			(FIND_FILE_STANDARD_INFO *)current_entry; | 
 | 473 | 		filename = &pFindData->FileName[0]; | 
 | 474 | 		/* one byte length, no name conversion */ | 
 | 475 | 		len = (unsigned int)pFindData->FileNameLength; | 
 | 476 | 		cifsFile->srch_inf.resume_key = pFindData->ResumeKey; | 
 | 477 | 	} else { | 
 | 478 | 		cFYI(1, ("Unknown findfirst level %d", level)); | 
 | 479 | 		return -EINVAL; | 
 | 480 | 	} | 
 | 481 | 	cifsFile->srch_inf.resume_name_len = len; | 
 | 482 | 	cifsFile->srch_inf.presume_name = filename; | 
 | 483 | 	return rc; | 
 | 484 | } | 
 | 485 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | /* find the corresponding entry in the search */ | 
 | 487 | /* Note that the SMB server returns search entries for . and .. which | 
 | 488 |    complicates logic here if we choose to parse for them and we do not | 
 | 489 |    assume that they are located in the findfirst return buffer.*/ | 
 | 490 | /* We start counting in the buffer with entry 2 and increment for every | 
 | 491 |    entry (do not increment for . or .. entry) */ | 
 | 492 | static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon, | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 493 | 	struct file *file, char **ppCurrentEntry, int *num_to_ret) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | { | 
 | 495 | 	int rc = 0; | 
 | 496 | 	int pos_in_buf = 0; | 
 | 497 | 	loff_t first_entry_in_buffer; | 
 | 498 | 	loff_t index_to_find = file->f_pos; | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 499 | 	struct cifsFileInfo *cifsFile = file->private_data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | 	/* check if index in the buffer */ | 
| Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 501 |  | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 502 | 	if ((cifsFile == NULL) || (ppCurrentEntry == NULL) || | 
| Steve French | eafe870 | 2005-09-15 21:47:30 -0700 | [diff] [blame] | 503 | 	   (num_to_ret == NULL)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | 		return -ENOENT; | 
| Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 505 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | 	*ppCurrentEntry = NULL; | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 507 | 	first_entry_in_buffer = | 
 | 508 | 		cifsFile->srch_inf.index_of_last_entry - | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | 			cifsFile->srch_inf.entries_in_buffer; | 
| Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 510 |  | 
 | 511 | 	/* if first entry in buf is zero then is first buffer | 
 | 512 | 	in search response data which means it is likely . and .. | 
 | 513 | 	will be in this buffer, although some servers do not return | 
 | 514 | 	. and .. for the root of a drive and for those we need | 
 | 515 | 	to start two entries earlier */ | 
 | 516 |  | 
| Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 517 | 	dump_cifs_file_struct(file, "In fce "); | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 518 | 	if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) && | 
 | 519 | 	     is_dir_changed(file)) || | 
| Steve French | eafe870 | 2005-09-15 21:47:30 -0700 | [diff] [blame] | 520 | 	   (index_to_find < first_entry_in_buffer)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | 		/* close and restart search */ | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 522 | 		cFYI(1, ("search backing up - close and restart search")); | 
| Steve French | ddb4cbf | 2008-11-20 20:00:44 +0000 | [diff] [blame] | 523 | 		write_lock(&GlobalSMBSeslock); | 
| Steve French | 77c57ec | 2008-05-13 21:39:32 +0000 | [diff] [blame] | 524 | 		if (!cifsFile->srch_inf.endOfSearch && | 
 | 525 | 		    !cifsFile->invalidHandle) { | 
 | 526 | 			cifsFile->invalidHandle = true; | 
| Steve French | ddb4cbf | 2008-11-20 20:00:44 +0000 | [diff] [blame] | 527 | 			write_unlock(&GlobalSMBSeslock); | 
| Steve French | 77c57ec | 2008-05-13 21:39:32 +0000 | [diff] [blame] | 528 | 			CIFSFindClose(xid, pTcon, cifsFile->netfid); | 
| Steve French | ddb4cbf | 2008-11-20 20:00:44 +0000 | [diff] [blame] | 529 | 		} else | 
 | 530 | 			write_unlock(&GlobalSMBSeslock); | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 531 | 		if (cifsFile->srch_inf.ntwrk_buf_start) { | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 532 | 			cFYI(1, ("freeing SMB ff cache buf on search rewind")); | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 533 | 			if (cifsFile->srch_inf.smallBuf) | 
| Steve French | d47d7c1 | 2006-02-28 03:45:48 +0000 | [diff] [blame] | 534 | 				cifs_small_buf_release(cifsFile->srch_inf. | 
 | 535 | 						ntwrk_buf_start); | 
 | 536 | 			else | 
 | 537 | 				cifs_buf_release(cifsFile->srch_inf. | 
 | 538 | 						ntwrk_buf_start); | 
| Shirish Pargaonkar | 76c510a | 2008-07-24 14:48:33 +0000 | [diff] [blame] | 539 | 			cifsFile->srch_inf.ntwrk_buf_start = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | 		} | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 541 | 		rc = initiate_cifs_search(xid, file); | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 542 | 		if (rc) { | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 543 | 			cFYI(1, ("error %d reinitiating a search on rewind", | 
 | 544 | 				 rc)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | 			return rc; | 
 | 546 | 		} | 
| Jeff Layton | a364bc0 | 2008-10-21 14:42:13 +0000 | [diff] [blame] | 547 | 		cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | 	} | 
 | 549 |  | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 550 | 	while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) && | 
| Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 551 | 	      (rc == 0) && !cifsFile->srch_inf.endOfSearch) { | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 552 | 		cFYI(1, ("calling findnext2")); | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 553 | 		rc = CIFSFindNext(xid, pTcon, cifsFile->netfid, | 
| Steve French | dfb7533 | 2005-06-22 17:13:47 -0700 | [diff] [blame] | 554 | 				  &cifsFile->srch_inf); | 
| Jeff Layton | a364bc0 | 2008-10-21 14:42:13 +0000 | [diff] [blame] | 555 | 		cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile); | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 556 | 		if (rc) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | 			return -ENOENT; | 
 | 558 | 	} | 
| Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 559 | 	if (index_to_find < cifsFile->srch_inf.index_of_last_entry) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | 		/* we found the buffer that contains the entry */ | 
 | 561 | 		/* scan and find it */ | 
 | 562 | 		int i; | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 563 | 		char *current_entry; | 
 | 564 | 		char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | 			smbCalcSize((struct smb_hdr *) | 
 | 566 | 				cifsFile->srch_inf.ntwrk_buf_start); | 
| Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 567 |  | 
 | 568 | 		current_entry = cifsFile->srch_inf.srch_entries_start; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | 		first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry | 
 | 570 | 					- cifsFile->srch_inf.entries_in_buffer; | 
 | 571 | 		pos_in_buf = index_to_find - first_entry_in_buffer; | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 572 | 		cFYI(1, ("found entry - pos_in_buf %d", pos_in_buf)); | 
| Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 573 |  | 
| Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 574 | 		for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) { | 
| Steve French | dfb7533 | 2005-06-22 17:13:47 -0700 | [diff] [blame] | 575 | 			/* go entry by entry figuring out which is first */ | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 576 | 			current_entry = nxt_dir_entry(current_entry, end_of_smb, | 
| Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 577 | 						cifsFile->srch_inf.info_level); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | 		} | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 579 | 		if ((current_entry == NULL) && (i < pos_in_buf)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | 			/* BB fixme - check if we should flag this error */ | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 581 | 			cERROR(1, ("reached end of buf searching for pos in buf" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | 			  " %d index to find %lld rc %d", | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 583 | 			  pos_in_buf, index_to_find, rc)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | 		} | 
 | 585 | 		rc = 0; | 
 | 586 | 		*ppCurrentEntry = current_entry; | 
 | 587 | 	} else { | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 588 | 		cFYI(1, ("index not in buffer - could not findnext into it")); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | 		return 0; | 
 | 590 | 	} | 
 | 591 |  | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 592 | 	if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) { | 
 | 593 | 		cFYI(1, ("can not return entries pos_in_buf beyond last")); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | 		*num_to_ret = 0; | 
 | 595 | 	} else | 
 | 596 | 		*num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 |  | 
 | 598 | 	return rc; | 
 | 599 | } | 
 | 600 |  | 
 | 601 | /* inode num, inode type and filename returned */ | 
 | 602 | static int cifs_get_name_from_search_buf(struct qstr *pqst, | 
 | 603 | 	char *current_entry, __u16 level, unsigned int unicode, | 
| Jeff Layton | 1829579 | 2009-04-30 20:45:45 -0400 | [diff] [blame] | 604 | 	struct cifs_sb_info *cifs_sb, unsigned int max_len, __u64 *pinum) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | { | 
 | 606 | 	int rc = 0; | 
 | 607 | 	unsigned int len = 0; | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 608 | 	char *filename; | 
 | 609 | 	struct nls_table *nlt = cifs_sb->local_nls; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 |  | 
 | 611 | 	*pinum = 0; | 
 | 612 |  | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 613 | 	if (level == SMB_FIND_FILE_UNIX) { | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 614 | 		FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 |  | 
 | 616 | 		filename = &pFindData->FileName[0]; | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 617 | 		if (unicode) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | 			len = cifs_unicode_bytelen(filename); | 
 | 619 | 		} else { | 
 | 620 | 			/* BB should we make this strnlen of PATH_MAX? */ | 
 | 621 | 			len = strnlen(filename, PATH_MAX); | 
 | 622 | 		} | 
 | 623 |  | 
| Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 624 | 		*pinum = le64_to_cpu(pFindData->basic.UniqueId); | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 625 | 	} else if (level == SMB_FIND_FILE_DIRECTORY_INFO) { | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 626 | 		FILE_DIRECTORY_INFO *pFindData = | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | 			(FILE_DIRECTORY_INFO *)current_entry; | 
 | 628 | 		filename = &pFindData->FileName[0]; | 
 | 629 | 		len = le32_to_cpu(pFindData->FileNameLength); | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 630 | 	} else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) { | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 631 | 		FILE_FULL_DIRECTORY_INFO *pFindData = | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | 			(FILE_FULL_DIRECTORY_INFO *)current_entry; | 
 | 633 | 		filename = &pFindData->FileName[0]; | 
 | 634 | 		len = le32_to_cpu(pFindData->FileNameLength); | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 635 | 	} else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) { | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 636 | 		SEARCH_ID_FULL_DIR_INFO *pFindData = | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | 			(SEARCH_ID_FULL_DIR_INFO *)current_entry; | 
 | 638 | 		filename = &pFindData->FileName[0]; | 
 | 639 | 		len = le32_to_cpu(pFindData->FileNameLength); | 
| Steve French | 85a6dac | 2009-04-01 05:22:00 +0000 | [diff] [blame] | 640 | 		*pinum = le64_to_cpu(pFindData->UniqueId); | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 641 | 	} else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) { | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 642 | 		FILE_BOTH_DIRECTORY_INFO *pFindData = | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | 			(FILE_BOTH_DIRECTORY_INFO *)current_entry; | 
 | 644 | 		filename = &pFindData->FileName[0]; | 
 | 645 | 		len = le32_to_cpu(pFindData->FileNameLength); | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 646 | 	} else if (level == SMB_FIND_FILE_INFO_STANDARD) { | 
| Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 647 | 		FIND_FILE_STANDARD_INFO *pFindData = | 
| Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 648 | 			(FIND_FILE_STANDARD_INFO *)current_entry; | 
 | 649 | 		filename = &pFindData->FileName[0]; | 
 | 650 | 		/* one byte length, no name conversion */ | 
 | 651 | 		len = (unsigned int)pFindData->FileNameLength; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | 	} else { | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 653 | 		cFYI(1, ("Unknown findfirst level %d", level)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | 		return -EINVAL; | 
 | 655 | 	} | 
| Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 656 |  | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 657 | 	if (len > max_len) { | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 658 | 		cERROR(1, ("bad search response length %d past smb end", len)); | 
| Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 659 | 		return -EINVAL; | 
 | 660 | 	} | 
 | 661 |  | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 662 | 	if (unicode) { | 
| Jeff Layton | f588416 | 2009-04-30 07:18:00 -0400 | [diff] [blame] | 663 | 		pqst->len = cifs_from_ucs2((char *) pqst->name, | 
 | 664 | 					   (__le16 *) filename, | 
| Jeff Layton | 1829579 | 2009-04-30 20:45:45 -0400 | [diff] [blame] | 665 | 					   UNICODE_NAME_MAX, | 
 | 666 | 					   min(len, max_len), nlt, | 
| Jeff Layton | f588416 | 2009-04-30 07:18:00 -0400 | [diff] [blame] | 667 | 					   cifs_sb->mnt_cifs_flags & | 
 | 668 | 						CIFS_MOUNT_MAP_SPECIAL_CHR); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | 	} else { | 
 | 670 | 		pqst->name = filename; | 
 | 671 | 		pqst->len = len; | 
 | 672 | 	} | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 673 | 	pqst->hash = full_name_hash(pqst->name, pqst->len); | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 674 | /*	cFYI(1, ("filldir on %s",pqst->name));  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | 	return rc; | 
 | 676 | } | 
 | 677 |  | 
| Jeff Layton | 1829579 | 2009-04-30 20:45:45 -0400 | [diff] [blame] | 678 | static int cifs_filldir(char *pfindEntry, struct file *file, filldir_t filldir, | 
 | 679 | 			void *direntry, char *scratch_buf, unsigned int max_len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | { | 
 | 681 | 	int rc = 0; | 
 | 682 | 	struct qstr qstring; | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 683 | 	struct cifsFileInfo *pCifsF; | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 684 | 	u64    inum; | 
| Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 685 | 	ino_t  ino; | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 686 | 	struct super_block *sb; | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 687 | 	struct cifs_sb_info *cifs_sb; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | 	struct dentry *tmp_dentry; | 
| Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 689 | 	struct cifs_fattr fattr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 |  | 
 | 691 | 	/* get filename and len into qstring */ | 
 | 692 | 	/* get dentry */ | 
 | 693 | 	/* decide whether to create and populate ionde */ | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 694 | 	if ((direntry == NULL) || (file == NULL)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | 		return -EINVAL; | 
 | 696 |  | 
 | 697 | 	pCifsF = file->private_data; | 
| Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 698 |  | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 699 | 	if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | 		return -ENOENT; | 
 | 701 |  | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 702 | 	rc = cifs_entry_is_dot(pfindEntry, pCifsF); | 
| Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 703 | 	/* skip . and .. since we added them first */ | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 704 | 	if (rc != 0) | 
| Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 705 | 		return 0; | 
 | 706 |  | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 707 | 	sb = file->f_path.dentry->d_sb; | 
 | 708 | 	cifs_sb = CIFS_SB(sb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 |  | 
 | 710 | 	qstring.name = scratch_buf; | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 711 | 	rc = cifs_get_name_from_search_buf(&qstring, pfindEntry, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | 			pCifsF->srch_inf.info_level, | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 713 | 			pCifsF->srch_inf.unicode, cifs_sb, | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 714 | 			max_len, &inum /* returned */); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 |  | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 716 | 	if (rc) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | 		return rc; | 
 | 718 |  | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 719 | 	if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX) | 
| Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame] | 720 | 		cifs_unix_basic_to_fattr(&fattr, | 
 | 721 | 				 &((FILE_UNIX_INFO *) pfindEntry)->basic, | 
 | 722 | 				 cifs_sb); | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 723 | 	else if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) | 
 | 724 | 		cifs_std_info_to_fattr(&fattr, (FIND_FILE_STANDARD_INFO *) | 
 | 725 | 					pfindEntry, cifs_sb); | 
 | 726 | 	else | 
 | 727 | 		cifs_dir_info_to_fattr(&fattr, (FILE_DIRECTORY_INFO *) | 
 | 728 | 					pfindEntry, cifs_sb); | 
| Jeff Layton | 132ac7b | 2009-02-10 07:33:57 -0500 | [diff] [blame] | 729 |  | 
| Jeff Layton | ec06aed | 2009-11-06 14:18:29 -0500 | [diff] [blame] | 730 | 	if (inum && (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) { | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 731 | 		fattr.cf_uniqueid = inum; | 
| Jeff Layton | ec06aed | 2009-11-06 14:18:29 -0500 | [diff] [blame] | 732 | 	} else { | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 733 | 		fattr.cf_uniqueid = iunique(sb, ROOT_I); | 
| Jeff Layton | ec06aed | 2009-11-06 14:18:29 -0500 | [diff] [blame] | 734 | 		cifs_autodisable_serverino(cifs_sb); | 
 | 735 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 |  | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 737 | 	ino = cifs_uniqueid_to_ino_t(fattr.cf_uniqueid); | 
 | 738 | 	tmp_dentry = cifs_readdir_lookup(file->f_dentry, &qstring, &fattr); | 
| Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 739 |  | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 740 | 	rc = filldir(direntry, qstring.name, qstring.len, file->f_pos, | 
| Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 741 | 		     ino, fattr.cf_dtype); | 
 | 742 |  | 
 | 743 | 	/* | 
 | 744 | 	 * we can not return filldir errors to the caller since they are | 
 | 745 | 	 * "normal" when the stat blocksize is too small - we return remapped | 
 | 746 | 	 * error instead | 
 | 747 | 	 * | 
 | 748 | 	 * FIXME: This looks bogus. filldir returns -EOVERFLOW in the above | 
 | 749 | 	 * case already. Why should we be clobbering other errors from it? | 
 | 750 | 	 */ | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 751 | 	if (rc) { | 
 | 752 | 		cFYI(1, ("filldir rc = %d", rc)); | 
| Steve French | 7ca85ba | 2006-10-30 21:42:57 +0000 | [diff] [blame] | 753 | 		rc = -EOVERFLOW; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | 	dput(tmp_dentry); | 
 | 756 | 	return rc; | 
 | 757 | } | 
 | 758 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 |  | 
 | 760 | int cifs_readdir(struct file *file, void *direntry, filldir_t filldir) | 
 | 761 | { | 
 | 762 | 	int rc = 0; | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 763 | 	int xid, i; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | 	struct cifs_sb_info *cifs_sb; | 
 | 765 | 	struct cifsTconInfo *pTcon; | 
 | 766 | 	struct cifsFileInfo *cifsFile = NULL; | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 767 | 	char *current_entry; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | 	int num_to_fill = 0; | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 769 | 	char *tmp_buf = NULL; | 
| Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 770 | 	char *end_of_smb; | 
| Jeff Layton | 1829579 | 2009-04-30 20:45:45 -0400 | [diff] [blame] | 771 | 	unsigned int max_len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 |  | 
 | 773 | 	xid = GetXid(); | 
 | 774 |  | 
| Josef "Jeff" Sipek | e6a0029 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 775 | 	cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | 	pTcon = cifs_sb->tcon; | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 777 | 	if (pTcon == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | 		return -EINVAL; | 
 | 779 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | 	switch ((int) file->f_pos) { | 
 | 781 | 	case 0: | 
| Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 782 | 		if (filldir(direntry, ".", 1, file->f_pos, | 
| Josef "Jeff" Sipek | e6a0029 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 783 | 		     file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) { | 
| Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 784 | 			cERROR(1, ("Filldir for current dir failed")); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | 			rc = -ENOMEM; | 
 | 786 | 			break; | 
 | 787 | 		} | 
| Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 788 | 		file->f_pos++; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | 	case 1: | 
| Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 790 | 		if (filldir(direntry, "..", 2, file->f_pos, | 
| Josef "Jeff" Sipek | e6a0029 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 791 | 		     file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) { | 
| Steve French | 26a21b9 | 2006-05-31 18:05:34 +0000 | [diff] [blame] | 792 | 			cERROR(1, ("Filldir for parent dir failed")); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | 			rc = -ENOMEM; | 
 | 794 | 			break; | 
 | 795 | 		} | 
| Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 796 | 		file->f_pos++; | 
 | 797 | 	default: | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 798 | 		/* 1) If search is active, | 
 | 799 | 			is in current search buffer? | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | 			if it before then restart search | 
 | 801 | 			if after then keep searching till find it */ | 
 | 802 |  | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 803 | 		if (file->private_data == NULL) { | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 804 | 			rc = initiate_cifs_search(xid, file); | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 805 | 			cFYI(1, ("initiate cifs search rc %d", rc)); | 
 | 806 | 			if (rc) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | 				FreeXid(xid); | 
 | 808 | 				return rc; | 
 | 809 | 			} | 
 | 810 | 		} | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 811 | 		if (file->private_data == NULL) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | 			rc = -EINVAL; | 
 | 813 | 			FreeXid(xid); | 
 | 814 | 			return rc; | 
 | 815 | 		} | 
 | 816 | 		cifsFile = file->private_data; | 
 | 817 | 		if (cifsFile->srch_inf.endOfSearch) { | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 818 | 			if (cifsFile->srch_inf.emptyDir) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | 				cFYI(1, ("End of search, empty dir")); | 
 | 820 | 				rc = 0; | 
 | 821 | 				break; | 
 | 822 | 			} | 
 | 823 | 		} /* else { | 
| Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 824 | 			cifsFile->invalidHandle = true; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | 			CIFSFindClose(xid, pTcon, cifsFile->netfid); | 
| Steve French | aaa9bbe | 2008-05-23 17:38:32 +0000 | [diff] [blame] | 826 | 		} */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 |  | 
| Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 828 | 		rc = find_cifs_entry(xid, pTcon, file, | 
 | 829 | 				¤t_entry, &num_to_fill); | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 830 | 		if (rc) { | 
 | 831 | 			cFYI(1, ("fce error %d", rc)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | 			goto rddir2_exit; | 
 | 833 | 		} else if (current_entry != NULL) { | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 834 | 			cFYI(1, ("entry %lld found", file->f_pos)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | 		} else { | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 836 | 			cFYI(1, ("could not find entry")); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | 			goto rddir2_exit; | 
 | 838 | 		} | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 839 | 		cFYI(1, ("loop through %d times filling dir for net buf %p", | 
 | 840 | 			num_to_fill, cifsFile->srch_inf.ntwrk_buf_start)); | 
| Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 841 | 		max_len = smbCalcSize((struct smb_hdr *) | 
 | 842 | 				cifsFile->srch_inf.ntwrk_buf_start); | 
 | 843 | 		end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len; | 
 | 844 |  | 
| Jeff Layton | f588416 | 2009-04-30 07:18:00 -0400 | [diff] [blame] | 845 | 		tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL); | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 846 | 		for (i = 0; (i < num_to_fill) && (rc == 0); i++) { | 
 | 847 | 			if (current_entry == NULL) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | 				/* evaluate whether this case is an error */ | 
| Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 849 | 				cERROR(1, ("past SMB end,  num to fill %d i %d", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | 					  num_to_fill, i)); | 
 | 851 | 				break; | 
 | 852 | 			} | 
| Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 853 | 			/* if buggy server returns . and .. late do | 
 | 854 | 			we want to check for that here? */ | 
| Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 855 | 			rc = cifs_filldir(current_entry, file, | 
 | 856 | 					filldir, direntry, tmp_buf, max_len); | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 857 | 			if (rc == -EOVERFLOW) { | 
| Steve French | 7ca85ba | 2006-10-30 21:42:57 +0000 | [diff] [blame] | 858 | 				rc = 0; | 
 | 859 | 				break; | 
 | 860 | 			} | 
 | 861 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | 			file->f_pos++; | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 863 | 			if (file->f_pos == | 
| Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 864 | 				cifsFile->srch_inf.index_of_last_entry) { | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 865 | 				cFYI(1, ("last entry in buf at pos %lld %s", | 
 | 866 | 					file->f_pos, tmp_buf)); | 
 | 867 | 				cifs_save_resume_key(current_entry, cifsFile); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | 				break; | 
| Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 869 | 			} else | 
 | 870 | 				current_entry = | 
| Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 871 | 					nxt_dir_entry(current_entry, end_of_smb, | 
 | 872 | 						cifsFile->srch_inf.info_level); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | 		} | 
 | 874 | 		kfree(tmp_buf); | 
 | 875 | 		break; | 
 | 876 | 	} /* end switch */ | 
 | 877 |  | 
 | 878 | rddir2_exit: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | 	FreeXid(xid); | 
 | 880 | 	return rc; | 
 | 881 | } |