blob: 90aed76e593bdf3c60a4bc72bc51476267530b6e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
39#include <linux/utsname.h>
40#include <linux/delay.h>
41#include <linux/errno.h>
42#include <linux/string.h>
43#include <linux/sunrpc/clnt.h>
44#include <linux/nfs.h>
45#include <linux/nfs4.h>
46#include <linux/nfs_fs.h>
47#include <linux/nfs_page.h>
48#include <linux/smp_lock.h>
49#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070050#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Trond Myklebust4ce79712005-06-22 17:16:21 +000052#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050054#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050055#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#define NFSDBG_FACILITY NFSDBG_PROC
58
Trond Myklebust2066fe82006-09-15 08:30:46 -040059#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define NFS4_POLL_RETRY_MAX (15*HZ)
61
Trond Myklebustcdd4e682006-01-03 09:55:12 +010062struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010063static int _nfs4_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050065static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040066static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
67static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050070static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 if (err < -1000) {
73 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070074 __func__, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return -EIO;
76 }
77 return err;
78}
79
80/*
81 * This is our standard bitmap for GETATTR requests.
82 */
83const u32 nfs4_fattr_bitmap[2] = {
84 FATTR4_WORD0_TYPE
85 | FATTR4_WORD0_CHANGE
86 | FATTR4_WORD0_SIZE
87 | FATTR4_WORD0_FSID
88 | FATTR4_WORD0_FILEID,
89 FATTR4_WORD1_MODE
90 | FATTR4_WORD1_NUMLINKS
91 | FATTR4_WORD1_OWNER
92 | FATTR4_WORD1_OWNER_GROUP
93 | FATTR4_WORD1_RAWDEV
94 | FATTR4_WORD1_SPACE_USED
95 | FATTR4_WORD1_TIME_ACCESS
96 | FATTR4_WORD1_TIME_METADATA
97 | FATTR4_WORD1_TIME_MODIFY
98};
99
100const u32 nfs4_statfs_bitmap[2] = {
101 FATTR4_WORD0_FILES_AVAIL
102 | FATTR4_WORD0_FILES_FREE
103 | FATTR4_WORD0_FILES_TOTAL,
104 FATTR4_WORD1_SPACE_AVAIL
105 | FATTR4_WORD1_SPACE_FREE
106 | FATTR4_WORD1_SPACE_TOTAL
107};
108
Trond Myklebust4ce79712005-06-22 17:16:21 +0000109const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 FATTR4_WORD0_MAXLINK
111 | FATTR4_WORD0_MAXNAME,
112 0
113};
114
115const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
116 | FATTR4_WORD0_MAXREAD
117 | FATTR4_WORD0_MAXWRITE
118 | FATTR4_WORD0_LEASE_TIME,
119 0
120};
121
Manoj Naik830b8e32006-06-09 09:34:25 -0400122const u32 nfs4_fs_locations_bitmap[2] = {
123 FATTR4_WORD0_TYPE
124 | FATTR4_WORD0_CHANGE
125 | FATTR4_WORD0_SIZE
126 | FATTR4_WORD0_FSID
127 | FATTR4_WORD0_FILEID
128 | FATTR4_WORD0_FS_LOCATIONS,
129 FATTR4_WORD1_MODE
130 | FATTR4_WORD1_NUMLINKS
131 | FATTR4_WORD1_OWNER
132 | FATTR4_WORD1_OWNER_GROUP
133 | FATTR4_WORD1_RAWDEV
134 | FATTR4_WORD1_SPACE_USED
135 | FATTR4_WORD1_TIME_ACCESS
136 | FATTR4_WORD1_TIME_METADATA
137 | FATTR4_WORD1_TIME_MODIFY
138 | FATTR4_WORD1_MOUNTED_ON_FILEID
139};
140
Al Virobc4785c2006-10-19 23:28:51 -0700141static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct nfs4_readdir_arg *readdir)
143{
Al Viro0dbb4c62006-10-19 23:28:49 -0700144 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 BUG_ON(readdir->count < 80);
147 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000148 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
150 return;
151 }
152
153 readdir->cookie = 0;
154 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
155 if (cookie == 2)
156 return;
157
158 /*
159 * NFSv4 servers do not return entries for '.' and '..'
160 * Therefore, we fake these entries here. We let '.'
161 * have cookie 0 and '..' have cookie 1. Note that
162 * when talking to the server, we always send cookie 0
163 * instead of 1 or 2.
164 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700165 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 if (cookie == 0) {
168 *p++ = xdr_one; /* next */
169 *p++ = xdr_zero; /* cookie, first word */
170 *p++ = xdr_one; /* cookie, second word */
171 *p++ = xdr_one; /* entry len */
172 memcpy(p, ".\0\0\0", 4); /* entry */
173 p++;
174 *p++ = xdr_one; /* bitmap length */
175 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
176 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400177 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179
180 *p++ = xdr_one; /* next */
181 *p++ = xdr_zero; /* cookie, first word */
182 *p++ = xdr_two; /* cookie, second word */
183 *p++ = xdr_two; /* entry len */
184 memcpy(p, "..\0\0", 4); /* entry */
185 p++;
186 *p++ = xdr_one; /* bitmap length */
187 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
188 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400189 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 readdir->pgbase = (char *)p - (char *)start;
192 readdir->count -= readdir->pgbase;
193 kunmap_atomic(start, KM_USER0);
194}
195
Trond Myklebust65de8722008-12-23 15:21:44 -0500196static int nfs4_wait_clnt_recover(struct nfs_client *clp)
197{
198 int res;
199
200 might_sleep();
201
Trond Myklebuste005e802008-12-23 15:21:48 -0500202 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400203 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500204 return res;
205}
206
207static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
208{
209 int res = 0;
210
211 might_sleep();
212
213 if (*timeout <= 0)
214 *timeout = NFS4_POLL_RETRY_MIN;
215 if (*timeout > NFS4_POLL_RETRY_MAX)
216 *timeout = NFS4_POLL_RETRY_MAX;
217 schedule_timeout_killable(*timeout);
218 if (fatal_signal_pending(current))
219 res = -ERESTARTSYS;
220 *timeout <<= 1;
221 return res;
222}
223
224/* This is the error handling routine for processes that are allowed
225 * to sleep.
226 */
227static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
228{
229 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500230 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500231 int ret = errorcode;
232
233 exception->retry = 0;
234 switch(errorcode) {
235 case 0:
236 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500237 case -NFS4ERR_ADMIN_REVOKED:
238 case -NFS4ERR_BAD_STATEID:
239 case -NFS4ERR_OPENMODE:
240 if (state == NULL)
241 break;
242 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500243 case -NFS4ERR_STALE_CLIENTID:
244 case -NFS4ERR_STALE_STATEID:
245 case -NFS4ERR_EXPIRED:
246 nfs4_schedule_state_recovery(clp);
247 ret = nfs4_wait_clnt_recover(clp);
248 if (ret == 0)
249 exception->retry = 1;
250 break;
251 case -NFS4ERR_FILE_OPEN:
252 case -NFS4ERR_GRACE:
253 case -NFS4ERR_DELAY:
254 ret = nfs4_delay(server->client, &exception->timeout);
255 if (ret != 0)
256 break;
257 case -NFS4ERR_OLD_STATEID:
258 exception->retry = 1;
259 }
260 /* We failed to handle the error */
261 return nfs4_map_errors(ret);
262}
263
264
Trond Myklebust26e976a2006-01-03 09:55:21 +0100265static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
David Howells7539bba2006-08-22 20:06:09 -0400267 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 spin_lock(&clp->cl_lock);
269 if (time_before(clp->cl_last_renewal,timestamp))
270 clp->cl_last_renewal = timestamp;
271 spin_unlock(&clp->cl_lock);
272}
273
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400274#if defined(CONFIG_NFS_V4_1)
275
Andy Adamsonce5039c2009-04-01 09:22:13 -0400276static int nfs41_setup_sequence(struct nfs4_session *session,
277 struct nfs4_sequence_args *args,
278 struct nfs4_sequence_res *res,
279 int cache_reply,
280 struct rpc_task *task)
281{
282 /* slot already allocated? */
283 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
284 return 0;
285
286 memset(res, 0, sizeof(*res));
287 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
288 return 0;
289}
290
291int nfs4_setup_sequence(struct nfs_client *clp,
292 struct nfs4_sequence_args *args,
293 struct nfs4_sequence_res *res,
294 int cache_reply,
295 struct rpc_task *task)
296{
297 int ret = 0;
298
299 dprintk("--> %s clp %p session %p sr_slotid %d\n",
300 __func__, clp, clp->cl_session, res->sr_slotid);
301
302 if (!nfs4_has_session(clp))
303 goto out;
304 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
305 task);
306 if (ret != -EAGAIN) {
307 /* terminate rpc task */
308 task->tk_status = ret;
309 task->tk_action = NULL;
310 }
311out:
312 dprintk("<-- %s status=%d\n", __func__, ret);
313 return ret;
314}
315
316struct nfs41_call_sync_data {
317 struct nfs_client *clp;
318 struct nfs4_sequence_args *seq_args;
319 struct nfs4_sequence_res *seq_res;
320 int cache_reply;
321};
322
323static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
324{
325 struct nfs41_call_sync_data *data = calldata;
326
327 dprintk("--> %s data->clp->cl_session %p\n", __func__,
328 data->clp->cl_session);
329 if (nfs4_setup_sequence(data->clp, data->seq_args,
330 data->seq_res, data->cache_reply, task))
331 return;
332 rpc_call_start(task);
333}
334
335struct rpc_call_ops nfs41_call_sync_ops = {
336 .rpc_call_prepare = nfs41_call_sync_prepare,
337};
338
339static int nfs4_call_sync_sequence(struct nfs_client *clp,
340 struct rpc_clnt *clnt,
341 struct rpc_message *msg,
342 struct nfs4_sequence_args *args,
343 struct nfs4_sequence_res *res,
344 int cache_reply)
345{
346 int ret;
347 struct rpc_task *task;
348 struct nfs41_call_sync_data data = {
349 .clp = clp,
350 .seq_args = args,
351 .seq_res = res,
352 .cache_reply = cache_reply,
353 };
354 struct rpc_task_setup task_setup = {
355 .rpc_client = clnt,
356 .rpc_message = msg,
357 .callback_ops = &nfs41_call_sync_ops,
358 .callback_data = &data
359 };
360
361 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
362 task = rpc_run_task(&task_setup);
363 if (IS_ERR(task))
364 ret = PTR_ERR(task);
365 else {
366 ret = task->tk_status;
367 rpc_put_task(task);
368 }
369 return ret;
370}
371
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400372int _nfs4_call_sync_session(struct nfs_server *server,
373 struct rpc_message *msg,
374 struct nfs4_sequence_args *args,
375 struct nfs4_sequence_res *res,
376 int cache_reply)
377{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400378 return nfs4_call_sync_sequence(server->nfs_client, server->client,
379 msg, args, res, cache_reply);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400380}
381
382#endif /* CONFIG_NFS_V4_1 */
383
384int _nfs4_call_sync(struct nfs_server *server,
385 struct rpc_message *msg,
386 struct nfs4_sequence_args *args,
387 struct nfs4_sequence_res *res,
388 int cache_reply)
389{
Benny Halevyf3752972009-04-01 09:22:04 -0400390 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400391 return rpc_call_sync(server->client, msg, 0);
392}
393
394#define nfs4_call_sync(server, msg, args, res, cache_reply) \
395 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
396 &(res)->seq_res, (cache_reply))
397
Trond Myklebust38478b22006-05-25 01:40:57 -0400398static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Trond Myklebust38478b22006-05-25 01:40:57 -0400400 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Trond Myklebust38478b22006-05-25 01:40:57 -0400402 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400403 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
404 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400405 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400406 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400407 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100410struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400411 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100412 struct nfs_openargs o_arg;
413 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100414 struct nfs_open_confirmargs c_arg;
415 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100416 struct nfs_fattr f_attr;
417 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400418 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100419 struct dentry *dir;
420 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400421 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100422 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100423 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400424 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100425 int rpc_status;
426 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100427};
428
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400429
430static void nfs4_init_opendata_res(struct nfs4_opendata *p)
431{
432 p->o_res.f_attr = &p->f_attr;
433 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400434 p->o_res.seqid = p->o_arg.seqid;
435 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400436 p->o_res.server = p->o_arg.server;
437 nfs_fattr_init(&p->f_attr);
438 nfs_fattr_init(&p->dir_attr);
439}
440
Trond Myklebustad389da2007-06-05 12:30:00 -0400441static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500442 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100443 const struct iattr *attrs)
444{
Trond Myklebustad389da2007-06-05 12:30:00 -0400445 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100446 struct inode *dir = parent->d_inode;
447 struct nfs_server *server = NFS_SERVER(dir);
448 struct nfs4_opendata *p;
449
450 p = kzalloc(sizeof(*p), GFP_KERNEL);
451 if (p == NULL)
452 goto err;
453 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
454 if (p->o_arg.seqid == NULL)
455 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400456 p->path.mnt = mntget(path->mnt);
457 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100458 p->dir = parent;
459 p->owner = sp;
460 atomic_inc(&sp->so_count);
461 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500462 p->o_arg.open_flags = flags;
463 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400464 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400465 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400466 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100467 p->o_arg.server = server;
468 p->o_arg.bitmask = server->attr_bitmask;
469 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Andy Adamson5f7dbd52009-04-01 09:22:05 -0400470 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100471 if (flags & O_EXCL) {
472 u32 *s = (u32 *) p->o_arg.u.verifier.data;
473 s[0] = jiffies;
474 s[1] = current->pid;
475 } else if (flags & O_CREAT) {
476 p->o_arg.u.attrs = &p->attrs;
477 memcpy(&p->attrs, attrs, sizeof(p->attrs));
478 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100479 p->c_arg.fh = &p->o_res.fh;
480 p->c_arg.stateid = &p->o_res.stateid;
481 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400482 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400483 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100484 return p;
485err_free:
486 kfree(p);
487err:
488 dput(parent);
489 return NULL;
490}
491
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400492static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100493{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400494 struct nfs4_opendata *p = container_of(kref,
495 struct nfs4_opendata, kref);
496
497 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400498 if (p->state != NULL)
499 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400500 nfs4_put_state_owner(p->owner);
501 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700502 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400503 kfree(p);
504}
505
506static void nfs4_opendata_put(struct nfs4_opendata *p)
507{
508 if (p != NULL)
509 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100510}
511
Trond Myklebust06f814a2006-01-03 09:55:07 +0100512static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
513{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100514 int ret;
515
Trond Myklebust06f814a2006-01-03 09:55:07 +0100516 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100517 return ret;
518}
519
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500520static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400521{
522 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500523
524 if (open_mode & O_EXCL)
525 goto out;
526 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400527 case FMODE_READ:
528 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400529 break;
530 case FMODE_WRITE:
531 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400532 break;
533 case FMODE_READ|FMODE_WRITE:
534 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
535 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500536out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400537 return ret;
538}
539
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500540static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400541{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500542 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400543 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500544 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400545 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500546 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400547 return 1;
548}
549
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500550static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100551{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500552 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100553 case FMODE_WRITE:
554 state->n_wronly++;
555 break;
556 case FMODE_READ:
557 state->n_rdonly++;
558 break;
559 case FMODE_READ|FMODE_WRITE:
560 state->n_rdwr++;
561 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500562 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100563}
564
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500565static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400566{
567 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
568 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
569 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500570 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400571 case FMODE_READ:
572 set_bit(NFS_O_RDONLY_STATE, &state->flags);
573 break;
574 case FMODE_WRITE:
575 set_bit(NFS_O_WRONLY_STATE, &state->flags);
576 break;
577 case FMODE_READ|FMODE_WRITE:
578 set_bit(NFS_O_RDWR_STATE, &state->flags);
579 }
580}
581
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500582static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400583{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400584 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500585 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400586 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400587}
588
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500589static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400591 /*
592 * Protect the call to nfs4_state_set_mode_locked and
593 * serialise the stateid update
594 */
595 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400596 if (deleg_stateid != NULL) {
597 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
598 set_bit(NFS_DELEGATED_STATE, &state->flags);
599 }
600 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500601 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400602 write_sequnlock(&state->seqlock);
603 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500604 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700605 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500608static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500609{
610 struct nfs_inode *nfsi = NFS_I(state->inode);
611 struct nfs_delegation *deleg_cur;
612 int ret = 0;
613
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500614 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500615
616 rcu_read_lock();
617 deleg_cur = rcu_dereference(nfsi->delegation);
618 if (deleg_cur == NULL)
619 goto no_delegation;
620
621 spin_lock(&deleg_cur->lock);
622 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500623 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500624 goto no_delegation_unlock;
625
626 if (delegation == NULL)
627 delegation = &deleg_cur->stateid;
628 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
629 goto no_delegation_unlock;
630
Trond Myklebustb7391f42008-12-23 15:21:52 -0500631 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500632 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500633 ret = 1;
634no_delegation_unlock:
635 spin_unlock(&deleg_cur->lock);
636no_delegation:
637 rcu_read_unlock();
638
639 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500640 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500641 ret = 1;
642 }
643
644 return ret;
645}
646
647
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500648static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400649{
650 struct nfs_delegation *delegation;
651
652 rcu_read_lock();
653 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500654 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400655 rcu_read_unlock();
656 return;
657 }
658 rcu_read_unlock();
659 nfs_inode_return_delegation(inode);
660}
661
Trond Myklebust6ee41262007-07-08 14:11:36 -0400662static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400663{
664 struct nfs4_state *state = opendata->state;
665 struct nfs_inode *nfsi = NFS_I(state->inode);
666 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500667 int open_mode = opendata->o_arg.open_flags & O_EXCL;
668 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400669 nfs4_stateid stateid;
670 int ret = -EAGAIN;
671
Trond Myklebustaac00a82007-07-05 19:02:21 -0400672 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500673 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400674 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500675 if (can_open_cached(state, fmode, open_mode)) {
676 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400677 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400678 goto out_return_state;
679 }
680 spin_unlock(&state->owner->so_lock);
681 }
Trond Myklebust34310432008-12-23 15:21:38 -0500682 rcu_read_lock();
683 delegation = rcu_dereference(nfsi->delegation);
684 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500685 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500686 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400687 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500688 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400689 /* Save the delegation */
690 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
691 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400692 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400693 if (ret != 0)
694 goto out;
695 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500696
697 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500698 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500699 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400700 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400701out:
702 return ERR_PTR(ret);
703out_return_state:
704 atomic_inc(&state->count);
705 return state;
706}
707
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100708static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
709{
710 struct inode *inode;
711 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400712 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400713 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100714
Trond Myklebustaac00a82007-07-05 19:02:21 -0400715 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400716 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400717 goto out;
718 }
719
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400720 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100721 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400722 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100723 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400724 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500725 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400726 goto err;
727 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100728 state = nfs4_get_open_state(inode, data->owner);
729 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400730 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400731 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400732 int delegation_flags = 0;
733
Trond Myklebust003707c2007-07-05 18:07:55 -0400734 rcu_read_lock();
735 delegation = rcu_dereference(NFS_I(inode)->delegation);
736 if (delegation)
737 delegation_flags = delegation->flags;
738 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -0500739 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400740 nfs_inode_set_delegation(state->inode,
741 data->owner->so_cred,
742 &data->o_res);
743 else
744 nfs_inode_reclaim_delegation(state->inode,
745 data->owner->so_cred,
746 &data->o_res);
747 }
Trond Myklebust34310432008-12-23 15:21:38 -0500748
749 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500750 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100751 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400752out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100753 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400754err_put_inode:
755 iput(inode);
756err:
757 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100758}
759
Trond Myklebust864472e2006-01-03 09:55:15 +0100760static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
761{
762 struct nfs_inode *nfsi = NFS_I(state->inode);
763 struct nfs_open_context *ctx;
764
765 spin_lock(&state->inode->i_lock);
766 list_for_each_entry(ctx, &nfsi->open_files, list) {
767 if (ctx->state != state)
768 continue;
769 get_nfs_open_context(ctx);
770 spin_unlock(&state->inode->i_lock);
771 return ctx;
772 }
773 spin_unlock(&state->inode->i_lock);
774 return ERR_PTR(-ENOENT);
775}
776
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400777static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
778{
779 struct nfs4_opendata *opendata;
780
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500781 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400782 if (opendata == NULL)
783 return ERR_PTR(-ENOMEM);
784 opendata->state = state;
785 atomic_inc(&state->count);
786 return opendata;
787}
788
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500789static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +0100790{
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400791 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100792 int ret;
793
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500794 opendata->o_arg.open_flags = 0;
795 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400796 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
797 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
798 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100799 ret = _nfs4_proc_open(opendata);
800 if (ret != 0)
801 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400802 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400803 if (IS_ERR(newstate))
804 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500805 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400806 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100807 return 0;
808}
809
810static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
811{
Trond Myklebust864472e2006-01-03 09:55:15 +0100812 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100813 int ret;
814
815 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400816 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100817 smp_rmb();
818 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400819 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100820 if (ret != 0)
821 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400822 if (newstate != state)
823 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100824 }
825 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400826 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100827 if (ret != 0)
828 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400829 if (newstate != state)
830 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100831 }
832 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400833 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100834 if (ret != 0)
835 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400836 if (newstate != state)
837 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100838 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400839 /*
840 * We may have performed cached opens for all three recoveries.
841 * Check if we need to update the current stateid.
842 */
843 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
844 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400845 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400846 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
847 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400848 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400849 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100850 return 0;
851}
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853/*
854 * OPEN_RECLAIM:
855 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 */
Trond Myklebust539cd032007-06-05 11:46:42 -0400857static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400859 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +0100860 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500861 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 int status;
863
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400864 opendata = nfs4_open_recoverdata_alloc(ctx, state);
865 if (IS_ERR(opendata))
866 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100867 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
868 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400869 rcu_read_lock();
870 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -0500871 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -0400872 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400873 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +0100874 opendata->o_arg.u.delegation_type = delegation_type;
875 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400876 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return status;
878}
879
Trond Myklebust539cd032007-06-05 11:46:42 -0400880static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 struct nfs_server *server = NFS_SERVER(state->inode);
883 struct nfs4_exception exception = { };
884 int err;
885 do {
Trond Myklebust539cd032007-06-05 11:46:42 -0400886 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000887 if (err != -NFS4ERR_DELAY)
888 break;
889 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 } while (exception.retry);
891 return err;
892}
893
Trond Myklebust864472e2006-01-03 09:55:15 +0100894static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
895{
896 struct nfs_open_context *ctx;
897 int ret;
898
899 ctx = nfs4_state_find_open_context(state);
900 if (IS_ERR(ctx))
901 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -0400902 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +0100903 put_nfs_open_context(ctx);
904 return ret;
905}
906
Trond Myklebust13437e12007-07-06 15:10:43 -0400907static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100909 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +0100910 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400912 opendata = nfs4_open_recoverdata_alloc(ctx, state);
913 if (IS_ERR(opendata))
914 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100915 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -0400916 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100917 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +0100918 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400919 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100920 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
Trond Myklebust13437e12007-07-06 15:10:43 -0400923int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
925 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -0400926 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 int err;
928 do {
Trond Myklebust13437e12007-07-06 15:10:43 -0400929 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 switch (err) {
931 case 0:
932 return err;
933 case -NFS4ERR_STALE_CLIENTID:
934 case -NFS4ERR_STALE_STATEID:
935 case -NFS4ERR_EXPIRED:
936 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -0400937 nfs4_schedule_state_recovery(server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return err;
939 }
940 err = nfs4_handle_exception(server, err, &exception);
941 } while (exception.retry);
942 return err;
943}
944
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100945static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
946{
947 struct nfs4_opendata *data = calldata;
948
949 data->rpc_status = task->tk_status;
950 if (RPC_ASSASSINATED(task))
951 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100952 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100953 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
954 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -0500955 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +0100956 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -0400957 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100958 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100959}
960
961static void nfs4_open_confirm_release(void *calldata)
962{
963 struct nfs4_opendata *data = calldata;
964 struct nfs4_state *state = NULL;
965
966 /* If this request hasn't been cancelled, do nothing */
967 if (data->cancelled == 0)
968 goto out_free;
969 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -0400970 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100971 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100972 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400973 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500974 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100975out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400976 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100977}
978
979static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100980 .rpc_call_done = nfs4_open_confirm_done,
981 .rpc_release = nfs4_open_confirm_release,
982};
983
984/*
985 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
986 */
987static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
988{
989 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
990 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -0400991 struct rpc_message msg = {
992 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
993 .rpc_argp = &data->c_arg,
994 .rpc_resp = &data->c_res,
995 .rpc_cred = data->owner->so_cred,
996 };
Trond Myklebustc970aa82007-07-14 15:39:59 -0400997 struct rpc_task_setup task_setup_data = {
998 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -0400999 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001000 .callback_ops = &nfs4_open_confirm_ops,
1001 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001002 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001003 .flags = RPC_TASK_ASYNC,
1004 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 int status;
1006
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001007 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001008 data->rpc_done = 0;
1009 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001010 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001011 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001012 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001013 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001014 status = nfs4_wait_for_completion_rpc_task(task);
1015 if (status != 0) {
1016 data->cancelled = 1;
1017 smp_wmb();
1018 } else
1019 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001020 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return status;
1022}
1023
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001024static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001026 struct nfs4_opendata *data = calldata;
1027 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001028
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001029 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1030 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001031 /*
1032 * Check if we still need to send an OPEN call, or if we can use
1033 * a delegation instead.
1034 */
1035 if (data->state != NULL) {
1036 struct nfs_delegation *delegation;
1037
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001038 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001039 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001040 rcu_read_lock();
1041 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1042 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001043 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001044 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001045 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001046 }
1047 rcu_read_unlock();
1048 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001049 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001050 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001051 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001052 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001053 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001054 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1055 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001056 data->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001057 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001058 return;
1059out_no_action:
1060 task->tk_action = NULL;
1061
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001062}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001064static void nfs4_open_done(struct rpc_task *task, void *calldata)
1065{
1066 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001068 data->rpc_status = task->tk_status;
1069 if (RPC_ASSASSINATED(task))
1070 return;
1071 if (task->tk_status == 0) {
1072 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001073 case S_IFREG:
1074 break;
1075 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001076 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001077 break;
1078 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001079 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001080 break;
1081 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001082 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001083 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001084 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001085 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1086 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001087 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001088 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001089}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001090
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001091static void nfs4_open_release(void *calldata)
1092{
1093 struct nfs4_opendata *data = calldata;
1094 struct nfs4_state *state = NULL;
1095
1096 /* If this request hasn't been cancelled, do nothing */
1097 if (data->cancelled == 0)
1098 goto out_free;
1099 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001100 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001101 goto out_free;
1102 /* In case we need an open_confirm, no cleanup! */
1103 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1104 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001105 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001106 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001107 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001108out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001109 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001110}
1111
1112static const struct rpc_call_ops nfs4_open_ops = {
1113 .rpc_call_prepare = nfs4_open_prepare,
1114 .rpc_call_done = nfs4_open_done,
1115 .rpc_release = nfs4_open_release,
1116};
1117
1118/*
1119 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1120 */
1121static int _nfs4_proc_open(struct nfs4_opendata *data)
1122{
1123 struct inode *dir = data->dir->d_inode;
1124 struct nfs_server *server = NFS_SERVER(dir);
1125 struct nfs_openargs *o_arg = &data->o_arg;
1126 struct nfs_openres *o_res = &data->o_res;
1127 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001128 struct rpc_message msg = {
1129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1130 .rpc_argp = o_arg,
1131 .rpc_resp = o_res,
1132 .rpc_cred = data->owner->so_cred,
1133 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001134 struct rpc_task_setup task_setup_data = {
1135 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001136 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001137 .callback_ops = &nfs4_open_ops,
1138 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001139 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001140 .flags = RPC_TASK_ASYNC,
1141 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001142 int status;
1143
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001144 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001145 data->rpc_done = 0;
1146 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001147 data->cancelled = 0;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001148 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001149 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001150 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001151 status = nfs4_wait_for_completion_rpc_task(task);
1152 if (status != 0) {
1153 data->cancelled = 1;
1154 smp_wmb();
1155 } else
1156 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001157 rpc_put_task(task);
Trond Myklebust3e309912007-07-07 13:19:59 -04001158 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001159 return status;
1160
Trond Myklebust99367812007-07-17 21:52:41 -04001161 if (o_res->fh.size == 0)
1162 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1163
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001164 if (o_arg->open_flags & O_CREAT) {
1165 update_changeattr(dir, &o_res->cinfo);
1166 nfs_post_op_update_inode(dir, o_res->dir_attr);
1167 } else
1168 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001170 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001172 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
1174 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001175 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
Trond Myklebust10afec902007-05-14 17:16:04 -04001179static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001180{
David Howells7539bba2006-08-22 20:06:09 -04001181 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6b309542006-09-14 14:03:14 -04001182 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001183
Trond Myklebust6b309542006-09-14 14:03:14 -04001184 for (;;) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001185 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001186 if (ret != 0)
1187 return ret;
Trond Myklebuste598d842008-12-23 15:21:42 -05001188 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1189 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001190 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001191 nfs4_schedule_state_recovery(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001192 }
1193 return 0;
Trond Myklebust58d97142006-01-03 09:55:24 +01001194}
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196/*
1197 * OPEN_EXPIRED:
1198 * reclaim state on the server after a network partition.
1199 * Assumes caller holds the appropriate lock
1200 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001201static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001203 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001204 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001206 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1207 if (IS_ERR(opendata))
1208 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001209 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001210 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001211 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001212 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001213 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
Trond Myklebust539cd032007-06-05 11:46:42 -04001216static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001217{
Trond Myklebust539cd032007-06-05 11:46:42 -04001218 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001219 struct nfs4_exception exception = { };
1220 int err;
1221
1222 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001223 err = _nfs4_open_expired(ctx, state);
Trond Myklebust027b6ca2008-12-23 16:04:13 -05001224 if (err != -NFS4ERR_DELAY)
1225 break;
1226 nfs4_handle_exception(server, err, &exception);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001227 } while (exception.retry);
1228 return err;
1229}
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1232{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001234 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Trond Myklebust864472e2006-01-03 09:55:15 +01001236 ctx = nfs4_state_find_open_context(state);
1237 if (IS_ERR(ctx))
1238 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001239 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001240 put_nfs_open_context(ctx);
1241 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
1244/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001245 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1246 * fields corresponding to attributes that were used to store the verifier.
1247 * Make sure we clobber those fields in the later setattr call
1248 */
1249static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1250{
1251 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1252 !(sattr->ia_valid & ATTR_ATIME_SET))
1253 sattr->ia_valid |= ATTR_ATIME;
1254
1255 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1256 !(sattr->ia_valid & ATTR_MTIME_SET))
1257 sattr->ia_valid |= ATTR_MTIME;
1258}
1259
1260/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001261 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001263static int _nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
1265 struct nfs4_state_owner *sp;
1266 struct nfs4_state *state = NULL;
1267 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001268 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001269 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 status = -ENOMEM;
1273 if (!(sp = nfs4_get_state_owner(server, cred))) {
1274 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1275 goto out_err;
1276 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001277 status = nfs4_recover_expired_lease(server);
1278 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001279 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001280 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001281 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001282 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001283 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001284 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001285 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Trond Myklebustaac00a82007-07-05 19:02:21 -04001287 if (path->dentry->d_inode != NULL)
1288 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1289
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001290 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001292 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001294 if (opendata->o_arg.open_flags & O_EXCL)
1295 nfs4_exclusive_attrset(opendata, sattr);
1296
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001297 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001298 status = PTR_ERR(state);
1299 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001300 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001301 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 *res = state;
1304 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001305err_opendata_put:
1306 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001307err_put_state_owner:
1308 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 *res = NULL;
1311 return status;
1312}
1313
1314
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001315static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
1317 struct nfs4_exception exception = { };
1318 struct nfs4_state *res;
1319 int status;
1320
1321 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001322 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (status == 0)
1324 break;
1325 /* NOTE: BAD_SEQID means the server and client disagree about the
1326 * book-keeping w.r.t. state-changing operations
1327 * (OPEN/CLOSE/LOCK/LOCKU...)
1328 * It is actually a sign of a bug on the client or on the server.
1329 *
1330 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001331 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 * have unhashed the old state_owner for us, and that we can
1333 * therefore safely retry using a new one. We should still warn
1334 * the user though...
1335 */
1336 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001337 printk(KERN_WARNING "NFS: v4 server %s "
1338 " returned a bad sequence-id error!\n",
1339 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 exception.retry = 1;
1341 continue;
1342 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001343 /*
1344 * BAD_STATEID on OPEN means that the server cancelled our
1345 * state before it received the OPEN_CONFIRM.
1346 * Recover by retrying the request as per the discussion
1347 * on Page 181 of RFC3530.
1348 */
1349 if (status == -NFS4ERR_BAD_STATEID) {
1350 exception.retry = 1;
1351 continue;
1352 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001353 if (status == -EAGAIN) {
1354 /* We must have found a delegation */
1355 exception.retry = 1;
1356 continue;
1357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1359 status, &exception));
1360 } while (exception.retry);
1361 return res;
1362}
1363
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001364static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1365 struct nfs_fattr *fattr, struct iattr *sattr,
1366 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001368 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001370 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 .iap = sattr,
1372 .server = server,
1373 .bitmask = server->attr_bitmask,
1374 };
1375 struct nfs_setattrres res = {
1376 .fattr = fattr,
1377 .server = server,
1378 };
1379 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001380 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1381 .rpc_argp = &arg,
1382 .rpc_resp = &res,
1383 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001385 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001386 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Trond Myklebust0e574af2005-10-27 22:12:38 -04001388 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001390 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1391 /* Use that stateid */
1392 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001393 nfs4_copy_stateid(&arg.stateid, state, current->files);
1394 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1396
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001397 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001398 if (status == 0 && state != NULL)
1399 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001400 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001403static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1404 struct nfs_fattr *fattr, struct iattr *sattr,
1405 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001407 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 struct nfs4_exception exception = { };
1409 int err;
1410 do {
1411 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001412 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 &exception);
1414 } while (exception.retry);
1415 return err;
1416}
1417
1418struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001419 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 struct inode *inode;
1421 struct nfs4_state *state;
1422 struct nfs_closeargs arg;
1423 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001424 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001425 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426};
1427
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001428static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001429{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001430 struct nfs4_closedata *calldata = data;
1431 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001432
1433 nfs4_put_open_state(calldata->state);
1434 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001435 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001436 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001437 kfree(calldata);
1438}
1439
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001440static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001442 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 struct nfs_server *server = NFS_SERVER(calldata->inode);
1445
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001446 if (RPC_ASSASSINATED(task))
1447 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 /* hmm. we are done with the inode, and in the process of freeing
1449 * the state_owner. we keep this around to process errors
1450 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 switch (task->tk_status) {
1452 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001453 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001454 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 break;
1456 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001457 case -NFS4ERR_OLD_STATEID:
1458 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001460 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001461 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001463 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 rpc_restart_call(task);
1465 return;
1466 }
1467 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001468 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001471static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001473 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001474 struct nfs4_state *state = calldata->state;
Trond Myklebust003707c2007-07-05 18:07:55 -04001475 int clear_rd, clear_wr, clear_rdwr;
Trond Myklebust95121352005-10-18 14:20:12 -07001476
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001477 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001478 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001479
Trond Myklebust003707c2007-07-05 18:07:55 -04001480 clear_rd = clear_wr = clear_rdwr = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001481 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001482 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001483 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001484 if (state->n_rdonly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001485 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1486 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1487 }
1488 if (state->n_wronly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001489 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1490 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1491 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001492 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001493 spin_unlock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001494 if (!clear_rd && !clear_wr && !clear_rdwr) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001495 /* Note: exit _without_ calling nfs4_close_done */
1496 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001497 return;
1498 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001499 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust45328c32007-07-26 17:47:34 -04001500 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001501 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001502 calldata->arg.fmode = FMODE_READ;
Trond Myklebust45328c32007-07-26 17:47:34 -04001503 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001504 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001505 calldata->arg.fmode = FMODE_WRITE;
Trond Myklebust45328c32007-07-26 17:47:34 -04001506 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001507 calldata->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001508 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001511static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001512 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001513 .rpc_call_done = nfs4_close_done,
1514 .rpc_release = nfs4_free_closedata,
1515};
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517/*
1518 * It is possible for data to be read/written from a mem-mapped file
1519 * after the sys_close call (which hits the vfs layer as a flush).
1520 * This means that we can't safely call nfsv4 close on a file until
1521 * the inode is cleared. This in turn means that we are not good
1522 * NFSv4 citizens - we do not indicate to the server to update the file's
1523 * share state even when we are done with one of the three share
1524 * stateid's in the inode.
1525 *
1526 * NOTE: Caller must be holding the sp->so_owner semaphore!
1527 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001528int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001530 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001532 struct nfs4_state_owner *sp = state->owner;
1533 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001534 struct rpc_message msg = {
1535 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1536 .rpc_cred = state->owner->so_cred,
1537 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001538 struct rpc_task_setup task_setup_data = {
1539 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001540 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001541 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001542 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001543 .flags = RPC_TASK_ASYNC,
1544 };
Trond Myklebust95121352005-10-18 14:20:12 -07001545 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Trond Myklebust95121352005-10-18 14:20:12 -07001547 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001549 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001550 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001552 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001553 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001555 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001556 if (calldata->arg.seqid == NULL)
1557 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001558 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001559 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001560 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001561 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001562 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001563 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001564 calldata->path.mnt = mntget(path->mnt);
1565 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001566
Trond Myklebust5138fde2007-07-14 15:40:01 -04001567 msg.rpc_argp = &calldata->arg,
1568 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001569 task_setup_data.callback_data = calldata;
1570 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001571 if (IS_ERR(task))
1572 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001573 status = 0;
1574 if (wait)
1575 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001576 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001577 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001578out_free_calldata:
1579 kfree(calldata);
1580out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001581 nfs4_put_open_state(state);
1582 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001583 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
1585
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001586static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
Trond Myklebust02a913a2005-10-18 14:20:17 -07001587{
1588 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001589 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001590
Trond Myklebust1b45c462007-07-03 13:04:56 -04001591 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001592 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001593 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001594 state->owner->so_cred,
1595 nd->intent.open.flags);
1596 if (ret < 0)
1597 goto out_close;
1598 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001599 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001600 if (!IS_ERR(filp)) {
1601 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001602 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001603 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001604 return 0;
1605 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001606 ret = PTR_ERR(filp);
1607out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001608 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001609 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001610}
1611
1612struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1614{
Trond Myklebustad389da2007-06-05 12:30:00 -04001615 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001616 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001617 .dentry = dentry,
1618 };
Jan Blunck4ac91372008-02-14 19:34:32 -08001619 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 struct iattr attr;
1621 struct rpc_cred *cred;
1622 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001623 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001624 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 if (nd->flags & LOOKUP_CREATE) {
1627 attr.ia_mode = nd->intent.open.create_mode;
1628 attr.ia_valid = ATTR_MODE;
1629 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04001630 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 } else {
1632 attr.ia_valid = 0;
1633 BUG_ON(nd->intent.open.flags & O_CREAT);
1634 }
1635
Trond Myklebust98a8e322008-03-12 12:25:28 -04001636 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001638 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04001639 parent = dentry->d_parent;
1640 /* Protect against concurrent sillydeletes */
1641 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001642 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001644 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001645 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001646 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04001647 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1648 }
Trond Myklebust565277f2007-10-15 18:17:53 -04001649 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001650 return (struct dentry *)state;
1651 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001652 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001653 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04001654 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04001655 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001656 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001657 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001658 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
1661int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001662nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Trond Myklebustad389da2007-06-05 12:30:00 -04001664 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001665 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001666 .dentry = dentry,
1667 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 struct rpc_cred *cred;
1669 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001670 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Trond Myklebust98a8e322008-03-12 12:25:28 -04001672 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if (IS_ERR(cred))
1674 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001675 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001677 if (IS_ERR(state)) {
1678 switch (PTR_ERR(state)) {
1679 case -EPERM:
1680 case -EACCES:
1681 case -EDQUOT:
1682 case -ENOSPC:
1683 case -EROFS:
1684 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1685 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001686 default:
1687 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001688 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001689 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001690 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001691 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001692 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return 1;
1694 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001695 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001696out_drop:
1697 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return 0;
1699}
1700
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001701void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1702{
1703 if (ctx->state == NULL)
1704 return;
1705 if (is_sync)
1706 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
1707 else
1708 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
1709}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1712{
Benny Halevy43652ad2009-04-01 09:21:54 -04001713 struct nfs4_server_caps_arg args = {
1714 .fhandle = fhandle,
1715 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 struct nfs4_server_caps_res res = {};
1717 struct rpc_message msg = {
1718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04001719 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 .rpc_resp = &res,
1721 };
1722 int status;
1723
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001724 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (status == 0) {
1726 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1727 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1728 server->caps |= NFS_CAP_ACLS;
1729 if (res.has_links != 0)
1730 server->caps |= NFS_CAP_HARDLINKS;
1731 if (res.has_symlinks != 0)
1732 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001733 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
1734 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
1735 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 server->acl_bitmask = res.acl_bitmask;
1737 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return status;
1740}
1741
Trond Myklebust55a97592006-06-09 09:34:19 -04001742int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
1744 struct nfs4_exception exception = { };
1745 int err;
1746 do {
1747 err = nfs4_handle_exception(server,
1748 _nfs4_server_capabilities(server, fhandle),
1749 &exception);
1750 } while (exception.retry);
1751 return err;
1752}
1753
1754static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1755 struct nfs_fsinfo *info)
1756{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 struct nfs4_lookup_root_arg args = {
1758 .bitmask = nfs4_fattr_bitmap,
1759 };
1760 struct nfs4_lookup_res res = {
1761 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001762 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 .fh = fhandle,
1764 };
1765 struct rpc_message msg = {
1766 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1767 .rpc_argp = &args,
1768 .rpc_resp = &res,
1769 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001770 nfs_fattr_init(info->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001771 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773
1774static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1775 struct nfs_fsinfo *info)
1776{
1777 struct nfs4_exception exception = { };
1778 int err;
1779 do {
1780 err = nfs4_handle_exception(server,
1781 _nfs4_lookup_root(server, fhandle, info),
1782 &exception);
1783 } while (exception.retry);
1784 return err;
1785}
1786
David Howells54ceac42006-08-22 20:06:13 -04001787/*
1788 * get the file handle for the "/" directory on the server
1789 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04001791 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 int status;
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 if (status == 0)
1797 status = nfs4_server_capabilities(server, fhandle);
1798 if (status == 0)
1799 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08001800 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
1802
Manoj Naik6b97fd32006-06-09 09:34:29 -04001803/*
1804 * Get locations and (maybe) other attributes of a referral.
1805 * Note that we'll actually follow the referral later when
1806 * we detect fsid mismatch in inode revalidation
1807 */
Trond Myklebust56659e92007-07-17 21:52:39 -04001808static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04001809{
1810 int status = -ENOMEM;
1811 struct page *page = NULL;
1812 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04001813
1814 page = alloc_page(GFP_KERNEL);
1815 if (page == NULL)
1816 goto out;
1817 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1818 if (locations == NULL)
1819 goto out;
1820
Trond Myklebustc228fd32007-01-13 02:28:11 -05001821 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001822 if (status != 0)
1823 goto out;
1824 /* Make sure server returned a different fsid for the referral */
1825 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07001826 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001827 status = -EIO;
1828 goto out;
1829 }
1830
1831 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1832 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1833 if (!fattr->mode)
1834 fattr->mode = S_IFDIR;
1835 memset(fhandle, 0, sizeof(struct nfs_fh));
1836out:
1837 if (page)
1838 __free_page(page);
1839 if (locations)
1840 kfree(locations);
1841 return status;
1842}
1843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1845{
1846 struct nfs4_getattr_arg args = {
1847 .fh = fhandle,
1848 .bitmask = server->attr_bitmask,
1849 };
1850 struct nfs4_getattr_res res = {
1851 .fattr = fattr,
1852 .server = server,
1853 };
1854 struct rpc_message msg = {
1855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1856 .rpc_argp = &args,
1857 .rpc_resp = &res,
1858 };
1859
Trond Myklebust0e574af2005-10-27 22:12:38 -04001860 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001861 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
1864static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1865{
1866 struct nfs4_exception exception = { };
1867 int err;
1868 do {
1869 err = nfs4_handle_exception(server,
1870 _nfs4_proc_getattr(server, fhandle, fattr),
1871 &exception);
1872 } while (exception.retry);
1873 return err;
1874}
1875
1876/*
1877 * The file is not closed if it is opened due to the a request to change
1878 * the size of the file. The open call will not be needed once the
1879 * VFS layer lookup-intents are implemented.
1880 *
1881 * Close is called when the inode is destroyed.
1882 * If we haven't opened the file for O_WRONLY, we
1883 * need to in the size_change case to obtain a stateid.
1884 *
1885 * Got race?
1886 * Because OPEN is always done by name in nfsv4, it is
1887 * possible that we opened a different file by the same
1888 * name. We can recognize this race condition, but we
1889 * can't do anything about it besides returning an error.
1890 *
1891 * This will be fixed with VFS changes (lookup-intent).
1892 */
1893static int
1894nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1895 struct iattr *sattr)
1896{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001897 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001898 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05001899 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 int status;
1901
Trond Myklebust0e574af2005-10-27 22:12:38 -04001902 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Trond Myklebustd5308382005-11-04 15:33:38 -05001904 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001905 if (sattr->ia_valid & ATTR_FILE) {
1906 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001907
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001908 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11001909 if (ctx) {
1910 cred = ctx->cred;
1911 state = ctx->state;
1912 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001913 }
1914
1915 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04001916 if (status == 0)
1917 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return status;
1919}
1920
Trond Myklebust56659e92007-07-17 21:52:39 -04001921static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
1922 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04001923 struct nfs_fattr *fattr)
1924{
1925 int status;
1926 struct nfs4_lookup_arg args = {
1927 .bitmask = server->attr_bitmask,
1928 .dir_fh = dirfh,
1929 .name = name,
1930 };
1931 struct nfs4_lookup_res res = {
1932 .server = server,
1933 .fattr = fattr,
1934 .fh = fhandle,
1935 };
1936 struct rpc_message msg = {
1937 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1938 .rpc_argp = &args,
1939 .rpc_resp = &res,
1940 };
1941
1942 nfs_fattr_init(fattr);
1943
1944 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001945 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04001946 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04001947 return status;
1948}
1949
1950static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1951 struct qstr *name, struct nfs_fh *fhandle,
1952 struct nfs_fattr *fattr)
1953{
1954 struct nfs4_exception exception = { };
1955 int err;
1956 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04001957 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
1958 /* FIXME: !!!! */
1959 if (err == -NFS4ERR_MOVED) {
1960 err = -EREMOTE;
1961 break;
1962 }
1963 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04001964 } while (exception.retry);
1965 return err;
1966}
1967
Trond Myklebust56659e92007-07-17 21:52:39 -04001968static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1970{
Trond Myklebust4e56e082007-07-01 18:13:52 -04001971 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04001974 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001975 if (status == -NFS4ERR_MOVED)
1976 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 dprintk("NFS reply lookup: %d\n", status);
1978 return status;
1979}
1980
1981static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1982{
1983 struct nfs4_exception exception = { };
1984 int err;
1985 do {
1986 err = nfs4_handle_exception(NFS_SERVER(dir),
1987 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1988 &exception);
1989 } while (exception.retry);
1990 return err;
1991}
1992
1993static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1994{
Trond Myklebust76b32992007-08-10 17:45:11 -04001995 struct nfs_server *server = NFS_SERVER(inode);
1996 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 struct nfs4_accessargs args = {
1998 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04001999 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002001 struct nfs4_accessres res = {
2002 .server = server,
2003 .fattr = &fattr,
2004 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 struct rpc_message msg = {
2006 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2007 .rpc_argp = &args,
2008 .rpc_resp = &res,
2009 .rpc_cred = entry->cred,
2010 };
2011 int mode = entry->mask;
2012 int status;
2013
2014 /*
2015 * Determine which access bits we want to ask for...
2016 */
2017 if (mode & MAY_READ)
2018 args.access |= NFS4_ACCESS_READ;
2019 if (S_ISDIR(inode->i_mode)) {
2020 if (mode & MAY_WRITE)
2021 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2022 if (mode & MAY_EXEC)
2023 args.access |= NFS4_ACCESS_LOOKUP;
2024 } else {
2025 if (mode & MAY_WRITE)
2026 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2027 if (mode & MAY_EXEC)
2028 args.access |= NFS4_ACCESS_EXECUTE;
2029 }
Trond Myklebust76b32992007-08-10 17:45:11 -04002030 nfs_fattr_init(&fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002031 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (!status) {
2033 entry->mask = 0;
2034 if (res.access & NFS4_ACCESS_READ)
2035 entry->mask |= MAY_READ;
2036 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2037 entry->mask |= MAY_WRITE;
2038 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2039 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04002040 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 }
2042 return status;
2043}
2044
2045static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2046{
2047 struct nfs4_exception exception = { };
2048 int err;
2049 do {
2050 err = nfs4_handle_exception(NFS_SERVER(inode),
2051 _nfs4_proc_access(inode, entry),
2052 &exception);
2053 } while (exception.retry);
2054 return err;
2055}
2056
2057/*
2058 * TODO: For the time being, we don't try to get any attributes
2059 * along with any of the zero-copy operations READ, READDIR,
2060 * READLINK, WRITE.
2061 *
2062 * In the case of the first three, we want to put the GETATTR
2063 * after the read-type operation -- this is because it is hard
2064 * to predict the length of a GETATTR response in v4, and thus
2065 * align the READ data correctly. This means that the GETATTR
2066 * may end up partially falling into the page cache, and we should
2067 * shift it into the 'tail' of the xdr_buf before processing.
2068 * To do this efficiently, we need to know the total length
2069 * of data received, which doesn't seem to be available outside
2070 * of the RPC layer.
2071 *
2072 * In the case of WRITE, we also want to put the GETATTR after
2073 * the operation -- in this case because we want to make sure
2074 * we get the post-operation mtime and size. This means that
2075 * we can't use xdr_encode_pages() as written: we need a variant
2076 * of it which would leave room in the 'tail' iovec.
2077 *
2078 * Both of these changes to the XDR layer would in fact be quite
2079 * minor, but I decided to leave them for a subsequent patch.
2080 */
2081static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2082 unsigned int pgbase, unsigned int pglen)
2083{
2084 struct nfs4_readlink args = {
2085 .fh = NFS_FH(inode),
2086 .pgbase = pgbase,
2087 .pglen = pglen,
2088 .pages = &page,
2089 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002090 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 struct rpc_message msg = {
2092 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2093 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002094 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 };
2096
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002097 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098}
2099
2100static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2101 unsigned int pgbase, unsigned int pglen)
2102{
2103 struct nfs4_exception exception = { };
2104 int err;
2105 do {
2106 err = nfs4_handle_exception(NFS_SERVER(inode),
2107 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2108 &exception);
2109 } while (exception.retry);
2110 return err;
2111}
2112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113/*
2114 * Got race?
2115 * We will need to arrange for the VFS layer to provide an atomic open.
2116 * Until then, this create/open method is prone to inefficiency and race
2117 * conditions due to the lookup, create, and open VFS calls from sys_open()
2118 * placed on the wire.
2119 *
2120 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2121 * The file will be opened again in the subsequent VFS open call
2122 * (nfs4_proc_file_open).
2123 *
2124 * The open for read will just hang around to be used by any process that
2125 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2126 */
2127
2128static int
2129nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002130 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
Trond Myklebustad389da2007-06-05 12:30:00 -04002132 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002133 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002134 .dentry = dentry,
2135 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 struct nfs4_state *state;
2137 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002138 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 int status = 0;
2140
Trond Myklebust98a8e322008-03-12 12:25:28 -04002141 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 if (IS_ERR(cred)) {
2143 status = PTR_ERR(cred);
2144 goto out;
2145 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002146 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002147 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 if (IS_ERR(state)) {
2149 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002150 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002152 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002153 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 if (flags & O_EXCL) {
2155 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002156 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002157 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002158 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002159 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002160 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002161 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002162 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002163 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002164 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002165out_putcred:
2166 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167out:
2168 return status;
2169}
2170
2171static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2172{
Trond Myklebust16e42952005-10-27 22:12:44 -04002173 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002174 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002176 .name.len = name->len,
2177 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002178 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002180 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002181 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002182 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002184 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2185 .rpc_argp = &args,
2186 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 };
2188 int status;
2189
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002190 nfs_fattr_init(&res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002191 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002192 if (status == 0) {
2193 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002194 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 return status;
2197}
2198
2199static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2200{
2201 struct nfs4_exception exception = { };
2202 int err;
2203 do {
2204 err = nfs4_handle_exception(NFS_SERVER(dir),
2205 _nfs4_proc_remove(dir, name),
2206 &exception);
2207 } while (exception.retry);
2208 return err;
2209}
2210
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002211static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002213 struct nfs_server *server = NFS_SERVER(dir);
2214 struct nfs_removeargs *args = msg->rpc_argp;
2215 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Trond Myklebusta65318b2009-03-11 14:10:28 -04002217 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002218 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
2221
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002222static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002224 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2225
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002226 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002227 return 0;
2228 update_changeattr(dir, &res->cinfo);
2229 nfs_post_op_update_inode(dir, &res->dir_attr);
2230 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
2233static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2234 struct inode *new_dir, struct qstr *new_name)
2235{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002236 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 struct nfs4_rename_arg arg = {
2238 .old_dir = NFS_FH(old_dir),
2239 .new_dir = NFS_FH(new_dir),
2240 .old_name = old_name,
2241 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002242 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002244 struct nfs_fattr old_fattr, new_fattr;
2245 struct nfs4_rename_res res = {
2246 .server = server,
2247 .old_fattr = &old_fattr,
2248 .new_fattr = &new_fattr,
2249 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 struct rpc_message msg = {
2251 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2252 .rpc_argp = &arg,
2253 .rpc_resp = &res,
2254 };
2255 int status;
2256
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002257 nfs_fattr_init(res.old_fattr);
2258 nfs_fattr_init(res.new_fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002259 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 if (!status) {
2262 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002263 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002265 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
2267 return status;
2268}
2269
2270static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2271 struct inode *new_dir, struct qstr *new_name)
2272{
2273 struct nfs4_exception exception = { };
2274 int err;
2275 do {
2276 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2277 _nfs4_proc_rename(old_dir, old_name,
2278 new_dir, new_name),
2279 &exception);
2280 } while (exception.retry);
2281 return err;
2282}
2283
2284static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2285{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002286 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 struct nfs4_link_arg arg = {
2288 .fh = NFS_FH(inode),
2289 .dir_fh = NFS_FH(dir),
2290 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002291 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002293 struct nfs_fattr fattr, dir_attr;
2294 struct nfs4_link_res res = {
2295 .server = server,
2296 .fattr = &fattr,
2297 .dir_attr = &dir_attr,
2298 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 struct rpc_message msg = {
2300 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2301 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002302 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 };
2304 int status;
2305
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002306 nfs_fattr_init(res.fattr);
2307 nfs_fattr_init(res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002308 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002309 if (!status) {
2310 update_changeattr(dir, &res.cinfo);
2311 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002312 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 return status;
2316}
2317
2318static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2319{
2320 struct nfs4_exception exception = { };
2321 int err;
2322 do {
2323 err = nfs4_handle_exception(NFS_SERVER(inode),
2324 _nfs4_proc_link(inode, dir, name),
2325 &exception);
2326 } while (exception.retry);
2327 return err;
2328}
2329
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002330struct nfs4_createdata {
2331 struct rpc_message msg;
2332 struct nfs4_create_arg arg;
2333 struct nfs4_create_res res;
2334 struct nfs_fh fh;
2335 struct nfs_fattr fattr;
2336 struct nfs_fattr dir_fattr;
2337};
2338
2339static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2340 struct qstr *name, struct iattr *sattr, u32 ftype)
2341{
2342 struct nfs4_createdata *data;
2343
2344 data = kzalloc(sizeof(*data), GFP_KERNEL);
2345 if (data != NULL) {
2346 struct nfs_server *server = NFS_SERVER(dir);
2347
2348 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2349 data->msg.rpc_argp = &data->arg;
2350 data->msg.rpc_resp = &data->res;
2351 data->arg.dir_fh = NFS_FH(dir);
2352 data->arg.server = server;
2353 data->arg.name = name;
2354 data->arg.attrs = sattr;
2355 data->arg.ftype = ftype;
2356 data->arg.bitmask = server->attr_bitmask;
2357 data->res.server = server;
2358 data->res.fh = &data->fh;
2359 data->res.fattr = &data->fattr;
2360 data->res.dir_fattr = &data->dir_fattr;
2361 nfs_fattr_init(data->res.fattr);
2362 nfs_fattr_init(data->res.dir_fattr);
2363 }
2364 return data;
2365}
2366
2367static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2368{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002369 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2370 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002371 if (status == 0) {
2372 update_changeattr(dir, &data->res.dir_cinfo);
2373 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2374 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2375 }
2376 return status;
2377}
2378
2379static void nfs4_free_createdata(struct nfs4_createdata *data)
2380{
2381 kfree(data);
2382}
2383
Chuck Lever4f390c12006-08-22 20:06:22 -04002384static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002385 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002387 struct nfs4_createdata *data;
2388 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Chuck Lever94a6d752006-08-22 20:06:23 -04002390 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002391 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002392
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002393 status = -ENOMEM;
2394 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2395 if (data == NULL)
2396 goto out;
2397
2398 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2399 data->arg.u.symlink.pages = &page;
2400 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002402 status = nfs4_do_create(dir, dentry, data);
2403
2404 nfs4_free_createdata(data);
2405out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 return status;
2407}
2408
Chuck Lever4f390c12006-08-22 20:06:22 -04002409static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002410 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411{
2412 struct nfs4_exception exception = { };
2413 int err;
2414 do {
2415 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002416 _nfs4_proc_symlink(dir, dentry, page,
2417 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 &exception);
2419 } while (exception.retry);
2420 return err;
2421}
2422
2423static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2424 struct iattr *sattr)
2425{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002426 struct nfs4_createdata *data;
2427 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002429 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2430 if (data == NULL)
2431 goto out;
2432
2433 status = nfs4_do_create(dir, dentry, data);
2434
2435 nfs4_free_createdata(data);
2436out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return status;
2438}
2439
2440static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2441 struct iattr *sattr)
2442{
2443 struct nfs4_exception exception = { };
2444 int err;
2445 do {
2446 err = nfs4_handle_exception(NFS_SERVER(dir),
2447 _nfs4_proc_mkdir(dir, dentry, sattr),
2448 &exception);
2449 } while (exception.retry);
2450 return err;
2451}
2452
2453static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2454 u64 cookie, struct page *page, unsigned int count, int plus)
2455{
2456 struct inode *dir = dentry->d_inode;
2457 struct nfs4_readdir_arg args = {
2458 .fh = NFS_FH(dir),
2459 .pages = &page,
2460 .pgbase = 0,
2461 .count = count,
Trond Myklebusta65318b2009-03-11 14:10:28 -04002462 .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 };
2464 struct nfs4_readdir_res res;
2465 struct rpc_message msg = {
2466 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2467 .rpc_argp = &args,
2468 .rpc_resp = &res,
2469 .rpc_cred = cred,
2470 };
2471 int status;
2472
Harvey Harrison3110ff82008-05-02 13:42:44 -07002473 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002474 dentry->d_parent->d_name.name,
2475 dentry->d_name.name,
2476 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2478 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002479 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 if (status == 0)
2481 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002482
2483 nfs_invalidate_atime(dir);
2484
Harvey Harrison3110ff82008-05-02 13:42:44 -07002485 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 return status;
2487}
2488
2489static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2490 u64 cookie, struct page *page, unsigned int count, int plus)
2491{
2492 struct nfs4_exception exception = { };
2493 int err;
2494 do {
2495 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2496 _nfs4_proc_readdir(dentry, cred, cookie,
2497 page, count, plus),
2498 &exception);
2499 } while (exception.retry);
2500 return err;
2501}
2502
2503static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2504 struct iattr *sattr, dev_t rdev)
2505{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002506 struct nfs4_createdata *data;
2507 int mode = sattr->ia_mode;
2508 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
2510 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2511 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002512
2513 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2514 if (data == NULL)
2515 goto out;
2516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002518 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002520 data->arg.ftype = NF4BLK;
2521 data->arg.u.device.specdata1 = MAJOR(rdev);
2522 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 }
2524 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002525 data->arg.ftype = NF4CHR;
2526 data->arg.u.device.specdata1 = MAJOR(rdev);
2527 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002530 status = nfs4_do_create(dir, dentry, data);
2531
2532 nfs4_free_createdata(data);
2533out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 return status;
2535}
2536
2537static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2538 struct iattr *sattr, dev_t rdev)
2539{
2540 struct nfs4_exception exception = { };
2541 int err;
2542 do {
2543 err = nfs4_handle_exception(NFS_SERVER(dir),
2544 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2545 &exception);
2546 } while (exception.retry);
2547 return err;
2548}
2549
2550static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2551 struct nfs_fsstat *fsstat)
2552{
2553 struct nfs4_statfs_arg args = {
2554 .fh = fhandle,
2555 .bitmask = server->attr_bitmask,
2556 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002557 struct nfs4_statfs_res res = {
2558 .fsstat = fsstat,
2559 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 struct rpc_message msg = {
2561 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2562 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002563 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 };
2565
Trond Myklebust0e574af2005-10-27 22:12:38 -04002566 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002567 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}
2569
2570static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2571{
2572 struct nfs4_exception exception = { };
2573 int err;
2574 do {
2575 err = nfs4_handle_exception(server,
2576 _nfs4_proc_statfs(server, fhandle, fsstat),
2577 &exception);
2578 } while (exception.retry);
2579 return err;
2580}
2581
2582static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2583 struct nfs_fsinfo *fsinfo)
2584{
2585 struct nfs4_fsinfo_arg args = {
2586 .fh = fhandle,
2587 .bitmask = server->attr_bitmask,
2588 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002589 struct nfs4_fsinfo_res res = {
2590 .fsinfo = fsinfo,
2591 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 struct rpc_message msg = {
2593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2594 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002595 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 };
2597
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002598 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599}
2600
2601static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2602{
2603 struct nfs4_exception exception = { };
2604 int err;
2605
2606 do {
2607 err = nfs4_handle_exception(server,
2608 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2609 &exception);
2610 } while (exception.retry);
2611 return err;
2612}
2613
2614static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2615{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002616 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2618}
2619
2620static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2621 struct nfs_pathconf *pathconf)
2622{
2623 struct nfs4_pathconf_arg args = {
2624 .fh = fhandle,
2625 .bitmask = server->attr_bitmask,
2626 };
Benny Halevyd45b2982009-04-01 09:21:58 -04002627 struct nfs4_pathconf_res res = {
2628 .pathconf = pathconf,
2629 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 struct rpc_message msg = {
2631 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2632 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04002633 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 };
2635
2636 /* None of the pathconf attributes are mandatory to implement */
2637 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2638 memset(pathconf, 0, sizeof(*pathconf));
2639 return 0;
2640 }
2641
Trond Myklebust0e574af2005-10-27 22:12:38 -04002642 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002643 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644}
2645
2646static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2647 struct nfs_pathconf *pathconf)
2648{
2649 struct nfs4_exception exception = { };
2650 int err;
2651
2652 do {
2653 err = nfs4_handle_exception(server,
2654 _nfs4_proc_pathconf(server, fhandle, pathconf),
2655 &exception);
2656 } while (exception.retry);
2657 return err;
2658}
2659
Trond Myklebustec06c092006-03-20 13:44:27 -05002660static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
Trond Myklebustec06c092006-03-20 13:44:27 -05002662 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002664 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 rpc_restart_call(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05002666 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 }
Trond Myklebust8850df92007-09-28 17:20:07 -04002668
2669 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002671 renew_lease(server, data->timestamp);
2672 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673}
2674
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002675static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002678 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679}
2680
Trond Myklebust788e7a82006-03-20 13:44:27 -05002681static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 struct inode *inode = data->inode;
2684
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002685 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002687 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002689 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04002691 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002692 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05002693 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694}
2695
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002696static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002698 struct nfs_server *server = NFS_SERVER(data->inode);
2699
Trond Myklebusta65318b2009-03-11 14:10:28 -04002700 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002701 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 data->timestamp = jiffies;
2703
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002704 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705}
2706
Trond Myklebust788e7a82006-03-20 13:44:27 -05002707static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 struct inode *inode = data->inode;
2710
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002711 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002713 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04002715 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002716 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717}
2718
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002719static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720{
Trond Myklebust788e7a82006-03-20 13:44:27 -05002721 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Trond Myklebusta65318b2009-03-11 14:10:28 -04002723 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002724 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002725 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726}
2727
2728/*
2729 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2730 * standalone procedure for queueing an asynchronous RENEW.
2731 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002732static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733{
David Howellsadfa6f92006-08-22 20:06:08 -04002734 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002735 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
2737 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04002738 /* Unless we're shutting down, schedule state recovery! */
2739 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
2740 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 return;
2742 }
2743 spin_lock(&clp->cl_lock);
2744 if (time_before(clp->cl_last_renewal,timestamp))
2745 clp->cl_last_renewal = timestamp;
2746 spin_unlock(&clp->cl_lock);
2747}
2748
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002749static const struct rpc_call_ops nfs4_renew_ops = {
2750 .rpc_call_done = nfs4_renew_done,
2751};
2752
David Howellsadfa6f92006-08-22 20:06:08 -04002753int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
2755 struct rpc_message msg = {
2756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2757 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002758 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 };
2760
2761 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002762 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763}
2764
David Howellsadfa6f92006-08-22 20:06:08 -04002765int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
2767 struct rpc_message msg = {
2768 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2769 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002770 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 };
2772 unsigned long now = jiffies;
2773 int status;
2774
2775 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2776 if (status < 0)
2777 return status;
2778 spin_lock(&clp->cl_lock);
2779 if (time_before(clp->cl_last_renewal,now))
2780 clp->cl_last_renewal = now;
2781 spin_unlock(&clp->cl_lock);
2782 return 0;
2783}
2784
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002785static inline int nfs4_server_supports_acls(struct nfs_server *server)
2786{
2787 return (server->caps & NFS_CAP_ACLS)
2788 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2789 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2790}
2791
2792/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2793 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2794 * the stack.
2795 */
2796#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2797
2798static void buf_to_pages(const void *buf, size_t buflen,
2799 struct page **pages, unsigned int *pgbase)
2800{
2801 const void *p = buf;
2802
2803 *pgbase = offset_in_page(buf);
2804 p -= *pgbase;
2805 while (p < buf + buflen) {
2806 *(pages++) = virt_to_page(p);
2807 p += PAGE_CACHE_SIZE;
2808 }
2809}
2810
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002811struct nfs4_cached_acl {
2812 int cached;
2813 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002814 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002815};
2816
2817static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002818{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002819 struct nfs_inode *nfsi = NFS_I(inode);
2820
2821 spin_lock(&inode->i_lock);
2822 kfree(nfsi->nfs4_acl);
2823 nfsi->nfs4_acl = acl;
2824 spin_unlock(&inode->i_lock);
2825}
2826
2827static void nfs4_zap_acl_attr(struct inode *inode)
2828{
2829 nfs4_set_cached_acl(inode, NULL);
2830}
2831
2832static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2833{
2834 struct nfs_inode *nfsi = NFS_I(inode);
2835 struct nfs4_cached_acl *acl;
2836 int ret = -ENOENT;
2837
2838 spin_lock(&inode->i_lock);
2839 acl = nfsi->nfs4_acl;
2840 if (acl == NULL)
2841 goto out;
2842 if (buf == NULL) /* user is just asking for length */
2843 goto out_len;
2844 if (acl->cached == 0)
2845 goto out;
2846 ret = -ERANGE; /* see getxattr(2) man page */
2847 if (acl->len > buflen)
2848 goto out;
2849 memcpy(buf, acl->data, acl->len);
2850out_len:
2851 ret = acl->len;
2852out:
2853 spin_unlock(&inode->i_lock);
2854 return ret;
2855}
2856
2857static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2858{
2859 struct nfs4_cached_acl *acl;
2860
2861 if (buf && acl_len <= PAGE_SIZE) {
2862 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2863 if (acl == NULL)
2864 goto out;
2865 acl->cached = 1;
2866 memcpy(acl->data, buf, acl_len);
2867 } else {
2868 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2869 if (acl == NULL)
2870 goto out;
2871 acl->cached = 0;
2872 }
2873 acl->len = acl_len;
2874out:
2875 nfs4_set_cached_acl(inode, acl);
2876}
2877
Trond Myklebust16b42892006-08-24 12:27:15 -04002878static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002879{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002880 struct page *pages[NFS4ACL_MAXPAGES];
2881 struct nfs_getaclargs args = {
2882 .fh = NFS_FH(inode),
2883 .acl_pages = pages,
2884 .acl_len = buflen,
2885 };
Benny Halevy663c79b2009-04-01 09:21:59 -04002886 struct nfs_getaclres res = {
2887 .acl_len = buflen,
2888 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002889 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002890 struct rpc_message msg = {
2891 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2892 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04002893 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002894 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002895 struct page *localpage = NULL;
2896 int ret;
2897
2898 if (buflen < PAGE_SIZE) {
2899 /* As long as we're doing a round trip to the server anyway,
2900 * let's be prepared for a page of acl data. */
2901 localpage = alloc_page(GFP_KERNEL);
2902 resp_buf = page_address(localpage);
2903 if (localpage == NULL)
2904 return -ENOMEM;
2905 args.acl_pages[0] = localpage;
2906 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04002907 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002908 } else {
2909 resp_buf = buf;
2910 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2911 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002912 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002913 if (ret)
2914 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04002915 if (res.acl_len > args.acl_len)
2916 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002917 else
Benny Halevy663c79b2009-04-01 09:21:59 -04002918 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002919 if (buf) {
2920 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04002921 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002922 goto out_free;
2923 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04002924 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002925 }
Benny Halevy663c79b2009-04-01 09:21:59 -04002926 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002927out_free:
2928 if (localpage)
2929 __free_page(localpage);
2930 return ret;
2931}
2932
Trond Myklebust16b42892006-08-24 12:27:15 -04002933static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2934{
2935 struct nfs4_exception exception = { };
2936 ssize_t ret;
2937 do {
2938 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
2939 if (ret >= 0)
2940 break;
2941 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
2942 } while (exception.retry);
2943 return ret;
2944}
2945
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002946static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2947{
2948 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002949 int ret;
2950
2951 if (!nfs4_server_supports_acls(server))
2952 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002953 ret = nfs_revalidate_inode(server, inode);
2954 if (ret < 0)
2955 return ret;
Trond Myklebustf41f7412008-06-11 17:39:04 -04002956 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
2957 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002958 ret = nfs4_read_cached_acl(inode, buf, buflen);
2959 if (ret != -ENOENT)
2960 return ret;
2961 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002962}
2963
Trond Myklebust16b42892006-08-24 12:27:15 -04002964static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002965{
2966 struct nfs_server *server = NFS_SERVER(inode);
2967 struct page *pages[NFS4ACL_MAXPAGES];
2968 struct nfs_setaclargs arg = {
2969 .fh = NFS_FH(inode),
2970 .acl_pages = pages,
2971 .acl_len = buflen,
2972 };
Benny Halevy73c403a2009-04-01 09:22:01 -04002973 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002974 struct rpc_message msg = {
2975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2976 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04002977 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002978 };
2979 int ret;
2980
2981 if (!nfs4_server_supports_acls(server))
2982 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07002983 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002984 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002985 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04002986 nfs_access_zap_cache(inode);
2987 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002988 return ret;
2989}
2990
Trond Myklebust16b42892006-08-24 12:27:15 -04002991static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2992{
2993 struct nfs4_exception exception = { };
2994 int err;
2995 do {
2996 err = nfs4_handle_exception(NFS_SERVER(inode),
2997 __nfs4_proc_set_acl(inode, buf, buflen),
2998 &exception);
2999 } while (exception.retry);
3000 return err;
3001}
3002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003static int
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003004nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
David Howells7539bba2006-08-22 20:06:09 -04003006 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
3008 if (!clp || task->tk_status >= 0)
3009 return 0;
3010 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003011 case -NFS4ERR_ADMIN_REVOKED:
3012 case -NFS4ERR_BAD_STATEID:
3013 case -NFS4ERR_OPENMODE:
3014 if (state == NULL)
3015 break;
3016 nfs4_state_mark_reclaim_nograce(clp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 case -NFS4ERR_STALE_CLIENTID:
3018 case -NFS4ERR_STALE_STATEID:
3019 case -NFS4ERR_EXPIRED:
Trond Myklebust5d008372008-02-22 16:34:17 -05003020 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 nfs4_schedule_state_recovery(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05003022 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
Trond Myklebustfda13932008-02-22 16:34:12 -05003023 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 task->tk_status = 0;
3025 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 case -NFS4ERR_DELAY:
Trond Myklebust2e96d282008-06-11 16:42:05 -04003027 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003028 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3030 task->tk_status = 0;
3031 return -EAGAIN;
3032 case -NFS4ERR_OLD_STATEID:
3033 task->tk_status = 0;
3034 return -EAGAIN;
3035 }
3036 task->tk_status = nfs4_map_errors(task->tk_status);
3037 return 0;
3038}
3039
David Howellsadfa6f92006-08-22 20:06:08 -04003040int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041{
3042 nfs4_verifier sc_verifier;
3043 struct nfs4_setclientid setclientid = {
3044 .sc_verifier = &sc_verifier,
3045 .sc_prog = program,
3046 };
3047 struct rpc_message msg = {
3048 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3049 .rpc_argp = &setclientid,
3050 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003051 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 };
Al Virobc4785c2006-10-19 23:28:51 -07003053 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 int loop = 0;
3055 int status;
3056
Al Virobc4785c2006-10-19 23:28:51 -07003057 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3059 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3060
3061 for(;;) {
3062 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003063 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003064 clp->cl_ipaddr,
3065 rpc_peeraddr2str(clp->cl_rpcclient,
3066 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003067 rpc_peeraddr2str(clp->cl_rpcclient,
3068 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003069 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 clp->cl_id_uniquifier);
3071 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003072 sizeof(setclientid.sc_netid),
3073 rpc_peeraddr2str(clp->cl_rpcclient,
3074 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003076 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 clp->cl_ipaddr, port >> 8, port & 255);
3078
3079 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3080 if (status != -NFS4ERR_CLID_INUSE)
3081 break;
3082 if (signalled())
3083 break;
3084 if (loop++ & 1)
3085 ssleep(clp->cl_lease_time + 1);
3086 else
3087 if (++clp->cl_id_uniquifier == 0)
3088 break;
3089 }
3090 return status;
3091}
3092
David Howellsadfa6f92006-08-22 20:06:08 -04003093static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
3095 struct nfs_fsinfo fsinfo;
3096 struct rpc_message msg = {
3097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3098 .rpc_argp = clp,
3099 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003100 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 };
3102 unsigned long now;
3103 int status;
3104
3105 now = jiffies;
3106 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3107 if (status == 0) {
3108 spin_lock(&clp->cl_lock);
3109 clp->cl_lease_time = fsinfo.lease_time * HZ;
3110 clp->cl_last_renewal = now;
3111 spin_unlock(&clp->cl_lock);
3112 }
3113 return status;
3114}
3115
David Howellsadfa6f92006-08-22 20:06:08 -04003116int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003117{
Benny Halevy77e03672008-06-24 20:25:57 +03003118 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003119 int err;
3120 do {
3121 err = _nfs4_proc_setclientid_confirm(clp, cred);
3122 switch (err) {
3123 case 0:
3124 return err;
3125 case -NFS4ERR_RESOURCE:
3126 /* The IBM lawyers misread another document! */
3127 case -NFS4ERR_DELAY:
3128 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3129 }
3130 } while (err == 0);
3131 return err;
3132}
3133
Trond Myklebustfe650402006-01-03 09:55:18 +01003134struct nfs4_delegreturndata {
3135 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003136 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003137 struct nfs_fh fh;
3138 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003139 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003140 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003141 int rpc_status;
3142};
3143
Trond Myklebustfe650402006-01-03 09:55:18 +01003144static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3145{
3146 struct nfs4_delegreturndata *data = calldata;
3147 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003148 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01003149 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01003150}
3151
3152static void nfs4_delegreturn_release(void *calldata)
3153{
Trond Myklebustfe650402006-01-03 09:55:18 +01003154 kfree(calldata);
3155}
3156
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003157static const struct rpc_call_ops nfs4_delegreturn_ops = {
Trond Myklebustfe650402006-01-03 09:55:18 +01003158 .rpc_call_done = nfs4_delegreturn_done,
3159 .rpc_release = nfs4_delegreturn_release,
3160};
3161
Trond Myklebuste6f81072008-01-24 18:14:34 -05003162static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003163{
3164 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003165 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003166 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003167 struct rpc_message msg = {
3168 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3169 .rpc_cred = cred,
3170 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003171 struct rpc_task_setup task_setup_data = {
3172 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003173 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003174 .callback_ops = &nfs4_delegreturn_ops,
3175 .flags = RPC_TASK_ASYNC,
3176 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003177 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003178
3179 data = kmalloc(sizeof(*data), GFP_KERNEL);
3180 if (data == NULL)
3181 return -ENOMEM;
3182 data->args.fhandle = &data->fh;
3183 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003184 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003185 nfs_copy_fh(&data->fh, NFS_FH(inode));
3186 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003187 data->res.fattr = &data->fattr;
3188 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003189 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003190 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003191 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003192 data->rpc_status = 0;
3193
Trond Myklebustc970aa82007-07-14 15:39:59 -04003194 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003195 msg.rpc_argp = &data->args,
3196 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003197 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003198 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003199 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003200 if (!issync)
3201 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003202 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003203 if (status != 0)
3204 goto out;
3205 status = data->rpc_status;
3206 if (status != 0)
3207 goto out;
3208 nfs_refresh_inode(inode, &data->fattr);
3209out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003210 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003211 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212}
3213
Trond Myklebuste6f81072008-01-24 18:14:34 -05003214int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215{
3216 struct nfs_server *server = NFS_SERVER(inode);
3217 struct nfs4_exception exception = { };
3218 int err;
3219 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003220 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 switch (err) {
3222 case -NFS4ERR_STALE_STATEID:
3223 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 case 0:
3225 return 0;
3226 }
3227 err = nfs4_handle_exception(server, err, &exception);
3228 } while (exception.retry);
3229 return err;
3230}
3231
3232#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3233#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3234
3235/*
3236 * sleep, with exponential backoff, and retry the LOCK operation.
3237 */
3238static unsigned long
3239nfs4_set_lock_task_retry(unsigned long timeout)
3240{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003241 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 timeout <<= 1;
3243 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3244 return NFS4_LOCK_MAXTIMEOUT;
3245 return timeout;
3246}
3247
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3249{
3250 struct inode *inode = state->inode;
3251 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003252 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003253 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003255 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003257 struct nfs_lockt_res res = {
3258 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 };
3260 struct rpc_message msg = {
3261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3262 .rpc_argp = &arg,
3263 .rpc_resp = &res,
3264 .rpc_cred = state->owner->so_cred,
3265 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 struct nfs4_lock_state *lsp;
3267 int status;
3268
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003269 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003270 status = nfs4_set_lock_state(state, request);
3271 if (status != 0)
3272 goto out;
3273 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003274 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003275 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003276 switch (status) {
3277 case 0:
3278 request->fl_type = F_UNLCK;
3279 break;
3280 case -NFS4ERR_DENIED:
3281 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003283 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003284out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 return status;
3286}
3287
3288static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3289{
3290 struct nfs4_exception exception = { };
3291 int err;
3292
3293 do {
3294 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3295 _nfs4_proc_getlk(state, cmd, request),
3296 &exception);
3297 } while (exception.retry);
3298 return err;
3299}
3300
3301static int do_vfs_lock(struct file *file, struct file_lock *fl)
3302{
3303 int res = 0;
3304 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3305 case FL_POSIX:
3306 res = posix_lock_file_wait(file, fl);
3307 break;
3308 case FL_FLOCK:
3309 res = flock_lock_file_wait(file, fl);
3310 break;
3311 default:
3312 BUG();
3313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 return res;
3315}
3316
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003317struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003318 struct nfs_locku_args arg;
3319 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003320 struct nfs4_lock_state *lsp;
3321 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003322 struct file_lock fl;
3323 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003324 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003325};
3326
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003327static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3328 struct nfs_open_context *ctx,
3329 struct nfs4_lock_state *lsp,
3330 struct nfs_seqid *seqid)
3331{
3332 struct nfs4_unlockdata *p;
3333 struct inode *inode = lsp->ls_state->inode;
3334
3335 p = kmalloc(sizeof(*p), GFP_KERNEL);
3336 if (p == NULL)
3337 return NULL;
3338 p->arg.fh = NFS_FH(inode);
3339 p->arg.fl = &p->fl;
3340 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003341 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003342 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003343 p->arg.stateid = &lsp->ls_stateid;
3344 p->lsp = lsp;
3345 atomic_inc(&lsp->ls_count);
3346 /* Ensure we don't close file until we're done freeing locks! */
3347 p->ctx = get_nfs_open_context(ctx);
3348 memcpy(&p->fl, fl, sizeof(p->fl));
3349 p->server = NFS_SERVER(inode);
3350 return p;
3351}
3352
Trond Myklebust06f814a2006-01-03 09:55:07 +01003353static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003354{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003355 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003356 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003357 nfs4_put_lock_state(calldata->lsp);
3358 put_nfs_open_context(calldata->ctx);
3359 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003360}
3361
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003362static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003363{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003364 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003365
Trond Myklebust06f814a2006-01-03 09:55:07 +01003366 if (RPC_ASSASSINATED(task))
3367 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003368 switch (task->tk_status) {
3369 case 0:
3370 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003371 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003372 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003373 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003374 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003375 case -NFS4ERR_BAD_STATEID:
3376 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003377 case -NFS4ERR_STALE_STATEID:
3378 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003379 break;
3380 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003381 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003382 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003383 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003384}
3385
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003386static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003387{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003388 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003390 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003391 return;
3392 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003393 /* Note: exit _without_ running nfs4_locku_done */
3394 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003395 return;
3396 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003397 calldata->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003398 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399}
3400
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003401static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003402 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003403 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003404 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003405};
3406
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003407static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3408 struct nfs_open_context *ctx,
3409 struct nfs4_lock_state *lsp,
3410 struct nfs_seqid *seqid)
3411{
3412 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003413 struct rpc_message msg = {
3414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3415 .rpc_cred = ctx->cred,
3416 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003417 struct rpc_task_setup task_setup_data = {
3418 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003419 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003420 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003421 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003422 .flags = RPC_TASK_ASYNC,
3423 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003424
Frank Filz137d6ac2007-07-09 15:32:29 -07003425 /* Ensure this is an unlock - when canceling a lock, the
3426 * canceled lock is passed in, and it won't be an unlock.
3427 */
3428 fl->fl_type = F_UNLCK;
3429
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003430 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3431 if (data == NULL) {
3432 nfs_free_seqid(seqid);
3433 return ERR_PTR(-ENOMEM);
3434 }
3435
Trond Myklebust5138fde2007-07-14 15:40:01 -04003436 msg.rpc_argp = &data->arg,
3437 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003438 task_setup_data.callback_data = data;
3439 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003440}
3441
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3443{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003444 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003445 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003446 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003447 struct rpc_task *task;
3448 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003449 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
Trond Myklebust9b073572006-06-29 16:38:34 -04003451 status = nfs4_set_lock_state(state, request);
3452 /* Unlock _before_ we do the RPC call */
3453 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003454 down_read(&nfsi->rwsem);
3455 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3456 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003457 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003458 }
3459 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003460 if (status != 0)
3461 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003462 /* Is this a delegated lock? */
3463 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003464 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003465 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003466 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003467 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003468 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003469 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003470 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003471 status = PTR_ERR(task);
3472 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003473 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003474 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003475 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003476out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003477 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003478 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479}
3480
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003481struct nfs4_lockdata {
3482 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003483 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003484 struct nfs4_lock_state *lsp;
3485 struct nfs_open_context *ctx;
3486 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003487 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003488 int rpc_status;
3489 int cancelled;
3490};
3491
3492static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3493 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3494{
3495 struct nfs4_lockdata *p;
3496 struct inode *inode = lsp->ls_state->inode;
3497 struct nfs_server *server = NFS_SERVER(inode);
3498
3499 p = kzalloc(sizeof(*p), GFP_KERNEL);
3500 if (p == NULL)
3501 return NULL;
3502
3503 p->arg.fh = NFS_FH(inode);
3504 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003505 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3506 if (p->arg.open_seqid == NULL)
3507 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003508 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3509 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003510 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003511 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003512 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003513 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003514 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003515 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003516 p->lsp = lsp;
3517 atomic_inc(&lsp->ls_count);
3518 p->ctx = get_nfs_open_context(ctx);
3519 memcpy(&p->fl, fl, sizeof(p->fl));
3520 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003521out_free_seqid:
3522 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003523out_free:
3524 kfree(p);
3525 return NULL;
3526}
3527
3528static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3529{
3530 struct nfs4_lockdata *data = calldata;
3531 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
Harvey Harrison3110ff82008-05-02 13:42:44 -07003533 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003534 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3535 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003536 /* Do we need to do an open_to_lock_owner? */
3537 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05003538 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3539 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003540 data->arg.open_stateid = &state->stateid;
3541 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003542 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003543 } else
3544 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003545 data->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003546 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003547 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003548}
3549
3550static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3551{
3552 struct nfs4_lockdata *data = calldata;
3553
Harvey Harrison3110ff82008-05-02 13:42:44 -07003554 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003555
3556 data->rpc_status = task->tk_status;
3557 if (RPC_ASSASSINATED(task))
3558 goto out;
3559 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003560 if (data->rpc_status == 0)
3561 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3562 else
3563 goto out;
3564 }
3565 if (data->rpc_status == 0) {
3566 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3567 sizeof(data->lsp->ls_stateid.data));
3568 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003569 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003570 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003571out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07003572 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003573}
3574
3575static void nfs4_lock_release(void *calldata)
3576{
3577 struct nfs4_lockdata *data = calldata;
3578
Harvey Harrison3110ff82008-05-02 13:42:44 -07003579 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003580 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003581 if (data->cancelled != 0) {
3582 struct rpc_task *task;
3583 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3584 data->arg.lock_seqid);
3585 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003586 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003587 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003588 } else
3589 nfs_free_seqid(data->arg.lock_seqid);
3590 nfs4_put_lock_state(data->lsp);
3591 put_nfs_open_context(data->ctx);
3592 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003593 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003594}
3595
3596static const struct rpc_call_ops nfs4_lock_ops = {
3597 .rpc_call_prepare = nfs4_lock_prepare,
3598 .rpc_call_done = nfs4_lock_done,
3599 .rpc_release = nfs4_lock_release,
3600};
3601
3602static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3603{
3604 struct nfs4_lockdata *data;
3605 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003606 struct rpc_message msg = {
3607 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3608 .rpc_cred = state->owner->so_cred,
3609 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003610 struct rpc_task_setup task_setup_data = {
3611 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003612 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003613 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003614 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003615 .flags = RPC_TASK_ASYNC,
3616 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003617 int ret;
3618
Harvey Harrison3110ff82008-05-02 13:42:44 -07003619 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003620 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003621 fl->fl_u.nfs4_fl.owner);
3622 if (data == NULL)
3623 return -ENOMEM;
3624 if (IS_SETLKW(cmd))
3625 data->arg.block = 1;
3626 if (reclaim != 0)
3627 data->arg.reclaim = 1;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003628 msg.rpc_argp = &data->arg,
3629 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003630 task_setup_data.callback_data = data;
3631 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003632 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003633 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003634 ret = nfs4_wait_for_completion_rpc_task(task);
3635 if (ret == 0) {
3636 ret = data->rpc_status;
3637 if (ret == -NFS4ERR_DENIED)
3638 ret = -EAGAIN;
3639 } else
3640 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003641 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003642 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003643 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644}
3645
3646static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3647{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003648 struct nfs_server *server = NFS_SERVER(state->inode);
3649 struct nfs4_exception exception = { };
3650 int err;
3651
3652 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003653 /* Cache the lock if possible... */
3654 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3655 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003656 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3657 if (err != -NFS4ERR_DELAY)
3658 break;
3659 nfs4_handle_exception(server, err, &exception);
3660 } while (exception.retry);
3661 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662}
3663
3664static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3665{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003666 struct nfs_server *server = NFS_SERVER(state->inode);
3667 struct nfs4_exception exception = { };
3668 int err;
3669
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003670 err = nfs4_set_lock_state(state, request);
3671 if (err != 0)
3672 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003673 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003674 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3675 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003676 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3677 if (err != -NFS4ERR_DELAY)
3678 break;
3679 nfs4_handle_exception(server, err, &exception);
3680 } while (exception.retry);
3681 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682}
3683
3684static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3685{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003686 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003687 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 int status;
3689
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003690 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003691 status = nfs4_set_lock_state(state, request);
3692 if (status != 0)
3693 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003694 request->fl_flags |= FL_ACCESS;
3695 status = do_vfs_lock(request->fl_file, request);
3696 if (status < 0)
3697 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003698 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003699 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04003700 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003701 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05003702 request->fl_flags = fl_flags & ~FL_SLEEP;
3703 status = do_vfs_lock(request->fl_file, request);
3704 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003705 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003706 status = _nfs4_do_setlk(state, cmd, request, 0);
3707 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04003708 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003709 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003710 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003711 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07003712 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003713out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05003714 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003715out:
3716 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 return status;
3718}
3719
3720static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3721{
3722 struct nfs4_exception exception = { };
3723 int err;
3724
3725 do {
3726 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3727 _nfs4_proc_setlk(state, cmd, request),
3728 &exception);
3729 } while (exception.retry);
3730 return err;
3731}
3732
3733static int
3734nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3735{
3736 struct nfs_open_context *ctx;
3737 struct nfs4_state *state;
3738 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3739 int status;
3740
3741 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003742 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 state = ctx->state;
3744
3745 if (request->fl_start < 0 || request->fl_end < 0)
3746 return -EINVAL;
3747
3748 if (IS_GETLK(cmd))
3749 return nfs4_proc_getlk(state, F_GETLK, request);
3750
3751 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3752 return -EINVAL;
3753
3754 if (request->fl_type == F_UNLCK)
3755 return nfs4_proc_unlck(state, cmd, request);
3756
3757 do {
3758 status = nfs4_proc_setlk(state, cmd, request);
3759 if ((status != -EAGAIN) || IS_SETLK(cmd))
3760 break;
3761 timeout = nfs4_set_lock_task_retry(timeout);
3762 status = -ERESTARTSYS;
3763 if (signalled())
3764 break;
3765 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 return status;
3767}
3768
Trond Myklebust888e6942005-11-04 15:38:11 -05003769int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3770{
3771 struct nfs_server *server = NFS_SERVER(state->inode);
3772 struct nfs4_exception exception = { };
3773 int err;
3774
3775 err = nfs4_set_lock_state(state, fl);
3776 if (err != 0)
3777 goto out;
3778 do {
3779 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3780 if (err != -NFS4ERR_DELAY)
3781 break;
3782 err = nfs4_handle_exception(server, err, &exception);
3783 } while (exception.retry);
3784out:
3785 return err;
3786}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003787
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003788#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3789
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003790int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3791 size_t buflen, int flags)
3792{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003793 struct inode *inode = dentry->d_inode;
3794
3795 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3796 return -EOPNOTSUPP;
3797
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003798 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003799}
3800
3801/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3802 * and that's what we'll do for e.g. user attributes that haven't been set.
3803 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3804 * attributes in kernel-managed attribute namespaces. */
3805ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3806 size_t buflen)
3807{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003808 struct inode *inode = dentry->d_inode;
3809
3810 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3811 return -EOPNOTSUPP;
3812
3813 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003814}
3815
3816ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3817{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003818 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003819
J. Bruce Fields096455a2006-03-20 23:23:42 -05003820 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3821 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003822 if (buf && buflen < len)
3823 return -ERANGE;
3824 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003825 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3826 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003827}
3828
Trond Myklebust69aaaae2009-03-11 14:10:28 -04003829static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
3830{
3831 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
3832 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
3833 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
3834 return;
3835
3836 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3837 NFS_ATTR_FATTR_NLINK;
3838 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3839 fattr->nlink = 2;
3840}
3841
Trond Myklebust56659e92007-07-17 21:52:39 -04003842int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003843 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04003844{
3845 struct nfs_server *server = NFS_SERVER(dir);
3846 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04003847 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3848 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003849 };
3850 struct nfs4_fs_locations_arg args = {
3851 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05003852 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003853 .page = page,
3854 .bitmask = bitmask,
3855 };
Benny Halevy22958462009-04-01 09:22:02 -04003856 struct nfs4_fs_locations_res res = {
3857 .fs_locations = fs_locations,
3858 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04003859 struct rpc_message msg = {
3860 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3861 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04003862 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003863 };
3864 int status;
3865
Harvey Harrison3110ff82008-05-02 13:42:44 -07003866 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05003867 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04003868 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04003869 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003870 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04003871 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003872 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04003873 return status;
3874}
3875
Andy Adamson557134a2009-04-01 09:21:53 -04003876#ifdef CONFIG_NFS_V4_1
3877/* Destroy the slot table */
3878static void nfs4_destroy_slot_table(struct nfs4_session *session)
3879{
3880 if (session->fc_slot_table.slots == NULL)
3881 return;
3882 kfree(session->fc_slot_table.slots);
3883 session->fc_slot_table.slots = NULL;
3884 return;
3885}
3886
3887struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
3888{
3889 struct nfs4_session *session;
3890 struct nfs4_slot_table *tbl;
3891
3892 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
3893 if (!session)
3894 return NULL;
3895 tbl = &session->fc_slot_table;
3896 spin_lock_init(&tbl->slot_tbl_lock);
3897 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "Slot table");
3898 session->clp = clp;
3899 return session;
3900}
3901
3902void nfs4_destroy_session(struct nfs4_session *session)
3903{
3904 nfs4_destroy_slot_table(session);
3905 kfree(session);
3906}
3907
3908#endif /* CONFIG_NFS_V4_1 */
3909
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05003911 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05003912 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 .recover_open = nfs4_open_reclaim,
3914 .recover_lock = nfs4_lock_reclaim,
3915};
3916
Trond Myklebustb79a4a12008-12-23 15:21:41 -05003917struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05003918 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05003919 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 .recover_open = nfs4_open_expired,
3921 .recover_lock = nfs4_lock_expired,
3922};
3923
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003924static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003925 .permission = nfs_permission,
3926 .getattr = nfs_getattr,
3927 .setattr = nfs_setattr,
3928 .getxattr = nfs4_getxattr,
3929 .setxattr = nfs4_setxattr,
3930 .listxattr = nfs4_listxattr,
3931};
3932
David Howells509de812006-08-22 20:06:11 -04003933const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 .version = 4, /* protocol version */
3935 .dentry_ops = &nfs4_dentry_operations,
3936 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003937 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 .getroot = nfs4_proc_get_root,
3939 .getattr = nfs4_proc_getattr,
3940 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04003941 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 .lookup = nfs4_proc_lookup,
3943 .access = nfs4_proc_access,
3944 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 .create = nfs4_proc_create,
3946 .remove = nfs4_proc_remove,
3947 .unlink_setup = nfs4_proc_unlink_setup,
3948 .unlink_done = nfs4_proc_unlink_done,
3949 .rename = nfs4_proc_rename,
3950 .link = nfs4_proc_link,
3951 .symlink = nfs4_proc_symlink,
3952 .mkdir = nfs4_proc_mkdir,
3953 .rmdir = nfs4_proc_remove,
3954 .readdir = nfs4_proc_readdir,
3955 .mknod = nfs4_proc_mknod,
3956 .statfs = nfs4_proc_statfs,
3957 .fsinfo = nfs4_proc_fsinfo,
3958 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04003959 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 .decode_dirent = nfs4_decode_dirent,
3961 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05003962 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003964 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003966 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003968 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003969 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970};
3971
3972/*
3973 * Local variables:
3974 * c-basic-offset: 8
3975 * End:
3976 */