4 * Directory search handling
6 * Copyright (C) International Business Machines Corp., 2004, 2008
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>
28 #include "cifsproto.h"
29 #include "cifs_unicode.h"
30 #include "cifs_debug.h"
31 #include "cifs_fs_sb.h"
35 * To be safe - for UCS to UTF-8 with strings loaded with the rare long
36 * characters alloc more to account for such multibyte target UTF-8
39 #define UNICODE_NAME_MAX ((4 * NAME_MAX) + 2)
41 #ifdef CONFIG_CIFS_DEBUG2
42 static void dump_cifs_file_struct(struct file *file, char *label)
44 struct cifsFileInfo *cf;
47 cf = file->private_data;
49 cFYI(1, ("empty cifs private file data"));
52 if (cf->invalidHandle)
53 cFYI(1, ("invalid handle"));
54 if (cf->srch_inf.endOfSearch)
55 cFYI(1, ("end of search"));
56 if (cf->srch_inf.emptyDir)
57 cFYI(1, ("empty dir"));
61 static inline void dump_cifs_file_struct(struct file *file, char *label)
66 /* Returns 1 if new inode created, 2 if both dentry and inode were */
67 /* Might check in the future if inode number changed so we can rehash inode */
69 construct_dentry(struct qstr *qstring, struct file *file,
70 struct inode **ptmp_inode, struct dentry **pnew_dentry,
73 struct dentry *tmp_dentry = NULL;
74 struct super_block *sb = file->f_path.dentry->d_sb;
77 cFYI(1, ("For %s", qstring->name));
79 qstring->hash = full_name_hash(qstring->name, qstring->len);
80 tmp_dentry = d_lookup(file->f_path.dentry, qstring);
82 /* BB: overwrite old name? i.e. tmp_dentry->d_name and
83 * tmp_dentry->d_name.len??
85 cFYI(0, ("existing dentry with inode 0x%p",
86 tmp_dentry->d_inode));
87 *ptmp_inode = tmp_dentry->d_inode;
88 if (*ptmp_inode == NULL) {
89 *ptmp_inode = cifs_new_inode(sb, inum);
90 if (*ptmp_inode == NULL)
95 tmp_dentry = d_alloc(file->f_path.dentry, qstring);
96 if (tmp_dentry == NULL) {
97 cERROR(1, ("Failed allocating dentry"));
102 if (CIFS_SB(sb)->tcon->nocase)
103 tmp_dentry->d_op = &cifs_ci_dentry_ops;
105 tmp_dentry->d_op = &cifs_dentry_ops;
107 *ptmp_inode = cifs_new_inode(sb, inum);
108 if (*ptmp_inode == NULL)
113 tmp_dentry->d_time = jiffies;
114 *pnew_dentry = tmp_dentry;
118 static void AdjustForTZ(struct cifsTconInfo *tcon, struct inode *inode)
120 if ((tcon) && (tcon->ses) && (tcon->ses->server)) {
121 inode->i_ctime.tv_sec += tcon->ses->server->timeAdj;
122 inode->i_mtime.tv_sec += tcon->ses->server->timeAdj;
123 inode->i_atime.tv_sec += tcon->ses->server->timeAdj;
129 static void fill_in_inode(struct inode *tmp_inode, int new_buf_type,
130 char *buf, unsigned int *pobject_type, int isNewInode)
133 struct timespec local_mtime;
135 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
136 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
138 __u64 allocation_size;
140 umode_t default_mode;
142 /* save mtime and size */
143 local_mtime = tmp_inode->i_mtime;
144 local_size = tmp_inode->i_size;
147 FILE_DIRECTORY_INFO *pfindData = (FILE_DIRECTORY_INFO *)buf;
149 attr = le32_to_cpu(pfindData->ExtFileAttributes);
150 allocation_size = le64_to_cpu(pfindData->AllocationSize);
151 end_of_file = le64_to_cpu(pfindData->EndOfFile);
153 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
155 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
157 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
158 } else { /* legacy, OS2 and DOS style */
159 /* struct timespec ts;*/
160 FIND_FILE_STANDARD_INFO *pfindData =
161 (FIND_FILE_STANDARD_INFO *)buf;
163 tmp_inode->i_mtime = cnvrtDosUnixTm(
164 le16_to_cpu(pfindData->LastWriteDate),
165 le16_to_cpu(pfindData->LastWriteTime));
166 tmp_inode->i_atime = cnvrtDosUnixTm(
167 le16_to_cpu(pfindData->LastAccessDate),
168 le16_to_cpu(pfindData->LastAccessTime));
169 tmp_inode->i_ctime = cnvrtDosUnixTm(
170 le16_to_cpu(pfindData->LastWriteDate),
171 le16_to_cpu(pfindData->LastWriteTime));
172 AdjustForTZ(cifs_sb->tcon, tmp_inode);
173 attr = le16_to_cpu(pfindData->Attributes);
174 allocation_size = le32_to_cpu(pfindData->AllocationSize);
175 end_of_file = le32_to_cpu(pfindData->DataSize);
178 /* Linux can not store file creation time unfortunately so ignore it */
180 cifsInfo->cifsAttrs = attr;
181 #ifdef CONFIG_CIFS_EXPERIMENTAL
182 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
183 /* get more accurate mode via ACL - so force inode refresh */
186 #endif /* CONFIG_CIFS_EXPERIMENTAL */
187 cifsInfo->time = jiffies;
189 /* treat dos attribute of read-only as read-only mode bit e.g. 555? */
190 /* 2767 perms - indicate mandatory locking */
191 /* BB fill in uid and gid here? with help from winbind?
192 or retrieve from NTFS stream extended attribute */
193 if (atomic_read(&cifsInfo->inUse) == 0) {
194 tmp_inode->i_uid = cifs_sb->mnt_uid;
195 tmp_inode->i_gid = cifs_sb->mnt_gid;
198 if (attr & ATTR_DIRECTORY)
199 default_mode = cifs_sb->mnt_dir_mode;
201 default_mode = cifs_sb->mnt_file_mode;
203 /* set initial permissions */
204 if ((atomic_read(&cifsInfo->inUse) == 0) ||
205 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
206 tmp_inode->i_mode = default_mode;
208 /* just reenable write bits if !ATTR_READONLY */
209 if ((tmp_inode->i_mode & S_IWUGO) == 0 &&
210 (attr & ATTR_READONLY) == 0)
211 tmp_inode->i_mode |= (S_IWUGO & default_mode);
213 tmp_inode->i_mode &= ~S_IFMT;
216 /* clear write bits if ATTR_READONLY is set */
217 if (attr & ATTR_READONLY)
218 tmp_inode->i_mode &= ~S_IWUGO;
221 if ((attr & ATTR_SYSTEM) &&
222 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) {
223 if (end_of_file == 0) {
224 tmp_inode->i_mode |= S_IFIFO;
225 *pobject_type = DT_FIFO;
228 * trying to get the type can be slow, so just call
229 * this a regular file for now, and mark for reval
231 tmp_inode->i_mode |= S_IFREG;
232 *pobject_type = DT_REG;
236 if (attr & ATTR_DIRECTORY) {
237 tmp_inode->i_mode |= S_IFDIR;
238 *pobject_type = DT_DIR;
240 tmp_inode->i_mode |= S_IFREG;
241 *pobject_type = DT_REG;
245 /* can not fill in nlink here as in qpathinfo version and Unx search */
246 if (atomic_read(&cifsInfo->inUse) == 0)
247 atomic_set(&cifsInfo->inUse, 1);
249 cifsInfo->server_eof = end_of_file;
250 spin_lock(&tmp_inode->i_lock);
251 if (is_size_safe_to_change(cifsInfo, end_of_file)) {
252 /* can not safely change the file size here if the
253 client is writing to it due to potential races */
254 i_size_write(tmp_inode, end_of_file);
256 /* 512 bytes (2**9) is the fake blocksize that must be used */
257 /* for this calculation, even though the reported blocksize is larger */
258 tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9;
260 spin_unlock(&tmp_inode->i_lock);
262 if (allocation_size < end_of_file)
263 cFYI(1, ("May be sparse file, allocation less than file size"));
264 cFYI(1, ("File Size %ld and blocks %llu",
265 (unsigned long)tmp_inode->i_size,
266 (unsigned long long)tmp_inode->i_blocks));
267 if (S_ISREG(tmp_inode->i_mode)) {
268 cFYI(1, ("File inode"));
269 tmp_inode->i_op = &cifs_file_inode_ops;
270 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
271 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
272 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
274 tmp_inode->i_fop = &cifs_file_direct_ops;
275 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
276 tmp_inode->i_fop = &cifs_file_nobrl_ops;
278 tmp_inode->i_fop = &cifs_file_ops;
280 if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
281 (cifs_sb->tcon->ses->server->maxBuf <
282 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
283 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
285 tmp_inode->i_data.a_ops = &cifs_addr_ops;
288 return; /* No sense invalidating pages for new inode
289 since have not started caching readahead file
292 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
293 (local_size == tmp_inode->i_size)) {
294 cFYI(1, ("inode exists but unchanged"));
296 /* file may have changed on server */
297 cFYI(1, ("invalidate inode, readdir detected change"));
298 invalidate_remote_inode(tmp_inode);
300 } else if (S_ISDIR(tmp_inode->i_mode)) {
301 cFYI(1, ("Directory inode"));
302 tmp_inode->i_op = &cifs_dir_inode_ops;
303 tmp_inode->i_fop = &cifs_dir_ops;
304 } else if (S_ISLNK(tmp_inode->i_mode)) {
305 cFYI(1, ("Symbolic Link inode"));
306 tmp_inode->i_op = &cifs_symlink_inode_ops;
308 cFYI(1, ("Init special inode"));
309 init_special_inode(tmp_inode, tmp_inode->i_mode,
314 static void unix_fill_in_inode(struct inode *tmp_inode,
315 FILE_UNIX_INFO *pfindData, unsigned int *pobject_type, int isNewInode)
318 struct timespec local_mtime;
320 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
321 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
323 __u32 type = le32_to_cpu(pfindData->Type);
324 __u64 num_of_bytes = le64_to_cpu(pfindData->NumOfBytes);
325 __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
326 cifsInfo->time = jiffies;
327 atomic_inc(&cifsInfo->inUse);
329 /* save mtime and size */
330 local_mtime = tmp_inode->i_mtime;
331 local_size = tmp_inode->i_size;
334 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
336 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastModificationTime));
338 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastStatusChange));
340 tmp_inode->i_mode = le64_to_cpu(pfindData->Permissions);
341 /* since we set the inode type below we need to mask off type
342 to avoid strange results if bits above were corrupt */
343 tmp_inode->i_mode &= ~S_IFMT;
344 if (type == UNIX_FILE) {
345 *pobject_type = DT_REG;
346 tmp_inode->i_mode |= S_IFREG;
347 } else if (type == UNIX_SYMLINK) {
348 *pobject_type = DT_LNK;
349 tmp_inode->i_mode |= S_IFLNK;
350 } else if (type == UNIX_DIR) {
351 *pobject_type = DT_DIR;
352 tmp_inode->i_mode |= S_IFDIR;
353 } else if (type == UNIX_CHARDEV) {
354 *pobject_type = DT_CHR;
355 tmp_inode->i_mode |= S_IFCHR;
356 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
357 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
358 } else if (type == UNIX_BLOCKDEV) {
359 *pobject_type = DT_BLK;
360 tmp_inode->i_mode |= S_IFBLK;
361 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
362 le64_to_cpu(pfindData->DevMinor) & MINORMASK);
363 } else if (type == UNIX_FIFO) {
364 *pobject_type = DT_FIFO;
365 tmp_inode->i_mode |= S_IFIFO;
366 } else if (type == UNIX_SOCKET) {
367 *pobject_type = DT_SOCK;
368 tmp_inode->i_mode |= S_IFSOCK;
370 /* safest to just call it a file */
371 *pobject_type = DT_REG;
372 tmp_inode->i_mode |= S_IFREG;
373 cFYI(1, ("unknown inode type %d", type));
376 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
377 tmp_inode->i_uid = cifs_sb->mnt_uid;
379 tmp_inode->i_uid = le64_to_cpu(pfindData->Uid);
380 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
381 tmp_inode->i_gid = cifs_sb->mnt_gid;
383 tmp_inode->i_gid = le64_to_cpu(pfindData->Gid);
384 tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks);
386 cifsInfo->server_eof = end_of_file;
387 spin_lock(&tmp_inode->i_lock);
388 if (is_size_safe_to_change(cifsInfo, end_of_file)) {
389 /* can not safely change the file size here if the
390 client is writing to it due to potential races */
391 i_size_write(tmp_inode, end_of_file);
393 /* 512 bytes (2**9) is the fake blocksize that must be used */
394 /* for this calculation, not the real blocksize */
395 tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
397 spin_unlock(&tmp_inode->i_lock);
399 if (S_ISREG(tmp_inode->i_mode)) {
400 cFYI(1, ("File inode"));
401 tmp_inode->i_op = &cifs_file_inode_ops;
403 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
404 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
405 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
407 tmp_inode->i_fop = &cifs_file_direct_ops;
408 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
409 tmp_inode->i_fop = &cifs_file_nobrl_ops;
411 tmp_inode->i_fop = &cifs_file_ops;
413 if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
414 (cifs_sb->tcon->ses->server->maxBuf <
415 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
416 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
418 tmp_inode->i_data.a_ops = &cifs_addr_ops;
421 return; /* No sense invalidating pages for new inode
422 since we have not started caching readahead
423 file data for it yet */
425 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
426 (local_size == tmp_inode->i_size)) {
427 cFYI(1, ("inode exists but unchanged"));
429 /* file may have changed on server */
430 cFYI(1, ("invalidate inode, readdir detected change"));
431 invalidate_remote_inode(tmp_inode);
433 } else if (S_ISDIR(tmp_inode->i_mode)) {
434 cFYI(1, ("Directory inode"));
435 tmp_inode->i_op = &cifs_dir_inode_ops;
436 tmp_inode->i_fop = &cifs_dir_ops;
437 } else if (S_ISLNK(tmp_inode->i_mode)) {
438 cFYI(1, ("Symbolic Link inode"));
439 tmp_inode->i_op = &cifs_symlink_inode_ops;
440 /* tmp_inode->i_fop = *//* do not need to set to anything */
442 cFYI(1, ("Special inode"));
443 init_special_inode(tmp_inode, tmp_inode->i_mode,
448 static int initiate_cifs_search(const int xid, struct file *file)
452 struct cifsFileInfo *cifsFile;
453 struct cifs_sb_info *cifs_sb;
454 struct cifsTconInfo *pTcon;
456 if (file->private_data == NULL) {
458 kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
461 if (file->private_data == NULL)
463 cifsFile = file->private_data;
464 cifsFile->invalidHandle = true;
465 cifsFile->srch_inf.endOfSearch = false;
467 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
471 pTcon = cifs_sb->tcon;
475 full_path = build_path_from_dentry(file->f_path.dentry);
477 if (full_path == NULL)
480 cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
483 /* test for Unix extensions */
484 /* but now check for them on the share/mount not on the SMB session */
485 /* if (pTcon->ses->capabilities & CAP_UNIX) { */
487 cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
488 else if ((pTcon->ses->capabilities &
489 (CAP_NT_SMBS | CAP_NT_FIND)) == 0) {
490 cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
491 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
492 cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
493 } else /* not srvinos - BB fixme add check for backlevel? */ {
494 cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
497 rc = CIFSFindFirst(xid, pTcon, full_path, cifs_sb->local_nls,
498 &cifsFile->netfid, &cifsFile->srch_inf,
499 cifs_sb->mnt_cifs_flags &
500 CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
502 cifsFile->invalidHandle = false;
503 if ((rc == -EOPNOTSUPP) &&
504 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
505 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
512 /* return length of unicode string in bytes */
513 static int cifs_unicode_bytelen(char *str)
516 __le16 *ustr = (__le16 *)str;
518 for (len = 0; len <= PATH_MAX; len++) {
522 cFYI(1, ("Unicode string longer than PATH_MAX found"));
526 static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
529 FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
531 if (level == SMB_FIND_FILE_INFO_STANDARD) {
532 FIND_FILE_STANDARD_INFO *pfData;
533 pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
535 new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
536 pfData->FileNameLength;
538 new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
539 cFYI(1, ("new entry %p old entry %p", new_entry, old_entry));
540 /* validate that new_entry is not past end of SMB */
541 if (new_entry >= end_of_smb) {
543 ("search entry %p began after end of SMB %p old entry %p",
544 new_entry, end_of_smb, old_entry));
546 } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
547 (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
548 || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
549 (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb))) {
550 cERROR(1, ("search entry %p extends after end of SMB %p",
551 new_entry, end_of_smb));
558 #define UNICODE_DOT cpu_to_le16(0x2e)
560 /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
561 static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
564 char *filename = NULL;
567 if (cfile->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
568 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
569 filename = &pFindData->FileName[0];
570 if (cfile->srch_inf.unicode) {
571 len = cifs_unicode_bytelen(filename);
573 /* BB should we make this strnlen of PATH_MAX? */
574 len = strnlen(filename, 5);
576 } else if (cfile->srch_inf.info_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 (cfile->srch_inf.info_level ==
582 SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
583 FILE_FULL_DIRECTORY_INFO *pFindData =
584 (FILE_FULL_DIRECTORY_INFO *)current_entry;
585 filename = &pFindData->FileName[0];
586 len = le32_to_cpu(pFindData->FileNameLength);
587 } else if (cfile->srch_inf.info_level ==
588 SMB_FIND_FILE_ID_FULL_DIR_INFO) {
589 SEARCH_ID_FULL_DIR_INFO *pFindData =
590 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
591 filename = &pFindData->FileName[0];
592 len = le32_to_cpu(pFindData->FileNameLength);
593 } else if (cfile->srch_inf.info_level ==
594 SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
595 FILE_BOTH_DIRECTORY_INFO *pFindData =
596 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
597 filename = &pFindData->FileName[0];
598 len = le32_to_cpu(pFindData->FileNameLength);
599 } else if (cfile->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD) {
600 FIND_FILE_STANDARD_INFO *pFindData =
601 (FIND_FILE_STANDARD_INFO *)current_entry;
602 filename = &pFindData->FileName[0];
603 len = pFindData->FileNameLength;
605 cFYI(1, ("Unknown findfirst level %d",
606 cfile->srch_inf.info_level));
610 if (cfile->srch_inf.unicode) {
611 __le16 *ufilename = (__le16 *)filename;
614 if (ufilename[0] == UNICODE_DOT)
616 } else if (len == 4) {
618 if ((ufilename[0] == UNICODE_DOT)
619 && (ufilename[1] == UNICODE_DOT))
624 if (filename[0] == '.')
626 } else if (len == 2) {
627 if ((filename[0] == '.') && (filename[1] == '.'))
636 /* Check if directory that we are searching has changed so we can decide
637 whether we can use the cached search results from the previous search */
638 static int is_dir_changed(struct file *file)
640 struct inode *inode = file->f_path.dentry->d_inode;
641 struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
643 if (cifsInfo->time == 0)
644 return 1; /* directory was changed, perhaps due to unlink */
650 static int cifs_save_resume_key(const char *current_entry,
651 struct cifsFileInfo *cifsFile)
654 unsigned int len = 0;
658 if ((cifsFile == NULL) || (current_entry == NULL))
661 level = cifsFile->srch_inf.info_level;
663 if (level == SMB_FIND_FILE_UNIX) {
664 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
666 filename = &pFindData->FileName[0];
667 if (cifsFile->srch_inf.unicode) {
668 len = cifs_unicode_bytelen(filename);
670 /* BB should we make this strnlen of PATH_MAX? */
671 len = strnlen(filename, PATH_MAX);
673 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
674 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
675 FILE_DIRECTORY_INFO *pFindData =
676 (FILE_DIRECTORY_INFO *)current_entry;
677 filename = &pFindData->FileName[0];
678 len = le32_to_cpu(pFindData->FileNameLength);
679 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
680 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
681 FILE_FULL_DIRECTORY_INFO *pFindData =
682 (FILE_FULL_DIRECTORY_INFO *)current_entry;
683 filename = &pFindData->FileName[0];
684 len = le32_to_cpu(pFindData->FileNameLength);
685 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
686 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
687 SEARCH_ID_FULL_DIR_INFO *pFindData =
688 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
689 filename = &pFindData->FileName[0];
690 len = le32_to_cpu(pFindData->FileNameLength);
691 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
692 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
693 FILE_BOTH_DIRECTORY_INFO *pFindData =
694 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
695 filename = &pFindData->FileName[0];
696 len = le32_to_cpu(pFindData->FileNameLength);
697 cifsFile->srch_inf.resume_key = pFindData->FileIndex;
698 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
699 FIND_FILE_STANDARD_INFO *pFindData =
700 (FIND_FILE_STANDARD_INFO *)current_entry;
701 filename = &pFindData->FileName[0];
702 /* one byte length, no name conversion */
703 len = (unsigned int)pFindData->FileNameLength;
704 cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
706 cFYI(1, ("Unknown findfirst level %d", level));
709 cifsFile->srch_inf.resume_name_len = len;
710 cifsFile->srch_inf.presume_name = filename;
714 /* find the corresponding entry in the search */
715 /* Note that the SMB server returns search entries for . and .. which
716 complicates logic here if we choose to parse for them and we do not
717 assume that they are located in the findfirst return buffer.*/
718 /* We start counting in the buffer with entry 2 and increment for every
719 entry (do not increment for . or .. entry) */
720 static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
721 struct file *file, char **ppCurrentEntry, int *num_to_ret)
725 loff_t first_entry_in_buffer;
726 loff_t index_to_find = file->f_pos;
727 struct cifsFileInfo *cifsFile = file->private_data;
728 /* check if index in the buffer */
730 if ((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
731 (num_to_ret == NULL))
734 *ppCurrentEntry = NULL;
735 first_entry_in_buffer =
736 cifsFile->srch_inf.index_of_last_entry -
737 cifsFile->srch_inf.entries_in_buffer;
739 /* if first entry in buf is zero then is first buffer
740 in search response data which means it is likely . and ..
741 will be in this buffer, although some servers do not return
742 . and .. for the root of a drive and for those we need
743 to start two entries earlier */
745 dump_cifs_file_struct(file, "In fce ");
746 if (((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
747 is_dir_changed(file)) ||
748 (index_to_find < first_entry_in_buffer)) {
749 /* close and restart search */
750 cFYI(1, ("search backing up - close and restart search"));
751 write_lock(&GlobalSMBSeslock);
752 if (!cifsFile->srch_inf.endOfSearch &&
753 !cifsFile->invalidHandle) {
754 cifsFile->invalidHandle = true;
755 write_unlock(&GlobalSMBSeslock);
756 CIFSFindClose(xid, pTcon, cifsFile->netfid);
758 write_unlock(&GlobalSMBSeslock);
759 if (cifsFile->srch_inf.ntwrk_buf_start) {
760 cFYI(1, ("freeing SMB ff cache buf on search rewind"));
761 if (cifsFile->srch_inf.smallBuf)
762 cifs_small_buf_release(cifsFile->srch_inf.
765 cifs_buf_release(cifsFile->srch_inf.
767 cifsFile->srch_inf.ntwrk_buf_start = NULL;
769 rc = initiate_cifs_search(xid, file);
771 cFYI(1, ("error %d reinitiating a search on rewind",
775 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
778 while ((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
779 (rc == 0) && !cifsFile->srch_inf.endOfSearch) {
780 cFYI(1, ("calling findnext2"));
781 rc = CIFSFindNext(xid, pTcon, cifsFile->netfid,
782 &cifsFile->srch_inf);
783 cifs_save_resume_key(cifsFile->srch_inf.last_entry, cifsFile);
787 if (index_to_find < cifsFile->srch_inf.index_of_last_entry) {
788 /* we found the buffer that contains the entry */
789 /* scan and find it */
792 char *end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
793 smbCalcSize((struct smb_hdr *)
794 cifsFile->srch_inf.ntwrk_buf_start);
796 current_entry = cifsFile->srch_inf.srch_entries_start;
797 first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
798 - cifsFile->srch_inf.entries_in_buffer;
799 pos_in_buf = index_to_find - first_entry_in_buffer;
800 cFYI(1, ("found entry - pos_in_buf %d", pos_in_buf));
802 for (i = 0; (i < (pos_in_buf)) && (current_entry != NULL); i++) {
803 /* go entry by entry figuring out which is first */
804 current_entry = nxt_dir_entry(current_entry, end_of_smb,
805 cifsFile->srch_inf.info_level);
807 if ((current_entry == NULL) && (i < pos_in_buf)) {
808 /* BB fixme - check if we should flag this error */
809 cERROR(1, ("reached end of buf searching for pos in buf"
810 " %d index to find %lld rc %d",
811 pos_in_buf, index_to_find, rc));
814 *ppCurrentEntry = current_entry;
816 cFYI(1, ("index not in buffer - could not findnext into it"));
820 if (pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
821 cFYI(1, ("can not return entries pos_in_buf beyond last"));
824 *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
829 /* inode num, inode type and filename returned */
830 static int cifs_get_name_from_search_buf(struct qstr *pqst,
831 char *current_entry, __u16 level, unsigned int unicode,
832 struct cifs_sb_info *cifs_sb, unsigned int max_len, __u64 *pinum)
835 unsigned int len = 0;
837 struct nls_table *nlt = cifs_sb->local_nls;
841 if (level == SMB_FIND_FILE_UNIX) {
842 FILE_UNIX_INFO *pFindData = (FILE_UNIX_INFO *)current_entry;
844 filename = &pFindData->FileName[0];
846 len = cifs_unicode_bytelen(filename);
848 /* BB should we make this strnlen of PATH_MAX? */
849 len = strnlen(filename, PATH_MAX);
852 *pinum = le64_to_cpu(pFindData->UniqueId);
853 } else if (level == SMB_FIND_FILE_DIRECTORY_INFO) {
854 FILE_DIRECTORY_INFO *pFindData =
855 (FILE_DIRECTORY_INFO *)current_entry;
856 filename = &pFindData->FileName[0];
857 len = le32_to_cpu(pFindData->FileNameLength);
858 } else if (level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
859 FILE_FULL_DIRECTORY_INFO *pFindData =
860 (FILE_FULL_DIRECTORY_INFO *)current_entry;
861 filename = &pFindData->FileName[0];
862 len = le32_to_cpu(pFindData->FileNameLength);
863 } else if (level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
864 SEARCH_ID_FULL_DIR_INFO *pFindData =
865 (SEARCH_ID_FULL_DIR_INFO *)current_entry;
866 filename = &pFindData->FileName[0];
867 len = le32_to_cpu(pFindData->FileNameLength);
868 *pinum = le64_to_cpu(pFindData->UniqueId);
869 } else if (level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
870 FILE_BOTH_DIRECTORY_INFO *pFindData =
871 (FILE_BOTH_DIRECTORY_INFO *)current_entry;
872 filename = &pFindData->FileName[0];
873 len = le32_to_cpu(pFindData->FileNameLength);
874 } else if (level == SMB_FIND_FILE_INFO_STANDARD) {
875 FIND_FILE_STANDARD_INFO *pFindData =
876 (FIND_FILE_STANDARD_INFO *)current_entry;
877 filename = &pFindData->FileName[0];
878 /* one byte length, no name conversion */
879 len = (unsigned int)pFindData->FileNameLength;
881 cFYI(1, ("Unknown findfirst level %d", level));
886 cERROR(1, ("bad search response length %d past smb end", len));
891 pqst->len = cifs_from_ucs2((char *) pqst->name,
894 min(len, max_len), nlt,
895 cifs_sb->mnt_cifs_flags &
896 CIFS_MOUNT_MAP_SPECIAL_CHR);
898 pqst->name = filename;
901 pqst->hash = full_name_hash(pqst->name, pqst->len);
902 /* cFYI(1, ("filldir on %s",pqst->name)); */
906 static int cifs_filldir(char *pfindEntry, struct file *file, filldir_t filldir,
907 void *direntry, char *scratch_buf, unsigned int max_len)
911 struct cifsFileInfo *pCifsF;
912 unsigned int obj_type;
914 struct cifs_sb_info *cifs_sb;
915 struct inode *tmp_inode;
916 struct dentry *tmp_dentry;
918 /* get filename and len into qstring */
920 /* decide whether to create and populate ionde */
921 if ((direntry == NULL) || (file == NULL))
924 pCifsF = file->private_data;
926 if ((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
929 rc = cifs_entry_is_dot(pfindEntry, pCifsF);
930 /* skip . and .. since we added them first */
934 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
936 qstring.name = scratch_buf;
937 rc = cifs_get_name_from_search_buf(&qstring, pfindEntry,
938 pCifsF->srch_inf.info_level,
939 pCifsF->srch_inf.unicode, cifs_sb,
941 &inum /* returned */);
946 /* only these two infolevels return valid inode numbers */
947 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX ||
948 pCifsF->srch_inf.info_level == SMB_FIND_FILE_ID_FULL_DIR_INFO)
949 rc = construct_dentry(&qstring, file, &tmp_inode, &tmp_dentry,
952 rc = construct_dentry(&qstring, file, &tmp_inode, &tmp_dentry,
955 if ((tmp_inode == NULL) || (tmp_dentry == NULL))
958 /* we pass in rc below, indicating whether it is a new inode,
959 so we can figure out whether to invalidate the inode cached
960 data if the file has changed */
961 if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX)
962 unix_fill_in_inode(tmp_inode,
963 (FILE_UNIX_INFO *)pfindEntry,
965 else if (pCifsF->srch_inf.info_level == SMB_FIND_FILE_INFO_STANDARD)
966 fill_in_inode(tmp_inode, 0 /* old level 1 buffer type */,
967 pfindEntry, &obj_type, rc);
969 fill_in_inode(tmp_inode, 1 /* NT */, pfindEntry, &obj_type, rc);
971 if (rc) /* new inode - needs to be tied to dentry */ {
972 d_instantiate(tmp_dentry, tmp_inode);
974 d_rehash(tmp_dentry);
978 rc = filldir(direntry, qstring.name, qstring.len, file->f_pos,
979 tmp_inode->i_ino, obj_type);
981 cFYI(1, ("filldir rc = %d", rc));
982 /* we can not return filldir errors to the caller
983 since they are "normal" when the stat blocksize
984 is too small - we return remapped error instead */
993 int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
997 struct cifs_sb_info *cifs_sb;
998 struct cifsTconInfo *pTcon;
999 struct cifsFileInfo *cifsFile = NULL;
1000 char *current_entry;
1001 int num_to_fill = 0;
1002 char *tmp_buf = NULL;
1004 unsigned int max_len;
1008 cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
1009 pTcon = cifs_sb->tcon;
1013 switch ((int) file->f_pos) {
1015 if (filldir(direntry, ".", 1, file->f_pos,
1016 file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
1017 cERROR(1, ("Filldir for current dir failed"));
1023 if (filldir(direntry, "..", 2, file->f_pos,
1024 file->f_path.dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
1025 cERROR(1, ("Filldir for parent dir failed"));
1031 /* 1) If search is active,
1032 is in current search buffer?
1033 if it before then restart search
1034 if after then keep searching till find it */
1036 if (file->private_data == NULL) {
1037 rc = initiate_cifs_search(xid, file);
1038 cFYI(1, ("initiate cifs search rc %d", rc));
1044 if (file->private_data == NULL) {
1049 cifsFile = file->private_data;
1050 if (cifsFile->srch_inf.endOfSearch) {
1051 if (cifsFile->srch_inf.emptyDir) {
1052 cFYI(1, ("End of search, empty dir"));
1057 cifsFile->invalidHandle = true;
1058 CIFSFindClose(xid, pTcon, cifsFile->netfid);
1061 rc = find_cifs_entry(xid, pTcon, file,
1062 ¤t_entry, &num_to_fill);
1064 cFYI(1, ("fce error %d", rc));
1066 } else if (current_entry != NULL) {
1067 cFYI(1, ("entry %lld found", file->f_pos));
1069 cFYI(1, ("could not find entry"));
1072 cFYI(1, ("loop through %d times filling dir for net buf %p",
1073 num_to_fill, cifsFile->srch_inf.ntwrk_buf_start));
1074 max_len = smbCalcSize((struct smb_hdr *)
1075 cifsFile->srch_inf.ntwrk_buf_start);
1076 end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
1078 tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
1079 for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
1080 if (current_entry == NULL) {
1081 /* evaluate whether this case is an error */
1082 cERROR(1, ("past SMB end, num to fill %d i %d",
1086 /* if buggy server returns . and .. late do
1087 we want to check for that here? */
1088 rc = cifs_filldir(current_entry, file,
1089 filldir, direntry, tmp_buf, max_len);
1090 if (rc == -EOVERFLOW) {
1097 cifsFile->srch_inf.index_of_last_entry) {
1098 cFYI(1, ("last entry in buf at pos %lld %s",
1099 file->f_pos, tmp_buf));
1100 cifs_save_resume_key(current_entry, cifsFile);
1104 nxt_dir_entry(current_entry, end_of_smb,
1105 cifsFile->srch_inf.info_level);