Merge tag 'nfs-for-4.17-1' of git://git.linux-nfs.org/projects/anna/linux-nfs
[linux-block.git] / fs / nfs / nfs4xdr.c
index b993ad282de2002579255914d8bca14698f16854..9b73920323211351f14752f3d6c92e627c6fa9ed 100644 (file)
@@ -98,6 +98,7 @@ static int nfs4_stat_to_errno(int);
                                ((3+NFS4_FHSIZE) >> 2))
 #define nfs4_fattr_bitmap_maxsz 4
 #define encode_getattr_maxsz    (op_encode_hdr_maxsz + nfs4_fattr_bitmap_maxsz)
+#define nfstime4_maxsz         (3)
 #define nfs4_name_maxsz                (1 + ((3 + NFS4_MAXNAMLEN) >> 2))
 #define nfs4_path_maxsz                (1 + ((3 + NFS4_MAXPATHLEN) >> 2))
 #define nfs4_owner_maxsz       (1 + XDR_QUADLEN(IDMAP_NAMESZ))
@@ -112,7 +113,8 @@ static int nfs4_stat_to_errno(int);
 #define decode_mdsthreshold_maxsz (1 + 1 + nfs4_fattr_bitmap_maxsz + 1 + 8)
 /* This is based on getfattr, which uses the most attributes: */
 #define nfs4_fattr_value_maxsz (1 + (1 + 2 + 2 + 4 + 2 + 1 + 1 + 2 + 2 + \
-                               3 + 3 + 3 + nfs4_owner_maxsz + \
+                               3*nfstime4_maxsz + \
+                               nfs4_owner_maxsz + \
                                nfs4_group_maxsz + nfs4_label_maxsz + \
                                 decode_mdsthreshold_maxsz))
 #define nfs4_fattr_maxsz       (nfs4_fattr_bitmap_maxsz + \
@@ -123,7 +125,8 @@ static int nfs4_stat_to_errno(int);
                                nfs4_owner_maxsz + \
                                nfs4_group_maxsz + \
                                nfs4_label_maxsz + \
-                               4 + 4)
+                               1 + nfstime4_maxsz + \
+                               1 + nfstime4_maxsz)
 #define encode_savefh_maxsz     (op_encode_hdr_maxsz)
 #define decode_savefh_maxsz     (op_decode_hdr_maxsz)
 #define encode_restorefh_maxsz  (op_encode_hdr_maxsz)
@@ -957,6 +960,35 @@ static void encode_uint64(struct xdr_stream *xdr, u64 n)
        WARN_ON_ONCE(xdr_stream_encode_u64(xdr, n) < 0);
 }
 
