]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
nfs: Add ALLOCATE support
authorAnna Schumaker <Anna.Schumaker@netapp.com>
Tue, 25 Nov 2014 18:18:15 +0000 (13:18 -0500)
committerTrond Myklebust <trond.myklebust@primarydata.com>
Tue, 25 Nov 2014 21:38:32 +0000 (16:38 -0500)
This patch adds support for using the NFS v4.2 operation ALLOCATE to
preallocate data in a file.

Signed-off-by: Anna Schumaker <Anna.Schumaker@Netapp.com>
Signed-off-by: Trond Myklebust <trond.myklebust@primarydata.com>
fs/nfs/inode.c
fs/nfs/nfs42.h
fs/nfs/nfs42proc.c
fs/nfs/nfs42xdr.c
fs/nfs/nfs4_fs.h
fs/nfs/nfs4file.c
fs/nfs/nfs4proc.c
fs/nfs/nfs4xdr.c
include/linux/nfs4.h
include/linux/nfs_fs_sb.h
include/linux/nfs_xdr.h

index 2b48ce58a584ef0a3afa2ad218b4d9f4d2a84ca8..4bffe637ea3255fd2e1e6812556e3907791f4152 100644 (file)
@@ -192,6 +192,7 @@ void nfs_zap_caches(struct inode *inode)
        nfs_zap_caches_locked(inode);
        spin_unlock(&inode->i_lock);
 }
+EXPORT_SYMBOL_GPL(nfs_zap_caches);
 
 void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
 {
index d10333a197bf80721e40dd6a46f1ebbde4c774df..42656a9cf48566cb3139f9408dce93bf35f1dd03 100644 (file)
@@ -6,6 +6,7 @@
 #define __LINUX_FS_NFS_NFS4_2_H
 
 /* nfs4.2proc.c */
+int nfs42_proc_allocate(struct file *, loff_t, loff_t);
 loff_t nfs42_proc_llseek(struct file *, loff_t, int);
 
 /* nfs4.2xdr.h */
index 056f5aafecc1dbd4c14949dc38b5baa1e0f49f3d..581f34e9d8a01ea5ff6f6b652bd940ea9f24f5e0 100644 (file)
@@ -32,6 +32,64 @@ static int nfs42_set_rw_stateid(nfs4_stateid *dst, struct file *file,
        return ret;
 }
 
+static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
+                                loff_t offset, loff_t len)
+{
+       struct inode *inode = file_inode(filep);
+       struct nfs42_falloc_args args = {
+               .falloc_fh      = NFS_FH(inode),
+               .falloc_offset  = offset,
+               .falloc_length  = len,
+       };
+       struct nfs42_falloc_res res;
+       struct nfs_server *server = NFS_SERVER(inode);
+       int status;
+
+       msg->rpc_argp = &args;
+       msg->rpc_resp = &res;
+
+       status = nfs42_set_rw_stateid(&args.falloc_stateid, filep, FMODE_WRITE);
+       if (status)
+               return status;
+
+       return nfs4_call_sync(server->client, server, msg,
+                             &args.seq_args, &res.seq_res, 0);
+}
+
+static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
+                               loff_t offset, loff_t len)
+{
+       struct nfs_server *server = NFS_SERVER(file_inode(filep));
+       struct nfs4_exception exception = { };
+       int err;
+
+       do {
+               err = _nfs42_proc_fallocate(msg, filep, offset, len);
+               if (err == -ENOTSUPP)
+                       return -EOPNOTSUPP;
+               err = nfs4_handle_exception(server, err, &exception);
+       } while (exception.retry);
+
+       return err;
+}
+
+int nfs42_proc_allocate(struct file *filep, loff_t offset, loff_t len)
+{
+       struct rpc_message msg = {
+               .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE],
+       };
+       struct inode *inode = file_inode(filep);
+       int err;
+
+       if (!nfs_server_capable(inode, NFS_CAP_ALLOCATE))
+               return -EOPNOTSUPP;
+
+       err = nfs42_proc_fallocate(&msg, filep, offset, len);
+       if (err == -EOPNOTSUPP)
+               NFS_SERVER(inode)->caps &= ~NFS_CAP_ALLOCATE;
+       return err;
+}
+
 loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence)
 {
        struct inode *inode = file_inode(filep);
index c90469b604b8b0045fa8eaac01a07b0fbb0f9114..4248d034479c992cfa8920dabcf09833dec9d1c1 100644 (file)
@@ -4,6 +4,12 @@
 #ifndef __LINUX_FS_NFS_NFS4_2XDR_H
 #define __LINUX_FS_NFS_NFS4_2XDR_H
 
+#define encode_fallocate_maxsz         (encode_stateid_maxsz + \
+                                        2 /* offset */ + \
+                                        2 /* length */)
+#define encode_allocate_maxsz          (op_encode_hdr_maxsz + \
+                                        encode_fallocate_maxsz)
+#define decode_allocate_maxsz          (op_decode_hdr_maxsz)
 #define encode_seek_maxsz              (op_encode_hdr_maxsz + \
                                         encode_stateid_maxsz + \
                                         2 /* offset */ + \
                                         2 /* offset */ + \
                                         2 /* length */)
 
