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