]> git.ipfire.org Git - thirdparty/linux.git/blobdiff - fs/smb/client/smb2ops.c
cifs: update iface_last_update on each query-and-update
[thirdparty/linux.git] / fs / smb / client / smb2ops.c
index 14bc745de199b85e6699ab97eaaaf33daea1c1c9..d9553c2556a290dcea14434e00df9d854e713aa3 100644 (file)
@@ -614,7 +614,8 @@ parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
                                 "multichannel not available\n"
                                 "Empty network interface list returned by server %s\n",
                                 ses->server->hostname);
-               rc = -EINVAL;
+               rc = -EOPNOTSUPP;
+               ses->iface_last_update = jiffies;
                goto out;
        }
 
@@ -712,7 +713,6 @@ parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
 
                ses->iface_count++;
                spin_unlock(&ses->iface_lock);
-               ses->iface_last_update = jiffies;
 next_iface:
                nb_iface++;
                next = le32_to_cpu(p->Next);
@@ -734,11 +734,7 @@ next_iface:
        if ((bytes_left > 8) || p->Next)
                cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
 
-
-       if (!ses->iface_count) {
-               rc = -EINVAL;
-               goto out;
-       }
+       ses->iface_last_update = jiffies;
 
 out:
        /*
@@ -1935,7 +1931,6 @@ static int
 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
                   struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
 {
-       __le64 eof = cpu_to_le64(size);
        struct inode *inode;
 
        /*
@@ -1952,7 +1947,7 @@ smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
        }
 
        return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
-                           cfile->fid.volatile_fid, cfile->pid, &eof);
+                           cfile->fid.volatile_fid, cfile->pid, size);
 }
 
 static int
@@ -2948,18 +2943,6 @@ int parse_reparse_point(struct reparse_data_buffer *buf,
                        u32 plen, struct cifs_sb_info *cifs_sb,
                        bool unicode, struct cifs_open_info_data *data)
 {
-       if (plen < sizeof(*buf)) {
-               cifs_dbg(VFS, "%s: reparse buffer is too small. Must be at least 8 bytes but was %d\n",
-                        __func__, plen);
-               return -EIO;
-       }
-
-       if (plen < le16_to_cpu(buf->ReparseDataLength) + sizeof(*buf)) {
-               cifs_dbg(VFS, "%s: invalid reparse buf length: %d\n",
-                        __func__, plen);
-               return -EIO;
-       }
-
        data->reparse.buf = buf;
 
        /* See MS-FSCC 2.1.2 */
@@ -2997,145 +2980,6 @@ static int smb2_parse_reparse_point(struct cifs_sb_info *cifs_sb,
        return parse_reparse_point(buf, plen, cifs_sb, true, data);
 }
 