+#define NFS4_enc_allocate_sz           (compound_encode_hdr_maxsz + \
+                                        encode_putfh_maxsz + \
+                                        encode_allocate_maxsz)
+#define NFS4_dec_allocate_sz           (compound_decode_hdr_maxsz + \
+                                        decode_putfh_maxsz + \
+                                        decode_allocate_maxsz)
 #define NFS4_enc_seek_sz               (compound_encode_hdr_maxsz + \
                                         encode_putfh_maxsz + \
                                         encode_seek_maxsz)
                                         decode_seek_maxsz)
 
 
+static void encode_fallocate(struct xdr_stream *xdr,
+                            struct nfs42_falloc_args *args)
+{
+       encode_nfs4_stateid(xdr, &args->falloc_stateid);
+       encode_uint64(xdr, args->falloc_offset);
+       encode_uint64(xdr, args->falloc_length);
+}
+
+static void encode_allocate(struct xdr_stream *xdr,
+                           struct nfs42_falloc_args *args,
+                           struct compound_hdr *hdr)
+{
+       encode_op_hdr(xdr, OP_ALLOCATE, decode_allocate_maxsz, hdr);
+       encode_fallocate(xdr, args);
+}
+
 static void encode_seek(struct xdr_stream *xdr,
                        struct nfs42_seek_args *args,
                        struct compound_hdr *hdr)
@@ -32,6 +60,24 @@ static void encode_seek(struct xdr_stream *xdr,
        encode_uint32(xdr, args->sa_what);
 }
 
+/*
+ * Encode ALLOCATE request
+ */
+static void nfs4_xdr_enc_allocate(struct rpc_rqst *req,
+                                 struct xdr_stream *xdr,
+                                 struct nfs42_falloc_args *args)
+{
+       struct compound_hdr hdr = {
+               .minorversion = nfs4_xdr_minorversion(&args->seq_args),
+       };
+
+       encode_compound_hdr(xdr, req, &hdr);
+       encode_sequence(xdr, &args->seq_args, &hdr);
+       encode_putfh(xdr, args->falloc_fh, &hdr);
+       encode_allocate(xdr, args, &hdr);
+       encode_nops(&hdr);
+}
+
 /*
  * Encode SEEK request
  */
@@ -50,6 +96,11 @@ static void nfs4_xdr_enc_seek(struct rpc_rqst *req,
        encode_nops(&hdr);
 }
 
+static int decode_allocate(struct xdr_stream *xdr, struct nfs42_falloc_res *res)
+{
+       return decode_op_hdr(xdr, OP_ALLOCATE);
+}
+
 static int decode_seek(struct xdr_stream *xdr, struct nfs42_seek_res *res)
 {
        int status;
@@ -72,6 +123,30 @@ out_overflow:
        return -EIO;
 }
 
+/*
+ * Decode ALLOCATE request
+ */
+static int nfs4_xdr_dec_allocate(struct rpc_rqst *rqstp,
+                                struct xdr_stream *xdr,
+                                struct nfs42_falloc_res *res)
+{
+       struct compound_hdr hdr;
+       int status;
+
+       status = decode_compound_hdr(xdr, &hdr);
+       if (status)
+               goto out;
+       status = decode_sequence(xdr, &res->seq_res, rqstp);
+       if (status)
+               goto out;
+       status = decode_putfh(xdr);
+       if (status)
+               goto out;
+       status = decode_allocate(xdr, res);
+out:
+       return status;
+}
+
 /*
  * Decode SEEK request
  */
index be6cac37ea1094e1202709a21506609b09e4cea8..a08178764cf96ef1f1b7eadb648956a2a70b3239 100644 (file)
@@ -226,6 +226,7 @@ int nfs4_replace_transport(struct nfs_server *server,
                                const struct nfs4_fs_locations *locations);
 
 /* nfs4proc.c */
+extern int nfs4_handle_exception(struct nfs_server *, int, struct nfs4_exception *);
 extern int nfs4_call_sync(struct rpc_clnt *, struct nfs_server *,
                          struct rpc_message *, struct nfs4_sequence_args *,
                          struct nfs4_sequence_res *, int);
index c51fb4db9bfef6d6d43c11841264b1180be3a039..f78e9fd0735a170884b0053c288a198cb24081b1 100644 (file)
@@ -3,6 +3,8 @@
  *
  *  Copyright (C) 1992  Rick Sladkey
  */
