Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: LGPL-2.1
0002 /*
0003  *
0004  *   vfs operations that deal with dentries
0005  *
0006  *   Copyright (C) International Business Machines  Corp., 2002,2009
0007  *   Author(s): Steve French (sfrench@us.ibm.com)
0008  *
0009  */
0010 #include <linux/fs.h>
0011 #include <linux/stat.h>
0012 #include <linux/slab.h>
0013 #include <linux/namei.h>
0014 #include <linux/mount.h>
0015 #include <linux/file.h>
0016 #include "cifsfs.h"
0017 #include "cifspdu.h"
0018 #include "cifsglob.h"
0019 #include "cifsproto.h"
0020 #include "cifs_debug.h"
0021 #include "cifs_fs_sb.h"
0022 #include "cifs_unicode.h"
0023 #include "fs_context.h"
0024 #include "cifs_ioctl.h"
0025 #include "fscache.h"
0026 
0027 static void
0028 renew_parental_timestamps(struct dentry *direntry)
0029 {
0030     /* BB check if there is a way to get the kernel to do this or if we
0031        really need this */
0032     do {
0033         cifs_set_time(direntry, jiffies);
0034         direntry = direntry->d_parent;
0035     } while (!IS_ROOT(direntry));
0036 }
0037 
0038 char *
0039 cifs_build_path_to_root(struct smb3_fs_context *ctx, struct cifs_sb_info *cifs_sb,
0040             struct cifs_tcon *tcon, int add_treename)
0041 {
0042     int pplen = ctx->prepath ? strlen(ctx->prepath) + 1 : 0;
0043     int dfsplen;
0044     char *full_path = NULL;
0045 
0046     /* if no prefix path, simply set path to the root of share to "" */
0047     if (pplen == 0) {
0048         full_path = kzalloc(1, GFP_KERNEL);
0049         return full_path;
0050     }
0051 
0052     if (add_treename)
0053         dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1);
0054     else
0055         dfsplen = 0;
0056 
0057     full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL);
0058     if (full_path == NULL)
0059         return full_path;
0060 
0061     if (dfsplen)
0062         memcpy(full_path, tcon->treeName, dfsplen);
0063     full_path[dfsplen] = CIFS_DIR_SEP(cifs_sb);
0064     memcpy(full_path + dfsplen + 1, ctx->prepath, pplen);
0065     convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
0066     return full_path;
0067 }
0068 
0069 /* Note: caller must free return buffer */
0070 const char *
0071 build_path_from_dentry(struct dentry *direntry, void *page)
0072 {
0073     struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
0074     struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
0075     bool prefix = tcon->Flags & SMB_SHARE_IS_IN_DFS;
0076 
0077     return build_path_from_dentry_optional_prefix(direntry, page,
0078                               prefix);
0079 }
0080 
0081 char *
0082 build_path_from_dentry_optional_prefix(struct dentry *direntry, void *page,
0083                        bool prefix)
0084 {
0085     int dfsplen;
0086     int pplen = 0;
0087     struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
0088     struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
0089     char dirsep = CIFS_DIR_SEP(cifs_sb);
0090     char *s;
0091 
0092     if (unlikely(!page))
0093         return ERR_PTR(-ENOMEM);
0094 
0095     if (prefix)
0096         dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1);
0097     else
0098         dfsplen = 0;
0099 
0100     if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
0101         pplen = cifs_sb->prepath ? strlen(cifs_sb->prepath) + 1 : 0;
0102 
0103     s = dentry_path_raw(direntry, page, PATH_MAX);
0104     if (IS_ERR(s))
0105         return s;
0106     if (!s[1])  // for root we want "", not "/"
0107         s++;
0108     if (s < (char *)page + pplen + dfsplen)
0109         return ERR_PTR(-ENAMETOOLONG);
0110     if (pplen) {
0111         cifs_dbg(FYI, "using cifs_sb prepath <%s>\n", cifs_sb->prepath);
0112         s -= pplen;
0113         memcpy(s + 1, cifs_sb->prepath, pplen - 1);
0114         *s = '/';
0115     }
0116     if (dirsep != '/') {
0117         /* BB test paths to Windows with '/' in the midst of prepath */
0118         char *p;
0119 
0120         for (p = s; *p; p++)
0121             if (*p == '/')
0122                 *p = dirsep;
0123     }
0124     if (dfsplen) {
0125         s -= dfsplen;
0126         memcpy(s, tcon->treeName, dfsplen);
0127         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) {
0128             int i;
0129             for (i = 0; i < dfsplen; i++) {
0130                 if (s[i] == '\\')
0131                     s[i] = '/';
0132             }
0133         }
0134     }
0135     return s;
0136 }
0137 
0138 /*
0139  * Don't allow path components longer than the server max.
0140  * Don't allow the separator character in a path component.
0141  * The VFS will not allow "/", but "\" is allowed by posix.
0142  */
0143 static int
0144 check_name(struct dentry *direntry, struct cifs_tcon *tcon)
0145 {
0146     struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
0147     int i;
0148 
0149     if (unlikely(tcon->fsAttrInfo.MaxPathNameComponentLength &&
0150              direntry->d_name.len >
0151              le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength)))
0152         return -ENAMETOOLONG;
0153 
0154     if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) {
0155         for (i = 0; i < direntry->d_name.len; i++) {
0156             if (direntry->d_name.name[i] == '\\') {
0157                 cifs_dbg(FYI, "Invalid file name\n");
0158                 return -EINVAL;
0159             }
0160         }
0161     }
0162     return 0;
0163 }
0164 
0165 
0166 /* Inode operations in similar order to how they appear in Linux file fs.h */
0167 
0168 static int
0169 cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned int xid,
0170            struct tcon_link *tlink, unsigned oflags, umode_t mode,
0171            __u32 *oplock, struct cifs_fid *fid)
0172 {
0173     int rc = -ENOENT;
0174     int create_options = CREATE_NOT_DIR;
0175     int desired_access;
0176     struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
0177     struct cifs_tcon *tcon = tlink_tcon(tlink);
0178     const char *full_path;
0179     void *page = alloc_dentry_path();
0180     FILE_ALL_INFO *buf = NULL;
0181     struct inode *newinode = NULL;
0182     int disposition;
0183     struct TCP_Server_Info *server = tcon->ses->server;
0184     struct cifs_open_parms oparms;
0185 
0186     *oplock = 0;
0187     if (tcon->ses->server->oplocks)
0188         *oplock = REQ_OPLOCK;
0189 
0190     full_path = build_path_from_dentry(direntry, page);
0191     if (IS_ERR(full_path)) {
0192         free_dentry_path(page);
0193         return PTR_ERR(full_path);
0194     }
0195 
0196 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
0197     if (tcon->unix_ext && cap_unix(tcon->ses) && !tcon->broken_posix_open &&
0198         (CIFS_UNIX_POSIX_PATH_OPS_CAP &
0199             le64_to_cpu(tcon->fsUnixInfo.Capability))) {
0200         rc = cifs_posix_open(full_path, &newinode, inode->i_sb, mode,
0201                      oflags, oplock, &fid->netfid, xid);
0202         switch (rc) {
0203         case 0:
0204             if (newinode == NULL) {
0205                 /* query inode info */
0206                 goto cifs_create_get_file_info;
0207             }
0208 
0209             if (S_ISDIR(newinode->i_mode)) {
0210                 CIFSSMBClose(xid, tcon, fid->netfid);
0211                 iput(newinode);
0212                 rc = -EISDIR;
0213                 goto out;
0214             }
0215 
0216             if (!S_ISREG(newinode->i_mode)) {
0217                 /*
0218                  * The server may allow us to open things like
0219                  * FIFOs, but the client isn't set up to deal
0220                  * with that. If it's not a regular file, just
0221                  * close it and proceed as if it were a normal
0222                  * lookup.
0223                  */
0224                 CIFSSMBClose(xid, tcon, fid->netfid);
0225                 goto cifs_create_get_file_info;
0226             }
0227             /* success, no need to query */
0228             goto cifs_create_set_dentry;
0229 
0230         case -ENOENT:
0231             goto cifs_create_get_file_info;
0232 
0233         case -EIO:
0234         case -EINVAL:
0235             /*
0236              * EIO could indicate that (posix open) operation is not
0237              * supported, despite what server claimed in capability
0238              * negotiation.
0239              *
0240              * POSIX open in samba versions 3.3.1 and earlier could
0241              * incorrectly fail with invalid parameter.
0242              */
0243             tcon->broken_posix_open = true;
0244             break;
0245 
0246         case -EREMOTE:
0247         case -EOPNOTSUPP:
0248             /*
0249              * EREMOTE indicates DFS junction, which is not handled
0250              * in posix open.  If either that or op not supported
0251              * returned, follow the normal lookup.
0252              */
0253             break;
0254 
0255         default:
0256             goto out;
0257         }
0258         /*
0259          * fallthrough to retry, using older open call, this is case
0260          * where server does not support this SMB level, and falsely
0261          * claims capability (also get here for DFS case which should be
0262          * rare for path not covered on files)
0263          */
0264     }
0265 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
0266 
0267     desired_access = 0;
0268     if (OPEN_FMODE(oflags) & FMODE_READ)
0269         desired_access |= GENERIC_READ; /* is this too little? */
0270     if (OPEN_FMODE(oflags) & FMODE_WRITE)
0271         desired_access |= GENERIC_WRITE;
0272 
0273     disposition = FILE_OVERWRITE_IF;
0274     if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
0275         disposition = FILE_CREATE;
0276     else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
0277         disposition = FILE_OVERWRITE_IF;
0278     else if ((oflags & O_CREAT) == O_CREAT)
0279         disposition = FILE_OPEN_IF;
0280     else
0281         cifs_dbg(FYI, "Create flag not set in create function\n");
0282 
0283     /*
0284      * BB add processing to set equivalent of mode - e.g. via CreateX with
0285      * ACLs
0286      */
0287 
0288     if (!server->ops->open) {
0289         rc = -ENOSYS;
0290         goto out;
0291     }
0292 
0293     buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
0294     if (buf == NULL) {
0295         rc = -ENOMEM;
0296         goto out;
0297     }
0298 
0299     /*
0300      * if we're not using unix extensions, see if we need to set
0301      * ATTR_READONLY on the create call
0302      */
0303     if (!tcon->unix_ext && (mode & S_IWUGO) == 0)
0304         create_options |= CREATE_OPTION_READONLY;
0305 
0306     oparms.tcon = tcon;
0307     oparms.cifs_sb = cifs_sb;
0308     oparms.desired_access = desired_access;
0309     oparms.create_options = cifs_create_options(cifs_sb, create_options);
0310     oparms.disposition = disposition;
0311     oparms.path = full_path;
0312     oparms.fid = fid;
0313     oparms.reconnect = false;
0314     oparms.mode = mode;
0315     rc = server->ops->open(xid, &oparms, oplock, buf);
0316     if (rc) {
0317         cifs_dbg(FYI, "cifs_create returned 0x%x\n", rc);
0318         goto out;
0319     }
0320 
0321 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
0322     /*
0323      * If Open reported that we actually created a file then we now have to
0324      * set the mode if possible.
0325      */
0326     if ((tcon->unix_ext) && (*oplock & CIFS_CREATE_ACTION)) {
0327         struct cifs_unix_set_info_args args = {
0328                 .mode   = mode,
0329                 .ctime  = NO_CHANGE_64,
0330                 .atime  = NO_CHANGE_64,
0331                 .mtime  = NO_CHANGE_64,
0332                 .device = 0,
0333         };
0334 
0335         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
0336             args.uid = current_fsuid();
0337             if (inode->i_mode & S_ISGID)
0338                 args.gid = inode->i_gid;
0339             else
0340                 args.gid = current_fsgid();
0341         } else {
0342             args.uid = INVALID_UID; /* no change */
0343             args.gid = INVALID_GID; /* no change */
0344         }
0345         CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid->netfid,
0346                        current->tgid);
0347     } else {
0348         /*
0349          * BB implement mode setting via Windows security
0350          * descriptors e.g.
0351          */
0352         /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/
0353 
0354         /* Could set r/o dos attribute if mode & 0222 == 0 */
0355     }
0356 
0357 cifs_create_get_file_info:
0358     /* server might mask mode so we have to query for it */
0359     if (tcon->unix_ext)
0360         rc = cifs_get_inode_info_unix(&newinode, full_path, inode->i_sb,
0361                           xid);
0362     else {
0363 #else
0364     {
0365 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
0366         /* TODO: Add support for calling POSIX query info here, but passing in fid */
0367         rc = cifs_get_inode_info(&newinode, full_path, buf, inode->i_sb,
0368                      xid, fid);
0369         if (newinode) {
0370             if (server->ops->set_lease_key)
0371                 server->ops->set_lease_key(newinode, fid);
0372             if ((*oplock & CIFS_CREATE_ACTION) && S_ISREG(newinode->i_mode)) {
0373                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
0374                     newinode->i_mode = mode;
0375                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
0376                     newinode->i_uid = current_fsuid();
0377                     if (inode->i_mode & S_ISGID)
0378                         newinode->i_gid = inode->i_gid;
0379                     else
0380                         newinode->i_gid = current_fsgid();
0381                 }
0382             }
0383         }
0384     }
0385 
0386 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
0387 cifs_create_set_dentry:
0388 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
0389     if (rc != 0) {
0390         cifs_dbg(FYI, "Create worked, get_inode_info failed rc = %d\n",
0391              rc);
0392         goto out_err;
0393     }
0394 
0395     if (newinode)
0396         if (S_ISDIR(newinode->i_mode)) {
0397             rc = -EISDIR;
0398             goto out_err;
0399         }
0400 
0401     d_drop(direntry);
0402     d_add(direntry, newinode);
0403 
0404 out:
0405     kfree(buf);
0406     free_dentry_path(page);
0407     return rc;
0408 
0409 out_err:
0410     if (server->ops->close)
0411         server->ops->close(xid, tcon, fid);
0412     if (newinode)
0413         iput(newinode);
0414     goto out;
0415 }
0416 
0417 int
0418 cifs_atomic_open(struct inode *inode, struct dentry *direntry,
0419          struct file *file, unsigned oflags, umode_t mode)
0420 {
0421     int rc;
0422     unsigned int xid;
0423     struct tcon_link *tlink;
0424     struct cifs_tcon *tcon;
0425     struct TCP_Server_Info *server;
0426     struct cifs_fid fid;
0427     struct cifs_pending_open open;
0428     __u32 oplock;
0429     struct cifsFileInfo *file_info;
0430 
0431     if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb))))
0432         return -EIO;
0433 
0434     /*
0435      * Posix open is only called (at lookup time) for file create now. For
0436      * opens (rather than creates), because we do not know if it is a file
0437      * or directory yet, and current Samba no longer allows us to do posix
0438      * open on dirs, we could end up wasting an open call on what turns out
0439      * to be a dir. For file opens, we wait to call posix open till
0440      * cifs_open.  It could be added to atomic_open in the future but the
0441      * performance tradeoff of the extra network request when EISDIR or
0442      * EACCES is returned would have to be weighed against the 50% reduction
0443      * in network traffic in the other paths.
0444      */
0445     if (!(oflags & O_CREAT)) {
0446         struct dentry *res;
0447 
0448         /*
0449          * Check for hashed negative dentry. We have already revalidated
0450          * the dentry and it is fine. No need to perform another lookup.
0451          */
0452         if (!d_in_lookup(direntry))
0453             return -ENOENT;
0454 
0455         res = cifs_lookup(inode, direntry, 0);
0456         if (IS_ERR(res))
0457             return PTR_ERR(res);
0458 
0459         return finish_no_open(file, res);
0460     }
0461 
0462     xid = get_xid();
0463 
0464     cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
0465          inode, direntry, direntry);
0466 
0467     tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb));
0468     if (IS_ERR(tlink)) {
0469         rc = PTR_ERR(tlink);
0470         goto out_free_xid;
0471     }
0472 
0473     tcon = tlink_tcon(tlink);
0474 
0475     rc = check_name(direntry, tcon);
0476     if (rc)
0477         goto out;
0478 
0479     server = tcon->ses->server;
0480 
0481     if (server->ops->new_lease_key)
0482         server->ops->new_lease_key(&fid);
0483 
0484     cifs_add_pending_open(&fid, tlink, &open);
0485 
0486     rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
0487                 &oplock, &fid);
0488 
0489     if (rc) {
0490         cifs_del_pending_open(&open);
0491         goto out;
0492     }
0493 
0494     if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
0495         file->f_mode |= FMODE_CREATED;
0496 
0497     rc = finish_open(file, direntry, generic_file_open);
0498     if (rc) {
0499         if (server->ops->close)
0500             server->ops->close(xid, tcon, &fid);
0501         cifs_del_pending_open(&open);
0502         goto out;
0503     }
0504 
0505     if (file->f_flags & O_DIRECT &&
0506         CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
0507         if (CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
0508             file->f_op = &cifs_file_direct_nobrl_ops;
0509         else
0510             file->f_op = &cifs_file_direct_ops;
0511         }
0512 
0513     file_info = cifs_new_fileinfo(&fid, file, tlink, oplock);
0514     if (file_info == NULL) {
0515         if (server->ops->close)
0516             server->ops->close(xid, tcon, &fid);
0517         cifs_del_pending_open(&open);
0518         rc = -ENOMEM;
0519         goto out;
0520     }
0521 
0522     fscache_use_cookie(cifs_inode_cookie(file_inode(file)),
0523                file->f_mode & FMODE_WRITE);
0524 
0525 out:
0526     cifs_put_tlink(tlink);
0527 out_free_xid:
0528     free_xid(xid);
0529     return rc;
0530 }
0531 
0532 int cifs_create(struct user_namespace *mnt_userns, struct inode *inode,
0533         struct dentry *direntry, umode_t mode, bool excl)
0534 {
0535     int rc;
0536     unsigned int xid = get_xid();
0537     /*
0538      * BB below access is probably too much for mknod to request
0539      *    but we have to do query and setpathinfo so requesting
0540      *    less could fail (unless we want to request getatr and setatr
0541      *    permissions (only).  At least for POSIX we do not have to
0542      *    request so much.
0543      */
0544     unsigned oflags = O_EXCL | O_CREAT | O_RDWR;
0545     struct tcon_link *tlink;
0546     struct cifs_tcon *tcon;
0547     struct TCP_Server_Info *server;
0548     struct cifs_fid fid;
0549     __u32 oplock;
0550 
0551     cifs_dbg(FYI, "cifs_create parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
0552          inode, direntry, direntry);
0553 
0554     if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb))))
0555         return -EIO;
0556 
0557     tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb));
0558     rc = PTR_ERR(tlink);
0559     if (IS_ERR(tlink))
0560         goto out_free_xid;
0561 
0562     tcon = tlink_tcon(tlink);
0563     server = tcon->ses->server;
0564 
0565     if (server->ops->new_lease_key)
0566         server->ops->new_lease_key(&fid);
0567 
0568     rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
0569                 &oplock, &fid);
0570     if (!rc && server->ops->close)
0571         server->ops->close(xid, tcon, &fid);
0572 
0573     cifs_put_tlink(tlink);
0574 out_free_xid:
0575     free_xid(xid);
0576     return rc;
0577 }
0578 
0579 int cifs_mknod(struct user_namespace *mnt_userns, struct inode *inode,
0580            struct dentry *direntry, umode_t mode, dev_t device_number)
0581 {
0582     int rc = -EPERM;
0583     unsigned int xid;
0584     struct cifs_sb_info *cifs_sb;
0585     struct tcon_link *tlink;
0586     struct cifs_tcon *tcon;
0587     const char *full_path;
0588     void *page;
0589 
0590     if (!old_valid_dev(device_number))
0591         return -EINVAL;
0592 
0593     cifs_sb = CIFS_SB(inode->i_sb);
0594     if (unlikely(cifs_forced_shutdown(cifs_sb)))
0595         return -EIO;
0596 
0597     tlink = cifs_sb_tlink(cifs_sb);
0598     if (IS_ERR(tlink))
0599         return PTR_ERR(tlink);
0600 
0601     page = alloc_dentry_path();
0602     tcon = tlink_tcon(tlink);
0603     xid = get_xid();
0604 
0605     full_path = build_path_from_dentry(direntry, page);
0606     if (IS_ERR(full_path)) {
0607         rc = PTR_ERR(full_path);
0608         goto mknod_out;
0609     }
0610 
0611     rc = tcon->ses->server->ops->make_node(xid, inode, direntry, tcon,
0612                            full_path, mode,
0613                            device_number);
0614 
0615 mknod_out:
0616     free_dentry_path(page);
0617     free_xid(xid);
0618     cifs_put_tlink(tlink);
0619     return rc;
0620 }
0621 
0622 struct dentry *
0623 cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
0624         unsigned int flags)
0625 {
0626     unsigned int xid;
0627     int rc = 0; /* to get around spurious gcc warning, set to zero here */
0628     struct cifs_sb_info *cifs_sb;
0629     struct tcon_link *tlink;
0630     struct cifs_tcon *pTcon;
0631     struct inode *newInode = NULL;
0632     const char *full_path;
0633     void *page;
0634     int retry_count = 0;
0635 
0636     xid = get_xid();
0637 
0638     cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
0639          parent_dir_inode, direntry, direntry);
0640 
0641     /* check whether path exists */
0642 
0643     cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
0644     tlink = cifs_sb_tlink(cifs_sb);
0645     if (IS_ERR(tlink)) {
0646         free_xid(xid);
0647         return ERR_CAST(tlink);
0648     }
0649     pTcon = tlink_tcon(tlink);
0650 
0651     rc = check_name(direntry, pTcon);
0652     if (unlikely(rc)) {
0653         cifs_put_tlink(tlink);
0654         free_xid(xid);
0655         return ERR_PTR(rc);
0656     }
0657 
0658     /* can not grab the rename sem here since it would
0659     deadlock in the cases (beginning of sys_rename itself)
0660     in which we already have the sb rename sem */
0661     page = alloc_dentry_path();
0662     full_path = build_path_from_dentry(direntry, page);
0663     if (IS_ERR(full_path)) {
0664         cifs_put_tlink(tlink);
0665         free_xid(xid);
0666         free_dentry_path(page);
0667         return ERR_CAST(full_path);
0668     }
0669 
0670     if (d_really_is_positive(direntry)) {
0671         cifs_dbg(FYI, "non-NULL inode in lookup\n");
0672     } else {
0673         cifs_dbg(FYI, "NULL inode in lookup\n");
0674     }
0675     cifs_dbg(FYI, "Full path: %s inode = 0x%p\n",
0676          full_path, d_inode(direntry));
0677 
0678 again:
0679     if (pTcon->posix_extensions)
0680         rc = smb311_posix_get_inode_info(&newInode, full_path, parent_dir_inode->i_sb, xid);
0681     else if (pTcon->unix_ext) {
0682         rc = cifs_get_inode_info_unix(&newInode, full_path,
0683                           parent_dir_inode->i_sb, xid);
0684     } else {
0685         rc = cifs_get_inode_info(&newInode, full_path, NULL,
0686                 parent_dir_inode->i_sb, xid, NULL);
0687     }
0688 
0689     if (rc == 0) {
0690         /* since paths are not looked up by component - the parent
0691            directories are presumed to be good here */
0692         renew_parental_timestamps(direntry);
0693     } else if (rc == -EAGAIN && retry_count++ < 10) {
0694         goto again;
0695     } else if (rc == -ENOENT) {
0696         cifs_set_time(direntry, jiffies);
0697         newInode = NULL;
0698     } else {
0699         if (rc != -EACCES) {
0700             cifs_dbg(FYI, "Unexpected lookup error %d\n", rc);
0701             /* We special case check for Access Denied - since that
0702             is a common return code */
0703         }
0704         newInode = ERR_PTR(rc);
0705     }
0706     free_dentry_path(page);
0707     cifs_put_tlink(tlink);
0708     free_xid(xid);
0709     return d_splice_alias(newInode, direntry);
0710 }
0711 
0712 static int
0713 cifs_d_revalidate(struct dentry *direntry, unsigned int flags)
0714 {
0715     struct inode *inode;
0716     int rc;
0717 
0718     if (flags & LOOKUP_RCU)
0719         return -ECHILD;
0720 
0721     if (d_really_is_positive(direntry)) {
0722         inode = d_inode(direntry);
0723         if ((flags & LOOKUP_REVAL) && !CIFS_CACHE_READ(CIFS_I(inode)))
0724             CIFS_I(inode)->time = 0; /* force reval */
0725 
0726         rc = cifs_revalidate_dentry(direntry);
0727         if (rc) {
0728             cifs_dbg(FYI, "cifs_revalidate_dentry failed with rc=%d", rc);
0729             switch (rc) {
0730             case -ENOENT:
0731             case -ESTALE:
0732                 /*
0733                  * Those errors mean the dentry is invalid
0734                  * (file was deleted or recreated)
0735                  */
0736                 return 0;
0737             default:
0738                 /*
0739                  * Otherwise some unexpected error happened
0740                  * report it as-is to VFS layer
0741                  */
0742                 return rc;
0743             }
0744         }
0745         else {
0746             /*
0747              * If the inode wasn't known to be a dfs entry when
0748              * the dentry was instantiated, such as when created
0749              * via ->readdir(), it needs to be set now since the
0750              * attributes will have been updated by
0751              * cifs_revalidate_dentry().
0752              */
0753             if (IS_AUTOMOUNT(inode) &&
0754                !(direntry->d_flags & DCACHE_NEED_AUTOMOUNT)) {
0755                 spin_lock(&direntry->d_lock);
0756                 direntry->d_flags |= DCACHE_NEED_AUTOMOUNT;
0757                 spin_unlock(&direntry->d_lock);
0758             }
0759 
0760             return 1;
0761         }
0762     }
0763 
0764     /*
0765      * This may be nfsd (or something), anyway, we can't see the
0766      * intent of this. So, since this can be for creation, drop it.
0767      */
0768     if (!flags)
0769         return 0;
0770 
0771     /*
0772      * Drop the negative dentry, in order to make sure to use the
0773      * case sensitive name which is specified by user if this is
0774      * for creation.
0775      */
0776     if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
0777         return 0;
0778 
0779     if (time_after(jiffies, cifs_get_time(direntry) + HZ) || !lookupCacheEnabled)
0780         return 0;
0781 
0782     return 1;
0783 }
0784 
0785 /* static int cifs_d_delete(struct dentry *direntry)
0786 {
0787     int rc = 0;
0788 
0789     cifs_dbg(FYI, "In cifs d_delete, name = %pd\n", direntry);
0790 
0791     return rc;
0792 }     */
0793 
0794 const struct dentry_operations cifs_dentry_ops = {
0795     .d_revalidate = cifs_d_revalidate,
0796     .d_automount = cifs_dfs_d_automount,
0797 /* d_delete:       cifs_d_delete,      */ /* not needed except for debugging */
0798 };
0799 
0800 static int cifs_ci_hash(const struct dentry *dentry, struct qstr *q)
0801 {
0802     struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
0803     unsigned long hash;
0804     wchar_t c;
0805     int i, charlen;
0806 
0807     hash = init_name_hash(dentry);
0808     for (i = 0; i < q->len; i += charlen) {
0809         charlen = codepage->char2uni(&q->name[i], q->len - i, &c);
0810         /* error out if we can't convert the character */
0811         if (unlikely(charlen < 0))
0812             return charlen;
0813         hash = partial_name_hash(cifs_toupper(c), hash);
0814     }
0815     q->hash = end_name_hash(hash);
0816 
0817     return 0;
0818 }
0819 
0820 static int cifs_ci_compare(const struct dentry *dentry,
0821         unsigned int len, const char *str, const struct qstr *name)
0822 {
0823     struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
0824     wchar_t c1, c2;
0825     int i, l1, l2;
0826 
0827     /*
0828      * We make the assumption here that uppercase characters in the local
0829      * codepage are always the same length as their lowercase counterparts.
0830      *
0831      * If that's ever not the case, then this will fail to match it.
0832      */
0833     if (name->len != len)
0834         return 1;
0835 
0836     for (i = 0; i < len; i += l1) {
0837         /* Convert characters in both strings to UTF-16. */
0838         l1 = codepage->char2uni(&str[i], len - i, &c1);
0839         l2 = codepage->char2uni(&name->name[i], name->len - i, &c2);
0840 
0841         /*
0842          * If we can't convert either character, just declare it to
0843          * be 1 byte long and compare the original byte.
0844          */
0845         if (unlikely(l1 < 0 && l2 < 0)) {
0846             if (str[i] != name->name[i])
0847                 return 1;
0848             l1 = 1;
0849             continue;
0850         }
0851 
0852         /*
0853          * Here, we again ass|u|me that upper/lowercase versions of
0854          * a character are the same length in the local NLS.
0855          */
0856         if (l1 != l2)
0857             return 1;
0858 
0859         /* Now compare uppercase versions of these characters */
0860         if (cifs_toupper(c1) != cifs_toupper(c2))
0861             return 1;
0862     }
0863 
0864     return 0;
0865 }
0866 
0867 const struct dentry_operations cifs_ci_dentry_ops = {
0868     .d_revalidate = cifs_d_revalidate,
0869     .d_hash = cifs_ci_hash,
0870     .d_compare = cifs_ci_compare,
0871     .d_automount = cifs_dfs_d_automount,
0872 };