blob: a0946a0d116e63017bfee91aeb5a21f00d8db468 [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
Benny Halevy510b8172009-04-01 09:22:14 -0400276/*
277 * nfs4_find_slot - efficiently look for a free slot
278 *
279 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
280 * If found, we mark the slot as used, update the highest_used_slotid,
281 * and respectively set up the sequence operation args.
282 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
283 */
284static u8
285nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
286{
287 int slotid;
288 u8 ret_id = NFS4_MAX_SLOT_TABLE;
289 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
290
291 spin_lock(&tbl->slot_tbl_lock);
292 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
293 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
294 tbl->max_slots);
295 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
296 if (slotid >= tbl->max_slots)
297 goto out;
298 __set_bit(slotid, tbl->used_slots);
299 if (slotid > tbl->highest_used_slotid)
300 tbl->highest_used_slotid = slotid;
301 ret_id = slotid;
302out:
303 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
304 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
305 spin_unlock(&tbl->slot_tbl_lock);
306 return ret_id;
307}
308
Andy Adamsonce5039c2009-04-01 09:22:13 -0400309static int nfs41_setup_sequence(struct nfs4_session *session,
310 struct nfs4_sequence_args *args,
311 struct nfs4_sequence_res *res,
312 int cache_reply,
313 struct rpc_task *task)
314{
315 /* slot already allocated? */
316 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
317 return 0;
318
319 memset(res, 0, sizeof(*res));
320 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
321 return 0;
322}
323
324int nfs4_setup_sequence(struct nfs_client *clp,
325 struct nfs4_sequence_args *args,
326 struct nfs4_sequence_res *res,
327 int cache_reply,
328 struct rpc_task *task)
329{
330 int ret = 0;
331
332 dprintk("--> %s clp %p session %p sr_slotid %d\n",
333 __func__, clp, clp->cl_session, res->sr_slotid);
334
335 if (!nfs4_has_session(clp))
336 goto out;
337 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
338 task);
339 if (ret != -EAGAIN) {
340 /* terminate rpc task */
341 task->tk_status = ret;
342 task->tk_action = NULL;
343 }
344out:
345 dprintk("<-- %s status=%d\n", __func__, ret);
346 return ret;
347}
348
349struct nfs41_call_sync_data {
350 struct nfs_client *clp;
351 struct nfs4_sequence_args *seq_args;
352 struct nfs4_sequence_res *seq_res;
353 int cache_reply;
354};
355
356static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
357{
358 struct nfs41_call_sync_data *data = calldata;
359
360 dprintk("--> %s data->clp->cl_session %p\n", __func__,
361 data->clp->cl_session);
362 if (nfs4_setup_sequence(data->clp, data->seq_args,
363 data->seq_res, data->cache_reply, task))
364 return;
365 rpc_call_start(task);
366}
367
368struct rpc_call_ops nfs41_call_sync_ops = {
369 .rpc_call_prepare = nfs41_call_sync_prepare,
370};
371
372static int nfs4_call_sync_sequence(struct nfs_client *clp,
373 struct rpc_clnt *clnt,
374 struct rpc_message *msg,
375 struct nfs4_sequence_args *args,
376 struct nfs4_sequence_res *res,
377 int cache_reply)
378{
379 int ret;
380 struct rpc_task *task;
381 struct nfs41_call_sync_data data = {
382 .clp = clp,
383 .seq_args = args,
384 .seq_res = res,
385 .cache_reply = cache_reply,
386 };
387 struct rpc_task_setup task_setup = {
388 .rpc_client = clnt,
389 .rpc_message = msg,
390 .callback_ops = &nfs41_call_sync_ops,
391 .callback_data = &data
392 };
393
394 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
395 task = rpc_run_task(&task_setup);
396 if (IS_ERR(task))
397 ret = PTR_ERR(task);
398 else {
399 ret = task->tk_status;
400 rpc_put_task(task);
401 }
402 return ret;
403}
404
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400405int _nfs4_call_sync_session(struct nfs_server *server,
406 struct rpc_message *msg,
407 struct nfs4_sequence_args *args,
408 struct nfs4_sequence_res *res,
409 int cache_reply)
410{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400411 return nfs4_call_sync_sequence(server->nfs_client, server->client,
412 msg, args, res, cache_reply);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400413}
414
415#endif /* CONFIG_NFS_V4_1 */
416
417int _nfs4_call_sync(struct nfs_server *server,
418 struct rpc_message *msg,
419 struct nfs4_sequence_args *args,
420 struct nfs4_sequence_res *res,
421 int cache_reply)
422{
Benny Halevyf3752972009-04-01 09:22:04 -0400423 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400424 return rpc_call_sync(server->client, msg, 0);
425}
426
427#define nfs4_call_sync(server, msg, args, res, cache_reply) \
428 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
429 &(res)->seq_res, (cache_reply))
430
Trond Myklebust38478b22006-05-25 01:40:57 -0400431static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Trond Myklebust38478b22006-05-25 01:40:57 -0400433 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Trond Myklebust38478b22006-05-25 01:40:57 -0400435 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400436 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
437 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400438 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400439 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400440 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100443struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400444 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100445 struct nfs_openargs o_arg;
446 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100447 struct nfs_open_confirmargs c_arg;
448 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100449 struct nfs_fattr f_attr;
450 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400451 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100452 struct dentry *dir;
453 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400454 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100455 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100456 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400457 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100458 int rpc_status;
459 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100460};
461
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400462
463static void nfs4_init_opendata_res(struct nfs4_opendata *p)
464{
465 p->o_res.f_attr = &p->f_attr;
466 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400467 p->o_res.seqid = p->o_arg.seqid;
468 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400469 p->o_res.server = p->o_arg.server;
470 nfs_fattr_init(&p->f_attr);
471 nfs_fattr_init(&p->dir_attr);
472}
473
Trond Myklebustad389da2007-06-05 12:30:00 -0400474static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500475 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100476 const struct iattr *attrs)
477{
Trond Myklebustad389da2007-06-05 12:30:00 -0400478 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100479 struct inode *dir = parent->d_inode;
480 struct nfs_server *server = NFS_SERVER(dir);
481 struct nfs4_opendata *p;
482
483 p = kzalloc(sizeof(*p), GFP_KERNEL);
484 if (p == NULL)
485 goto err;
486 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
487 if (p->o_arg.seqid == NULL)
488 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400489 p->path.mnt = mntget(path->mnt);
490 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100491 p->dir = parent;
492 p->owner = sp;
493 atomic_inc(&sp->so_count);
494 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500495 p->o_arg.open_flags = flags;
496 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400497 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400498 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400499 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100500 p->o_arg.server = server;
501 p->o_arg.bitmask = server->attr_bitmask;
502 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Andy Adamson5f7dbd52009-04-01 09:22:05 -0400503 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100504 if (flags & O_EXCL) {
505 u32 *s = (u32 *) p->o_arg.u.verifier.data;
506 s[0] = jiffies;
507 s[1] = current->pid;
508 } else if (flags & O_CREAT) {
509 p->o_arg.u.attrs = &p->attrs;
510 memcpy(&p->attrs, attrs, sizeof(p->attrs));
511 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100512 p->c_arg.fh = &p->o_res.fh;
513 p->c_arg.stateid = &p->o_res.stateid;
514 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400515 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400516 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100517 return p;
518err_free:
519 kfree(p);
520err:
521 dput(parent);
522 return NULL;
523}
524
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400525static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100526{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400527 struct nfs4_opendata *p = container_of(kref,
528 struct nfs4_opendata, kref);
529
530 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400531 if (p->state != NULL)
532 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400533 nfs4_put_state_owner(p->owner);
534 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700535 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400536 kfree(p);
537}
538
539static void nfs4_opendata_put(struct nfs4_opendata *p)
540{
541 if (p != NULL)
542 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100543}
544
Trond Myklebust06f814a2006-01-03 09:55:07 +0100545static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
546{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100547 int ret;
548
Trond Myklebust06f814a2006-01-03 09:55:07 +0100549 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100550 return ret;
551}
552
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500553static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400554{
555 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500556
557 if (open_mode & O_EXCL)
558 goto out;
559 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400560 case FMODE_READ:
561 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400562 break;
563 case FMODE_WRITE:
564 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400565 break;
566 case FMODE_READ|FMODE_WRITE:
567 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
568 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500569out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400570 return ret;
571}
572
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500573static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400574{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500575 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400576 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500577 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400578 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500579 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400580 return 1;
581}
582
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500583static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100584{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500585 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100586 case FMODE_WRITE:
587 state->n_wronly++;
588 break;
589 case FMODE_READ:
590 state->n_rdonly++;
591 break;
592 case FMODE_READ|FMODE_WRITE:
593 state->n_rdwr++;
594 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500595 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100596}
597
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500598static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400599{
600 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
601 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
602 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500603 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400604 case FMODE_READ:
605 set_bit(NFS_O_RDONLY_STATE, &state->flags);
606 break;
607 case FMODE_WRITE:
608 set_bit(NFS_O_WRONLY_STATE, &state->flags);
609 break;
610 case FMODE_READ|FMODE_WRITE:
611 set_bit(NFS_O_RDWR_STATE, &state->flags);
612 }
613}
614
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500615static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400616{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400617 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500618 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400619 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400620}
621
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500622static 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 -0700623{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400624 /*
625 * Protect the call to nfs4_state_set_mode_locked and
626 * serialise the stateid update
627 */
628 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400629 if (deleg_stateid != NULL) {
630 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
631 set_bit(NFS_DELEGATED_STATE, &state->flags);
632 }
633 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500634 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400635 write_sequnlock(&state->seqlock);
636 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500637 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700638 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500641static 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 -0500642{
643 struct nfs_inode *nfsi = NFS_I(state->inode);
644 struct nfs_delegation *deleg_cur;
645 int ret = 0;
646
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500647 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500648
649 rcu_read_lock();
650 deleg_cur = rcu_dereference(nfsi->delegation);
651 if (deleg_cur == NULL)
652 goto no_delegation;
653
654 spin_lock(&deleg_cur->lock);
655 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500656 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500657 goto no_delegation_unlock;
658
659 if (delegation == NULL)
660 delegation = &deleg_cur->stateid;
661 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
662 goto no_delegation_unlock;
663
Trond Myklebustb7391f42008-12-23 15:21:52 -0500664 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500665 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500666 ret = 1;
667no_delegation_unlock:
668 spin_unlock(&deleg_cur->lock);
669no_delegation:
670 rcu_read_unlock();
671
672 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500673 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500674 ret = 1;
675 }
676
677 return ret;
678}
679
680
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500681static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400682{
683 struct nfs_delegation *delegation;
684
685 rcu_read_lock();
686 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500687 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400688 rcu_read_unlock();
689 return;
690 }
691 rcu_read_unlock();
692 nfs_inode_return_delegation(inode);
693}
694
Trond Myklebust6ee41262007-07-08 14:11:36 -0400695static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400696{
697 struct nfs4_state *state = opendata->state;
698 struct nfs_inode *nfsi = NFS_I(state->inode);
699 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500700 int open_mode = opendata->o_arg.open_flags & O_EXCL;
701 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400702 nfs4_stateid stateid;
703 int ret = -EAGAIN;
704
Trond Myklebustaac00a82007-07-05 19:02:21 -0400705 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500706 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400707 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500708 if (can_open_cached(state, fmode, open_mode)) {
709 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400710 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400711 goto out_return_state;
712 }
713 spin_unlock(&state->owner->so_lock);
714 }
Trond Myklebust34310432008-12-23 15:21:38 -0500715 rcu_read_lock();
716 delegation = rcu_dereference(nfsi->delegation);
717 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500718 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500719 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400720 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500721 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400722 /* Save the delegation */
723 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
724 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400725 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400726 if (ret != 0)
727 goto out;
728 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500729
730 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500731 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500732 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400733 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400734out:
735 return ERR_PTR(ret);
736out_return_state:
737 atomic_inc(&state->count);
738 return state;
739}
740
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100741static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
742{
743 struct inode *inode;
744 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400745 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400746 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100747
Trond Myklebustaac00a82007-07-05 19:02:21 -0400748 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400749 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400750 goto out;
751 }
752
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400753 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100754 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400755 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100756 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400757 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500758 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400759 goto err;
760 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100761 state = nfs4_get_open_state(inode, data->owner);
762 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400763 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400764 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400765 int delegation_flags = 0;
766
Trond Myklebust003707c2007-07-05 18:07:55 -0400767 rcu_read_lock();
768 delegation = rcu_dereference(NFS_I(inode)->delegation);
769 if (delegation)
770 delegation_flags = delegation->flags;
771 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -0500772 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400773 nfs_inode_set_delegation(state->inode,
774 data->owner->so_cred,
775 &data->o_res);
776 else
777 nfs_inode_reclaim_delegation(state->inode,
778 data->owner->so_cred,
779 &data->o_res);
780 }
Trond Myklebust34310432008-12-23 15:21:38 -0500781
782 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500783 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100784 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400785out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100786 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400787err_put_inode:
788 iput(inode);
789err:
790 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100791}
792
Trond Myklebust864472e2006-01-03 09:55:15 +0100793static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
794{
795 struct nfs_inode *nfsi = NFS_I(state->inode);
796 struct nfs_open_context *ctx;
797
798 spin_lock(&state->inode->i_lock);
799 list_for_each_entry(ctx, &nfsi->open_files, list) {
800 if (ctx->state != state)
801 continue;
802 get_nfs_open_context(ctx);
803 spin_unlock(&state->inode->i_lock);
804 return ctx;
805 }
806 spin_unlock(&state->inode->i_lock);
807 return ERR_PTR(-ENOENT);
808}
809
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400810static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
811{
812 struct nfs4_opendata *opendata;
813
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500814 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400815 if (opendata == NULL)
816 return ERR_PTR(-ENOMEM);
817 opendata->state = state;
818 atomic_inc(&state->count);
819 return opendata;
820}
821
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500822static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +0100823{
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400824 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100825 int ret;
826
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500827 opendata->o_arg.open_flags = 0;
828 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400829 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
830 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
831 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100832 ret = _nfs4_proc_open(opendata);
833 if (ret != 0)
834 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400835 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400836 if (IS_ERR(newstate))
837 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500838 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400839 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100840 return 0;
841}
842
843static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
844{
Trond Myklebust864472e2006-01-03 09:55:15 +0100845 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100846 int ret;
847
848 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400849 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100850 smp_rmb();
851 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400852 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100853 if (ret != 0)
854 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400855 if (newstate != state)
856 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100857 }
858 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400859 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100860 if (ret != 0)
861 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400862 if (newstate != state)
863 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100864 }
865 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400866 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100867 if (ret != 0)
868 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400869 if (newstate != state)
870 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100871 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400872 /*
873 * We may have performed cached opens for all three recoveries.
874 * Check if we need to update the current stateid.
875 */
876 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
877 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400878 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400879 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
880 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400881 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400882 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100883 return 0;
884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886/*
887 * OPEN_RECLAIM:
888 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 */
Trond Myklebust539cd032007-06-05 11:46:42 -0400890static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400892 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +0100893 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500894 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 int status;
896
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400897 opendata = nfs4_open_recoverdata_alloc(ctx, state);
898 if (IS_ERR(opendata))
899 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100900 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
901 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400902 rcu_read_lock();
903 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -0500904 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -0400905 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400906 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +0100907 opendata->o_arg.u.delegation_type = delegation_type;
908 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400909 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return status;
911}
912
Trond Myklebust539cd032007-06-05 11:46:42 -0400913static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
915 struct nfs_server *server = NFS_SERVER(state->inode);
916 struct nfs4_exception exception = { };
917 int err;
918 do {
Trond Myklebust539cd032007-06-05 11:46:42 -0400919 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000920 if (err != -NFS4ERR_DELAY)
921 break;
922 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 } while (exception.retry);
924 return err;
925}
926
Trond Myklebust864472e2006-01-03 09:55:15 +0100927static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
928{
929 struct nfs_open_context *ctx;
930 int ret;
931
932 ctx = nfs4_state_find_open_context(state);
933 if (IS_ERR(ctx))
934 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -0400935 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +0100936 put_nfs_open_context(ctx);
937 return ret;
938}
939
Trond Myklebust13437e12007-07-06 15:10:43 -0400940static 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 -0700941{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100942 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +0100943 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400945 opendata = nfs4_open_recoverdata_alloc(ctx, state);
946 if (IS_ERR(opendata))
947 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100948 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -0400949 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100950 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +0100951 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400952 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100953 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
955
Trond Myklebust13437e12007-07-06 15:10:43 -0400956int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -0400959 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 int err;
961 do {
Trond Myklebust13437e12007-07-06 15:10:43 -0400962 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 switch (err) {
964 case 0:
965 return err;
966 case -NFS4ERR_STALE_CLIENTID:
967 case -NFS4ERR_STALE_STATEID:
968 case -NFS4ERR_EXPIRED:
969 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -0400970 nfs4_schedule_state_recovery(server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return err;
972 }
973 err = nfs4_handle_exception(server, err, &exception);
974 } while (exception.retry);
975 return err;
976}
977
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100978static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
979{
980 struct nfs4_opendata *data = calldata;
981
982 data->rpc_status = task->tk_status;
983 if (RPC_ASSASSINATED(task))
984 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100985 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100986 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
987 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -0500988 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +0100989 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -0400990 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100991 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100992}
993
994static void nfs4_open_confirm_release(void *calldata)
995{
996 struct nfs4_opendata *data = calldata;
997 struct nfs4_state *state = NULL;
998
999 /* If this request hasn't been cancelled, do nothing */
1000 if (data->cancelled == 0)
1001 goto out_free;
1002 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001003 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001004 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001005 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001006 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001007 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001008out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001009 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001010}
1011
1012static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001013 .rpc_call_done = nfs4_open_confirm_done,
1014 .rpc_release = nfs4_open_confirm_release,
1015};
1016
1017/*
1018 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1019 */
1020static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1021{
1022 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1023 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001024 struct rpc_message msg = {
1025 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1026 .rpc_argp = &data->c_arg,
1027 .rpc_resp = &data->c_res,
1028 .rpc_cred = data->owner->so_cred,
1029 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001030 struct rpc_task_setup task_setup_data = {
1031 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001032 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001033 .callback_ops = &nfs4_open_confirm_ops,
1034 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001035 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001036 .flags = RPC_TASK_ASYNC,
1037 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 int status;
1039
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001040 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001041 data->rpc_done = 0;
1042 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001043 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001044 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001045 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001046 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001047 status = nfs4_wait_for_completion_rpc_task(task);
1048 if (status != 0) {
1049 data->cancelled = 1;
1050 smp_wmb();
1051 } else
1052 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001053 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return status;
1055}
1056
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001057static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001059 struct nfs4_opendata *data = calldata;
1060 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001061
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001062 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1063 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001064 /*
1065 * Check if we still need to send an OPEN call, or if we can use
1066 * a delegation instead.
1067 */
1068 if (data->state != NULL) {
1069 struct nfs_delegation *delegation;
1070
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001071 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001072 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001073 rcu_read_lock();
1074 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1075 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001076 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001077 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001078 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001079 }
1080 rcu_read_unlock();
1081 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001082 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001083 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001084 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001085 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001086 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001087 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1088 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001089 data->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001090 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001091 return;
1092out_no_action:
1093 task->tk_action = NULL;
1094
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001095}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001097static void nfs4_open_done(struct rpc_task *task, void *calldata)
1098{
1099 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001101 data->rpc_status = task->tk_status;
1102 if (RPC_ASSASSINATED(task))
1103 return;
1104 if (task->tk_status == 0) {
1105 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001106 case S_IFREG:
1107 break;
1108 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001109 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001110 break;
1111 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001112 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001113 break;
1114 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001115 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001116 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001117 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001118 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1119 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001120 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001121 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001122}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001123
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001124static void nfs4_open_release(void *calldata)
1125{
1126 struct nfs4_opendata *data = calldata;
1127 struct nfs4_state *state = NULL;
1128
1129 /* If this request hasn't been cancelled, do nothing */
1130 if (data->cancelled == 0)
1131 goto out_free;
1132 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001133 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001134 goto out_free;
1135 /* In case we need an open_confirm, no cleanup! */
1136 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1137 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001138 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001139 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001140 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001141out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001142 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001143}
1144
1145static const struct rpc_call_ops nfs4_open_ops = {
1146 .rpc_call_prepare = nfs4_open_prepare,
1147 .rpc_call_done = nfs4_open_done,
1148 .rpc_release = nfs4_open_release,
1149};
1150
1151/*
1152 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1153 */
1154static int _nfs4_proc_open(struct nfs4_opendata *data)
1155{
1156 struct inode *dir = data->dir->d_inode;
1157 struct nfs_server *server = NFS_SERVER(dir);
1158 struct nfs_openargs *o_arg = &data->o_arg;
1159 struct nfs_openres *o_res = &data->o_res;
1160 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001161 struct rpc_message msg = {
1162 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1163 .rpc_argp = o_arg,
1164 .rpc_resp = o_res,
1165 .rpc_cred = data->owner->so_cred,
1166 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001167 struct rpc_task_setup task_setup_data = {
1168 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001169 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001170 .callback_ops = &nfs4_open_ops,
1171 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001172 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001173 .flags = RPC_TASK_ASYNC,
1174 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001175 int status;
1176
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001177 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001178 data->rpc_done = 0;
1179 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001180 data->cancelled = 0;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001181 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001182 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001183 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001184 status = nfs4_wait_for_completion_rpc_task(task);
1185 if (status != 0) {
1186 data->cancelled = 1;
1187 smp_wmb();
1188 } else
1189 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001190 rpc_put_task(task);
Trond Myklebust3e309912007-07-07 13:19:59 -04001191 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001192 return status;
1193
Trond Myklebust99367812007-07-17 21:52:41 -04001194 if (o_res->fh.size == 0)
1195 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1196
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001197 if (o_arg->open_flags & O_CREAT) {
1198 update_changeattr(dir, &o_res->cinfo);
1199 nfs_post_op_update_inode(dir, o_res->dir_attr);
1200 } else
1201 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001203 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001205 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001208 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
Trond Myklebust10afec92007-05-14 17:16:04 -04001212static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001213{
David Howells7539bba2006-08-22 20:06:09 -04001214 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6b309542006-09-14 14:03:14 -04001215 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001216
Trond Myklebust6b309542006-09-14 14:03:14 -04001217 for (;;) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001218 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001219 if (ret != 0)
1220 return ret;
Trond Myklebuste598d842008-12-23 15:21:42 -05001221 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1222 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001223 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001224 nfs4_schedule_state_recovery(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001225 }
1226 return 0;
Trond Myklebust58d97142006-01-03 09:55:24 +01001227}
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229/*
1230 * OPEN_EXPIRED:
1231 * reclaim state on the server after a network partition.
1232 * Assumes caller holds the appropriate lock
1233 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001234static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001236 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001237 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001239 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1240 if (IS_ERR(opendata))
1241 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001242 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001243 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001244 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001245 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001246 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
Trond Myklebust539cd032007-06-05 11:46:42 -04001249static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001250{
Trond Myklebust539cd032007-06-05 11:46:42 -04001251 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001252 struct nfs4_exception exception = { };
1253 int err;
1254
1255 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001256 err = _nfs4_open_expired(ctx, state);
Trond Myklebust027b6ca2008-12-23 16:04:13 -05001257 if (err != -NFS4ERR_DELAY)
1258 break;
1259 nfs4_handle_exception(server, err, &exception);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001260 } while (exception.retry);
1261 return err;
1262}
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1265{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001267 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Trond Myklebust864472e2006-01-03 09:55:15 +01001269 ctx = nfs4_state_find_open_context(state);
1270 if (IS_ERR(ctx))
1271 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001272 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001273 put_nfs_open_context(ctx);
1274 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
1277/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001278 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1279 * fields corresponding to attributes that were used to store the verifier.
1280 * Make sure we clobber those fields in the later setattr call
1281 */
1282static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1283{
1284 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1285 !(sattr->ia_valid & ATTR_ATIME_SET))
1286 sattr->ia_valid |= ATTR_ATIME;
1287
1288 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1289 !(sattr->ia_valid & ATTR_MTIME_SET))
1290 sattr->ia_valid |= ATTR_MTIME;
1291}
1292
1293/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001294 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001296static 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 -07001297{
1298 struct nfs4_state_owner *sp;
1299 struct nfs4_state *state = NULL;
1300 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001301 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001302 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 status = -ENOMEM;
1306 if (!(sp = nfs4_get_state_owner(server, cred))) {
1307 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1308 goto out_err;
1309 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001310 status = nfs4_recover_expired_lease(server);
1311 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001312 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001313 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001314 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001315 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001316 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001317 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001318 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Trond Myklebustaac00a82007-07-05 19:02:21 -04001320 if (path->dentry->d_inode != NULL)
1321 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1322
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001323 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001325 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001327 if (opendata->o_arg.open_flags & O_EXCL)
1328 nfs4_exclusive_attrset(opendata, sattr);
1329
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001330 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001331 status = PTR_ERR(state);
1332 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001333 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001334 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 *res = state;
1337 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001338err_opendata_put:
1339 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001340err_put_state_owner:
1341 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 *res = NULL;
1344 return status;
1345}
1346
1347
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001348static 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 -07001349{
1350 struct nfs4_exception exception = { };
1351 struct nfs4_state *res;
1352 int status;
1353
1354 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001355 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (status == 0)
1357 break;
1358 /* NOTE: BAD_SEQID means the server and client disagree about the
1359 * book-keeping w.r.t. state-changing operations
1360 * (OPEN/CLOSE/LOCK/LOCKU...)
1361 * It is actually a sign of a bug on the client or on the server.
1362 *
1363 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001364 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 * have unhashed the old state_owner for us, and that we can
1366 * therefore safely retry using a new one. We should still warn
1367 * the user though...
1368 */
1369 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001370 printk(KERN_WARNING "NFS: v4 server %s "
1371 " returned a bad sequence-id error!\n",
1372 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 exception.retry = 1;
1374 continue;
1375 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001376 /*
1377 * BAD_STATEID on OPEN means that the server cancelled our
1378 * state before it received the OPEN_CONFIRM.
1379 * Recover by retrying the request as per the discussion
1380 * on Page 181 of RFC3530.
1381 */
1382 if (status == -NFS4ERR_BAD_STATEID) {
1383 exception.retry = 1;
1384 continue;
1385 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001386 if (status == -EAGAIN) {
1387 /* We must have found a delegation */
1388 exception.retry = 1;
1389 continue;
1390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1392 status, &exception));
1393 } while (exception.retry);
1394 return res;
1395}
1396
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001397static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1398 struct nfs_fattr *fattr, struct iattr *sattr,
1399 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001401 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001403 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 .iap = sattr,
1405 .server = server,
1406 .bitmask = server->attr_bitmask,
1407 };
1408 struct nfs_setattrres res = {
1409 .fattr = fattr,
1410 .server = server,
1411 };
1412 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1414 .rpc_argp = &arg,
1415 .rpc_resp = &res,
1416 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001418 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001419 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Trond Myklebust0e574af2005-10-27 22:12:38 -04001421 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001423 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1424 /* Use that stateid */
1425 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001426 nfs4_copy_stateid(&arg.stateid, state, current->files);
1427 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1429
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001430 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001431 if (status == 0 && state != NULL)
1432 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001433 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
1435
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001436static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1437 struct nfs_fattr *fattr, struct iattr *sattr,
1438 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001440 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 struct nfs4_exception exception = { };
1442 int err;
1443 do {
1444 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001445 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 &exception);
1447 } while (exception.retry);
1448 return err;
1449}
1450
1451struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001452 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 struct inode *inode;
1454 struct nfs4_state *state;
1455 struct nfs_closeargs arg;
1456 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001457 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001458 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459};
1460
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001461static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001462{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001463 struct nfs4_closedata *calldata = data;
1464 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001465
1466 nfs4_put_open_state(calldata->state);
1467 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001468 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001469 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001470 kfree(calldata);
1471}
1472
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001473static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001475 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 struct nfs_server *server = NFS_SERVER(calldata->inode);
1478
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001479 if (RPC_ASSASSINATED(task))
1480 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 /* hmm. we are done with the inode, and in the process of freeing
1482 * the state_owner. we keep this around to process errors
1483 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 switch (task->tk_status) {
1485 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001486 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001487 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 break;
1489 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001490 case -NFS4ERR_OLD_STATEID:
1491 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001493 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001494 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001496 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 rpc_restart_call(task);
1498 return;
1499 }
1500 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001501 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001504static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001506 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001507 struct nfs4_state *state = calldata->state;
Trond Myklebust003707c2007-07-05 18:07:55 -04001508 int clear_rd, clear_wr, clear_rdwr;
Trond Myklebust95121352005-10-18 14:20:12 -07001509
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001510 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001511 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001512
Trond Myklebust003707c2007-07-05 18:07:55 -04001513 clear_rd = clear_wr = clear_rdwr = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001514 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001515 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001516 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001517 if (state->n_rdonly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001518 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1519 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1520 }
1521 if (state->n_wronly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001522 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1523 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1524 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001525 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001526 spin_unlock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001527 if (!clear_rd && !clear_wr && !clear_rdwr) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001528 /* Note: exit _without_ calling nfs4_close_done */
1529 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001530 return;
1531 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001532 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust45328c32007-07-26 17:47:34 -04001533 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001534 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001535 calldata->arg.fmode = FMODE_READ;
Trond Myklebust45328c32007-07-26 17:47:34 -04001536 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001537 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001538 calldata->arg.fmode = FMODE_WRITE;
Trond Myklebust45328c32007-07-26 17:47:34 -04001539 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001540 calldata->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001541 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001544static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001545 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001546 .rpc_call_done = nfs4_close_done,
1547 .rpc_release = nfs4_free_closedata,
1548};
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550/*
1551 * It is possible for data to be read/written from a mem-mapped file
1552 * after the sys_close call (which hits the vfs layer as a flush).
1553 * This means that we can't safely call nfsv4 close on a file until
1554 * the inode is cleared. This in turn means that we are not good
1555 * NFSv4 citizens - we do not indicate to the server to update the file's
1556 * share state even when we are done with one of the three share
1557 * stateid's in the inode.
1558 *
1559 * NOTE: Caller must be holding the sp->so_owner semaphore!
1560 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001561int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001563 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001565 struct nfs4_state_owner *sp = state->owner;
1566 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001567 struct rpc_message msg = {
1568 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1569 .rpc_cred = state->owner->so_cred,
1570 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001571 struct rpc_task_setup task_setup_data = {
1572 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001573 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001574 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001575 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001576 .flags = RPC_TASK_ASYNC,
1577 };
Trond Myklebust95121352005-10-18 14:20:12 -07001578 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Trond Myklebust95121352005-10-18 14:20:12 -07001580 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001582 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001583 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001585 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001586 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001588 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001589 if (calldata->arg.seqid == NULL)
1590 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001591 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001592 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001593 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001594 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001595 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001596 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001597 calldata->path.mnt = mntget(path->mnt);
1598 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001599
Trond Myklebust5138fde2007-07-14 15:40:01 -04001600 msg.rpc_argp = &calldata->arg,
1601 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001602 task_setup_data.callback_data = calldata;
1603 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001604 if (IS_ERR(task))
1605 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001606 status = 0;
1607 if (wait)
1608 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001609 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001610 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001611out_free_calldata:
1612 kfree(calldata);
1613out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001614 nfs4_put_open_state(state);
1615 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001616 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001619static 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 -07001620{
1621 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001622 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001623
Trond Myklebust1b45c462007-07-03 13:04:56 -04001624 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001625 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001626 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001627 state->owner->so_cred,
1628 nd->intent.open.flags);
1629 if (ret < 0)
1630 goto out_close;
1631 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001632 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001633 if (!IS_ERR(filp)) {
1634 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001635 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001636 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001637 return 0;
1638 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001639 ret = PTR_ERR(filp);
1640out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001641 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001642 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001643}
1644
1645struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1647{
Trond Myklebustad389da2007-06-05 12:30:00 -04001648 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001649 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001650 .dentry = dentry,
1651 };
Jan Blunck4ac91372008-02-14 19:34:32 -08001652 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 struct iattr attr;
1654 struct rpc_cred *cred;
1655 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001656 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001657 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 if (nd->flags & LOOKUP_CREATE) {
1660 attr.ia_mode = nd->intent.open.create_mode;
1661 attr.ia_valid = ATTR_MODE;
1662 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04001663 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 } else {
1665 attr.ia_valid = 0;
1666 BUG_ON(nd->intent.open.flags & O_CREAT);
1667 }
1668
Trond Myklebust98a8e322008-03-12 12:25:28 -04001669 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001671 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04001672 parent = dentry->d_parent;
1673 /* Protect against concurrent sillydeletes */
1674 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001675 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001677 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001678 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001679 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04001680 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1681 }
Trond Myklebust565277f2007-10-15 18:17:53 -04001682 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001683 return (struct dentry *)state;
1684 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001685 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001686 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04001687 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04001688 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001689 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001690 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001691 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692}
1693
1694int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001695nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Trond Myklebustad389da2007-06-05 12:30:00 -04001697 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001698 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001699 .dentry = dentry,
1700 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 struct rpc_cred *cred;
1702 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001703 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Trond Myklebust98a8e322008-03-12 12:25:28 -04001705 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (IS_ERR(cred))
1707 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001708 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001710 if (IS_ERR(state)) {
1711 switch (PTR_ERR(state)) {
1712 case -EPERM:
1713 case -EACCES:
1714 case -EDQUOT:
1715 case -ENOSPC:
1716 case -EROFS:
1717 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1718 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001719 default:
1720 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001721 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001722 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001723 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001724 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001725 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return 1;
1727 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001728 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001729out_drop:
1730 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 return 0;
1732}
1733
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001734void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1735{
1736 if (ctx->state == NULL)
1737 return;
1738 if (is_sync)
1739 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
1740 else
1741 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
1742}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1745{
Benny Halevy43652ad2009-04-01 09:21:54 -04001746 struct nfs4_server_caps_arg args = {
1747 .fhandle = fhandle,
1748 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 struct nfs4_server_caps_res res = {};
1750 struct rpc_message msg = {
1751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04001752 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 .rpc_resp = &res,
1754 };
1755 int status;
1756
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001757 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (status == 0) {
1759 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1760 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1761 server->caps |= NFS_CAP_ACLS;
1762 if (res.has_links != 0)
1763 server->caps |= NFS_CAP_HARDLINKS;
1764 if (res.has_symlinks != 0)
1765 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001766 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
1767 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
1768 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 server->acl_bitmask = res.acl_bitmask;
1770 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return status;
1773}
1774
Trond Myklebust55a97592006-06-09 09:34:19 -04001775int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 struct nfs4_exception exception = { };
1778 int err;
1779 do {
1780 err = nfs4_handle_exception(server,
1781 _nfs4_server_capabilities(server, fhandle),
1782 &exception);
1783 } while (exception.retry);
1784 return err;
1785}
1786
1787static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1788 struct nfs_fsinfo *info)
1789{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 struct nfs4_lookup_root_arg args = {
1791 .bitmask = nfs4_fattr_bitmap,
1792 };
1793 struct nfs4_lookup_res res = {
1794 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001795 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 .fh = fhandle,
1797 };
1798 struct rpc_message msg = {
1799 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1800 .rpc_argp = &args,
1801 .rpc_resp = &res,
1802 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001803 nfs_fattr_init(info->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001804 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
1807static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1808 struct nfs_fsinfo *info)
1809{
1810 struct nfs4_exception exception = { };
1811 int err;
1812 do {
1813 err = nfs4_handle_exception(server,
1814 _nfs4_lookup_root(server, fhandle, info),
1815 &exception);
1816 } while (exception.retry);
1817 return err;
1818}
1819
David Howells54ceac42006-08-22 20:06:13 -04001820/*
1821 * get the file handle for the "/" directory on the server
1822 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04001824 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 int status;
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 if (status == 0)
1830 status = nfs4_server_capabilities(server, fhandle);
1831 if (status == 0)
1832 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08001833 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
Manoj Naik6b97fd32006-06-09 09:34:29 -04001836/*
1837 * Get locations and (maybe) other attributes of a referral.
1838 * Note that we'll actually follow the referral later when
1839 * we detect fsid mismatch in inode revalidation
1840 */
Trond Myklebust56659e92007-07-17 21:52:39 -04001841static 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 -04001842{
1843 int status = -ENOMEM;
1844 struct page *page = NULL;
1845 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04001846
1847 page = alloc_page(GFP_KERNEL);
1848 if (page == NULL)
1849 goto out;
1850 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1851 if (locations == NULL)
1852 goto out;
1853
Trond Myklebustc228fd32007-01-13 02:28:11 -05001854 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001855 if (status != 0)
1856 goto out;
1857 /* Make sure server returned a different fsid for the referral */
1858 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07001859 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 -04001860 status = -EIO;
1861 goto out;
1862 }
1863
1864 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1865 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1866 if (!fattr->mode)
1867 fattr->mode = S_IFDIR;
1868 memset(fhandle, 0, sizeof(struct nfs_fh));
1869out:
1870 if (page)
1871 __free_page(page);
1872 if (locations)
1873 kfree(locations);
1874 return status;
1875}
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1878{
1879 struct nfs4_getattr_arg args = {
1880 .fh = fhandle,
1881 .bitmask = server->attr_bitmask,
1882 };
1883 struct nfs4_getattr_res res = {
1884 .fattr = fattr,
1885 .server = server,
1886 };
1887 struct rpc_message msg = {
1888 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1889 .rpc_argp = &args,
1890 .rpc_resp = &res,
1891 };
1892
Trond Myklebust0e574af2005-10-27 22:12:38 -04001893 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001894 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895}
1896
1897static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1898{
1899 struct nfs4_exception exception = { };
1900 int err;
1901 do {
1902 err = nfs4_handle_exception(server,
1903 _nfs4_proc_getattr(server, fhandle, fattr),
1904 &exception);
1905 } while (exception.retry);
1906 return err;
1907}
1908
1909/*
1910 * The file is not closed if it is opened due to the a request to change
1911 * the size of the file. The open call will not be needed once the
1912 * VFS layer lookup-intents are implemented.
1913 *
1914 * Close is called when the inode is destroyed.
1915 * If we haven't opened the file for O_WRONLY, we
1916 * need to in the size_change case to obtain a stateid.
1917 *
1918 * Got race?
1919 * Because OPEN is always done by name in nfsv4, it is
1920 * possible that we opened a different file by the same
1921 * name. We can recognize this race condition, but we
1922 * can't do anything about it besides returning an error.
1923 *
1924 * This will be fixed with VFS changes (lookup-intent).
1925 */
1926static int
1927nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1928 struct iattr *sattr)
1929{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001930 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001931 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05001932 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 int status;
1934
Trond Myklebust0e574af2005-10-27 22:12:38 -04001935 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Trond Myklebustd5308382005-11-04 15:33:38 -05001937 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001938 if (sattr->ia_valid & ATTR_FILE) {
1939 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001940
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001941 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11001942 if (ctx) {
1943 cred = ctx->cred;
1944 state = ctx->state;
1945 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001946 }
1947
1948 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04001949 if (status == 0)
1950 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 return status;
1952}
1953
Trond Myklebust56659e92007-07-17 21:52:39 -04001954static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
1955 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04001956 struct nfs_fattr *fattr)
1957{
1958 int status;
1959 struct nfs4_lookup_arg args = {
1960 .bitmask = server->attr_bitmask,
1961 .dir_fh = dirfh,
1962 .name = name,
1963 };
1964 struct nfs4_lookup_res res = {
1965 .server = server,
1966 .fattr = fattr,
1967 .fh = fhandle,
1968 };
1969 struct rpc_message msg = {
1970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1971 .rpc_argp = &args,
1972 .rpc_resp = &res,
1973 };
1974
1975 nfs_fattr_init(fattr);
1976
1977 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001978 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04001979 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04001980 return status;
1981}
1982
1983static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
1984 struct qstr *name, struct nfs_fh *fhandle,
1985 struct nfs_fattr *fattr)
1986{
1987 struct nfs4_exception exception = { };
1988 int err;
1989 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04001990 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
1991 /* FIXME: !!!! */
1992 if (err == -NFS4ERR_MOVED) {
1993 err = -EREMOTE;
1994 break;
1995 }
1996 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04001997 } while (exception.retry);
1998 return err;
1999}
2000
Trond Myklebust56659e92007-07-17 21:52:39 -04002001static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2003{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002004 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002007 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002008 if (status == -NFS4ERR_MOVED)
2009 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 dprintk("NFS reply lookup: %d\n", status);
2011 return status;
2012}
2013
2014static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2015{
2016 struct nfs4_exception exception = { };
2017 int err;
2018 do {
2019 err = nfs4_handle_exception(NFS_SERVER(dir),
2020 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2021 &exception);
2022 } while (exception.retry);
2023 return err;
2024}
2025
2026static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2027{
Trond Myklebust76b32992007-08-10 17:45:11 -04002028 struct nfs_server *server = NFS_SERVER(inode);
2029 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 struct nfs4_accessargs args = {
2031 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002032 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002034 struct nfs4_accessres res = {
2035 .server = server,
2036 .fattr = &fattr,
2037 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 struct rpc_message msg = {
2039 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2040 .rpc_argp = &args,
2041 .rpc_resp = &res,
2042 .rpc_cred = entry->cred,
2043 };
2044 int mode = entry->mask;
2045 int status;
2046
2047 /*
2048 * Determine which access bits we want to ask for...
2049 */
2050 if (mode & MAY_READ)
2051 args.access |= NFS4_ACCESS_READ;
2052 if (S_ISDIR(inode->i_mode)) {
2053 if (mode & MAY_WRITE)
2054 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2055 if (mode & MAY_EXEC)
2056 args.access |= NFS4_ACCESS_LOOKUP;
2057 } else {
2058 if (mode & MAY_WRITE)
2059 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2060 if (mode & MAY_EXEC)
2061 args.access |= NFS4_ACCESS_EXECUTE;
2062 }
Trond Myklebust76b32992007-08-10 17:45:11 -04002063 nfs_fattr_init(&fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002064 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (!status) {
2066 entry->mask = 0;
2067 if (res.access & NFS4_ACCESS_READ)
2068 entry->mask |= MAY_READ;
2069 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2070 entry->mask |= MAY_WRITE;
2071 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2072 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04002073 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 }
2075 return status;
2076}
2077
2078static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2079{
2080 struct nfs4_exception exception = { };
2081 int err;
2082 do {
2083 err = nfs4_handle_exception(NFS_SERVER(inode),
2084 _nfs4_proc_access(inode, entry),
2085 &exception);
2086 } while (exception.retry);
2087 return err;
2088}
2089
2090/*
2091 * TODO: For the time being, we don't try to get any attributes
2092 * along with any of the zero-copy operations READ, READDIR,
2093 * READLINK, WRITE.
2094 *
2095 * In the case of the first three, we want to put the GETATTR
2096 * after the read-type operation -- this is because it is hard
2097 * to predict the length of a GETATTR response in v4, and thus
2098 * align the READ data correctly. This means that the GETATTR
2099 * may end up partially falling into the page cache, and we should
2100 * shift it into the 'tail' of the xdr_buf before processing.
2101 * To do this efficiently, we need to know the total length
2102 * of data received, which doesn't seem to be available outside
2103 * of the RPC layer.
2104 *
2105 * In the case of WRITE, we also want to put the GETATTR after
2106 * the operation -- in this case because we want to make sure
2107 * we get the post-operation mtime and size. This means that
2108 * we can't use xdr_encode_pages() as written: we need a variant
2109 * of it which would leave room in the 'tail' iovec.
2110 *
2111 * Both of these changes to the XDR layer would in fact be quite
2112 * minor, but I decided to leave them for a subsequent patch.
2113 */
2114static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2115 unsigned int pgbase, unsigned int pglen)
2116{
2117 struct nfs4_readlink args = {
2118 .fh = NFS_FH(inode),
2119 .pgbase = pgbase,
2120 .pglen = pglen,
2121 .pages = &page,
2122 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002123 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 struct rpc_message msg = {
2125 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2126 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002127 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 };
2129
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002130 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131}
2132
2133static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2134 unsigned int pgbase, unsigned int pglen)
2135{
2136 struct nfs4_exception exception = { };
2137 int err;
2138 do {
2139 err = nfs4_handle_exception(NFS_SERVER(inode),
2140 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2141 &exception);
2142 } while (exception.retry);
2143 return err;
2144}
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146/*
2147 * Got race?
2148 * We will need to arrange for the VFS layer to provide an atomic open.
2149 * Until then, this create/open method is prone to inefficiency and race
2150 * conditions due to the lookup, create, and open VFS calls from sys_open()
2151 * placed on the wire.
2152 *
2153 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2154 * The file will be opened again in the subsequent VFS open call
2155 * (nfs4_proc_file_open).
2156 *
2157 * The open for read will just hang around to be used by any process that
2158 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2159 */
2160
2161static int
2162nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002163 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Trond Myklebustad389da2007-06-05 12:30:00 -04002165 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002166 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002167 .dentry = dentry,
2168 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 struct nfs4_state *state;
2170 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002171 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 int status = 0;
2173
Trond Myklebust98a8e322008-03-12 12:25:28 -04002174 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 if (IS_ERR(cred)) {
2176 status = PTR_ERR(cred);
2177 goto out;
2178 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002179 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002180 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (IS_ERR(state)) {
2182 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002183 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002185 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002186 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 if (flags & O_EXCL) {
2188 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002189 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002190 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002191 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002192 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002193 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002194 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002195 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002196 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002197 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002198out_putcred:
2199 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200out:
2201 return status;
2202}
2203
2204static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2205{
Trond Myklebust16e42952005-10-27 22:12:44 -04002206 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002207 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002209 .name.len = name->len,
2210 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002211 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002213 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002214 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002215 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002217 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2218 .rpc_argp = &args,
2219 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 };
2221 int status;
2222
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002223 nfs_fattr_init(&res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002224 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002225 if (status == 0) {
2226 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002227 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 return status;
2230}
2231
2232static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2233{
2234 struct nfs4_exception exception = { };
2235 int err;
2236 do {
2237 err = nfs4_handle_exception(NFS_SERVER(dir),
2238 _nfs4_proc_remove(dir, name),
2239 &exception);
2240 } while (exception.retry);
2241 return err;
2242}
2243
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002244static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002246 struct nfs_server *server = NFS_SERVER(dir);
2247 struct nfs_removeargs *args = msg->rpc_argp;
2248 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Trond Myklebusta65318b2009-03-11 14:10:28 -04002250 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002251 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253}
2254
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002255static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002257 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2258
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002259 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002260 return 0;
2261 update_changeattr(dir, &res->cinfo);
2262 nfs_post_op_update_inode(dir, &res->dir_attr);
2263 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264}
2265
2266static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2267 struct inode *new_dir, struct qstr *new_name)
2268{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002269 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 struct nfs4_rename_arg arg = {
2271 .old_dir = NFS_FH(old_dir),
2272 .new_dir = NFS_FH(new_dir),
2273 .old_name = old_name,
2274 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002275 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002277 struct nfs_fattr old_fattr, new_fattr;
2278 struct nfs4_rename_res res = {
2279 .server = server,
2280 .old_fattr = &old_fattr,
2281 .new_fattr = &new_fattr,
2282 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 struct rpc_message msg = {
2284 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2285 .rpc_argp = &arg,
2286 .rpc_resp = &res,
2287 };
2288 int status;
2289
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002290 nfs_fattr_init(res.old_fattr);
2291 nfs_fattr_init(res.new_fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002292 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 if (!status) {
2295 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002296 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002298 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 }
2300 return status;
2301}
2302
2303static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2304 struct inode *new_dir, struct qstr *new_name)
2305{
2306 struct nfs4_exception exception = { };
2307 int err;
2308 do {
2309 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2310 _nfs4_proc_rename(old_dir, old_name,
2311 new_dir, new_name),
2312 &exception);
2313 } while (exception.retry);
2314 return err;
2315}
2316
2317static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2318{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002319 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 struct nfs4_link_arg arg = {
2321 .fh = NFS_FH(inode),
2322 .dir_fh = NFS_FH(dir),
2323 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002324 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002326 struct nfs_fattr fattr, dir_attr;
2327 struct nfs4_link_res res = {
2328 .server = server,
2329 .fattr = &fattr,
2330 .dir_attr = &dir_attr,
2331 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 struct rpc_message msg = {
2333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2334 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002335 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 };
2337 int status;
2338
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002339 nfs_fattr_init(res.fattr);
2340 nfs_fattr_init(res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002341 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002342 if (!status) {
2343 update_changeattr(dir, &res.cinfo);
2344 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002345 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
2348 return status;
2349}
2350
2351static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2352{
2353 struct nfs4_exception exception = { };
2354 int err;
2355 do {
2356 err = nfs4_handle_exception(NFS_SERVER(inode),
2357 _nfs4_proc_link(inode, dir, name),
2358 &exception);
2359 } while (exception.retry);
2360 return err;
2361}
2362
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002363struct nfs4_createdata {
2364 struct rpc_message msg;
2365 struct nfs4_create_arg arg;
2366 struct nfs4_create_res res;
2367 struct nfs_fh fh;
2368 struct nfs_fattr fattr;
2369 struct nfs_fattr dir_fattr;
2370};
2371
2372static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2373 struct qstr *name, struct iattr *sattr, u32 ftype)
2374{
2375 struct nfs4_createdata *data;
2376
2377 data = kzalloc(sizeof(*data), GFP_KERNEL);
2378 if (data != NULL) {
2379 struct nfs_server *server = NFS_SERVER(dir);
2380
2381 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2382 data->msg.rpc_argp = &data->arg;
2383 data->msg.rpc_resp = &data->res;
2384 data->arg.dir_fh = NFS_FH(dir);
2385 data->arg.server = server;
2386 data->arg.name = name;
2387 data->arg.attrs = sattr;
2388 data->arg.ftype = ftype;
2389 data->arg.bitmask = server->attr_bitmask;
2390 data->res.server = server;
2391 data->res.fh = &data->fh;
2392 data->res.fattr = &data->fattr;
2393 data->res.dir_fattr = &data->dir_fattr;
2394 nfs_fattr_init(data->res.fattr);
2395 nfs_fattr_init(data->res.dir_fattr);
2396 }
2397 return data;
2398}
2399
2400static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2401{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002402 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2403 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002404 if (status == 0) {
2405 update_changeattr(dir, &data->res.dir_cinfo);
2406 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2407 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2408 }
2409 return status;
2410}
2411
2412static void nfs4_free_createdata(struct nfs4_createdata *data)
2413{
2414 kfree(data);
2415}
2416
Chuck Lever4f390c12006-08-22 20:06:22 -04002417static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002418 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002420 struct nfs4_createdata *data;
2421 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Chuck Lever94a6d752006-08-22 20:06:23 -04002423 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002424 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002425
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002426 status = -ENOMEM;
2427 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2428 if (data == NULL)
2429 goto out;
2430
2431 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2432 data->arg.u.symlink.pages = &page;
2433 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002435 status = nfs4_do_create(dir, dentry, data);
2436
2437 nfs4_free_createdata(data);
2438out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 return status;
2440}
2441
Chuck Lever4f390c12006-08-22 20:06:22 -04002442static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002443 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
2445 struct nfs4_exception exception = { };
2446 int err;
2447 do {
2448 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002449 _nfs4_proc_symlink(dir, dentry, page,
2450 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 &exception);
2452 } while (exception.retry);
2453 return err;
2454}
2455
2456static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2457 struct iattr *sattr)
2458{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002459 struct nfs4_createdata *data;
2460 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002462 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2463 if (data == NULL)
2464 goto out;
2465
2466 status = nfs4_do_create(dir, dentry, data);
2467
2468 nfs4_free_createdata(data);
2469out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 return status;
2471}
2472
2473static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2474 struct iattr *sattr)
2475{
2476 struct nfs4_exception exception = { };
2477 int err;
2478 do {
2479 err = nfs4_handle_exception(NFS_SERVER(dir),
2480 _nfs4_proc_mkdir(dir, dentry, sattr),
2481 &exception);
2482 } while (exception.retry);
2483 return err;
2484}
2485
2486static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2487 u64 cookie, struct page *page, unsigned int count, int plus)
2488{
2489 struct inode *dir = dentry->d_inode;
2490 struct nfs4_readdir_arg args = {
2491 .fh = NFS_FH(dir),
2492 .pages = &page,
2493 .pgbase = 0,
2494 .count = count,
Trond Myklebusta65318b2009-03-11 14:10:28 -04002495 .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 };
2497 struct nfs4_readdir_res res;
2498 struct rpc_message msg = {
2499 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2500 .rpc_argp = &args,
2501 .rpc_resp = &res,
2502 .rpc_cred = cred,
2503 };
2504 int status;
2505
Harvey Harrison3110ff82008-05-02 13:42:44 -07002506 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002507 dentry->d_parent->d_name.name,
2508 dentry->d_name.name,
2509 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2511 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002512 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 if (status == 0)
2514 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002515
2516 nfs_invalidate_atime(dir);
2517
Harvey Harrison3110ff82008-05-02 13:42:44 -07002518 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 return status;
2520}
2521
2522static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2523 u64 cookie, struct page *page, unsigned int count, int plus)
2524{
2525 struct nfs4_exception exception = { };
2526 int err;
2527 do {
2528 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2529 _nfs4_proc_readdir(dentry, cred, cookie,
2530 page, count, plus),
2531 &exception);
2532 } while (exception.retry);
2533 return err;
2534}
2535
2536static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2537 struct iattr *sattr, dev_t rdev)
2538{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002539 struct nfs4_createdata *data;
2540 int mode = sattr->ia_mode;
2541 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
2543 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2544 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002545
2546 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2547 if (data == NULL)
2548 goto out;
2549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002551 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002553 data->arg.ftype = NF4BLK;
2554 data->arg.u.device.specdata1 = MAJOR(rdev);
2555 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 }
2557 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002558 data->arg.ftype = NF4CHR;
2559 data->arg.u.device.specdata1 = MAJOR(rdev);
2560 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002563 status = nfs4_do_create(dir, dentry, data);
2564
2565 nfs4_free_createdata(data);
2566out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 return status;
2568}
2569
2570static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2571 struct iattr *sattr, dev_t rdev)
2572{
2573 struct nfs4_exception exception = { };
2574 int err;
2575 do {
2576 err = nfs4_handle_exception(NFS_SERVER(dir),
2577 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2578 &exception);
2579 } while (exception.retry);
2580 return err;
2581}
2582
2583static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2584 struct nfs_fsstat *fsstat)
2585{
2586 struct nfs4_statfs_arg args = {
2587 .fh = fhandle,
2588 .bitmask = server->attr_bitmask,
2589 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002590 struct nfs4_statfs_res res = {
2591 .fsstat = fsstat,
2592 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 struct rpc_message msg = {
2594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2595 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002596 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 };
2598
Trond Myklebust0e574af2005-10-27 22:12:38 -04002599 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002600 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601}
2602
2603static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2604{
2605 struct nfs4_exception exception = { };
2606 int err;
2607 do {
2608 err = nfs4_handle_exception(server,
2609 _nfs4_proc_statfs(server, fhandle, fsstat),
2610 &exception);
2611 } while (exception.retry);
2612 return err;
2613}
2614
2615static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2616 struct nfs_fsinfo *fsinfo)
2617{
2618 struct nfs4_fsinfo_arg args = {
2619 .fh = fhandle,
2620 .bitmask = server->attr_bitmask,
2621 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002622 struct nfs4_fsinfo_res res = {
2623 .fsinfo = fsinfo,
2624 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 struct rpc_message msg = {
2626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2627 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002628 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 };
2630
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002631 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632}
2633
2634static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2635{
2636 struct nfs4_exception exception = { };
2637 int err;
2638
2639 do {
2640 err = nfs4_handle_exception(server,
2641 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2642 &exception);
2643 } while (exception.retry);
2644 return err;
2645}
2646
2647static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2648{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002649 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2651}
2652
2653static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2654 struct nfs_pathconf *pathconf)
2655{
2656 struct nfs4_pathconf_arg args = {
2657 .fh = fhandle,
2658 .bitmask = server->attr_bitmask,
2659 };
Benny Halevyd45b2982009-04-01 09:21:58 -04002660 struct nfs4_pathconf_res res = {
2661 .pathconf = pathconf,
2662 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 struct rpc_message msg = {
2664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2665 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04002666 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 };
2668
2669 /* None of the pathconf attributes are mandatory to implement */
2670 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2671 memset(pathconf, 0, sizeof(*pathconf));
2672 return 0;
2673 }
2674
Trond Myklebust0e574af2005-10-27 22:12:38 -04002675 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002676 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
2678
2679static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2680 struct nfs_pathconf *pathconf)
2681{
2682 struct nfs4_exception exception = { };
2683 int err;
2684
2685 do {
2686 err = nfs4_handle_exception(server,
2687 _nfs4_proc_pathconf(server, fhandle, pathconf),
2688 &exception);
2689 } while (exception.retry);
2690 return err;
2691}
2692
Trond Myklebustec06c092006-03-20 13:44:27 -05002693static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694{
Trond Myklebustec06c092006-03-20 13:44:27 -05002695 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002697 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 rpc_restart_call(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05002699 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
Trond Myklebust8850df92007-09-28 17:20:07 -04002701
2702 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002704 renew_lease(server, data->timestamp);
2705 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706}
2707
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002708static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002711 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712}
2713
Trond Myklebust788e7a82006-03-20 13:44:27 -05002714static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 struct inode *inode = data->inode;
2717
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002718 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002720 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002722 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04002724 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002725 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05002726 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727}
2728
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002729static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002731 struct nfs_server *server = NFS_SERVER(data->inode);
2732
Trond Myklebusta65318b2009-03-11 14:10:28 -04002733 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002734 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 data->timestamp = jiffies;
2736
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002737 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738}
2739
Trond Myklebust788e7a82006-03-20 13:44:27 -05002740static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 struct inode *inode = data->inode;
2743
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002744 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002746 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04002748 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002749 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750}
2751
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002752static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753{
Trond Myklebust788e7a82006-03-20 13:44:27 -05002754 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
Trond Myklebusta65318b2009-03-11 14:10:28 -04002756 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002757 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002758 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759}
2760
2761/*
2762 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2763 * standalone procedure for queueing an asynchronous RENEW.
2764 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002765static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
David Howellsadfa6f92006-08-22 20:06:08 -04002767 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002768 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
2770 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04002771 /* Unless we're shutting down, schedule state recovery! */
2772 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
2773 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 return;
2775 }
2776 spin_lock(&clp->cl_lock);
2777 if (time_before(clp->cl_last_renewal,timestamp))
2778 clp->cl_last_renewal = timestamp;
2779 spin_unlock(&clp->cl_lock);
2780}
2781
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002782static const struct rpc_call_ops nfs4_renew_ops = {
2783 .rpc_call_done = nfs4_renew_done,
2784};
2785
David Howellsadfa6f92006-08-22 20:06:08 -04002786int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787{
2788 struct rpc_message msg = {
2789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2790 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002791 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 };
2793
2794 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002795 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796}
2797
David Howellsadfa6f92006-08-22 20:06:08 -04002798int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
2800 struct rpc_message msg = {
2801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2802 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002803 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 };
2805 unsigned long now = jiffies;
2806 int status;
2807
2808 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2809 if (status < 0)
2810 return status;
2811 spin_lock(&clp->cl_lock);
2812 if (time_before(clp->cl_last_renewal,now))
2813 clp->cl_last_renewal = now;
2814 spin_unlock(&clp->cl_lock);
2815 return 0;
2816}
2817
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002818static inline int nfs4_server_supports_acls(struct nfs_server *server)
2819{
2820 return (server->caps & NFS_CAP_ACLS)
2821 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2822 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2823}
2824
2825/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2826 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2827 * the stack.
2828 */
2829#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2830
2831static void buf_to_pages(const void *buf, size_t buflen,
2832 struct page **pages, unsigned int *pgbase)
2833{
2834 const void *p = buf;
2835
2836 *pgbase = offset_in_page(buf);
2837 p -= *pgbase;
2838 while (p < buf + buflen) {
2839 *(pages++) = virt_to_page(p);
2840 p += PAGE_CACHE_SIZE;
2841 }
2842}
2843
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002844struct nfs4_cached_acl {
2845 int cached;
2846 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002847 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002848};
2849
2850static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002851{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002852 struct nfs_inode *nfsi = NFS_I(inode);
2853
2854 spin_lock(&inode->i_lock);
2855 kfree(nfsi->nfs4_acl);
2856 nfsi->nfs4_acl = acl;
2857 spin_unlock(&inode->i_lock);
2858}
2859
2860static void nfs4_zap_acl_attr(struct inode *inode)
2861{
2862 nfs4_set_cached_acl(inode, NULL);
2863}
2864
2865static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2866{
2867 struct nfs_inode *nfsi = NFS_I(inode);
2868 struct nfs4_cached_acl *acl;
2869 int ret = -ENOENT;
2870
2871 spin_lock(&inode->i_lock);
2872 acl = nfsi->nfs4_acl;
2873 if (acl == NULL)
2874 goto out;
2875 if (buf == NULL) /* user is just asking for length */
2876 goto out_len;
2877 if (acl->cached == 0)
2878 goto out;
2879 ret = -ERANGE; /* see getxattr(2) man page */
2880 if (acl->len > buflen)
2881 goto out;
2882 memcpy(buf, acl->data, acl->len);
2883out_len:
2884 ret = acl->len;
2885out:
2886 spin_unlock(&inode->i_lock);
2887 return ret;
2888}
2889
2890static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2891{
2892 struct nfs4_cached_acl *acl;
2893
2894 if (buf && acl_len <= PAGE_SIZE) {
2895 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2896 if (acl == NULL)
2897 goto out;
2898 acl->cached = 1;
2899 memcpy(acl->data, buf, acl_len);
2900 } else {
2901 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2902 if (acl == NULL)
2903 goto out;
2904 acl->cached = 0;
2905 }
2906 acl->len = acl_len;
2907out:
2908 nfs4_set_cached_acl(inode, acl);
2909}
2910
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002911static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002912{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002913 struct page *pages[NFS4ACL_MAXPAGES];
2914 struct nfs_getaclargs args = {
2915 .fh = NFS_FH(inode),
2916 .acl_pages = pages,
2917 .acl_len = buflen,
2918 };
Benny Halevy663c79b2009-04-01 09:21:59 -04002919 struct nfs_getaclres res = {
2920 .acl_len = buflen,
2921 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002922 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002923 struct rpc_message msg = {
2924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2925 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04002926 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002927 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002928 struct page *localpage = NULL;
2929 int ret;
2930
2931 if (buflen < PAGE_SIZE) {
2932 /* As long as we're doing a round trip to the server anyway,
2933 * let's be prepared for a page of acl data. */
2934 localpage = alloc_page(GFP_KERNEL);
2935 resp_buf = page_address(localpage);
2936 if (localpage == NULL)
2937 return -ENOMEM;
2938 args.acl_pages[0] = localpage;
2939 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04002940 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002941 } else {
2942 resp_buf = buf;
2943 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2944 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002945 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002946 if (ret)
2947 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04002948 if (res.acl_len > args.acl_len)
2949 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002950 else
Benny Halevy663c79b2009-04-01 09:21:59 -04002951 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002952 if (buf) {
2953 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04002954 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002955 goto out_free;
2956 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04002957 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002958 }
Benny Halevy663c79b2009-04-01 09:21:59 -04002959 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002960out_free:
2961 if (localpage)
2962 __free_page(localpage);
2963 return ret;
2964}
2965
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002966static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2967{
2968 struct nfs4_exception exception = { };
2969 ssize_t ret;
2970 do {
2971 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
2972 if (ret >= 0)
2973 break;
2974 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
2975 } while (exception.retry);
2976 return ret;
2977}
2978
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002979static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2980{
2981 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002982 int ret;
2983
2984 if (!nfs4_server_supports_acls(server))
2985 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002986 ret = nfs_revalidate_inode(server, inode);
2987 if (ret < 0)
2988 return ret;
Trond Myklebustf41f7412008-06-11 17:39:04 -04002989 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
2990 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002991 ret = nfs4_read_cached_acl(inode, buf, buflen);
2992 if (ret != -ENOENT)
2993 return ret;
2994 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002995}
2996
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002997static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002998{
2999 struct nfs_server *server = NFS_SERVER(inode);
3000 struct page *pages[NFS4ACL_MAXPAGES];
3001 struct nfs_setaclargs arg = {
3002 .fh = NFS_FH(inode),
3003 .acl_pages = pages,
3004 .acl_len = buflen,
3005 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003006 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003007 struct rpc_message msg = {
3008 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3009 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003010 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003011 };
3012 int ret;
3013
3014 if (!nfs4_server_supports_acls(server))
3015 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003016 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003017 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003018 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003019 nfs_access_zap_cache(inode);
3020 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003021 return ret;
3022}
3023
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003024static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3025{
3026 struct nfs4_exception exception = { };
3027 int err;
3028 do {
3029 err = nfs4_handle_exception(NFS_SERVER(inode),
3030 __nfs4_proc_set_acl(inode, buf, buflen),
3031 &exception);
3032 } while (exception.retry);
3033 return err;
3034}
3035
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036static int
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003037nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
David Howells7539bba2006-08-22 20:06:09 -04003039 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
3041 if (!clp || task->tk_status >= 0)
3042 return 0;
3043 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003044 case -NFS4ERR_ADMIN_REVOKED:
3045 case -NFS4ERR_BAD_STATEID:
3046 case -NFS4ERR_OPENMODE:
3047 if (state == NULL)
3048 break;
3049 nfs4_state_mark_reclaim_nograce(clp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 case -NFS4ERR_STALE_CLIENTID:
3051 case -NFS4ERR_STALE_STATEID:
3052 case -NFS4ERR_EXPIRED:
Trond Myklebust5d008372008-02-22 16:34:17 -05003053 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 nfs4_schedule_state_recovery(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05003055 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
Trond Myklebustfda13932008-02-22 16:34:12 -05003056 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 task->tk_status = 0;
3058 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 case -NFS4ERR_DELAY:
Trond Myklebust2e96d282008-06-11 16:42:05 -04003060 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003061 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3063 task->tk_status = 0;
3064 return -EAGAIN;
3065 case -NFS4ERR_OLD_STATEID:
3066 task->tk_status = 0;
3067 return -EAGAIN;
3068 }
3069 task->tk_status = nfs4_map_errors(task->tk_status);
3070 return 0;
3071}
3072
David Howellsadfa6f92006-08-22 20:06:08 -04003073int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074{
3075 nfs4_verifier sc_verifier;
3076 struct nfs4_setclientid setclientid = {
3077 .sc_verifier = &sc_verifier,
3078 .sc_prog = program,
3079 };
3080 struct rpc_message msg = {
3081 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3082 .rpc_argp = &setclientid,
3083 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003084 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 };
Al Virobc4785c2006-10-19 23:28:51 -07003086 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 int loop = 0;
3088 int status;
3089
Al Virobc4785c2006-10-19 23:28:51 -07003090 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3092 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3093
3094 for(;;) {
3095 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003096 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003097 clp->cl_ipaddr,
3098 rpc_peeraddr2str(clp->cl_rpcclient,
3099 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003100 rpc_peeraddr2str(clp->cl_rpcclient,
3101 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003102 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 clp->cl_id_uniquifier);
3104 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003105 sizeof(setclientid.sc_netid),
3106 rpc_peeraddr2str(clp->cl_rpcclient,
3107 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003109 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 clp->cl_ipaddr, port >> 8, port & 255);
3111
3112 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3113 if (status != -NFS4ERR_CLID_INUSE)
3114 break;
3115 if (signalled())
3116 break;
3117 if (loop++ & 1)
3118 ssleep(clp->cl_lease_time + 1);
3119 else
3120 if (++clp->cl_id_uniquifier == 0)
3121 break;
3122 }
3123 return status;
3124}
3125
David Howellsadfa6f92006-08-22 20:06:08 -04003126static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
3128 struct nfs_fsinfo fsinfo;
3129 struct rpc_message msg = {
3130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3131 .rpc_argp = clp,
3132 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003133 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 };
3135 unsigned long now;
3136 int status;
3137
3138 now = jiffies;
3139 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3140 if (status == 0) {
3141 spin_lock(&clp->cl_lock);
3142 clp->cl_lease_time = fsinfo.lease_time * HZ;
3143 clp->cl_last_renewal = now;
3144 spin_unlock(&clp->cl_lock);
3145 }
3146 return status;
3147}
3148
David Howellsadfa6f92006-08-22 20:06:08 -04003149int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003150{
Benny Halevy77e03672008-06-24 20:25:57 +03003151 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003152 int err;
3153 do {
3154 err = _nfs4_proc_setclientid_confirm(clp, cred);
3155 switch (err) {
3156 case 0:
3157 return err;
3158 case -NFS4ERR_RESOURCE:
3159 /* The IBM lawyers misread another document! */
3160 case -NFS4ERR_DELAY:
3161 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3162 }
3163 } while (err == 0);
3164 return err;
3165}
3166
Trond Myklebustfe650402006-01-03 09:55:18 +01003167struct nfs4_delegreturndata {
3168 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003169 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003170 struct nfs_fh fh;
3171 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003172 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003173 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003174 int rpc_status;
3175};
3176
Trond Myklebustfe650402006-01-03 09:55:18 +01003177static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3178{
3179 struct nfs4_delegreturndata *data = calldata;
3180 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003181 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01003182 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01003183}
3184
3185static void nfs4_delegreturn_release(void *calldata)
3186{
Trond Myklebustfe650402006-01-03 09:55:18 +01003187 kfree(calldata);
3188}
3189
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003190static const struct rpc_call_ops nfs4_delegreturn_ops = {
Trond Myklebustfe650402006-01-03 09:55:18 +01003191 .rpc_call_done = nfs4_delegreturn_done,
3192 .rpc_release = nfs4_delegreturn_release,
3193};
3194
Trond Myklebuste6f81072008-01-24 18:14:34 -05003195static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003196{
3197 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003198 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003199 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003200 struct rpc_message msg = {
3201 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3202 .rpc_cred = cred,
3203 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003204 struct rpc_task_setup task_setup_data = {
3205 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003206 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003207 .callback_ops = &nfs4_delegreturn_ops,
3208 .flags = RPC_TASK_ASYNC,
3209 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003210 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003211
3212 data = kmalloc(sizeof(*data), GFP_KERNEL);
3213 if (data == NULL)
3214 return -ENOMEM;
3215 data->args.fhandle = &data->fh;
3216 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003217 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003218 nfs_copy_fh(&data->fh, NFS_FH(inode));
3219 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003220 data->res.fattr = &data->fattr;
3221 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003222 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003223 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003224 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003225 data->rpc_status = 0;
3226
Trond Myklebustc970aa82007-07-14 15:39:59 -04003227 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003228 msg.rpc_argp = &data->args,
3229 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003230 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003231 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003232 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003233 if (!issync)
3234 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003235 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003236 if (status != 0)
3237 goto out;
3238 status = data->rpc_status;
3239 if (status != 0)
3240 goto out;
3241 nfs_refresh_inode(inode, &data->fattr);
3242out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003243 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003244 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245}
3246
Trond Myklebuste6f81072008-01-24 18:14:34 -05003247int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248{
3249 struct nfs_server *server = NFS_SERVER(inode);
3250 struct nfs4_exception exception = { };
3251 int err;
3252 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003253 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 switch (err) {
3255 case -NFS4ERR_STALE_STATEID:
3256 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 case 0:
3258 return 0;
3259 }
3260 err = nfs4_handle_exception(server, err, &exception);
3261 } while (exception.retry);
3262 return err;
3263}
3264
3265#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3266#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3267
3268/*
3269 * sleep, with exponential backoff, and retry the LOCK operation.
3270 */
3271static unsigned long
3272nfs4_set_lock_task_retry(unsigned long timeout)
3273{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003274 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 timeout <<= 1;
3276 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3277 return NFS4_LOCK_MAXTIMEOUT;
3278 return timeout;
3279}
3280
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3282{
3283 struct inode *inode = state->inode;
3284 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003285 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003286 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003288 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003290 struct nfs_lockt_res res = {
3291 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 };
3293 struct rpc_message msg = {
3294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3295 .rpc_argp = &arg,
3296 .rpc_resp = &res,
3297 .rpc_cred = state->owner->so_cred,
3298 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 struct nfs4_lock_state *lsp;
3300 int status;
3301
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003302 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003303 status = nfs4_set_lock_state(state, request);
3304 if (status != 0)
3305 goto out;
3306 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003307 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003308 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003309 switch (status) {
3310 case 0:
3311 request->fl_type = F_UNLCK;
3312 break;
3313 case -NFS4ERR_DENIED:
3314 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003316 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003317out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 return status;
3319}
3320
3321static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3322{
3323 struct nfs4_exception exception = { };
3324 int err;
3325
3326 do {
3327 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3328 _nfs4_proc_getlk(state, cmd, request),
3329 &exception);
3330 } while (exception.retry);
3331 return err;
3332}
3333
3334static int do_vfs_lock(struct file *file, struct file_lock *fl)
3335{
3336 int res = 0;
3337 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3338 case FL_POSIX:
3339 res = posix_lock_file_wait(file, fl);
3340 break;
3341 case FL_FLOCK:
3342 res = flock_lock_file_wait(file, fl);
3343 break;
3344 default:
3345 BUG();
3346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 return res;
3348}
3349
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003350struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003351 struct nfs_locku_args arg;
3352 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003353 struct nfs4_lock_state *lsp;
3354 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003355 struct file_lock fl;
3356 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003357 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003358};
3359
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003360static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3361 struct nfs_open_context *ctx,
3362 struct nfs4_lock_state *lsp,
3363 struct nfs_seqid *seqid)
3364{
3365 struct nfs4_unlockdata *p;
3366 struct inode *inode = lsp->ls_state->inode;
3367
3368 p = kmalloc(sizeof(*p), GFP_KERNEL);
3369 if (p == NULL)
3370 return NULL;
3371 p->arg.fh = NFS_FH(inode);
3372 p->arg.fl = &p->fl;
3373 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003374 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003375 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003376 p->arg.stateid = &lsp->ls_stateid;
3377 p->lsp = lsp;
3378 atomic_inc(&lsp->ls_count);
3379 /* Ensure we don't close file until we're done freeing locks! */
3380 p->ctx = get_nfs_open_context(ctx);
3381 memcpy(&p->fl, fl, sizeof(p->fl));
3382 p->server = NFS_SERVER(inode);
3383 return p;
3384}
3385
Trond Myklebust06f814a2006-01-03 09:55:07 +01003386static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003387{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003388 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003389 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003390 nfs4_put_lock_state(calldata->lsp);
3391 put_nfs_open_context(calldata->ctx);
3392 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003393}
3394
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003395static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003396{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003397 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003398
Trond Myklebust06f814a2006-01-03 09:55:07 +01003399 if (RPC_ASSASSINATED(task))
3400 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003401 switch (task->tk_status) {
3402 case 0:
3403 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003404 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003405 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003406 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003407 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003408 case -NFS4ERR_BAD_STATEID:
3409 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003410 case -NFS4ERR_STALE_STATEID:
3411 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003412 break;
3413 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003414 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003415 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003416 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003417}
3418
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003419static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003420{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003421 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003423 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003424 return;
3425 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003426 /* Note: exit _without_ running nfs4_locku_done */
3427 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003428 return;
3429 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003430 calldata->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003431 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432}
3433
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003434static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003435 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003436 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003437 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003438};
3439
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003440static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3441 struct nfs_open_context *ctx,
3442 struct nfs4_lock_state *lsp,
3443 struct nfs_seqid *seqid)
3444{
3445 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003446 struct rpc_message msg = {
3447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3448 .rpc_cred = ctx->cred,
3449 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003450 struct rpc_task_setup task_setup_data = {
3451 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003452 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003453 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003454 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003455 .flags = RPC_TASK_ASYNC,
3456 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003457
Frank Filz137d6ac2007-07-09 15:32:29 -07003458 /* Ensure this is an unlock - when canceling a lock, the
3459 * canceled lock is passed in, and it won't be an unlock.
3460 */
3461 fl->fl_type = F_UNLCK;
3462
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003463 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3464 if (data == NULL) {
3465 nfs_free_seqid(seqid);
3466 return ERR_PTR(-ENOMEM);
3467 }
3468
Trond Myklebust5138fde2007-07-14 15:40:01 -04003469 msg.rpc_argp = &data->arg,
3470 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003471 task_setup_data.callback_data = data;
3472 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003473}
3474
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3476{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003477 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003478 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003479 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003480 struct rpc_task *task;
3481 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003482 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Trond Myklebust9b073572006-06-29 16:38:34 -04003484 status = nfs4_set_lock_state(state, request);
3485 /* Unlock _before_ we do the RPC call */
3486 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003487 down_read(&nfsi->rwsem);
3488 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3489 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003490 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003491 }
3492 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003493 if (status != 0)
3494 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003495 /* Is this a delegated lock? */
3496 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003497 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003498 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003499 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003500 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003501 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003502 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003503 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003504 status = PTR_ERR(task);
3505 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003506 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003507 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003508 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003509out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003510 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003511 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512}
3513
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003514struct nfs4_lockdata {
3515 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003516 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003517 struct nfs4_lock_state *lsp;
3518 struct nfs_open_context *ctx;
3519 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003520 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003521 int rpc_status;
3522 int cancelled;
3523};
3524
3525static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3526 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3527{
3528 struct nfs4_lockdata *p;
3529 struct inode *inode = lsp->ls_state->inode;
3530 struct nfs_server *server = NFS_SERVER(inode);
3531
3532 p = kzalloc(sizeof(*p), GFP_KERNEL);
3533 if (p == NULL)
3534 return NULL;
3535
3536 p->arg.fh = NFS_FH(inode);
3537 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003538 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3539 if (p->arg.open_seqid == NULL)
3540 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003541 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3542 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003543 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003544 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003545 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003546 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003547 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003548 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003549 p->lsp = lsp;
3550 atomic_inc(&lsp->ls_count);
3551 p->ctx = get_nfs_open_context(ctx);
3552 memcpy(&p->fl, fl, sizeof(p->fl));
3553 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003554out_free_seqid:
3555 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003556out_free:
3557 kfree(p);
3558 return NULL;
3559}
3560
3561static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3562{
3563 struct nfs4_lockdata *data = calldata;
3564 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
Harvey Harrison3110ff82008-05-02 13:42:44 -07003566 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003567 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3568 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003569 /* Do we need to do an open_to_lock_owner? */
3570 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05003571 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3572 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003573 data->arg.open_stateid = &state->stateid;
3574 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003575 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003576 } else
3577 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003578 data->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003579 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003580 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003581}
3582
3583static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3584{
3585 struct nfs4_lockdata *data = calldata;
3586
Harvey Harrison3110ff82008-05-02 13:42:44 -07003587 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003588
3589 data->rpc_status = task->tk_status;
3590 if (RPC_ASSASSINATED(task))
3591 goto out;
3592 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003593 if (data->rpc_status == 0)
3594 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3595 else
3596 goto out;
3597 }
3598 if (data->rpc_status == 0) {
3599 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3600 sizeof(data->lsp->ls_stateid.data));
3601 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003602 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003603 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003604out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07003605 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003606}
3607
3608static void nfs4_lock_release(void *calldata)
3609{
3610 struct nfs4_lockdata *data = calldata;
3611
Harvey Harrison3110ff82008-05-02 13:42:44 -07003612 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003613 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003614 if (data->cancelled != 0) {
3615 struct rpc_task *task;
3616 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3617 data->arg.lock_seqid);
3618 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003619 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003620 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003621 } else
3622 nfs_free_seqid(data->arg.lock_seqid);
3623 nfs4_put_lock_state(data->lsp);
3624 put_nfs_open_context(data->ctx);
3625 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003626 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003627}
3628
3629static const struct rpc_call_ops nfs4_lock_ops = {
3630 .rpc_call_prepare = nfs4_lock_prepare,
3631 .rpc_call_done = nfs4_lock_done,
3632 .rpc_release = nfs4_lock_release,
3633};
3634
3635static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3636{
3637 struct nfs4_lockdata *data;
3638 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003639 struct rpc_message msg = {
3640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3641 .rpc_cred = state->owner->so_cred,
3642 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003643 struct rpc_task_setup task_setup_data = {
3644 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003645 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003646 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003647 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003648 .flags = RPC_TASK_ASYNC,
3649 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003650 int ret;
3651
Harvey Harrison3110ff82008-05-02 13:42:44 -07003652 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003653 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003654 fl->fl_u.nfs4_fl.owner);
3655 if (data == NULL)
3656 return -ENOMEM;
3657 if (IS_SETLKW(cmd))
3658 data->arg.block = 1;
3659 if (reclaim != 0)
3660 data->arg.reclaim = 1;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003661 msg.rpc_argp = &data->arg,
3662 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003663 task_setup_data.callback_data = data;
3664 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003665 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003666 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003667 ret = nfs4_wait_for_completion_rpc_task(task);
3668 if (ret == 0) {
3669 ret = data->rpc_status;
3670 if (ret == -NFS4ERR_DENIED)
3671 ret = -EAGAIN;
3672 } else
3673 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003674 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003675 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003676 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677}
3678
3679static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3680{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003681 struct nfs_server *server = NFS_SERVER(state->inode);
3682 struct nfs4_exception exception = { };
3683 int err;
3684
3685 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003686 /* Cache the lock if possible... */
3687 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3688 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003689 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3690 if (err != -NFS4ERR_DELAY)
3691 break;
3692 nfs4_handle_exception(server, err, &exception);
3693 } while (exception.retry);
3694 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695}
3696
3697static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3698{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003699 struct nfs_server *server = NFS_SERVER(state->inode);
3700 struct nfs4_exception exception = { };
3701 int err;
3702
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003703 err = nfs4_set_lock_state(state, request);
3704 if (err != 0)
3705 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003706 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003707 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3708 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003709 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3710 if (err != -NFS4ERR_DELAY)
3711 break;
3712 nfs4_handle_exception(server, err, &exception);
3713 } while (exception.retry);
3714 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715}
3716
3717static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3718{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003719 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003720 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 int status;
3722
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003723 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003724 status = nfs4_set_lock_state(state, request);
3725 if (status != 0)
3726 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003727 request->fl_flags |= FL_ACCESS;
3728 status = do_vfs_lock(request->fl_file, request);
3729 if (status < 0)
3730 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003731 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003732 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04003733 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003734 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05003735 request->fl_flags = fl_flags & ~FL_SLEEP;
3736 status = do_vfs_lock(request->fl_file, request);
3737 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003738 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003739 status = _nfs4_do_setlk(state, cmd, request, 0);
3740 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04003741 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003742 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003743 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003744 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07003745 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003746out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05003747 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003748out:
3749 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 return status;
3751}
3752
3753static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3754{
3755 struct nfs4_exception exception = { };
3756 int err;
3757
3758 do {
3759 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3760 _nfs4_proc_setlk(state, cmd, request),
3761 &exception);
3762 } while (exception.retry);
3763 return err;
3764}
3765
3766static int
3767nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3768{
3769 struct nfs_open_context *ctx;
3770 struct nfs4_state *state;
3771 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3772 int status;
3773
3774 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003775 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 state = ctx->state;
3777
3778 if (request->fl_start < 0 || request->fl_end < 0)
3779 return -EINVAL;
3780
3781 if (IS_GETLK(cmd))
3782 return nfs4_proc_getlk(state, F_GETLK, request);
3783
3784 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3785 return -EINVAL;
3786
3787 if (request->fl_type == F_UNLCK)
3788 return nfs4_proc_unlck(state, cmd, request);
3789
3790 do {
3791 status = nfs4_proc_setlk(state, cmd, request);
3792 if ((status != -EAGAIN) || IS_SETLK(cmd))
3793 break;
3794 timeout = nfs4_set_lock_task_retry(timeout);
3795 status = -ERESTARTSYS;
3796 if (signalled())
3797 break;
3798 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 return status;
3800}
3801
Trond Myklebust888e6942005-11-04 15:38:11 -05003802int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3803{
3804 struct nfs_server *server = NFS_SERVER(state->inode);
3805 struct nfs4_exception exception = { };
3806 int err;
3807
3808 err = nfs4_set_lock_state(state, fl);
3809 if (err != 0)
3810 goto out;
3811 do {
3812 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3813 if (err != -NFS4ERR_DELAY)
3814 break;
3815 err = nfs4_handle_exception(server, err, &exception);
3816 } while (exception.retry);
3817out:
3818 return err;
3819}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003820
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003821#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3822
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003823int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3824 size_t buflen, int flags)
3825{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003826 struct inode *inode = dentry->d_inode;
3827
3828 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3829 return -EOPNOTSUPP;
3830
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003831 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003832}
3833
3834/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3835 * and that's what we'll do for e.g. user attributes that haven't been set.
3836 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3837 * attributes in kernel-managed attribute namespaces. */
3838ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3839 size_t buflen)
3840{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003841 struct inode *inode = dentry->d_inode;
3842
3843 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3844 return -EOPNOTSUPP;
3845
3846 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003847}
3848
3849ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3850{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003851 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003852
J. Bruce Fields096455a2006-03-20 23:23:42 -05003853 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3854 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003855 if (buf && buflen < len)
3856 return -ERANGE;
3857 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003858 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3859 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003860}
3861
Trond Myklebust69aaaae2009-03-11 14:10:28 -04003862static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
3863{
3864 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
3865 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
3866 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
3867 return;
3868
3869 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3870 NFS_ATTR_FATTR_NLINK;
3871 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3872 fattr->nlink = 2;
3873}
3874
Trond Myklebust56659e92007-07-17 21:52:39 -04003875int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003876 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04003877{
3878 struct nfs_server *server = NFS_SERVER(dir);
3879 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04003880 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3881 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003882 };
3883 struct nfs4_fs_locations_arg args = {
3884 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05003885 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003886 .page = page,
3887 .bitmask = bitmask,
3888 };
Benny Halevy22958462009-04-01 09:22:02 -04003889 struct nfs4_fs_locations_res res = {
3890 .fs_locations = fs_locations,
3891 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04003892 struct rpc_message msg = {
3893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3894 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04003895 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003896 };
3897 int status;
3898
Harvey Harrison3110ff82008-05-02 13:42:44 -07003899 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05003900 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04003901 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04003902 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003903 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04003904 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003905 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04003906 return status;
3907}
3908
Andy Adamson557134a2009-04-01 09:21:53 -04003909#ifdef CONFIG_NFS_V4_1
3910/* Destroy the slot table */
3911static void nfs4_destroy_slot_table(struct nfs4_session *session)
3912{
3913 if (session->fc_slot_table.slots == NULL)
3914 return;
3915 kfree(session->fc_slot_table.slots);
3916 session->fc_slot_table.slots = NULL;
3917 return;
3918}
3919
3920struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
3921{
3922 struct nfs4_session *session;
3923 struct nfs4_slot_table *tbl;
3924
3925 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
3926 if (!session)
3927 return NULL;
3928 tbl = &session->fc_slot_table;
3929 spin_lock_init(&tbl->slot_tbl_lock);
3930 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "Slot table");
3931 session->clp = clp;
3932 return session;
3933}
3934
3935void nfs4_destroy_session(struct nfs4_session *session)
3936{
3937 nfs4_destroy_slot_table(session);
3938 kfree(session);
3939}
3940
3941#endif /* CONFIG_NFS_V4_1 */
3942
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05003944 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05003945 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 .recover_open = nfs4_open_reclaim,
3947 .recover_lock = nfs4_lock_reclaim,
3948};
3949
Trond Myklebustb79a4a12008-12-23 15:21:41 -05003950struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05003951 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05003952 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 .recover_open = nfs4_open_expired,
3954 .recover_lock = nfs4_lock_expired,
3955};
3956
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003957static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003958 .permission = nfs_permission,
3959 .getattr = nfs_getattr,
3960 .setattr = nfs_setattr,
3961 .getxattr = nfs4_getxattr,
3962 .setxattr = nfs4_setxattr,
3963 .listxattr = nfs4_listxattr,
3964};
3965
David Howells509de812006-08-22 20:06:11 -04003966const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 .version = 4, /* protocol version */
3968 .dentry_ops = &nfs4_dentry_operations,
3969 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003970 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 .getroot = nfs4_proc_get_root,
3972 .getattr = nfs4_proc_getattr,
3973 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04003974 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 .lookup = nfs4_proc_lookup,
3976 .access = nfs4_proc_access,
3977 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 .create = nfs4_proc_create,
3979 .remove = nfs4_proc_remove,
3980 .unlink_setup = nfs4_proc_unlink_setup,
3981 .unlink_done = nfs4_proc_unlink_done,
3982 .rename = nfs4_proc_rename,
3983 .link = nfs4_proc_link,
3984 .symlink = nfs4_proc_symlink,
3985 .mkdir = nfs4_proc_mkdir,
3986 .rmdir = nfs4_proc_remove,
3987 .readdir = nfs4_proc_readdir,
3988 .mknod = nfs4_proc_mknod,
3989 .statfs = nfs4_proc_statfs,
3990 .fsinfo = nfs4_proc_fsinfo,
3991 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04003992 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 .decode_dirent = nfs4_decode_dirent,
3994 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05003995 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003997 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05003999 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004001 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04004002 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003};
4004
4005/*
4006 * Local variables:
4007 * c-basic-offset: 8
4008 * End:
4009 */