+#include <linux/fs.h>
+#include <linux/falloc.h>
 #include <linux/nfs_fs.h>
 #include "internal.h"
 #include "fscache.h"
@@ -134,6 +136,29 @@ static loff_t nfs4_file_llseek(struct file *filep, loff_t offset, int whence)
                return nfs_file_llseek(filep, offset, whence);
        }
 }
+
+static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t len)
+{
+       struct inode *inode = file_inode(filep);
+       long ret;
+
+       if (!S_ISREG(inode->i_mode))
+               return -EOPNOTSUPP;
+
+       if (mode != 0)
+               return -EOPNOTSUPP;
+
+       ret = inode_newsize_ok(inode, offset + len);
+       if (ret < 0)
+               return ret;
+
+       mutex_lock(&inode->i_mutex);
+       ret = nfs42_proc_allocate(filep, offset, len);
+       mutex_unlock(&inode->i_mutex);
+
+       nfs_zap_caches(inode);
+       return ret;
+}
 #endif /* CONFIG_NFS_V4_2 */
 
 const struct file_operations nfs4_file_operations = {
@@ -155,6 +180,9 @@ const struct file_operations nfs4_file_operations = {
        .flock          = nfs_flock,
        .splice_read    = nfs_file_splice_read,
        .splice_write   = iter_file_splice_write,
+#ifdef CONFIG_NFS_V4_2
+       .fallocate      = nfs42_fallocate,
+#endif /* CONFIG_NFS_V4_2 */
        .check_flags    = nfs_check_flags,
        .setlease       = simple_nosetlease,
 };
index 3138913d1cdf1a92cf37d0ca13f4cd60ea4db940..b1b403b0ca0d6144693ceb948a78813073e4a2e3 100644 (file)
@@ -342,7 +342,7 @@ static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
 /* This is the error handling routine for processes that are allowed
  * to sleep.
  */
-static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
+int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
 {
        struct nfs_client *clp = server->nfs_client;
        struct nfs4_state *state = exception->state;
@@ -8424,6 +8424,7 @@ static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
                | NFS_CAP_POSIX_LOCK
                | NFS_CAP_STATEID_NFSV41
                | NFS_CAP_ATOMIC_OPEN_V1
+               | NFS_CAP_ALLOCATE
                | NFS_CAP_SEEK,
        .init_client = nfs41_init_client,
        .shutdown_client = nfs41_shutdown_client,
index 206c08a60c7fb9cf4a7f5674722fe9e917d4ec5c..0a1484561e4b19cc654cfc55fb870a50a2cced0e 100644 (file)
@@ -7394,6 +7394,7 @@ struct rpc_procinfo       nfs4_procedures[] = {
 #endif /* CONFIG_NFS_V4_1 */
 #ifdef CONFIG_NFS_V4_2
        PROC(SEEK,              enc_seek,               dec_seek),
+       PROC(ALLOCATE,          enc_allocate,           dec_allocate),
 #endif /* CONFIG_NFS_V4_2 */
 };
 
index 356acc2846fd795e31abdca2c2c76172f6ca7297..2b28a21edcd0d8fb7c3a29a5e5a796e61d4f4a90 100644 (file)
@@ -490,6 +490,7 @@ enum {
 
        /* nfs42 */
        NFSPROC4_CLNT_SEEK,
+       NFSPROC4_CLNT_ALLOCATE,
 };
 
 /* nfs41 types */
index a32ba0d7a98fe9eaeb4b95f971c6730bc128b9db..df6ed429b40697a365b34bf89b1288203284806a 100644 (file)
@@ -231,5 +231,6 @@ struct nfs_server {
 #define NFS_CAP_ATOMIC_OPEN_V1 (1U << 17)
 #define NFS_CAP_SECURITY_LABEL (1U << 18)
 #define NFS_CAP_SEEK           (1U << 19)
+#define NFS_CAP_ALLOCATE       (1U << 20)
 
 #endif
index 47ebb4fafd87f7ba0f37b36b22f54952f6f823c8..467c84efb5960f8e79fac918eabc721e5f3722d4 100644 (file)
@@ -1243,6 +1243,20 @@ nfs_free_pnfs_ds_cinfo(struct pnfs_ds_commit_info *cinfo)
 #endif /* CONFIG_NFS_V4_1 */
 
 #ifdef CONFIG_NFS_V4_2
+struct nfs42_falloc_args {
+       struct nfs4_sequence_args       seq_args;
+
+       struct nfs_fh                   *falloc_fh;
+       nfs4_stateid                     falloc_stateid;
+       u64                              falloc_offset;
+       u64                              falloc_length;
+};
+
+struct nfs42_falloc_res {
+       struct nfs4_sequence_res        seq_res;
+       unsigned int                    status;
+};
+
 struct nfs42_seek_args {
        struct nfs4_sequence_args       seq_args;