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 | 132ac7b | 2009-02-10 07:33:57 -0500 | [diff] [blame] | 66 | /* Returns 1 if new inode created, 2 if both dentry and inode were */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | /* Might check in the future if inode number changed so we can rehash inode */ |
Jeff Layton | 132ac7b | 2009-02-10 07:33:57 -0500 | [diff] [blame] | 68 | static int |
| 69 | construct_dentry(struct qstr *qstring, struct file *file, |
| 70 | struct inode **ptmp_inode, struct dentry **pnew_dentry, |
Jeff Layton | 950ec52 | 2009-02-11 08:08:26 -0500 | [diff] [blame] | 71 | __u64 *inum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
Jeff Layton | 132ac7b | 2009-02-10 07:33:57 -0500 | [diff] [blame] | 73 | struct dentry *tmp_dentry = NULL; |
| 74 | struct super_block *sb = file->f_path.dentry->d_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | int rc = 0; |
| 76 | |
Steve French | 966ca92 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 77 | cFYI(1, ("For %s", qstring->name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | qstring->hash = full_name_hash(qstring->name, qstring->len); |
Josef "Jeff" Sipek | e6a0029 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 80 | tmp_dentry = d_lookup(file->f_path.dentry, qstring); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | if (tmp_dentry) { |
Jeff Layton | 132ac7b | 2009-02-10 07:33:57 -0500 | [diff] [blame] | 82 | /* BB: overwrite old name? i.e. tmp_dentry->d_name and |
| 83 | * tmp_dentry->d_name.len?? |
| 84 | */ |
Steve French | 6dc0f87 | 2007-07-06 23:13:06 +0000 | [diff] [blame] | 85 | cFYI(0, ("existing dentry with inode 0x%p", |
| 86 | tmp_dentry->d_inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | *ptmp_inode = tmp_dentry->d_inode; |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 88 | if (*ptmp_inode == NULL) { |
Jeff Layton | 132ac7b | 2009-02-10 07:33:57 -0500 | [diff] [blame] | 89 | *ptmp_inode = cifs_new_inode(sb, inum); |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 90 | if (*ptmp_inode == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | return rc; |
| 92 | rc = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | } else { |
Josef "Jeff" Sipek | e6a0029 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 95 | tmp_dentry = d_alloc(file->f_path.dentry, qstring); |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 96 | if (tmp_dentry == NULL) { |
Steve French | 6dc0f87 | 2007-07-06 23:13:06 +0000 | [diff] [blame] | 97 | cERROR(1, ("Failed allocating dentry")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | *ptmp_inode = NULL; |
| 99 | return rc; |
| 100 | } |
| 101 | |
Jeff Layton | 132ac7b | 2009-02-10 07:33:57 -0500 | [diff] [blame] | 102 | if (CIFS_SB(sb)->tcon->nocase) |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 103 | tmp_dentry->d_op = &cifs_ci_dentry_ops; |
| 104 | else |
| 105 | tmp_dentry->d_op = &cifs_dentry_ops; |
Jeff Layton | 132ac7b | 2009-02-10 07:33:57 -0500 | [diff] [blame] | 106 | |
| 107 | *ptmp_inode = cifs_new_inode(sb, inum); |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 108 | if (*ptmp_inode == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | return rc; |
Steve French | b835beb | 2006-09-06 22:02:22 +0000 | [diff] [blame] | 110 | rc = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | tmp_dentry->d_time = jiffies; |
| 114 | *pnew_dentry = tmp_dentry; |
| 115 | return rc; |
| 116 | } |
| 117 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 118 | static void AdjustForTZ(struct cifsTconInfo *tcon, struct inode *inode) |
Steve French | 533f90a | 2006-10-12 00:02:32 +0000 | [diff] [blame] | 119 | { |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 120 | if ((tcon) && (tcon->ses) && (tcon->ses->server)) { |
Steve French | ddae957 | 2006-10-12 01:23:29 +0000 | [diff] [blame] | 121 | inode->i_ctime.tv_sec += tcon->ses->server->timeAdj; |
| 122 | inode->i_mtime.tv_sec += tcon->ses->server->timeAdj; |
| 123 | inode->i_atime.tv_sec += tcon->ses->server->timeAdj; |
Steve French | 533f90a | 2006-10-12 00:02:32 +0000 | [diff] [blame] | 124 | } |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 129 | static void fill_in_inode(struct inode *tmp_inode, int new_buf_type, |
Steve French | 2c2130e | 2007-10-12 19:10:28 +0000 | [diff] [blame] | 130 | char *buf, unsigned int *pobject_type, int isNewInode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
Steve French | 966ca92 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 132 | loff_t local_size; |
| 133 | struct timespec local_mtime; |
| 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode); |
| 136 | struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb); |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 137 | __u32 attr; |
| 138 | __u64 allocation_size; |
| 139 | __u64 end_of_file; |
Jeff Layton | 4468eb3 | 2008-05-22 09:31:40 -0400 | [diff] [blame] | 140 | umode_t default_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Steve French | 966ca92 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 142 | /* save mtime and size */ |
| 143 | local_mtime = tmp_inode->i_mtime; |
| 144 | local_size = tmp_inode->i_size; |
| 145 | |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 146 | if (new_buf_type) { |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 147 | FILE_DIRECTORY_INFO *pfindData = (FILE_DIRECTORY_INFO *)buf; |
| 148 | |
| 149 | attr = le32_to_cpu(pfindData->ExtFileAttributes); |
| 150 | allocation_size = le64_to_cpu(pfindData->AllocationSize); |
| 151 | end_of_file = le64_to_cpu(pfindData->EndOfFile); |
| 152 | tmp_inode->i_atime = |
| 153 | cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime)); |
| 154 | tmp_inode->i_mtime = |
| 155 | cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime)); |
| 156 | tmp_inode->i_ctime = |
| 157 | cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime)); |
| 158 | } else { /* legacy, OS2 and DOS style */ |
Steve French | 1bd5bbc | 2006-09-28 03:35:57 +0000 | [diff] [blame] | 159 | /* struct timespec ts;*/ |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 160 | FIND_FILE_STANDARD_INFO *pfindData = |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 161 | (FIND_FILE_STANDARD_INFO *)buf; |
| 162 | |
Steve French | 268f3be | 2006-10-06 21:47:09 +0000 | [diff] [blame] | 163 | tmp_inode->i_mtime = cnvrtDosUnixTm( |
Steve French | 1bd5bbc | 2006-09-28 03:35:57 +0000 | [diff] [blame] | 164 | le16_to_cpu(pfindData->LastWriteDate), |
Steve French | 268f3be | 2006-10-06 21:47:09 +0000 | [diff] [blame] | 165 | le16_to_cpu(pfindData->LastWriteTime)); |
| 166 | tmp_inode->i_atime = cnvrtDosUnixTm( |
| 167 | le16_to_cpu(pfindData->LastAccessDate), |
| 168 | le16_to_cpu(pfindData->LastAccessTime)); |
Steve French | a850790 | 2007-02-06 20:43:30 +0000 | [diff] [blame] | 169 | tmp_inode->i_ctime = cnvrtDosUnixTm( |
| 170 | le16_to_cpu(pfindData->LastWriteDate), |
| 171 | le16_to_cpu(pfindData->LastWriteTime)); |
Steve French | 533f90a | 2006-10-12 00:02:32 +0000 | [diff] [blame] | 172 | AdjustForTZ(cifs_sb->tcon, tmp_inode); |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 173 | attr = le16_to_cpu(pfindData->Attributes); |
| 174 | allocation_size = le32_to_cpu(pfindData->AllocationSize); |
| 175 | end_of_file = le32_to_cpu(pfindData->DataSize); |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | /* Linux can not store file creation time unfortunately so ignore it */ |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 179 | |
| 180 | cifsInfo->cifsAttrs = attr; |
Steve French | a6f8de3 | 2007-11-08 23:10:32 +0000 | [diff] [blame] | 181 | #ifdef CONFIG_CIFS_EXPERIMENTAL |
| 182 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) { |
| 183 | /* get more accurate mode via ACL - so force inode refresh */ |
| 184 | cifsInfo->time = 0; |
| 185 | } else |
| 186 | #endif /* CONFIG_CIFS_EXPERIMENTAL */ |
| 187 | cifsInfo->time = jiffies; |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | /* treat dos attribute of read-only as read-only mode bit e.g. 555? */ |
| 190 | /* 2767 perms - indicate mandatory locking */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 191 | /* BB fill in uid and gid here? with help from winbind? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | or retrieve from NTFS stream extended attribute */ |
| 193 | if (atomic_read(&cifsInfo->inUse) == 0) { |
| 194 | tmp_inode->i_uid = cifs_sb->mnt_uid; |
| 195 | tmp_inode->i_gid = cifs_sb->mnt_gid; |
Jeff Layton | 4468eb3 | 2008-05-22 09:31:40 -0400 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | if (attr & ATTR_DIRECTORY) |
| 199 | default_mode = cifs_sb->mnt_dir_mode; |
| 200 | else |
| 201 | default_mode = cifs_sb->mnt_file_mode; |
| 202 | |
| 203 | /* set initial permissions */ |
| 204 | if ((atomic_read(&cifsInfo->inUse) == 0) || |
| 205 | (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0) |
| 206 | tmp_inode->i_mode = default_mode; |
| 207 | else { |
| 208 | /* just reenable write bits if !ATTR_READONLY */ |
| 209 | if ((tmp_inode->i_mode & S_IWUGO) == 0 && |
| 210 | (attr & ATTR_READONLY) == 0) |
| 211 | tmp_inode->i_mode |= (S_IWUGO & default_mode); |
| 212 | |
Steve French | 3020a1f | 2005-11-18 11:31:10 -0800 | [diff] [blame] | 213 | tmp_inode->i_mode &= ~S_IFMT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Jeff Layton | 4468eb3 | 2008-05-22 09:31:40 -0400 | [diff] [blame] | 216 | /* clear write bits if ATTR_READONLY is set */ |
| 217 | if (attr & ATTR_READONLY) |
| 218 | tmp_inode->i_mode &= ~S_IWUGO; |
| 219 | |
| 220 | /* set inode type */ |
| 221 | if ((attr & ATTR_SYSTEM) && |
| 222 | (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) { |
Steve French | 3020a1f | 2005-11-18 11:31:10 -0800 | [diff] [blame] | 223 | if (end_of_file == 0) { |
Steve French | 3020a1f | 2005-11-18 11:31:10 -0800 | [diff] [blame] | 224 | tmp_inode->i_mode |= S_IFIFO; |
Jeff Layton | 4468eb3 | 2008-05-22 09:31:40 -0400 | [diff] [blame] | 225 | *pobject_type = DT_FIFO; |
Steve French | 3020a1f | 2005-11-18 11:31:10 -0800 | [diff] [blame] | 226 | } else { |
Jeff Layton | 4468eb3 | 2008-05-22 09:31:40 -0400 | [diff] [blame] | 227 | /* |
| 228 | * trying to get the type can be slow, so just call |
| 229 | * this a regular file for now, and mark for reval |
| 230 | */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 231 | tmp_inode->i_mode |= S_IFREG; |
Jeff Layton | 4468eb3 | 2008-05-22 09:31:40 -0400 | [diff] [blame] | 232 | *pobject_type = DT_REG; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 233 | cifsInfo->time = 0; |
Steve French | 3020a1f | 2005-11-18 11:31:10 -0800 | [diff] [blame] | 234 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } else { |
Jeff Layton | 4468eb3 | 2008-05-22 09:31:40 -0400 | [diff] [blame] | 236 | if (attr & ATTR_DIRECTORY) { |
| 237 | tmp_inode->i_mode |= S_IFDIR; |
| 238 | *pobject_type = DT_DIR; |
| 239 | } else { |
| 240 | tmp_inode->i_mode |= S_IFREG; |
| 241 | *pobject_type = DT_REG; |
| 242 | } |
| 243 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
| 245 | /* can not fill in nlink here as in qpathinfo version and Unx search */ |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 246 | if (atomic_read(&cifsInfo->inUse) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | atomic_set(&cifsInfo->inUse, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
Jeff Layton | fbec9ab | 2009-04-03 13:44:00 -0400 | [diff] [blame] | 249 | cifsInfo->server_eof = end_of_file; |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 250 | spin_lock(&tmp_inode->i_lock); |
Steve French | 7ba5263 | 2007-02-08 18:14:13 +0000 | [diff] [blame] | 251 | if (is_size_safe_to_change(cifsInfo, end_of_file)) { |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 252 | /* can not safely change the file size here if the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | client is writing to it due to potential races */ |
| 254 | i_size_write(tmp_inode, end_of_file); |
| 255 | |
| 256 | /* 512 bytes (2**9) is the fake blocksize that must be used */ |
| 257 | /* for this calculation, even though the reported blocksize is larger */ |
| 258 | tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9; |
| 259 | } |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 260 | spin_unlock(&tmp_inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
| 262 | if (allocation_size < end_of_file) |
| 263 | cFYI(1, ("May be sparse file, allocation less than file size")); |
Theodore Ts'o | ba52de1 | 2006-09-27 01:50:49 -0700 | [diff] [blame] | 264 | cFYI(1, ("File Size %ld and blocks %llu", |
Andrew Morton | 5515eff | 2006-03-26 01:37:53 -0800 | [diff] [blame] | 265 | (unsigned long)tmp_inode->i_size, |
Theodore Ts'o | ba52de1 | 2006-09-27 01:50:49 -0700 | [diff] [blame] | 266 | (unsigned long long)tmp_inode->i_blocks)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | if (S_ISREG(tmp_inode->i_mode)) { |
Steve French | 966ca92 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 268 | cFYI(1, ("File inode")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | tmp_inode->i_op = &cifs_file_inode_ops; |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 270 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) { |
| 271 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) |
Steve French | 8b94bcb | 2005-11-11 11:41:00 -0800 | [diff] [blame] | 272 | tmp_inode->i_fop = &cifs_file_direct_nobrl_ops; |
| 273 | else |
| 274 | tmp_inode->i_fop = &cifs_file_direct_ops; |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 275 | } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) |
Steve French | 8b94bcb | 2005-11-11 11:41:00 -0800 | [diff] [blame] | 276 | tmp_inode->i_fop = &cifs_file_nobrl_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | else |
| 278 | tmp_inode->i_fop = &cifs_file_ops; |
Steve French | f3f6ec4 | 2006-01-08 20:12:58 -0800 | [diff] [blame] | 279 | |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 280 | if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) && |
Steve French | bfa0d75 | 2005-08-31 21:50:37 -0700 | [diff] [blame] | 281 | (cifs_sb->tcon->ses->server->maxBuf < |
Dave Kleikamp | 273d81d | 2006-06-01 19:41:23 +0000 | [diff] [blame] | 282 | PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)) |
| 283 | tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf; |
| 284 | else |
| 285 | tmp_inode->i_data.a_ops = &cifs_addr_ops; |
| 286 | |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 287 | if (isNewInode) |
Steve French | dfb7533 | 2005-06-22 17:13:47 -0700 | [diff] [blame] | 288 | return; /* No sense invalidating pages for new inode |
| 289 | since have not started caching readahead file |
| 290 | data yet */ |
Steve French | 966ca92 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 291 | |
| 292 | if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) && |
| 293 | (local_size == tmp_inode->i_size)) { |
| 294 | cFYI(1, ("inode exists but unchanged")); |
| 295 | } else { |
| 296 | /* file may have changed on server */ |
| 297 | cFYI(1, ("invalidate inode, readdir detected change")); |
| 298 | invalidate_remote_inode(tmp_inode); |
| 299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } else if (S_ISDIR(tmp_inode->i_mode)) { |
Steve French | 966ca92 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 301 | cFYI(1, ("Directory inode")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | tmp_inode->i_op = &cifs_dir_inode_ops; |
| 303 | tmp_inode->i_fop = &cifs_dir_ops; |
| 304 | } else if (S_ISLNK(tmp_inode->i_mode)) { |
Steve French | 966ca92 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 305 | cFYI(1, ("Symbolic Link inode")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | tmp_inode->i_op = &cifs_symlink_inode_ops; |
| 307 | } else { |
Steve French | 966ca92 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 308 | cFYI(1, ("Init special inode")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | init_special_inode(tmp_inode, tmp_inode->i_mode, |
| 310 | tmp_inode->i_rdev); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | static void unix_fill_in_inode(struct inode *tmp_inode, |
Steve French | 2c2130e | 2007-10-12 19:10:28 +0000 | [diff] [blame] | 315 | FILE_UNIX_INFO *pfindData, unsigned int *pobject_type, int isNewInode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | { |
Steve French | 966ca92 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 317 | loff_t local_size; |
| 318 | struct timespec local_mtime; |
| 319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode); |
| 321 | struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb); |
| 322 | |
| 323 | __u32 type = le32_to_cpu(pfindData->Type); |
| 324 | __u64 num_of_bytes = le64_to_cpu(pfindData->NumOfBytes); |
| 325 | __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile); |
| 326 | cifsInfo->time = jiffies; |
| 327 | atomic_inc(&cifsInfo->inUse); |
| 328 | |
Steve French | 966ca92 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 329 | /* save mtime and size */ |
| 330 | local_mtime = tmp_inode->i_mtime; |
| 331 | local_size = tmp_inode->i_size; |
| 332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | tmp_inode->i_atime = |
| 334 | cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime)); |
| 335 | tmp_inode->i_mtime = |
| 336 | cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastModificationTime)); |
| 337 | tmp_inode->i_ctime = |
| 338 | cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastStatusChange)); |
| 339 | |
| 340 | tmp_inode->i_mode = le64_to_cpu(pfindData->Permissions); |
Steve French | 3020a1f | 2005-11-18 11:31:10 -0800 | [diff] [blame] | 341 | /* since we set the inode type below we need to mask off type |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 342 | to avoid strange results if bits above were corrupt */ |
| 343 | tmp_inode->i_mode &= ~S_IFMT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | if (type == UNIX_FILE) { |
| 345 | *pobject_type = DT_REG; |
| 346 | tmp_inode->i_mode |= S_IFREG; |
| 347 | } else if (type == UNIX_SYMLINK) { |
| 348 | *pobject_type = DT_LNK; |
| 349 | tmp_inode->i_mode |= S_IFLNK; |
| 350 | } else if (type == UNIX_DIR) { |
| 351 | *pobject_type = DT_DIR; |
| 352 | tmp_inode->i_mode |= S_IFDIR; |
| 353 | } else if (type == UNIX_CHARDEV) { |
| 354 | *pobject_type = DT_CHR; |
| 355 | tmp_inode->i_mode |= S_IFCHR; |
| 356 | tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor), |
| 357 | le64_to_cpu(pfindData->DevMinor) & MINORMASK); |
| 358 | } else if (type == UNIX_BLOCKDEV) { |
| 359 | *pobject_type = DT_BLK; |
| 360 | tmp_inode->i_mode |= S_IFBLK; |
| 361 | tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor), |
| 362 | le64_to_cpu(pfindData->DevMinor) & MINORMASK); |
| 363 | } else if (type == UNIX_FIFO) { |
| 364 | *pobject_type = DT_FIFO; |
| 365 | tmp_inode->i_mode |= S_IFIFO; |
| 366 | } else if (type == UNIX_SOCKET) { |
| 367 | *pobject_type = DT_SOCK; |
| 368 | tmp_inode->i_mode |= S_IFSOCK; |
Steve French | 3020a1f | 2005-11-18 11:31:10 -0800 | [diff] [blame] | 369 | } else { |
| 370 | /* safest to just call it a file */ |
| 371 | *pobject_type = DT_REG; |
| 372 | tmp_inode->i_mode |= S_IFREG; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 373 | cFYI(1, ("unknown inode type %d", type)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 376 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID) |
| 377 | tmp_inode->i_uid = cifs_sb->mnt_uid; |
| 378 | else |
| 379 | tmp_inode->i_uid = le64_to_cpu(pfindData->Uid); |
| 380 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID) |
| 381 | tmp_inode->i_gid = cifs_sb->mnt_gid; |
| 382 | else |
| 383 | tmp_inode->i_gid = le64_to_cpu(pfindData->Gid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks); |
| 385 | |
Jeff Layton | fbec9ab | 2009-04-03 13:44:00 -0400 | [diff] [blame] | 386 | cifsInfo->server_eof = end_of_file; |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 387 | spin_lock(&tmp_inode->i_lock); |
Steve French | 7ba5263 | 2007-02-08 18:14:13 +0000 | [diff] [blame] | 388 | if (is_size_safe_to_change(cifsInfo, end_of_file)) { |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 389 | /* can not safely change the file size here if the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | client is writing to it due to potential races */ |
Steve French | 7ba5263 | 2007-02-08 18:14:13 +0000 | [diff] [blame] | 391 | i_size_write(tmp_inode, end_of_file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
| 393 | /* 512 bytes (2**9) is the fake blocksize that must be used */ |
| 394 | /* for this calculation, not the real blocksize */ |
| 395 | tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9; |
| 396 | } |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 397 | spin_unlock(&tmp_inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
| 399 | if (S_ISREG(tmp_inode->i_mode)) { |
| 400 | cFYI(1, ("File inode")); |
| 401 | tmp_inode->i_op = &cifs_file_inode_ops; |
Steve French | f3f6ec4 | 2006-01-08 20:12:58 -0800 | [diff] [blame] | 402 | |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 403 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) { |
| 404 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) |
Steve French | f3f6ec4 | 2006-01-08 20:12:58 -0800 | [diff] [blame] | 405 | tmp_inode->i_fop = &cifs_file_direct_nobrl_ops; |
| 406 | else |
| 407 | tmp_inode->i_fop = &cifs_file_direct_ops; |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 408 | } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) |
Steve French | f3f6ec4 | 2006-01-08 20:12:58 -0800 | [diff] [blame] | 409 | tmp_inode->i_fop = &cifs_file_nobrl_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | else |
| 411 | tmp_inode->i_fop = &cifs_file_ops; |
Steve French | f3f6ec4 | 2006-01-08 20:12:58 -0800 | [diff] [blame] | 412 | |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 413 | if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) && |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 414 | (cifs_sb->tcon->ses->server->maxBuf < |
Dave Kleikamp | 273d81d | 2006-06-01 19:41:23 +0000 | [diff] [blame] | 415 | PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)) |
| 416 | tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf; |
| 417 | else |
| 418 | tmp_inode->i_data.a_ops = &cifs_addr_ops; |
Steve French | 966ca92 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 419 | |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 420 | if (isNewInode) |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 421 | return; /* No sense invalidating pages for new inode |
| 422 | since we have not started caching readahead |
| 423 | file data for it yet */ |
Steve French | 966ca92 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 424 | |
| 425 | if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) && |
| 426 | (local_size == tmp_inode->i_size)) { |
| 427 | cFYI(1, ("inode exists but unchanged")); |
| 428 | } else { |
| 429 | /* file may have changed on server */ |
| 430 | cFYI(1, ("invalidate inode, readdir detected change")); |
| 431 | invalidate_remote_inode(tmp_inode); |
| 432 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } else if (S_ISDIR(tmp_inode->i_mode)) { |
| 434 | cFYI(1, ("Directory inode")); |
| 435 | tmp_inode->i_op = &cifs_dir_inode_ops; |
| 436 | tmp_inode->i_fop = &cifs_dir_ops; |
| 437 | } else if (S_ISLNK(tmp_inode->i_mode)) { |
| 438 | cFYI(1, ("Symbolic Link inode")); |
| 439 | tmp_inode->i_op = &cifs_symlink_inode_ops; |
| 440 | /* tmp_inode->i_fop = *//* do not need to set to anything */ |
| 441 | } else { |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 442 | cFYI(1, ("Special inode")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | init_special_inode(tmp_inode, tmp_inode->i_mode, |
| 444 | tmp_inode->i_rdev); |
| 445 | } |
| 446 | } |
| 447 | |
Steve French | 0e0d2cf | 2009-05-01 05:27:32 +0000 | [diff] [blame^] | 448 | /* BB eventually need to add the following helper function to |
| 449 | resolve NT_STATUS_STOPPED_ON_SYMLINK return code when |
| 450 | we try to do FindFirst on (NTFS) directory symlinks */ |
| 451 | /* |
| 452 | int get_symlink_reparse_path(char *full_path, struct cifs_sb_info *cifs_sb, |
| 453 | int xid) |
| 454 | { |
| 455 | __u16 fid; |
| 456 | int len; |
| 457 | int oplock = 0; |
| 458 | int rc; |
| 459 | struct cifsTconInfo *ptcon = cifs_sb->tcon; |
| 460 | char *tmpbuffer; |
| 461 | |
| 462 | rc = CIFSSMBOpen(xid, ptcon, full_path, FILE_OPEN, GENERIC_READ, |
| 463 | OPEN_REPARSE_POINT, &fid, &oplock, NULL, |
| 464 | cifs_sb->local_nls, |
| 465 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 466 | if (!rc) { |
| 467 | tmpbuffer = kmalloc(maxpath); |
| 468 | rc = CIFSSMBQueryReparseLinkInfo(xid, ptcon, full_path, |
| 469 | tmpbuffer, |
| 470 | maxpath -1, |
| 471 | fid, |
| 472 | cifs_sb->local_nls); |
| 473 | if (CIFSSMBClose(xid, ptcon, fid)) { |
| 474 | cFYI(1, ("Error closing temporary reparsepoint open)")); |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | */ |
| 479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | static int initiate_cifs_search(const int xid, struct file *file) |
| 481 | { |
| 482 | int rc = 0; |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 483 | char *full_path; |
| 484 | struct cifsFileInfo *cifsFile; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | struct cifs_sb_info *cifs_sb; |
| 486 | struct cifsTconInfo *pTcon; |
| 487 | |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 488 | if (file->private_data == NULL) { |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 489 | file->private_data = |
| 490 | kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 493 | if (file->private_data == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | cifsFile = file->private_data; |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 496 | cifsFile->invalidHandle = true; |
| 497 | cifsFile->srch_inf.endOfSearch = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | |
Josef "Jeff" Sipek | e6a0029 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 499 | cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 500 | if (cifs_sb == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | return -EINVAL; |
| 502 | |
| 503 | pTcon = cifs_sb->tcon; |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 504 | if (pTcon == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | return -EINVAL; |
| 506 | |
Josef "Jeff" Sipek | e6a0029 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 507 | full_path = build_path_from_dentry(file->f_path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 509 | if (full_path == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | |
Steve French | 966ca92 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 512 | 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] | 513 | |
Steve French | 75cf6bd | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 514 | ffirst_retry: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | /* test for Unix extensions */ |
Steve French | c18c842 | 2007-07-18 23:21:09 +0000 | [diff] [blame] | 516 | /* but now check for them on the share/mount not on the SMB session */ |
| 517 | /* if (pTcon->ses->capabilities & CAP_UNIX) { */ |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 518 | if (pTcon->unix_ext) |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 519 | cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX; |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 520 | else if ((pTcon->ses->capabilities & |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 521 | (CAP_NT_SMBS | CAP_NT_FIND)) == 0) { |
| 522 | cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) { |
| 524 | cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO; |
| 525 | } else /* not srvinos - BB fixme add check for backlevel? */ { |
| 526 | cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO; |
| 527 | } |
| 528 | |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 529 | rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 530 | &cifsFile->netfid, &cifsFile->srch_inf, |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 531 | cifs_sb->mnt_cifs_flags & |
Steve French | eafe870 | 2005-09-15 21:47:30 -0700 | [diff] [blame] | 532 | CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb)); |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 533 | if (rc == 0) |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 534 | cifsFile->invalidHandle = false; |
Steve French | 0e0d2cf | 2009-05-01 05:27:32 +0000 | [diff] [blame^] | 535 | /* BB add following call to handle readdir on new NTFS symlink errors |
| 536 | else if STATUS_STOPPED_ON_SYMLINK |
| 537 | call get_symlink_reparse_path and retry with new path */ |
| 538 | else if ((rc == -EOPNOTSUPP) && |
Steve French | 75cf6bd | 2005-04-28 22:41:04 -0700 | [diff] [blame] | 539 | (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) { |
| 540 | cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM; |
| 541 | goto ffirst_retry; |
| 542 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | kfree(full_path); |
| 544 | return rc; |
| 545 | } |
| 546 | |
| 547 | /* return length of unicode string in bytes */ |
| 548 | static int cifs_unicode_bytelen(char *str) |
| 549 | { |
| 550 | int len; |
Steve French | 63d2583 | 2007-11-05 21:46:10 +0000 | [diff] [blame] | 551 | __le16 *ustr = (__le16 *)str; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 553 | for (len = 0; len <= PATH_MAX; len++) { |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 554 | if (ustr[len] == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | return len << 1; |
| 556 | } |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 557 | cFYI(1, ("Unicode string longer than PATH_MAX found")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | return len << 1; |
| 559 | } |
| 560 | |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 561 | 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] | 562 | { |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 563 | char *new_entry; |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 564 | FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 566 | if (level == SMB_FIND_FILE_INFO_STANDARD) { |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 567 | FIND_FILE_STANDARD_INFO *pfData; |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 568 | pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo; |
| 569 | |
| 570 | new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) + |
| 571 | pfData->FileNameLength; |
| 572 | } else |
| 573 | new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 574 | cFYI(1, ("new entry %p old entry %p", new_entry, old_entry)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | /* validate that new_entry is not past end of SMB */ |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 576 | if (new_entry >= end_of_smb) { |
Steve French | 09d1db5 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 577 | cERROR(1, |
| 578 | ("search entry %p began after end of SMB %p old entry %p", |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 579 | new_entry, end_of_smb, old_entry)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | return NULL; |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 581 | } else if (((level == SMB_FIND_FILE_INFO_STANDARD) && |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 582 | (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb)) |
| 583 | || ((level != SMB_FIND_FILE_INFO_STANDARD) && |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 584 | (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) { |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 585 | cERROR(1, ("search entry %p extends after end of SMB %p", |
Steve French | 09d1db5 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 586 | new_entry, end_of_smb)); |
| 587 | return NULL; |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 588 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | return new_entry; |
| 590 | |
| 591 | } |
| 592 | |
| 593 | #define UNICODE_DOT cpu_to_le16(0x2e) |
| 594 | |
| 595 | /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */ |
| 596 | static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile) |
| 597 | { |
| 598 | int rc = 0; |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 599 | char *filename = NULL; |
| 600 | int len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 602 | if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) { |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 603 | FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | filename = &pFindData->FileName[0]; |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 605 | if (cfile->srch_inf.unicode) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | len = cifs_unicode_bytelen(filename); |
| 607 | } else { |
| 608 | /* BB should we make this strnlen of PATH_MAX? */ |
| 609 | len = strnlen(filename, 5); |
| 610 | } |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 611 | } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) { |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 612 | FILE_DIRECTORY_INFO *pFindData = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | (FILE_DIRECTORY_INFO *)current_entry; |
| 614 | filename = &pFindData->FileName[0]; |
| 615 | len = le32_to_cpu(pFindData->FileNameLength); |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 616 | } else if (cfile->srch_inf.info_level == |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 617 | SMB_FIND_FILE_FULL_DIRECTORY_INFO) { |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 618 | FILE_FULL_DIRECTORY_INFO *pFindData = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | (FILE_FULL_DIRECTORY_INFO *)current_entry; |
| 620 | filename = &pFindData->FileName[0]; |
| 621 | len = le32_to_cpu(pFindData->FileNameLength); |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 622 | } else if (cfile->srch_inf.info_level == |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 623 | SMB_FIND_FILE_ID_FULL_DIR_INFO) { |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 624 | SEARCH_ID_FULL_DIR_INFO *pFindData = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | (SEARCH_ID_FULL_DIR_INFO *)current_entry; |
| 626 | filename = &pFindData->FileName[0]; |
| 627 | len = le32_to_cpu(pFindData->FileNameLength); |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 628 | } else if (cfile->srch_inf.info_level == |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 629 | SMB_FIND_FILE_BOTH_DIRECTORY_INFO) { |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 630 | FILE_BOTH_DIRECTORY_INFO *pFindData = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | (FILE_BOTH_DIRECTORY_INFO *)current_entry; |
| 632 | filename = &pFindData->FileName[0]; |
| 633 | len = le32_to_cpu(pFindData->FileNameLength); |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 634 | } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) { |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 635 | FIND_FILE_STANDARD_INFO *pFindData = |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 636 | (FIND_FILE_STANDARD_INFO *)current_entry; |
| 637 | filename = &pFindData->FileName[0]; |
Steve French | 5ddaa68 | 2006-08-15 13:35:48 +0000 | [diff] [blame] | 638 | len = pFindData->FileNameLength; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | } else { |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 640 | cFYI(1, ("Unknown findfirst level %d", |
| 641 | cfile->srch_inf.info_level)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 644 | if (filename) { |
| 645 | if (cfile->srch_inf.unicode) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | __le16 *ufilename = (__le16 *)filename; |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 647 | if (len == 2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | /* check for . */ |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 649 | if (ufilename[0] == UNICODE_DOT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | rc = 1; |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 651 | } else if (len == 4) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | /* check for .. */ |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 653 | if ((ufilename[0] == UNICODE_DOT) |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 654 | && (ufilename[1] == UNICODE_DOT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | rc = 2; |
| 656 | } |
| 657 | } else /* ASCII */ { |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 658 | if (len == 1) { |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 659 | if (filename[0] == '.') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | rc = 1; |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 661 | } else if (len == 2) { |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 662 | if ((filename[0] == '.') && (filename[1] == '.')) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | rc = 2; |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | return rc; |
| 669 | } |
| 670 | |
Steve French | eafe870 | 2005-09-15 21:47:30 -0700 | [diff] [blame] | 671 | /* Check if directory that we are searching has changed so we can decide |
| 672 | whether we can use the cached search results from the previous search */ |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 673 | static int is_dir_changed(struct file *file) |
Steve French | eafe870 | 2005-09-15 21:47:30 -0700 | [diff] [blame] | 674 | { |
Christoph Hellwig | c33f8d3 | 2007-04-02 18:47:20 +0000 | [diff] [blame] | 675 | struct inode *inode = file->f_path.dentry->d_inode; |
| 676 | struct cifsInodeInfo *cifsInfo = CIFS_I(inode); |
Steve French | eafe870 | 2005-09-15 21:47:30 -0700 | [diff] [blame] | 677 | |
Christoph Hellwig | c33f8d3 | 2007-04-02 18:47:20 +0000 | [diff] [blame] | 678 | if (cifsInfo->time == 0) |
Steve French | eafe870 | 2005-09-15 21:47:30 -0700 | [diff] [blame] | 679 | return 1; /* directory was changed, perhaps due to unlink */ |
| 680 | else |
| 681 | return 0; |
| 682 | |
| 683 | } |
| 684 | |
Steve French | 0752f15 | 2008-10-07 20:03:33 +0000 | [diff] [blame] | 685 | static int cifs_save_resume_key(const char *current_entry, |
| 686 | struct cifsFileInfo *cifsFile) |
| 687 | { |
| 688 | int rc = 0; |
| 689 | unsigned int len = 0; |
| 690 | __u16 level; |
| 691 | char *filename; |
| 692 | |
| 693 | if ((cifsFile == NULL) || (current_entry == NULL)) |
| 694 | return -EINVAL; |
| 695 | |
| 696 | level = cifsFile->srch_inf.info_level; |
| 697 | |
| 698 | if (level == SMB_FIND_FILE_UNIX) { |
| 699 | FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry; |
| 700 | |
| 701 | filename = &pFindData->FileName[0]; |
| 702 | if (cifsFile->srch_inf.unicode) { |
| 703 | len = cifs_unicode_bytelen(filename); |
| 704 | } else { |
| 705 | /* BB should we make this strnlen of PATH_MAX? */ |
| 706 | len = strnlen(filename, PATH_MAX); |
| 707 | } |
| 708 | cifsFile->srch_inf.resume_key = pFindData->ResumeKey; |
| 709 | } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) { |
| 710 | FILE_DIRECTORY_INFO *pFindData = |
| 711 | (FILE_DIRECTORY_INFO *)current_entry; |
| 712 | filename = &pFindData->FileName[0]; |
| 713 | len = le32_to_cpu(pFindData->FileNameLength); |
| 714 | cifsFile->srch_inf.resume_key = pFindData->FileIndex; |
| 715 | } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) { |
| 716 | FILE_FULL_DIRECTORY_INFO *pFindData = |
| 717 | (FILE_FULL_DIRECTORY_INFO *)current_entry; |
| 718 | filename = &pFindData->FileName[0]; |
| 719 | len = le32_to_cpu(pFindData->FileNameLength); |
| 720 | cifsFile->srch_inf.resume_key = pFindData->FileIndex; |
| 721 | } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) { |
| 722 | SEARCH_ID_FULL_DIR_INFO *pFindData = |
| 723 | (SEARCH_ID_FULL_DIR_INFO *)current_entry; |
| 724 | filename = &pFindData->FileName[0]; |
| 725 | len = le32_to_cpu(pFindData->FileNameLength); |
| 726 | cifsFile->srch_inf.resume_key = pFindData->FileIndex; |
| 727 | } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) { |
| 728 | FILE_BOTH_DIRECTORY_INFO *pFindData = |
| 729 | (FILE_BOTH_DIRECTORY_INFO *)current_entry; |
| 730 | filename = &pFindData->FileName[0]; |
| 731 | len = le32_to_cpu(pFindData->FileNameLength); |
| 732 | cifsFile->srch_inf.resume_key = pFindData->FileIndex; |
| 733 | } else if (level == SMB_FIND_FILE_INFO_STANDARD) { |
| 734 | FIND_FILE_STANDARD_INFO *pFindData = |
| 735 | (FIND_FILE_STANDARD_INFO *)current_entry; |
| 736 | filename = &pFindData->FileName[0]; |
| 737 | /* one byte length, no name conversion */ |
| 738 | len = (unsigned int)pFindData->FileNameLength; |
| 739 | cifsFile->srch_inf.resume_key = pFindData->ResumeKey; |
| 740 | } else { |
| 741 | cFYI(1, ("Unknown findfirst level %d", level)); |
| 742 | return -EINVAL; |
| 743 | } |
| 744 | cifsFile->srch_inf.resume_name_len = len; |
| 745 | cifsFile->srch_inf.presume_name = filename; |
| 746 | return rc; |
| 747 | } |
| 748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | /* find the corresponding entry in the search */ |
| 750 | /* Note that the SMB server returns search entries for . and .. which |
| 751 | complicates logic here if we choose to parse for them and we do not |
| 752 | assume that they are located in the findfirst return buffer.*/ |
| 753 | /* We start counting in the buffer with entry 2 and increment for every |
| 754 | entry (do not increment for . or .. entry) */ |
| 755 | static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon, |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 756 | struct file *file, char **ppCurrentEntry, int *num_to_ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | { |
| 758 | int rc = 0; |
| 759 | int pos_in_buf = 0; |
| 760 | loff_t first_entry_in_buffer; |
| 761 | loff_t index_to_find = file->f_pos; |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 762 | struct cifsFileInfo *cifsFile = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | /* check if index in the buffer */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 764 | |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 765 | if ((cifsFile == NULL) || (ppCurrentEntry == NULL) || |
Steve French | eafe870 | 2005-09-15 21:47:30 -0700 | [diff] [blame] | 766 | (num_to_ret == NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | return -ENOENT; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | *ppCurrentEntry = NULL; |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 770 | first_entry_in_buffer = |
| 771 | cifsFile->srch_inf.index_of_last_entry - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | cifsFile->srch_inf.entries_in_buffer; |
Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 773 | |
| 774 | /* if first entry in buf is zero then is first buffer |
| 775 | in search response data which means it is likely . and .. |
| 776 | will be in this buffer, although some servers do not return |
| 777 | . and .. for the root of a drive and for those we need |
| 778 | to start two entries earlier */ |
| 779 | |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 780 | dump_cifs_file_struct(file, "In fce "); |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 781 | if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) && |
| 782 | is_dir_changed(file)) || |
Steve French | eafe870 | 2005-09-15 21:47:30 -0700 | [diff] [blame] | 783 | (index_to_find < first_entry_in_buffer)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | /* close and restart search */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 785 | cFYI(1, ("search backing up - close and restart search")); |
Steve French | ddb4cbf | 2008-11-20 20:00:44 +0000 | [diff] [blame] | 786 | write_lock(&GlobalSMBSeslock); |
Steve French | 77c57ec | 2008-05-13 21:39:32 +0000 | [diff] [blame] | 787 | if (!cifsFile->srch_inf.endOfSearch && |
| 788 | !cifsFile->invalidHandle) { |
| 789 | cifsFile->invalidHandle = true; |
Steve French | ddb4cbf | 2008-11-20 20:00:44 +0000 | [diff] [blame] | 790 | write_unlock(&GlobalSMBSeslock); |
Steve French | 77c57ec | 2008-05-13 21:39:32 +0000 | [diff] [blame] | 791 | CIFSFindClose(xid, pTcon, cifsFile->netfid); |
Steve French | ddb4cbf | 2008-11-20 20:00:44 +0000 | [diff] [blame] | 792 | } else |
| 793 | write_unlock(&GlobalSMBSeslock); |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 794 | if (cifsFile->srch_inf.ntwrk_buf_start) { |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 795 | cFYI(1, ("freeing SMB ff cache buf on search rewind")); |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 796 | if (cifsFile->srch_inf.smallBuf) |
Steve French | d47d7c1 | 2006-02-28 03:45:48 +0000 | [diff] [blame] | 797 | cifs_small_buf_release(cifsFile->srch_inf. |
| 798 | ntwrk_buf_start); |
| 799 | else |
| 800 | cifs_buf_release(cifsFile->srch_inf. |
| 801 | ntwrk_buf_start); |
Shirish Pargaonkar | 76c510a | 2008-07-24 14:48:33 +0000 | [diff] [blame] | 802 | cifsFile->srch_inf.ntwrk_buf_start = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | } |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 804 | rc = initiate_cifs_search(xid, file); |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 805 | if (rc) { |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 806 | cFYI(1, ("error %d reinitiating a search on rewind", |
| 807 | rc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | return rc; |
| 809 | } |
Jeff Layton | a364bc0 | 2008-10-21 14:42:13 +0000 | [diff] [blame] | 810 | cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 813 | while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) && |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 814 | (rc == 0) && !cifsFile->srch_inf.endOfSearch) { |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 815 | cFYI(1, ("calling findnext2")); |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 816 | rc = CIFSFindNext(xid, pTcon, cifsFile->netfid, |
Steve French | dfb7533 | 2005-06-22 17:13:47 -0700 | [diff] [blame] | 817 | &cifsFile->srch_inf); |
Jeff Layton | a364bc0 | 2008-10-21 14:42:13 +0000 | [diff] [blame] | 818 | cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile); |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 819 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | return -ENOENT; |
| 821 | } |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 822 | if (index_to_find < cifsFile->srch_inf.index_of_last_entry) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | /* we found the buffer that contains the entry */ |
| 824 | /* scan and find it */ |
| 825 | int i; |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 826 | char *current_entry; |
| 827 | char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | smbCalcSize((struct smb_hdr *) |
| 829 | cifsFile->srch_inf.ntwrk_buf_start); |
Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 830 | |
| 831 | current_entry = cifsFile->srch_inf.srch_entries_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry |
| 833 | - cifsFile->srch_inf.entries_in_buffer; |
| 834 | pos_in_buf = index_to_find - first_entry_in_buffer; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 835 | cFYI(1, ("found entry - pos_in_buf %d", pos_in_buf)); |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 836 | |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 837 | for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) { |
Steve French | dfb7533 | 2005-06-22 17:13:47 -0700 | [diff] [blame] | 838 | /* go entry by entry figuring out which is first */ |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 839 | current_entry = nxt_dir_entry(current_entry, end_of_smb, |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 840 | cifsFile->srch_inf.info_level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | } |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 842 | if ((current_entry == NULL) && (i < pos_in_buf)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | /* BB fixme - check if we should flag this error */ |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 844 | cERROR(1, ("reached end of buf searching for pos in buf" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | " %d index to find %lld rc %d", |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 846 | pos_in_buf, index_to_find, rc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | } |
| 848 | rc = 0; |
| 849 | *ppCurrentEntry = current_entry; |
| 850 | } else { |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 851 | cFYI(1, ("index not in buffer - could not findnext into it")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | return 0; |
| 853 | } |
| 854 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 855 | if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) { |
| 856 | cFYI(1, ("can not return entries pos_in_buf beyond last")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | *num_to_ret = 0; |
| 858 | } else |
| 859 | *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | |
| 861 | return rc; |
| 862 | } |
| 863 | |
| 864 | /* inode num, inode type and filename returned */ |
| 865 | static int cifs_get_name_from_search_buf(struct qstr *pqst, |
| 866 | char *current_entry, __u16 level, unsigned int unicode, |
Jeff Layton | 1829579 | 2009-04-30 20:45:45 -0400 | [diff] [blame] | 867 | struct cifs_sb_info *cifs_sb, unsigned int max_len, __u64 *pinum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | { |
| 869 | int rc = 0; |
| 870 | unsigned int len = 0; |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 871 | char *filename; |
| 872 | struct nls_table *nlt = cifs_sb->local_nls; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | |
| 874 | *pinum = 0; |
| 875 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 876 | if (level == SMB_FIND_FILE_UNIX) { |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 877 | FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | |
| 879 | filename = &pFindData->FileName[0]; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 880 | if (unicode) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | len = cifs_unicode_bytelen(filename); |
| 882 | } else { |
| 883 | /* BB should we make this strnlen of PATH_MAX? */ |
| 884 | len = strnlen(filename, PATH_MAX); |
| 885 | } |
| 886 | |
Steve French | 85a6dac | 2009-04-01 05:22:00 +0000 | [diff] [blame] | 887 | *pinum = le64_to_cpu(pFindData->UniqueId); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 888 | } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) { |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 889 | FILE_DIRECTORY_INFO *pFindData = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | (FILE_DIRECTORY_INFO *)current_entry; |
| 891 | filename = &pFindData->FileName[0]; |
| 892 | len = le32_to_cpu(pFindData->FileNameLength); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 893 | } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) { |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 894 | FILE_FULL_DIRECTORY_INFO *pFindData = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | (FILE_FULL_DIRECTORY_INFO *)current_entry; |
| 896 | filename = &pFindData->FileName[0]; |
| 897 | len = le32_to_cpu(pFindData->FileNameLength); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 898 | } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) { |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 899 | SEARCH_ID_FULL_DIR_INFO *pFindData = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | (SEARCH_ID_FULL_DIR_INFO *)current_entry; |
| 901 | filename = &pFindData->FileName[0]; |
| 902 | len = le32_to_cpu(pFindData->FileNameLength); |
Steve French | 85a6dac | 2009-04-01 05:22:00 +0000 | [diff] [blame] | 903 | *pinum = le64_to_cpu(pFindData->UniqueId); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 904 | } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) { |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 905 | FILE_BOTH_DIRECTORY_INFO *pFindData = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | (FILE_BOTH_DIRECTORY_INFO *)current_entry; |
| 907 | filename = &pFindData->FileName[0]; |
| 908 | len = le32_to_cpu(pFindData->FileNameLength); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 909 | } else if (level == SMB_FIND_FILE_INFO_STANDARD) { |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 910 | FIND_FILE_STANDARD_INFO *pFindData = |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 911 | (FIND_FILE_STANDARD_INFO *)current_entry; |
| 912 | filename = &pFindData->FileName[0]; |
| 913 | /* one byte length, no name conversion */ |
| 914 | len = (unsigned int)pFindData->FileNameLength; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | } else { |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 916 | cFYI(1, ("Unknown findfirst level %d", level)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | return -EINVAL; |
| 918 | } |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 919 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 920 | if (len > max_len) { |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 921 | cERROR(1, ("bad search response length %d past smb end", len)); |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 922 | return -EINVAL; |
| 923 | } |
| 924 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 925 | if (unicode) { |
Jeff Layton | f588416 | 2009-04-30 07:18:00 -0400 | [diff] [blame] | 926 | pqst->len = cifs_from_ucs2((char *) pqst->name, |
| 927 | (__le16 *) filename, |
Jeff Layton | 1829579 | 2009-04-30 20:45:45 -0400 | [diff] [blame] | 928 | UNICODE_NAME_MAX, |
| 929 | min(len, max_len), nlt, |
Jeff Layton | f588416 | 2009-04-30 07:18:00 -0400 | [diff] [blame] | 930 | cifs_sb->mnt_cifs_flags & |
| 931 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | } else { |
| 933 | pqst->name = filename; |
| 934 | pqst->len = len; |
| 935 | } |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 936 | pqst->hash = full_name_hash(pqst->name, pqst->len); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 937 | /* cFYI(1, ("filldir on %s",pqst->name)); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | return rc; |
| 939 | } |
| 940 | |
Jeff Layton | 1829579 | 2009-04-30 20:45:45 -0400 | [diff] [blame] | 941 | static int cifs_filldir(char *pfindEntry, struct file *file, filldir_t filldir, |
| 942 | void *direntry, char *scratch_buf, unsigned int max_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | { |
| 944 | int rc = 0; |
| 945 | struct qstr qstring; |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 946 | struct cifsFileInfo *pCifsF; |
Steve French | 2c2130e | 2007-10-12 19:10:28 +0000 | [diff] [blame] | 947 | unsigned int obj_type; |
Jeff Layton | 950ec52 | 2009-02-11 08:08:26 -0500 | [diff] [blame] | 948 | __u64 inum; |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 949 | struct cifs_sb_info *cifs_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | struct inode *tmp_inode; |
| 951 | struct dentry *tmp_dentry; |
| 952 | |
| 953 | /* get filename and len into qstring */ |
| 954 | /* get dentry */ |
| 955 | /* decide whether to create and populate ionde */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 956 | if ((direntry == NULL) || (file == NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | return -EINVAL; |
| 958 | |
| 959 | pCifsF = file->private_data; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 960 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 961 | if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | return -ENOENT; |
| 963 | |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 964 | rc = cifs_entry_is_dot(pfindEntry, pCifsF); |
Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 965 | /* skip . and .. since we added them first */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 966 | if (rc != 0) |
Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 967 | return 0; |
| 968 | |
Josef "Jeff" Sipek | e6a0029 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 969 | cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | |
| 971 | qstring.name = scratch_buf; |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 972 | rc = cifs_get_name_from_search_buf(&qstring, pfindEntry, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | pCifsF->srch_inf.info_level, |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 974 | pCifsF->srch_inf.unicode, cifs_sb, |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 975 | max_len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | &inum /* returned */); |
| 977 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 978 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | return rc; |
| 980 | |
Jeff Layton | 132ac7b | 2009-02-10 07:33:57 -0500 | [diff] [blame] | 981 | /* only these two infolevels return valid inode numbers */ |
| 982 | if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX || |
| 983 | pCifsF->srch_inf.info_level == SMB_FIND_FILE_ID_FULL_DIR_INFO) |
| 984 | rc = construct_dentry(&qstring, file, &tmp_inode, &tmp_dentry, |
| 985 | &inum); |
| 986 | else |
| 987 | rc = construct_dentry(&qstring, file, &tmp_inode, &tmp_dentry, |
| 988 | NULL); |
| 989 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 990 | if ((tmp_inode == NULL) || (tmp_dentry == NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | return -ENOMEM; |
| 992 | |
Steve French | 966ca92 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 993 | /* we pass in rc below, indicating whether it is a new inode, |
| 994 | so we can figure out whether to invalidate the inode cached |
| 995 | data if the file has changed */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 996 | if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX) |
Steve French | 966ca92 | 2005-04-28 22:41:08 -0700 | [diff] [blame] | 997 | unix_fill_in_inode(tmp_inode, |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 998 | (FILE_UNIX_INFO *)pfindEntry, |
| 999 | &obj_type, rc); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1000 | else if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 1001 | fill_in_inode(tmp_inode, 0 /* old level 1 buffer type */, |
| 1002 | pfindEntry, &obj_type, rc); |
| 1003 | else |
| 1004 | fill_in_inode(tmp_inode, 1 /* NT */, pfindEntry, &obj_type, rc); |
Steve French | b835beb | 2006-09-06 22:02:22 +0000 | [diff] [blame] | 1005 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1006 | if (rc) /* new inode - needs to be tied to dentry */ { |
Steve French | b835beb | 2006-09-06 22:02:22 +0000 | [diff] [blame] | 1007 | d_instantiate(tmp_dentry, tmp_inode); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1008 | if (rc == 2) |
Steve French | b835beb | 2006-09-06 22:02:22 +0000 | [diff] [blame] | 1009 | d_rehash(tmp_dentry); |
| 1010 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1011 | |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 1012 | |
| 1013 | rc = filldir(direntry, qstring.name, qstring.len, file->f_pos, |
| 1014 | tmp_inode->i_ino, obj_type); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1015 | if (rc) { |
| 1016 | cFYI(1, ("filldir rc = %d", rc)); |
Steve French | 7ca85ba | 2006-10-30 21:42:57 +0000 | [diff] [blame] | 1017 | /* we can not return filldir errors to the caller |
| 1018 | since they are "normal" when the stat blocksize |
| 1019 | is too small - we return remapped error instead */ |
| 1020 | rc = -EOVERFLOW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | dput(tmp_dentry); |
| 1024 | return rc; |
| 1025 | } |
| 1026 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | |
| 1028 | int cifs_readdir(struct file *file, void *direntry, filldir_t filldir) |
| 1029 | { |
| 1030 | int rc = 0; |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 1031 | int xid, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | struct cifs_sb_info *cifs_sb; |
| 1033 | struct cifsTconInfo *pTcon; |
| 1034 | struct cifsFileInfo *cifsFile = NULL; |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 1035 | char *current_entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | int num_to_fill = 0; |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 1037 | char *tmp_buf = NULL; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1038 | char *end_of_smb; |
Jeff Layton | 1829579 | 2009-04-30 20:45:45 -0400 | [diff] [blame] | 1039 | unsigned int max_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | |
| 1041 | xid = GetXid(); |
| 1042 | |
Josef "Jeff" Sipek | e6a0029 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 1043 | cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | pTcon = cifs_sb->tcon; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1045 | if (pTcon == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | return -EINVAL; |
| 1047 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | switch ((int) file->f_pos) { |
| 1049 | case 0: |
Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 1050 | if (filldir(direntry, ".", 1, file->f_pos, |
Josef "Jeff" Sipek | e6a0029 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 1051 | file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) { |
Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 1052 | cERROR(1, ("Filldir for current dir failed")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | rc = -ENOMEM; |
| 1054 | break; |
| 1055 | } |
Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 1056 | file->f_pos++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | case 1: |
Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 1058 | if (filldir(direntry, "..", 2, file->f_pos, |
Josef "Jeff" Sipek | e6a0029 | 2006-12-08 02:36:48 -0800 | [diff] [blame] | 1059 | 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] | 1060 | cERROR(1, ("Filldir for parent dir failed")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | rc = -ENOMEM; |
| 1062 | break; |
| 1063 | } |
Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 1064 | file->f_pos++; |
| 1065 | default: |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 1066 | /* 1) If search is active, |
| 1067 | is in current search buffer? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | if it before then restart search |
| 1069 | if after then keep searching till find it */ |
| 1070 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1071 | if (file->private_data == NULL) { |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 1072 | rc = initiate_cifs_search(xid, file); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1073 | cFYI(1, ("initiate cifs search rc %d", rc)); |
| 1074 | if (rc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | FreeXid(xid); |
| 1076 | return rc; |
| 1077 | } |
| 1078 | } |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1079 | if (file->private_data == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | rc = -EINVAL; |
| 1081 | FreeXid(xid); |
| 1082 | return rc; |
| 1083 | } |
| 1084 | cifsFile = file->private_data; |
| 1085 | if (cifsFile->srch_inf.endOfSearch) { |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1086 | if (cifsFile->srch_inf.emptyDir) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | cFYI(1, ("End of search, empty dir")); |
| 1088 | rc = 0; |
| 1089 | break; |
| 1090 | } |
| 1091 | } /* else { |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 1092 | cifsFile->invalidHandle = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | CIFSFindClose(xid, pTcon, cifsFile->netfid); |
Steve French | aaa9bbe | 2008-05-23 17:38:32 +0000 | [diff] [blame] | 1094 | } */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 1096 | rc = find_cifs_entry(xid, pTcon, file, |
| 1097 | ¤t_entry, &num_to_fill); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1098 | if (rc) { |
| 1099 | cFYI(1, ("fce error %d", rc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | goto rddir2_exit; |
| 1101 | } else if (current_entry != NULL) { |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1102 | cFYI(1, ("entry %lld found", file->f_pos)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | } else { |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1104 | cFYI(1, ("could not find entry")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | goto rddir2_exit; |
| 1106 | } |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1107 | cFYI(1, ("loop through %d times filling dir for net buf %p", |
| 1108 | num_to_fill, cifsFile->srch_inf.ntwrk_buf_start)); |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 1109 | max_len = smbCalcSize((struct smb_hdr *) |
| 1110 | cifsFile->srch_inf.ntwrk_buf_start); |
| 1111 | end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len; |
| 1112 | |
Jeff Layton | f588416 | 2009-04-30 07:18:00 -0400 | [diff] [blame] | 1113 | tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1114 | for (i = 0; (i < num_to_fill) && (rc == 0); i++) { |
| 1115 | if (current_entry == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | /* evaluate whether this case is an error */ |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 1117 | cERROR(1, ("past SMB end, num to fill %d i %d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | num_to_fill, i)); |
| 1119 | break; |
| 1120 | } |
Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 1121 | /* if buggy server returns . and .. late do |
| 1122 | we want to check for that here? */ |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 1123 | rc = cifs_filldir(current_entry, file, |
| 1124 | filldir, direntry, tmp_buf, max_len); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1125 | if (rc == -EOVERFLOW) { |
Steve French | 7ca85ba | 2006-10-30 21:42:57 +0000 | [diff] [blame] | 1126 | rc = 0; |
| 1127 | break; |
| 1128 | } |
| 1129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | file->f_pos++; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1131 | if (file->f_pos == |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 1132 | cifsFile->srch_inf.index_of_last_entry) { |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1133 | cFYI(1, ("last entry in buf at pos %lld %s", |
| 1134 | file->f_pos, tmp_buf)); |
| 1135 | cifs_save_resume_key(current_entry, cifsFile); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | break; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 1137 | } else |
| 1138 | current_entry = |
Steve French | 5bafd76 | 2006-06-07 00:18:43 +0000 | [diff] [blame] | 1139 | nxt_dir_entry(current_entry, end_of_smb, |
| 1140 | cifsFile->srch_inf.info_level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | } |
| 1142 | kfree(tmp_buf); |
| 1143 | break; |
| 1144 | } /* end switch */ |
| 1145 | |
| 1146 | rddir2_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | FreeXid(xid); |
| 1148 | return rc; |
| 1149 | } |