-static int smb2_query_reparse_point(const unsigned int xid,
-                                   struct cifs_tcon *tcon,
-                                   struct cifs_sb_info *cifs_sb,
-                                   const char *full_path,
-                                   u32 *tag, struct kvec *rsp,
-                                   int *rsp_buftype)
-{
-       struct smb2_compound_vars *vars;
-       int rc;
-       __le16 *utf16_path = NULL;
-       __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
-       struct cifs_open_parms oparms;
-       struct cifs_fid fid;
-       struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
-       int flags = CIFS_CP_CREATE_CLOSE_OP;
-       struct smb_rqst *rqst;
-       int resp_buftype[3];
-       struct kvec *rsp_iov;
-       struct smb2_ioctl_rsp *ioctl_rsp;
-       struct reparse_data_buffer *reparse_buf;
-       u32 off, count, len;
-
-       cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
-
-       if (smb3_encryption_required(tcon))
-               flags |= CIFS_TRANSFORM_REQ;
-
-       utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
-       if (!utf16_path)
-               return -ENOMEM;
-
-       resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
-       vars = kzalloc(sizeof(*vars), GFP_KERNEL);
-       if (!vars) {
-               rc = -ENOMEM;
-               goto out_free_path;
-       }
-       rqst = vars->rqst;
-       rsp_iov = vars->rsp_iov;
-
-       /*
-        * setup smb2open - TODO add optimization to call cifs_get_readable_path
-        * to see if there is a handle already open that we can use
-        */
-       rqst[0].rq_iov = vars->open_iov;
-       rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
-
-       oparms = (struct cifs_open_parms) {
-               .tcon = tcon,
-               .path = full_path,
-               .desired_access = FILE_READ_ATTRIBUTES,
-               .disposition = FILE_OPEN,
-               .create_options = cifs_create_options(cifs_sb, OPEN_REPARSE_POINT),
-               .fid = &fid,
-       };
-
-       rc = SMB2_open_init(tcon, server,
-                           &rqst[0], &oplock, &oparms, utf16_path);
-       if (rc)
-               goto query_rp_exit;
-       smb2_set_next_command(tcon, &rqst[0]);
-
-
-       /* IOCTL */
-       rqst[1].rq_iov = vars->io_iov;
-       rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
-
-       rc = SMB2_ioctl_init(tcon, server,
-                            &rqst[1], COMPOUND_FID,
-                            COMPOUND_FID, FSCTL_GET_REPARSE_POINT, NULL, 0,
-                            CIFSMaxBufSize -
-                            MAX_SMB2_CREATE_RESPONSE_SIZE -
-                            MAX_SMB2_CLOSE_RESPONSE_SIZE);
-       if (rc)
-               goto query_rp_exit;
-
-       smb2_set_next_command(tcon, &rqst[1]);
-       smb2_set_related(&rqst[1]);
-
-       /* Close */
-       rqst[2].rq_iov = &vars->close_iov;
-       rqst[2].rq_nvec = 1;
-
-       rc = SMB2_close_init(tcon, server,
-                            &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
-       if (rc)
-               goto query_rp_exit;
-
-       smb2_set_related(&rqst[2]);
-
-       rc = compound_send_recv(xid, tcon->ses, server,
-                               flags, 3, rqst,
-                               resp_buftype, rsp_iov);
-
-       ioctl_rsp = rsp_iov[1].iov_base;
-
-       /*
-        * Open was successful and we got an ioctl response.
-        */
-       if (rc == 0) {
-               /* See MS-FSCC 2.3.23 */
-               off = le32_to_cpu(ioctl_rsp->OutputOffset);
-               count = le32_to_cpu(ioctl_rsp->OutputCount);
-               if (check_add_overflow(off, count, &len) ||
-                   len > rsp_iov[1].iov_len) {
-                       cifs_tcon_dbg(VFS, "%s: invalid ioctl: off=%d count=%d\n",
-                                     __func__, off, count);
-                       rc = -EIO;
-                       goto query_rp_exit;
-               }
-
-               reparse_buf = (void *)((u8 *)ioctl_rsp + off);
-               len = sizeof(*reparse_buf);
-               if (count < len ||
-                   count < le16_to_cpu(reparse_buf->ReparseDataLength) + len) {
-                       cifs_tcon_dbg(VFS, "%s: invalid ioctl: off=%d count=%d\n",
-                                     __func__, off, count);
-                       rc = -EIO;
-                       goto query_rp_exit;
-               }
-               *tag = le32_to_cpu(reparse_buf->ReparseTag);
-               *rsp = rsp_iov[1];
-               *rsp_buftype = resp_buftype[1];
-               resp_buftype[1] = CIFS_NO_BUFFER;
-       }
-
- query_rp_exit:
-       SMB2_open_free(&rqst[0]);
-       SMB2_ioctl_free(&rqst[1]);
-       SMB2_close_free(&rqst[2]);
-       free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
-       free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
-       free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
-       kfree(vars);
-out_free_path:
-       kfree(utf16_path);
-       return rc;
-}
-
 static struct cifs_ntsd *
 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
                    const struct cifs_fid *cifsfid, u32 *pacllen, u32 info)
@@ -3336,7 +3180,6 @@ static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
        unsigned long long new_size;
        long rc;
        unsigned int xid;
-       __le64 eof;
 
        xid = get_xid();
 
