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)
87 tmp_dentry = d_alloc(file->f_dentry, qstring);
88 if(tmp_dentry == NULL) {
89 cERROR(1,("Failed allocating dentry"));
94 *ptmp_inode = new_inode(file->f_dentry->d_sb);
96 tmp_dentry->d_op = &cifs_ci_dentry_ops;
98 tmp_dentry->d_op = &cifs_dentry_ops;
99 if(*ptmp_inode == NULL)
104 tmp_dentry->d_time = jiffies;
105 *pnew_dentry = tmp_dentry;
109 static void fill_in_inode(struct inode *tmp_inode, int new_buf_type,
110 char * buf, int *pobject_type, int isNewInode)
113 struct timespec local_mtime;
115 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
116 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
118 __u64 allocation_size;
121 /* save mtime and size */
122 local_mtime = tmp_inode->i_mtime;
123 local_size = tmp_inode->i_size;
126 FILE_DIRECTORY_INFO *pfindData = (FILE_DIRECTORY_INFO *)buf;
128 attr = le32_to_cpu(pfindData->ExtFileAttributes);
129 allocation_size = le64_to_cpu(pfindData->AllocationSize);
130 end_of_file = le64_to_cpu(pfindData->EndOfFile);
132 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
134 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
136 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
137 } else { /* legacy, OS2 and DOS style */
138 /* struct timespec ts;*/
139 FIND_FILE_STANDARD_INFO * pfindData =
140 (FIND_FILE_STANDARD_INFO *)buf;
142 /* ts = cnvrtDosUnixTm(
143 le16_to_cpu(pfindData->LastWriteDate),
144 le16_to_cpu(pfindData->LastWriteTime));*/
145 attr = le16_to_cpu(pfindData->Attributes);
146 allocation_size = le32_to_cpu(pfindData->AllocationSize);
147 end_of_file = le32_to_cpu(pfindData->DataSize);
148 /* do not need to use current_fs_time helper function since
149 time not stored for this case so atime can not "go backwards"
150 by pulling newer older from disk when inode refrenshed */
151 tmp_inode->i_atime = CURRENT_TIME;
152 /* tmp_inode->i_mtime = BB FIXME - add dos time handling
153 tmp_inode->i_ctime = 0; BB FIXME */
157 /* Linux can not store file creation time unfortunately so ignore it */
159 cifsInfo->cifsAttrs = attr;
160 cifsInfo->time = jiffies;
162 /* treat dos attribute of read-only as read-only mode bit e.g. 555? */
163 /* 2767 perms - indicate mandatory locking */
164 /* BB fill in uid and gid here? with help from winbind?
165 or retrieve from NTFS stream extended attribute */
166 if (atomic_read(&cifsInfo->inUse) == 0) {
167 tmp_inode->i_uid = cifs_sb->mnt_uid;
168 tmp_inode->i_gid = cifs_sb->mnt_gid;
169 /* set default mode. will override for dirs below */
170 tmp_inode->i_mode = cifs_sb->mnt_file_mode;
172 /* mask off the type bits since it gets set
173 below and we do not want to get two type
175 tmp_inode->i_mode &= ~S_IFMT;
178 if (attr & ATTR_DIRECTORY) {
179 *pobject_type = DT_DIR;
180 /* override default perms since we do not lock dirs */
181 if(atomic_read(&cifsInfo->inUse) == 0) {
182 tmp_inode->i_mode = cifs_sb->mnt_dir_mode;
184 tmp_inode->i_mode |= S_IFDIR;
185 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
186 (attr & ATTR_SYSTEM)) {
187 if (end_of_file == 0) {
188 *pobject_type = DT_FIFO;
189 tmp_inode->i_mode |= S_IFIFO;
191 /* rather than get the type here, we mark the
192 inode as needing revalidate and get the real type
193 (blk vs chr vs. symlink) later ie in lookup */
194 *pobject_type = DT_REG;
195 tmp_inode->i_mode |= S_IFREG;
198 /* we no longer mark these because we could not follow them */
199 /* } else if (attr & ATTR_REPARSE) {
200 *pobject_type = DT_LNK;
201 tmp_inode->i_mode |= S_IFLNK; */
203 *pobject_type = DT_REG;
204 tmp_inode->i_mode |= S_IFREG;
205 if (attr & ATTR_READONLY)
206 tmp_inode->i_mode &= ~(S_IWUGO);
207 } /* could add code here - to validate if device or weird share type? */
209 /* can not fill in nlink here as in qpathinfo version and Unx search */
210 if (atomic_read(&cifsInfo->inUse) == 0) {
211 atomic_set(&cifsInfo->inUse, 1);
214 if (is_size_safe_to_change(cifsInfo)) {
215 /* can not safely change the file size here if the
216 client is writing to it due to potential races */
217 i_size_write(tmp_inode, end_of_file);
219 /* 512 bytes (2**9) is the fake blocksize that must be used */
220 /* for this calculation, even though the reported blocksize is larger */
221 tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9;
224 if (allocation_size < end_of_file)
225 cFYI(1, ("May be sparse file, allocation less than file size"));
226 cFYI(1, ("File Size %ld and blocks %llu and blocksize %ld",
227 (unsigned long)tmp_inode->i_size,
228 (unsigned long long)tmp_inode->i_blocks,
229 tmp_inode->i_blksize));
230 if (S_ISREG(tmp_inode->i_mode)) {
231 cFYI(1, ("File inode"));
232 tmp_inode->i_op = &cifs_file_inode_ops;
233 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
234 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
235 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
237 tmp_inode->i_fop = &cifs_file_direct_ops;
239 } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
240 tmp_inode->i_fop = &cifs_file_nobrl_ops;
242 tmp_inode->i_fop = &cifs_file_ops;
244 if((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
245 (cifs_sb->tcon->ses->server->maxBuf <
246 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
247 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
249 tmp_inode->i_data.a_ops = &cifs_addr_ops;
252 return; /* No sense invalidating pages for new inode
253 since have not started caching readahead file
256 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
257 (local_size == tmp_inode->i_size)) {
258 cFYI(1, ("inode exists but unchanged"));
260 /* file may have changed on server */
261 cFYI(1, ("invalidate inode, readdir detected change"));
262 invalidate_remote_inode(tmp_inode);
264 } else if (S_ISDIR(tmp_inode->i_mode)) {
265 cFYI(1, ("Directory inode"));
266 tmp_inode->i_op = &cifs_dir_inode_ops;
267 tmp_inode->i_fop = &cifs_dir_ops;
268 } else if (S_ISLNK(tmp_inode->i_mode)) {
269 cFYI(1, ("Symbolic Link inode"));
270 tmp_inode->i_op = &cifs_symlink_inode_ops;
272 cFYI(1, ("Init special inode"));
273 init_special_inode(tmp_inode, tmp_inode->i_mode,
278 static void unix_fill_in_inode(struct inode *tmp_inode,
279 FILE_UNIX_INFO *pfindData, int *pobject_type, int isNewInode)
282 struct timespec local_mtime;
284 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
285 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
287 __u32 type = le32_to_cpu(pfindData->Type);
288 __u64 num_of_bytes = le64_to_cpu(pfindData->NumOfBytes);
289 __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
290 cifsInfo->time = jiffies;
291 atomic_inc(&cifsInfo->inUse);
293 /* save mtime and size */
294 local_mtime = tmp_inode->i_mtime;
295 local_size = tmp_inode->i_size;
298 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
300 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastModificationTime));
302 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastStatusChange));
304 tmp_inode->i_mode = le64_to_cpu(pfindData->Permissions);
305 /* since we set the inode type below we need to mask off type
306 to avoid strange results if bits above were corrupt */
307 tmp_inode->i_mode &= ~S_IFMT;
308 if (type == UNIX_FILE) {
309 *pobject_type = DT_REG;
310 tmp_inode->i_mode |= S_IFREG;
311 } else if (type == UNIX_SYMLINK) {
312 *pobject_type = DT_LNK;
313 tmp_inode->i_mode |= S_IFLNK;
314 } else if (type == UNIX_DIR) {
315 *pobject_type = DT_DIR;
316 tmp_inode->i_mode |= S_IFDIR;
317 } else if (type == UNIX_CHARDEV) {
318 *pobject_type = DT_CHR;
319 tmp_inode->i_mode |= S_IFCHR;
320 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
321 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
322 } else if (type == UNIX_BLOCKDEV) {
323 *pobject_type = DT_BLK;
324 tmp_inode->i_mode |= S_IFBLK;
325 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
326 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
327 } else if (type == UNIX_FIFO) {
328 *pobject_type = DT_FIFO;
329 tmp_inode->i_mode |= S_IFIFO;
330 } else if (type == UNIX_SOCKET) {
331 *pobject_type = DT_SOCK;
332 tmp_inode->i_mode |= S_IFSOCK;
334 /* safest to just call it a file */
335 *pobject_type = DT_REG;
336 tmp_inode->i_mode |= S_IFREG;
337 cFYI(1,("unknown inode type %d",type));
340 tmp_inode->i_uid = le64_to_cpu(pfindData->Uid);
341 tmp_inode->i_gid = le64_to_cpu(pfindData->Gid);
342 tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks);
344 if (is_size_safe_to_change(cifsInfo)) {
345 /* can not safely change the file size here if the
346 client is writing to it due to potential races */
347 i_size_write(tmp_inode,end_of_file);
349 /* 512 bytes (2**9) is the fake blocksize that must be used */
350 /* for this calculation, not the real blocksize */
351 tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
354 if (S_ISREG(tmp_inode->i_mode)) {
355 cFYI(1, ("File inode"));
356 tmp_inode->i_op = &cifs_file_inode_ops;
358 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
359 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
360 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
362 tmp_inode->i_fop = &cifs_file_direct_ops;
364 } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
365 tmp_inode->i_fop = &cifs_file_nobrl_ops;
367 tmp_inode->i_fop = &cifs_file_ops;
369 if((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
370 (cifs_sb->tcon->ses->server->maxBuf <
371 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
372 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
374 tmp_inode->i_data.a_ops = &cifs_addr_ops;
377 return; /* No sense invalidating pages for new inode since we
378 have not started caching readahead file data yet */
380 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
381 (local_size == tmp_inode->i_size)) {
382 cFYI(1, ("inode exists but unchanged"));
384 /* file may have changed on server */
385 cFYI(1, ("invalidate inode, readdir detected change"));
386 invalidate_remote_inode(tmp_inode);
388 } else if (S_ISDIR(tmp_inode->i_mode)) {
389 cFYI(1, ("Directory inode"));
390 tmp_inode->i_op = &cifs_dir_inode_ops;
391 tmp_inode->i_fop = &cifs_dir_ops;
392 } else if (S_ISLNK(tmp_inode->i_mode)) {
393 cFYI(1, ("Symbolic Link inode"));
394 tmp_inode->i_op = &cifs_symlink_inode_ops;
395 /* tmp_inode->i_fop = *//* do not need to set to anything */
397 cFYI(1, ("Special inode"));
398 init_special_inode(tmp_inode, tmp_inode->i_mode,
403 static int initiate_cifs_search(const int xid, struct file *file)
407 struct cifsFileInfo * cifsFile;
408 struct cifs_sb_info *cifs_sb;
409 struct cifsTconInfo *pTcon;
411 if(file->private_data == NULL) {
413 kmalloc(sizeof(struct cifsFileInfo),GFP_KERNEL);
416 if(file->private_data == NULL) {
419 memset(file->private_data,0,sizeof(struct cifsFileInfo));
421 cifsFile = file->private_data;
422 cifsFile->invalidHandle = TRUE;
423 cifsFile->srch_inf.endOfSearch = FALSE;
425 if(file->f_dentry == NULL)
428 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
432 pTcon = cifs_sb->tcon;
436 full_path = build_path_from_dentry(file->f_dentry);
438 if(full_path == NULL) {
442 cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
445 /* test for Unix extensions */
446 if (pTcon->ses->capabilities & CAP_UNIX) {
447 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
448 } else if ((pTcon->ses->capabilities &
449 (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
450 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
451 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
452 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
453 } else /* not srvinos - BB fixme add check for backlevel? */ {
454 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
457 rc = CIFSFindFirst(xid, pTcon,full_path,cifs_sb->local_nls,
458 &cifsFile->netfid, &cifsFile->srch_inf,
459 cifs_sb->mnt_cifs_flags &
460 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
462 cifsFile->invalidHandle = FALSE;
463 if((rc == -EOPNOTSUPP) &&
464 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
465 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
472 /* return length of unicode string in bytes */
473 static int cifs_unicode_bytelen(char *str)
476 __le16 * ustr = (__le16 *)str;
478 for(len=0;len <= PATH_MAX;len++) {
482 cFYI(1,("Unicode string longer than PATH_MAX found"));
486 static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
489 FILE_DIRECTORY_INFO * pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
491 if(level == SMB_FIND_FILE_INFO_STANDARD) {
492 FIND_FILE_STANDARD_INFO * pfData;
493 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
495 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
496 pfData->FileNameLength;
498 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
499 cFYI(1,("new entry %p old entry %p",new_entry,old_entry));
500 /* validate that new_entry is not past end of SMB */
501 if(new_entry >= end_of_smb) {
503 ("search entry %p began after end of SMB %p old entry %p",
504 new_entry, end_of_smb, old_entry));
506 } else if(((level == SMB_FIND_FILE_INFO_STANDARD) &&
507 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb)) ||
508 ((level != SMB_FIND_FILE_INFO_STANDARD) &&
509 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
510 cERROR(1,("search entry %p extends after end of SMB %p",
511 new_entry, end_of_smb));
518 #define UNICODE_DOT cpu_to_le16(0x2e)
520 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
521 static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
524 char * filename = NULL;
527 if(cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
528 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
529 filename = &pFindData->FileName[0];
530 if(cfile->srch_inf.unicode) {
531 len = cifs_unicode_bytelen(filename);
533 /* BB should we make this strnlen of PATH_MAX? */
534 len = strnlen(filename, 5);
536 } else if(cfile->srch_inf.info_level == SMB_FIND_FILE_DIRECTORY_INFO) {
537 FILE_DIRECTORY_INFO * pFindData =
538 (FILE_DIRECTORY_INFO *)current_entry;
539 filename = &pFindData->FileName[0];
540 len = le32_to_cpu(pFindData->FileNameLength);
541 } else if(cfile->srch_inf.info_level ==
542 SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
543 FILE_FULL_DIRECTORY_INFO * pFindData =
544 (FILE_FULL_DIRECTORY_INFO *)current_entry;
545 filename = &pFindData->FileName[0];
546 len = le32_to_cpu(pFindData->FileNameLength);
547 } else if(cfile->srch_inf.info_level ==
548 SMB_FIND_FILE_ID_FULL_DIR_INFO) {
549 SEARCH_ID_FULL_DIR_INFO * pFindData =
550 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
551 filename = &pFindData->FileName[0];
552 len = le32_to_cpu(pFindData->FileNameLength);
553 } else if(cfile->srch_inf.info_level ==
554 SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
555 FILE_BOTH_DIRECTORY_INFO * pFindData =
556 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
557 filename = &pFindData->FileName[0];
558 len = le32_to_cpu(pFindData->FileNameLength);
559 } else if(cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
560 FIND_FILE_STANDARD_INFO * pFindData =
561 (FIND_FILE_STANDARD_INFO *)current_entry;
562 filename = &pFindData->FileName[0];
563 len = pFindData->FileNameLength;
565 cFYI(1,("Unknown findfirst level %d",cfile->srch_inf.info_level));
569 if(cfile->srch_inf.unicode) {
570 __le16 *ufilename = (__le16 *)filename;
573 if(ufilename[0] == UNICODE_DOT)
575 } else if(len == 4) {
577 if((ufilename[0] == UNICODE_DOT)
578 &&(ufilename[1] == UNICODE_DOT))
583 if(filename[0] == '.')
585 } else if(len == 2) {
586 if((filename[0] == '.') && (filename[1] == '.'))
595 /* Check if directory that we are searching has changed so we can decide
596 whether we can use the cached search results from the previous search */
597 static int is_dir_changed(struct file * file)
599 struct inode * inode;
600 struct cifsInodeInfo *cifsInfo;
602 if(file->f_dentry == NULL)
605 inode = file->f_dentry->d_inode;
610 cifsInfo = CIFS_I(inode);
612 if(cifsInfo->time == 0)
613 return 1; /* directory was changed, perhaps due to unlink */
619 /* find the corresponding entry in the search */
620 /* Note that the SMB server returns search entries for . and .. which
621 complicates logic here if we choose to parse for them and we do not
622 assume that they are located in the findfirst return buffer.*/
623 /* We start counting in the buffer with entry 2 and increment for every
624 entry (do not increment for . or .. entry) */
625 static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
626 struct file *file, char **ppCurrentEntry, int *num_to_ret)
630 loff_t first_entry_in_buffer;
631 loff_t index_to_find = file->f_pos;
632 struct cifsFileInfo * cifsFile = file->private_data;
633 /* check if index in the buffer */
635 if((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
636 (num_to_ret == NULL))
639 *ppCurrentEntry = NULL;
640 first_entry_in_buffer =
641 cifsFile->srch_inf.index_of_last_entry -
642 cifsFile->srch_inf.entries_in_buffer;
644 /* if first entry in buf is zero then is first buffer
645 in search response data which means it is likely . and ..
646 will be in this buffer, although some servers do not return
647 . and .. for the root of a drive and for those we need
648 to start two entries earlier */
650 #ifdef CONFIG_CIFS_DEBUG2
651 dump_cifs_file_struct(file, "In fce ");
653 if(((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
654 is_dir_changed(file)) ||
655 (index_to_find < first_entry_in_buffer)) {
656 /* close and restart search */
657 cFYI(1,("search backing up - close and restart search"));
658 cifsFile->invalidHandle = TRUE;
659 CIFSFindClose(xid, pTcon, cifsFile->netfid);
660 kfree(cifsFile->search_resume_name);
661 cifsFile->search_resume_name = NULL;
662 if(cifsFile->srch_inf.ntwrk_buf_start) {
663 cFYI(1,("freeing SMB ff cache buf on search rewind"));
664 if(cifsFile->srch_inf.smallBuf)
665 cifs_small_buf_release(cifsFile->srch_inf.
668 cifs_buf_release(cifsFile->srch_inf.
671 rc = initiate_cifs_search(xid,file);
673 cFYI(1,("error %d reinitiating a search on rewind",rc));
678 while((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
679 (rc == 0) && (cifsFile->srch_inf.endOfSearch == FALSE)){
680 cFYI(1,("calling findnext2"));
681 rc = CIFSFindNext(xid,pTcon,cifsFile->netfid,
682 &cifsFile->srch_inf);
686 if(index_to_find < cifsFile->srch_inf.index_of_last_entry) {
687 /* we found the buffer that contains the entry */
688 /* scan and find it */
690 char * current_entry;
691 char * end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
692 smbCalcSize((struct smb_hdr *)
693 cifsFile->srch_inf.ntwrk_buf_start);
695 current_entry = cifsFile->srch_inf.srch_entries_start;
696 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
697 - cifsFile->srch_inf.entries_in_buffer;
698 pos_in_buf = index_to_find - first_entry_in_buffer;
699 cFYI(1,("found entry - pos_in_buf %d",pos_in_buf));
701 for(i=0;(i<(pos_in_buf)) && (current_entry != NULL);i++) {
702 /* go entry by entry figuring out which is first */
703 current_entry = nxt_dir_entry(current_entry,end_of_smb,
704 cifsFile->srch_inf.info_level);
706 if((current_entry == NULL) && (i < pos_in_buf)) {
707 /* BB fixme - check if we should flag this error */
708 cERROR(1,("reached end of buf searching for pos in buf"
709 " %d index to find %lld rc %d",
710 pos_in_buf,index_to_find,rc));
713 *ppCurrentEntry = current_entry;
715 cFYI(1,("index not in buffer - could not findnext into it"));
719 if(pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
720 cFYI(1,("can not return entries pos_in_buf beyond last entry"));
723 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
728 /* inode num, inode type and filename returned */
729 static int cifs_get_name_from_search_buf(struct qstr *pqst,
730 char *current_entry, __u16 level, unsigned int unicode,
731 struct cifs_sb_info * cifs_sb, int max_len, ino_t *pinum)
734 unsigned int len = 0;
736 struct nls_table * nlt = cifs_sb->local_nls;
740 if(level == SMB_FIND_FILE_UNIX) {
741 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
743 filename = &pFindData->FileName[0];
745 len = cifs_unicode_bytelen(filename);
747 /* BB should we make this strnlen of PATH_MAX? */
748 len = strnlen(filename, PATH_MAX);
751 /* BB fixme - hash low and high 32 bits if not 64 bit arch BB fixme */
752 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
753 *pinum = pFindData->UniqueId;
754 } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
755 FILE_DIRECTORY_INFO * pFindData =
756 (FILE_DIRECTORY_INFO *)current_entry;
757 filename = &pFindData->FileName[0];
758 len = le32_to_cpu(pFindData->FileNameLength);
759 } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
760 FILE_FULL_DIRECTORY_INFO * pFindData =
761 (FILE_FULL_DIRECTORY_INFO *)current_entry;
762 filename = &pFindData->FileName[0];
763 len = le32_to_cpu(pFindData->FileNameLength);
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 *pinum = pFindData->UniqueId;
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 } else if(level == SMB_FIND_FILE_INFO_STANDARD) {
776 FIND_FILE_STANDARD_INFO * pFindData =
777 (FIND_FILE_STANDARD_INFO *)current_entry;
778 filename = &pFindData->FileName[0];
779 /* one byte length, no name conversion */
780 len = (unsigned int)pFindData->FileNameLength;
782 cFYI(1,("Unknown findfirst level %d",level));
787 cERROR(1,("bad search response length %d past smb end", len));
792 /* BB fixme - test with long names */
793 /* Note converted filename can be longer than in unicode */
794 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
795 pqst->len = cifs_convertUCSpath((char *)pqst->name,
796 (__le16 *)filename, len/2, nlt);
798 pqst->len = cifs_strfromUCS_le((char *)pqst->name,
799 (__le16 *)filename,len/2,nlt);
801 pqst->name = filename;
804 pqst->hash = full_name_hash(pqst->name,pqst->len);
805 /* cFYI(1,("filldir on %s",pqst->name)); */
809 static int cifs_filldir(char *pfindEntry, struct file *file,
810 filldir_t filldir, void *direntry, char *scratch_buf, int max_len)
814 struct cifsFileInfo * pCifsF;
817 struct cifs_sb_info * cifs_sb;
818 struct inode *tmp_inode;
819 struct dentry *tmp_dentry;
821 /* get filename and len into qstring */
823 /* decide whether to create and populate ionde */
824 if((direntry == NULL) || (file == NULL))
827 pCifsF = file->private_data;
829 if((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
832 if(file->f_dentry == NULL)
835 rc = cifs_entry_is_dot(pfindEntry,pCifsF);
836 /* skip . and .. since we added them first */
840 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
842 qstring.name = scratch_buf;
843 rc = cifs_get_name_from_search_buf(&qstring,pfindEntry,
844 pCifsF->srch_inf.info_level,
845 pCifsF->srch_inf.unicode,cifs_sb,
847 &inum /* returned */);
852 rc = construct_dentry(&qstring,file,&tmp_inode, &tmp_dentry);
853 if((tmp_inode == NULL) || (tmp_dentry == NULL))
857 /* inode created, we need to hash it with right inode number */
859 /* BB fixme - hash the 2 32 quantities bits together if necessary BB */
860 tmp_inode->i_ino = inum;
862 insert_inode_hash(tmp_inode);
865 /* we pass in rc below, indicating whether it is a new inode,
866 so we can figure out whether to invalidate the inode cached
867 data if the file has changed */
868 if(pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
869 unix_fill_in_inode(tmp_inode,
870 (FILE_UNIX_INFO *)pfindEntry,
872 else if(pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
873 fill_in_inode(tmp_inode, 0 /* old level 1 buffer type */,
874 pfindEntry, &obj_type, rc);
876 fill_in_inode(tmp_inode, 1 /* NT */, pfindEntry, &obj_type, rc);
878 if(rc) /* new inode - needs to be tied to dentry */ {
879 d_instantiate(tmp_dentry, tmp_inode);
881 d_rehash(tmp_dentry);
885 rc = filldir(direntry,qstring.name,qstring.len,file->f_pos,
886 tmp_inode->i_ino,obj_type);
888 cFYI(1,("filldir rc = %d",rc));
895 static int cifs_save_resume_key(const char *current_entry,
896 struct cifsFileInfo *cifsFile)
899 unsigned int len = 0;
903 if((cifsFile == NULL) || (current_entry == NULL))
906 level = cifsFile->srch_inf.info_level;
908 if(level == SMB_FIND_FILE_UNIX) {
909 FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
911 filename = &pFindData->FileName[0];
912 if(cifsFile->srch_inf.unicode) {
913 len = cifs_unicode_bytelen(filename);
915 /* BB should we make this strnlen of PATH_MAX? */
916 len = strnlen(filename, PATH_MAX);
918 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
919 } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
920 FILE_DIRECTORY_INFO * pFindData =
921 (FILE_DIRECTORY_INFO *)current_entry;
922 filename = &pFindData->FileName[0];
923 len = le32_to_cpu(pFindData->FileNameLength);
924 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
925 } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
926 FILE_FULL_DIRECTORY_INFO * pFindData =
927 (FILE_FULL_DIRECTORY_INFO *)current_entry;
928 filename = &pFindData->FileName[0];
929 len = le32_to_cpu(pFindData->FileNameLength);
930 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
931 } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
932 SEARCH_ID_FULL_DIR_INFO * pFindData =
933 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
934 filename = &pFindData->FileName[0];
935 len = le32_to_cpu(pFindData->FileNameLength);
936 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
937 } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
938 FILE_BOTH_DIRECTORY_INFO * pFindData =
939 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
940 filename = &pFindData->FileName[0];
941 len = le32_to_cpu(pFindData->FileNameLength);
942 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
943 } else if(level == SMB_FIND_FILE_INFO_STANDARD) {
944 FIND_FILE_STANDARD_INFO * pFindData =
945 (FIND_FILE_STANDARD_INFO *)current_entry;
946 filename = &pFindData->FileName[0];
947 /* one byte length, no name conversion */
948 len = (unsigned int)pFindData->FileNameLength;
950 cFYI(1,("Unknown findfirst level %d",level));
953 cifsFile->srch_inf.resume_name_len = len;
954 cifsFile->srch_inf.presume_name = filename;
958 int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
962 struct cifs_sb_info *cifs_sb;
963 struct cifsTconInfo *pTcon;
964 struct cifsFileInfo *cifsFile = NULL;
965 char * current_entry;
967 char * tmp_buf = NULL;
973 if(file->f_dentry == NULL) {
978 cifs_sb = CIFS_SB(file->f_dentry->d_sb);
979 pTcon = cifs_sb->tcon;
983 switch ((int) file->f_pos) {
985 if (filldir(direntry, ".", 1, file->f_pos,
986 file->f_dentry->d_inode->i_ino, DT_DIR) < 0) {
987 cERROR(1, ("Filldir for current dir failed"));
993 if (filldir(direntry, "..", 2, file->f_pos,
994 file->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
995 cERROR(1, ("Filldir for parent dir failed"));
1001 /* 1) If search is active,
1002 is in current search buffer?
1003 if it before then restart search
1004 if after then keep searching till find it */
1006 if(file->private_data == NULL) {
1007 rc = initiate_cifs_search(xid,file);
1008 cFYI(1,("initiate cifs search rc %d",rc));
1014 if(file->private_data == NULL) {
1019 cifsFile = file->private_data;
1020 if (cifsFile->srch_inf.endOfSearch) {
1021 if(cifsFile->srch_inf.emptyDir) {
1022 cFYI(1, ("End of search, empty dir"));
1027 cifsFile->invalidHandle = TRUE;
1028 CIFSFindClose(xid, pTcon, cifsFile->netfid);
1030 kfree(cifsFile->search_resume_name);
1031 cifsFile->search_resume_name = NULL; */
1033 rc = find_cifs_entry(xid,pTcon, file,
1034 ¤t_entry,&num_to_fill);
1036 cFYI(1,("fce error %d",rc));
1038 } else if (current_entry != NULL) {
1039 cFYI(1,("entry %lld found",file->f_pos));
1041 cFYI(1,("could not find entry"));
1044 cFYI(1,("loop through %d times filling dir for net buf %p",
1045 num_to_fill,cifsFile->srch_inf.ntwrk_buf_start));
1046 max_len = smbCalcSize((struct smb_hdr *)
1047 cifsFile->srch_inf.ntwrk_buf_start);
1048 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
1050 /* To be safe - for UCS to UTF-8 with strings loaded
1051 with the rare long characters alloc more to account for
1052 such multibyte target UTF-8 characters. cifs_unicode.c,
1053 which actually does the conversion, has the same limit */
1054 tmp_buf = kmalloc((2 * NAME_MAX) + 4, GFP_KERNEL);
1055 for(i=0;(i<num_to_fill) && (rc == 0);i++) {
1056 if(current_entry == NULL) {
1057 /* evaluate whether this case is an error */
1058 cERROR(1,("past end of SMB num to fill %d i %d",
1062 /* if buggy server returns . and .. late do
1063 we want to check for that here? */
1064 rc = cifs_filldir(current_entry, file,
1065 filldir, direntry, tmp_buf, max_len);
1068 cifsFile->srch_inf.index_of_last_entry) {
1069 cFYI(1,("last entry in buf at pos %lld %s",
1070 file->f_pos,tmp_buf));
1071 cifs_save_resume_key(current_entry,cifsFile);
1075 nxt_dir_entry(current_entry, end_of_smb,
1076 cifsFile->srch_inf.info_level);