Skip to contentSkip to conversation

NFS: Add a GETATTR to ALLOCATE and DEALLOCATE calls

PATCH In review Anna Schumaker 2 participants 4 messages Feb 23, 2015 – Feb 24, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 NFS: Add a GETATTR to ALLOCATE and DEALLOCATE calls

Adding a GETATTR lets us update file attributes immediately, rather than invalidating all cached data and updating later on. I use the offset provided to fallocate() to determine what page cache data needs to be trashed.

---
 fs/nfs/inode.c          |  4 ++--
 fs/nfs/nfs42proc.c      | 21 ++++++++++++++++-----
 fs/nfs/nfs42xdr.c       | 20 ++++++++++++++++----
 fs/nfs/nfs4file.c       |  1 -
 include/linux/nfs_fs.h  |  1 +
 include/linux/nfs_xdr.h |  4 ++++
 6 files changed, 39 insertions(+), 12 deletions(-)
fs/nfs/inode.c +2 -2
index 83107be..f67aadb 100644
@@ -192,7 +192,6 @@ 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)
 {
@@ -557,7 +556,7 @@ EXPORT_SYMBOL_GPL(nfs_setattr);
  * corrected to take into account the fact that NFS requires
  * inode->i_size to be updated under the inode->i_lock.
  */
-static int nfs_vmtruncate(struct inode * inode, loff_t offset)
+int nfs_vmtruncate(struct inode * inode, loff_t offset)
 {
 	int err;
 
@@ -576,6 +575,7 @@ static int nfs_vmtruncate(struct inode * inode, loff_t offset)
 out:
 	return err;
 }
+EXPORT_SYMBOL_GPL(nfs_vmtruncate);
 
 /**
  * nfs_setattr_update_inode - Update inode metadata after a setattr call.
fs/nfs/nfs42proc.c +16 -5
index cb17072..407bfc3 100644
@@ -36,24 +36,35 @@ 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 nfs_server *server = NFS_SERVER(inode);
 	struct nfs42_falloc_args args = {
 		.falloc_fh	= NFS_FH(inode),
 		.falloc_offset	= offset,
 		.falloc_length	= len,
+		.falloc_bitmask	= server->attr_bitmask,
 	};
-	struct nfs42_falloc_res res;
-	struct nfs_server *server = NFS_SERVER(inode);
-	int status;
+	struct nfs42_falloc_res res = {
+		.falloc_server	= server,
+	};
+	int status = -ENOMEM;
 
 	msg->rpc_argp = &args;
 	msg->rpc_resp = &res;
 
+	nfs_fattr_init(&res.falloc_fattr);
+
 	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);
+	status = nfs4_call_sync(server->client, server, msg,
+				&args.seq_args, &res.seq_res, 0);
+	if (!status) {
+		nfs_vmtruncate(inode, offset);
+		status = nfs_post_op_update_inode(inode, &res.falloc_fattr);
+	}
+
+	return status;
 }
 
 static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
fs/nfs/nfs42xdr.c +16 -4
index 038a7e1..d3fcc5f 100644
@@ -25,16 +25,20 @@
 
 #define NFS4_enc_allocate_sz		(compound_encode_hdr_maxsz + \
 					 encode_putfh_maxsz + \
-					 encode_allocate_maxsz)
+					 encode_allocate_maxsz + \
+					 encode_getattr_maxsz)
 #define NFS4_dec_allocate_sz		(compound_decode_hdr_maxsz + \
 					 decode_putfh_maxsz + \
-					 decode_allocate_maxsz)
+					 decode_allocate_maxsz + \
+					 decode_getattr_maxsz)
 #define NFS4_enc_deallocate_sz		(compound_encode_hdr_maxsz + \
 					 encode_putfh_maxsz + \
-					 encode_deallocate_maxsz)
+					 encode_deallocate_maxsz + \
+					 encode_getattr_maxsz)
 #define NFS4_dec_deallocate_sz		(compound_decode_hdr_maxsz + \
 					 decode_putfh_maxsz + \
-					 decode_deallocate_maxsz)
+					 decode_deallocate_maxsz + \
+					 decode_getattr_maxsz)
 #define NFS4_enc_seek_sz		(compound_encode_hdr_maxsz + \
 					 encode_putfh_maxsz + \
 					 encode_seek_maxsz)
@@ -92,6 +96,7 @@ static void nfs4_xdr_enc_allocate(struct rpc_rqst *req,
 	encode_sequence(xdr, &args->seq_args, &hdr);
 	encode_putfh(xdr, args->falloc_fh, &hdr);
 	encode_allocate(xdr, args, &hdr);
+	encode_getfattr(xdr, args->falloc_bitmask, &hdr);
 	encode_nops(&hdr);
 }
 
@@ -110,6 +115,7 @@ static void nfs4_xdr_enc_deallocate(struct rpc_rqst *req,
 	encode_sequence(xdr, &args->seq_args, &hdr);
 	encode_putfh(xdr, args->falloc_fh, &hdr);
 	encode_deallocate(xdr, args, &hdr);
+	encode_getfattr(xdr, args->falloc_bitmask, &hdr);
 	encode_nops(&hdr);
 }
 
@@ -183,6 +189,9 @@ static int nfs4_xdr_dec_allocate(struct rpc_rqst *rqstp,
 	if (status)
 		goto out;
 	status = decode_allocate(xdr, res);
+	if (status)
+		goto out;
+	decode_getfattr(xdr, &res->falloc_fattr, res->falloc_server);
 out:
 	return status;
 }
@@ -207,6 +216,9 @@ static int nfs4_xdr_dec_deallocate(struct rpc_rqst *rqstp,
 	if (status)
 		goto out;
 	status = decode_deallocate(xdr, res);
+	if (status)
+		goto out;
+	decode_getfattr(xdr, &res->falloc_fattr, res->falloc_server);
 out:
 	return status;
 }
fs/nfs/nfs4file.c +0 -1
index 8b46389..d09c689 100644
@@ -159,7 +159,6 @@ static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t
 		ret = nfs42_proc_allocate(filep, offset, len);
 	mutex_unlock(&inode->i_mutex);
 
-	nfs_zap_caches(inode);
 	return ret;
 }
 #endif /* CONFIG_NFS_V4_2 */
include/linux/nfs_fs.h +1 -0
index 2f77e0c..fb3dc3a 100644
@@ -337,6 +337,7 @@ static inline int nfs_verify_change_attribute(struct inode *dir, unsigned long c
 extern int nfs_sync_mapping(struct address_space *mapping);
 extern void nfs_zap_mapping(struct inode *inode, struct address_space *mapping);
 extern void nfs_zap_caches(struct inode *);
+extern int nfs_vmtruncate(struct inode *, loff_t);
 extern void nfs_invalidate_atime(struct inode *);
 extern struct inode *nfs_fhget(struct super_block *, struct nfs_fh *,
 				struct nfs_fattr *, struct nfs4_label *);
include/linux/nfs_xdr.h +4 -0
index 4cb3eaa..23aa37a 100644
@@ -1271,11 +1271,15 @@ struct nfs42_falloc_args {
 	nfs4_stateid			 falloc_stateid;
 	u64				 falloc_offset;
 	u64				 falloc_length;
+	const u32			*falloc_bitmask;
 };
 
 struct nfs42_falloc_res {
 	struct nfs4_sequence_res	seq_res;
 	unsigned int			status;
+
+	struct nfs_fattr		 falloc_fattr;
+	const struct nfs_server		*falloc_server;
 };
 
 struct nfs42_seek_args {
Signature · 1 line
2.3.0
#1 lore raw
Quoted 49 lines · Anna Schumaker

Adding a GETATTR lets us update file attributes immediately, rather than invalidating all cached data and updating later on. I use the offset provided to fallocate() to determine what page cache data needs to be trashed.

---
 fs/nfs/inode.c          |  4 ++--
 fs/nfs/nfs42proc.c      | 21 ++++++++++++++++-----
 fs/nfs/nfs42xdr.c       | 20 ++++++++++++++++----
 fs/nfs/nfs4file.c       |  1 -
 include/linux/nfs_fs.h  |  1 +
 include/linux/nfs_xdr.h |  4 ++++
 6 files changed, 39 insertions(+), 12 deletions(-)
fs/nfs/inode.c +2 -2
index 83107be..f67aadb 100644
@@ -192,7 +192,6 @@ 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)
 {
@@ -557,7 +556,7 @@ EXPORT_SYMBOL_GPL(nfs_setattr);
  * corrected to take into account the fact that NFS requires
  * inode->i_size to be updated under the inode->i_lock.
  */
-static int nfs_vmtruncate(struct inode * inode, loff_t offset)
+int nfs_vmtruncate(struct inode * inode, loff_t offset)
 {
        int err;
 
@@ -576,6 +575,7 @@ static int nfs_vmtruncate(struct inode * inode, loff_t offset)
 out:
        return err;
 }
+EXPORT_SYMBOL_GPL(nfs_vmtruncate);
 
 /**
  * nfs_setattr_update_inode - Update inode metadata after a setattr call.
fs/nfs/nfs42proc.c +0 -0
index cb17072..407bfc3 100644

fs/nfs/nfs42proc.c static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep, · line 44

Quoted 9 lines
fs/nfs/nfs42proc.c +2 -0
@@ -36,24 +36,35 @@ 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 nfs_server *server = NFS_SERVER(inode);
        struct nfs42_falloc_args args = {
                .falloc_fh      = NFS_FH(inode),
                .falloc_offset  = offset,
                .falloc_length  = len,
+               .falloc_bitmask = server->attr_bitmask,

Why do a full getattr? Won't the cache consistency bitmask suffice? All you want is to get the change attribute, and possibly the new file size so that you can verify it is correct.

Quoted 14 lines
fs/nfs/nfs42proc.c +6 -3
        };
-       struct nfs42_falloc_res res;
-       struct nfs_server *server = NFS_SERVER(inode);
-       int status;
+       struct nfs42_falloc_res res = {
+               .falloc_server  = server,
+       };
+       int status = -ENOMEM;
 
        msg->rpc_argp = &args;
        msg->rpc_resp = &res;
 
+       nfs_fattr_init(&res.falloc_fattr);
+

fs/nfs/nfs42proc.c static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep, · line 63

Quoted 10 lines
fs/nfs/nfs42proc.c +4 -2
        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);
+       status = nfs4_call_sync(server->client, server, msg,
+                               &args.seq_args, &res.seq_res, 0);
+       if (!status) {
+               nfs_vmtruncate(inode, offset);

Do you need the vmtruncate? I thought ALLOCATE could only extend the file, in which case calling truncate_pagecache() seems like overkill (and maybe even racy).

As for DEALLOCATE, you're punching a hole, so you really want to call truncate_inode_pages_range().

IOW: It looks to me as if this post-processing really wants to be done in nfs42_proc_deallocate() and nfs42_proc_allocate().

Quoted 107 lines
fs/nfs/nfs42proc.c +4 -0
+               status = nfs_post_op_update_inode(inode, &res.falloc_fattr);
+       }
+
+       return status;
 }
 
 static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
fs/nfs/nfs42xdr.c +16 -4
index 038a7e1..d3fcc5f 100644
@@ -25,16 +25,20 @@
 
 #define NFS4_enc_allocate_sz           (compound_encode_hdr_maxsz + \
                                         encode_putfh_maxsz + \
-                                        encode_allocate_maxsz)
+                                        encode_allocate_maxsz + \
+                                        encode_getattr_maxsz)
 #define NFS4_dec_allocate_sz           (compound_decode_hdr_maxsz + \
                                         decode_putfh_maxsz + \
-                                        decode_allocate_maxsz)
+                                        decode_allocate_maxsz + \
+                                        decode_getattr_maxsz)
 #define NFS4_enc_deallocate_sz         (compound_encode_hdr_maxsz + \
                                         encode_putfh_maxsz + \
-                                        encode_deallocate_maxsz)
+                                        encode_deallocate_maxsz + \
+                                        encode_getattr_maxsz)
 #define NFS4_dec_deallocate_sz         (compound_decode_hdr_maxsz + \
                                         decode_putfh_maxsz + \
-                                        decode_deallocate_maxsz)
+                                        decode_deallocate_maxsz + \
+                                        decode_getattr_maxsz)
 #define NFS4_enc_seek_sz               (compound_encode_hdr_maxsz + \
                                         encode_putfh_maxsz + \
                                         encode_seek_maxsz)
@@ -92,6 +96,7 @@ static void nfs4_xdr_enc_allocate(struct rpc_rqst *req,
        encode_sequence(xdr, &args->seq_args, &hdr);
        encode_putfh(xdr, args->falloc_fh, &hdr);
        encode_allocate(xdr, args, &hdr);
+       encode_getfattr(xdr, args->falloc_bitmask, &hdr);
        encode_nops(&hdr);
 }
 
@@ -110,6 +115,7 @@ static void nfs4_xdr_enc_deallocate(struct rpc_rqst *req,
        encode_sequence(xdr, &args->seq_args, &hdr);
        encode_putfh(xdr, args->falloc_fh, &hdr);
        encode_deallocate(xdr, args, &hdr);
+       encode_getfattr(xdr, args->falloc_bitmask, &hdr);
        encode_nops(&hdr);
 }
 
@@ -183,6 +189,9 @@ static int nfs4_xdr_dec_allocate(struct rpc_rqst *rqstp,
        if (status)
                goto out;
        status = decode_allocate(xdr, res);
+       if (status)
+               goto out;
+       decode_getfattr(xdr, &res->falloc_fattr, res->falloc_server);
 out:
        return status;
 }
@@ -207,6 +216,9 @@ static int nfs4_xdr_dec_deallocate(struct rpc_rqst *rqstp,
        if (status)
                goto out;
        status = decode_deallocate(xdr, res);
+       if (status)
+               goto out;
+       decode_getfattr(xdr, &res->falloc_fattr, res->falloc_server);
 out:
        return status;
 }
fs/nfs/nfs4file.c +0 -1
index 8b46389..d09c689 100644
@@ -159,7 +159,6 @@ static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t
                ret = nfs42_proc_allocate(filep, offset, len);
        mutex_unlock(&inode->i_mutex);
 
-       nfs_zap_caches(inode);
        return ret;
 }
 #endif /* CONFIG_NFS_V4_2 */
include/linux/nfs_fs.h +1 -0
index 2f77e0c..fb3dc3a 100644
@@ -337,6 +337,7 @@ static inline int nfs_verify_change_attribute(struct inode *dir, unsigned long c
 extern int nfs_sync_mapping(struct address_space *mapping);
 extern void nfs_zap_mapping(struct inode *inode, struct address_space *mapping);
 extern void nfs_zap_caches(struct inode *);
+extern int nfs_vmtruncate(struct inode *, loff_t);
 extern void nfs_invalidate_atime(struct inode *);
 extern struct inode *nfs_fhget(struct super_block *, struct nfs_fh *,
                                struct nfs_fattr *, struct nfs4_label *);
include/linux/nfs_xdr.h +1 -0
index 4cb3eaa..23aa37a 100644
@@ -1271,11 +1271,15 @@ struct nfs42_falloc_args {
        nfs4_stateid                     falloc_stateid;
        u64                              falloc_offset;
        u64                              falloc_length;
+       const u32                       *falloc_bitmask;
 };

include/linux/nfs_xdr.h struct nfs42_falloc_args { · line 1285

Quoted 10 lines
include/linux/nfs_xdr.h +3 -0
 
 struct nfs42_falloc_res {
        struct nfs4_sequence_res        seq_res;
        unsigned int                    status;
+
+       struct nfs_fattr                 falloc_fattr;
+       const struct nfs_server         *falloc_server;
 };
 
 struct nfs42_seek_args {

Please could you allocate the struct nfs_fattr dynamically using nfs_alloc_fattr(). Those are deemed too large to fit comfortably on the stack.

Signature · 3 lines
Trond Myklebust
Linux NFS client maintainer, PrimaryData
[email protected]
#2 lore raw

Hi Trond,

Thanks for the review!

Quoted 90 lines · Trond Myklebust
Quoted 49 lines · Anna Schumaker

Adding a GETATTR lets us update file attributes immediately, rather than invalidating all cached data and updating later on. I use the offset provided to fallocate() to determine what page cache data needs to be trashed.

---
 fs/nfs/inode.c          |  4 ++--
 fs/nfs/nfs42proc.c      | 21 ++++++++++++++++-----
 fs/nfs/nfs42xdr.c       | 20 ++++++++++++++++----
 fs/nfs/nfs4file.c       |  1 -
 include/linux/nfs_fs.h  |  1 +
 include/linux/nfs_xdr.h |  4 ++++
 6 files changed, 39 insertions(+), 12 deletions(-)
fs/nfs/inode.c +2 -2
index 83107be..f67aadb 100644
@@ -192,7 +192,6 @@ 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)
 {
@@ -557,7 +556,7 @@ EXPORT_SYMBOL_GPL(nfs_setattr);
  * corrected to take into account the fact that NFS requires
  * inode->i_size to be updated under the inode->i_lock.
  */
-static int nfs_vmtruncate(struct inode * inode, loff_t offset)
+int nfs_vmtruncate(struct inode * inode, loff_t offset)
 {
        int err;
 
@@ -576,6 +575,7 @@ static int nfs_vmtruncate(struct inode * inode, loff_t offset)
 out:
        return err;
 }
+EXPORT_SYMBOL_GPL(nfs_vmtruncate);
 
 /**
  * nfs_setattr_update_inode - Update inode metadata after a setattr call.
fs/nfs/nfs42proc.c +0 -0
index cb17072..407bfc3 100644

fs/nfs/nfs42proc.c static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep, · line 44

Quoted 9 lines
fs/nfs/nfs42proc.c +2 -0
@@ -36,24 +36,35 @@ 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 nfs_server *server = NFS_SERVER(inode);
        struct nfs42_falloc_args args = {
                .falloc_fh      = NFS_FH(inode),
                .falloc_offset  = offset,
                .falloc_length  = len,
+               .falloc_bitmask = server->attr_bitmask,

Why do a full getattr? Won't the cache consistency bitmask suffice? All you want is to get the change attribute, and possibly the new file size so that you can verify it is correct.

Quoted 14 lines
fs/nfs/nfs42proc.c +6 -3
        };
-       struct nfs42_falloc_res res;
-       struct nfs_server *server = NFS_SERVER(inode);
-       int status;
+       struct nfs42_falloc_res res = {
+               .falloc_server  = server,
+       };
+       int status = -ENOMEM;
 
        msg->rpc_argp = &args;
        msg->rpc_resp = &res;
 
+       nfs_fattr_init(&res.falloc_fattr);
+

fs/nfs/nfs42proc.c static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep, · line 63

Quoted 10 lines
fs/nfs/nfs42proc.c +4 -2
        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);
+       status = nfs4_call_sync(server->client, server, msg,
+                               &args.seq_args, &res.seq_res, 0);
+       if (!status) {
+               nfs_vmtruncate(inode, offset);
Quoted 6 lines

Do you need the vmtruncate? I thought ALLOCATE could only extend the file, in which case calling truncate_pagecache() seems like overkill (and maybe even racy).

As for DEALLOCATE, you're punching a hole, so you really want to call truncate_inode_pages_range().

It looks like truncate_inode_pages_range() works just as well, so I'll switch over to that. ALLOCATE needs to mark the entire range as "unallocated", so calling this function for both operations is correct.

I'll send a v2 soon!

Anna

Quoted 126 lines

IOW: It looks to me as if this post-processing really wants to be done in nfs42_proc_deallocate() and nfs42_proc_allocate().

Quoted 107 lines
fs/nfs/nfs42proc.c +4 -0
+               status = nfs_post_op_update_inode(inode, &res.falloc_fattr);
+       }
+
+       return status;
 }
 
 static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
fs/nfs/nfs42xdr.c +16 -4
index 038a7e1..d3fcc5f 100644
@@ -25,16 +25,20 @@
 
 #define NFS4_enc_allocate_sz           (compound_encode_hdr_maxsz + \
                                         encode_putfh_maxsz + \
-                                        encode_allocate_maxsz)
+                                        encode_allocate_maxsz + \
+                                        encode_getattr_maxsz)
 #define NFS4_dec_allocate_sz           (compound_decode_hdr_maxsz + \
                                         decode_putfh_maxsz + \
-                                        decode_allocate_maxsz)
+                                        decode_allocate_maxsz + \
+                                        decode_getattr_maxsz)
 #define NFS4_enc_deallocate_sz         (compound_encode_hdr_maxsz + \
                                         encode_putfh_maxsz + \
-                                        encode_deallocate_maxsz)
+                                        encode_deallocate_maxsz + \
+                                        encode_getattr_maxsz)
 #define NFS4_dec_deallocate_sz         (compound_decode_hdr_maxsz + \
                                         decode_putfh_maxsz + \
-                                        decode_deallocate_maxsz)
+                                        decode_deallocate_maxsz + \
+                                        decode_getattr_maxsz)
 #define NFS4_enc_seek_sz               (compound_encode_hdr_maxsz + \
                                         encode_putfh_maxsz + \
                                         encode_seek_maxsz)
@@ -92,6 +96,7 @@ static void nfs4_xdr_enc_allocate(struct rpc_rqst *req,
        encode_sequence(xdr, &args->seq_args, &hdr);
        encode_putfh(xdr, args->falloc_fh, &hdr);
        encode_allocate(xdr, args, &hdr);
+       encode_getfattr(xdr, args->falloc_bitmask, &hdr);
        encode_nops(&hdr);
 }
 
@@ -110,6 +115,7 @@ static void nfs4_xdr_enc_deallocate(struct rpc_rqst *req,
        encode_sequence(xdr, &args->seq_args, &hdr);
        encode_putfh(xdr, args->falloc_fh, &hdr);
        encode_deallocate(xdr, args, &hdr);
+       encode_getfattr(xdr, args->falloc_bitmask, &hdr);
        encode_nops(&hdr);
 }
 
@@ -183,6 +189,9 @@ static int nfs4_xdr_dec_allocate(struct rpc_rqst *rqstp,
        if (status)
                goto out;
        status = decode_allocate(xdr, res);
+       if (status)
+               goto out;
+       decode_getfattr(xdr, &res->falloc_fattr, res->falloc_server);
 out:
        return status;
 }
@@ -207,6 +216,9 @@ static int nfs4_xdr_dec_deallocate(struct rpc_rqst *rqstp,
        if (status)
                goto out;
        status = decode_deallocate(xdr, res);
+       if (status)
+               goto out;
+       decode_getfattr(xdr, &res->falloc_fattr, res->falloc_server);
 out:
        return status;
 }
fs/nfs/nfs4file.c +0 -1
index 8b46389..d09c689 100644
@@ -159,7 +159,6 @@ static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t
                ret = nfs42_proc_allocate(filep, offset, len);
        mutex_unlock(&inode->i_mutex);
 
-       nfs_zap_caches(inode);
        return ret;
 }
 #endif /* CONFIG_NFS_V4_2 */
include/linux/nfs_fs.h +1 -0
index 2f77e0c..fb3dc3a 100644
@@ -337,6 +337,7 @@ static inline int nfs_verify_change_attribute(struct inode *dir, unsigned long c
 extern int nfs_sync_mapping(struct address_space *mapping);
 extern void nfs_zap_mapping(struct inode *inode, struct address_space *mapping);
 extern void nfs_zap_caches(struct inode *);
+extern int nfs_vmtruncate(struct inode *, loff_t);
 extern void nfs_invalidate_atime(struct inode *);
 extern struct inode *nfs_fhget(struct super_block *, struct nfs_fh *,
                                struct nfs_fattr *, struct nfs4_label *);
include/linux/nfs_xdr.h +1 -0
index 4cb3eaa..23aa37a 100644
@@ -1271,11 +1271,15 @@ struct nfs42_falloc_args {
        nfs4_stateid                     falloc_stateid;
        u64                              falloc_offset;
        u64                              falloc_length;
+       const u32                       *falloc_bitmask;
 };

include/linux/nfs_xdr.h struct nfs42_falloc_args { · line 1285

Quoted 10 lines
include/linux/nfs_xdr.h +3 -0
 
 struct nfs42_falloc_res {
        struct nfs4_sequence_res        seq_res;
        unsigned int                    status;
+
+       struct nfs_fattr                 falloc_fattr;
+       const struct nfs_server         *falloc_server;
 };
 
 struct nfs42_seek_args {

Please could you allocate the struct nfs_fattr dynamically using nfs_alloc_fattr(). Those are deemed too large to fit comfortably on the stack.

#3 lore raw
Quoted 95 lines · Anna Schumaker

Hi Trond,

Thanks for the review!

Quoted 90 lines · Trond Myklebust
Quoted 49 lines · Anna Schumaker

Adding a GETATTR lets us update file attributes immediately, rather than invalidating all cached data and updating later on. I use the offset provided to fallocate() to determine what page cache data needs to be trashed.

---
 fs/nfs/inode.c          |  4 ++--
 fs/nfs/nfs42proc.c      | 21 ++++++++++++++++-----
 fs/nfs/nfs42xdr.c       | 20 ++++++++++++++++----
 fs/nfs/nfs4file.c       |  1 -
 include/linux/nfs_fs.h  |  1 +
 include/linux/nfs_xdr.h |  4 ++++
 6 files changed, 39 insertions(+), 12 deletions(-)
fs/nfs/inode.c +2 -2
index 83107be..f67aadb 100644
@@ -192,7 +192,6 @@ 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)
 {
@@ -557,7 +556,7 @@ EXPORT_SYMBOL_GPL(nfs_setattr);
  * corrected to take into account the fact that NFS requires
  * inode->i_size to be updated under the inode->i_lock.
  */
-static int nfs_vmtruncate(struct inode * inode, loff_t offset)
+int nfs_vmtruncate(struct inode * inode, loff_t offset)
 {
        int err;
 
@@ -576,6 +575,7 @@ static int nfs_vmtruncate(struct inode * inode, loff_t offset)
 out:
        return err;
 }
+EXPORT_SYMBOL_GPL(nfs_vmtruncate);
 
 /**
  * nfs_setattr_update_inode - Update inode metadata after a setattr call.
fs/nfs/nfs42proc.c +0 -0
index cb17072..407bfc3 100644

fs/nfs/nfs42proc.c static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep, · line 44

Quoted 9 lines
fs/nfs/nfs42proc.c +2 -0
@@ -36,24 +36,35 @@ 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 nfs_server *server = NFS_SERVER(inode);
        struct nfs42_falloc_args args = {
                .falloc_fh      = NFS_FH(inode),
                .falloc_offset  = offset,
                .falloc_length  = len,
+               .falloc_bitmask = server->attr_bitmask,

Why do a full getattr? Won't the cache consistency bitmask suffice? All you want is to get the change attribute, and possibly the new file size so that you can verify it is correct.

Quoted 24 lines
fs/nfs/nfs42proc.c +10 -5
        };
-       struct nfs42_falloc_res res;
-       struct nfs_server *server = NFS_SERVER(inode);
-       int status;
+       struct nfs42_falloc_res res = {
+               .falloc_server  = server,
+       };
+       int status = -ENOMEM;
 
        msg->rpc_argp = &args;
        msg->rpc_resp = &res;
 
+       nfs_fattr_init(&res.falloc_fattr);
+
        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);
+       status = nfs4_call_sync(server->client, server, msg,
+                               &args.seq_args, &res.seq_res, 0);
+       if (!status) {
+               nfs_vmtruncate(inode, offset);
Quoted 8 lines
Quoted 6 lines

Do you need the vmtruncate? I thought ALLOCATE could only extend the file, in which case calling truncate_pagecache() seems like overkill (and maybe even racy).

As for DEALLOCATE, you're punching a hole, so you really want to call truncate_inode_pages_range().

It looks like truncate_inode_pages_range() works just as well, so I'll switch over to that. ALLOCATE needs to mark the entire range as "unallocated", so calling this function for both operations is correct.

No. ALLOCATE (a.k.a. fallocate(~FALLOC_FL_PUNCH_HOLE)) is not supposed to change the contents of the data range being preallocated. If there is data in that range, then it must remain unchanged. The only extra requirement is that if you use it to extend the file, then the region which did not previously contain any data must return zeros. For that reason, both vmtruncate() and truncate_inode_pages_range() must _not_ be used, as those will clear out data in the page cache.

DEALLOCATE (a.k.a. fallocate(FL_PUNCH_HOLE)) does, OTOH, change the data contents in the range specified; it should zero out the data on both the client and the server. That is why calling truncate_inode_pages_range() on that range of the page cache is correct.

Signature · 3 lines
Trond Myklebust
Linux NFS client maintainer, PrimaryData
[email protected]