Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: LGPL-2.1
0002 /*
0003  *
0004  *   Copyright (C) International Business Machines  Corp., 2002,2010
0005  *   Author(s): Steve French (sfrench@us.ibm.com)
0006  *
0007  */
0008 #include <linux/fs.h>
0009 #include <linux/stat.h>
0010 #include <linux/slab.h>
0011 #include <linux/pagemap.h>
0012 #include <linux/freezer.h>
0013 #include <linux/sched/signal.h>
0014 #include <linux/wait_bit.h>
0015 #include <linux/fiemap.h>
0016 #include <asm/div64.h>
0017 #include "cifsfs.h"
0018 #include "cifspdu.h"
0019 #include "cifsglob.h"
0020 #include "cifsproto.h"
0021 #include "smb2proto.h"
0022 #include "cifs_debug.h"
0023 #include "cifs_fs_sb.h"
0024 #include "cifs_unicode.h"
0025 #include "fscache.h"
0026 #include "fs_context.h"
0027 #include "cifs_ioctl.h"
0028 #include "cached_dir.h"
0029 
0030 static void cifs_set_ops(struct inode *inode)
0031 {
0032     struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
0033 
0034     switch (inode->i_mode & S_IFMT) {
0035     case S_IFREG:
0036         inode->i_op = &cifs_file_inode_ops;
0037         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
0038             if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
0039                 inode->i_fop = &cifs_file_direct_nobrl_ops;
0040             else
0041                 inode->i_fop = &cifs_file_direct_ops;
0042         } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
0043             if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
0044                 inode->i_fop = &cifs_file_strict_nobrl_ops;
0045             else
0046                 inode->i_fop = &cifs_file_strict_ops;
0047         } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
0048             inode->i_fop = &cifs_file_nobrl_ops;
0049         else { /* not direct, send byte range locks */
0050             inode->i_fop = &cifs_file_ops;
0051         }
0052 
0053         /* check if server can support readahead */
0054         if (cifs_sb_master_tcon(cifs_sb)->ses->server->max_read <
0055                 PAGE_SIZE + MAX_CIFS_HDR_SIZE)
0056             inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
0057         else
0058             inode->i_data.a_ops = &cifs_addr_ops;
0059         break;
0060     case S_IFDIR:
0061 #ifdef CONFIG_CIFS_DFS_UPCALL
0062         if (IS_AUTOMOUNT(inode)) {
0063             inode->i_op = &cifs_dfs_referral_inode_operations;
0064         } else {
0065 #else /* NO DFS support, treat as a directory */
0066         {
0067 #endif
0068             inode->i_op = &cifs_dir_inode_ops;
0069             inode->i_fop = &cifs_dir_ops;
0070         }
0071         break;
0072     case S_IFLNK:
0073         inode->i_op = &cifs_symlink_inode_ops;
0074         break;
0075     default:
0076         init_special_inode(inode, inode->i_mode, inode->i_rdev);
0077         break;
0078     }
0079 }
0080 
0081 /* check inode attributes against fattr. If they don't match, tag the
0082  * inode for cache invalidation
0083  */
0084 static void
0085 cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
0086 {
0087     struct cifs_fscache_inode_coherency_data cd;
0088     struct cifsInodeInfo *cifs_i = CIFS_I(inode);
0089 
0090     cifs_dbg(FYI, "%s: revalidating inode %llu\n",
0091          __func__, cifs_i->uniqueid);
0092 
0093     if (inode->i_state & I_NEW) {
0094         cifs_dbg(FYI, "%s: inode %llu is new\n",
0095              __func__, cifs_i->uniqueid);
0096         return;
0097     }
0098 
0099     /* don't bother with revalidation if we have an oplock */
0100     if (CIFS_CACHE_READ(cifs_i)) {
0101         cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
0102              __func__, cifs_i->uniqueid);
0103         return;
0104     }
0105 
0106      /* revalidate if mtime or size have changed */
0107     fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
0108     if (timespec64_equal(&inode->i_mtime, &fattr->cf_mtime) &&
0109         cifs_i->server_eof == fattr->cf_eof) {
0110         cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
0111              __func__, cifs_i->uniqueid);
0112         return;
0113     }
0114 
0115     cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
0116          __func__, cifs_i->uniqueid);
0117     set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
0118     /* Invalidate fscache cookie */
0119     cifs_fscache_fill_coherency(&cifs_i->netfs.inode, &cd);
0120     fscache_invalidate(cifs_inode_cookie(inode), &cd, i_size_read(inode), 0);
0121 }
0122 
0123 /*
0124  * copy nlink to the inode, unless it wasn't provided.  Provide
0125  * sane values if we don't have an existing one and none was provided
0126  */
0127 static void
0128 cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
0129 {
0130     /*
0131      * if we're in a situation where we can't trust what we
0132      * got from the server (readdir, some non-unix cases)
0133      * fake reasonable values
0134      */
0135     if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
0136         /* only provide fake values on a new inode */
0137         if (inode->i_state & I_NEW) {
0138             if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
0139                 set_nlink(inode, 2);
0140             else
0141                 set_nlink(inode, 1);
0142         }
0143         return;
0144     }
0145 
0146     /* we trust the server, so update it */
0147     set_nlink(inode, fattr->cf_nlink);
0148 }
0149 
0150 /* populate an inode with info from a cifs_fattr struct */
0151 int
0152 cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
0153 {
0154     struct cifsInodeInfo *cifs_i = CIFS_I(inode);
0155     struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
0156 
0157     if (!(inode->i_state & I_NEW) &&
0158         unlikely(inode_wrong_type(inode, fattr->cf_mode))) {
0159         CIFS_I(inode)->time = 0; /* force reval */
0160         return -ESTALE;
0161     }
0162 
0163     cifs_revalidate_cache(inode, fattr);
0164 
0165     spin_lock(&inode->i_lock);
0166     fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
0167     fattr->cf_atime = timestamp_truncate(fattr->cf_atime, inode);
0168     fattr->cf_ctime = timestamp_truncate(fattr->cf_ctime, inode);
0169     /* we do not want atime to be less than mtime, it broke some apps */
0170     if (timespec64_compare(&fattr->cf_atime, &fattr->cf_mtime) < 0)
0171         inode->i_atime = fattr->cf_mtime;
0172     else
0173         inode->i_atime = fattr->cf_atime;
0174     inode->i_mtime = fattr->cf_mtime;
0175     inode->i_ctime = fattr->cf_ctime;
0176     inode->i_rdev = fattr->cf_rdev;
0177     cifs_nlink_fattr_to_inode(inode, fattr);
0178     inode->i_uid = fattr->cf_uid;
0179     inode->i_gid = fattr->cf_gid;
0180 
0181     /* if dynperm is set, don't clobber existing mode */
0182     if (inode->i_state & I_NEW ||
0183         !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
0184         inode->i_mode = fattr->cf_mode;
0185 
0186     cifs_i->cifsAttrs = fattr->cf_cifsattrs;
0187 
0188     if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
0189         cifs_i->time = 0;
0190     else
0191         cifs_i->time = jiffies;
0192 
0193     if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
0194         set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
0195     else
0196         clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
0197 
0198     cifs_i->server_eof = fattr->cf_eof;
0199     /*
0200      * Can't safely change the file size here if the client is writing to
0201      * it due to potential races.
0202      */
0203     if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
0204         i_size_write(inode, fattr->cf_eof);
0205 
0206         /*
0207          * i_blocks is not related to (i_size / i_blksize),
0208          * but instead 512 byte (2**9) size is required for
0209          * calculating num blocks.
0210          */
0211         inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
0212     }
0213     spin_unlock(&inode->i_lock);
0214 
0215     if (fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL)
0216         inode->i_flags |= S_AUTOMOUNT;
0217     if (inode->i_state & I_NEW)
0218         cifs_set_ops(inode);
0219     return 0;
0220 }
0221 
0222 void
0223 cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
0224 {
0225     struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
0226 
0227     if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
0228         return;
0229 
0230     fattr->cf_uniqueid = iunique(sb, ROOT_I);
0231 }
0232 
0233 /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
0234 void
0235 cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
0236              struct cifs_sb_info *cifs_sb)
0237 {
0238     memset(fattr, 0, sizeof(*fattr));
0239     fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
0240     fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
0241     fattr->cf_eof = le64_to_cpu(info->EndOfFile);
0242 
0243     fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
0244     fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
0245     fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
0246     /* old POSIX extensions don't get create time */
0247 
0248     fattr->cf_mode = le64_to_cpu(info->Permissions);
0249 
0250     /*
0251      * Since we set the inode type below we need to mask off
0252      * to avoid strange results if bits set above.
0253      */
0254     fattr->cf_mode &= ~S_IFMT;
0255     switch (le32_to_cpu(info->Type)) {
0256     case UNIX_FILE:
0257         fattr->cf_mode |= S_IFREG;
0258         fattr->cf_dtype = DT_REG;
0259         break;
0260     case UNIX_SYMLINK:
0261         fattr->cf_mode |= S_IFLNK;
0262         fattr->cf_dtype = DT_LNK;
0263         break;
0264     case UNIX_DIR:
0265         fattr->cf_mode |= S_IFDIR;
0266         fattr->cf_dtype = DT_DIR;
0267         break;
0268     case UNIX_CHARDEV:
0269         fattr->cf_mode |= S_IFCHR;
0270         fattr->cf_dtype = DT_CHR;
0271         fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
0272                        le64_to_cpu(info->DevMinor) & MINORMASK);
0273         break;
0274     case UNIX_BLOCKDEV:
0275         fattr->cf_mode |= S_IFBLK;
0276         fattr->cf_dtype = DT_BLK;
0277         fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
0278                        le64_to_cpu(info->DevMinor) & MINORMASK);
0279         break;
0280     case UNIX_FIFO:
0281         fattr->cf_mode |= S_IFIFO;
0282         fattr->cf_dtype = DT_FIFO;
0283         break;
0284     case UNIX_SOCKET:
0285         fattr->cf_mode |= S_IFSOCK;
0286         fattr->cf_dtype = DT_SOCK;
0287         break;
0288     default:
0289         /* safest to call it a file if we do not know */
0290         fattr->cf_mode |= S_IFREG;
0291         fattr->cf_dtype = DT_REG;
0292         cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
0293         break;
0294     }
0295 
0296     fattr->cf_uid = cifs_sb->ctx->linux_uid;
0297     if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
0298         u64 id = le64_to_cpu(info->Uid);
0299         if (id < ((uid_t)-1)) {
0300             kuid_t uid = make_kuid(&init_user_ns, id);
0301             if (uid_valid(uid))
0302                 fattr->cf_uid = uid;
0303         }
0304     }
0305     
0306     fattr->cf_gid = cifs_sb->ctx->linux_gid;
0307     if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
0308         u64 id = le64_to_cpu(info->Gid);
0309         if (id < ((gid_t)-1)) {
0310             kgid_t gid = make_kgid(&init_user_ns, id);
0311             if (gid_valid(gid))
0312                 fattr->cf_gid = gid;
0313         }
0314     }
0315 
0316     fattr->cf_nlink = le64_to_cpu(info->Nlinks);
0317 }
0318 
0319 /*
0320  * Fill a cifs_fattr struct with fake inode info.
0321  *
0322  * Needed to setup cifs_fattr data for the directory which is the
0323  * junction to the new submount (ie to setup the fake directory
0324  * which represents a DFS referral).
0325  */
0326 static void
0327 cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
0328 {
0329     struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
0330 
0331     cifs_dbg(FYI, "creating fake fattr for DFS referral\n");
0332 
0333     memset(fattr, 0, sizeof(*fattr));
0334     fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
0335     fattr->cf_uid = cifs_sb->ctx->linux_uid;
0336     fattr->cf_gid = cifs_sb->ctx->linux_gid;
0337     ktime_get_coarse_real_ts64(&fattr->cf_mtime);
0338     fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime;
0339     fattr->cf_nlink = 2;
0340     fattr->cf_flags = CIFS_FATTR_DFS_REFERRAL;
0341 }
0342 
0343 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
0344 static int
0345 cifs_get_file_info_unix(struct file *filp)
0346 {
0347     int rc;
0348     unsigned int xid;
0349     FILE_UNIX_BASIC_INFO find_data;
0350     struct cifs_fattr fattr;
0351     struct inode *inode = file_inode(filp);
0352     struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
0353     struct cifsFileInfo *cfile = filp->private_data;
0354     struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
0355 
0356     xid = get_xid();
0357     rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
0358     if (!rc) {
0359         cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
0360     } else if (rc == -EREMOTE) {
0361         cifs_create_dfs_fattr(&fattr, inode->i_sb);
0362         rc = 0;
0363     } else
0364         goto cifs_gfiunix_out;
0365 
0366     rc = cifs_fattr_to_inode(inode, &fattr);
0367 
0368 cifs_gfiunix_out:
0369     free_xid(xid);
0370     return rc;
0371 }
0372 
0373 int cifs_get_inode_info_unix(struct inode **pinode,
0374                  const unsigned char *full_path,
0375                  struct super_block *sb, unsigned int xid)
0376 {
0377     int rc;
0378     FILE_UNIX_BASIC_INFO find_data;
0379     struct cifs_fattr fattr;
0380     struct cifs_tcon *tcon;
0381     struct tcon_link *tlink;
0382     struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
0383 
0384     cifs_dbg(FYI, "Getting info on %s\n", full_path);
0385 
0386     tlink = cifs_sb_tlink(cifs_sb);
0387     if (IS_ERR(tlink))
0388         return PTR_ERR(tlink);
0389     tcon = tlink_tcon(tlink);
0390 
0391     /* could have done a find first instead but this returns more info */
0392     rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
0393                   cifs_sb->local_nls, cifs_remap(cifs_sb));
0394     cifs_put_tlink(tlink);
0395 
0396     if (!rc) {
0397         cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
0398     } else if (rc == -EREMOTE) {
0399         cifs_create_dfs_fattr(&fattr, sb);
0400         rc = 0;
0401     } else {
0402         return rc;
0403     }
0404 
0405     /* check for Minshall+French symlinks */
0406     if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
0407         int tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
0408                          full_path);
0409         if (tmprc)
0410             cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
0411     }
0412 
0413     if (*pinode == NULL) {
0414         /* get new inode */
0415         cifs_fill_uniqueid(sb, &fattr);
0416         *pinode = cifs_iget(sb, &fattr);
0417         if (!*pinode)
0418             rc = -ENOMEM;
0419     } else {
0420         /* we already have inode, update it */
0421 
0422         /* if uniqueid is different, return error */
0423         if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
0424             CIFS_I(*pinode)->uniqueid != fattr.cf_uniqueid)) {
0425             CIFS_I(*pinode)->time = 0; /* force reval */
0426             rc = -ESTALE;
0427             goto cgiiu_exit;
0428         }
0429 
0430         /* if filetype is different, return error */
0431         rc = cifs_fattr_to_inode(*pinode, &fattr);
0432     }
0433 
0434 cgiiu_exit:
0435     return rc;
0436 }
0437 #else
0438 int cifs_get_inode_info_unix(struct inode **pinode,
0439                  const unsigned char *full_path,
0440                  struct super_block *sb, unsigned int xid)
0441 {
0442     return -EOPNOTSUPP;
0443 }
0444 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
0445 
0446 static int
0447 cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
0448           struct cifs_sb_info *cifs_sb, unsigned int xid)
0449 {
0450     int rc;
0451     __u32 oplock;
0452     struct tcon_link *tlink;
0453     struct cifs_tcon *tcon;
0454     struct cifs_fid fid;
0455     struct cifs_open_parms oparms;
0456     struct cifs_io_parms io_parms = {0};
0457     char buf[24];
0458     unsigned int bytes_read;
0459     char *pbuf;
0460     int buf_type = CIFS_NO_BUFFER;
0461 
0462     pbuf = buf;
0463 
0464     fattr->cf_mode &= ~S_IFMT;
0465 
0466     if (fattr->cf_eof == 0) {
0467         fattr->cf_mode |= S_IFIFO;
0468         fattr->cf_dtype = DT_FIFO;
0469         return 0;
0470     } else if (fattr->cf_eof < 8) {
0471         fattr->cf_mode |= S_IFREG;
0472         fattr->cf_dtype = DT_REG;
0473         return -EINVAL;  /* EOPNOTSUPP? */
0474     }
0475 
0476     tlink = cifs_sb_tlink(cifs_sb);
0477     if (IS_ERR(tlink))
0478         return PTR_ERR(tlink);
0479     tcon = tlink_tcon(tlink);
0480 
0481     oparms.tcon = tcon;
0482     oparms.cifs_sb = cifs_sb;
0483     oparms.desired_access = GENERIC_READ;
0484     oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
0485     oparms.disposition = FILE_OPEN;
0486     oparms.path = path;
0487     oparms.fid = &fid;
0488     oparms.reconnect = false;
0489 
0490     if (tcon->ses->server->oplocks)
0491         oplock = REQ_OPLOCK;
0492     else
0493         oplock = 0;
0494     rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
0495     if (rc) {
0496         cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
0497         cifs_put_tlink(tlink);
0498         return rc;
0499     }
0500 
0501     /* Read header */
0502     io_parms.netfid = fid.netfid;
0503     io_parms.pid = current->tgid;
0504     io_parms.tcon = tcon;
0505     io_parms.offset = 0;
0506     io_parms.length = 24;
0507 
0508     rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
0509                     &bytes_read, &pbuf, &buf_type);
0510     if ((rc == 0) && (bytes_read >= 8)) {
0511         if (memcmp("IntxBLK", pbuf, 8) == 0) {
0512             cifs_dbg(FYI, "Block device\n");
0513             fattr->cf_mode |= S_IFBLK;
0514             fattr->cf_dtype = DT_BLK;
0515             if (bytes_read == 24) {
0516                 /* we have enough to decode dev num */
0517                 __u64 mjr; /* major */
0518                 __u64 mnr; /* minor */
0519                 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
0520                 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
0521                 fattr->cf_rdev = MKDEV(mjr, mnr);
0522             }
0523         } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
0524             cifs_dbg(FYI, "Char device\n");
0525             fattr->cf_mode |= S_IFCHR;
0526             fattr->cf_dtype = DT_CHR;
0527             if (bytes_read == 24) {
0528                 /* we have enough to decode dev num */
0529                 __u64 mjr; /* major */
0530                 __u64 mnr; /* minor */
0531                 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
0532                 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
0533                 fattr->cf_rdev = MKDEV(mjr, mnr);
0534             }
0535         } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
0536             cifs_dbg(FYI, "Symlink\n");
0537             fattr->cf_mode |= S_IFLNK;
0538             fattr->cf_dtype = DT_LNK;
0539         } else {
0540             fattr->cf_mode |= S_IFREG; /* file? */
0541             fattr->cf_dtype = DT_REG;
0542             rc = -EOPNOTSUPP;
0543         }
0544     } else {
0545         fattr->cf_mode |= S_IFREG; /* then it is a file */
0546         fattr->cf_dtype = DT_REG;
0547         rc = -EOPNOTSUPP; /* or some unknown SFU type */
0548     }
0549 
0550     tcon->ses->server->ops->close(xid, tcon, &fid);
0551     cifs_put_tlink(tlink);
0552     return rc;
0553 }
0554 
0555 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID)  /* SETFILEBITS valid bits */
0556 
0557 /*
0558  * Fetch mode bits as provided by SFU.
0559  *
0560  * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
0561  */
0562 static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
0563              struct cifs_sb_info *cifs_sb, unsigned int xid)
0564 {
0565 #ifdef CONFIG_CIFS_XATTR
0566     ssize_t rc;
0567     char ea_value[4];
0568     __u32 mode;
0569     struct tcon_link *tlink;
0570     struct cifs_tcon *tcon;
0571 
0572     tlink = cifs_sb_tlink(cifs_sb);
0573     if (IS_ERR(tlink))
0574         return PTR_ERR(tlink);
0575     tcon = tlink_tcon(tlink);
0576 
0577     if (tcon->ses->server->ops->query_all_EAs == NULL) {
0578         cifs_put_tlink(tlink);
0579         return -EOPNOTSUPP;
0580     }
0581 
0582     rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
0583             "SETFILEBITS", ea_value, 4 /* size of buf */,
0584             cifs_sb);
0585     cifs_put_tlink(tlink);
0586     if (rc < 0)
0587         return (int)rc;
0588     else if (rc > 3) {
0589         mode = le32_to_cpu(*((__le32 *)ea_value));
0590         fattr->cf_mode &= ~SFBITS_MASK;
0591         cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
0592              mode, fattr->cf_mode);
0593         fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
0594         cifs_dbg(FYI, "special mode bits 0%o\n", mode);
0595     }
0596 
0597     return 0;
0598 #else
0599     return -EOPNOTSUPP;
0600 #endif
0601 }
0602 
0603 /* Fill a cifs_fattr struct with info from POSIX info struct */
0604 static void
0605 smb311_posix_info_to_fattr(struct cifs_fattr *fattr, struct smb311_posix_qinfo *info,
0606                struct super_block *sb, bool adjust_tz, bool symlink)
0607 {
0608     struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
0609     struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
0610 
0611     memset(fattr, 0, sizeof(*fattr));
0612 
0613     /* no fattr->flags to set */
0614     fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
0615     fattr->cf_uniqueid = le64_to_cpu(info->Inode);
0616 
0617     if (info->LastAccessTime)
0618         fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
0619     else
0620         ktime_get_coarse_real_ts64(&fattr->cf_atime);
0621 
0622     fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
0623     fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
0624 
0625     if (adjust_tz) {
0626         fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
0627         fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
0628     }
0629 
0630     fattr->cf_eof = le64_to_cpu(info->EndOfFile);
0631     fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
0632     fattr->cf_createtime = le64_to_cpu(info->CreationTime);
0633 
0634     fattr->cf_nlink = le32_to_cpu(info->HardLinks);
0635     fattr->cf_mode = (umode_t) le32_to_cpu(info->Mode);
0636     /* The srv fs device id is overridden on network mount so setting rdev isn't needed here */
0637     /* fattr->cf_rdev = le32_to_cpu(info->DeviceId); */
0638 
0639     if (symlink) {
0640         fattr->cf_mode |= S_IFLNK;
0641         fattr->cf_dtype = DT_LNK;
0642     } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
0643         fattr->cf_mode |= S_IFDIR;
0644         fattr->cf_dtype = DT_DIR;
0645     } else { /* file */
0646         fattr->cf_mode |= S_IFREG;
0647         fattr->cf_dtype = DT_REG;
0648     }
0649     /* else if reparse point ... TODO: add support for FIFO and blk dev; special file types */
0650 
0651     fattr->cf_uid = cifs_sb->ctx->linux_uid; /* TODO: map uid and gid from SID */
0652     fattr->cf_gid = cifs_sb->ctx->linux_gid;
0653 
0654     cifs_dbg(FYI, "POSIX query info: mode 0x%x uniqueid 0x%llx nlink %d\n",
0655         fattr->cf_mode, fattr->cf_uniqueid, fattr->cf_nlink);
0656 }
0657 
0658 
0659 /* Fill a cifs_fattr struct with info from FILE_ALL_INFO */
0660 static void
0661 cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info,
0662                struct super_block *sb, bool adjust_tz,
0663                bool symlink, u32 reparse_tag)
0664 {
0665     struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
0666     struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
0667 
0668     memset(fattr, 0, sizeof(*fattr));
0669     fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
0670     if (info->DeletePending)
0671         fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
0672 
0673     if (info->LastAccessTime)
0674         fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
0675     else
0676         ktime_get_coarse_real_ts64(&fattr->cf_atime);
0677 
0678     fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
0679     fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
0680 
0681     if (adjust_tz) {
0682         fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
0683         fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
0684     }
0685 
0686     fattr->cf_eof = le64_to_cpu(info->EndOfFile);
0687     fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
0688     fattr->cf_createtime = le64_to_cpu(info->CreationTime);
0689 
0690     fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
0691     if (reparse_tag == IO_REPARSE_TAG_LX_SYMLINK) {
0692         fattr->cf_mode |= S_IFLNK | cifs_sb->ctx->file_mode;
0693         fattr->cf_dtype = DT_LNK;
0694     } else if (reparse_tag == IO_REPARSE_TAG_LX_FIFO) {
0695         fattr->cf_mode |= S_IFIFO | cifs_sb->ctx->file_mode;
0696         fattr->cf_dtype = DT_FIFO;
0697     } else if (reparse_tag == IO_REPARSE_TAG_AF_UNIX) {
0698         fattr->cf_mode |= S_IFSOCK | cifs_sb->ctx->file_mode;
0699         fattr->cf_dtype = DT_SOCK;
0700     } else if (reparse_tag == IO_REPARSE_TAG_LX_CHR) {
0701         fattr->cf_mode |= S_IFCHR | cifs_sb->ctx->file_mode;
0702         fattr->cf_dtype = DT_CHR;
0703     } else if (reparse_tag == IO_REPARSE_TAG_LX_BLK) {
0704         fattr->cf_mode |= S_IFBLK | cifs_sb->ctx->file_mode;
0705         fattr->cf_dtype = DT_BLK;
0706     } else if (symlink) { /* TODO add more reparse tag checks */
0707         fattr->cf_mode = S_IFLNK;
0708         fattr->cf_dtype = DT_LNK;
0709     } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
0710         fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode;
0711         fattr->cf_dtype = DT_DIR;
0712         /*
0713          * Server can return wrong NumberOfLinks value for directories
0714          * when Unix extensions are disabled - fake it.
0715          */
0716         if (!tcon->unix_ext)
0717             fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
0718     } else {
0719         fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode;
0720         fattr->cf_dtype = DT_REG;
0721 
0722         /* clear write bits if ATTR_READONLY is set */
0723         if (fattr->cf_cifsattrs & ATTR_READONLY)
0724             fattr->cf_mode &= ~(S_IWUGO);
0725 
0726         /*
0727          * Don't accept zero nlink from non-unix servers unless
0728          * delete is pending.  Instead mark it as unknown.
0729          */
0730         if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
0731             !info->DeletePending) {
0732             cifs_dbg(VFS, "bogus file nlink value %u\n",
0733                  fattr->cf_nlink);
0734             fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
0735         }
0736     }
0737 
0738     fattr->cf_uid = cifs_sb->ctx->linux_uid;
0739     fattr->cf_gid = cifs_sb->ctx->linux_gid;
0740 }
0741 
0742 static int
0743 cifs_get_file_info(struct file *filp)
0744 {
0745     int rc;
0746     unsigned int xid;
0747     FILE_ALL_INFO find_data;
0748     struct cifs_fattr fattr;
0749     struct inode *inode = file_inode(filp);
0750     struct cifsFileInfo *cfile = filp->private_data;
0751     struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
0752     struct TCP_Server_Info *server = tcon->ses->server;
0753 
0754     if (!server->ops->query_file_info)
0755         return -ENOSYS;
0756 
0757     xid = get_xid();
0758     rc = server->ops->query_file_info(xid, tcon, &cfile->fid, &find_data);
0759     switch (rc) {
0760     case 0:
0761         /* TODO: add support to query reparse tag */
0762         cifs_all_info_to_fattr(&fattr, &find_data, inode->i_sb, false,
0763                        false, 0 /* no reparse tag */);
0764         break;
0765     case -EREMOTE:
0766         cifs_create_dfs_fattr(&fattr, inode->i_sb);
0767         rc = 0;
0768         break;
0769     case -EOPNOTSUPP:
0770     case -EINVAL:
0771         /*
0772          * FIXME: legacy server -- fall back to path-based call?
0773          * for now, just skip revalidating and mark inode for
0774          * immediate reval.
0775          */
0776         rc = 0;
0777         CIFS_I(inode)->time = 0;
0778         goto cgfi_exit;
0779     default:
0780         goto cgfi_exit;
0781     }
0782 
0783     /*
0784      * don't bother with SFU junk here -- just mark inode as needing
0785      * revalidation.
0786      */
0787     fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
0788     fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
0789     /* if filetype is different, return error */
0790     rc = cifs_fattr_to_inode(inode, &fattr);
0791 cgfi_exit:
0792     free_xid(xid);
0793     return rc;
0794 }
0795 
0796 /* Simple function to return a 64 bit hash of string.  Rarely called */
0797 static __u64 simple_hashstr(const char *str)
0798 {
0799     const __u64 hash_mult =  1125899906842597ULL; /* a big enough prime */
0800     __u64 hash = 0;
0801 
0802     while (*str)
0803         hash = (hash + (__u64) *str++) * hash_mult;
0804 
0805     return hash;
0806 }
0807 
0808 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
0809 /**
0810  * cifs_backup_query_path_info - SMB1 fallback code to get ino
0811  *
0812  * Fallback code to get file metadata when we don't have access to
0813  * full_path (EACCES) and have backup creds.
0814  *
0815  * @xid:    transaction id used to identify original request in logs
0816  * @tcon:   information about the server share we have mounted
0817  * @sb: the superblock stores info such as disk space available
0818  * @full_path:  name of the file we are getting the metadata for
0819  * @resp_buf:   will be set to cifs resp buf and needs to be freed with
0820  *      cifs_buf_release() when done with @data
0821  * @data:   will be set to search info result buffer
0822  */
0823 static int
0824 cifs_backup_query_path_info(int xid,
0825                 struct cifs_tcon *tcon,
0826                 struct super_block *sb,
0827                 const char *full_path,
0828                 void **resp_buf,
0829                 FILE_ALL_INFO **data)
0830 {
0831     struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
0832     struct cifs_search_info info = {0};
0833     u16 flags;
0834     int rc;
0835 
0836     *resp_buf = NULL;
0837     info.endOfSearch = false;
0838     if (tcon->unix_ext)
0839         info.info_level = SMB_FIND_FILE_UNIX;
0840     else if ((tcon->ses->capabilities &
0841           tcon->ses->server->vals->cap_nt_find) == 0)
0842         info.info_level = SMB_FIND_FILE_INFO_STANDARD;
0843     else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
0844         info.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
0845     else /* no srvino useful for fallback to some netapp */
0846         info.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
0847 
0848     flags = CIFS_SEARCH_CLOSE_ALWAYS |
0849         CIFS_SEARCH_CLOSE_AT_END |
0850         CIFS_SEARCH_BACKUP_SEARCH;
0851 
0852     rc = CIFSFindFirst(xid, tcon, full_path,
0853                cifs_sb, NULL, flags, &info, false);
0854     if (rc)
0855         return rc;
0856 
0857     *resp_buf = (void *)info.ntwrk_buf_start;
0858     *data = (FILE_ALL_INFO *)info.srch_entries_start;
0859     return 0;
0860 }
0861 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
0862 
0863 static void
0864 cifs_set_fattr_ino(int xid,
0865            struct cifs_tcon *tcon,
0866            struct super_block *sb,
0867            struct inode **inode,
0868            const char *full_path,
0869            FILE_ALL_INFO *data,
0870            struct cifs_fattr *fattr)
0871 {
0872     struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
0873     struct TCP_Server_Info *server = tcon->ses->server;
0874     int rc;
0875 
0876     if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
0877         if (*inode)
0878             fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
0879         else
0880             fattr->cf_uniqueid = iunique(sb, ROOT_I);
0881         return;
0882     }
0883 
0884     /*
0885      * If we have an inode pass a NULL tcon to ensure we don't
0886      * make a round trip to the server. This only works for SMB2+.
0887      */
0888     rc = server->ops->get_srv_inum(xid,
0889                        *inode ? NULL : tcon,
0890                        cifs_sb, full_path,
0891                        &fattr->cf_uniqueid,
0892                        data);
0893     if (rc) {
0894         /*
0895          * If that fails reuse existing ino or generate one
0896          * and disable server ones
0897          */
0898         if (*inode)
0899             fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
0900         else {
0901             fattr->cf_uniqueid = iunique(sb, ROOT_I);
0902             cifs_autodisable_serverino(cifs_sb);
0903         }
0904         return;
0905     }
0906 
0907     /* If no errors, check for zero root inode (invalid) */
0908     if (fattr->cf_uniqueid == 0 && strlen(full_path) == 0) {
0909         cifs_dbg(FYI, "Invalid (0) inodenum\n");
0910         if (*inode) {
0911             /* reuse */
0912             fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
0913         } else {
0914             /* make an ino by hashing the UNC */
0915             fattr->cf_flags |= CIFS_FATTR_FAKE_ROOT_INO;
0916             fattr->cf_uniqueid = simple_hashstr(tcon->treeName);
0917         }
0918     }
0919 }
0920 
0921 static inline bool is_inode_cache_good(struct inode *ino)
0922 {
0923     return ino && CIFS_CACHE_READ(CIFS_I(ino)) && CIFS_I(ino)->time != 0;
0924 }
0925 
0926 int
0927 cifs_get_inode_info(struct inode **inode,
0928             const char *full_path,
0929             FILE_ALL_INFO *in_data,
0930             struct super_block *sb, int xid,
0931             const struct cifs_fid *fid)
0932 {
0933 
0934     struct cifs_tcon *tcon;
0935     struct TCP_Server_Info *server;
0936     struct tcon_link *tlink;
0937     struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
0938     bool adjust_tz = false;
0939     struct cifs_fattr fattr = {0};
0940     bool is_reparse_point = false;
0941     FILE_ALL_INFO *data = in_data;
0942     FILE_ALL_INFO *tmp_data = NULL;
0943     void *smb1_backup_rsp_buf = NULL;
0944     int rc = 0;
0945     int tmprc = 0;
0946     __u32 reparse_tag = 0;
0947 
0948     tlink = cifs_sb_tlink(cifs_sb);
0949     if (IS_ERR(tlink))
0950         return PTR_ERR(tlink);
0951     tcon = tlink_tcon(tlink);
0952     server = tcon->ses->server;
0953 
0954     /*
0955      * 1. Fetch file metadata if not provided (data)
0956      */
0957 
0958     if (!data) {
0959         if (is_inode_cache_good(*inode)) {
0960             cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
0961             goto out;
0962         }
0963         tmp_data = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
0964         if (!tmp_data) {
0965             rc = -ENOMEM;
0966             goto out;
0967         }
0968         rc = server->ops->query_path_info(xid, tcon, cifs_sb,
0969                          full_path, tmp_data,
0970                          &adjust_tz, &is_reparse_point);
0971 #ifdef CONFIG_CIFS_DFS_UPCALL
0972         if (rc == -ENOENT && is_tcon_dfs(tcon))
0973             rc = cifs_dfs_query_info_nonascii_quirk(xid, tcon,
0974                                 cifs_sb,
0975                                 full_path);
0976 #endif
0977         data = tmp_data;
0978     }
0979 
0980     /*
0981      * 2. Convert it to internal cifs metadata (fattr)
0982      */
0983 
0984     switch (rc) {
0985     case 0:
0986         /*
0987          * If the file is a reparse point, it is more complicated
0988          * since we have to check if its reparse tag matches a known
0989          * special file type e.g. symlink or fifo or char etc.
0990          */
0991         if ((le32_to_cpu(data->Attributes) & ATTR_REPARSE) &&
0992             server->ops->query_reparse_tag) {
0993             rc = server->ops->query_reparse_tag(xid, tcon, cifs_sb,
0994                         full_path, &reparse_tag);
0995             cifs_dbg(FYI, "reparse tag 0x%x\n", reparse_tag);
0996         }
0997         cifs_all_info_to_fattr(&fattr, data, sb, adjust_tz,
0998                        is_reparse_point, reparse_tag);
0999         break;
1000     case -EREMOTE:
1001         /* DFS link, no metadata available on this server */
1002         cifs_create_dfs_fattr(&fattr, sb);
1003         rc = 0;
1004         break;
1005     case -EACCES:
1006 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1007         /*
1008          * perm errors, try again with backup flags if possible
1009          *
1010          * For SMB2 and later the backup intent flag
1011          * is already sent if needed on open and there
1012          * is no path based FindFirst operation to use
1013          * to retry with
1014          */
1015         if (backup_cred(cifs_sb) && is_smb1_server(server)) {
1016             /* for easier reading */
1017             FILE_DIRECTORY_INFO *fdi;
1018             SEARCH_ID_FULL_DIR_INFO *si;
1019 
1020             rc = cifs_backup_query_path_info(xid, tcon, sb,
1021                              full_path,
1022                              &smb1_backup_rsp_buf,
1023                              &data);
1024             if (rc)
1025                 goto out;
1026 
1027             fdi = (FILE_DIRECTORY_INFO *)data;
1028             si = (SEARCH_ID_FULL_DIR_INFO *)data;
1029 
1030             cifs_dir_info_to_fattr(&fattr, fdi, cifs_sb);
1031             fattr.cf_uniqueid = le64_to_cpu(si->UniqueId);
1032             /* uniqueid set, skip get inum step */
1033             goto handle_mnt_opt;
1034         } else {
1035             /* nothing we can do, bail out */
1036             goto out;
1037         }
1038 #else
1039         goto out;
1040 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1041         break;
1042     default:
1043         cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1044         goto out;
1045     }
1046 
1047     /*
1048      * 3. Get or update inode number (fattr.cf_uniqueid)
1049      */
1050 
1051     cifs_set_fattr_ino(xid, tcon, sb, inode, full_path, data, &fattr);
1052 
1053     /*
1054      * 4. Tweak fattr based on mount options
1055      */
1056 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1057 handle_mnt_opt:
1058 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1059     /* query for SFU type info if supported and needed */
1060     if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
1061         cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
1062         tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
1063         if (tmprc)
1064             cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
1065     }
1066 
1067     /* fill in 0777 bits from ACL */
1068     if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) {
1069         rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, true,
1070                        full_path, fid);
1071         if (rc == -EREMOTE)
1072             rc = 0;
1073         if (rc) {
1074             cifs_dbg(FYI, "%s: Get mode from SID failed. rc=%d\n",
1075                  __func__, rc);
1076             goto out;
1077         }
1078     } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
1079         rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, false,
1080                        full_path, fid);
1081         if (rc == -EREMOTE)
1082             rc = 0;
1083         if (rc) {
1084             cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
1085                  __func__, rc);
1086             goto out;
1087         }
1088     }
1089 
1090     /* fill in remaining high mode bits e.g. SUID, VTX */
1091     if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
1092         cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
1093 
1094     /* check for Minshall+French symlinks */
1095     if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1096         tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
1097                      full_path);
1098         if (tmprc)
1099             cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1100     }
1101 
1102     /*
1103      * 5. Update inode with final fattr data
1104      */
1105 
1106     if (!*inode) {
1107         *inode = cifs_iget(sb, &fattr);
1108         if (!*inode)
1109             rc = -ENOMEM;
1110     } else {
1111         /* we already have inode, update it */
1112 
1113         /* if uniqueid is different, return error */
1114         if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
1115             CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
1116             CIFS_I(*inode)->time = 0; /* force reval */
1117             rc = -ESTALE;
1118             goto out;
1119         }
1120         /* if filetype is different, return error */
1121         rc = cifs_fattr_to_inode(*inode, &fattr);
1122     }
1123 out:
1124     cifs_buf_release(smb1_backup_rsp_buf);
1125     cifs_put_tlink(tlink);
1126     kfree(tmp_data);
1127     return rc;
1128 }
1129 
1130 int
1131 smb311_posix_get_inode_info(struct inode **inode,
1132             const char *full_path,
1133             struct super_block *sb, unsigned int xid)
1134 {
1135     struct cifs_tcon *tcon;
1136     struct tcon_link *tlink;
1137     struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1138     bool adjust_tz = false;
1139     struct cifs_fattr fattr = {0};
1140     bool symlink = false;
1141     struct smb311_posix_qinfo *data = NULL;
1142     int rc = 0;
1143     int tmprc = 0;
1144 
1145     tlink = cifs_sb_tlink(cifs_sb);
1146     if (IS_ERR(tlink))
1147         return PTR_ERR(tlink);
1148     tcon = tlink_tcon(tlink);
1149 
1150     /*
1151      * 1. Fetch file metadata
1152      */
1153 
1154     if (is_inode_cache_good(*inode)) {
1155         cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1156         goto out;
1157     }
1158     data = kmalloc(sizeof(struct smb311_posix_qinfo), GFP_KERNEL);
1159     if (!data) {
1160         rc = -ENOMEM;
1161         goto out;
1162     }
1163 
1164     rc = smb311_posix_query_path_info(xid, tcon, cifs_sb,
1165                           full_path, data,
1166                           &adjust_tz, &symlink);
1167 
1168     /*
1169      * 2. Convert it to internal cifs metadata (fattr)
1170      */
1171 
1172     switch (rc) {
1173     case 0:
1174         smb311_posix_info_to_fattr(&fattr, data, sb, adjust_tz, symlink);
1175         break;
1176     case -EREMOTE:
1177         /* DFS link, no metadata available on this server */
1178         cifs_create_dfs_fattr(&fattr, sb);
1179         rc = 0;
1180         break;
1181     case -EACCES:
1182         /*
1183          * For SMB2 and later the backup intent flag
1184          * is already sent if needed on open and there
1185          * is no path based FindFirst operation to use
1186          * to retry with so nothing we can do, bail out
1187          */
1188         goto out;
1189     default:
1190         cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1191         goto out;
1192     }
1193 
1194 
1195     /*
1196      * 3. Tweak fattr based on mount options
1197      */
1198 
1199     /* check for Minshall+French symlinks */
1200     if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1201         tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
1202                      full_path);
1203         if (tmprc)
1204             cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1205     }
1206 
1207     /*
1208      * 4. Update inode with final fattr data
1209      */
1210 
1211     if (!*inode) {
1212         *inode = cifs_iget(sb, &fattr);
1213         if (!*inode)
1214             rc = -ENOMEM;
1215     } else {
1216         /* we already have inode, update it */
1217 
1218         /* if uniqueid is different, return error */
1219         if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
1220             CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
1221             CIFS_I(*inode)->time = 0; /* force reval */
1222             rc = -ESTALE;
1223             goto out;
1224         }
1225 
1226         /* if filetype is different, return error */
1227         rc = cifs_fattr_to_inode(*inode, &fattr);
1228     }
1229 out:
1230     cifs_put_tlink(tlink);
1231     kfree(data);
1232     return rc;
1233 }
1234 
1235 
1236 static const struct inode_operations cifs_ipc_inode_ops = {
1237     .lookup = cifs_lookup,
1238 };
1239 
1240 static int
1241 cifs_find_inode(struct inode *inode, void *opaque)
1242 {
1243     struct cifs_fattr *fattr = opaque;
1244 
1245     /* don't match inode with different uniqueid */
1246     if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
1247         return 0;
1248 
1249     /* use createtime like an i_generation field */
1250     if (CIFS_I(inode)->createtime != fattr->cf_createtime)
1251         return 0;
1252 
1253     /* don't match inode of different type */
1254     if (inode_wrong_type(inode, fattr->cf_mode))
1255         return 0;
1256 
1257     /* if it's not a directory or has no dentries, then flag it */
1258     if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
1259         fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
1260 
1261     return 1;
1262 }
1263 
1264 static int
1265 cifs_init_inode(struct inode *inode, void *opaque)
1266 {
1267     struct cifs_fattr *fattr = opaque;
1268 
1269     CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
1270     CIFS_I(inode)->createtime = fattr->cf_createtime;
1271     return 0;
1272 }
1273 
1274 /*
1275  * walk dentry list for an inode and report whether it has aliases that
1276  * are hashed. We use this to determine if a directory inode can actually
1277  * be used.
1278  */
1279 static bool
1280 inode_has_hashed_dentries(struct inode *inode)
1281 {
1282     struct dentry *dentry;
1283 
1284     spin_lock(&inode->i_lock);
1285     hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1286         if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
1287             spin_unlock(&inode->i_lock);
1288             return true;
1289         }
1290     }
1291     spin_unlock(&inode->i_lock);
1292     return false;
1293 }
1294 
1295 /* Given fattrs, get a corresponding inode */
1296 struct inode *
1297 cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
1298 {
1299     unsigned long hash;
1300     struct inode *inode;
1301 
1302 retry_iget5_locked:
1303     cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
1304 
1305     /* hash down to 32-bits on 32-bit arch */
1306     hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
1307 
1308     inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
1309     if (inode) {
1310         /* was there a potentially problematic inode collision? */
1311         if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
1312             fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
1313 
1314             if (inode_has_hashed_dentries(inode)) {
1315                 cifs_autodisable_serverino(CIFS_SB(sb));
1316                 iput(inode);
1317                 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1318                 goto retry_iget5_locked;
1319             }
1320         }
1321 
1322         /* can't fail - see cifs_find_inode() */
1323         cifs_fattr_to_inode(inode, fattr);
1324         if (sb->s_flags & SB_NOATIME)
1325             inode->i_flags |= S_NOATIME | S_NOCMTIME;
1326         if (inode->i_state & I_NEW) {
1327             inode->i_ino = hash;
1328             cifs_fscache_get_inode_cookie(inode);
1329             unlock_new_inode(inode);
1330         }
1331     }
1332 
1333     return inode;
1334 }
1335 
1336 /* gets root inode */
1337 struct inode *cifs_root_iget(struct super_block *sb)
1338 {
1339     unsigned int xid;
1340     struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1341     struct inode *inode = NULL;
1342     long rc;
1343     struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1344     char *path = NULL;
1345     int len;
1346 
1347     if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1348         && cifs_sb->prepath) {
1349         len = strlen(cifs_sb->prepath);
1350         path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1351         if (path == NULL)
1352             return ERR_PTR(-ENOMEM);
1353         path[0] = '/';
1354         memcpy(path+1, cifs_sb->prepath, len);
1355     } else {
1356         path = kstrdup("", GFP_KERNEL);
1357         if (path == NULL)
1358             return ERR_PTR(-ENOMEM);
1359     }
1360 
1361     xid = get_xid();
1362     if (tcon->unix_ext) {
1363         rc = cifs_get_inode_info_unix(&inode, path, sb, xid);
1364         /* some servers mistakenly claim POSIX support */
1365         if (rc != -EOPNOTSUPP)
1366             goto iget_no_retry;
1367         cifs_dbg(VFS, "server does not support POSIX extensions\n");
1368         tcon->unix_ext = false;
1369     }
1370 
1371     convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1372     if (tcon->posix_extensions)
1373         rc = smb311_posix_get_inode_info(&inode, path, sb, xid);
1374     else
1375         rc = cifs_get_inode_info(&inode, path, NULL, sb, xid, NULL);
1376 
1377 iget_no_retry:
1378     if (!inode) {
1379         inode = ERR_PTR(rc);
1380         goto out;
1381     }
1382 
1383     if (rc && tcon->pipe) {
1384         cifs_dbg(FYI, "ipc connection - fake read inode\n");
1385         spin_lock(&inode->i_lock);
1386         inode->i_mode |= S_IFDIR;
1387         set_nlink(inode, 2);
1388         inode->i_op = &cifs_ipc_inode_ops;
1389         inode->i_fop = &simple_dir_operations;
1390         inode->i_uid = cifs_sb->ctx->linux_uid;
1391         inode->i_gid = cifs_sb->ctx->linux_gid;
1392         spin_unlock(&inode->i_lock);
1393     } else if (rc) {
1394         iget_failed(inode);
1395         inode = ERR_PTR(rc);
1396     }
1397 
1398 out:
1399     kfree(path);
1400     free_xid(xid);
1401     return inode;
1402 }
1403 
1404 int
1405 cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
1406            const char *full_path, __u32 dosattr)
1407 {
1408     bool set_time = false;
1409     struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1410     struct TCP_Server_Info *server;
1411     FILE_BASIC_INFO info_buf;
1412 
1413     if (attrs == NULL)
1414         return -EINVAL;
1415 
1416     server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1417     if (!server->ops->set_file_info)
1418         return -ENOSYS;
1419 
1420     info_buf.Pad = 0;
1421 
1422     if (attrs->ia_valid & ATTR_ATIME) {
1423         set_time = true;
1424         info_buf.LastAccessTime =
1425             cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1426     } else
1427         info_buf.LastAccessTime = 0;
1428 
1429     if (attrs->ia_valid & ATTR_MTIME) {
1430         set_time = true;
1431         info_buf.LastWriteTime =
1432             cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1433     } else
1434         info_buf.LastWriteTime = 0;
1435 
1436     /*
1437      * Samba throws this field away, but windows may actually use it.
1438      * Do not set ctime unless other time stamps are changed explicitly
1439      * (i.e. by utimes()) since we would then have a mix of client and
1440      * server times.
1441      */
1442     if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1443         cifs_dbg(FYI, "CIFS - CTIME changed\n");
1444         info_buf.ChangeTime =
1445             cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1446     } else
1447         info_buf.ChangeTime = 0;
1448 
1449     info_buf.CreationTime = 0;  /* don't change */
1450     info_buf.Attributes = cpu_to_le32(dosattr);
1451 
1452     return server->ops->set_file_info(inode, full_path, &info_buf, xid);
1453 }
1454 
1455 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1456 /*
1457  * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1458  * and rename it to a random name that hopefully won't conflict with
1459  * anything else.
1460  */
1461 int
1462 cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1463                const unsigned int xid)
1464 {
1465     int oplock = 0;
1466     int rc;
1467     struct cifs_fid fid;
1468     struct cifs_open_parms oparms;
1469     struct inode *inode = d_inode(dentry);
1470     struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1471     struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1472     struct tcon_link *tlink;
1473     struct cifs_tcon *tcon;
1474     __u32 dosattr, origattr;
1475     FILE_BASIC_INFO *info_buf = NULL;
1476 
1477     tlink = cifs_sb_tlink(cifs_sb);
1478     if (IS_ERR(tlink))
1479         return PTR_ERR(tlink);
1480     tcon = tlink_tcon(tlink);
1481 
1482     /*
1483      * We cannot rename the file if the server doesn't support
1484      * CAP_INFOLEVEL_PASSTHRU
1485      */
1486     if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1487         rc = -EBUSY;
1488         goto out;
1489     }
1490 
1491     oparms.tcon = tcon;
1492     oparms.cifs_sb = cifs_sb;
1493     oparms.desired_access = DELETE | FILE_WRITE_ATTRIBUTES;
1494     oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
1495     oparms.disposition = FILE_OPEN;
1496     oparms.path = full_path;
1497     oparms.fid = &fid;
1498     oparms.reconnect = false;
1499 
1500     rc = CIFS_open(xid, &oparms, &oplock, NULL);
1501     if (rc != 0)
1502         goto out;
1503 
1504     origattr = cifsInode->cifsAttrs;
1505     if (origattr == 0)
1506         origattr |= ATTR_NORMAL;
1507 
1508     dosattr = origattr & ~ATTR_READONLY;
1509     if (dosattr == 0)
1510         dosattr |= ATTR_NORMAL;
1511     dosattr |= ATTR_HIDDEN;
1512 
1513     /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1514     if (dosattr != origattr) {
1515         info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1516         if (info_buf == NULL) {
1517             rc = -ENOMEM;
1518             goto out_close;
1519         }
1520         info_buf->Attributes = cpu_to_le32(dosattr);
1521         rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1522                     current->tgid);
1523         /* although we would like to mark the file hidden
1524            if that fails we will still try to rename it */
1525         if (!rc)
1526             cifsInode->cifsAttrs = dosattr;
1527         else
1528             dosattr = origattr; /* since not able to change them */
1529     }
1530 
1531     /* rename the file */
1532     rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1533                    cifs_sb->local_nls,
1534                    cifs_remap(cifs_sb));
1535     if (rc != 0) {
1536         rc = -EBUSY;
1537         goto undo_setattr;
1538     }
1539 
1540     /* try to set DELETE_ON_CLOSE */
1541     if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
1542         rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
1543                            current->tgid);
1544         /*
1545          * some samba versions return -ENOENT when we try to set the
1546          * file disposition here. Likely a samba bug, but work around
1547          * it for now. This means that some cifsXXX files may hang
1548          * around after they shouldn't.
1549          *
1550          * BB: remove this hack after more servers have the fix
1551          */
1552         if (rc == -ENOENT)
1553             rc = 0;
1554         else if (rc != 0) {
1555             rc = -EBUSY;
1556             goto undo_rename;
1557         }
1558         set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
1559     }
1560 
1561 out_close:
1562     CIFSSMBClose(xid, tcon, fid.netfid);
1563 out:
1564     kfree(info_buf);
1565     cifs_put_tlink(tlink);
1566     return rc;
1567 
1568     /*
1569      * reset everything back to the original state. Don't bother
1570      * dealing with errors here since we can't do anything about
1571      * them anyway.
1572      */
1573 undo_rename:
1574     CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
1575                 cifs_sb->local_nls, cifs_remap(cifs_sb));
1576 undo_setattr:
1577     if (dosattr != origattr) {
1578         info_buf->Attributes = cpu_to_le32(origattr);
1579         if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1580                     current->tgid))
1581             cifsInode->cifsAttrs = origattr;
1582     }
1583 
1584     goto out_close;
1585 }
1586 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1587 
1588 /* copied from fs/nfs/dir.c with small changes */
1589 static void
1590 cifs_drop_nlink(struct inode *inode)
1591 {
1592     spin_lock(&inode->i_lock);
1593     if (inode->i_nlink > 0)
1594         drop_nlink(inode);
1595     spin_unlock(&inode->i_lock);
1596 }
1597 
1598 /*
1599  * If d_inode(dentry) is null (usually meaning the cached dentry
1600  * is a negative dentry) then we would attempt a standard SMB delete, but
1601  * if that fails we can not attempt the fall back mechanisms on EACCES
1602  * but will return the EACCES to the caller. Note that the VFS does not call
1603  * unlink on negative dentries currently.
1604  */
1605 int cifs_unlink(struct inode *dir, struct dentry *dentry)
1606 {
1607     int rc = 0;
1608     unsigned int xid;
1609     const char *full_path;
1610     void *page;
1611     struct inode *inode = d_inode(dentry);
1612     struct cifsInodeInfo *cifs_inode;
1613     struct super_block *sb = dir->i_sb;
1614     struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1615     struct tcon_link *tlink;
1616     struct cifs_tcon *tcon;
1617     struct TCP_Server_Info *server;
1618     struct iattr *attrs = NULL;
1619     __u32 dosattr = 0, origattr = 0;
1620 
1621     cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1622 
1623     if (unlikely(cifs_forced_shutdown(cifs_sb)))
1624         return -EIO;
1625 
1626     tlink = cifs_sb_tlink(cifs_sb);
1627     if (IS_ERR(tlink))
1628         return PTR_ERR(tlink);
1629     tcon = tlink_tcon(tlink);
1630     server = tcon->ses->server;
1631 
1632     xid = get_xid();
1633     page = alloc_dentry_path();
1634 
1635     if (tcon->nodelete) {
1636         rc = -EACCES;
1637         goto unlink_out;
1638     }
1639 
1640     /* Unlink can be called from rename so we can not take the
1641      * sb->s_vfs_rename_mutex here */
1642     full_path = build_path_from_dentry(dentry, page);
1643     if (IS_ERR(full_path)) {
1644         rc = PTR_ERR(full_path);
1645         goto unlink_out;
1646     }
1647 
1648     cifs_close_deferred_file_under_dentry(tcon, full_path);
1649 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1650     if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1651                 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1652         rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1653             SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1654             cifs_remap(cifs_sb));
1655         cifs_dbg(FYI, "posix del rc %d\n", rc);
1656         if ((rc == 0) || (rc == -ENOENT))
1657             goto psx_del_no_retry;
1658     }
1659 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1660 
1661 retry_std_delete:
1662     if (!server->ops->unlink) {
1663         rc = -ENOSYS;
1664         goto psx_del_no_retry;
1665     }
1666 
1667     rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
1668 
1669 psx_del_no_retry:
1670     if (!rc) {
1671         if (inode)
1672             cifs_drop_nlink(inode);
1673     } else if (rc == -ENOENT) {
1674         d_drop(dentry);
1675     } else if (rc == -EBUSY) {
1676         if (server->ops->rename_pending_delete) {
1677             rc = server->ops->rename_pending_delete(full_path,
1678                                 dentry, xid);
1679             if (rc == 0)
1680                 cifs_drop_nlink(inode);
1681         }
1682     } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
1683         attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1684         if (attrs == NULL) {
1685             rc = -ENOMEM;
1686             goto out_reval;
1687         }
1688 
1689         /* try to reset dos attributes */
1690         cifs_inode = CIFS_I(inode);
1691         origattr = cifs_inode->cifsAttrs;
1692         if (origattr == 0)
1693             origattr |= ATTR_NORMAL;
1694         dosattr = origattr & ~ATTR_READONLY;
1695         if (dosattr == 0)
1696             dosattr |= ATTR_NORMAL;
1697         dosattr |= ATTR_HIDDEN;
1698 
1699         rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1700         if (rc != 0)
1701             goto out_reval;
1702 
1703         goto retry_std_delete;
1704     }
1705 
1706     /* undo the setattr if we errored out and it's needed */
1707     if (rc != 0 && dosattr != 0)
1708         cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1709 
1710 out_reval:
1711     if (inode) {
1712         cifs_inode = CIFS_I(inode);
1713         cifs_inode->time = 0;   /* will force revalidate to get info
1714                        when needed */
1715         inode->i_ctime = current_time(inode);
1716     }
1717     dir->i_ctime = dir->i_mtime = current_time(dir);
1718     cifs_inode = CIFS_I(dir);
1719     CIFS_I(dir)->time = 0;  /* force revalidate of dir as well */
1720 unlink_out:
1721     free_dentry_path(page);
1722     kfree(attrs);
1723     free_xid(xid);
1724     cifs_put_tlink(tlink);
1725     return rc;
1726 }
1727 
1728 static int
1729 cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
1730          const char *full_path, struct cifs_sb_info *cifs_sb,
1731          struct cifs_tcon *tcon, const unsigned int xid)
1732 {
1733     int rc = 0;
1734     struct inode *inode = NULL;
1735 
1736     if (tcon->posix_extensions)
1737         rc = smb311_posix_get_inode_info(&inode, full_path, parent->i_sb, xid);
1738 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1739     else if (tcon->unix_ext)
1740         rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
1741                           xid);
1742 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1743     else
1744         rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1745                      xid, NULL);
1746 
1747     if (rc)
1748         return rc;
1749 
1750     if (!S_ISDIR(inode->i_mode)) {
1751         /*
1752          * mkdir succeeded, but another client has managed to remove the
1753          * sucker and replace it with non-directory.  Return success,
1754          * but don't leave the child in dcache.
1755          */
1756          iput(inode);
1757          d_drop(dentry);
1758          return 0;
1759     }
1760     /*
1761      * setting nlink not necessary except in cases where we failed to get it
1762      * from the server or was set bogus. Also, since this is a brand new
1763      * inode, no need to grab the i_lock before setting the i_nlink.
1764      */
1765     if (inode->i_nlink < 2)
1766         set_nlink(inode, 2);
1767     mode &= ~current_umask();
1768     /* must turn on setgid bit if parent dir has it */
1769     if (parent->i_mode & S_ISGID)
1770         mode |= S_ISGID;
1771 
1772 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1773     if (tcon->unix_ext) {
1774         struct cifs_unix_set_info_args args = {
1775             .mode   = mode,
1776             .ctime  = NO_CHANGE_64,
1777             .atime  = NO_CHANGE_64,
1778             .mtime  = NO_CHANGE_64,
1779             .device = 0,
1780         };
1781         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1782             args.uid = current_fsuid();
1783             if (parent->i_mode & S_ISGID)
1784                 args.gid = parent->i_gid;
1785             else
1786                 args.gid = current_fsgid();
1787         } else {
1788             args.uid = INVALID_UID; /* no change */
1789             args.gid = INVALID_GID; /* no change */
1790         }
1791         CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1792                        cifs_sb->local_nls,
1793                        cifs_remap(cifs_sb));
1794     } else {
1795 #else
1796     {
1797 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1798         struct TCP_Server_Info *server = tcon->ses->server;
1799         if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1800             (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
1801             server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
1802                            tcon, xid);
1803         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1804             inode->i_mode = (mode | S_IFDIR);
1805 
1806         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1807             inode->i_uid = current_fsuid();
1808             if (inode->i_mode & S_ISGID)
1809                 inode->i_gid = parent->i_gid;
1810             else
1811                 inode->i_gid = current_fsgid();
1812         }
1813     }
1814     d_instantiate(dentry, inode);
1815     return 0;
1816 }
1817 
1818 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1819 static int
1820 cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
1821          const char *full_path, struct cifs_sb_info *cifs_sb,
1822          struct cifs_tcon *tcon, const unsigned int xid)
1823 {
1824     int rc = 0;
1825     u32 oplock = 0;
1826     FILE_UNIX_BASIC_INFO *info = NULL;
1827     struct inode *newinode = NULL;
1828     struct cifs_fattr fattr;
1829 
1830     info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1831     if (info == NULL) {
1832         rc = -ENOMEM;
1833         goto posix_mkdir_out;
1834     }
1835 
1836     mode &= ~current_umask();
1837     rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
1838                  NULL /* netfid */, info, &oplock, full_path,
1839                  cifs_sb->local_nls, cifs_remap(cifs_sb));
1840     if (rc == -EOPNOTSUPP)
1841         goto posix_mkdir_out;
1842     else if (rc) {
1843         cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
1844         d_drop(dentry);
1845         goto posix_mkdir_out;
1846     }
1847 
1848     if (info->Type == cpu_to_le32(-1))
1849         /* no return info, go query for it */
1850         goto posix_mkdir_get_info;
1851     /*
1852      * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
1853      * need to set uid/gid.
1854      */
1855 
1856     cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
1857     cifs_fill_uniqueid(inode->i_sb, &fattr);
1858     newinode = cifs_iget(inode->i_sb, &fattr);
1859     if (!newinode)
1860         goto posix_mkdir_get_info;
1861 
1862     d_instantiate(dentry, newinode);
1863 
1864 #ifdef CONFIG_CIFS_DEBUG2
1865     cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
1866          dentry, dentry, newinode);
1867 
1868     if (newinode->i_nlink != 2)
1869         cifs_dbg(FYI, "unexpected number of links %d\n",
1870              newinode->i_nlink);
1871 #endif
1872 
1873 posix_mkdir_out:
1874     kfree(info);
1875     return rc;
1876 posix_mkdir_get_info:
1877     rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
1878                   xid);
1879     goto posix_mkdir_out;
1880 }
1881 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1882 
1883 int cifs_mkdir(struct user_namespace *mnt_userns, struct inode *inode,
1884            struct dentry *direntry, umode_t mode)
1885 {
1886     int rc = 0;
1887     unsigned int xid;
1888     struct cifs_sb_info *cifs_sb;
1889     struct tcon_link *tlink;
1890     struct cifs_tcon *tcon;
1891     struct TCP_Server_Info *server;
1892     const char *full_path;
1893     void *page;
1894 
1895     cifs_dbg(FYI, "In cifs_mkdir, mode = %04ho inode = 0x%p\n",
1896          mode, inode);
1897 
1898     cifs_sb = CIFS_SB(inode->i_sb);
1899     if (unlikely(cifs_forced_shutdown(cifs_sb)))
1900         return -EIO;
1901     tlink = cifs_sb_tlink(cifs_sb);
1902     if (IS_ERR(tlink))
1903         return PTR_ERR(tlink);
1904     tcon = tlink_tcon(tlink);
1905 
1906     xid = get_xid();
1907 
1908     page = alloc_dentry_path();
1909     full_path = build_path_from_dentry(direntry, page);
1910     if (IS_ERR(full_path)) {
1911         rc = PTR_ERR(full_path);
1912         goto mkdir_out;
1913     }
1914 
1915     server = tcon->ses->server;
1916 
1917     if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) {
1918         rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path,
1919                           cifs_sb);
1920         d_drop(direntry); /* for time being always refresh inode info */
1921         goto mkdir_out;
1922     }
1923 
1924 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1925     if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1926                 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1927         rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
1928                       tcon, xid);
1929         if (rc != -EOPNOTSUPP)
1930             goto mkdir_out;
1931     }
1932 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1933 
1934     if (!server->ops->mkdir) {
1935         rc = -ENOSYS;
1936         goto mkdir_out;
1937     }
1938 
1939     /* BB add setting the equivalent of mode via CreateX w/ACLs */
1940     rc = server->ops->mkdir(xid, inode, mode, tcon, full_path, cifs_sb);
1941     if (rc) {
1942         cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
1943         d_drop(direntry);
1944         goto mkdir_out;
1945     }
1946 
1947     /* TODO: skip this for smb2/smb3 */
1948     rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
1949                   xid);
1950 mkdir_out:
1951     /*
1952      * Force revalidate to get parent dir info when needed since cached
1953      * attributes are invalid now.
1954      */
1955     CIFS_I(inode)->time = 0;
1956     free_dentry_path(page);
1957     free_xid(xid);
1958     cifs_put_tlink(tlink);
1959     return rc;
1960 }
1961 
1962 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
1963 {
1964     int rc = 0;
1965     unsigned int xid;
1966     struct cifs_sb_info *cifs_sb;
1967     struct tcon_link *tlink;
1968     struct cifs_tcon *tcon;
1969     struct TCP_Server_Info *server;
1970     const char *full_path;
1971     void *page = alloc_dentry_path();
1972     struct cifsInodeInfo *cifsInode;
1973 
1974     cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
1975 
1976     xid = get_xid();
1977 
1978     full_path = build_path_from_dentry(direntry, page);
1979     if (IS_ERR(full_path)) {
1980         rc = PTR_ERR(full_path);
1981         goto rmdir_exit;
1982     }
1983 
1984     cifs_sb = CIFS_SB(inode->i_sb);
1985     if (unlikely(cifs_forced_shutdown(cifs_sb))) {
1986         rc = -EIO;
1987         goto rmdir_exit;
1988     }
1989 
1990     tlink = cifs_sb_tlink(cifs_sb);
1991     if (IS_ERR(tlink)) {
1992         rc = PTR_ERR(tlink);
1993         goto rmdir_exit;
1994     }
1995     tcon = tlink_tcon(tlink);
1996     server = tcon->ses->server;
1997 
1998     if (!server->ops->rmdir) {
1999         rc = -ENOSYS;
2000         cifs_put_tlink(tlink);
2001         goto rmdir_exit;
2002     }
2003 
2004     if (tcon->nodelete) {
2005         rc = -EACCES;
2006         cifs_put_tlink(tlink);
2007         goto rmdir_exit;
2008     }
2009 
2010     rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
2011     cifs_put_tlink(tlink);
2012 
2013     if (!rc) {
2014         spin_lock(&d_inode(direntry)->i_lock);
2015         i_size_write(d_inode(direntry), 0);
2016         clear_nlink(d_inode(direntry));
2017         spin_unlock(&d_inode(direntry)->i_lock);
2018     }
2019 
2020     cifsInode = CIFS_I(d_inode(direntry));
2021     /* force revalidate to go get info when needed */
2022     cifsInode->time = 0;
2023 
2024     cifsInode = CIFS_I(inode);
2025     /*
2026      * Force revalidate to get parent dir info when needed since cached
2027      * attributes are invalid now.
2028      */
2029     cifsInode->time = 0;
2030 
2031     d_inode(direntry)->i_ctime = inode->i_ctime = inode->i_mtime =
2032         current_time(inode);
2033 
2034 rmdir_exit:
2035     free_dentry_path(page);
2036     free_xid(xid);
2037     return rc;
2038 }
2039 
2040 static int
2041 cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
2042            const char *from_path, struct dentry *to_dentry,
2043            const char *to_path)
2044 {
2045     struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
2046     struct tcon_link *tlink;
2047     struct cifs_tcon *tcon;
2048     struct TCP_Server_Info *server;
2049 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2050     struct cifs_fid fid;
2051     struct cifs_open_parms oparms;
2052     int oplock;
2053 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2054     int rc;
2055 
2056     tlink = cifs_sb_tlink(cifs_sb);
2057     if (IS_ERR(tlink))
2058         return PTR_ERR(tlink);
2059     tcon = tlink_tcon(tlink);
2060     server = tcon->ses->server;
2061 
2062     if (!server->ops->rename)
2063         return -ENOSYS;
2064 
2065     /* try path-based rename first */
2066     rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
2067 
2068     /*
2069      * Don't bother with rename by filehandle unless file is busy and
2070      * source. Note that cross directory moves do not work with
2071      * rename by filehandle to various Windows servers.
2072      */
2073     if (rc == 0 || rc != -EBUSY)
2074         goto do_rename_exit;
2075 
2076     /* Don't fall back to using SMB on SMB 2+ mount */
2077     if (server->vals->protocol_id != 0)
2078         goto do_rename_exit;
2079 
2080 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2081     /* open-file renames don't work across directories */
2082     if (to_dentry->d_parent != from_dentry->d_parent)
2083         goto do_rename_exit;
2084 
2085     oparms.tcon = tcon;
2086     oparms.cifs_sb = cifs_sb;
2087     /* open the file to be renamed -- we need DELETE perms */
2088     oparms.desired_access = DELETE;
2089     oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
2090     oparms.disposition = FILE_OPEN;
2091     oparms.path = from_path;
2092     oparms.fid = &fid;
2093     oparms.reconnect = false;
2094 
2095     rc = CIFS_open(xid, &oparms, &oplock, NULL);
2096     if (rc == 0) {
2097         rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
2098                 (const char *) to_dentry->d_name.name,
2099                 cifs_sb->local_nls, cifs_remap(cifs_sb));
2100         CIFSSMBClose(xid, tcon, fid.netfid);
2101     }
2102 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2103 do_rename_exit:
2104     if (rc == 0)
2105         d_move(from_dentry, to_dentry);
2106     cifs_put_tlink(tlink);
2107     return rc;
2108 }
2109 
2110 int
2111 cifs_rename2(struct user_namespace *mnt_userns, struct inode *source_dir,
2112          struct dentry *source_dentry, struct inode *target_dir,
2113          struct dentry *target_dentry, unsigned int flags)
2114 {
2115     const char *from_name, *to_name;
2116     void *page1, *page2;
2117     struct cifs_sb_info *cifs_sb;
2118     struct tcon_link *tlink;
2119     struct cifs_tcon *tcon;
2120     unsigned int xid;
2121     int rc, tmprc;
2122     int retry_count = 0;
2123     FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
2124 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2125     FILE_UNIX_BASIC_INFO *info_buf_target;
2126 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2127 
2128     if (flags & ~RENAME_NOREPLACE)
2129         return -EINVAL;
2130 
2131     cifs_sb = CIFS_SB(source_dir->i_sb);
2132     if (unlikely(cifs_forced_shutdown(cifs_sb)))
2133         return -EIO;
2134 
2135     tlink = cifs_sb_tlink(cifs_sb);
2136     if (IS_ERR(tlink))
2137         return PTR_ERR(tlink);
2138     tcon = tlink_tcon(tlink);
2139 
2140     page1 = alloc_dentry_path();
2141     page2 = alloc_dentry_path();
2142     xid = get_xid();
2143 
2144     from_name = build_path_from_dentry(source_dentry, page1);
2145     if (IS_ERR(from_name)) {
2146         rc = PTR_ERR(from_name);
2147         goto cifs_rename_exit;
2148     }
2149 
2150     to_name = build_path_from_dentry(target_dentry, page2);
2151     if (IS_ERR(to_name)) {
2152         rc = PTR_ERR(to_name);
2153         goto cifs_rename_exit;
2154     }
2155 
2156     cifs_close_deferred_file_under_dentry(tcon, from_name);
2157     if (d_inode(target_dentry) != NULL)
2158         cifs_close_deferred_file_under_dentry(tcon, to_name);
2159 
2160     rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2161                 to_name);
2162 
2163     if (rc == -EACCES) {
2164         while (retry_count < 3) {
2165             cifs_close_all_deferred_files(tcon);
2166             rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2167                         to_name);
2168             if (rc != -EACCES)
2169                 break;
2170             retry_count++;
2171         }
2172     }
2173 
2174     /*
2175      * No-replace is the natural behavior for CIFS, so skip unlink hacks.
2176      */
2177     if (flags & RENAME_NOREPLACE)
2178         goto cifs_rename_exit;
2179 
2180 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2181     if (rc == -EEXIST && tcon->unix_ext) {
2182         /*
2183          * Are src and dst hardlinks of same inode? We can only tell
2184          * with unix extensions enabled.
2185          */
2186         info_buf_source =
2187             kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO),
2188                     GFP_KERNEL);
2189         if (info_buf_source == NULL) {
2190             rc = -ENOMEM;
2191             goto cifs_rename_exit;
2192         }
2193 
2194         info_buf_target = info_buf_source + 1;
2195         tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
2196                          info_buf_source,
2197                          cifs_sb->local_nls,
2198                          cifs_remap(cifs_sb));
2199         if (tmprc != 0)
2200             goto unlink_target;
2201 
2202         tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
2203                          info_buf_target,
2204                          cifs_sb->local_nls,
2205                          cifs_remap(cifs_sb));
2206 
2207         if (tmprc == 0 && (info_buf_source->UniqueId ==
2208                    info_buf_target->UniqueId)) {
2209             /* same file, POSIX says that this is a noop */
2210             rc = 0;
2211             goto cifs_rename_exit;
2212         }
2213     }
2214     /*
2215      * else ... BB we could add the same check for Windows by
2216      * checking the UniqueId via FILE_INTERNAL_INFO
2217      */
2218 
2219 unlink_target:
2220 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2221 
2222     /* Try unlinking the target dentry if it's not negative */
2223     if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
2224         if (d_is_dir(target_dentry))
2225             tmprc = cifs_rmdir(target_dir, target_dentry);
2226         else
2227             tmprc = cifs_unlink(target_dir, target_dentry);
2228         if (tmprc)
2229             goto cifs_rename_exit;
2230         rc = cifs_do_rename(xid, source_dentry, from_name,
2231                     target_dentry, to_name);
2232     }
2233 
2234     /* force revalidate to go get info when needed */
2235     CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
2236 
2237     source_dir->i_ctime = source_dir->i_mtime = target_dir->i_ctime =
2238         target_dir->i_mtime = current_time(source_dir);
2239 
2240 cifs_rename_exit:
2241     kfree(info_buf_source);
2242     free_dentry_path(page2);
2243     free_dentry_path(page1);
2244     free_xid(xid);
2245     cifs_put_tlink(tlink);
2246     return rc;
2247 }
2248 
2249 static bool
2250 cifs_dentry_needs_reval(struct dentry *dentry)
2251 {
2252     struct inode *inode = d_inode(dentry);
2253     struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2254     struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2255     struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2256     struct cached_fid *cfid = NULL;
2257 
2258     if (cifs_i->time == 0)
2259         return true;
2260 
2261     if (CIFS_CACHE_READ(cifs_i))
2262         return false;
2263 
2264     if (!lookupCacheEnabled)
2265         return true;
2266 
2267     if (!open_cached_dir_by_dentry(tcon, dentry->d_parent, &cfid)) {
2268         mutex_lock(&cfid->fid_mutex);
2269         if (cfid->time && cifs_i->time > cfid->time) {
2270             mutex_unlock(&cfid->fid_mutex);
2271             close_cached_dir(cfid);
2272             return false;
2273         }
2274         mutex_unlock(&cfid->fid_mutex);
2275         close_cached_dir(cfid);
2276     }
2277     /*
2278      * depending on inode type, check if attribute caching disabled for
2279      * files or directories
2280      */
2281     if (S_ISDIR(inode->i_mode)) {
2282         if (!cifs_sb->ctx->acdirmax)
2283             return true;
2284         if (!time_in_range(jiffies, cifs_i->time,
2285                    cifs_i->time + cifs_sb->ctx->acdirmax))
2286             return true;
2287     } else { /* file */
2288         if (!cifs_sb->ctx->acregmax)
2289             return true;
2290         if (!time_in_range(jiffies, cifs_i->time,
2291                    cifs_i->time + cifs_sb->ctx->acregmax))
2292             return true;
2293     }
2294 
2295     /* hardlinked files w/ noserverino get "special" treatment */
2296     if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
2297         S_ISREG(inode->i_mode) && inode->i_nlink != 1)
2298         return true;
2299 
2300     return false;
2301 }
2302 
2303 /*
2304  * Zap the cache. Called when invalid_mapping flag is set.
2305  */
2306 int
2307 cifs_invalidate_mapping(struct inode *inode)
2308 {
2309     int rc = 0;
2310 
2311     if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
2312         rc = invalidate_inode_pages2(inode->i_mapping);
2313         if (rc)
2314             cifs_dbg(VFS, "%s: Could not invalidate inode %p\n",
2315                  __func__, inode);
2316     }
2317 
2318     return rc;
2319 }
2320 
2321 /**
2322  * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
2323  *
2324  * @key:    currently unused
2325  * @mode:   the task state to sleep in
2326  */
2327 static int
2328 cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
2329 {
2330     freezable_schedule_unsafe();
2331     if (signal_pending_state(mode, current))
2332         return -ERESTARTSYS;
2333     return 0;
2334 }
2335 
2336 int
2337 cifs_revalidate_mapping(struct inode *inode)
2338 {
2339     int rc;
2340     unsigned long *flags = &CIFS_I(inode)->flags;
2341     struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2342 
2343     /* swapfiles are not supposed to be shared */
2344     if (IS_SWAPFILE(inode))
2345         return 0;
2346 
2347     rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
2348                      TASK_KILLABLE);
2349     if (rc)
2350         return rc;
2351 
2352     if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
2353         /* for cache=singleclient, do not invalidate */
2354         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
2355             goto skip_invalidate;
2356 
2357         rc = cifs_invalidate_mapping(inode);
2358         if (rc)
2359             set_bit(CIFS_INO_INVALID_MAPPING, flags);
2360     }
2361 
2362 skip_invalidate:
2363     clear_bit_unlock(CIFS_INO_LOCK, flags);
2364     smp_mb__after_atomic();
2365     wake_up_bit(flags, CIFS_INO_LOCK);
2366 
2367     return rc;
2368 }
2369 
2370 int
2371 cifs_zap_mapping(struct inode *inode)
2372 {
2373     set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
2374     return cifs_revalidate_mapping(inode);
2375 }
2376 
2377 int cifs_revalidate_file_attr(struct file *filp)
2378 {
2379     int rc = 0;
2380     struct dentry *dentry = file_dentry(filp);
2381 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2382     struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
2383 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2384 
2385     if (!cifs_dentry_needs_reval(dentry))
2386         return rc;
2387 
2388 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2389     if (tlink_tcon(cfile->tlink)->unix_ext)
2390         rc = cifs_get_file_info_unix(filp);
2391     else
2392 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2393         rc = cifs_get_file_info(filp);
2394 
2395     return rc;
2396 }
2397 
2398 int cifs_revalidate_dentry_attr(struct dentry *dentry)
2399 {
2400     unsigned int xid;
2401     int rc = 0;
2402     struct inode *inode = d_inode(dentry);
2403     struct super_block *sb = dentry->d_sb;
2404     const char *full_path;
2405     void *page;
2406     int count = 0;
2407 
2408     if (inode == NULL)
2409         return -ENOENT;
2410 
2411     if (!cifs_dentry_needs_reval(dentry))
2412         return rc;
2413 
2414     xid = get_xid();
2415 
2416     page = alloc_dentry_path();
2417     full_path = build_path_from_dentry(dentry, page);
2418     if (IS_ERR(full_path)) {
2419         rc = PTR_ERR(full_path);
2420         goto out;
2421     }
2422 
2423     cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
2424          full_path, inode, inode->i_count.counter,
2425          dentry, cifs_get_time(dentry), jiffies);
2426 
2427 again:
2428     if (cifs_sb_master_tcon(CIFS_SB(sb))->posix_extensions)
2429         rc = smb311_posix_get_inode_info(&inode, full_path, sb, xid);
2430     else if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
2431         rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
2432     else
2433         rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
2434                      xid, NULL);
2435     if (rc == -EAGAIN && count++ < 10)
2436         goto again;
2437 out:
2438     free_dentry_path(page);
2439     free_xid(xid);
2440 
2441     return rc;
2442 }
2443 
2444 int cifs_revalidate_file(struct file *filp)
2445 {
2446     int rc;
2447     struct inode *inode = file_inode(filp);
2448 
2449     rc = cifs_revalidate_file_attr(filp);
2450     if (rc)
2451         return rc;
2452 
2453     return cifs_revalidate_mapping(inode);
2454 }
2455 
2456 /* revalidate a dentry's inode attributes */
2457 int cifs_revalidate_dentry(struct dentry *dentry)
2458 {
2459     int rc;
2460     struct inode *inode = d_inode(dentry);
2461 
2462     rc = cifs_revalidate_dentry_attr(dentry);
2463     if (rc)
2464         return rc;
2465 
2466     return cifs_revalidate_mapping(inode);
2467 }
2468 
2469 int cifs_getattr(struct user_namespace *mnt_userns, const struct path *path,
2470          struct kstat *stat, u32 request_mask, unsigned int flags)
2471 {
2472     struct dentry *dentry = path->dentry;
2473     struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
2474     struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2475     struct inode *inode = d_inode(dentry);
2476     int rc;
2477 
2478     if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb))))
2479         return -EIO;
2480 
2481     /*
2482      * We need to be sure that all dirty pages are written and the server
2483      * has actual ctime, mtime and file length.
2484      */
2485     if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_SIZE | STATX_BLOCKS)) &&
2486         !CIFS_CACHE_READ(CIFS_I(inode)) &&
2487         inode->i_mapping && inode->i_mapping->nrpages != 0) {
2488         rc = filemap_fdatawait(inode->i_mapping);
2489         if (rc) {
2490             mapping_set_error(inode->i_mapping, rc);
2491             return rc;
2492         }
2493     }
2494 
2495     if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_FORCE_SYNC)
2496         CIFS_I(inode)->time = 0; /* force revalidate */
2497 
2498     /*
2499      * If the caller doesn't require syncing, only sync if
2500      * necessary (e.g. due to earlier truncate or setattr
2501      * invalidating the cached metadata)
2502      */
2503     if (((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) ||
2504         (CIFS_I(inode)->time == 0)) {
2505         rc = cifs_revalidate_dentry_attr(dentry);
2506         if (rc)
2507             return rc;
2508     }
2509 
2510     generic_fillattr(&init_user_ns, inode, stat);
2511     stat->blksize = cifs_sb->ctx->bsize;
2512     stat->ino = CIFS_I(inode)->uniqueid;
2513 
2514     /* old CIFS Unix Extensions doesn't return create time */
2515     if (CIFS_I(inode)->createtime) {
2516         stat->result_mask |= STATX_BTIME;
2517         stat->btime =
2518               cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
2519     }
2520 
2521     stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
2522     if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
2523         stat->attributes |= STATX_ATTR_COMPRESSED;
2524     if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
2525         stat->attributes |= STATX_ATTR_ENCRYPTED;
2526 
2527     /*
2528      * If on a multiuser mount without unix extensions or cifsacl being
2529      * enabled, and the admin hasn't overridden them, set the ownership
2530      * to the fsuid/fsgid of the current process.
2531      */
2532     if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
2533         !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2534         !tcon->unix_ext) {
2535         if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2536             stat->uid = current_fsuid();
2537         if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2538             stat->gid = current_fsgid();
2539     }
2540     return 0;
2541 }
2542 
2543 int cifs_fiemap(struct inode *inode, struct fiemap_extent_info *fei, u64 start,
2544         u64 len)
2545 {
2546     struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2547     struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_i->netfs.inode.i_sb);
2548     struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2549     struct TCP_Server_Info *server = tcon->ses->server;
2550     struct cifsFileInfo *cfile;
2551     int rc;
2552 
2553     if (unlikely(cifs_forced_shutdown(cifs_sb)))
2554         return -EIO;
2555 
2556     /*
2557      * We need to be sure that all dirty pages are written as they
2558      * might fill holes on the server.
2559      */
2560     if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
2561         inode->i_mapping->nrpages != 0) {
2562         rc = filemap_fdatawait(inode->i_mapping);
2563         if (rc) {
2564             mapping_set_error(inode->i_mapping, rc);
2565             return rc;
2566         }
2567     }
2568 
2569     cfile = find_readable_file(cifs_i, false);
2570     if (cfile == NULL)
2571         return -EINVAL;
2572 
2573     if (server->ops->fiemap) {
2574         rc = server->ops->fiemap(tcon, cfile, fei, start, len);
2575         cifsFileInfo_put(cfile);
2576         return rc;
2577     }
2578 
2579     cifsFileInfo_put(cfile);
2580     return -ENOTSUPP;
2581 }
2582 
2583 int cifs_truncate_page(struct address_space *mapping, loff_t from)
2584 {
2585     pgoff_t index = from >> PAGE_SHIFT;
2586     unsigned offset = from & (PAGE_SIZE - 1);
2587     struct page *page;
2588     int rc = 0;
2589 
2590     page = grab_cache_page(mapping, index);
2591     if (!page)
2592         return -ENOMEM;
2593 
2594     zero_user_segment(page, offset, PAGE_SIZE);
2595     unlock_page(page);
2596     put_page(page);
2597     return rc;
2598 }
2599 
2600 void cifs_setsize(struct inode *inode, loff_t offset)
2601 {
2602     struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2603 
2604     spin_lock(&inode->i_lock);
2605     i_size_write(inode, offset);
2606     spin_unlock(&inode->i_lock);
2607 
2608     /* Cached inode must be refreshed on truncate */
2609     cifs_i->time = 0;
2610     truncate_pagecache(inode, offset);
2611 }
2612 
2613 static int
2614 cifs_set_file_size(struct inode *inode, struct iattr *attrs,
2615            unsigned int xid, const char *full_path)
2616 {
2617     int rc;
2618     struct cifsFileInfo *open_file;
2619     struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2620     struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2621     struct tcon_link *tlink = NULL;
2622     struct cifs_tcon *tcon = NULL;
2623     struct TCP_Server_Info *server;
2624 
2625     /*
2626      * To avoid spurious oplock breaks from server, in the case of
2627      * inodes that we already have open, avoid doing path based
2628      * setting of file size if we can do it by handle.
2629      * This keeps our caching token (oplock) and avoids timeouts
2630      * when the local oplock break takes longer to flush
2631      * writebehind data than the SMB timeout for the SetPathInfo
2632      * request would allow
2633      */
2634     open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2635     if (open_file) {
2636         tcon = tlink_tcon(open_file->tlink);
2637         server = tcon->ses->server;
2638         if (server->ops->set_file_size)
2639             rc = server->ops->set_file_size(xid, tcon, open_file,
2640                             attrs->ia_size, false);
2641         else
2642             rc = -ENOSYS;
2643         cifsFileInfo_put(open_file);
2644         cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
2645     } else
2646         rc = -EINVAL;
2647 
2648     if (!rc)
2649         goto set_size_out;
2650 
2651     if (tcon == NULL) {
2652         tlink = cifs_sb_tlink(cifs_sb);
2653         if (IS_ERR(tlink))
2654             return PTR_ERR(tlink);
2655         tcon = tlink_tcon(tlink);
2656         server = tcon->ses->server;
2657     }
2658 
2659     /*
2660      * Set file size by pathname rather than by handle either because no
2661      * valid, writeable file handle for it was found or because there was
2662      * an error setting it by handle.
2663      */
2664     if (server->ops->set_path_size)
2665         rc = server->ops->set_path_size(xid, tcon, full_path,
2666                         attrs->ia_size, cifs_sb, false);
2667     else
2668         rc = -ENOSYS;
2669     cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
2670 
2671     if (tlink)
2672         cifs_put_tlink(tlink);
2673 
2674 set_size_out:
2675     if (rc == 0) {
2676         cifsInode->server_eof = attrs->ia_size;
2677         cifs_setsize(inode, attrs->ia_size);
2678         /*
2679          * i_blocks is not related to (i_size / i_blksize), but instead
2680          * 512 byte (2**9) size is required for calculating num blocks.
2681          * Until we can query the server for actual allocation size,
2682          * this is best estimate we have for blocks allocated for a file
2683          * Number of blocks must be rounded up so size 1 is not 0 blocks
2684          */
2685         inode->i_blocks = (512 - 1 + attrs->ia_size) >> 9;
2686 
2687         /*
2688          * The man page of truncate says if the size changed,
2689          * then the st_ctime and st_mtime fields for the file
2690          * are updated.
2691          */
2692         attrs->ia_ctime = attrs->ia_mtime = current_time(inode);
2693         attrs->ia_valid |= ATTR_CTIME | ATTR_MTIME;
2694 
2695         cifs_truncate_page(inode->i_mapping, inode->i_size);
2696     }
2697 
2698     return rc;
2699 }
2700 
2701 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2702 static int
2703 cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2704 {
2705     int rc;
2706     unsigned int xid;
2707     const char *full_path;
2708     void *page = alloc_dentry_path();
2709     struct inode *inode = d_inode(direntry);
2710     struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2711     struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2712     struct tcon_link *tlink;
2713     struct cifs_tcon *pTcon;
2714     struct cifs_unix_set_info_args *args = NULL;
2715     struct cifsFileInfo *open_file;
2716 
2717     cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
2718          direntry, attrs->ia_valid);
2719 
2720     xid = get_xid();
2721 
2722     if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2723         attrs->ia_valid |= ATTR_FORCE;
2724 
2725     rc = setattr_prepare(&init_user_ns, direntry, attrs);
2726     if (rc < 0)
2727         goto out;
2728 
2729     full_path = build_path_from_dentry(direntry, page);
2730     if (IS_ERR(full_path)) {
2731         rc = PTR_ERR(full_path);
2732         goto out;
2733     }
2734 
2735     /*
2736      * Attempt to flush data before changing attributes. We need to do
2737      * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2738      * ownership or mode then we may also need to do this. Here, we take
2739      * the safe way out and just do the flush on all setattr requests. If
2740      * the flush returns error, store it to report later and continue.
2741      *
2742      * BB: This should be smarter. Why bother flushing pages that
2743      * will be truncated anyway? Also, should we error out here if
2744      * the flush returns error?
2745      */
2746     rc = filemap_write_and_wait(inode->i_mapping);
2747     if (is_interrupt_error(rc)) {
2748         rc = -ERESTARTSYS;
2749         goto out;
2750     }
2751 
2752     mapping_set_error(inode->i_mapping, rc);
2753     rc = 0;
2754 
2755     if (attrs->ia_valid & ATTR_SIZE) {
2756         rc = cifs_set_file_size(inode, attrs, xid, full_path);
2757         if (rc != 0)
2758             goto out;
2759     }
2760 
2761     /* skip mode change if it's just for clearing setuid/setgid */
2762     if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2763         attrs->ia_valid &= ~ATTR_MODE;
2764 
2765     args = kmalloc(sizeof(*args), GFP_KERNEL);
2766     if (args == NULL) {
2767         rc = -ENOMEM;
2768         goto out;
2769     }
2770 
2771     /* set up the struct */
2772     if (attrs->ia_valid & ATTR_MODE)
2773         args->mode = attrs->ia_mode;
2774     else
2775         args->mode = NO_CHANGE_64;
2776 
2777     if (attrs->ia_valid & ATTR_UID)
2778         args->uid = attrs->ia_uid;
2779     else
2780         args->uid = INVALID_UID; /* no change */
2781 
2782     if (attrs->ia_valid & ATTR_GID)
2783         args->gid = attrs->ia_gid;
2784     else
2785         args->gid = INVALID_GID; /* no change */
2786 
2787     if (attrs->ia_valid & ATTR_ATIME)
2788         args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2789     else
2790         args->atime = NO_CHANGE_64;
2791 
2792     if (attrs->ia_valid & ATTR_MTIME)
2793         args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2794     else
2795         args->mtime = NO_CHANGE_64;
2796 
2797     if (attrs->ia_valid & ATTR_CTIME)
2798         args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2799     else
2800         args->ctime = NO_CHANGE_64;
2801 
2802     args->device = 0;
2803     open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2804     if (open_file) {
2805         u16 nfid = open_file->fid.netfid;
2806         u32 npid = open_file->pid;
2807         pTcon = tlink_tcon(open_file->tlink);
2808         rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
2809         cifsFileInfo_put(open_file);
2810     } else {
2811         tlink = cifs_sb_tlink(cifs_sb);
2812         if (IS_ERR(tlink)) {
2813             rc = PTR_ERR(tlink);
2814             goto out;
2815         }
2816         pTcon = tlink_tcon(tlink);
2817         rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
2818                     cifs_sb->local_nls,
2819                     cifs_remap(cifs_sb));
2820         cifs_put_tlink(tlink);
2821     }
2822 
2823     if (rc)
2824         goto out;
2825 
2826     if ((attrs->ia_valid & ATTR_SIZE) &&
2827         attrs->ia_size != i_size_read(inode)) {
2828         truncate_setsize(inode, attrs->ia_size);
2829         fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
2830     }
2831 
2832     setattr_copy(&init_user_ns, inode, attrs);
2833     mark_inode_dirty(inode);
2834 
2835     /* force revalidate when any of these times are set since some
2836        of the fs types (eg ext3, fat) do not have fine enough
2837        time granularity to match protocol, and we do not have a
2838        a way (yet) to query the server fs's time granularity (and
2839        whether it rounds times down).
2840     */
2841     if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2842         cifsInode->time = 0;
2843 out:
2844     kfree(args);
2845     free_dentry_path(page);
2846     free_xid(xid);
2847     return rc;
2848 }
2849 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2850 
2851 static int
2852 cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
2853 {
2854     unsigned int xid;
2855     kuid_t uid = INVALID_UID;
2856     kgid_t gid = INVALID_GID;
2857     struct inode *inode = d_inode(direntry);
2858     struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2859     struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2860     struct cifsFileInfo *wfile;
2861     struct cifs_tcon *tcon;
2862     const char *full_path;
2863     void *page = alloc_dentry_path();
2864     int rc = -EACCES;
2865     __u32 dosattr = 0;
2866     __u64 mode = NO_CHANGE_64;
2867 
2868     xid = get_xid();
2869 
2870     cifs_dbg(FYI, "setattr on file %pd attrs->ia_valid 0x%x\n",
2871          direntry, attrs->ia_valid);
2872 
2873     if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2874         attrs->ia_valid |= ATTR_FORCE;
2875 
2876     rc = setattr_prepare(&init_user_ns, direntry, attrs);
2877     if (rc < 0)
2878         goto cifs_setattr_exit;
2879 
2880     full_path = build_path_from_dentry(direntry, page);
2881     if (IS_ERR(full_path)) {
2882         rc = PTR_ERR(full_path);
2883         goto cifs_setattr_exit;
2884     }
2885 
2886     /*
2887      * Attempt to flush data before changing attributes. We need to do
2888      * this for ATTR_SIZE and ATTR_MTIME.  If the flush of the data
2889      * returns error, store it to report later and continue.
2890      *
2891      * BB: This should be smarter. Why bother flushing pages that
2892      * will be truncated anyway? Also, should we error out here if
2893      * the flush returns error? Do we need to check for ATTR_MTIME_SET flag?
2894      */
2895     if (attrs->ia_valid & (ATTR_MTIME | ATTR_SIZE | ATTR_CTIME)) {
2896         rc = filemap_write_and_wait(inode->i_mapping);
2897         if (is_interrupt_error(rc)) {
2898             rc = -ERESTARTSYS;
2899             goto cifs_setattr_exit;
2900         }
2901         mapping_set_error(inode->i_mapping, rc);
2902     }
2903 
2904     rc = 0;
2905 
2906     if ((attrs->ia_valid & ATTR_MTIME) &&
2907         !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2908         rc = cifs_get_writable_file(cifsInode, FIND_WR_ANY, &wfile);
2909         if (!rc) {
2910             tcon = tlink_tcon(wfile->tlink);
2911             rc = tcon->ses->server->ops->flush(xid, tcon, &wfile->fid);
2912             cifsFileInfo_put(wfile);
2913             if (rc)
2914                 goto cifs_setattr_exit;
2915         } else if (rc != -EBADF)
2916             goto cifs_setattr_exit;
2917         else
2918             rc = 0;
2919     }
2920 
2921     if (attrs->ia_valid & ATTR_SIZE) {
2922         rc = cifs_set_file_size(inode, attrs, xid, full_path);
2923         if (rc != 0)
2924             goto cifs_setattr_exit;
2925     }
2926 
2927     if (attrs->ia_valid & ATTR_UID)
2928         uid = attrs->ia_uid;
2929 
2930     if (attrs->ia_valid & ATTR_GID)
2931         gid = attrs->ia_gid;
2932 
2933     if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
2934         (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
2935         if (uid_valid(uid) || gid_valid(gid)) {
2936             mode = NO_CHANGE_64;
2937             rc = id_mode_to_cifs_acl(inode, full_path, &mode,
2938                             uid, gid);
2939             if (rc) {
2940                 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
2941                      __func__, rc);
2942                 goto cifs_setattr_exit;
2943             }
2944         }
2945     } else
2946     if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
2947         attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
2948 
2949     /* skip mode change if it's just for clearing setuid/setgid */
2950     if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2951         attrs->ia_valid &= ~ATTR_MODE;
2952 
2953     if (attrs->ia_valid & ATTR_MODE) {
2954         mode = attrs->ia_mode;
2955         rc = 0;
2956         if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
2957             (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
2958             rc = id_mode_to_cifs_acl(inode, full_path, &mode,
2959                         INVALID_UID, INVALID_GID);
2960             if (rc) {
2961                 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
2962                      __func__, rc);
2963                 goto cifs_setattr_exit;
2964             }
2965 
2966             /*
2967              * In case of CIFS_MOUNT_CIFS_ACL, we cannot support all modes.
2968              * Pick up the actual mode bits that were set.
2969              */
2970             if (mode != attrs->ia_mode)
2971                 attrs->ia_mode = mode;
2972         } else
2973         if (((mode & S_IWUGO) == 0) &&
2974             (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
2975 
2976             dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
2977 
2978             /* fix up mode if we're not using dynperm */
2979             if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
2980                 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
2981         } else if ((mode & S_IWUGO) &&
2982                (cifsInode->cifsAttrs & ATTR_READONLY)) {
2983 
2984             dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
2985             /* Attributes of 0 are ignored */
2986             if (dosattr == 0)
2987                 dosattr |= ATTR_NORMAL;
2988 
2989             /* reset local inode permissions to normal */
2990             if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2991                 attrs->ia_mode &= ~(S_IALLUGO);
2992                 if (S_ISDIR(inode->i_mode))
2993                     attrs->ia_mode |=
2994                         cifs_sb->ctx->dir_mode;
2995                 else
2996                     attrs->ia_mode |=
2997                         cifs_sb->ctx->file_mode;
2998             }
2999         } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3000             /* ignore mode change - ATTR_READONLY hasn't changed */
3001             attrs->ia_valid &= ~ATTR_MODE;
3002         }
3003     }
3004 
3005     if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
3006         ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
3007         rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
3008         /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
3009 
3010         /* Even if error on time set, no sense failing the call if
3011         the server would set the time to a reasonable value anyway,
3012         and this check ensures that we are not being called from
3013         sys_utimes in which case we ought to fail the call back to
3014         the user when the server rejects the call */
3015         if ((rc) && (attrs->ia_valid &
3016                 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
3017             rc = 0;
3018     }
3019 
3020     /* do not need local check to inode_check_ok since the server does
3021        that */
3022     if (rc)
3023         goto cifs_setattr_exit;
3024 
3025     if ((attrs->ia_valid & ATTR_SIZE) &&
3026         attrs->ia_size != i_size_read(inode)) {
3027         truncate_setsize(inode, attrs->ia_size);
3028         fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
3029     }
3030 
3031     setattr_copy(&init_user_ns, inode, attrs);
3032     mark_inode_dirty(inode);
3033 
3034 cifs_setattr_exit:
3035     free_xid(xid);
3036     free_dentry_path(page);
3037     return rc;
3038 }
3039 
3040 int
3041 cifs_setattr(struct user_namespace *mnt_userns, struct dentry *direntry,
3042          struct iattr *attrs)
3043 {
3044     struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
3045     int rc, retries = 0;
3046 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3047     struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
3048 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3049 
3050     if (unlikely(cifs_forced_shutdown(cifs_sb)))
3051         return -EIO;
3052 
3053     do {
3054 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3055         if (pTcon->unix_ext)
3056             rc = cifs_setattr_unix(direntry, attrs);
3057         else
3058 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3059             rc = cifs_setattr_nounix(direntry, attrs);
3060         retries++;
3061     } while (is_retryable_error(rc) && retries < 2);
3062 
3063     /* BB: add cifs_setattr_legacy for really old servers */
3064     return rc;
3065 }