linux/fs/cifs/readdir.c
<<
>>
Prefs
   1/*
   2 *   fs/cifs/readdir.c
   3 *
   4 *   Directory search handling
   5 *
   6 *   Copyright (C) International Business Machines  Corp., 2004, 2008
   7 *   Copyright (C) Red Hat, Inc., 2011
   8 *   Author(s): Steve French (sfrench@us.ibm.com)
   9 *
  10 *   This library is free software; you can redistribute it and/or modify
  11 *   it under the terms of the GNU Lesser General Public License as published
  12 *   by the Free Software Foundation; either version 2.1 of the License, or
  13 *   (at your option) any later version.
  14 *
  15 *   This library is distributed in the hope that it will be useful,
  16 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  17 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
  18 *   the GNU Lesser General Public License for more details.
  19 *
  20 *   You should have received a copy of the GNU Lesser General Public License
  21 *   along with this library; if not, write to the Free Software
  22 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23 */
  24#include <linux/fs.h>
  25#include <linux/pagemap.h>
  26#include <linux/slab.h>
  27#include <linux/stat.h>
  28#include "cifspdu.h"
  29#include "cifsglob.h"
  30#include "cifsproto.h"
  31#include "cifs_unicode.h"
  32#include "cifs_debug.h"
  33#include "cifs_fs_sb.h"
  34#include "cifsfs.h"
  35
  36/*
  37 * To be safe - for UCS to UTF-8 with strings loaded with the rare long
  38 * characters alloc more to account for such multibyte target UTF-8
  39 * characters.
  40 */
  41#define UNICODE_NAME_MAX ((4 * NAME_MAX) + 2)
  42
  43#ifdef CONFIG_CIFS_DEBUG2
  44static void dump_cifs_file_struct(struct file *file, char *label)
  45{
  46        struct cifsFileInfo *cf;
  47
  48        if (file) {
  49                cf = file->private_data;
  50                if (cf == NULL) {
  51                        cFYI(1, "empty cifs private file data");
  52                        return;
  53                }
  54                if (cf->invalidHandle)
  55                        cFYI(1, "invalid handle");
  56                if (cf->srch_inf.endOfSearch)
  57                        cFYI(1, "end of search");
  58                if (cf->srch_inf.emptyDir)
  59                        cFYI(1, "empty dir");
  60        }
  61}
  62#else
  63static inline void dump_cifs_file_struct(struct file *file, char *label)
  64{
  65}
  66#endif /* DEBUG2 */
  67
  68/*
  69 * Attempt to preload the dcache with the results from the FIND_FIRST/NEXT
  70 *
  71 * Find the dentry that matches "name". If there isn't one, create one. If it's
  72 * a negative dentry or the uniqueid changed, then drop it and recreate it.
  73 */
  74static void
  75cifs_prime_dcache(struct dentry *parent, struct qstr *name,
  76                    struct cifs_fattr *fattr)
  77{
  78        struct dentry *dentry, *alias;
  79        struct inode *inode;
  80        struct super_block *sb = parent->d_inode->i_sb;
  81        struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  82
  83        cFYI(1, "%s: for %s", __func__, name->name);
  84
  85        if (parent->d_op && parent->d_op->d_hash)
  86                parent->d_op->d_hash(parent, parent->d_inode, name);
  87        else
  88                name->hash = full_name_hash(name->name, name->len);
  89
  90        dentry = d_lookup(parent, name);
  91        if (dentry) {
  92                int err;
  93
  94                inode = dentry->d_inode;
  95                if (inode) {
  96                        /*
  97                         * If we're generating inode numbers, then we don't
  98                         * want to clobber the existing one with the one that
  99                         * the readdir code created.
 100                         */
 101                        if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM))
 102                                fattr->cf_uniqueid = CIFS_I(inode)->uniqueid;
 103
 104                        /* update inode in place if i_ino didn't change */
 105                        if (CIFS_I(inode)->uniqueid == fattr->cf_uniqueid) {
 106                                cifs_fattr_to_inode(inode, fattr);
 107                                goto out;
 108                        }
 109                }
 110                err = d_invalidate(dentry);
 111                dput(dentry);
 112                if (err)
 113                        return;
 114        }
 115
 116        dentry = d_alloc(parent, name);
 117        if (!dentry)
 118                return;
 119
 120        inode = cifs_iget(sb, fattr);
 121        if (!inode)
 122                goto out;
 123
 124        alias = d_materialise_unique(dentry, inode);
 125        if (alias && !IS_ERR(alias))
 126                dput(alias);
 127out:
 128        dput(dentry);
 129}
 130
 131static void
 132cifs_fill_common_info(struct cifs_fattr *fattr, struct cifs_sb_info *cifs_sb)
 133{
 134        fattr->cf_uid = cifs_sb->mnt_uid;
 135        fattr->cf_gid = cifs_sb->mnt_gid;
 136
 137        if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
 138                fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
 139                fattr->cf_dtype = DT_DIR;
 140        } else {
 141                fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
 142                fattr->cf_dtype = DT_REG;
 143        }
 144
 145        if (fattr->cf_cifsattrs & ATTR_READONLY)
 146                fattr->cf_mode &= ~S_IWUGO;
 147
 148        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL &&
 149            fattr->cf_cifsattrs & ATTR_SYSTEM) {
 150                if (fattr->cf_eof == 0)  {
 151                        fattr->cf_mode &= ~S_IFMT;
 152                        fattr->cf_mode |= S_IFIFO;
 153                        fattr->cf_dtype = DT_FIFO;
 154                } else {
 155                        /*
 156                         * trying to get the type and mode via SFU can be slow,
 157                         * so just call those regular files for now, and mark
 158                         * for reval
 159                         */
 160                        fattr->cf_flags |= CIFS_FATTR_NEED_REVAL;
 161                }
 162        }
 163}
 164
 165void
 166cifs_dir_info_to_fattr(struct cifs_fattr *fattr, FILE_DIRECTORY_INFO *info,
 167                       struct cifs_sb_info *cifs_sb)
 168{
 169        memset(fattr, 0, sizeof(*fattr));
 170        fattr->cf_cifsattrs = le32_to_cpu(info->ExtFileAttributes);
 171        fattr->cf_eof = le64_to_cpu(info->EndOfFile);
 172        fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
 173        fattr->cf_createtime = le64_to_cpu(info->CreationTime);
 174        fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
 175        fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
 176        fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
 177
 178        cifs_fill_common_info(fattr, cifs_sb);
 179}
 180
 181static void
 182cifs_std_info_to_fattr(struct cifs_fattr *fattr, FIND_FILE_STANDARD_INFO *info,
 183                       struct cifs_sb_info *cifs_sb)
 184{
 185        int offset = cifs_sb_master_tcon(cifs_sb)->ses->server->timeAdj;
 186
 187        memset(fattr, 0, sizeof(*fattr));
 188        fattr->cf_atime = cnvrtDosUnixTm(info->LastAccessDate,
 189                                            info->LastAccessTime, offset);
 190        fattr->cf_ctime = cnvrtDosUnixTm(info->LastWriteDate,
 191                                            info->LastWriteTime, offset);
 192        fattr->cf_mtime = cnvrtDosUnixTm(info->LastWriteDate,
 193                                            info->LastWriteTime, offset);
 194
 195        fattr->cf_cifsattrs = le16_to_cpu(info->Attributes);
 196        fattr->cf_bytes = le32_to_cpu(info->AllocationSize);
 197        fattr->cf_eof = le32_to_cpu(info->DataSize);
 198
 199        cifs_fill_common_info(fattr, cifs_sb);
 200}
 201
 202/* BB eventually need to add the following helper function to
 203      resolve NT_STATUS_STOPPED_ON_SYMLINK return code when
 204      we try to do FindFirst on (NTFS) directory symlinks */
 205/*
 206int get_symlink_reparse_path(char *full_path, struct cifs_sb_info *cifs_sb,
 207                             unsigned int xid)
 208{
 209        __u16 fid;
 210        int len;
 211        int oplock = 0;
 212        int rc;
 213        struct cifs_tcon *ptcon = cifs_sb_tcon(cifs_sb);
 214        char *tmpbuffer;
 215
 216        rc = CIFSSMBOpen(xid, ptcon, full_path, FILE_OPEN, GENERIC_READ,
 217                        OPEN_REPARSE_POINT, &fid, &oplock, NULL,
 218                        cifs_sb->local_nls,
 219                        cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
 220        if (!rc) {
 221                tmpbuffer = kmalloc(maxpath);
 222                rc = CIFSSMBQueryReparseLinkInfo(xid, ptcon, full_path,
 223                                tmpbuffer,
 224                                maxpath -1,
 225                                fid,
 226                                cifs_sb->local_nls);
 227                if (CIFSSMBClose(xid, ptcon, fid)) {
 228                        cFYI(1, "Error closing temporary reparsepoint open");
 229                }
 230        }
 231}
 232 */
 233
 234static int
 235initiate_cifs_search(const unsigned int xid, struct file *file)
 236{
 237        __u16 search_flags;
 238        int rc = 0;
 239        char *full_path = NULL;
 240        struct cifsFileInfo *cifsFile;
 241        struct cifs_sb_info *cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
 242        struct tcon_link *tlink = NULL;
 243        struct cifs_tcon *tcon;
 244        struct TCP_Server_Info *server;
 245
 246        if (file->private_data == NULL) {
 247                tlink = cifs_sb_tlink(cifs_sb);
 248                if (IS_ERR(tlink))
 249                        return PTR_ERR(tlink);
 250
 251                cifsFile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
 252                if (cifsFile == NULL) {
 253                        rc = -ENOMEM;
 254                        goto error_exit;
 255                }
 256                file->private_data = cifsFile;
 257                cifsFile->tlink = cifs_get_tlink(tlink);
 258                tcon = tlink_tcon(tlink);
 259        } else {
 260                cifsFile = file->private_data;
 261                tcon = tlink_tcon(cifsFile->tlink);
 262        }
 263
 264        server = tcon->ses->server;
 265
 266        if (!server->ops->query_dir_first) {
 267                rc = -ENOSYS;
 268                goto error_exit;
 269        }
 270
 271        cifsFile->invalidHandle = true;
 272        cifsFile->srch_inf.endOfSearch = false;
 273
 274        full_path = build_path_from_dentry(file->f_path.dentry);
 275        if (full_path == NULL) {
 276                rc = -ENOMEM;
 277                goto error_exit;
 278        }
 279
 280        cFYI(1, "Full path: %s start at: %lld", full_path, file->f_pos);
 281
 282ffirst_retry:
 283        /* test for Unix extensions */
 284        /* but now check for them on the share/mount not on the SMB session */
 285        /* if (cap_unix(tcon->ses) { */
 286        if (tcon->unix_ext)
 287                cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
 288        else if ((tcon->ses->capabilities &
 289                  tcon->ses->server->vals->cap_nt_find) == 0) {
 290                cifsFile->srch_inf.info_level = SMB_FIND_FILE_INFO_STANDARD;
 291        } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
 292                cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
 293        } else /* not srvinos - BB fixme add check for backlevel? */ {
 294                cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
 295        }
 296
 297        search_flags = CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME;
 298        if (backup_cred(cifs_sb))
 299                search_flags |= CIFS_SEARCH_BACKUP_SEARCH;
 300
 301        rc = server->ops->query_dir_first(xid, tcon, full_path, cifs_sb,
 302                                          &cifsFile->fid, search_flags,
 303                                          &cifsFile->srch_inf);
 304
 305        if (rc == 0)
 306                cifsFile->invalidHandle = false;
 307        /* BB add following call to handle readdir on new NTFS symlink errors
 308        else if STATUS_STOPPED_ON_SYMLINK
 309                call get_symlink_reparse_path and retry with new path */
 310        else if ((rc == -EOPNOTSUPP) &&
 311                (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
 312                cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
 313                goto ffirst_retry;
 314        }
 315error_exit:
 316        kfree(full_path);
 317        cifs_put_tlink(tlink);
 318        return rc;
 319}
 320
 321/* return length of unicode string in bytes */
 322static int cifs_unicode_bytelen(const char *str)
 323{
 324        int len;
 325        const __le16 *ustr = (const __le16 *)str;
 326
 327        for (len = 0; len <= PATH_MAX; len++) {
 328                if (ustr[len] == 0)
 329                        return len << 1;
 330        }
 331        cFYI(1, "Unicode string longer than PATH_MAX found");
 332        return len << 1;
 333}
 334
 335static char *nxt_dir_entry(char *old_entry, char *end_of_smb, int level)
 336{
 337        char *new_entry;
 338        FILE_DIRECTORY_INFO *pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
 339
 340        if (level == SMB_FIND_FILE_INFO_STANDARD) {
 341                FIND_FILE_STANDARD_INFO *pfData;
 342                pfData = (FIND_FILE_STANDARD_INFO *)pDirInfo;
 343
 344                new_entry = old_entry + sizeof(FIND_FILE_STANDARD_INFO) +
 345                                pfData->FileNameLength;
 346        } else
 347                new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
 348        cFYI(1, "new entry %p old entry %p", new_entry, old_entry);
 349        /* validate that new_entry is not past end of SMB */
 350        if (new_entry >= end_of_smb) {
 351                cERROR(1, "search entry %p began after end of SMB %p old entry %p",
 352                        new_entry, end_of_smb, old_entry);
 353                return NULL;
 354        } else if (((level == SMB_FIND_FILE_INFO_STANDARD) &&
 355                    (new_entry + sizeof(FIND_FILE_STANDARD_INFO) > end_of_smb))
 356                  || ((level != SMB_FIND_FILE_INFO_STANDARD) &&
 357                   (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb)))  {
 358                cERROR(1, "search entry %p extends after end of SMB %p",
 359                        new_entry, end_of_smb);
 360                return NULL;
 361        } else
 362                return new_entry;
 363
 364}
 365
 366struct cifs_dirent {
 367        const char      *name;
 368        size_t          namelen;
 369        u32             resume_key;
 370        u64             ino;
 371};
 372
 373static void cifs_fill_dirent_unix(struct cifs_dirent *de,
 374                const FILE_UNIX_INFO *info, bool is_unicode)
 375{
 376        de->name = &info->FileName[0];
 377        if (is_unicode)
 378                de->namelen = cifs_unicode_bytelen(de->name);
 379        else
 380                de->namelen = strnlen(de->name, PATH_MAX);
 381        de->resume_key = info->ResumeKey;
 382        de->ino = le64_to_cpu(info->basic.UniqueId);
 383}
 384
 385static void cifs_fill_dirent_dir(struct cifs_dirent *de,
 386                const FILE_DIRECTORY_INFO *info)
 387{
 388        de->name = &info->FileName[0];
 389        de->namelen = le32_to_cpu(info->FileNameLength);
 390        de->resume_key = info->FileIndex;
 391}
 392
 393static void cifs_fill_dirent_full(struct cifs_dirent *de,
 394                const FILE_FULL_DIRECTORY_INFO *info)
 395{
 396        de->name = &info->FileName[0];
 397        de->namelen = le32_to_cpu(info->FileNameLength);
 398        de->resume_key = info->FileIndex;
 399}
 400
 401static void cifs_fill_dirent_search(struct cifs_dirent *de,
 402                const SEARCH_ID_FULL_DIR_INFO *info)
 403{
 404        de->name = &info->FileName[0];
 405        de->namelen = le32_to_cpu(info->FileNameLength);
 406        de->resume_key = info->FileIndex;
 407        de->ino = le64_to_cpu(info->UniqueId);
 408}
 409
 410static void cifs_fill_dirent_both(struct cifs_dirent *de,
 411                const FILE_BOTH_DIRECTORY_INFO *info)
 412{
 413        de->name = &info->FileName[0];
 414        de->namelen = le32_to_cpu(info->FileNameLength);
 415        de->resume_key = info->FileIndex;
 416}
 417
 418static void cifs_fill_dirent_std(struct cifs_dirent *de,
 419                const FIND_FILE_STANDARD_INFO *info)
 420{
 421        de->name = &info->FileName[0];
 422        /* one byte length, no endianess conversion */
 423        de->namelen = info->FileNameLength;
 424        de->resume_key = info->ResumeKey;
 425}
 426
 427static int cifs_fill_dirent(struct cifs_dirent *de, const void *info,
 428                u16 level, bool is_unicode)
 429{
 430        memset(de, 0, sizeof(*de));
 431
 432        switch (level) {
 433        case SMB_FIND_FILE_UNIX:
 434                cifs_fill_dirent_unix(de, info, is_unicode);
 435                break;
 436        case SMB_FIND_FILE_DIRECTORY_INFO:
 437                cifs_fill_dirent_dir(de, info);
 438                break;
 439        case SMB_FIND_FILE_FULL_DIRECTORY_INFO:
 440                cifs_fill_dirent_full(de, info);
 441                break;
 442        case SMB_FIND_FILE_ID_FULL_DIR_INFO:
 443                cifs_fill_dirent_search(de, info);
 444                break;
 445        case SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
 446                cifs_fill_dirent_both(de, info);
 447                break;
 448        case SMB_FIND_FILE_INFO_STANDARD:
 449                cifs_fill_dirent_std(de, info);
 450                break;
 451        default:
 452                cFYI(1, "Unknown findfirst level %d", level);
 453                return -EINVAL;
 454        }
 455
 456        return 0;
 457}
 458
 459#define UNICODE_DOT cpu_to_le16(0x2e)
 460
 461/* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
 462static int cifs_entry_is_dot(struct cifs_dirent *de, bool is_unicode)
 463{
 464        int rc = 0;
 465
 466        if (!de->name)
 467                return 0;
 468
 469        if (is_unicode) {
 470                __le16 *ufilename = (__le16 *)de->name;
 471                if (de->namelen == 2) {
 472                        /* check for . */
 473                        if (ufilename[0] == UNICODE_DOT)
 474                                rc = 1;
 475                } else if (de->namelen == 4) {
 476                        /* check for .. */
 477                        if (ufilename[0] == UNICODE_DOT &&
 478                            ufilename[1] == UNICODE_DOT)
 479                                rc = 2;
 480                }
 481        } else /* ASCII */ {
 482                if (de->namelen == 1) {
 483                        if (de->name[0] == '.')
 484                                rc = 1;
 485                } else if (de->namelen == 2) {
 486                        if (de->name[0] == '.' && de->name[1] == '.')
 487                                rc = 2;
 488                }
 489        }
 490
 491        return rc;
 492}
 493
 494/* Check if directory that we are searching has changed so we can decide
 495   whether we can use the cached search results from the previous search */
 496static int is_dir_changed(struct file *file)
 497{
 498        struct inode *inode = file->f_path.dentry->d_inode;
 499        struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
 500
 501        if (cifsInfo->time == 0)
 502                return 1; /* directory was changed, perhaps due to unlink */
 503        else
 504                return 0;
 505
 506}
 507
 508static int cifs_save_resume_key(const char *current_entry,
 509        struct cifsFileInfo *file_info)
 510{
 511        struct cifs_dirent de;
 512        int rc;
 513
 514        rc = cifs_fill_dirent(&de, current_entry, file_info->srch_inf.info_level,
 515                              file_info->srch_inf.unicode);
 516        if (!rc) {
 517                file_info->srch_inf.presume_name = de.name;
 518                file_info->srch_inf.resume_name_len = de.namelen;
 519                file_info->srch_inf.resume_key = de.resume_key;
 520        }
 521        return rc;
 522}
 523
 524/*
 525 * Find the corresponding entry in the search. Note that the SMB server returns
 526 * search entries for . and .. which complicates logic here if we choose to
 527 * parse for them and we do not assume that they are located in the findfirst
 528 * return buffer. We start counting in the buffer with entry 2 and increment for
 529 * every entry (do not increment for . or .. entry).
 530 */
 531static int
 532find_cifs_entry(const unsigned int xid, struct cifs_tcon *tcon,
 533                struct file *file, char **current_entry, int *num_to_ret)
 534{
 535        __u16 search_flags;
 536        int rc = 0;
 537        int pos_in_buf = 0;
 538        loff_t first_entry_in_buffer;
 539        loff_t index_to_find = file->f_pos;
 540        struct cifsFileInfo *cfile = file->private_data;
 541        struct cifs_sb_info *cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
 542        struct TCP_Server_Info *server = tcon->ses->server;
 543        /* check if index in the buffer */
 544
 545        if (!server->ops->query_dir_first || !server->ops->query_dir_next)
 546                return -ENOSYS;
 547
 548        if ((cfile == NULL) || (current_entry == NULL) || (num_to_ret == NULL))
 549                return -ENOENT;
 550
 551        *current_entry = NULL;
 552        first_entry_in_buffer = cfile->srch_inf.index_of_last_entry -
 553                                        cfile->srch_inf.entries_in_buffer;
 554
 555        /*
 556         * If first entry in buf is zero then is first buffer
 557         * in search response data which means it is likely . and ..
 558         * will be in this buffer, although some servers do not return
 559         * . and .. for the root of a drive and for those we need
 560         * to start two entries earlier.
 561         */
 562
 563        dump_cifs_file_struct(file, "In fce ");
 564        if (((index_to_find < cfile->srch_inf.index_of_last_entry) &&
 565             is_dir_changed(file)) || (index_to_find < first_entry_in_buffer)) {
 566                /* close and restart search */
 567                cFYI(1, "search backing up - close and restart search");
 568                spin_lock(&cifs_file_list_lock);
 569                if (!cfile->srch_inf.endOfSearch && !cfile->invalidHandle) {
 570                        cfile->invalidHandle = true;
 571                        spin_unlock(&cifs_file_list_lock);
 572                        if (server->ops->close)
 573                                server->ops->close(xid, tcon, &cfile->fid);
 574                } else
 575                        spin_unlock(&cifs_file_list_lock);
 576                if (cfile->srch_inf.ntwrk_buf_start) {
 577                        cFYI(1, "freeing SMB ff cache buf on search rewind");
 578                        if (cfile->srch_inf.smallBuf)
 579                                cifs_small_buf_release(cfile->srch_inf.
 580                                                ntwrk_buf_start);
 581                        else
 582                                cifs_buf_release(cfile->srch_inf.
 583                                                ntwrk_buf_start);
 584                        cfile->srch_inf.ntwrk_buf_start = NULL;
 585                }
 586                rc = initiate_cifs_search(xid, file);
 587                if (rc) {
 588                        cFYI(1, "error %d reinitiating a search on rewind",
 589                                 rc);
 590                        return rc;
 591                }
 592                /* FindFirst/Next set last_entry to NULL on malformed reply */
 593                if (cfile->srch_inf.last_entry)
 594                        cifs_save_resume_key(cfile->srch_inf.last_entry, cfile);
 595        }
 596
 597        search_flags = CIFS_SEARCH_CLOSE_AT_END | CIFS_SEARCH_RETURN_RESUME;
 598        if (backup_cred(cifs_sb))
 599                search_flags |= CIFS_SEARCH_BACKUP_SEARCH;
 600
 601        while ((index_to_find >= cfile->srch_inf.index_of_last_entry) &&
 602               (rc == 0) && !cfile->srch_inf.endOfSearch) {
 603                cFYI(1, "calling findnext2");
 604                rc = server->ops->query_dir_next(xid, tcon, &cfile->fid,
 605                                                 search_flags,
 606                                                 &cfile->srch_inf);
 607                /* FindFirst/Next set last_entry to NULL on malformed reply */
 608                if (cfile->srch_inf.last_entry)
 609                        cifs_save_resume_key(cfile->srch_inf.last_entry, cfile);
 610                if (rc)
 611                        return -ENOENT;
 612        }
 613        if (index_to_find < cfile->srch_inf.index_of_last_entry) {
 614                /* we found the buffer that contains the entry */
 615                /* scan and find it */
 616                int i;
 617                char *cur_ent;
 618                char *end_of_smb = cfile->srch_inf.ntwrk_buf_start +
 619                        server->ops->calc_smb_size(
 620                                        cfile->srch_inf.ntwrk_buf_start);
 621
 622                cur_ent = cfile->srch_inf.srch_entries_start;
 623                first_entry_in_buffer = cfile->srch_inf.index_of_last_entry
 624                                        - cfile->srch_inf.entries_in_buffer;
 625                pos_in_buf = index_to_find - first_entry_in_buffer;
 626                cFYI(1, "found entry - pos_in_buf %d", pos_in_buf);
 627
 628                for (i = 0; (i < (pos_in_buf)) && (cur_ent != NULL); i++) {
 629                        /* go entry by entry figuring out which is first */
 630                        cur_ent = nxt_dir_entry(cur_ent, end_of_smb,
 631                                                cfile->srch_inf.info_level);
 632                }
 633                if ((cur_ent == NULL) && (i < pos_in_buf)) {
 634                        /* BB fixme - check if we should flag this error */
 635                        cERROR(1, "reached end of buf searching for pos in buf"
 636                                  " %d index to find %lld rc %d", pos_in_buf,
 637                                  index_to_find, rc);
 638                }
 639                rc = 0;
 640                *current_entry = cur_ent;
 641        } else {
 642                cFYI(1, "index not in buffer - could not findnext into it");
 643                return 0;
 644        }
 645
 646        if (pos_in_buf >= cfile->srch_inf.entries_in_buffer) {
 647                cFYI(1, "can not return entries pos_in_buf beyond last");
 648                *num_to_ret = 0;
 649        } else
 650                *num_to_ret = cfile->srch_inf.entries_in_buffer - pos_in_buf;
 651
 652        return rc;
 653}
 654
 655static int cifs_filldir(char *find_entry, struct file *file, filldir_t filldir,
 656                void *dirent, char *scratch_buf, unsigned int max_len)
 657{
 658        struct cifsFileInfo *file_info = file->private_data;
 659        struct super_block *sb = file->f_path.dentry->d_sb;
 660        struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
 661        struct cifs_dirent de = { NULL, };
 662        struct cifs_fattr fattr;
 663        struct qstr name;
 664        int rc = 0;
 665        ino_t ino;
 666
 667        rc = cifs_fill_dirent(&de, find_entry, file_info->srch_inf.info_level,
 668                              file_info->srch_inf.unicode);
 669        if (rc)
 670                return rc;
 671
 672        if (de.namelen > max_len) {
 673                cERROR(1, "bad search response length %zd past smb end",
 674                          de.namelen);
 675                return -EINVAL;
 676        }
 677
 678        /* skip . and .. since we added them first */
 679        if (cifs_entry_is_dot(&de, file_info->srch_inf.unicode))
 680                return 0;
 681
 682        if (file_info->srch_inf.unicode) {
 683                struct nls_table *nlt = cifs_sb->local_nls;
 684
 685                name.name = scratch_buf;
 686                name.len =
 687                        cifs_from_utf16((char *)name.name, (__le16 *)de.name,
 688                                        UNICODE_NAME_MAX,
 689                                        min_t(size_t, de.namelen,
 690                                              (size_t)max_len), nlt,
 691                                        cifs_sb->mnt_cifs_flags &
 692                                                CIFS_MOUNT_MAP_SPECIAL_CHR);
 693                name.len -= nls_nullsize(nlt);
 694        } else {
 695                name.name = de.name;
 696                name.len = de.namelen;
 697        }
 698
 699        switch (file_info->srch_inf.info_level) {
 700        case SMB_FIND_FILE_UNIX:
 701                cifs_unix_basic_to_fattr(&fattr,
 702                                         &((FILE_UNIX_INFO *)find_entry)->basic,
 703                                         cifs_sb);
 704                break;
 705        case SMB_FIND_FILE_INFO_STANDARD:
 706                cifs_std_info_to_fattr(&fattr,
 707                                       (FIND_FILE_STANDARD_INFO *)find_entry,
 708                                       cifs_sb);
 709                break;
 710        default:
 711                cifs_dir_info_to_fattr(&fattr,
 712                                       (FILE_DIRECTORY_INFO *)find_entry,
 713                                       cifs_sb);
 714                break;
 715        }
 716
 717        if (de.ino && (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
 718                fattr.cf_uniqueid = de.ino;
 719        } else {
 720                fattr.cf_uniqueid = iunique(sb, ROOT_I);
 721                cifs_autodisable_serverino(cifs_sb);
 722        }
 723
 724        if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) &&
 725            CIFSCouldBeMFSymlink(&fattr))
 726                /*
 727                 * trying to get the type and mode can be slow,
 728                 * so just call those regular files for now, and mark
 729                 * for reval
 730                 */
 731                fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
 732
 733        cifs_prime_dcache(file->f_dentry, &name, &fattr);
 734
 735        ino = cifs_uniqueid_to_ino_t(fattr.cf_uniqueid);
 736        rc = filldir(dirent, name.name, name.len, file->f_pos, ino,
 737                     fattr.cf_dtype);
 738        return rc;
 739}
 740
 741
 742int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
 743{
 744        int rc = 0;
 745        unsigned int xid;
 746        int i;
 747        struct cifs_tcon *tcon;
 748        struct cifsFileInfo *cifsFile = NULL;
 749        char *current_entry;
 750        int num_to_fill = 0;
 751        char *tmp_buf = NULL;
 752        char *end_of_smb;
 753        unsigned int max_len;
 754
 755        xid = get_xid();
 756
 757        /*
 758         * Ensure FindFirst doesn't fail before doing filldir() for '.' and
 759         * '..'. Otherwise we won't be able to notify VFS in case of failure.
 760         */
 761        if (file->private_data == NULL) {
 762                rc = initiate_cifs_search(xid, file);
 763                cFYI(1, "initiate cifs search rc %d", rc);
 764                if (rc)
 765                        goto rddir2_exit;
 766        }
 767
 768        switch ((int) file->f_pos) {
 769        case 0:
 770                if (filldir(direntry, ".", 1, file->f_pos,
 771                     file->f_path.dentry->d_inode->i_ino, DT_DIR) < 0) {
 772                        cERROR(1, "Filldir for current dir failed");
 773                        rc = -ENOMEM;
 774                        break;
 775                }
 776                file->f_pos++;
 777        case 1:
 778                if (filldir(direntry, "..", 2, file->f_pos,
 779                     parent_ino(file->f_path.dentry), DT_DIR) < 0) {
 780                        cERROR(1, "Filldir for parent dir failed");
 781                        rc = -ENOMEM;
 782                        break;
 783                }
 784                file->f_pos++;
 785        default:
 786                /* 1) If search is active,
 787                        is in current search buffer?
 788                        if it before then restart search
 789                        if after then keep searching till find it */
 790
 791                if (file->private_data == NULL) {
 792                        rc = -EINVAL;
 793                        free_xid(xid);
 794                        return rc;
 795                }
 796                cifsFile = file->private_data;
 797                if (cifsFile->srch_inf.endOfSearch) {
 798                        if (cifsFile->srch_inf.emptyDir) {
 799                                cFYI(1, "End of search, empty dir");
 800                                rc = 0;
 801                                break;
 802                        }
 803                } /* else {
 804                        cifsFile->invalidHandle = true;
 805                        tcon->ses->server->close(xid, tcon, &cifsFile->fid);
 806                } */
 807
 808                tcon = tlink_tcon(cifsFile->tlink);
 809                rc = find_cifs_entry(xid, tcon, file, &current_entry,
 810                                     &num_to_fill);
 811                if (rc) {
 812                        cFYI(1, "fce error %d", rc);
 813                        goto rddir2_exit;
 814                } else if (current_entry != NULL) {
 815                        cFYI(1, "entry %lld found", file->f_pos);
 816                } else {
 817                        cFYI(1, "could not find entry");
 818                        goto rddir2_exit;
 819                }
 820                cFYI(1, "loop through %d times filling dir for net buf %p",
 821                        num_to_fill, cifsFile->srch_inf.ntwrk_buf_start);
 822                max_len = tcon->ses->server->ops->calc_smb_size(
 823                                cifsFile->srch_inf.ntwrk_buf_start);
 824                end_of_smb = cifsFile->srch_inf.ntwrk_buf_start + max_len;
 825
 826                tmp_buf = kmalloc(UNICODE_NAME_MAX, GFP_KERNEL);
 827                if (tmp_buf == NULL) {
 828                        rc = -ENOMEM;
 829                        break;
 830                }
 831
 832                for (i = 0; (i < num_to_fill) && (rc == 0); i++) {
 833                        if (current_entry == NULL) {
 834                                /* evaluate whether this case is an error */
 835                                cERROR(1, "past SMB end,  num to fill %d i %d",
 836                                          num_to_fill, i);
 837                                break;
 838                        }
 839                        /*
 840                         * if buggy server returns . and .. late do we want to
 841                         * check for that here?
 842                         */
 843                        rc = cifs_filldir(current_entry, file, filldir,
 844                                          direntry, tmp_buf, max_len);
 845                        if (rc == -EOVERFLOW) {
 846                                rc = 0;
 847                                break;
 848                        }
 849
 850                        file->f_pos++;
 851                        if (file->f_pos ==
 852                                cifsFile->srch_inf.index_of_last_entry) {
 853                                cFYI(1, "last entry in buf at pos %lld %s",
 854                                        file->f_pos, tmp_buf);
 855                                cifs_save_resume_key(current_entry, cifsFile);
 856                                break;
 857                        } else
 858                                current_entry =
 859                                        nxt_dir_entry(current_entry, end_of_smb,
 860                                                cifsFile->srch_inf.info_level);
 861                }
 862                kfree(tmp_buf);
 863                break;
 864        } /* end switch */
 865
 866rddir2_exit:
 867        free_xid(xid);
 868        return rc;
 869}
 870
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.