4 * Directory search handling
6 * Copyright (C) International Business Machines Corp., 2004
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/stat.h>
25 #include <linux/smp_lock.h>
28 #include "cifsproto.h"
29 #include "cifs_unicode.h"
30 #include "cifs_debug.h"
31 #include "cifs_fs_sb.h"
33 extern int CIFSFindFirst2(const int xid, struct cifsTconInfo *tcon,
34 const char *searchName, const struct nls_table *nls_codepage,
35 __u16 *searchHandle, struct cifs_search_info * psrch_inf);
37 extern int CIFSFindNext2(const int xid, struct cifsTconInfo *tcon,
38 __u16 searchHandle, struct cifs_search_info * psrch_inf);
40 extern int construct_dentry(struct qstr *qstring, struct file *file,
41 struct inode **ptmp_inode, struct dentry **pnew_dentry);
43 extern void fill_in_inode(struct inode *tmp_inode,
44 FILE_DIRECTORY_INFO * pfindData, int *pobject_type);
46 extern void unix_fill_in_inode(struct inode *tmp_inode,
47 FILE_UNIX_INFO * pfindData, int *pobject_type);
50 /* BB fixme - add debug wrappers around this function to disable it fixme BB */
51 /* static void dump_cifs_file_struct(struct file * file, char * label)
53 struct cifsFileInfo * cf;
56 cf = (struct cifsFileInfo *)file->private_data;
58 cFYI(1,("empty cifs private file data"));
61 if(cf->invalidHandle) {
62 cFYI(1,("invalid handle"));
64 if(cf->srch_inf.endOfSearch) {
65 cFYI(1,("end of search"));
67 if(cf->srch_inf.emptyDir) {
68 cFYI(1,("empty dir"));
74 static int initiate_cifs_search(const int xid, struct file * file)
78 struct cifsFileInfo * cifsFile;
79 struct cifs_sb_info *cifs_sb;
80 struct cifsTconInfo *pTcon;
82 if(file->private_data == NULL) {
84 kmalloc(sizeof(struct cifsFileInfo),GFP_KERNEL);
87 if(file->private_data == NULL) {
90 memset(file->private_data,0,sizeof(struct cifsFileInfo));
92 cifsFile = (struct cifsFileInfo *)file->private_data;
93 cifsFile->invalidHandle = TRUE;
94 cifsFile->srch_inf.endOfSearch = FALSE;
96 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
100 pTcon = cifs_sb->tcon;
104 if(file->f_dentry == NULL)
107 down(&file->f_dentry->d_sb->s_vfs_rename_sem);
108 full_path = build_wildcard_path_from_dentry(file->f_dentry);
109 up(&file->f_dentry->d_sb->s_vfs_rename_sem);
111 if(full_path == NULL) {
115 cFYI(1, ("Full path: %s start at: %lld ", full_path, file->f_pos));
117 /* test for Unix extensions */
118 if (pTcon->ses->capabilities & CAP_UNIX) {
119 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
120 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
121 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
122 } else /* not srvinos - BB fixme add check for backlevel? */ {
123 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
126 rc = CIFSFindFirst2(xid, pTcon,full_path,cifs_sb->local_nls,
127 &cifsFile->netfid, &cifsFile->srch_inf);
129 cifsFile->invalidHandle = FALSE;
135 /* return length of unicode string in bytes */
136 static int cifs_unicode_bytelen(char * str)
139 __le16 * ustr = (__le16 *)str;
141 for(len=0;len <= PATH_MAX;len++) {
145 cFYI(1,("Unicode string longer than PATH_MAX found"));
149 static char * nxt_dir_entry(char * old_entry, char * end_of_smb)
152 FILE_DIRECTORY_INFO * pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
154 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
155 cFYI(1,("new entry %p old entry %p",new_entry,old_entry));
156 /* validate that new_entry is not past end of SMB */
157 if(new_entry >= end_of_smb) {
158 cFYI(1,("search entry %p began after end of SMB %p old entry %p",
159 new_entry,end_of_smb,old_entry));
166 #define UNICODE_DOT cpu_to_le16(0x2e)
168 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
169 static int cifs_entry_is_dot(char * current_entry, struct cifsFileInfo * cfile)
172 char * filename = NULL;
175 if(cfile->srch_inf.info_level == 0x202) {
176 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
177 filename = &pFindData->FileName[0];
178 if(cfile->srch_inf.unicode) {
179 len = cifs_unicode_bytelen(filename);
181 /* BB should we make this strnlen of PATH_MAX? */
182 len = strnlen(filename, 5);
184 } else if(cfile->srch_inf.info_level == 0x101) {
185 FILE_DIRECTORY_INFO * pFindData =
186 (FILE_DIRECTORY_INFO *)current_entry;
187 filename = &pFindData->FileName[0];
188 len = le32_to_cpu(pFindData->FileNameLength);
189 } else if(cfile->srch_inf.info_level == 0x102) {
190 FILE_FULL_DIRECTORY_INFO * pFindData =
191 (FILE_FULL_DIRECTORY_INFO *)current_entry;
192 filename = &pFindData->FileName[0];
193 len = le32_to_cpu(pFindData->FileNameLength);
194 } else if(cfile->srch_inf.info_level == 0x105) {
195 SEARCH_ID_FULL_DIR_INFO * pFindData =
196 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
197 filename = &pFindData->FileName[0];
198 len = le32_to_cpu(pFindData->FileNameLength);
199 } else if(cfile->srch_inf.info_level == 0x104) {
200 FILE_BOTH_DIRECTORY_INFO * pFindData =
201 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
202 filename = &pFindData->FileName[0];
203 len = le32_to_cpu(pFindData->FileNameLength);
205 cFYI(1,("Unknown findfirst level %d",cfile->srch_inf.info_level));
209 if(cfile->srch_inf.unicode) {
210 __le16 *ufilename = (__le16 *)filename;
213 if(ufilename[0] == UNICODE_DOT)
215 } else if(len == 4) {
217 if((ufilename[0] == UNICODE_DOT)
218 &&(ufilename[1] == UNICODE_DOT))
223 if(filename[0] == '.')
225 } else if(len == 2) {
226 if((filename[0] == '.') && (filename[1] == '.'))
235 /* find the corresponding entry in the search */
236 /* Note that the SMB server returns search entries for . and .. which
237 complicates logic here if we choose to parse for them and we do not
238 assume that they are located in the findfirst return buffer.*/
239 /* We start counting in the buffer with entry 2 and increment for every
240 entry (do not increment for . or .. entry) */
241 static int find_cifs_entry(const int xid, struct cifsTconInfo * pTcon,
242 struct file * file, char ** ppCurrentEntry,int * num_to_ret)
246 loff_t first_entry_in_buffer;
247 loff_t index_to_find = file->f_pos;
248 struct cifsFileInfo * cifsFile = (struct cifsFileInfo *)file->private_data;
249 /* check if index in the buffer */
251 if((cifsFile == NULL) || (ppCurrentEntry == NULL) || (num_to_ret == NULL))
254 *ppCurrentEntry = NULL;
255 first_entry_in_buffer =
256 cifsFile->srch_inf.index_of_last_entry -
257 cifsFile->srch_inf.entries_in_buffer;
258 /* dump_cifs_file_struct(file, "In fce ");*/
259 if(index_to_find < first_entry_in_buffer) {
260 /* close and restart search */
261 cFYI(1,("search backing up - close and restart search"));
262 cifsFile->invalidHandle = TRUE;
263 CIFSFindClose(xid, pTcon, cifsFile->netfid);
264 if(cifsFile->search_resume_name) {
265 kfree(cifsFile->search_resume_name);
266 cifsFile->search_resume_name = NULL;
268 if(cifsFile->srch_inf.ntwrk_buf_start) {
269 cFYI(1,("freeing SMB ff cache buf on search rewind"));
270 cifs_buf_release(cifsFile->srch_inf.ntwrk_buf_start);
272 rc = initiate_cifs_search(xid,file);
274 cFYI(1,("error %d reinitiating a search on rewind",rc));
279 while((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
280 (rc == 0) && (cifsFile->srch_inf.endOfSearch == FALSE)){
281 cFYI(1,("calling findnext2"));
282 rc = CIFSFindNext2(xid,pTcon,cifsFile->netfid, &cifsFile->srch_inf);
286 if(index_to_find < cifsFile->srch_inf.index_of_last_entry) {
287 /* we found the buffer that contains the entry */
288 /* scan and find it */
290 char * current_entry;
291 char * end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
292 smbCalcSize((struct smb_hdr *)cifsFile->srch_inf.ntwrk_buf_start);
293 /* dump_cifs_file_struct(file,"found entry in fce "); */
294 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry -
295 cifsFile->srch_inf.entries_in_buffer;
296 pos_in_buf = index_to_find - first_entry_in_buffer;
297 cFYI(1,("found entry - pos_in_buf %d",pos_in_buf));
298 current_entry = cifsFile->srch_inf.srch_entries_start;
299 for(i=0;(i<(pos_in_buf)) && (current_entry != NULL);i++) {
300 /* go entry to next entry figuring out which we need to start with */
303 rc = cifs_entry_is_dot(current_entry,cifsFile);
304 if(rc == 1) /* is . or .. so skip */ {
305 cFYI(1,("Entry is .")); /* BB removeme BB */
307 } else if (rc == 2 ) {
308 cFYI(1,("Entry is ..")); /* BB removeme BB */
311 current_entry = nxt_dir_entry(current_entry,end_of_smb);
313 if((current_entry == NULL) && (i < pos_in_buf)) {
314 cERROR(1,("reached end of buf searching for pos in buf %d index to find %lld rc %d",pos_in_buf,index_to_find,rc)); /* BB removeme BB */
317 *ppCurrentEntry = current_entry;
319 cFYI(1,("index not in buffer - could not findnext into it"));
323 if(pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
324 cFYI(1,("can not return entries when pos_in_buf beyond last entry"));
327 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
328 /* dump_cifs_file_struct(file, "end fce ");*/
333 /* inode num, inode type and filename returned */
334 static int cifs_get_name_from_search_buf(struct qstr * pqst,char * current_entry,
335 __u16 level,unsigned int unicode,struct nls_table * nlt,
339 unsigned int len = 0;
344 if(level == SMB_FIND_FILE_UNIX) {
345 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
347 filename = &pFindData->FileName[0];
349 len = cifs_unicode_bytelen(filename);
351 /* BB should we make this strnlen of PATH_MAX? */
352 len = strnlen(filename, PATH_MAX);
355 /* BB fixme - hash low and high 32 bits if not 64 bit arch BB fixme */
356 *pinum = pFindData->UniqueId;
357 } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
358 FILE_DIRECTORY_INFO * pFindData =
359 (FILE_DIRECTORY_INFO *)current_entry;
360 filename = &pFindData->FileName[0];
361 len = le32_to_cpu(pFindData->FileNameLength);
362 } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
363 FILE_FULL_DIRECTORY_INFO * pFindData =
364 (FILE_FULL_DIRECTORY_INFO *)current_entry;
365 filename = &pFindData->FileName[0];
366 len = le32_to_cpu(pFindData->FileNameLength);
367 } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
368 SEARCH_ID_FULL_DIR_INFO * pFindData =
369 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
370 filename = &pFindData->FileName[0];
371 len = le32_to_cpu(pFindData->FileNameLength);
372 *pinum = pFindData->UniqueId;
373 } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
374 FILE_BOTH_DIRECTORY_INFO * pFindData =
375 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
376 filename = &pFindData->FileName[0];
377 len = le32_to_cpu(pFindData->FileNameLength);
379 cFYI(1,("Unknown findfirst level %d",level));
383 /* BB fixme - test with long names */
384 /* Note converted filename can be longer than in unicode */
385 pqst->len = cifs_strfromUCS_le((char *)pqst->name,(wchar_t *)filename,len/2,nlt);
387 pqst->name = filename;
390 pqst->hash = full_name_hash(pqst->name,pqst->len);
391 /* cFYI(1,("filldir on %s",pqst->name)); */
397 cifs_filldir2(char * pfindEntry, struct file *file,
398 filldir_t filldir, void *direntry,char * scratch_buf)
402 struct cifsFileInfo * pCifsF;
405 struct cifs_sb_info * cifs_sb;
406 struct inode *tmp_inode;
407 struct dentry *tmp_dentry;
409 /* get filename and len into qstring */
411 /* decide whether to create and populate ionde */
412 if((direntry == NULL) || (file == NULL))
415 pCifsF = file->private_data;
417 if((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
420 if(file->f_dentry == NULL)
423 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
425 qstring.name = scratch_buf;
426 rc = cifs_get_name_from_search_buf(&qstring,pfindEntry,
427 pCifsF->srch_inf.info_level,
428 pCifsF->srch_inf.unicode,cifs_sb->local_nls,
429 &inum /* returned */);
434 rc = construct_dentry(&qstring,file,&tmp_inode, &tmp_dentry);
435 if((tmp_inode == NULL) || (tmp_dentry == NULL))
439 /* inode created, we need to hash it with right inode number */
441 /* BB fixme - hash the 2 32 quantities bits together if necessary BB */
442 tmp_inode->i_ino = inum;
444 insert_inode_hash(tmp_inode);
447 if(pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
448 unix_fill_in_inode(tmp_inode,(FILE_UNIX_INFO *)pfindEntry,&obj_type);
450 fill_in_inode(tmp_inode,(FILE_DIRECTORY_INFO *)pfindEntry,&obj_type);
453 rc = filldir(direntry,qstring.name,qstring.len,file->f_pos,tmp_inode->i_ino,obj_type);
455 cFYI(1,("filldir rc = %d",rc));
462 int cifs_save_resume_key(const char * current_entry,struct cifsFileInfo * cifsFile)
465 unsigned int len = 0;
469 if((cifsFile == NULL) || (current_entry == NULL))
472 level = cifsFile->srch_inf.info_level;
474 if(level == SMB_FIND_FILE_UNIX) {
475 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
477 filename = &pFindData->FileName[0];
478 if(cifsFile->srch_inf.unicode) {
479 len = cifs_unicode_bytelen(filename);
481 /* BB should we make this strnlen of PATH_MAX? */
482 len = strnlen(filename, PATH_MAX);
484 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
485 } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
486 FILE_DIRECTORY_INFO * pFindData =
487 (FILE_DIRECTORY_INFO *)current_entry;
488 filename = &pFindData->FileName[0];
489 len = le32_to_cpu(pFindData->FileNameLength);
490 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
491 } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
492 FILE_FULL_DIRECTORY_INFO * pFindData =
493 (FILE_FULL_DIRECTORY_INFO *)current_entry;
494 filename = &pFindData->FileName[0];
495 len = le32_to_cpu(pFindData->FileNameLength);
496 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
497 } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
498 SEARCH_ID_FULL_DIR_INFO * pFindData =
499 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
500 filename = &pFindData->FileName[0];
501 len = le32_to_cpu(pFindData->FileNameLength);
502 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
503 } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
504 FILE_BOTH_DIRECTORY_INFO * pFindData =
505 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
506 filename = &pFindData->FileName[0];
507 len = le32_to_cpu(pFindData->FileNameLength);
508 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
510 cFYI(1,("Unknown findfirst level %d",level));
513 cifsFile->srch_inf.resume_name_len = len;
514 cifsFile->srch_inf.presume_name = filename;
518 int cifs_readdir2(struct file *file, void *direntry, filldir_t filldir)
522 struct cifs_sb_info *cifs_sb;
523 struct cifsTconInfo *pTcon;
524 struct cifsFileInfo *cifsFile = NULL;
525 char * current_entry;
527 char * tmp_buf = NULL;
532 if(file->f_dentry == NULL) {
536 /* dump_cifs_file_struct(file, "Begin rdir "); */
538 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
539 pTcon = cifs_sb->tcon;
543 /* cFYI(1,("readdir2 pos: %lld",file->f_pos)); */
545 switch ((int) file->f_pos) {
547 /*if (filldir(direntry, ".", 1, file->f_pos,
548 file->f_dentry->d_inode->i_ino, DT_DIR) < 0) {
549 cERROR(1, ("Filldir for current dir failed "));
555 /* if (filldir(direntry, "..", 2, file->f_pos,
556 file->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
557 cERROR(1, ("Filldir for parent dir failed "));
563 /* 1) If search is active,
564 is in current search buffer?
565 if it before then restart search
566 if after then keep searching till find it */
568 if(file->private_data == NULL) {
569 rc = initiate_cifs_search(xid,file);
570 cFYI(1,("initiate cifs search rc %d",rc));
577 if(file->private_data == NULL) {
582 cifsFile = (struct cifsFileInfo *) file->private_data;
583 if (cifsFile->srch_inf.endOfSearch) {
584 if(cifsFile->srch_inf.emptyDir) {
585 cFYI(1, ("End of search, empty dir"));
590 cifsFile->invalidHandle = TRUE;
591 CIFSFindClose(xid, pTcon, cifsFile->netfid);
593 if(cifsFile->search_resume_name) {
594 kfree(cifsFile->search_resume_name);
595 cifsFile->search_resume_name = NULL;
597 /* BB account for . and .. in f_pos */
598 /* dump_cifs_file_struct(file, "rdir after default ");*/
600 rc = find_cifs_entry(xid,pTcon, file,
601 ¤t_entry,&num_to_fill);
603 cFYI(1,("fce error %d",rc));
605 } else if (current_entry != NULL) {
606 cFYI(1,("entry %lld found",file->f_pos));
608 cFYI(1,("could not find entry"));
611 cFYI(1,("loop through %d times filling dir for net buf %p",
612 num_to_fill,cifsFile->srch_inf.ntwrk_buf_start));
613 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
614 smbCalcSize((struct smb_hdr *)cifsFile->srch_inf.ntwrk_buf_start);
615 tmp_buf = kmalloc(NAME_MAX+1,GFP_KERNEL);
616 for(i=0;(i<num_to_fill) && (rc == 0);i++) {
617 if(current_entry == NULL) {
618 cERROR(1,("beyond end of smb with num to fill %d i %d",num_to_fill,i)); /* BB removeme BB */
621 /* if((!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) ||
622 (cifsFile->srch_inf.info_level != something that supports server inodes)) {
625 fill in inode new_inode (which makes number locally)
627 also create local inode for per reasons unless new mount parm says otherwise */
628 rc = cifs_filldir2(current_entry, file,
629 filldir, direntry,tmp_buf);
631 if(file->f_pos == cifsFile->srch_inf.index_of_last_entry) {
632 cFYI(1,("last entry in buf at pos %lld %s",file->f_pos,tmp_buf)); /* BB removeme BB */
633 cifs_save_resume_key(current_entry,cifsFile);
636 current_entry = nxt_dir_entry(current_entry,end_of_smb);
644 /* dump_cifs_file_struct(file, "end rdir "); */