@@ -3366,9 +3209,8 @@ static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
         */
        new_size = offset + len;
        if (keep_size == false && (unsigned long long)i_size_read(inode) < new_size) {
-               eof = cpu_to_le64(new_size);
                rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
-                                 cfile->fid.volatile_fid, cfile->pid, &eof);
+                                 cfile->fid.volatile_fid, cfile->pid, new_size);
                if (rc >= 0) {
                        truncate_setsize(inode, new_size);
                        fscache_resize_cookie(cifs_inode_cookie(inode), new_size);
@@ -3561,7 +3403,7 @@ static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
        struct cifsFileInfo *cfile = file->private_data;
        long rc = -EOPNOTSUPP;
        unsigned int xid;
-       __le64 eof;
+       loff_t new_eof;
 
        xid = get_xid();
 
@@ -3590,14 +3432,14 @@ static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
                if (cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)
                        smb2_set_sparse(xid, tcon, cfile, inode, false);
 
-               eof = cpu_to_le64(off + len);
+               new_eof = off + len;
                rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
-                                 cfile->fid.volatile_fid, cfile->pid, &eof);
+                                 cfile->fid.volatile_fid, cfile->pid, new_eof);
                if (rc == 0) {
-                       cifsi->server_eof = off + len;
-                       cifs_setsize(inode, off + len);
+                       cifsi->server_eof = new_eof;
+                       cifs_setsize(inode, new_eof);
                        cifs_truncate_page(inode->i_mapping, inode->i_size);
-                       truncate_setsize(inode, off + len);
+                       truncate_setsize(inode, new_eof);
                }
                goto out;
        }
@@ -3688,8 +3530,7 @@ static long smb3_collapse_range(struct file *file, struct cifs_tcon *tcon,
        struct inode *inode = file_inode(file);
        struct cifsFileInfo *cfile = file->private_data;
        struct cifsInodeInfo *cifsi = CIFS_I(inode);
-       __le64 eof;
-       loff_t old_eof;
+       loff_t old_eof, new_eof;
 
        xid = get_xid();
 
@@ -3714,9 +3555,9 @@ static long smb3_collapse_range(struct file *file, struct cifs_tcon *tcon,
        if (rc < 0)
                goto out_2;
 
-       eof = cpu_to_le64(old_eof - len);
+       new_eof = old_eof - len;
        rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
-                         cfile->fid.volatile_fid, cfile->pid, &eof);
+                         cfile->fid.volatile_fid, cfile->pid, new_eof);
        if (rc < 0)
                goto out_2;
 
@@ -3740,8 +3581,7 @@ static long smb3_insert_range(struct file *file, struct cifs_tcon *tcon,
        unsigned int xid;
        struct cifsFileInfo *cfile = file->private_data;
        struct inode *inode = file_inode(file);
-       __le64 eof;
-       __u64  count, old_eof;
+       __u64 count, old_eof, new_eof;
 
        xid = get_xid();
 
@@ -3754,20 +3594,20 @@ static long smb3_insert_range(struct file *file, struct cifs_tcon *tcon,
        }
 
        count = old_eof - off;
-       eof = cpu_to_le64(old_eof + len);
+       new_eof = old_eof + len;
 
        filemap_invalidate_lock(inode->i_mapping);
-       rc = filemap_write_and_wait_range(inode->i_mapping, off, old_eof + len - 1);
+       rc = filemap_write_and_wait_range(inode->i_mapping, off, new_eof - 1);
        if (rc < 0)
                goto out_2;
        truncate_pagecache_range(inode, off, old_eof);
 
        rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
-                         cfile->fid.volatile_fid, cfile->pid, &eof);
+                         cfile->fid.volatile_fid, cfile->pid, new_eof);
        if (rc < 0)
                goto out_2;
 
-       truncate_setsize(inode, old_eof + len);
+       truncate_setsize(inode, new_eof);
        fscache_resize_cookie(cifs_inode_cookie(inode), i_size_read(inode));
 
        rc = smb2_copychunk_range(xid, cfile, cfile, off, count, off + len);
@@ -5171,11 +5011,154 @@ int cifs_sfu_make_node(unsigned int xid, struct inode *inode,
        return rc;
 }
 