+static ssize_t xdr_encode_bitmap4(struct xdr_stream *xdr,
+               const __u32 *bitmap, size_t len)
+{
+       ssize_t ret;
+
+       /* Trim empty words */
+       while (len > 0 && bitmap[len-1] == 0)
+               len--;
+       ret = xdr_stream_encode_uint32_array(xdr, bitmap, len);
+       if (WARN_ON_ONCE(ret < 0))
+               return ret;
+       return len;
+}
+
+static size_t mask_bitmap4(const __u32 *bitmap, const __u32 *mask,
+               __u32 *res, size_t len)
+{
+       size_t i;
+       __u32 tmp;
+
+       while (len > 0 && (bitmap[len-1] == 0 || mask[len-1] == 0))
+               len--;
+       for (i = len; i-- > 0;) {
+               tmp = bitmap[i] & mask[i];
+               res[i] = tmp;
+       }
+       return len;
+}
+
 static void encode_nfs4_seqid(struct xdr_stream *xdr,
                const struct nfs_seqid *seqid)
 {
@@ -1011,6 +1043,14 @@ static void encode_nfs4_verifier(struct xdr_stream *xdr, const nfs4_verifier *ve
        encode_opaque_fixed(xdr, verf->data, NFS4_VERIFIER_SIZE);
 }
 
+static __be32 *
+xdr_encode_nfstime4(__be32 *p, const struct timespec *t)
+{
+       p = xdr_encode_hyper(p, (__s64)t->tv_sec);
+       *p++ = cpu_to_be32(t->tv_nsec);
+       return p;
+}
+
 static void encode_attrs(struct xdr_stream *xdr, const struct iattr *iap,
                                const struct nfs4_label *label,
                                const umode_t *umask,
@@ -1022,9 +1062,7 @@ static void encode_attrs(struct xdr_stream *xdr, const struct iattr *iap,
        int owner_namelen = 0;
        int owner_grouplen = 0;
        __be32 *p;
-       unsigned i;
        uint32_t len = 0;
-       uint32_t bmval_len;
        uint32_t bmval[3] = { 0 };
 
        /*
@@ -1072,7 +1110,7 @@ static void encode_attrs(struct xdr_stream *xdr, const struct iattr *iap,
        if (attrmask[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
                if (iap->ia_valid & ATTR_ATIME_SET) {
                        bmval[1] |= FATTR4_WORD1_TIME_ACCESS_SET;
-                       len += 16;
+                       len += 4 + (nfstime4_maxsz << 2);
                } else if (iap->ia_valid & ATTR_ATIME) {
                        bmval[1] |= FATTR4_WORD1_TIME_ACCESS_SET;
                        len += 4;
@@ -1081,7 +1119,7 @@ static void encode_attrs(struct xdr_stream *xdr, const struct iattr *iap,
        if (attrmask[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
                if (iap->ia_valid & ATTR_MTIME_SET) {
                        bmval[1] |= FATTR4_WORD1_TIME_MODIFY_SET;
-                       len += 16;
+                       len += 4 + (nfstime4_maxsz << 2);
                } else if (iap->ia_valid & ATTR_MTIME) {
                        bmval[1] |= FATTR4_WORD1_TIME_MODIFY_SET;
                        len += 4;
@@ -1093,19 +1131,8 @@ static void encode_attrs(struct xdr_stream *xdr, const struct iattr *iap,
                bmval[2] |= FATTR4_WORD2_SECURITY_LABEL;
        }
 
-       if (bmval[2] != 0)
-               bmval_len = 3;
-       else if (bmval[1] != 0)
-               bmval_len = 2;
-       else
-               bmval_len = 1;
-
-       p = reserve_space(xdr, 4 + (bmval_len << 2) + 4 + len);
-
-       *p++ = cpu_to_be32(bmval_len);
-       for (i = 0; i < bmval_len; i++)
-               *p++ = cpu_to_be32(bmval[i]);
-       *p++ = cpu_to_be32(len);
+       xdr_encode_bitmap4(xdr, bmval, ARRAY_SIZE(bmval));
+       xdr_stream_encode_opaque_inline(xdr, (void **)&p, len);
 
        if (bmval[0] & FATTR4_WORD0_SIZE)
                p = xdr_encode_hyper(p, iap->ia_size);
@@ -1118,16 +1145,14 @@ static void encode_attrs(struct xdr_stream *xdr, const struct iattr *iap,
        if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
                if (iap->ia_valid & ATTR_ATIME_SET) {
                        *p++ = cpu_to_be32(NFS4_SET_TO_CLIENT_TIME);
-                       p = xdr_encode_hyper(p, (s64)iap->ia_atime.tv_sec);
-                       *p++ = cpu_to_be32(iap->ia_atime.tv_nsec);
+                       p = xdr_encode_nfstime4(p, &iap->ia_atime);
                } else
                        *p++ = cpu_to_be32(NFS4_SET_TO_SERVER_TIME);
        }
        if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
                if (iap->ia_valid & ATTR_MTIME_SET) {
                        *p++ = cpu_to_be32(NFS4_SET_TO_CLIENT_TIME);
-                       p = xdr_encode_hyper(p, (s64)iap->ia_mtime.tv_sec);
-                       *p++ = cpu_to_be32(iap->ia_mtime.tv_nsec);
+                       p = xdr_encode_nfstime4(p, &iap->ia_mtime);
                } else
                        *p++ = cpu_to_be32(NFS4_SET_TO_SERVER_TIME);
        }
@@ -1199,85 +1224,45 @@ static void encode_create(struct xdr_stream *xdr, const struct nfs4_create_arg *
                        create->server, create->server->attr_bitmask);
 }
 
-static void encode_getattr_one(struct xdr_stream *xdr, uint32_t bitmap, struct compound_hdr *hdr)
-{
-       __be32 *p;
-
-       encode_op_hdr(xdr, OP_GETATTR, decode_getattr_maxsz, hdr);
-       p = reserve_space(xdr, 8);
-       *p++ = cpu_to_be32(1);
-       *p = cpu_to_be32(bitmap);
-}
-
-static void encode_getattr_two(struct xdr_stream *xdr, uint32_t bm0, uint32_t bm1, struct compound_hdr *hdr)
-{
-       __be32 *p;
-
-       encode_op_hdr(xdr, OP_GETATTR, decode_getattr_maxsz, hdr);
-       p = reserve_space(xdr, 12);
-       *p++ = cpu_to_be32(2);
-       *p++ = cpu_to_be32(bm0);
-       *p = cpu_to_be32(bm1);
-}
-
-static void
-encode_getattr_three(struct xdr_stream *xdr,
-                    uint32_t bm0, uint32_t bm1, uint32_t bm2,
-                    struct compound_hdr *hdr)
+static void encode_getattr(struct xdr_stream *xdr,
+               const __u32 *bitmap, const __u32 *mask, size_t len,
+               struct compound_hdr *hdr)
 {
-       __be32 *p;
+       __u32 masked_bitmap[nfs4_fattr_bitmap_maxsz];
 
        encode_op_hdr(xdr, OP_GETATTR, decode_getattr_maxsz, hdr);
-       if (bm2) {
-               p = reserve_space(xdr, 16);
-               *p++ = cpu_to_be32(3);
-               *p++ = cpu_to_be32(bm0);
-               *p++ = cpu_to_be32(bm1);
-               *p = cpu_to_be32(bm2);
-       } else if (bm1) {
-               p = reserve_space(xdr, 12);
-               *p++ = cpu_to_be32(2);
-               *p++ = cpu_to_be32(bm0);
-               *p = cpu_to_be32(bm1);
-       } else {
-               p = reserve_space(xdr, 8);
-               *p++ = cpu_to_be32(1);
-               *p = cpu_to_be32(bm0);
+       if (mask) {
+               if (WARN_ON_ONCE(len > ARRAY_SIZE(masked_bitmap)))
+                       len = ARRAY_SIZE(masked_bitmap);
+               len = mask_bitmap4(bitmap, mask, masked_bitmap, len);
+               bitmap = masked_bitmap;
        }
+       xdr_encode_bitmap4(xdr, bitmap, len);
 }
 
 static void encode_getfattr(struct xdr_stream *xdr, const u32* bitmask, struct compound_hdr *hdr)
 {
-       encode_getattr_three(xdr, bitmask[0] & nfs4_fattr_bitmap[0],
-                          bitmask[1] & nfs4_fattr_bitmap[1],
-                          bitmask[2] & nfs4_fattr_bitmap[2],
-                          hdr);
+       encode_getattr(xdr, nfs4_fattr_bitmap, bitmask,
+                       ARRAY_SIZE(nfs4_fattr_bitmap), hdr);
 }
 
 static void encode_getfattr_open(struct xdr_stream *xdr, const u32 *bitmask,
                                 const u32 *open_bitmap,
                                 struct compound_hdr *hdr)
 {
-       encode_getattr_three(xdr,
-                            bitmask[0] & open_bitmap[0],
-                            bitmask[1] & open_bitmap[1],
-                            bitmask[2] & open_bitmap[2],
-                            hdr);
+       encode_getattr(xdr, open_bitmap, bitmask, 3, hdr);
 }
 
 static void encode_fsinfo(struct xdr_stream *xdr, const u32* bitmask, struct compound_hdr *hdr)
 {
-       encode_getattr_three(xdr,
-                            bitmask[0] & nfs4_fsinfo_bitmap[0],
-                            bitmask[1] & nfs4_fsinfo_bitmap[1],
-                            bitmask[2] & nfs4_fsinfo_bitmap[2],
-                            hdr);
+       encode_getattr(xdr, nfs4_fsinfo_bitmap, bitmask,
+                       ARRAY_SIZE(nfs4_fsinfo_bitmap), hdr);
 }
 
 static void encode_fs_locations(struct xdr_stream *xdr, const u32* bitmask, struct compound_hdr *hdr)
 {
-       encode_getattr_two(xdr, bitmask[0] & nfs4_fs_locations_bitmap[0],
-                          bitmask[1] & nfs4_fs_locations_bitmap[1], hdr);
+       encode_getattr(xdr, nfs4_fs_locations_bitmap, bitmask,
+                       ARRAY_SIZE(nfs4_fs_locations_bitmap), hdr);
 }
 
 static void encode_getfh(struct xdr_stream *xdr, struct compound_hdr *hdr)
@@ -2116,7 +2101,8 @@ static void nfs4_xdr_enc_access(struct rpc_rqst *req, struct xdr_stream *xdr,
        encode_sequence(xdr, &args->seq_args, &hdr);
        encode_putfh(xdr, args->fh, &hdr);
        encode_access(xdr, args->access, &hdr);
-       encode_getfattr(xdr, args->bitmask, &hdr);
+       if (args->bitmask)
+               encode_getfattr(xdr, args->bitmask, &hdr);
        encode_nops(&hdr);
 }
 
@@ -2558,13 +2544,17 @@ static void nfs4_xdr_enc_getacl(struct rpc_rqst *req, struct xdr_stream *xdr,
        struct compound_hdr hdr = {
                .minorversion = nfs4_xdr_minorversion(&args->seq_args),
        };
+       const __u32 nfs4_acl_bitmap[1] = {
+               [0] = FATTR4_WORD0_ACL,
+       };
        uint32_t replen;
 
        encode_compound_hdr(xdr, req, &hdr);
        encode_sequence(xdr, &args->seq_args, &hdr);
        encode_putfh(xdr, args->fh, &hdr);
        replen = hdr.replen + op_decode_hdr_maxsz;
-       encode_getattr_two(xdr, FATTR4_WORD0_ACL, 0, &hdr);
+       encode_getattr(xdr, nfs4_acl_bitmap, NULL,
+                       ARRAY_SIZE(nfs4_acl_bitmap), &hdr);
 
        xdr_inline_pages(&req->rq_rcv_buf, replen << 2,
                args->acl_pages, 0, args->acl_len);
@@ -2643,8 +2633,8 @@ static void nfs4_xdr_enc_pathconf(struct rpc_rqst *req, struct xdr_stream *xdr,
        encode_compound_hdr(xdr, req, &hdr);
        encode_sequence(xdr, &args->seq_args, &hdr);
        encode_putfh(xdr, args->fh, &hdr);
-       encode_getattr_one(xdr, args->bitmask[0] & nfs4_pathconf_bitmap[0],
-                          &hdr);
+       encode_getattr(xdr, nfs4_pathconf_bitmap, args->bitmask,
+                       ARRAY_SIZE(nfs4_pathconf_bitmap), &hdr);
        encode_nops(&hdr);
 }
 
@@ -2662,8 +2652,8 @@ static void nfs4_xdr_enc_statfs(struct rpc_rqst *req, struct xdr_stream *xdr,
        encode_compound_hdr(xdr, req, &hdr);
        encode_sequence(xdr, &args->seq_args, &hdr);
        encode_putfh(xdr, args->fh, &hdr);
-       encode_getattr_two(xdr, args->bitmask[0] & nfs4_statfs_bitmap[0],
-                          args->bitmask[1] & nfs4_statfs_bitmap[1], &hdr);
+       encode_getattr(xdr, nfs4_statfs_bitmap, args->bitmask,
+                       ARRAY_SIZE(nfs4_statfs_bitmap), &hdr);
        encode_nops(&hdr);
 }
 
@@ -2683,7 +2673,7 @@ static void nfs4_xdr_enc_server_caps(struct rpc_rqst *req,
        encode_compound_hdr(xdr, req, &hdr);
        encode_sequence(xdr, &args->seq_args, &hdr);
        encode_putfh(xdr, args->fhandle, &hdr);
-       encode_getattr_three(xdr, bitmask[0], bitmask[1], bitmask[2], &hdr);
+       encode_getattr(xdr, bitmask, NULL, 3, &hdr);
        encode_nops(&hdr);
 }
 
@@ -3217,34 +3207,27 @@ static int decode_ace(struct xdr_stream *xdr, void *ace)
        return -EIO;
 }
 
-static int decode_attr_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
+static ssize_t
+decode_bitmap4(struct xdr_stream *xdr, uint32_t *bitmap, size_t sz)
 {
-       uint32_t bmlen;
-       __be32 *p;
-
-       p = xdr_inline_decode(xdr, 4);
-       if (unlikely(!p))
-               goto out_overflow;
-       bmlen = be32_to_cpup(p);
+       ssize_t ret;
 
-       bitmap[0] = bitmap[1] = bitmap[2] = 0;
-       p = xdr_inline_decode(xdr, (bmlen << 2));
-       if (unlikely(!p))
-               goto out_overflow;
-       if (bmlen > 0) {
-               bitmap[0] = be32_to_cpup(p++);
-               if (bmlen > 1) {
-                       bitmap[1] = be32_to_cpup(p++);
-                       if (bmlen > 2)
-                               bitmap[2] = be32_to_cpup(p);
-               }
-       }
-       return 0;
-out_overflow:
+       ret = xdr_stream_decode_uint32_array(xdr, bitmap, sz);
+       if (likely(ret >= 0))
+               return ret;
+       if (ret == -EMSGSIZE)
+               return sz;
        print_overflow_msg(__func__, xdr);
        return -EIO;
 }
 
+static int decode_attr_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
+{
+       ssize_t ret;
+       ret = decode_bitmap4(xdr, bitmap, 3);
+       return ret < 0 ? ret : 0;
+}
+
 static int decode_attr_length(struct xdr_stream *xdr, uint32_t *attrlen, unsigned int *savep)
 {
        __be32 *p;
@@ -3980,7 +3963,7 @@ static int decode_attr_owner(struct xdr_stream *xdr, uint32_t *bitmap,
        bitmap[1] &= ~FATTR4_WORD1_OWNER;
 
        if (owner_name != NULL) {
-               len = decode_nfs4_string(xdr, owner_name, GFP_NOWAIT);
+               len = decode_nfs4_string(xdr, owner_name, GFP_NOIO);
                if (len <= 0)
                        goto out;
                dprintk("%s: name=%s\n", __func__, owner_name->data);
@@ -4015,7 +3998,7 @@ static int decode_attr_group(struct xdr_stream *xdr, uint32_t *bitmap,
        bitmap[1] &= ~FATTR4_WORD1_OWNER_GROUP;
 
        if (group_name != NULL) {
-               len = decode_nfs4_string(xdr, group_name, GFP_NOWAIT);
+               len = decode_nfs4_string(xdr, group_name, GFP_NOIO);
                if (len <= 0)
                        goto out;
                dprintk("%s: name=%s\n", __func__, group_name->data);
@@ -4155,19 +4138,25 @@ out_overflow:
        return -EIO;
 }
 
+static __be32 *
+xdr_decode_nfstime4(__be32 *p, struct timespec *t)
+{
+       __u64 sec;
+
+       p = xdr_decode_hyper(p, &sec);
+       t-> tv_sec = (time_t)sec;
+       t->tv_nsec = be32_to_cpup(p++);
+       return p;
+}
+
 static int decode_attr_time(struct xdr_stream *xdr, struct timespec *time)
 {
        __be32 *p;
-       uint64_t sec;
-       uint32_t nsec;
 
-       p = xdr_inline_decode(xdr, 12);
+       p = xdr_inline_decode(xdr, nfstime4_maxsz << 2);
        if (unlikely(!p))
                goto out_overflow;
-       p = xdr_decode_hyper(p, &sec);
-       nsec = be32_to_cpup(p);
-       time->tv_sec = (time_t)sec;
-       time->tv_nsec = (long)nsec;
+       xdr_decode_nfstime4(p, time);
        return 0;
 out_overflow:
        print_overflow_msg(__func__, xdr);
@@ -5470,21 +5459,13 @@ decode_savefh(struct xdr_stream *xdr)
 
 static int decode_setattr(struct xdr_stream *xdr)
 {
-       __be32 *p;
-       uint32_t bmlen;
        int status;
 
        status = decode_op_hdr(xdr, OP_SETATTR);
        if (status)
                return status;
-       p = xdr_inline_decode(xdr, 4);
-       if (unlikely(!p))
-               goto out_overflow;
-       bmlen = be32_to_cpup(p);
-       p = xdr_inline_decode(xdr, bmlen << 2);
-       if (likely(p))
+       if (decode_bitmap4(xdr, NULL, 0) >= 0)
                return 0;
-out_overflow:
        print_overflow_msg(__func__, xdr);
        return -EIO;
 }
@@ -6255,7 +6236,8 @@ static int nfs4_xdr_dec_access(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
        status = decode_access(xdr, &res->supported, &res->access);
        if (status != 0)
                goto out;
-       decode_getfattr(xdr, res->fattr, res->server);
+       if (res->fattr)
+               decode_getfattr(xdr, res->fattr, res->server);
 out:
        return status;
 }
@@ -7535,6 +7517,7 @@ int nfs4_decode_dirent(struct xdr_stream *xdr, struct nfs_entry *entry,
        unsigned int savep;
        uint32_t bitmap[3] = {0};
        uint32_t len;
+       uint64_t new_cookie;
        __be32 *p = xdr_inline_decode(xdr, 4);
        if (unlikely(!p))
                goto out_overflow;
@@ -7551,8 +7534,7 @@ int nfs4_decode_dirent(struct xdr_stream *xdr, struct nfs_entry *entry,
        p = xdr_inline_decode(xdr, 12);
        if (unlikely(!p))
                goto out_overflow;
-       entry->prev_cookie = entry->cookie;
-       p = xdr_decode_hyper(p, &entry->cookie);
+       p = xdr_decode_hyper(p, &new_cookie);
        entry->len = be32_to_cpup(p);
 
        p = xdr_inline_decode(xdr, entry->len);
@@ -7586,6 +7568,9 @@ int nfs4_decode_dirent(struct xdr_stream *xdr, struct nfs_entry *entry,
        if (entry->fattr->valid & NFS_ATTR_FATTR_TYPE)
                entry->d_type = nfs_umode_to_dtype(entry->fattr->mode);
 
+       entry->prev_cookie = entry->cookie;
+       entry->cookie = new_cookie;
+
        return 0;
 
 out_overflow: