blob: 3a9e75235f30e60e5a4ae974864c63fb003b969d [file] [log] [blame]
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -04001/*
2 * Copyright (c) 2014 Anna Schumaker <Anna.Schumaker@Netapp.com>
3 */
4#include <linux/fs.h>
5#include <linux/sunrpc/sched.h>
6#include <linux/nfs.h>
7#include <linux/nfs3.h>
8#include <linux/nfs4.h>
9#include <linux/nfs_xdr.h>
10#include <linux/nfs_fs.h>
11#include "nfs4_fs.h"
12#include "nfs42.h"
13
14static int nfs42_set_rw_stateid(nfs4_stateid *dst, struct file *file,
15 fmode_t fmode)
16{
17 struct nfs_open_context *open;
18 struct nfs_lock_context *lock;
19 int ret;
20
21 open = get_nfs_open_context(nfs_file_open_context(file));
22 lock = nfs_get_lock_context(open);
23 if (IS_ERR(lock)) {
24 put_nfs_open_context(open);
25 return PTR_ERR(lock);
26 }
27
28 ret = nfs4_set_rw_stateid(dst, open, lock, fmode);
29
30 nfs_put_lock_context(lock);
31 put_nfs_open_context(open);
32 return ret;
33}
34
Anna Schumakerf4ac1672014-11-25 13:18:15 -050035static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
36 loff_t offset, loff_t len)
37{
38 struct inode *inode = file_inode(filep);
Anna Schumaker9a519402015-03-16 14:06:23 -040039 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerf4ac1672014-11-25 13:18:15 -050040 struct nfs42_falloc_args args = {
41 .falloc_fh = NFS_FH(inode),
42 .falloc_offset = offset,
43 .falloc_length = len,
Anna Schumaker9a519402015-03-16 14:06:23 -040044 .falloc_bitmask = server->cache_consistency_bitmask,
Anna Schumakerf4ac1672014-11-25 13:18:15 -050045 };
Anna Schumaker9a519402015-03-16 14:06:23 -040046 struct nfs42_falloc_res res = {
47 .falloc_server = server,
48 };
Anna Schumakerf4ac1672014-11-25 13:18:15 -050049 int status;
50
51 msg->rpc_argp = &args;
52 msg->rpc_resp = &res;
53
54 status = nfs42_set_rw_stateid(&args.falloc_stateid, filep, FMODE_WRITE);
55 if (status)
56 return status;
57
Anna Schumaker9a519402015-03-16 14:06:23 -040058 res.falloc_fattr = nfs_alloc_fattr();
59 if (!res.falloc_fattr)
60 return -ENOMEM;
61
62 status = nfs4_call_sync(server->client, server, msg,
63 &args.seq_args, &res.seq_res, 0);
64 if (status == 0)
65 status = nfs_post_op_update_inode(inode, res.falloc_fattr);
66
67 kfree(res.falloc_fattr);
68 return status;
Anna Schumakerf4ac1672014-11-25 13:18:15 -050069}
70
71static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
72 loff_t offset, loff_t len)
73{
74 struct nfs_server *server = NFS_SERVER(file_inode(filep));
75 struct nfs4_exception exception = { };
76 int err;
77
78 do {
79 err = _nfs42_proc_fallocate(msg, filep, offset, len);
80 if (err == -ENOTSUPP)
81 return -EOPNOTSUPP;
82 err = nfs4_handle_exception(server, err, &exception);
83 } while (exception.retry);
84
85 return err;
86}
87
88int nfs42_proc_allocate(struct file *filep, loff_t offset, loff_t len)
89{
90 struct rpc_message msg = {
91 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE],
92 };
93 struct inode *inode = file_inode(filep);
94 int err;
95
96 if (!nfs_server_capable(inode, NFS_CAP_ALLOCATE))
97 return -EOPNOTSUPP;
98
Anna Schumakerf830f7d2015-03-16 14:06:24 -040099 mutex_lock(&inode->i_mutex);
100
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500101 err = nfs42_proc_fallocate(&msg, filep, offset, len);
102 if (err == -EOPNOTSUPP)
103 NFS_SERVER(inode)->caps &= ~NFS_CAP_ALLOCATE;
Anna Schumakerf830f7d2015-03-16 14:06:24 -0400104
105 mutex_unlock(&inode->i_mutex);
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500106 return err;
107}
108
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500109int nfs42_proc_deallocate(struct file *filep, loff_t offset, loff_t len)
110{
111 struct rpc_message msg = {
112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DEALLOCATE],
113 };
114 struct inode *inode = file_inode(filep);
115 int err;
116
117 if (!nfs_server_capable(inode, NFS_CAP_DEALLOCATE))
118 return -EOPNOTSUPP;
119
Anna Schumaker9a519402015-03-16 14:06:23 -0400120 nfs_wb_all(inode);
Anna Schumakerf830f7d2015-03-16 14:06:24 -0400121 mutex_lock(&inode->i_mutex);
122
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500123 err = nfs42_proc_fallocate(&msg, filep, offset, len);
Anna Schumaker9a519402015-03-16 14:06:23 -0400124 if (err == 0)
125 truncate_pagecache_range(inode, offset, (offset + len) -1);
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500126 if (err == -EOPNOTSUPP)
127 NFS_SERVER(inode)->caps &= ~NFS_CAP_DEALLOCATE;
Anna Schumakerf830f7d2015-03-16 14:06:24 -0400128
129 mutex_unlock(&inode->i_mutex);
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500130 return err;
131}
132
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400133loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence)
134{
135 struct inode *inode = file_inode(filep);
136 struct nfs42_seek_args args = {
137 .sa_fh = NFS_FH(inode),
138 .sa_offset = offset,
139 .sa_what = (whence == SEEK_HOLE) ?
140 NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA,
141 };
142 struct nfs42_seek_res res;
143 struct rpc_message msg = {
144 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK],
145 .rpc_argp = &args,
146 .rpc_resp = &res,
147 };
148 struct nfs_server *server = NFS_SERVER(inode);
149 int status;
150
Anna Schumaker878ffa92014-10-23 14:00:54 -0400151 if (!nfs_server_capable(inode, NFS_CAP_SEEK))
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400152 return -ENOTSUPP;
153
154 status = nfs42_set_rw_stateid(&args.sa_stateid, filep, FMODE_READ);
155 if (status)
156 return status;
157
158 nfs_wb_all(inode);
159 status = nfs4_call_sync(server->client, server, &msg,
160 &args.seq_args, &res.seq_res, 0);
161 if (status == -ENOTSUPP)
162 server->caps &= ~NFS_CAP_SEEK;
163 if (status)
164 return status;
165
166 return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes);
167}