Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * fs/cifs/readdir.c | |
3 | * | |
4 | * Directory search handling | |
5 | * | |
966ca923 | 6 | * Copyright (C) International Business Machines Corp., 2004, 2005 |
1da177e4 LT |
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> | |
24 | #include <linux/stat.h> | |
25 | #include <linux/smp_lock.h> | |
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 | ||
34 | /* BB fixme - add debug wrappers around this function to disable it fixme BB */ | |
35 | /* static void dump_cifs_file_struct(struct file *file, char *label) | |
36 | { | |
37 | struct cifsFileInfo * cf; | |
38 | ||
39 | if(file) { | |
40 | cf = file->private_data; | |
41 | if(cf == NULL) { | |
42 | cFYI(1,("empty cifs private file data")); | |
43 | return; | |
44 | } | |
45 | if(cf->invalidHandle) { | |
46 | cFYI(1,("invalid handle")); | |
47 | } | |
48 | if(cf->srch_inf.endOfSearch) { | |
49 | cFYI(1,("end of search")); | |
50 | } | |
51 | if(cf->srch_inf.emptyDir) { | |
52 | cFYI(1,("empty dir")); | |
53 | } | |
54 | ||
55 | } | |
56 | } */ | |
57 | ||
58 | /* Returns one if new inode created (which therefore needs to be hashed) */ | |
59 | /* Might check in the future if inode number changed so we can rehash inode */ | |
60 | static int construct_dentry(struct qstr *qstring, struct file *file, | |
61 | struct inode **ptmp_inode, struct dentry **pnew_dentry) | |
62 | { | |
63 | struct dentry *tmp_dentry; | |
64 | struct cifs_sb_info *cifs_sb; | |
65 | struct cifsTconInfo *pTcon; | |
66 | int rc = 0; | |
67 | ||
966ca923 | 68 | cFYI(1, ("For %s", qstring->name)); |
1da177e4 LT |
69 | cifs_sb = CIFS_SB(file->f_dentry->d_sb); |
70 | pTcon = cifs_sb->tcon; | |
71 | ||
72 | qstring->hash = full_name_hash(qstring->name, qstring->len); | |
73 | tmp_dentry = d_lookup(file->f_dentry, qstring); | |
74 | if (tmp_dentry) { | |
966ca923 | 75 | cFYI(0, ("existing dentry with inode 0x%p", tmp_dentry->d_inode)); |
1da177e4 LT |
76 | *ptmp_inode = tmp_dentry->d_inode; |
77 | /* BB overwrite old name? i.e. tmp_dentry->d_name and tmp_dentry->d_name.len??*/ | |
78 | if(*ptmp_inode == NULL) { | |
79 | *ptmp_inode = new_inode(file->f_dentry->d_sb); | |
80 | if(*ptmp_inode == NULL) | |
81 | return rc; | |
82 | rc = 1; | |
83 | d_instantiate(tmp_dentry, *ptmp_inode); | |
84 | } | |
85 | } else { | |
86 | tmp_dentry = d_alloc(file->f_dentry, qstring); | |
87 | if(tmp_dentry == NULL) { | |
88 | cERROR(1,("Failed allocating dentry")); | |
89 | *ptmp_inode = NULL; | |
90 | return rc; | |
91 | } | |
92 | ||
93 | *ptmp_inode = new_inode(file->f_dentry->d_sb); | |
94 | tmp_dentry->d_op = &cifs_dentry_ops; | |
95 | if(*ptmp_inode == NULL) | |
96 | return rc; | |
97 | rc = 1; | |
98 | d_instantiate(tmp_dentry, *ptmp_inode); | |
99 | d_rehash(tmp_dentry); | |
100 | } | |
101 | ||
102 | tmp_dentry->d_time = jiffies; | |
103 | *pnew_dentry = tmp_dentry; | |
104 | return rc; | |
105 | } | |
106 | ||
107 | static void fill_in_inode(struct inode *tmp_inode, | |
966ca923 | 108 | FILE_DIRECTORY_INFO *pfindData, int *pobject_type, int isNewInode) |
1da177e4 | 109 | { |
966ca923 SF |
110 | loff_t local_size; |
111 | struct timespec local_mtime; | |
112 | ||
1da177e4 LT |
113 | struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode); |
114 | struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb); | |
115 | __u32 attr = le32_to_cpu(pfindData->ExtFileAttributes); | |
116 | __u64 allocation_size = le64_to_cpu(pfindData->AllocationSize); | |
117 | __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile); | |
118 | ||
119 | cifsInfo->cifsAttrs = attr; | |
120 | cifsInfo->time = jiffies; | |
121 | ||
966ca923 SF |
122 | /* save mtime and size */ |
123 | local_mtime = tmp_inode->i_mtime; | |
124 | local_size = tmp_inode->i_size; | |
125 | ||
1da177e4 LT |
126 | /* Linux can not store file creation time unfortunately so ignore it */ |
127 | tmp_inode->i_atime = | |
128 | cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime)); | |
129 | tmp_inode->i_mtime = | |
130 | cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime)); | |
131 | tmp_inode->i_ctime = | |
132 | cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime)); | |
133 | /* treat dos attribute of read-only as read-only mode bit e.g. 555? */ | |
134 | /* 2767 perms - indicate mandatory locking */ | |
135 | /* BB fill in uid and gid here? with help from winbind? | |
136 | or retrieve from NTFS stream extended attribute */ | |
137 | if (atomic_read(&cifsInfo->inUse) == 0) { | |
138 | tmp_inode->i_uid = cifs_sb->mnt_uid; | |
139 | tmp_inode->i_gid = cifs_sb->mnt_gid; | |
140 | /* set default mode. will override for dirs below */ | |
141 | tmp_inode->i_mode = cifs_sb->mnt_file_mode; | |
142 | } | |
143 | ||
1da177e4 LT |
144 | if (attr & ATTR_DIRECTORY) { |
145 | *pobject_type = DT_DIR; | |
146 | /* override default perms since we do not lock dirs */ | |
147 | if(atomic_read(&cifsInfo->inUse) == 0) { | |
148 | tmp_inode->i_mode = cifs_sb->mnt_dir_mode; | |
149 | } | |
150 | tmp_inode->i_mode |= S_IFDIR; | |
151 | /* we no longer mark these because we could not follow them */ | |
152 | /* } else if (attr & ATTR_REPARSE) { | |
153 | *pobject_type = DT_LNK; | |
154 | tmp_inode->i_mode |= S_IFLNK; */ | |
155 | } else { | |
156 | *pobject_type = DT_REG; | |
157 | tmp_inode->i_mode |= S_IFREG; | |
158 | if (attr & ATTR_READONLY) | |
159 | tmp_inode->i_mode &= ~(S_IWUGO); | |
160 | } /* could add code here - to validate if device or weird share type? */ | |
161 | ||
162 | /* can not fill in nlink here as in qpathinfo version and Unx search */ | |
163 | if (atomic_read(&cifsInfo->inUse) == 0) { | |
164 | atomic_set(&cifsInfo->inUse, 1); | |
165 | } | |
166 | ||
167 | if (is_size_safe_to_change(cifsInfo)) { | |
168 | /* can not safely change the file size here if the | |
169 | client is writing to it due to potential races */ | |
170 | i_size_write(tmp_inode, end_of_file); | |
171 | ||
172 | /* 512 bytes (2**9) is the fake blocksize that must be used */ | |
173 | /* for this calculation, even though the reported blocksize is larger */ | |
174 | tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9; | |
175 | } | |
176 | ||
177 | if (allocation_size < end_of_file) | |
178 | cFYI(1, ("May be sparse file, allocation less than file size")); | |
179 | cFYI(1, | |
180 | ("File Size %ld and blocks %ld and blocksize %ld", | |
181 | (unsigned long)tmp_inode->i_size, tmp_inode->i_blocks, | |
182 | tmp_inode->i_blksize)); | |
183 | if (S_ISREG(tmp_inode->i_mode)) { | |
966ca923 | 184 | cFYI(1, ("File inode")); |
1da177e4 LT |
185 | tmp_inode->i_op = &cifs_file_inode_ops; |
186 | if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) | |
187 | tmp_inode->i_fop = &cifs_file_direct_ops; | |
188 | else | |
189 | tmp_inode->i_fop = &cifs_file_ops; | |
190 | tmp_inode->i_data.a_ops = &cifs_addr_ops; | |
966ca923 SF |
191 | |
192 | if(isNewInode) | |
193 | return; /* No sense invalidating pages for new inode since we | |
194 | have not started caching readahead file data yet */ | |
195 | ||
196 | if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) && | |
197 | (local_size == tmp_inode->i_size)) { | |
198 | cFYI(1, ("inode exists but unchanged")); | |
199 | } else { | |
200 | /* file may have changed on server */ | |
201 | cFYI(1, ("invalidate inode, readdir detected change")); | |
202 | invalidate_remote_inode(tmp_inode); | |
203 | } | |
1da177e4 | 204 | } else if (S_ISDIR(tmp_inode->i_mode)) { |
966ca923 | 205 | cFYI(1, ("Directory inode")); |
1da177e4 LT |
206 | tmp_inode->i_op = &cifs_dir_inode_ops; |
207 | tmp_inode->i_fop = &cifs_dir_ops; | |
208 | } else if (S_ISLNK(tmp_inode->i_mode)) { | |
966ca923 | 209 | cFYI(1, ("Symbolic Link inode")); |
1da177e4 LT |
210 | tmp_inode->i_op = &cifs_symlink_inode_ops; |
211 | } else { | |
966ca923 | 212 | cFYI(1, ("Init special inode")); |
1da177e4 LT |
213 | init_special_inode(tmp_inode, tmp_inode->i_mode, |
214 | tmp_inode->i_rdev); | |
215 | } | |
216 | } | |
217 | ||
218 | static void unix_fill_in_inode(struct inode *tmp_inode, | |
966ca923 | 219 | FILE_UNIX_INFO *pfindData, int *pobject_type, int isNewInode) |
1da177e4 | 220 | { |
966ca923 SF |
221 | loff_t local_size; |
222 | struct timespec local_mtime; | |
223 | ||
1da177e4 LT |
224 | struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode); |
225 | struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb); | |
226 | ||
227 | __u32 type = le32_to_cpu(pfindData->Type); | |
228 | __u64 num_of_bytes = le64_to_cpu(pfindData->NumOfBytes); | |
229 | __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile); | |
230 | cifsInfo->time = jiffies; | |
231 | atomic_inc(&cifsInfo->inUse); | |
232 | ||
966ca923 SF |
233 | /* save mtime and size */ |
234 | local_mtime = tmp_inode->i_mtime; | |
235 | local_size = tmp_inode->i_size; | |
236 | ||
1da177e4 LT |
237 | tmp_inode->i_atime = |
238 | cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime)); | |
239 | tmp_inode->i_mtime = | |
240 | cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastModificationTime)); | |
241 | tmp_inode->i_ctime = | |
242 | cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastStatusChange)); | |
243 | ||
244 | tmp_inode->i_mode = le64_to_cpu(pfindData->Permissions); | |
245 | if (type == UNIX_FILE) { | |
246 | *pobject_type = DT_REG; | |
247 | tmp_inode->i_mode |= S_IFREG; | |
248 | } else if (type == UNIX_SYMLINK) { | |
249 | *pobject_type = DT_LNK; | |
250 | tmp_inode->i_mode |= S_IFLNK; | |
251 | } else if (type == UNIX_DIR) { | |
252 | *pobject_type = DT_DIR; | |
253 | tmp_inode->i_mode |= S_IFDIR; | |
254 | } else if (type == UNIX_CHARDEV) { | |
255 | *pobject_type = DT_CHR; | |
256 | tmp_inode->i_mode |= S_IFCHR; | |
257 | tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor), | |
258 | le64_to_cpu(pfindData->DevMinor) & MINORMASK); | |
259 | } else if (type == UNIX_BLOCKDEV) { | |
260 | *pobject_type = DT_BLK; | |
261 | tmp_inode->i_mode |= S_IFBLK; | |
262 | tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor), | |
263 | le64_to_cpu(pfindData->DevMinor) & MINORMASK); | |
264 | } else if (type == UNIX_FIFO) { | |
265 | *pobject_type = DT_FIFO; | |
266 | tmp_inode->i_mode |= S_IFIFO; | |
267 | } else if (type == UNIX_SOCKET) { | |
268 | *pobject_type = DT_SOCK; | |
269 | tmp_inode->i_mode |= S_IFSOCK; | |
270 | } | |
271 | ||
272 | tmp_inode->i_uid = le64_to_cpu(pfindData->Uid); | |
273 | tmp_inode->i_gid = le64_to_cpu(pfindData->Gid); | |
274 | tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks); | |
275 | ||
276 | if (is_size_safe_to_change(cifsInfo)) { | |
277 | /* can not safely change the file size here if the | |
278 | client is writing to it due to potential races */ | |
279 | i_size_write(tmp_inode,end_of_file); | |
280 | ||
281 | /* 512 bytes (2**9) is the fake blocksize that must be used */ | |
282 | /* for this calculation, not the real blocksize */ | |
283 | tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9; | |
284 | } | |
285 | ||
286 | if (S_ISREG(tmp_inode->i_mode)) { | |
287 | cFYI(1, ("File inode")); | |
288 | tmp_inode->i_op = &cifs_file_inode_ops; | |
289 | if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) | |
290 | tmp_inode->i_fop = &cifs_file_direct_ops; | |
291 | else | |
292 | tmp_inode->i_fop = &cifs_file_ops; | |
293 | tmp_inode->i_data.a_ops = &cifs_addr_ops; | |
966ca923 SF |
294 | |
295 | if(isNewInode) | |
296 | return; /* No sense invalidating pages for new inode since we | |
297 | have not started caching readahead file data yet */ | |
298 | ||
299 | if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) && | |
300 | (local_size == tmp_inode->i_size)) { | |
301 | cFYI(1, ("inode exists but unchanged")); | |
302 | } else { | |
303 | /* file may have changed on server */ | |
304 | cFYI(1, ("invalidate inode, readdir detected change")); | |
305 | invalidate_remote_inode(tmp_inode); | |
306 | } | |
1da177e4 LT |
307 | } else if (S_ISDIR(tmp_inode->i_mode)) { |
308 | cFYI(1, ("Directory inode")); | |
309 | tmp_inode->i_op = &cifs_dir_inode_ops; | |
310 | tmp_inode->i_fop = &cifs_dir_ops; | |
311 | } else if (S_ISLNK(tmp_inode->i_mode)) { | |
312 | cFYI(1, ("Symbolic Link inode")); | |
313 | tmp_inode->i_op = &cifs_symlink_inode_ops; | |
314 | /* tmp_inode->i_fop = *//* do not need to set to anything */ | |
315 | } else { | |
316 | cFYI(1, ("Special inode")); | |
317 | init_special_inode(tmp_inode, tmp_inode->i_mode, | |
318 | tmp_inode->i_rdev); | |
319 | } | |
320 | } | |
321 | ||
322 | static int initiate_cifs_search(const int xid, struct file *file) | |
323 | { | |
324 | int rc = 0; | |
325 | char * full_path; | |
326 | struct cifsFileInfo * cifsFile; | |
327 | struct cifs_sb_info *cifs_sb; | |
328 | struct cifsTconInfo *pTcon; | |
329 | ||
330 | if(file->private_data == NULL) { | |
331 | file->private_data = | |
332 | kmalloc(sizeof(struct cifsFileInfo),GFP_KERNEL); | |
333 | } | |
334 | ||
335 | if(file->private_data == NULL) { | |
336 | return -ENOMEM; | |
337 | } else { | |
338 | memset(file->private_data,0,sizeof(struct cifsFileInfo)); | |
339 | } | |
340 | cifsFile = file->private_data; | |
341 | cifsFile->invalidHandle = TRUE; | |
342 | cifsFile->srch_inf.endOfSearch = FALSE; | |
343 | ||
344 | if(file->f_dentry == NULL) | |
345 | return -ENOENT; | |
346 | ||
347 | cifs_sb = CIFS_SB(file->f_dentry->d_sb); | |
348 | if(cifs_sb == NULL) | |
349 | return -EINVAL; | |
350 | ||
351 | pTcon = cifs_sb->tcon; | |
352 | if(pTcon == NULL) | |
353 | return -EINVAL; | |
354 | ||
355 | down(&file->f_dentry->d_sb->s_vfs_rename_sem); | |
737b758c | 356 | full_path = build_path_from_dentry(file->f_dentry); |
1da177e4 LT |
357 | up(&file->f_dentry->d_sb->s_vfs_rename_sem); |
358 | ||
359 | if(full_path == NULL) { | |
360 | return -ENOMEM; | |
361 | } | |
362 | ||
966ca923 | 363 | cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos)); |
1da177e4 | 364 | |
75cf6bdc | 365 | ffirst_retry: |
1da177e4 LT |
366 | /* test for Unix extensions */ |
367 | if (pTcon->ses->capabilities & CAP_UNIX) { | |
368 | cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX; | |
369 | } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) { | |
370 | cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO; | |
371 | } else /* not srvinos - BB fixme add check for backlevel? */ { | |
372 | cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO; | |
373 | } | |
374 | ||
737b758c SF |
375 | rc = CIFSFindFirst(xid, pTcon,full_path,cifs_sb->local_nls, |
376 | &cifsFile->netfid, &cifsFile->srch_inf, | |
377 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
378 | if(rc == 0) |
379 | cifsFile->invalidHandle = FALSE; | |
75cf6bdc SF |
380 | if((rc == -EOPNOTSUPP) && |
381 | (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) { | |
382 | cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM; | |
383 | goto ffirst_retry; | |
384 | } | |
1da177e4 LT |
385 | kfree(full_path); |
386 | return rc; | |
387 | } | |
388 | ||
389 | /* return length of unicode string in bytes */ | |
390 | static int cifs_unicode_bytelen(char *str) | |
391 | { | |
392 | int len; | |
393 | __le16 * ustr = (__le16 *)str; | |
394 | ||
395 | for(len=0;len <= PATH_MAX;len++) { | |
396 | if(ustr[len] == 0) | |
397 | return len << 1; | |
398 | } | |
399 | cFYI(1,("Unicode string longer than PATH_MAX found")); | |
400 | return len << 1; | |
401 | } | |
402 | ||
403 | static char *nxt_dir_entry(char *old_entry, char *end_of_smb) | |
404 | { | |
405 | char * new_entry; | |
406 | FILE_DIRECTORY_INFO * pDirInfo = (FILE_DIRECTORY_INFO *)old_entry; | |
407 | ||
408 | new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset); | |
409 | cFYI(1,("new entry %p old entry %p",new_entry,old_entry)); | |
410 | /* validate that new_entry is not past end of SMB */ | |
411 | if(new_entry >= end_of_smb) { | |
412 | cFYI(1,("search entry %p began after end of SMB %p old entry %p", | |
413 | new_entry,end_of_smb,old_entry)); | |
414 | return NULL; | |
415 | } else | |
416 | return new_entry; | |
417 | ||
418 | } | |
419 | ||
420 | #define UNICODE_DOT cpu_to_le16(0x2e) | |
421 | ||
422 | /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */ | |
423 | static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile) | |
424 | { | |
425 | int rc = 0; | |
426 | char * filename = NULL; | |
427 | int len = 0; | |
428 | ||
429 | if(cfile->srch_inf.info_level == 0x202) { | |
430 | FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry; | |
431 | filename = &pFindData->FileName[0]; | |
432 | if(cfile->srch_inf.unicode) { | |
433 | len = cifs_unicode_bytelen(filename); | |
434 | } else { | |
435 | /* BB should we make this strnlen of PATH_MAX? */ | |
436 | len = strnlen(filename, 5); | |
437 | } | |
438 | } else if(cfile->srch_inf.info_level == 0x101) { | |
439 | FILE_DIRECTORY_INFO * pFindData = | |
440 | (FILE_DIRECTORY_INFO *)current_entry; | |
441 | filename = &pFindData->FileName[0]; | |
442 | len = le32_to_cpu(pFindData->FileNameLength); | |
443 | } else if(cfile->srch_inf.info_level == 0x102) { | |
444 | FILE_FULL_DIRECTORY_INFO * pFindData = | |
445 | (FILE_FULL_DIRECTORY_INFO *)current_entry; | |
446 | filename = &pFindData->FileName[0]; | |
447 | len = le32_to_cpu(pFindData->FileNameLength); | |
448 | } else if(cfile->srch_inf.info_level == 0x105) { | |
449 | SEARCH_ID_FULL_DIR_INFO * pFindData = | |
450 | (SEARCH_ID_FULL_DIR_INFO *)current_entry; | |
451 | filename = &pFindData->FileName[0]; | |
452 | len = le32_to_cpu(pFindData->FileNameLength); | |
453 | } else if(cfile->srch_inf.info_level == 0x104) { | |
454 | FILE_BOTH_DIRECTORY_INFO * pFindData = | |
455 | (FILE_BOTH_DIRECTORY_INFO *)current_entry; | |
456 | filename = &pFindData->FileName[0]; | |
457 | len = le32_to_cpu(pFindData->FileNameLength); | |
458 | } else { | |
459 | cFYI(1,("Unknown findfirst level %d",cfile->srch_inf.info_level)); | |
460 | } | |
461 | ||
462 | if(filename) { | |
463 | if(cfile->srch_inf.unicode) { | |
464 | __le16 *ufilename = (__le16 *)filename; | |
465 | if(len == 2) { | |
466 | /* check for . */ | |
467 | if(ufilename[0] == UNICODE_DOT) | |
468 | rc = 1; | |
469 | } else if(len == 4) { | |
470 | /* check for .. */ | |
471 | if((ufilename[0] == UNICODE_DOT) | |
472 | &&(ufilename[1] == UNICODE_DOT)) | |
473 | rc = 2; | |
474 | } | |
475 | } else /* ASCII */ { | |
476 | if(len == 1) { | |
477 | if(filename[0] == '.') | |
478 | rc = 1; | |
479 | } else if(len == 2) { | |
480 | if((filename[0] == '.') && (filename[1] == '.')) | |
481 | rc = 2; | |
482 | } | |
483 | } | |
484 | } | |
485 | ||
486 | return rc; | |
487 | } | |
488 | ||
489 | /* find the corresponding entry in the search */ | |
490 | /* Note that the SMB server returns search entries for . and .. which | |
491 | complicates logic here if we choose to parse for them and we do not | |
492 | assume that they are located in the findfirst return buffer.*/ | |
493 | /* We start counting in the buffer with entry 2 and increment for every | |
494 | entry (do not increment for . or .. entry) */ | |
495 | static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon, | |
496 | struct file *file, char **ppCurrentEntry, int *num_to_ret) | |
497 | { | |
498 | int rc = 0; | |
499 | int pos_in_buf = 0; | |
500 | loff_t first_entry_in_buffer; | |
501 | loff_t index_to_find = file->f_pos; | |
502 | struct cifsFileInfo * cifsFile = file->private_data; | |
503 | /* check if index in the buffer */ | |
504 | ||
505 | if((cifsFile == NULL) || (ppCurrentEntry == NULL) || (num_to_ret == NULL)) | |
506 | return -ENOENT; | |
507 | ||
508 | *ppCurrentEntry = NULL; | |
509 | first_entry_in_buffer = | |
510 | cifsFile->srch_inf.index_of_last_entry - | |
511 | cifsFile->srch_inf.entries_in_buffer; | |
512 | /* dump_cifs_file_struct(file, "In fce ");*/ | |
513 | if(index_to_find < first_entry_in_buffer) { | |
514 | /* close and restart search */ | |
515 | cFYI(1,("search backing up - close and restart search")); | |
516 | cifsFile->invalidHandle = TRUE; | |
517 | CIFSFindClose(xid, pTcon, cifsFile->netfid); | |
518 | kfree(cifsFile->search_resume_name); | |
519 | cifsFile->search_resume_name = NULL; | |
520 | if(cifsFile->srch_inf.ntwrk_buf_start) { | |
521 | cFYI(1,("freeing SMB ff cache buf on search rewind")); | |
522 | cifs_buf_release(cifsFile->srch_inf.ntwrk_buf_start); | |
523 | } | |
524 | rc = initiate_cifs_search(xid,file); | |
525 | if(rc) { | |
526 | cFYI(1,("error %d reinitiating a search on rewind",rc)); | |
527 | return rc; | |
528 | } | |
529 | } | |
530 | ||
531 | while((index_to_find >= cifsFile->srch_inf.index_of_last_entry) && | |
532 | (rc == 0) && (cifsFile->srch_inf.endOfSearch == FALSE)){ | |
533 | cFYI(1,("calling findnext2")); | |
534 | rc = CIFSFindNext(xid,pTcon,cifsFile->netfid, &cifsFile->srch_inf); | |
535 | if(rc) | |
536 | return -ENOENT; | |
537 | } | |
538 | if(index_to_find < cifsFile->srch_inf.index_of_last_entry) { | |
539 | /* we found the buffer that contains the entry */ | |
540 | /* scan and find it */ | |
541 | int i; | |
542 | char * current_entry; | |
543 | char * end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + | |
544 | smbCalcSize((struct smb_hdr *) | |
545 | cifsFile->srch_inf.ntwrk_buf_start); | |
546 | /* dump_cifs_file_struct(file,"found entry in fce "); */ | |
547 | first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry | |
548 | - cifsFile->srch_inf.entries_in_buffer; | |
549 | pos_in_buf = index_to_find - first_entry_in_buffer; | |
550 | cFYI(1,("found entry - pos_in_buf %d",pos_in_buf)); | |
551 | current_entry = cifsFile->srch_inf.srch_entries_start; | |
552 | for(i=0;(i<(pos_in_buf)) && (current_entry != NULL);i++) { | |
553 | /* go entry to next entry figuring out which we need to start with */ | |
554 | /* if( . or ..) | |
555 | skip */ | |
556 | rc = cifs_entry_is_dot(current_entry,cifsFile); | |
557 | if(rc == 1) /* is . or .. so skip */ { | |
558 | cFYI(1,("Entry is .")); /* BB removeme BB */ | |
559 | /* continue; */ | |
560 | } else if (rc == 2 ) { | |
561 | cFYI(1,("Entry is ..")); /* BB removeme BB */ | |
562 | /* continue; */ | |
563 | } | |
564 | current_entry = nxt_dir_entry(current_entry,end_of_smb); | |
565 | } | |
566 | if((current_entry == NULL) && (i < pos_in_buf)) { | |
567 | /* BB fixme - check if we should flag this error */ | |
568 | cERROR(1,("reached end of buf searching for pos in buf" | |
569 | " %d index to find %lld rc %d", | |
570 | pos_in_buf,index_to_find,rc)); | |
571 | } | |
572 | rc = 0; | |
573 | *ppCurrentEntry = current_entry; | |
574 | } else { | |
575 | cFYI(1,("index not in buffer - could not findnext into it")); | |
576 | return 0; | |
577 | } | |
578 | ||
579 | if(pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) { | |
580 | cFYI(1,("can not return entries when pos_in_buf beyond last entry")); | |
581 | *num_to_ret = 0; | |
582 | } else | |
583 | *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf; | |
584 | /* dump_cifs_file_struct(file, "end fce ");*/ | |
585 | ||
586 | return rc; | |
587 | } | |
588 | ||
589 | /* inode num, inode type and filename returned */ | |
590 | static int cifs_get_name_from_search_buf(struct qstr *pqst, | |
591 | char *current_entry, __u16 level, unsigned int unicode, | |
592 | struct cifs_sb_info * cifs_sb, ino_t *pinum) | |
593 | { | |
594 | int rc = 0; | |
595 | unsigned int len = 0; | |
596 | char * filename; | |
597 | struct nls_table * nlt = cifs_sb->local_nls; | |
598 | ||
599 | *pinum = 0; | |
600 | ||
601 | if(level == SMB_FIND_FILE_UNIX) { | |
602 | FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry; | |
603 | ||
604 | filename = &pFindData->FileName[0]; | |
605 | if(unicode) { | |
606 | len = cifs_unicode_bytelen(filename); | |
607 | } else { | |
608 | /* BB should we make this strnlen of PATH_MAX? */ | |
609 | len = strnlen(filename, PATH_MAX); | |
610 | } | |
611 | ||
612 | /* BB fixme - hash low and high 32 bits if not 64 bit arch BB fixme */ | |
613 | if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) | |
614 | *pinum = pFindData->UniqueId; | |
615 | } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) { | |
616 | FILE_DIRECTORY_INFO * pFindData = | |
617 | (FILE_DIRECTORY_INFO *)current_entry; | |
618 | filename = &pFindData->FileName[0]; | |
619 | len = le32_to_cpu(pFindData->FileNameLength); | |
620 | } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) { | |
621 | FILE_FULL_DIRECTORY_INFO * pFindData = | |
622 | (FILE_FULL_DIRECTORY_INFO *)current_entry; | |
623 | filename = &pFindData->FileName[0]; | |
624 | len = le32_to_cpu(pFindData->FileNameLength); | |
625 | } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) { | |
626 | SEARCH_ID_FULL_DIR_INFO * pFindData = | |
627 | (SEARCH_ID_FULL_DIR_INFO *)current_entry; | |
628 | filename = &pFindData->FileName[0]; | |
629 | len = le32_to_cpu(pFindData->FileNameLength); | |
630 | *pinum = pFindData->UniqueId; | |
631 | } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) { | |
632 | FILE_BOTH_DIRECTORY_INFO * pFindData = | |
633 | (FILE_BOTH_DIRECTORY_INFO *)current_entry; | |
634 | filename = &pFindData->FileName[0]; | |
635 | len = le32_to_cpu(pFindData->FileNameLength); | |
636 | } else { | |
637 | cFYI(1,("Unknown findfirst level %d",level)); | |
638 | return -EINVAL; | |
639 | } | |
640 | if(unicode) { | |
641 | /* BB fixme - test with long names */ | |
642 | /* Note converted filename can be longer than in unicode */ | |
6a0b4824 SF |
643 | if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR) |
644 | pqst->len = cifs_convertUCSpath((char *)pqst->name, | |
645 | (__le16 *)filename, len/2, nlt); | |
646 | else | |
6a0b4824 SF |
647 | pqst->len = cifs_strfromUCS_le((char *)pqst->name, |
648 | (wchar_t *)filename,len/2,nlt); | |
1da177e4 LT |
649 | } else { |
650 | pqst->name = filename; | |
651 | pqst->len = len; | |
652 | } | |
653 | pqst->hash = full_name_hash(pqst->name,pqst->len); | |
654 | /* cFYI(1,("filldir on %s",pqst->name)); */ | |
655 | return rc; | |
656 | } | |
657 | ||
658 | static int cifs_filldir(char *pfindEntry, struct file *file, | |
659 | filldir_t filldir, void *direntry, char *scratch_buf) | |
660 | { | |
661 | int rc = 0; | |
662 | struct qstr qstring; | |
663 | struct cifsFileInfo * pCifsF; | |
664 | unsigned obj_type; | |
665 | ino_t inum; | |
666 | struct cifs_sb_info * cifs_sb; | |
667 | struct inode *tmp_inode; | |
668 | struct dentry *tmp_dentry; | |
669 | ||
670 | /* get filename and len into qstring */ | |
671 | /* get dentry */ | |
672 | /* decide whether to create and populate ionde */ | |
673 | if((direntry == NULL) || (file == NULL)) | |
674 | return -EINVAL; | |
675 | ||
676 | pCifsF = file->private_data; | |
677 | ||
678 | if((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL)) | |
679 | return -ENOENT; | |
680 | ||
681 | if(file->f_dentry == NULL) | |
682 | return -ENOENT; | |
683 | ||
684 | cifs_sb = CIFS_SB(file->f_dentry->d_sb); | |
685 | ||
686 | qstring.name = scratch_buf; | |
687 | rc = cifs_get_name_from_search_buf(&qstring,pfindEntry, | |
688 | pCifsF->srch_inf.info_level, | |
689 | pCifsF->srch_inf.unicode,cifs_sb, | |
690 | &inum /* returned */); | |
691 | ||
692 | if(rc) | |
693 | return rc; | |
694 | ||
695 | rc = construct_dentry(&qstring,file,&tmp_inode, &tmp_dentry); | |
696 | if((tmp_inode == NULL) || (tmp_dentry == NULL)) | |
697 | return -ENOMEM; | |
698 | ||
699 | if(rc) { | |
700 | /* inode created, we need to hash it with right inode number */ | |
701 | if(inum != 0) { | |
702 | /* BB fixme - hash the 2 32 quantities bits together if necessary BB */ | |
703 | tmp_inode->i_ino = inum; | |
704 | } | |
705 | insert_inode_hash(tmp_inode); | |
706 | } | |
707 | ||
966ca923 SF |
708 | /* we pass in rc below, indicating whether it is a new inode, |
709 | so we can figure out whether to invalidate the inode cached | |
710 | data if the file has changed */ | |
1da177e4 | 711 | if(pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX) { |
966ca923 SF |
712 | unix_fill_in_inode(tmp_inode, |
713 | (FILE_UNIX_INFO *)pfindEntry,&obj_type, rc); | |
1da177e4 | 714 | } else { |
966ca923 SF |
715 | fill_in_inode(tmp_inode, |
716 | (FILE_DIRECTORY_INFO *)pfindEntry,&obj_type, rc); | |
1da177e4 LT |
717 | } |
718 | ||
719 | rc = filldir(direntry,qstring.name,qstring.len,file->f_pos,tmp_inode->i_ino,obj_type); | |
720 | if(rc) { | |
721 | cFYI(1,("filldir rc = %d",rc)); | |
722 | } | |
723 | ||
724 | dput(tmp_dentry); | |
725 | return rc; | |
726 | } | |
727 | ||
728 | static int cifs_save_resume_key(const char *current_entry, | |
729 | struct cifsFileInfo *cifsFile) | |
730 | { | |
731 | int rc = 0; | |
732 | unsigned int len = 0; | |
733 | __u16 level; | |
734 | char * filename; | |
735 | ||
736 | if((cifsFile == NULL) || (current_entry == NULL)) | |
737 | return -EINVAL; | |
738 | ||
739 | level = cifsFile->srch_inf.info_level; | |
740 | ||
741 | if(level == SMB_FIND_FILE_UNIX) { | |
742 | FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry; | |
743 | ||
744 | filename = &pFindData->FileName[0]; | |
745 | if(cifsFile->srch_inf.unicode) { | |
746 | len = cifs_unicode_bytelen(filename); | |
747 | } else { | |
748 | /* BB should we make this strnlen of PATH_MAX? */ | |
749 | len = strnlen(filename, PATH_MAX); | |
750 | } | |
751 | cifsFile->srch_inf.resume_key = pFindData->ResumeKey; | |
752 | } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) { | |
753 | FILE_DIRECTORY_INFO * pFindData = | |
754 | (FILE_DIRECTORY_INFO *)current_entry; | |
755 | filename = &pFindData->FileName[0]; | |
756 | len = le32_to_cpu(pFindData->FileNameLength); | |
757 | cifsFile->srch_inf.resume_key = pFindData->FileIndex; | |
758 | } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) { | |
759 | FILE_FULL_DIRECTORY_INFO * pFindData = | |
760 | (FILE_FULL_DIRECTORY_INFO *)current_entry; | |
761 | filename = &pFindData->FileName[0]; | |
762 | len = le32_to_cpu(pFindData->FileNameLength); | |
763 | cifsFile->srch_inf.resume_key = pFindData->FileIndex; | |
764 | } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) { | |
765 | SEARCH_ID_FULL_DIR_INFO * pFindData = | |
766 | (SEARCH_ID_FULL_DIR_INFO *)current_entry; | |
767 | filename = &pFindData->FileName[0]; | |
768 | len = le32_to_cpu(pFindData->FileNameLength); | |
769 | cifsFile->srch_inf.resume_key = pFindData->FileIndex; | |
770 | } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) { | |
771 | FILE_BOTH_DIRECTORY_INFO * pFindData = | |
772 | (FILE_BOTH_DIRECTORY_INFO *)current_entry; | |
773 | filename = &pFindData->FileName[0]; | |
774 | len = le32_to_cpu(pFindData->FileNameLength); | |
775 | cifsFile->srch_inf.resume_key = pFindData->FileIndex; | |
776 | } else { | |
777 | cFYI(1,("Unknown findfirst level %d",level)); | |
778 | return -EINVAL; | |
779 | } | |
780 | cifsFile->srch_inf.resume_name_len = len; | |
781 | cifsFile->srch_inf.presume_name = filename; | |
782 | return rc; | |
783 | } | |
784 | ||
785 | int cifs_readdir(struct file *file, void *direntry, filldir_t filldir) | |
786 | { | |
787 | int rc = 0; | |
788 | int xid,i; | |
789 | struct cifs_sb_info *cifs_sb; | |
790 | struct cifsTconInfo *pTcon; | |
791 | struct cifsFileInfo *cifsFile = NULL; | |
792 | char * current_entry; | |
793 | int num_to_fill = 0; | |
794 | char * tmp_buf = NULL; | |
795 | char * end_of_smb; | |
796 | ||
797 | xid = GetXid(); | |
798 | ||
799 | if(file->f_dentry == NULL) { | |
800 | FreeXid(xid); | |
801 | return -EIO; | |
802 | } | |
803 | /* dump_cifs_file_struct(file, "Begin rdir "); */ | |
804 | ||
805 | cifs_sb = CIFS_SB(file->f_dentry->d_sb); | |
806 | pTcon = cifs_sb->tcon; | |
807 | if(pTcon == NULL) | |
808 | return -EINVAL; | |
809 | ||
810 | /* cFYI(1,("readdir2 pos: %lld",file->f_pos)); */ | |
811 | ||
812 | switch ((int) file->f_pos) { | |
813 | case 0: | |
814 | /*if (filldir(direntry, ".", 1, file->f_pos, | |
815 | file->f_dentry->d_inode->i_ino, DT_DIR) < 0) { | |
816 | cERROR(1, ("Filldir for current dir failed ")); | |
817 | rc = -ENOMEM; | |
818 | break; | |
819 | } | |
820 | file->f_pos++; */ | |
821 | case 1: | |
822 | /* if (filldir(direntry, "..", 2, file->f_pos, | |
823 | file->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) { | |
824 | cERROR(1, ("Filldir for parent dir failed ")); | |
825 | rc = -ENOMEM; | |
826 | break; | |
827 | } | |
828 | file->f_pos++; */ | |
829 | case 2: | |
830 | /* 1) If search is active, | |
831 | is in current search buffer? | |
832 | if it before then restart search | |
833 | if after then keep searching till find it */ | |
834 | ||
835 | if(file->private_data == NULL) { | |
836 | rc = initiate_cifs_search(xid,file); | |
837 | cFYI(1,("initiate cifs search rc %d",rc)); | |
838 | if(rc) { | |
839 | FreeXid(xid); | |
840 | return rc; | |
841 | } | |
842 | } | |
843 | default: | |
844 | if(file->private_data == NULL) { | |
845 | rc = -EINVAL; | |
846 | FreeXid(xid); | |
847 | return rc; | |
848 | } | |
849 | cifsFile = file->private_data; | |
850 | if (cifsFile->srch_inf.endOfSearch) { | |
851 | if(cifsFile->srch_inf.emptyDir) { | |
852 | cFYI(1, ("End of search, empty dir")); | |
853 | rc = 0; | |
854 | break; | |
855 | } | |
856 | } /* else { | |
857 | cifsFile->invalidHandle = TRUE; | |
858 | CIFSFindClose(xid, pTcon, cifsFile->netfid); | |
859 | } | |
860 | kfree(cifsFile->search_resume_name); | |
861 | cifsFile->search_resume_name = NULL; */ | |
862 | ||
863 | /* BB account for . and .. in f_pos as special case */ | |
864 | /* dump_cifs_file_struct(file, "rdir after default ");*/ | |
865 | ||
866 | rc = find_cifs_entry(xid,pTcon, file, | |
867 | ¤t_entry,&num_to_fill); | |
868 | if(rc) { | |
869 | cFYI(1,("fce error %d",rc)); | |
870 | goto rddir2_exit; | |
871 | } else if (current_entry != NULL) { | |
872 | cFYI(1,("entry %lld found",file->f_pos)); | |
873 | } else { | |
874 | cFYI(1,("could not find entry")); | |
875 | goto rddir2_exit; | |
876 | } | |
877 | cFYI(1,("loop through %d times filling dir for net buf %p", | |
878 | num_to_fill,cifsFile->srch_inf.ntwrk_buf_start)); | |
879 | end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + | |
880 | smbCalcSize((struct smb_hdr *) | |
881 | cifsFile->srch_inf.ntwrk_buf_start); | |
6a0b4824 SF |
882 | /* To be safe - for UCS to UTF-8 with strings loaded |
883 | with the rare long characters alloc more to account for | |
884 | such multibyte target UTF-8 characters. cifs_unicode.c, | |
885 | which actually does the conversion, has the same limit */ | |
886 | tmp_buf = kmalloc((2 * NAME_MAX) + 4, GFP_KERNEL); | |
1da177e4 LT |
887 | for(i=0;(i<num_to_fill) && (rc == 0);i++) { |
888 | if(current_entry == NULL) { | |
889 | /* evaluate whether this case is an error */ | |
890 | cERROR(1,("past end of SMB num to fill %d i %d", | |
891 | num_to_fill, i)); | |
892 | break; | |
893 | } | |
894 | ||
1da177e4 LT |
895 | rc = cifs_filldir(current_entry, file, |
896 | filldir, direntry,tmp_buf); | |
897 | file->f_pos++; | |
898 | if(file->f_pos == cifsFile->srch_inf.index_of_last_entry) { | |
899 | cFYI(1,("last entry in buf at pos %lld %s", | |
900 | file->f_pos,tmp_buf)); /* BB removeme BB */ | |
901 | cifs_save_resume_key(current_entry,cifsFile); | |
902 | break; | |
903 | } else | |
904 | current_entry = nxt_dir_entry(current_entry,end_of_smb); | |
905 | } | |
906 | kfree(tmp_buf); | |
907 | break; | |
908 | } /* end switch */ | |
909 | ||
910 | rddir2_exit: | |
911 | /* dump_cifs_file_struct(file, "end rdir "); */ | |
912 | FreeXid(xid); | |
913 | return rc; | |
914 | } |