4 * Directory search handling
6 * Copyright (C) International Business Machines Corp., 2004, 2005
7 * Author(s): Steve French (sfrench@us.ibm.com)
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.
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.
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
24 #include <linux/pagemap.h>
25 #include <linux/stat.h>
26 #include <linux/smp_lock.h>
29 #include "cifsproto.h"
30 #include "cifs_unicode.h"
31 #include "cifs_debug.h"
32 #include "cifs_fs_sb.h"
35 #ifdef CONFIG_CIFS_DEBUG2
36 static void dump_cifs_file_struct(struct file *file, char *label)
38 struct cifsFileInfo * cf;
41 cf = file->private_data;
43 cFYI(1,("empty cifs private file data"));
46 if(cf->invalidHandle) {
47 cFYI(1,("invalid handle"));
49 if(cf->srch_inf.endOfSearch) {
50 cFYI(1,("end of search"));
52 if(cf->srch_inf.emptyDir) {
53 cFYI(1,("empty dir"));
60 /* Returns one if new inode created (which therefore needs to be hashed) */
61 /* Might check in the future if inode number changed so we can rehash inode */
62 static int construct_dentry(struct qstr *qstring, struct file *file,
63 struct inode **ptmp_inode, struct dentry **pnew_dentry)
65 struct dentry *tmp_dentry;
66 struct cifs_sb_info *cifs_sb;
67 struct cifsTconInfo *pTcon;
70 cFYI(1, ("For %s", qstring->name));
71 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
72 pTcon = cifs_sb->tcon;
74 qstring->hash = full_name_hash(qstring->name, qstring->len);
75 tmp_dentry = d_lookup(file->f_dentry, qstring);
77 cFYI(0, ("existing dentry with inode 0x%p", tmp_dentry->d_inode));
78 *ptmp_inode = tmp_dentry->d_inode;
79 /* BB overwrite old name? i.e. tmp_dentry->d_name and tmp_dentry->d_name.len??*/
80 if(*ptmp_inode == NULL) {
81 *ptmp_inode = new_inode(file->f_dentry->d_sb);
82 if(*ptmp_inode == NULL)
85 d_instantiate(tmp_dentry, *ptmp_inode);
88 tmp_dentry = d_alloc(file->f_dentry, qstring);
89 if(tmp_dentry == NULL) {
90 cERROR(1,("Failed allocating dentry"));
95 *ptmp_inode = new_inode(file->f_dentry->d_sb);
97 tmp_dentry->d_op = &cifs_ci_dentry_ops;
99 tmp_dentry->d_op = &cifs_dentry_ops;
100 if(*ptmp_inode == NULL)
103 d_instantiate(tmp_dentry, *ptmp_inode);
104 d_rehash(tmp_dentry);
107 tmp_dentry->d_time = jiffies;
108 *pnew_dentry = tmp_dentry;
112 static void fill_in_inode(struct inode *tmp_inode, int new_buf_type,
113 char * buf, int *pobject_type, int isNewInode)
116 struct timespec local_mtime;
118 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
119 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
121 __u64 allocation_size;
124 /* save mtime and size */
125 local_mtime = tmp_inode->i_mtime;
126 local_size = tmp_inode->i_size;
129 FILE_DIRECTORY_INFO *pfindData = (FILE_DIRECTORY_INFO *)buf;
131 attr = le32_to_cpu(pfindData->ExtFileAttributes);
132 allocation_size = le64_to_cpu(pfindData->AllocationSize);
133 end_of_file = le64_to_cpu(pfindData->EndOfFile);
135 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
137 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
139 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
140 } else { /* legacy, OS2 and DOS style */
141 FIND_FILE_STANDARD_INFO * pfindData =
142 (FIND_FILE_STANDARD_INFO *)buf;
144 attr = le16_to_cpu(pfindData->Attributes);
145 allocation_size = le32_to_cpu(pfindData->AllocationSize);
146 end_of_file = le32_to_cpu(pfindData->DataSize);
147 tmp_inode->i_atime = CURRENT_TIME;
148 /* tmp_inode->i_mtime = BB FIXME - add dos time handling
149 tmp_inode->i_ctime = 0; BB FIXME */
153 /* Linux can not store file creation time unfortunately so ignore it */
155 cifsInfo->cifsAttrs = attr;
156 cifsInfo->time = jiffies;
158 /* treat dos attribute of read-only as read-only mode bit e.g. 555? */
159 /* 2767 perms - indicate mandatory locking */
160 /* BB fill in uid and gid here? with help from winbind?
161 or retrieve from NTFS stream extended attribute */
162 if (atomic_read(&cifsInfo->inUse) == 0) {
163 tmp_inode->i_uid = cifs_sb->mnt_uid;
164 tmp_inode->i_gid = cifs_sb->mnt_gid;
165 /* set default mode. will override for dirs below */
166 tmp_inode->i_mode = cifs_sb->mnt_file_mode;
168 /* mask off the type bits since it gets set
169 below and we do not want to get two type
171 tmp_inode->i_mode &= ~S_IFMT;
174 if (attr & ATTR_DIRECTORY) {
175 *pobject_type = DT_DIR;
176 /* override default perms since we do not lock dirs */
177 if(atomic_read(&cifsInfo->inUse) == 0) {
178 tmp_inode->i_mode = cifs_sb->mnt_dir_mode;
180 tmp_inode->i_mode |= S_IFDIR;
181 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
182 (attr & ATTR_SYSTEM)) {
183 if (end_of_file == 0) {
184 *pobject_type = DT_FIFO;
185 tmp_inode->i_mode |= S_IFIFO;
187 /* rather than get the type here, we mark the
188 inode as needing revalidate and get the real type
189 (blk vs chr vs. symlink) later ie in lookup */
190 *pobject_type = DT_REG;
191 tmp_inode->i_mode |= S_IFREG;
194 /* we no longer mark these because we could not follow them */
195 /* } else if (attr & ATTR_REPARSE) {
196 *pobject_type = DT_LNK;
197 tmp_inode->i_mode |= S_IFLNK; */
199 *pobject_type = DT_REG;
200 tmp_inode->i_mode |= S_IFREG;
201 if (attr & ATTR_READONLY)
202 tmp_inode->i_mode &= ~(S_IWUGO);
203 } /* could add code here - to validate if device or weird share type? */
205 /* can not fill in nlink here as in qpathinfo version and Unx search */
206 if (atomic_read(&cifsInfo->inUse) == 0) {
207 atomic_set(&cifsInfo->inUse, 1);
210 if (is_size_safe_to_change(cifsInfo)) {
211 /* can not safely change the file size here if the
212 client is writing to it due to potential races */
213 i_size_write(tmp_inode, end_of_file);
215 /* 512 bytes (2**9) is the fake blocksize that must be used */
216 /* for this calculation, even though the reported blocksize is larger */
217 tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9;
220 if (allocation_size < end_of_file)
221 cFYI(1, ("May be sparse file, allocation less than file size"));
222 cFYI(1, ("File Size %ld and blocks %llu and blocksize %ld",
223 (unsigned long)tmp_inode->i_size,
224 (unsigned long long)tmp_inode->i_blocks,
225 tmp_inode->i_blksize));
226 if (S_ISREG(tmp_inode->i_mode)) {
227 cFYI(1, ("File inode"));
228 tmp_inode->i_op = &cifs_file_inode_ops;
229 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
230 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
231 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
233 tmp_inode->i_fop = &cifs_file_direct_ops;
235 } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
236 tmp_inode->i_fop = &cifs_file_nobrl_ops;
238 tmp_inode->i_fop = &cifs_file_ops;
240 if((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
241 (cifs_sb->tcon->ses->server->maxBuf <
242 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
243 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
245 tmp_inode->i_data.a_ops = &cifs_addr_ops;
248 return; /* No sense invalidating pages for new inode
249 since have not started caching readahead file
252 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
253 (local_size == tmp_inode->i_size)) {
254 cFYI(1, ("inode exists but unchanged"));
256 /* file may have changed on server */
257 cFYI(1, ("invalidate inode, readdir detected change"));
258 invalidate_remote_inode(tmp_inode);
260 } else if (S_ISDIR(tmp_inode->i_mode)) {
261 cFYI(1, ("Directory inode"));
262 tmp_inode->i_op = &cifs_dir_inode_ops;
263 tmp_inode->i_fop = &cifs_dir_ops;
264 } else if (S_ISLNK(tmp_inode->i_mode)) {
265 cFYI(1, ("Symbolic Link inode"));
266 tmp_inode->i_op = &cifs_symlink_inode_ops;
268 cFYI(1, ("Init special inode"));
269 init_special_inode(tmp_inode, tmp_inode->i_mode,
274 static void unix_fill_in_inode(struct inode *tmp_inode,
275 FILE_UNIX_INFO *pfindData, int *pobject_type, int isNewInode)
278 struct timespec local_mtime;
280 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
281 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
283 __u32 type = le32_to_cpu(pfindData->Type);
284 __u64 num_of_bytes = le64_to_cpu(pfindData->NumOfBytes);
285 __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
286 cifsInfo->time = jiffies;
287 atomic_inc(&cifsInfo->inUse);
289 /* save mtime and size */
290 local_mtime = tmp_inode->i_mtime;
291 local_size = tmp_inode->i_size;
294 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
296 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastModificationTime));
298 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastStatusChange));
300 tmp_inode->i_mode = le64_to_cpu(pfindData->Permissions);
301 /* since we set the inode type below we need to mask off type
302 to avoid strange results if bits above were corrupt */
303 tmp_inode->i_mode &= ~S_IFMT;
304 if (type == UNIX_FILE) {
305 *pobject_type = DT_REG;
306 tmp_inode->i_mode |= S_IFREG;
307 } else if (type == UNIX_SYMLINK) {
308 *pobject_type = DT_LNK;
309 tmp_inode->i_mode |= S_IFLNK;
310 } else if (type == UNIX_DIR) {
311 *pobject_type = DT_DIR;
312 tmp_inode->i_mode |= S_IFDIR;
313 } else if (type == UNIX_CHARDEV) {
314 *pobject_type = DT_CHR;
315 tmp_inode->i_mode |= S_IFCHR;
316 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
317 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
318 } else if (type == UNIX_BLOCKDEV) {
319 *pobject_type = DT_BLK;
320 tmp_inode->i_mode |= S_IFBLK;
321 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
322 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
323 } else if (type == UNIX_FIFO) {
324 *pobject_type = DT_FIFO;
325 tmp_inode->i_mode |= S_IFIFO;
326 } else if (type == UNIX_SOCKET) {
327 *pobject_type = DT_SOCK;
328 tmp_inode->i_mode |= S_IFSOCK;
330 /* safest to just call it a file */
331 *pobject_type = DT_REG;
332 tmp_inode->i_mode |= S_IFREG;
333 cFYI(1,("unknown inode type %d",type));
336 tmp_inode->i_uid = le64_to_cpu(pfindData->Uid);
337 tmp_inode->i_gid = le64_to_cpu(pfindData->Gid);
338 tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks);
340 if (is_size_safe_to_change(cifsInfo)) {
341 /* can not safely change the file size here if the
342 client is writing to it due to potential races */
343 i_size_write(tmp_inode,end_of_file);
345 /* 512 bytes (2**9) is the fake blocksize that must be used */
346 /* for this calculation, not the real blocksize */
347 tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
350 if (S_ISREG(tmp_inode->i_mode)) {
351 cFYI(1, ("File inode"));
352 tmp_inode->i_op = &cifs_file_inode_ops;
354 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
355 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
356 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
358 tmp_inode->i_fop = &cifs_file_direct_ops;
360 } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
361 tmp_inode->i_fop = &cifs_file_nobrl_ops;
363 tmp_inode->i_fop = &cifs_file_ops;
365 if((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
366 (cifs_sb->tcon->ses->server->maxBuf <
367 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
368 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
370 tmp_inode->i_data.a_ops = &cifs_addr_ops;
373 return; /* No sense invalidating pages for new inode since we
374 have not started caching readahead file data yet */
376 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
377 (local_size == tmp_inode->i_size)) {
378 cFYI(1, ("inode exists but unchanged"));
380 /* file may have changed on server */
381 cFYI(1, ("invalidate inode, readdir detected change"));
382 invalidate_remote_inode(tmp_inode);
384 } else if (S_ISDIR(tmp_inode->i_mode)) {
385 cFYI(1, ("Directory inode"));
386 tmp_inode->i_op = &cifs_dir_inode_ops;
387 tmp_inode->i_fop = &cifs_dir_ops;
388 } else if (S_ISLNK(tmp_inode->i_mode)) {
389 cFYI(1, ("Symbolic Link inode"));
390 tmp_inode->i_op = &cifs_symlink_inode_ops;
391 /* tmp_inode->i_fop = *//* do not need to set to anything */
393 cFYI(1, ("Special inode"));
394 init_special_inode(tmp_inode, tmp_inode->i_mode,
399 static int initiate_cifs_search(const int xid, struct file *file)
403 struct cifsFileInfo * cifsFile;
404 struct cifs_sb_info *cifs_sb;
405 struct cifsTconInfo *pTcon;
407 if(file->private_data == NULL) {
409 kmalloc(sizeof(struct cifsFileInfo),GFP_KERNEL);
412 if(file->private_data == NULL) {
415 memset(file->private_data,0,sizeof(struct cifsFileInfo));
417 cifsFile = file->private_data;
418 cifsFile->invalidHandle = TRUE;
419 cifsFile->srch_inf.endOfSearch = FALSE;
421 if(file->f_dentry == NULL)
424 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
428 pTcon = cifs_sb->tcon;
432 full_path = build_path_from_dentry(file->f_dentry);
434 if(full_path == NULL) {
438 cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
441 /* test for Unix extensions */
442 if (pTcon->ses->capabilities & CAP_UNIX) {
443 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
444 } else if ((pTcon->ses->capabilities &
445 (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
446 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
447 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
448 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
449 } else /* not srvinos - BB fixme add check for backlevel? */ {
450 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
453 rc = CIFSFindFirst(xid, pTcon,full_path,cifs_sb->local_nls,
454 &cifsFile->netfid, &cifsFile->srch_inf,
455 cifs_sb->mnt_cifs_flags &
456 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
458 cifsFile->invalidHandle = FALSE;
459 if((rc == -EOPNOTSUPP) &&
460 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
461 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
468 /* return length of unicode string in bytes */
469 static int cifs_unicode_bytelen(char *str)
472 __le16 * ustr = (__le16 *)str;
474 for(len=0;len <= PATH_MAX;len++) {
478 cFYI(1,("Unicode string longer than PATH_MAX found"));
482 static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
485 FILE_DIRECTORY_INFO * pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
487 if(level == SMB_FIND_FILE_INFO_STANDARD) {
488 FIND_FILE_STANDARD_INFO * pfData;
489 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
491 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
492 pfData->FileNameLength;
494 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
495 cFYI(1,("new entry %p old entry %p",new_entry,old_entry));
496 /* validate that new_entry is not past end of SMB */
497 if(new_entry >= end_of_smb) {
499 ("search entry %p began after end of SMB %p old entry %p",
500 new_entry, end_of_smb, old_entry));
502 } else if(((level == SMB_FIND_FILE_INFO_STANDARD) &&
503 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb)) ||
504 ((level != SMB_FIND_FILE_INFO_STANDARD) &&
505 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
506 cERROR(1,("search entry %p extends after end of SMB %p",
507 new_entry, end_of_smb));
514 #define UNICODE_DOT cpu_to_le16(0x2e)
516 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
517 static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
520 char * filename = NULL;
523 if(cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
524 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
525 filename = &pFindData->FileName[0];
526 if(cfile->srch_inf.unicode) {
527 len = cifs_unicode_bytelen(filename);
529 /* BB should we make this strnlen of PATH_MAX? */
530 len = strnlen(filename, 5);
532 } else if(cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
533 FILE_DIRECTORY_INFO * pFindData =
534 (FILE_DIRECTORY_INFO *)current_entry;
535 filename = &pFindData->FileName[0];
536 len = le32_to_cpu(pFindData->FileNameLength);
537 } else if(cfile->srch_inf.info_level ==
538 SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
539 FILE_FULL_DIRECTORY_INFO * pFindData =
540 (FILE_FULL_DIRECTORY_INFO *)current_entry;
541 filename = &pFindData->FileName[0];
542 len = le32_to_cpu(pFindData->FileNameLength);
543 } else if(cfile->srch_inf.info_level ==
544 SMB_FIND_FILE_ID_FULL_DIR_INFO) {
545 SEARCH_ID_FULL_DIR_INFO * pFindData =
546 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
547 filename = &pFindData->FileName[0];
548 len = le32_to_cpu(pFindData->FileNameLength);
549 } else if(cfile->srch_inf.info_level ==
550 SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
551 FILE_BOTH_DIRECTORY_INFO * pFindData =
552 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
553 filename = &pFindData->FileName[0];
554 len = le32_to_cpu(pFindData->FileNameLength);
555 } else if(cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
556 FIND_FILE_STANDARD_INFO * pFindData =
557 (FIND_FILE_STANDARD_INFO *)current_entry;
558 filename = &pFindData->FileName[0];
559 len = le32_to_cpu(pFindData->FileNameLength);
561 cFYI(1,("Unknown findfirst level %d",cfile->srch_inf.info_level));
565 if(cfile->srch_inf.unicode) {
566 __le16 *ufilename = (__le16 *)filename;
569 if(ufilename[0] == UNICODE_DOT)
571 } else if(len == 4) {
573 if((ufilename[0] == UNICODE_DOT)
574 &&(ufilename[1] == UNICODE_DOT))
579 if(filename[0] == '.')
581 } else if(len == 2) {
582 if((filename[0] == '.') && (filename[1] == '.'))
591 /* Check if directory that we are searching has changed so we can decide
592 whether we can use the cached search results from the previous search */
593 static int is_dir_changed(struct file * file)
595 struct inode * inode;
596 struct cifsInodeInfo *cifsInfo;
598 if(file->f_dentry == NULL)
601 inode = file->f_dentry->d_inode;
606 cifsInfo = CIFS_I(inode);
608 if(cifsInfo->time == 0)
609 return 1; /* directory was changed, perhaps due to unlink */
615 /* find the corresponding entry in the search */
616 /* Note that the SMB server returns search entries for . and .. which
617 complicates logic here if we choose to parse for them and we do not
618 assume that they are located in the findfirst return buffer.*/
619 /* We start counting in the buffer with entry 2 and increment for every
620 entry (do not increment for . or .. entry) */
621 static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
622 struct file *file, char **ppCurrentEntry, int *num_to_ret)
626 loff_t first_entry_in_buffer;
627 loff_t index_to_find = file->f_pos;
628 struct cifsFileInfo * cifsFile = file->private_data;
629 /* check if index in the buffer */
631 if((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
632 (num_to_ret == NULL))
635 *ppCurrentEntry = NULL;
636 first_entry_in_buffer =
637 cifsFile->srch_inf.index_of_last_entry -
638 cifsFile->srch_inf.entries_in_buffer;
640 /* if first entry in buf is zero then is first buffer
641 in search response data which means it is likely . and ..
642 will be in this buffer, although some servers do not return
643 . and .. for the root of a drive and for those we need
644 to start two entries earlier */
646 #ifdef CONFIG_CIFS_DEBUG2
647 dump_cifs_file_struct(file, "In fce ");
649 if(((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
650 is_dir_changed(file)) ||
651 (index_to_find < first_entry_in_buffer)) {
652 /* close and restart search */
653 cFYI(1,("search backing up - close and restart search"));
654 cifsFile->invalidHandle = TRUE;
655 CIFSFindClose(xid, pTcon, cifsFile->netfid);
656 kfree(cifsFile->search_resume_name);
657 cifsFile->search_resume_name = NULL;
658 if(cifsFile->srch_inf.ntwrk_buf_start) {
659 cFYI(1,("freeing SMB ff cache buf on search rewind"));
660 if(cifsFile->srch_inf.smallBuf)
661 cifs_small_buf_release(cifsFile->srch_inf.
664 cifs_buf_release(cifsFile->srch_inf.
667 rc = initiate_cifs_search(xid,file);
669 cFYI(1,("error %d reinitiating a search on rewind",rc));
674 while((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
675 (rc == 0) && (cifsFile->srch_inf.endOfSearch == FALSE)){
676 cFYI(1,("calling findnext2"));
677 rc = CIFSFindNext(xid,pTcon,cifsFile->netfid,
678 &cifsFile->srch_inf);
682 if(index_to_find < cifsFile->srch_inf.index_of_last_entry) {
683 /* we found the buffer that contains the entry */
684 /* scan and find it */
686 char * current_entry;
687 char * end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
688 smbCalcSize((struct smb_hdr *)
689 cifsFile->srch_inf.ntwrk_buf_start);
691 current_entry = cifsFile->srch_inf.srch_entries_start;
692 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
693 - cifsFile->srch_inf.entries_in_buffer;
694 pos_in_buf = index_to_find - first_entry_in_buffer;
695 cFYI(1,("found entry - pos_in_buf %d",pos_in_buf));
697 for(i=0;(i<(pos_in_buf)) && (current_entry != NULL);i++) {
698 /* go entry by entry figuring out which is first */
699 current_entry = nxt_dir_entry(current_entry,end_of_smb,
700 cifsFile->srch_inf.info_level);
702 if((current_entry == NULL) && (i < pos_in_buf)) {
703 /* BB fixme - check if we should flag this error */
704 cERROR(1,("reached end of buf searching for pos in buf"
705 " %d index to find %lld rc %d",
706 pos_in_buf,index_to_find,rc));
709 *ppCurrentEntry = current_entry;
711 cFYI(1,("index not in buffer - could not findnext into it"));
715 if(pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
716 cFYI(1,("can not return entries pos_in_buf beyond last entry"));
719 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
724 /* inode num, inode type and filename returned */
725 static int cifs_get_name_from_search_buf(struct qstr *pqst,
726 char *current_entry, __u16 level, unsigned int unicode,
727 struct cifs_sb_info * cifs_sb, int max_len, ino_t *pinum)
730 unsigned int len = 0;
732 struct nls_table * nlt = cifs_sb->local_nls;
736 if(level == SMB_FIND_FILE_UNIX) {
737 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
739 filename = &pFindData->FileName[0];
741 len = cifs_unicode_bytelen(filename);
743 /* BB should we make this strnlen of PATH_MAX? */
744 len = strnlen(filename, PATH_MAX);
747 /* BB fixme - hash low and high 32 bits if not 64 bit arch BB fixme */
748 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
749 *pinum = pFindData->UniqueId;
750 } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
751 FILE_DIRECTORY_INFO * pFindData =
752 (FILE_DIRECTORY_INFO *)current_entry;
753 filename = &pFindData->FileName[0];
754 len = le32_to_cpu(pFindData->FileNameLength);
755 } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
756 FILE_FULL_DIRECTORY_INFO * pFindData =
757 (FILE_FULL_DIRECTORY_INFO *)current_entry;
758 filename = &pFindData->FileName[0];
759 len = le32_to_cpu(pFindData->FileNameLength);
760 } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
761 SEARCH_ID_FULL_DIR_INFO * pFindData =
762 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
763 filename = &pFindData->FileName[0];
764 len = le32_to_cpu(pFindData->FileNameLength);
765 *pinum = pFindData->UniqueId;
766 } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
767 FILE_BOTH_DIRECTORY_INFO * pFindData =
768 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
769 filename = &pFindData->FileName[0];
770 len = le32_to_cpu(pFindData->FileNameLength);
771 } else if(level == SMB_FIND_FILE_INFO_STANDARD) {
772 FIND_FILE_STANDARD_INFO * pFindData =
773 (FIND_FILE_STANDARD_INFO *)current_entry;
774 filename = &pFindData->FileName[0];
775 /* one byte length, no name conversion */
776 len = (unsigned int)pFindData->FileNameLength;
778 cFYI(1,("Unknown findfirst level %d",level));
783 cERROR(1,("bad search response length %d past smb end", len));
788 /* BB fixme - test with long names */
789 /* Note converted filename can be longer than in unicode */
790 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
791 pqst->len = cifs_convertUCSpath((char *)pqst->name,
792 (__le16 *)filename, len/2, nlt);
794 pqst->len = cifs_strfromUCS_le((char *)pqst->name,
795 (__le16 *)filename,len/2,nlt);
797 pqst->name = filename;
800 pqst->hash = full_name_hash(pqst->name,pqst->len);
801 /* cFYI(1,("filldir on %s",pqst->name)); */
805 static int cifs_filldir(char *pfindEntry, struct file *file,
806 filldir_t filldir, void *direntry, char *scratch_buf, int max_len)
810 struct cifsFileInfo * pCifsF;
813 struct cifs_sb_info * cifs_sb;
814 struct inode *tmp_inode;
815 struct dentry *tmp_dentry;
817 /* get filename and len into qstring */
819 /* decide whether to create and populate ionde */
820 if((direntry == NULL) || (file == NULL))
823 pCifsF = file->private_data;
825 if((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
828 if(file->f_dentry == NULL)
831 rc = cifs_entry_is_dot(pfindEntry,pCifsF);
832 /* skip . and .. since we added them first */
836 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
838 qstring.name = scratch_buf;
839 rc = cifs_get_name_from_search_buf(&qstring,pfindEntry,
840 pCifsF->srch_inf.info_level,
841 pCifsF->srch_inf.unicode,cifs_sb,
843 &inum /* returned */);
848 rc = construct_dentry(&qstring,file,&tmp_inode, &tmp_dentry);
849 if((tmp_inode == NULL) || (tmp_dentry == NULL))
853 /* inode created, we need to hash it with right inode number */
855 /* BB fixme - hash the 2 32 quantities bits together if necessary BB */
856 tmp_inode->i_ino = inum;
858 insert_inode_hash(tmp_inode);
861 /* we pass in rc below, indicating whether it is a new inode,
862 so we can figure out whether to invalidate the inode cached
863 data if the file has changed */
864 if(pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
865 unix_fill_in_inode(tmp_inode,
866 (FILE_UNIX_INFO *)pfindEntry,
868 else if(pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
869 fill_in_inode(tmp_inode, 0 /* old level 1 buffer type */,
870 pfindEntry, &obj_type, rc);
872 fill_in_inode(tmp_inode, 1 /* NT */, pfindEntry, &obj_type, rc);
875 rc = filldir(direntry,qstring.name,qstring.len,file->f_pos,
876 tmp_inode->i_ino,obj_type);
878 cFYI(1,("filldir rc = %d",rc));
885 static int cifs_save_resume_key(const char *current_entry,
886 struct cifsFileInfo *cifsFile)
889 unsigned int len = 0;
893 if((cifsFile == NULL) || (current_entry == NULL))
896 level = cifsFile->srch_inf.info_level;
898 if(level == SMB_FIND_FILE_UNIX) {
899 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
901 filename = &pFindData->FileName[0];
902 if(cifsFile->srch_inf.unicode) {
903 len = cifs_unicode_bytelen(filename);
905 /* BB should we make this strnlen of PATH_MAX? */
906 len = strnlen(filename, PATH_MAX);
908 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
909 } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
910 FILE_DIRECTORY_INFO * pFindData =
911 (FILE_DIRECTORY_INFO *)current_entry;
912 filename = &pFindData->FileName[0];
913 len = le32_to_cpu(pFindData->FileNameLength);
914 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
915 } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
916 FILE_FULL_DIRECTORY_INFO * pFindData =
917 (FILE_FULL_DIRECTORY_INFO *)current_entry;
918 filename = &pFindData->FileName[0];
919 len = le32_to_cpu(pFindData->FileNameLength);
920 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
921 } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
922 SEARCH_ID_FULL_DIR_INFO * pFindData =
923 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
924 filename = &pFindData->FileName[0];
925 len = le32_to_cpu(pFindData->FileNameLength);
926 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
927 } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
928 FILE_BOTH_DIRECTORY_INFO * pFindData =
929 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
930 filename = &pFindData->FileName[0];
931 len = le32_to_cpu(pFindData->FileNameLength);
932 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
933 } else if(level == SMB_FIND_FILE_INFO_STANDARD) {
934 FIND_FILE_STANDARD_INFO * pFindData =
935 (FIND_FILE_STANDARD_INFO *)current_entry;
936 filename = &pFindData->FileName[0];
937 /* one byte length, no name conversion */
938 len = (unsigned int)pFindData->FileNameLength;
940 cFYI(1,("Unknown findfirst level %d",level));
943 cifsFile->srch_inf.resume_name_len = len;
944 cifsFile->srch_inf.presume_name = filename;
948 int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
952 struct cifs_sb_info *cifs_sb;
953 struct cifsTconInfo *pTcon;
954 struct cifsFileInfo *cifsFile = NULL;
955 char * current_entry;
957 char * tmp_buf = NULL;
963 if(file->f_dentry == NULL) {
968 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
969 pTcon = cifs_sb->tcon;
973 switch ((int) file->f_pos) {
975 if (filldir(direntry, ".", 1, file->f_pos,
976 file->f_dentry->d_inode->i_ino, DT_DIR) < 0) {
977 cERROR(1, ("Filldir for current dir failed"));
983 if (filldir(direntry, "..", 2, file->f_pos,
984 file->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
985 cERROR(1, ("Filldir for parent dir failed"));
991 /* 1) If search is active,
992 is in current search buffer?
993 if it before then restart search
994 if after then keep searching till find it */
996 if(file->private_data == NULL) {
997 rc = initiate_cifs_search(xid,file);
998 cFYI(1,("initiate cifs search rc %d",rc));
1004 if(file->private_data == NULL) {
1009 cifsFile = file->private_data;
1010 if (cifsFile->srch_inf.endOfSearch) {
1011 if(cifsFile->srch_inf.emptyDir) {
1012 cFYI(1, ("End of search, empty dir"));
1017 cifsFile->invalidHandle = TRUE;
1018 CIFSFindClose(xid, pTcon, cifsFile->netfid);
1020 kfree(cifsFile->search_resume_name);
1021 cifsFile->search_resume_name = NULL; */
1023 rc = find_cifs_entry(xid,pTcon, file,
1024 ¤t_entry,&num_to_fill);
1026 cFYI(1,("fce error %d",rc));
1028 } else if (current_entry != NULL) {
1029 cFYI(1,("entry %lld found",file->f_pos));
1031 cFYI(1,("could not find entry"));
1034 cFYI(1,("loop through %d times filling dir for net buf %p",
1035 num_to_fill,cifsFile->srch_inf.ntwrk_buf_start));
1036 max_len = smbCalcSize((struct smb_hdr *)
1037 cifsFile->srch_inf.ntwrk_buf_start);
1038 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
1040 /* To be safe - for UCS to UTF-8 with strings loaded
1041 with the rare long characters alloc more to account for
1042 such multibyte target UTF-8 characters. cifs_unicode.c,
1043 which actually does the conversion, has the same limit */
1044 tmp_buf = kmalloc((2 * NAME_MAX) + 4, GFP_KERNEL);
1045 for(i=0;(i<num_to_fill) && (rc == 0);i++) {
1046 if(current_entry == NULL) {
1047 /* evaluate whether this case is an error */
1048 cERROR(1,("past end of SMB num to fill %d i %d",
1052 /* if buggy server returns . and .. late do
1053 we want to check for that here? */
1054 rc = cifs_filldir(current_entry, file,
1055 filldir, direntry, tmp_buf, max_len);
1058 cifsFile->srch_inf.index_of_last_entry) {
1059 cFYI(1,("last entry in buf at pos %lld %s",
1060 file->f_pos,tmp_buf));
1061 cifs_save_resume_key(current_entry,cifsFile);
1065 nxt_dir_entry(current_entry, end_of_smb,
1066 cifsFile->srch_inf.info_level);