+static inline u64 mode_nfs_type(mode_t mode)
+{
+       switch (mode & S_IFMT) {
+       case S_IFBLK: return NFS_SPECFILE_BLK;
+       case S_IFCHR: return NFS_SPECFILE_CHR;
+       case S_IFIFO: return NFS_SPECFILE_FIFO;
+       case S_IFSOCK: return NFS_SPECFILE_SOCK;
+       }
+       return 0;
+}
+
+static int nfs_set_reparse_buf(struct reparse_posix_data *buf,
+                              mode_t mode, dev_t dev,
+                              struct kvec *iov)
+{
+       u64 type;
+       u16 len, dlen;
+
+       len = sizeof(*buf);
+
+       switch ((type = mode_nfs_type(mode))) {
+       case NFS_SPECFILE_BLK:
+       case NFS_SPECFILE_CHR:
+               dlen = sizeof(__le64);
+               break;
+       case NFS_SPECFILE_FIFO:
+       case NFS_SPECFILE_SOCK:
+               dlen = 0;
+               break;
+       default:
+               return -EOPNOTSUPP;
+       }
+
+       buf->ReparseTag = cpu_to_le32(IO_REPARSE_TAG_NFS);
+       buf->Reserved = 0;
+       buf->InodeType = cpu_to_le64(type);
+       buf->ReparseDataLength = cpu_to_le16(len + dlen -
+                                            sizeof(struct reparse_data_buffer));
+       *(__le64 *)buf->DataBuffer = cpu_to_le64(((u64)MAJOR(dev) << 32) |
+                                                MINOR(dev));
+       iov->iov_base = buf;
+       iov->iov_len = len + dlen;
+       return 0;
+}
+
+static int nfs_make_node(unsigned int xid, struct inode *inode,
+                        struct dentry *dentry, struct cifs_tcon *tcon,
+                        const char *full_path, umode_t mode, dev_t dev)
+{
+       struct cifs_open_info_data data;
+       struct reparse_posix_data *p;
+       struct inode *new;
+       struct kvec iov;
+       __u8 buf[sizeof(*p) + sizeof(__le64)];
+       int rc;
+
+       p = (struct reparse_posix_data *)buf;
+       rc = nfs_set_reparse_buf(p, mode, dev, &iov);
+       if (rc)
+               return rc;
+
+       data = (struct cifs_open_info_data) {
+               .reparse_point = true,
+               .reparse = { .tag = IO_REPARSE_TAG_NFS, .posix = p, },
+       };
+
+       new = smb2_get_reparse_inode(&data, inode->i_sb, xid,
+                                    tcon, full_path, &iov);
+       if (!IS_ERR(new))
+               d_instantiate(dentry, new);
+       else
+               rc = PTR_ERR(new);
+       cifs_free_open_info(&data);
+       return rc;
+}
+
+static int smb2_create_reparse_symlink(const unsigned int xid,
+                                      struct inode *inode,
+                                      struct dentry *dentry,
+                                      struct cifs_tcon *tcon,
+                                      const char *full_path,
+                                      const char *symname)
+{
+       struct reparse_symlink_data_buffer *buf = NULL;
+       struct cifs_open_info_data data;
+       struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
+       struct inode *new;
+       struct kvec iov;
+       __le16 *path;
+       char *sym;
+       u16 len, plen;
+       int rc = 0;
+
+       sym = kstrdup(symname, GFP_KERNEL);
+       if (!sym)
+               return -ENOMEM;
+
+       data = (struct cifs_open_info_data) {
+               .reparse_point = true,
+               .reparse = { .tag = IO_REPARSE_TAG_SYMLINK, },
+               .symlink_target = sym,
+       };
+
+       path = cifs_convert_path_to_utf16(symname, cifs_sb);
+       if (!path) {
+               rc = -ENOMEM;
+               goto out;
+       }
+
+       plen = 2 * UniStrnlen((wchar_t *)path, PATH_MAX);
+       len = sizeof(*buf) + plen * 2;
+       buf = kzalloc(len, GFP_KERNEL);
+       if (!buf) {
+               rc = -ENOMEM;
+               goto out;
+       }
+
+       buf->ReparseTag = cpu_to_le32(IO_REPARSE_TAG_SYMLINK);
+       buf->ReparseDataLength = cpu_to_le16(len - sizeof(struct reparse_data_buffer));
+       buf->SubstituteNameOffset = cpu_to_le16(plen);
+       buf->SubstituteNameLength = cpu_to_le16(plen);
+       memcpy(&buf->PathBuffer[plen], path, plen);
+       buf->PrintNameOffset = 0;
+       buf->PrintNameLength = cpu_to_le16(plen);
+       memcpy(buf->PathBuffer, path, plen);
+       buf->Flags = cpu_to_le32(*symname != '/' ? SYMLINK_FLAG_RELATIVE : 0);
+
+       iov.iov_base = buf;
+       iov.iov_len = len;
+       new = smb2_get_reparse_inode(&data, inode->i_sb, xid,
+                                    tcon, full_path, &iov);
+       if (!IS_ERR(new))
+               d_instantiate(dentry, new);
+       else
+               rc = PTR_ERR(new);
+out:
+       kfree(path);
+       cifs_free_open_info(&data);
+       kfree(buf);
+       return rc;
+}
+
 static int smb2_make_node(unsigned int xid, struct inode *inode,
                          struct dentry *dentry, struct cifs_tcon *tcon,
                          const char *full_path, umode_t mode, dev_t dev)
 {
        struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
+       int rc;
 
        /*
         * Check if mounted with mount parm 'sfu' mount parm.
@@ -5183,15 +5166,14 @@ static int smb2_make_node(unsigned int xid, struct inode *inode,
         * supports block and char device (no socket & fifo),
         * and was used by default in earlier versions of Windows
         */
-       if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
-               return -EPERM;
-       /*
-        * TODO: Add ability to create instead via reparse point. Windows (e.g.
-        * their current NFS server) uses this approach to expose special files
-        * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
-        */
-       return cifs_sfu_make_node(xid, inode, dentry, tcon,
-                                 full_path, mode, dev);
+       if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
+               rc = cifs_sfu_make_node(xid, inode, dentry, tcon,
+                                       full_path, mode, dev);
+       } else {
+               rc = nfs_make_node(xid, inode, dentry, tcon,
+                                  full_path, mode, dev);
+       }
+       return rc;
 }
 
 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
