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