@@ -5247,6 +5229,7 @@ struct smb_version_operations smb20_operations = {
        .parse_reparse_point = smb2_parse_reparse_point,
        .query_mf_symlink = smb3_query_mf_symlink,
        .create_mf_symlink = smb3_create_mf_symlink,
+       .create_reparse_symlink = smb2_create_reparse_symlink,
        .open = smb2_open_file,
        .set_fid = smb2_set_fid,
        .close = smb2_close_file,
@@ -5349,6 +5332,7 @@ struct smb_version_operations smb21_operations = {
        .parse_reparse_point = smb2_parse_reparse_point,
        .query_mf_symlink = smb3_query_mf_symlink,
        .create_mf_symlink = smb3_create_mf_symlink,
+       .create_reparse_symlink = smb2_create_reparse_symlink,
        .open = smb2_open_file,
        .set_fid = smb2_set_fid,
        .close = smb2_close_file,
@@ -5454,6 +5438,7 @@ struct smb_version_operations smb30_operations = {
        .parse_reparse_point = smb2_parse_reparse_point,
        .query_mf_symlink = smb3_query_mf_symlink,
        .create_mf_symlink = smb3_create_mf_symlink,
+       .create_reparse_symlink = smb2_create_reparse_symlink,
        .open = smb2_open_file,
        .set_fid = smb2_set_fid,
        .close = smb2_close_file,
@@ -5568,6 +5553,7 @@ struct smb_version_operations smb311_operations = {
        .parse_reparse_point = smb2_parse_reparse_point,
        .query_mf_symlink = smb3_query_mf_symlink,
        .create_mf_symlink = smb3_create_mf_symlink,
+       .create_reparse_symlink = smb2_create_reparse_symlink,
        .open = smb2_open_file,
        .set_fid = smb2_set_fid,
        .close = smb2_close_file,