blob: c9618080317ed0f12db5adefe263d7d9643dddab [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.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400283 *
284 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400285 */
286static u8
287nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
288{
289 int slotid;
290 u8 ret_id = NFS4_MAX_SLOT_TABLE;
291 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
292
Benny Halevy510b8172009-04-01 09:22:14 -0400293 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
294 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
295 tbl->max_slots);
296 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
297 if (slotid >= tbl->max_slots)
298 goto out;
299 __set_bit(slotid, tbl->used_slots);
300 if (slotid > tbl->highest_used_slotid)
301 tbl->highest_used_slotid = slotid;
302 ret_id = slotid;
303out:
304 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
305 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400306 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{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400315 struct nfs4_slot *slot;
316 struct nfs4_slot_table *tbl;
317 u8 slotid;
318
319 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400320 /* slot already allocated? */
321 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
322 return 0;
323
324 memset(res, 0, sizeof(*res));
325 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400326 tbl = &session->fc_slot_table;
327
328 spin_lock(&tbl->slot_tbl_lock);
329 slotid = nfs4_find_slot(tbl, task);
330 if (slotid == NFS4_MAX_SLOT_TABLE) {
331 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
332 spin_unlock(&tbl->slot_tbl_lock);
333 dprintk("<-- %s: no free slots\n", __func__);
334 return -EAGAIN;
335 }
336 spin_unlock(&tbl->slot_tbl_lock);
337
338 slot = tbl->slots + slotid;
339 args->sa_slotid = slotid;
340 args->sa_cache_this = cache_reply;
341
342 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
343
344 res->sr_slotid = slotid;
345 res->sr_renewal_time = jiffies;
346 /*
347 * sr_status is only set in decode_sequence, and so will remain
348 * set to 1 if an rpc level failure occurs.
349 */
350 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400351 return 0;
352}
353
354int nfs4_setup_sequence(struct nfs_client *clp,
355 struct nfs4_sequence_args *args,
356 struct nfs4_sequence_res *res,
357 int cache_reply,
358 struct rpc_task *task)
359{
360 int ret = 0;
361
362 dprintk("--> %s clp %p session %p sr_slotid %d\n",
363 __func__, clp, clp->cl_session, res->sr_slotid);
364
365 if (!nfs4_has_session(clp))
366 goto out;
367 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
368 task);
369 if (ret != -EAGAIN) {
370 /* terminate rpc task */
371 task->tk_status = ret;
372 task->tk_action = NULL;
373 }
374out:
375 dprintk("<-- %s status=%d\n", __func__, ret);
376 return ret;
377}
378
379struct nfs41_call_sync_data {
380 struct nfs_client *clp;
381 struct nfs4_sequence_args *seq_args;
382 struct nfs4_sequence_res *seq_res;
383 int cache_reply;
384};
385
386static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
387{
388 struct nfs41_call_sync_data *data = calldata;
389
390 dprintk("--> %s data->clp->cl_session %p\n", __func__,
391 data->clp->cl_session);
392 if (nfs4_setup_sequence(data->clp, data->seq_args,
393 data->seq_res, data->cache_reply, task))
394 return;
395 rpc_call_start(task);
396}
397
398struct rpc_call_ops nfs41_call_sync_ops = {
399 .rpc_call_prepare = nfs41_call_sync_prepare,
400};
401
402static int nfs4_call_sync_sequence(struct nfs_client *clp,
403 struct rpc_clnt *clnt,
404 struct rpc_message *msg,
405 struct nfs4_sequence_args *args,
406 struct nfs4_sequence_res *res,
407 int cache_reply)
408{
409 int ret;
410 struct rpc_task *task;
411 struct nfs41_call_sync_data data = {
412 .clp = clp,
413 .seq_args = args,
414 .seq_res = res,
415 .cache_reply = cache_reply,
416 };
417 struct rpc_task_setup task_setup = {
418 .rpc_client = clnt,
419 .rpc_message = msg,
420 .callback_ops = &nfs41_call_sync_ops,
421 .callback_data = &data
422 };
423
424 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
425 task = rpc_run_task(&task_setup);
426 if (IS_ERR(task))
427 ret = PTR_ERR(task);
428 else {
429 ret = task->tk_status;
430 rpc_put_task(task);
431 }
432 return ret;
433}
434
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400435int _nfs4_call_sync_session(struct nfs_server *server,
436 struct rpc_message *msg,
437 struct nfs4_sequence_args *args,
438 struct nfs4_sequence_res *res,
439 int cache_reply)
440{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400441 return nfs4_call_sync_sequence(server->nfs_client, server->client,
442 msg, args, res, cache_reply);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400443}
444
445#endif /* CONFIG_NFS_V4_1 */
446
447int _nfs4_call_sync(struct nfs_server *server,
448 struct rpc_message *msg,
449 struct nfs4_sequence_args *args,
450 struct nfs4_sequence_res *res,
451 int cache_reply)
452{
Benny Halevyf3752972009-04-01 09:22:04 -0400453 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400454 return rpc_call_sync(server->client, msg, 0);
455}
456
457#define nfs4_call_sync(server, msg, args, res, cache_reply) \
458 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
459 &(res)->seq_res, (cache_reply))
460
Trond Myklebust38478b22006-05-25 01:40:57 -0400461static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Trond Myklebust38478b22006-05-25 01:40:57 -0400463 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Trond Myklebust38478b22006-05-25 01:40:57 -0400465 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400466 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
467 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400468 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400469 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400470 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100473struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400474 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100475 struct nfs_openargs o_arg;
476 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100477 struct nfs_open_confirmargs c_arg;
478 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100479 struct nfs_fattr f_attr;
480 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400481 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100482 struct dentry *dir;
483 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400484 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100485 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100486 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400487 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100488 int rpc_status;
489 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100490};
491
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400492
493static void nfs4_init_opendata_res(struct nfs4_opendata *p)
494{
495 p->o_res.f_attr = &p->f_attr;
496 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400497 p->o_res.seqid = p->o_arg.seqid;
498 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400499 p->o_res.server = p->o_arg.server;
500 nfs_fattr_init(&p->f_attr);
501 nfs_fattr_init(&p->dir_attr);
502}
503
Trond Myklebustad389da2007-06-05 12:30:00 -0400504static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500505 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100506 const struct iattr *attrs)
507{
Trond Myklebustad389da2007-06-05 12:30:00 -0400508 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100509 struct inode *dir = parent->d_inode;
510 struct nfs_server *server = NFS_SERVER(dir);
511 struct nfs4_opendata *p;
512
513 p = kzalloc(sizeof(*p), GFP_KERNEL);
514 if (p == NULL)
515 goto err;
516 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
517 if (p->o_arg.seqid == NULL)
518 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400519 p->path.mnt = mntget(path->mnt);
520 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100521 p->dir = parent;
522 p->owner = sp;
523 atomic_inc(&sp->so_count);
524 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500525 p->o_arg.open_flags = flags;
526 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400527 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400528 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400529 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100530 p->o_arg.server = server;
531 p->o_arg.bitmask = server->attr_bitmask;
532 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Andy Adamson5f7dbd52009-04-01 09:22:05 -0400533 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100534 if (flags & O_EXCL) {
535 u32 *s = (u32 *) p->o_arg.u.verifier.data;
536 s[0] = jiffies;
537 s[1] = current->pid;
538 } else if (flags & O_CREAT) {
539 p->o_arg.u.attrs = &p->attrs;
540 memcpy(&p->attrs, attrs, sizeof(p->attrs));
541 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100542 p->c_arg.fh = &p->o_res.fh;
543 p->c_arg.stateid = &p->o_res.stateid;
544 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400545 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400546 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100547 return p;
548err_free:
549 kfree(p);
550err:
551 dput(parent);
552 return NULL;
553}
554
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400555static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100556{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400557 struct nfs4_opendata *p = container_of(kref,
558 struct nfs4_opendata, kref);
559
560 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400561 if (p->state != NULL)
562 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400563 nfs4_put_state_owner(p->owner);
564 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700565 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400566 kfree(p);
567}
568
569static void nfs4_opendata_put(struct nfs4_opendata *p)
570{
571 if (p != NULL)
572 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100573}
574
Trond Myklebust06f814a2006-01-03 09:55:07 +0100575static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
576{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100577 int ret;
578
Trond Myklebust06f814a2006-01-03 09:55:07 +0100579 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100580 return ret;
581}
582
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500583static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400584{
585 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500586
587 if (open_mode & O_EXCL)
588 goto out;
589 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400590 case FMODE_READ:
591 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400592 break;
593 case FMODE_WRITE:
594 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400595 break;
596 case FMODE_READ|FMODE_WRITE:
597 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
598 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500599out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400600 return ret;
601}
602
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500603static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400604{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500605 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400606 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500607 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400608 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500609 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400610 return 1;
611}
612
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500613static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100614{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500615 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100616 case FMODE_WRITE:
617 state->n_wronly++;
618 break;
619 case FMODE_READ:
620 state->n_rdonly++;
621 break;
622 case FMODE_READ|FMODE_WRITE:
623 state->n_rdwr++;
624 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500625 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100626}
627
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500628static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400629{
630 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
631 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
632 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500633 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400634 case FMODE_READ:
635 set_bit(NFS_O_RDONLY_STATE, &state->flags);
636 break;
637 case FMODE_WRITE:
638 set_bit(NFS_O_WRONLY_STATE, &state->flags);
639 break;
640 case FMODE_READ|FMODE_WRITE:
641 set_bit(NFS_O_RDWR_STATE, &state->flags);
642 }
643}
644
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500645static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400646{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400647 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500648 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400649 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400650}
651
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500652static 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 -0700653{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400654 /*
655 * Protect the call to nfs4_state_set_mode_locked and
656 * serialise the stateid update
657 */
658 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400659 if (deleg_stateid != NULL) {
660 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
661 set_bit(NFS_DELEGATED_STATE, &state->flags);
662 }
663 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500664 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400665 write_sequnlock(&state->seqlock);
666 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500667 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700668 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500671static 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 -0500672{
673 struct nfs_inode *nfsi = NFS_I(state->inode);
674 struct nfs_delegation *deleg_cur;
675 int ret = 0;
676
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500677 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500678
679 rcu_read_lock();
680 deleg_cur = rcu_dereference(nfsi->delegation);
681 if (deleg_cur == NULL)
682 goto no_delegation;
683
684 spin_lock(&deleg_cur->lock);
685 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500686 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500687 goto no_delegation_unlock;
688
689 if (delegation == NULL)
690 delegation = &deleg_cur->stateid;
691 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
692 goto no_delegation_unlock;
693
Trond Myklebustb7391f42008-12-23 15:21:52 -0500694 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500695 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500696 ret = 1;
697no_delegation_unlock:
698 spin_unlock(&deleg_cur->lock);
699no_delegation:
700 rcu_read_unlock();
701
702 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500703 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500704 ret = 1;
705 }
706
707 return ret;
708}
709
710
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500711static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400712{
713 struct nfs_delegation *delegation;
714
715 rcu_read_lock();
716 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500717 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400718 rcu_read_unlock();
719 return;
720 }
721 rcu_read_unlock();
722 nfs_inode_return_delegation(inode);
723}
724
Trond Myklebust6ee41262007-07-08 14:11:36 -0400725static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400726{
727 struct nfs4_state *state = opendata->state;
728 struct nfs_inode *nfsi = NFS_I(state->inode);
729 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500730 int open_mode = opendata->o_arg.open_flags & O_EXCL;
731 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400732 nfs4_stateid stateid;
733 int ret = -EAGAIN;
734
Trond Myklebustaac00a82007-07-05 19:02:21 -0400735 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500736 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400737 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500738 if (can_open_cached(state, fmode, open_mode)) {
739 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400740 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400741 goto out_return_state;
742 }
743 spin_unlock(&state->owner->so_lock);
744 }
Trond Myklebust34310432008-12-23 15:21:38 -0500745 rcu_read_lock();
746 delegation = rcu_dereference(nfsi->delegation);
747 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500748 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500749 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400750 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500751 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400752 /* Save the delegation */
753 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
754 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400755 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400756 if (ret != 0)
757 goto out;
758 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500759
760 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500761 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500762 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400763 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400764out:
765 return ERR_PTR(ret);
766out_return_state:
767 atomic_inc(&state->count);
768 return state;
769}
770
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100771static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
772{
773 struct inode *inode;
774 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400775 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400776 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100777
Trond Myklebustaac00a82007-07-05 19:02:21 -0400778 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400779 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400780 goto out;
781 }
782
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400783 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100784 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400785 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100786 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400787 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500788 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400789 goto err;
790 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100791 state = nfs4_get_open_state(inode, data->owner);
792 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400793 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400794 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400795 int delegation_flags = 0;
796
Trond Myklebust003707c2007-07-05 18:07:55 -0400797 rcu_read_lock();
798 delegation = rcu_dereference(NFS_I(inode)->delegation);
799 if (delegation)
800 delegation_flags = delegation->flags;
801 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -0500802 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400803 nfs_inode_set_delegation(state->inode,
804 data->owner->so_cred,
805 &data->o_res);
806 else
807 nfs_inode_reclaim_delegation(state->inode,
808 data->owner->so_cred,
809 &data->o_res);
810 }
Trond Myklebust34310432008-12-23 15:21:38 -0500811
812 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500813 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100814 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400815out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100816 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400817err_put_inode:
818 iput(inode);
819err:
820 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100821}
822
Trond Myklebust864472e2006-01-03 09:55:15 +0100823static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
824{
825 struct nfs_inode *nfsi = NFS_I(state->inode);
826 struct nfs_open_context *ctx;
827
828 spin_lock(&state->inode->i_lock);
829 list_for_each_entry(ctx, &nfsi->open_files, list) {
830 if (ctx->state != state)
831 continue;
832 get_nfs_open_context(ctx);
833 spin_unlock(&state->inode->i_lock);
834 return ctx;
835 }
836 spin_unlock(&state->inode->i_lock);
837 return ERR_PTR(-ENOENT);
838}
839
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400840static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
841{
842 struct nfs4_opendata *opendata;
843
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500844 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400845 if (opendata == NULL)
846 return ERR_PTR(-ENOMEM);
847 opendata->state = state;
848 atomic_inc(&state->count);
849 return opendata;
850}
851
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500852static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +0100853{
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400854 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100855 int ret;
856
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500857 opendata->o_arg.open_flags = 0;
858 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400859 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
860 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
861 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100862 ret = _nfs4_proc_open(opendata);
863 if (ret != 0)
864 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400865 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400866 if (IS_ERR(newstate))
867 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500868 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400869 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100870 return 0;
871}
872
873static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
874{
Trond Myklebust864472e2006-01-03 09:55:15 +0100875 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100876 int ret;
877
878 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400879 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100880 smp_rmb();
881 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400882 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100883 if (ret != 0)
884 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400885 if (newstate != state)
886 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100887 }
888 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400889 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100890 if (ret != 0)
891 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400892 if (newstate != state)
893 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100894 }
895 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400896 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100897 if (ret != 0)
898 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400899 if (newstate != state)
900 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100901 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400902 /*
903 * We may have performed cached opens for all three recoveries.
904 * Check if we need to update the current stateid.
905 */
906 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
907 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400908 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400909 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
910 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400911 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400912 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100913 return 0;
914}
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916/*
917 * OPEN_RECLAIM:
918 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 */
Trond Myklebust539cd032007-06-05 11:46:42 -0400920static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400922 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +0100923 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 int status;
926
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400927 opendata = nfs4_open_recoverdata_alloc(ctx, state);
928 if (IS_ERR(opendata))
929 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100930 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
931 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400932 rcu_read_lock();
933 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -0500934 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -0400935 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400936 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +0100937 opendata->o_arg.u.delegation_type = delegation_type;
938 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400939 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return status;
941}
942
Trond Myklebust539cd032007-06-05 11:46:42 -0400943static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
945 struct nfs_server *server = NFS_SERVER(state->inode);
946 struct nfs4_exception exception = { };
947 int err;
948 do {
Trond Myklebust539cd032007-06-05 11:46:42 -0400949 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000950 if (err != -NFS4ERR_DELAY)
951 break;
952 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 } while (exception.retry);
954 return err;
955}
956
Trond Myklebust864472e2006-01-03 09:55:15 +0100957static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
958{
959 struct nfs_open_context *ctx;
960 int ret;
961
962 ctx = nfs4_state_find_open_context(state);
963 if (IS_ERR(ctx))
964 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -0400965 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +0100966 put_nfs_open_context(ctx);
967 return ret;
968}
969
Trond Myklebust13437e12007-07-06 15:10:43 -0400970static 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 -0700971{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100972 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +0100973 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400975 opendata = nfs4_open_recoverdata_alloc(ctx, state);
976 if (IS_ERR(opendata))
977 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100978 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -0400979 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100980 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +0100981 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400982 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100983 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Trond Myklebust13437e12007-07-06 15:10:43 -0400986int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -0400989 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 int err;
991 do {
Trond Myklebust13437e12007-07-06 15:10:43 -0400992 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 switch (err) {
994 case 0:
995 return err;
996 case -NFS4ERR_STALE_CLIENTID:
997 case -NFS4ERR_STALE_STATEID:
998 case -NFS4ERR_EXPIRED:
999 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001000 nfs4_schedule_state_recovery(server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return err;
1002 }
1003 err = nfs4_handle_exception(server, err, &exception);
1004 } while (exception.retry);
1005 return err;
1006}
1007
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001008static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1009{
1010 struct nfs4_opendata *data = calldata;
1011
1012 data->rpc_status = task->tk_status;
1013 if (RPC_ASSASSINATED(task))
1014 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001015 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001016 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1017 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001018 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001019 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001020 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001021 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001022}
1023
1024static void nfs4_open_confirm_release(void *calldata)
1025{
1026 struct nfs4_opendata *data = calldata;
1027 struct nfs4_state *state = NULL;
1028
1029 /* If this request hasn't been cancelled, do nothing */
1030 if (data->cancelled == 0)
1031 goto out_free;
1032 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001033 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001034 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001035 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001036 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001037 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001038out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001039 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001040}
1041
1042static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001043 .rpc_call_done = nfs4_open_confirm_done,
1044 .rpc_release = nfs4_open_confirm_release,
1045};
1046
1047/*
1048 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1049 */
1050static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1051{
1052 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1053 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001054 struct rpc_message msg = {
1055 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1056 .rpc_argp = &data->c_arg,
1057 .rpc_resp = &data->c_res,
1058 .rpc_cred = data->owner->so_cred,
1059 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001060 struct rpc_task_setup task_setup_data = {
1061 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001062 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001063 .callback_ops = &nfs4_open_confirm_ops,
1064 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001065 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001066 .flags = RPC_TASK_ASYNC,
1067 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 int status;
1069
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001070 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001071 data->rpc_done = 0;
1072 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001073 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001074 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001075 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001076 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001077 status = nfs4_wait_for_completion_rpc_task(task);
1078 if (status != 0) {
1079 data->cancelled = 1;
1080 smp_wmb();
1081 } else
1082 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001083 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return status;
1085}
1086
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001087static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001089 struct nfs4_opendata *data = calldata;
1090 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001091
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001092 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1093 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001094 /*
1095 * Check if we still need to send an OPEN call, or if we can use
1096 * a delegation instead.
1097 */
1098 if (data->state != NULL) {
1099 struct nfs_delegation *delegation;
1100
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001101 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001102 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001103 rcu_read_lock();
1104 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1105 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001106 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001107 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001108 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001109 }
1110 rcu_read_unlock();
1111 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001112 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001113 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001114 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001115 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001116 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001117 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1118 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001119 data->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001120 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001121 return;
1122out_no_action:
1123 task->tk_action = NULL;
1124
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001125}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001127static void nfs4_open_done(struct rpc_task *task, void *calldata)
1128{
1129 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001131 data->rpc_status = task->tk_status;
1132 if (RPC_ASSASSINATED(task))
1133 return;
1134 if (task->tk_status == 0) {
1135 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001136 case S_IFREG:
1137 break;
1138 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001139 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001140 break;
1141 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001142 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001143 break;
1144 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001145 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001146 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001147 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001148 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1149 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001150 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001151 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001152}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001153
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001154static void nfs4_open_release(void *calldata)
1155{
1156 struct nfs4_opendata *data = calldata;
1157 struct nfs4_state *state = NULL;
1158
1159 /* If this request hasn't been cancelled, do nothing */
1160 if (data->cancelled == 0)
1161 goto out_free;
1162 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001163 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001164 goto out_free;
1165 /* In case we need an open_confirm, no cleanup! */
1166 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1167 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001168 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001169 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001170 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001171out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001172 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001173}
1174
1175static const struct rpc_call_ops nfs4_open_ops = {
1176 .rpc_call_prepare = nfs4_open_prepare,
1177 .rpc_call_done = nfs4_open_done,
1178 .rpc_release = nfs4_open_release,
1179};
1180
1181/*
1182 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1183 */
1184static int _nfs4_proc_open(struct nfs4_opendata *data)
1185{
1186 struct inode *dir = data->dir->d_inode;
1187 struct nfs_server *server = NFS_SERVER(dir);
1188 struct nfs_openargs *o_arg = &data->o_arg;
1189 struct nfs_openres *o_res = &data->o_res;
1190 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001191 struct rpc_message msg = {
1192 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1193 .rpc_argp = o_arg,
1194 .rpc_resp = o_res,
1195 .rpc_cred = data->owner->so_cred,
1196 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001197 struct rpc_task_setup task_setup_data = {
1198 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001199 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001200 .callback_ops = &nfs4_open_ops,
1201 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001202 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001203 .flags = RPC_TASK_ASYNC,
1204 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001205 int status;
1206
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001207 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001208 data->rpc_done = 0;
1209 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001210 data->cancelled = 0;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001211 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001212 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001213 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001214 status = nfs4_wait_for_completion_rpc_task(task);
1215 if (status != 0) {
1216 data->cancelled = 1;
1217 smp_wmb();
1218 } else
1219 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001220 rpc_put_task(task);
Trond Myklebust3e309912007-07-07 13:19:59 -04001221 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001222 return status;
1223
Trond Myklebust99367812007-07-17 21:52:41 -04001224 if (o_res->fh.size == 0)
1225 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1226
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001227 if (o_arg->open_flags & O_CREAT) {
1228 update_changeattr(dir, &o_res->cinfo);
1229 nfs_post_op_update_inode(dir, o_res->dir_attr);
1230 } else
1231 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001233 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001235 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
1237 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001238 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
Trond Myklebust10afec92007-05-14 17:16:04 -04001242static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001243{
David Howells7539bba2006-08-22 20:06:09 -04001244 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6b309542006-09-14 14:03:14 -04001245 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001246
Trond Myklebust6b309542006-09-14 14:03:14 -04001247 for (;;) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001248 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001249 if (ret != 0)
1250 return ret;
Trond Myklebuste598d842008-12-23 15:21:42 -05001251 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1252 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001253 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001254 nfs4_schedule_state_recovery(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001255 }
1256 return 0;
Trond Myklebust58d97142006-01-03 09:55:24 +01001257}
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259/*
1260 * OPEN_EXPIRED:
1261 * reclaim state on the server after a network partition.
1262 * Assumes caller holds the appropriate lock
1263 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001264static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001266 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001267 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001269 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1270 if (IS_ERR(opendata))
1271 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001272 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001273 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001274 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001275 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001276 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
Trond Myklebust539cd032007-06-05 11:46:42 -04001279static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001280{
Trond Myklebust539cd032007-06-05 11:46:42 -04001281 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001282 struct nfs4_exception exception = { };
1283 int err;
1284
1285 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001286 err = _nfs4_open_expired(ctx, state);
Trond Myklebust027b6ca2008-12-23 16:04:13 -05001287 if (err != -NFS4ERR_DELAY)
1288 break;
1289 nfs4_handle_exception(server, err, &exception);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001290 } while (exception.retry);
1291 return err;
1292}
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1295{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001297 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Trond Myklebust864472e2006-01-03 09:55:15 +01001299 ctx = nfs4_state_find_open_context(state);
1300 if (IS_ERR(ctx))
1301 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001302 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001303 put_nfs_open_context(ctx);
1304 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
1307/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001308 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1309 * fields corresponding to attributes that were used to store the verifier.
1310 * Make sure we clobber those fields in the later setattr call
1311 */
1312static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1313{
1314 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1315 !(sattr->ia_valid & ATTR_ATIME_SET))
1316 sattr->ia_valid |= ATTR_ATIME;
1317
1318 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1319 !(sattr->ia_valid & ATTR_MTIME_SET))
1320 sattr->ia_valid |= ATTR_MTIME;
1321}
1322
1323/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001324 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001326static 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 -07001327{
1328 struct nfs4_state_owner *sp;
1329 struct nfs4_state *state = NULL;
1330 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001331 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001332 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 status = -ENOMEM;
1336 if (!(sp = nfs4_get_state_owner(server, cred))) {
1337 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1338 goto out_err;
1339 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001340 status = nfs4_recover_expired_lease(server);
1341 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001342 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001343 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001344 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001345 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001346 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001347 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001348 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Trond Myklebustaac00a82007-07-05 19:02:21 -04001350 if (path->dentry->d_inode != NULL)
1351 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1352
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001353 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001355 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001357 if (opendata->o_arg.open_flags & O_EXCL)
1358 nfs4_exclusive_attrset(opendata, sattr);
1359
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001360 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001361 status = PTR_ERR(state);
1362 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001363 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001364 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 *res = state;
1367 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001368err_opendata_put:
1369 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001370err_put_state_owner:
1371 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 *res = NULL;
1374 return status;
1375}
1376
1377
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001378static 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 -07001379{
1380 struct nfs4_exception exception = { };
1381 struct nfs4_state *res;
1382 int status;
1383
1384 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001385 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (status == 0)
1387 break;
1388 /* NOTE: BAD_SEQID means the server and client disagree about the
1389 * book-keeping w.r.t. state-changing operations
1390 * (OPEN/CLOSE/LOCK/LOCKU...)
1391 * It is actually a sign of a bug on the client or on the server.
1392 *
1393 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001394 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 * have unhashed the old state_owner for us, and that we can
1396 * therefore safely retry using a new one. We should still warn
1397 * the user though...
1398 */
1399 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001400 printk(KERN_WARNING "NFS: v4 server %s "
1401 " returned a bad sequence-id error!\n",
1402 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 exception.retry = 1;
1404 continue;
1405 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001406 /*
1407 * BAD_STATEID on OPEN means that the server cancelled our
1408 * state before it received the OPEN_CONFIRM.
1409 * Recover by retrying the request as per the discussion
1410 * on Page 181 of RFC3530.
1411 */
1412 if (status == -NFS4ERR_BAD_STATEID) {
1413 exception.retry = 1;
1414 continue;
1415 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001416 if (status == -EAGAIN) {
1417 /* We must have found a delegation */
1418 exception.retry = 1;
1419 continue;
1420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1422 status, &exception));
1423 } while (exception.retry);
1424 return res;
1425}
1426
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001427static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1428 struct nfs_fattr *fattr, struct iattr *sattr,
1429 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001431 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001433 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 .iap = sattr,
1435 .server = server,
1436 .bitmask = server->attr_bitmask,
1437 };
1438 struct nfs_setattrres res = {
1439 .fattr = fattr,
1440 .server = server,
1441 };
1442 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001443 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1444 .rpc_argp = &arg,
1445 .rpc_resp = &res,
1446 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001448 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001449 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Trond Myklebust0e574af2005-10-27 22:12:38 -04001451 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001453 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1454 /* Use that stateid */
1455 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001456 nfs4_copy_stateid(&arg.stateid, state, current->files);
1457 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1459
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001460 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001461 if (status == 0 && state != NULL)
1462 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001463 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001466static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1467 struct nfs_fattr *fattr, struct iattr *sattr,
1468 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001470 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 struct nfs4_exception exception = { };
1472 int err;
1473 do {
1474 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001475 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 &exception);
1477 } while (exception.retry);
1478 return err;
1479}
1480
1481struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001482 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 struct inode *inode;
1484 struct nfs4_state *state;
1485 struct nfs_closeargs arg;
1486 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001487 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001488 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489};
1490
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001491static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001492{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001493 struct nfs4_closedata *calldata = data;
1494 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001495
1496 nfs4_put_open_state(calldata->state);
1497 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001498 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001499 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001500 kfree(calldata);
1501}
1502
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001503static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001505 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 struct nfs_server *server = NFS_SERVER(calldata->inode);
1508
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001509 if (RPC_ASSASSINATED(task))
1510 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 /* hmm. we are done with the inode, and in the process of freeing
1512 * the state_owner. we keep this around to process errors
1513 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 switch (task->tk_status) {
1515 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001516 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001517 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 break;
1519 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001520 case -NFS4ERR_OLD_STATEID:
1521 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001523 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001524 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001526 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 rpc_restart_call(task);
1528 return;
1529 }
1530 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001531 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001534static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001536 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001537 struct nfs4_state *state = calldata->state;
Trond Myklebust003707c2007-07-05 18:07:55 -04001538 int clear_rd, clear_wr, clear_rdwr;
Trond Myklebust95121352005-10-18 14:20:12 -07001539
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001540 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001541 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001542
Trond Myklebust003707c2007-07-05 18:07:55 -04001543 clear_rd = clear_wr = clear_rdwr = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001544 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001545 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001546 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001547 if (state->n_rdonly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001548 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1549 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1550 }
1551 if (state->n_wronly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001552 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1553 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1554 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001555 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001556 spin_unlock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001557 if (!clear_rd && !clear_wr && !clear_rdwr) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001558 /* Note: exit _without_ calling nfs4_close_done */
1559 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001560 return;
1561 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001562 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust45328c32007-07-26 17:47:34 -04001563 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001564 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001565 calldata->arg.fmode = FMODE_READ;
Trond Myklebust45328c32007-07-26 17:47:34 -04001566 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001567 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001568 calldata->arg.fmode = FMODE_WRITE;
Trond Myklebust45328c32007-07-26 17:47:34 -04001569 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001570 calldata->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001571 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001574static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001575 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001576 .rpc_call_done = nfs4_close_done,
1577 .rpc_release = nfs4_free_closedata,
1578};
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580/*
1581 * It is possible for data to be read/written from a mem-mapped file
1582 * after the sys_close call (which hits the vfs layer as a flush).
1583 * This means that we can't safely call nfsv4 close on a file until
1584 * the inode is cleared. This in turn means that we are not good
1585 * NFSv4 citizens - we do not indicate to the server to update the file's
1586 * share state even when we are done with one of the three share
1587 * stateid's in the inode.
1588 *
1589 * NOTE: Caller must be holding the sp->so_owner semaphore!
1590 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001591int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001593 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001595 struct nfs4_state_owner *sp = state->owner;
1596 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001597 struct rpc_message msg = {
1598 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1599 .rpc_cred = state->owner->so_cred,
1600 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001601 struct rpc_task_setup task_setup_data = {
1602 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001603 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001604 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001605 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001606 .flags = RPC_TASK_ASYNC,
1607 };
Trond Myklebust95121352005-10-18 14:20:12 -07001608 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Trond Myklebust95121352005-10-18 14:20:12 -07001610 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001612 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001613 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001615 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001616 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001618 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001619 if (calldata->arg.seqid == NULL)
1620 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001621 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001622 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001623 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001624 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001625 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001626 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001627 calldata->path.mnt = mntget(path->mnt);
1628 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001629
Trond Myklebust5138fde2007-07-14 15:40:01 -04001630 msg.rpc_argp = &calldata->arg,
1631 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001632 task_setup_data.callback_data = calldata;
1633 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001634 if (IS_ERR(task))
1635 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001636 status = 0;
1637 if (wait)
1638 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001639 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001640 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001641out_free_calldata:
1642 kfree(calldata);
1643out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001644 nfs4_put_open_state(state);
1645 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001646 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001649static 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 -07001650{
1651 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001652 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001653
Trond Myklebust1b45c462007-07-03 13:04:56 -04001654 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001655 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001656 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001657 state->owner->so_cred,
1658 nd->intent.open.flags);
1659 if (ret < 0)
1660 goto out_close;
1661 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001662 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001663 if (!IS_ERR(filp)) {
1664 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001665 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001666 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001667 return 0;
1668 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001669 ret = PTR_ERR(filp);
1670out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001671 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001672 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001673}
1674
1675struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1677{
Trond Myklebustad389da2007-06-05 12:30:00 -04001678 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001679 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001680 .dentry = dentry,
1681 };
Jan Blunck4ac91372008-02-14 19:34:32 -08001682 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 struct iattr attr;
1684 struct rpc_cred *cred;
1685 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001686 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001687 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 if (nd->flags & LOOKUP_CREATE) {
1690 attr.ia_mode = nd->intent.open.create_mode;
1691 attr.ia_valid = ATTR_MODE;
1692 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04001693 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 } else {
1695 attr.ia_valid = 0;
1696 BUG_ON(nd->intent.open.flags & O_CREAT);
1697 }
1698
Trond Myklebust98a8e322008-03-12 12:25:28 -04001699 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001701 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04001702 parent = dentry->d_parent;
1703 /* Protect against concurrent sillydeletes */
1704 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001705 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001707 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001708 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001709 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04001710 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1711 }
Trond Myklebust565277f2007-10-15 18:17:53 -04001712 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001713 return (struct dentry *)state;
1714 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001715 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001716 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04001717 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04001718 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001719 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001720 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001721 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
1724int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001725nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Trond Myklebustad389da2007-06-05 12:30:00 -04001727 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001728 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001729 .dentry = dentry,
1730 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 struct rpc_cred *cred;
1732 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001733 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Trond Myklebust98a8e322008-03-12 12:25:28 -04001735 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (IS_ERR(cred))
1737 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001738 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001740 if (IS_ERR(state)) {
1741 switch (PTR_ERR(state)) {
1742 case -EPERM:
1743 case -EACCES:
1744 case -EDQUOT:
1745 case -ENOSPC:
1746 case -EROFS:
1747 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1748 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001749 default:
1750 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001751 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001752 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001753 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001754 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001755 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return 1;
1757 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001758 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001759out_drop:
1760 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return 0;
1762}
1763
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001764void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1765{
1766 if (ctx->state == NULL)
1767 return;
1768 if (is_sync)
1769 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
1770 else
1771 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
1772}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1775{
Benny Halevy43652ad2009-04-01 09:21:54 -04001776 struct nfs4_server_caps_arg args = {
1777 .fhandle = fhandle,
1778 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 struct nfs4_server_caps_res res = {};
1780 struct rpc_message msg = {
1781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04001782 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 .rpc_resp = &res,
1784 };
1785 int status;
1786
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001787 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 if (status == 0) {
1789 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1790 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1791 server->caps |= NFS_CAP_ACLS;
1792 if (res.has_links != 0)
1793 server->caps |= NFS_CAP_HARDLINKS;
1794 if (res.has_symlinks != 0)
1795 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001796 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
1797 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
1798 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 server->acl_bitmask = res.acl_bitmask;
1800 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return status;
1803}
1804
Trond Myklebust55a97592006-06-09 09:34:19 -04001805int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
1807 struct nfs4_exception exception = { };
1808 int err;
1809 do {
1810 err = nfs4_handle_exception(server,
1811 _nfs4_server_capabilities(server, fhandle),
1812 &exception);
1813 } while (exception.retry);
1814 return err;
1815}
1816
1817static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1818 struct nfs_fsinfo *info)
1819{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 struct nfs4_lookup_root_arg args = {
1821 .bitmask = nfs4_fattr_bitmap,
1822 };
1823 struct nfs4_lookup_res res = {
1824 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001825 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 .fh = fhandle,
1827 };
1828 struct rpc_message msg = {
1829 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1830 .rpc_argp = &args,
1831 .rpc_resp = &res,
1832 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001833 nfs_fattr_init(info->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001834 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
1837static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1838 struct nfs_fsinfo *info)
1839{
1840 struct nfs4_exception exception = { };
1841 int err;
1842 do {
1843 err = nfs4_handle_exception(server,
1844 _nfs4_lookup_root(server, fhandle, info),
1845 &exception);
1846 } while (exception.retry);
1847 return err;
1848}
1849
David Howells54ceac42006-08-22 20:06:13 -04001850/*
1851 * get the file handle for the "/" directory on the server
1852 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04001854 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 int status;
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 if (status == 0)
1860 status = nfs4_server_capabilities(server, fhandle);
1861 if (status == 0)
1862 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08001863 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864}
1865
Manoj Naik6b97fd32006-06-09 09:34:29 -04001866/*
1867 * Get locations and (maybe) other attributes of a referral.
1868 * Note that we'll actually follow the referral later when
1869 * we detect fsid mismatch in inode revalidation
1870 */
Trond Myklebust56659e92007-07-17 21:52:39 -04001871static 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 -04001872{
1873 int status = -ENOMEM;
1874 struct page *page = NULL;
1875 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04001876
1877 page = alloc_page(GFP_KERNEL);
1878 if (page == NULL)
1879 goto out;
1880 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1881 if (locations == NULL)
1882 goto out;
1883
Trond Myklebustc228fd32007-01-13 02:28:11 -05001884 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001885 if (status != 0)
1886 goto out;
1887 /* Make sure server returned a different fsid for the referral */
1888 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07001889 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 -04001890 status = -EIO;
1891 goto out;
1892 }
1893
1894 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1895 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1896 if (!fattr->mode)
1897 fattr->mode = S_IFDIR;
1898 memset(fhandle, 0, sizeof(struct nfs_fh));
1899out:
1900 if (page)
1901 __free_page(page);
1902 if (locations)
1903 kfree(locations);
1904 return status;
1905}
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1908{
1909 struct nfs4_getattr_arg args = {
1910 .fh = fhandle,
1911 .bitmask = server->attr_bitmask,
1912 };
1913 struct nfs4_getattr_res res = {
1914 .fattr = fattr,
1915 .server = server,
1916 };
1917 struct rpc_message msg = {
1918 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1919 .rpc_argp = &args,
1920 .rpc_resp = &res,
1921 };
1922
Trond Myklebust0e574af2005-10-27 22:12:38 -04001923 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001924 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
1927static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1928{
1929 struct nfs4_exception exception = { };
1930 int err;
1931 do {
1932 err = nfs4_handle_exception(server,
1933 _nfs4_proc_getattr(server, fhandle, fattr),
1934 &exception);
1935 } while (exception.retry);
1936 return err;
1937}
1938
1939/*
1940 * The file is not closed if it is opened due to the a request to change
1941 * the size of the file. The open call will not be needed once the
1942 * VFS layer lookup-intents are implemented.
1943 *
1944 * Close is called when the inode is destroyed.
1945 * If we haven't opened the file for O_WRONLY, we
1946 * need to in the size_change case to obtain a stateid.
1947 *
1948 * Got race?
1949 * Because OPEN is always done by name in nfsv4, it is
1950 * possible that we opened a different file by the same
1951 * name. We can recognize this race condition, but we
1952 * can't do anything about it besides returning an error.
1953 *
1954 * This will be fixed with VFS changes (lookup-intent).
1955 */
1956static int
1957nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1958 struct iattr *sattr)
1959{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001960 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001961 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05001962 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 int status;
1964
Trond Myklebust0e574af2005-10-27 22:12:38 -04001965 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Trond Myklebustd5308382005-11-04 15:33:38 -05001967 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001968 if (sattr->ia_valid & ATTR_FILE) {
1969 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001970
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001971 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11001972 if (ctx) {
1973 cred = ctx->cred;
1974 state = ctx->state;
1975 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001976 }
1977
1978 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04001979 if (status == 0)
1980 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 return status;
1982}
1983
Trond Myklebust56659e92007-07-17 21:52:39 -04001984static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
1985 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04001986 struct nfs_fattr *fattr)
1987{
1988 int status;
1989 struct nfs4_lookup_arg args = {
1990 .bitmask = server->attr_bitmask,
1991 .dir_fh = dirfh,
1992 .name = name,
1993 };
1994 struct nfs4_lookup_res res = {
1995 .server = server,
1996 .fattr = fattr,
1997 .fh = fhandle,
1998 };
1999 struct rpc_message msg = {
2000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2001 .rpc_argp = &args,
2002 .rpc_resp = &res,
2003 };
2004
2005 nfs_fattr_init(fattr);
2006
2007 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002008 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002009 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002010 return status;
2011}
2012
2013static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2014 struct qstr *name, struct nfs_fh *fhandle,
2015 struct nfs_fattr *fattr)
2016{
2017 struct nfs4_exception exception = { };
2018 int err;
2019 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002020 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2021 /* FIXME: !!!! */
2022 if (err == -NFS4ERR_MOVED) {
2023 err = -EREMOTE;
2024 break;
2025 }
2026 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002027 } while (exception.retry);
2028 return err;
2029}
2030
Trond Myklebust56659e92007-07-17 21:52:39 -04002031static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2033{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002034 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002037 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002038 if (status == -NFS4ERR_MOVED)
2039 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 dprintk("NFS reply lookup: %d\n", status);
2041 return status;
2042}
2043
2044static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2045{
2046 struct nfs4_exception exception = { };
2047 int err;
2048 do {
2049 err = nfs4_handle_exception(NFS_SERVER(dir),
2050 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2051 &exception);
2052 } while (exception.retry);
2053 return err;
2054}
2055
2056static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2057{
Trond Myklebust76b32992007-08-10 17:45:11 -04002058 struct nfs_server *server = NFS_SERVER(inode);
2059 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 struct nfs4_accessargs args = {
2061 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002062 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002064 struct nfs4_accessres res = {
2065 .server = server,
2066 .fattr = &fattr,
2067 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 struct rpc_message msg = {
2069 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2070 .rpc_argp = &args,
2071 .rpc_resp = &res,
2072 .rpc_cred = entry->cred,
2073 };
2074 int mode = entry->mask;
2075 int status;
2076
2077 /*
2078 * Determine which access bits we want to ask for...
2079 */
2080 if (mode & MAY_READ)
2081 args.access |= NFS4_ACCESS_READ;
2082 if (S_ISDIR(inode->i_mode)) {
2083 if (mode & MAY_WRITE)
2084 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2085 if (mode & MAY_EXEC)
2086 args.access |= NFS4_ACCESS_LOOKUP;
2087 } else {
2088 if (mode & MAY_WRITE)
2089 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2090 if (mode & MAY_EXEC)
2091 args.access |= NFS4_ACCESS_EXECUTE;
2092 }
Trond Myklebust76b32992007-08-10 17:45:11 -04002093 nfs_fattr_init(&fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002094 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (!status) {
2096 entry->mask = 0;
2097 if (res.access & NFS4_ACCESS_READ)
2098 entry->mask |= MAY_READ;
2099 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2100 entry->mask |= MAY_WRITE;
2101 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2102 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04002103 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
2105 return status;
2106}
2107
2108static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2109{
2110 struct nfs4_exception exception = { };
2111 int err;
2112 do {
2113 err = nfs4_handle_exception(NFS_SERVER(inode),
2114 _nfs4_proc_access(inode, entry),
2115 &exception);
2116 } while (exception.retry);
2117 return err;
2118}
2119
2120/*
2121 * TODO: For the time being, we don't try to get any attributes
2122 * along with any of the zero-copy operations READ, READDIR,
2123 * READLINK, WRITE.
2124 *
2125 * In the case of the first three, we want to put the GETATTR
2126 * after the read-type operation -- this is because it is hard
2127 * to predict the length of a GETATTR response in v4, and thus
2128 * align the READ data correctly. This means that the GETATTR
2129 * may end up partially falling into the page cache, and we should
2130 * shift it into the 'tail' of the xdr_buf before processing.
2131 * To do this efficiently, we need to know the total length
2132 * of data received, which doesn't seem to be available outside
2133 * of the RPC layer.
2134 *
2135 * In the case of WRITE, we also want to put the GETATTR after
2136 * the operation -- in this case because we want to make sure
2137 * we get the post-operation mtime and size. This means that
2138 * we can't use xdr_encode_pages() as written: we need a variant
2139 * of it which would leave room in the 'tail' iovec.
2140 *
2141 * Both of these changes to the XDR layer would in fact be quite
2142 * minor, but I decided to leave them for a subsequent patch.
2143 */
2144static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2145 unsigned int pgbase, unsigned int pglen)
2146{
2147 struct nfs4_readlink args = {
2148 .fh = NFS_FH(inode),
2149 .pgbase = pgbase,
2150 .pglen = pglen,
2151 .pages = &page,
2152 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002153 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 struct rpc_message msg = {
2155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2156 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002157 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 };
2159
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002160 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
2162
2163static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2164 unsigned int pgbase, unsigned int pglen)
2165{
2166 struct nfs4_exception exception = { };
2167 int err;
2168 do {
2169 err = nfs4_handle_exception(NFS_SERVER(inode),
2170 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2171 &exception);
2172 } while (exception.retry);
2173 return err;
2174}
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176/*
2177 * Got race?
2178 * We will need to arrange for the VFS layer to provide an atomic open.
2179 * Until then, this create/open method is prone to inefficiency and race
2180 * conditions due to the lookup, create, and open VFS calls from sys_open()
2181 * placed on the wire.
2182 *
2183 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2184 * The file will be opened again in the subsequent VFS open call
2185 * (nfs4_proc_file_open).
2186 *
2187 * The open for read will just hang around to be used by any process that
2188 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2189 */
2190
2191static int
2192nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002193 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
Trond Myklebustad389da2007-06-05 12:30:00 -04002195 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002196 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002197 .dentry = dentry,
2198 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 struct nfs4_state *state;
2200 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002201 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 int status = 0;
2203
Trond Myklebust98a8e322008-03-12 12:25:28 -04002204 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 if (IS_ERR(cred)) {
2206 status = PTR_ERR(cred);
2207 goto out;
2208 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002209 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002210 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if (IS_ERR(state)) {
2212 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002213 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002215 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002216 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (flags & O_EXCL) {
2218 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002219 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002220 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002221 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002222 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002223 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002224 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002225 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002226 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002227 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002228out_putcred:
2229 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230out:
2231 return status;
2232}
2233
2234static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2235{
Trond Myklebust16e42952005-10-27 22:12:44 -04002236 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002237 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002239 .name.len = name->len,
2240 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002241 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002243 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002244 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002245 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002247 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2248 .rpc_argp = &args,
2249 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 };
2251 int status;
2252
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002253 nfs_fattr_init(&res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002254 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002255 if (status == 0) {
2256 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002257 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 return status;
2260}
2261
2262static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2263{
2264 struct nfs4_exception exception = { };
2265 int err;
2266 do {
2267 err = nfs4_handle_exception(NFS_SERVER(dir),
2268 _nfs4_proc_remove(dir, name),
2269 &exception);
2270 } while (exception.retry);
2271 return err;
2272}
2273
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002274static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002276 struct nfs_server *server = NFS_SERVER(dir);
2277 struct nfs_removeargs *args = msg->rpc_argp;
2278 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Trond Myklebusta65318b2009-03-11 14:10:28 -04002280 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002281 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283}
2284
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002285static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002287 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2288
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002289 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002290 return 0;
2291 update_changeattr(dir, &res->cinfo);
2292 nfs_post_op_update_inode(dir, &res->dir_attr);
2293 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
2296static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2297 struct inode *new_dir, struct qstr *new_name)
2298{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002299 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 struct nfs4_rename_arg arg = {
2301 .old_dir = NFS_FH(old_dir),
2302 .new_dir = NFS_FH(new_dir),
2303 .old_name = old_name,
2304 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002305 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002307 struct nfs_fattr old_fattr, new_fattr;
2308 struct nfs4_rename_res res = {
2309 .server = server,
2310 .old_fattr = &old_fattr,
2311 .new_fattr = &new_fattr,
2312 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 struct rpc_message msg = {
2314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2315 .rpc_argp = &arg,
2316 .rpc_resp = &res,
2317 };
2318 int status;
2319
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002320 nfs_fattr_init(res.old_fattr);
2321 nfs_fattr_init(res.new_fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002322 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
2324 if (!status) {
2325 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002326 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002328 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
2330 return status;
2331}
2332
2333static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2334 struct inode *new_dir, struct qstr *new_name)
2335{
2336 struct nfs4_exception exception = { };
2337 int err;
2338 do {
2339 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2340 _nfs4_proc_rename(old_dir, old_name,
2341 new_dir, new_name),
2342 &exception);
2343 } while (exception.retry);
2344 return err;
2345}
2346
2347static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2348{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002349 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 struct nfs4_link_arg arg = {
2351 .fh = NFS_FH(inode),
2352 .dir_fh = NFS_FH(dir),
2353 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002354 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002356 struct nfs_fattr fattr, dir_attr;
2357 struct nfs4_link_res res = {
2358 .server = server,
2359 .fattr = &fattr,
2360 .dir_attr = &dir_attr,
2361 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 struct rpc_message msg = {
2363 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2364 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002365 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 };
2367 int status;
2368
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002369 nfs_fattr_init(res.fattr);
2370 nfs_fattr_init(res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002371 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002372 if (!status) {
2373 update_changeattr(dir, &res.cinfo);
2374 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002375 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
2378 return status;
2379}
2380
2381static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2382{
2383 struct nfs4_exception exception = { };
2384 int err;
2385 do {
2386 err = nfs4_handle_exception(NFS_SERVER(inode),
2387 _nfs4_proc_link(inode, dir, name),
2388 &exception);
2389 } while (exception.retry);
2390 return err;
2391}
2392
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002393struct nfs4_createdata {
2394 struct rpc_message msg;
2395 struct nfs4_create_arg arg;
2396 struct nfs4_create_res res;
2397 struct nfs_fh fh;
2398 struct nfs_fattr fattr;
2399 struct nfs_fattr dir_fattr;
2400};
2401
2402static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2403 struct qstr *name, struct iattr *sattr, u32 ftype)
2404{
2405 struct nfs4_createdata *data;
2406
2407 data = kzalloc(sizeof(*data), GFP_KERNEL);
2408 if (data != NULL) {
2409 struct nfs_server *server = NFS_SERVER(dir);
2410
2411 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2412 data->msg.rpc_argp = &data->arg;
2413 data->msg.rpc_resp = &data->res;
2414 data->arg.dir_fh = NFS_FH(dir);
2415 data->arg.server = server;
2416 data->arg.name = name;
2417 data->arg.attrs = sattr;
2418 data->arg.ftype = ftype;
2419 data->arg.bitmask = server->attr_bitmask;
2420 data->res.server = server;
2421 data->res.fh = &data->fh;
2422 data->res.fattr = &data->fattr;
2423 data->res.dir_fattr = &data->dir_fattr;
2424 nfs_fattr_init(data->res.fattr);
2425 nfs_fattr_init(data->res.dir_fattr);
2426 }
2427 return data;
2428}
2429
2430static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2431{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002432 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2433 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002434 if (status == 0) {
2435 update_changeattr(dir, &data->res.dir_cinfo);
2436 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2437 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2438 }
2439 return status;
2440}
2441
2442static void nfs4_free_createdata(struct nfs4_createdata *data)
2443{
2444 kfree(data);
2445}
2446
Chuck Lever4f390c12006-08-22 20:06:22 -04002447static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002448 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002450 struct nfs4_createdata *data;
2451 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Chuck Lever94a6d752006-08-22 20:06:23 -04002453 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002454 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002455
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002456 status = -ENOMEM;
2457 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2458 if (data == NULL)
2459 goto out;
2460
2461 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2462 data->arg.u.symlink.pages = &page;
2463 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002465 status = nfs4_do_create(dir, dentry, data);
2466
2467 nfs4_free_createdata(data);
2468out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 return status;
2470}
2471
Chuck Lever4f390c12006-08-22 20:06:22 -04002472static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002473 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474{
2475 struct nfs4_exception exception = { };
2476 int err;
2477 do {
2478 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002479 _nfs4_proc_symlink(dir, dentry, page,
2480 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 &exception);
2482 } while (exception.retry);
2483 return err;
2484}
2485
2486static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2487 struct iattr *sattr)
2488{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002489 struct nfs4_createdata *data;
2490 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002492 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2493 if (data == NULL)
2494 goto out;
2495
2496 status = nfs4_do_create(dir, dentry, data);
2497
2498 nfs4_free_createdata(data);
2499out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 return status;
2501}
2502
2503static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2504 struct iattr *sattr)
2505{
2506 struct nfs4_exception exception = { };
2507 int err;
2508 do {
2509 err = nfs4_handle_exception(NFS_SERVER(dir),
2510 _nfs4_proc_mkdir(dir, dentry, sattr),
2511 &exception);
2512 } while (exception.retry);
2513 return err;
2514}
2515
2516static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2517 u64 cookie, struct page *page, unsigned int count, int plus)
2518{
2519 struct inode *dir = dentry->d_inode;
2520 struct nfs4_readdir_arg args = {
2521 .fh = NFS_FH(dir),
2522 .pages = &page,
2523 .pgbase = 0,
2524 .count = count,
Trond Myklebusta65318b2009-03-11 14:10:28 -04002525 .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 };
2527 struct nfs4_readdir_res res;
2528 struct rpc_message msg = {
2529 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2530 .rpc_argp = &args,
2531 .rpc_resp = &res,
2532 .rpc_cred = cred,
2533 };
2534 int status;
2535
Harvey Harrison3110ff82008-05-02 13:42:44 -07002536 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002537 dentry->d_parent->d_name.name,
2538 dentry->d_name.name,
2539 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2541 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002542 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 if (status == 0)
2544 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002545
2546 nfs_invalidate_atime(dir);
2547
Harvey Harrison3110ff82008-05-02 13:42:44 -07002548 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 return status;
2550}
2551
2552static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2553 u64 cookie, struct page *page, unsigned int count, int plus)
2554{
2555 struct nfs4_exception exception = { };
2556 int err;
2557 do {
2558 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2559 _nfs4_proc_readdir(dentry, cred, cookie,
2560 page, count, plus),
2561 &exception);
2562 } while (exception.retry);
2563 return err;
2564}
2565
2566static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2567 struct iattr *sattr, dev_t rdev)
2568{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002569 struct nfs4_createdata *data;
2570 int mode = sattr->ia_mode;
2571 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
2573 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2574 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002575
2576 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2577 if (data == NULL)
2578 goto out;
2579
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002581 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002583 data->arg.ftype = NF4BLK;
2584 data->arg.u.device.specdata1 = MAJOR(rdev);
2585 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 }
2587 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002588 data->arg.ftype = NF4CHR;
2589 data->arg.u.device.specdata1 = MAJOR(rdev);
2590 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002593 status = nfs4_do_create(dir, dentry, data);
2594
2595 nfs4_free_createdata(data);
2596out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 return status;
2598}
2599
2600static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2601 struct iattr *sattr, dev_t rdev)
2602{
2603 struct nfs4_exception exception = { };
2604 int err;
2605 do {
2606 err = nfs4_handle_exception(NFS_SERVER(dir),
2607 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2608 &exception);
2609 } while (exception.retry);
2610 return err;
2611}
2612
2613static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2614 struct nfs_fsstat *fsstat)
2615{
2616 struct nfs4_statfs_arg args = {
2617 .fh = fhandle,
2618 .bitmask = server->attr_bitmask,
2619 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002620 struct nfs4_statfs_res res = {
2621 .fsstat = fsstat,
2622 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 struct rpc_message msg = {
2624 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2625 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002626 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 };
2628
Trond Myklebust0e574af2005-10-27 22:12:38 -04002629 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002630 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631}
2632
2633static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2634{
2635 struct nfs4_exception exception = { };
2636 int err;
2637 do {
2638 err = nfs4_handle_exception(server,
2639 _nfs4_proc_statfs(server, fhandle, fsstat),
2640 &exception);
2641 } while (exception.retry);
2642 return err;
2643}
2644
2645static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2646 struct nfs_fsinfo *fsinfo)
2647{
2648 struct nfs4_fsinfo_arg args = {
2649 .fh = fhandle,
2650 .bitmask = server->attr_bitmask,
2651 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002652 struct nfs4_fsinfo_res res = {
2653 .fsinfo = fsinfo,
2654 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 struct rpc_message msg = {
2656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2657 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002658 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 };
2660
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002661 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662}
2663
2664static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2665{
2666 struct nfs4_exception exception = { };
2667 int err;
2668
2669 do {
2670 err = nfs4_handle_exception(server,
2671 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2672 &exception);
2673 } while (exception.retry);
2674 return err;
2675}
2676
2677static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2678{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002679 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2681}
2682
2683static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2684 struct nfs_pathconf *pathconf)
2685{
2686 struct nfs4_pathconf_arg args = {
2687 .fh = fhandle,
2688 .bitmask = server->attr_bitmask,
2689 };
Benny Halevyd45b2982009-04-01 09:21:58 -04002690 struct nfs4_pathconf_res res = {
2691 .pathconf = pathconf,
2692 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 struct rpc_message msg = {
2694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2695 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04002696 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 };
2698
2699 /* None of the pathconf attributes are mandatory to implement */
2700 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2701 memset(pathconf, 0, sizeof(*pathconf));
2702 return 0;
2703 }
2704
Trond Myklebust0e574af2005-10-27 22:12:38 -04002705 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002706 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707}
2708
2709static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2710 struct nfs_pathconf *pathconf)
2711{
2712 struct nfs4_exception exception = { };
2713 int err;
2714
2715 do {
2716 err = nfs4_handle_exception(server,
2717 _nfs4_proc_pathconf(server, fhandle, pathconf),
2718 &exception);
2719 } while (exception.retry);
2720 return err;
2721}
2722
Trond Myklebustec06c092006-03-20 13:44:27 -05002723static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724{
Trond Myklebustec06c092006-03-20 13:44:27 -05002725 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002727 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 rpc_restart_call(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05002729 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 }
Trond Myklebust8850df92007-09-28 17:20:07 -04002731
2732 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002734 renew_lease(server, data->timestamp);
2735 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736}
2737
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002738static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002741 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742}
2743
Trond Myklebust788e7a82006-03-20 13:44:27 -05002744static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 struct inode *inode = data->inode;
2747
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002748 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002750 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002752 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04002754 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002755 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05002756 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757}
2758
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002759static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002761 struct nfs_server *server = NFS_SERVER(data->inode);
2762
Trond Myklebusta65318b2009-03-11 14:10:28 -04002763 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002764 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 data->timestamp = jiffies;
2766
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002767 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768}
2769
Trond Myklebust788e7a82006-03-20 13:44:27 -05002770static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 struct inode *inode = data->inode;
2773
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002774 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002776 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04002778 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002779 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780}
2781
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002782static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783{
Trond Myklebust788e7a82006-03-20 13:44:27 -05002784 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Trond Myklebusta65318b2009-03-11 14:10:28 -04002786 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002787 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002788 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789}
2790
2791/*
2792 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2793 * standalone procedure for queueing an asynchronous RENEW.
2794 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002795static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796{
David Howellsadfa6f92006-08-22 20:06:08 -04002797 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002798 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04002801 /* Unless we're shutting down, schedule state recovery! */
2802 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
2803 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 return;
2805 }
2806 spin_lock(&clp->cl_lock);
2807 if (time_before(clp->cl_last_renewal,timestamp))
2808 clp->cl_last_renewal = timestamp;
2809 spin_unlock(&clp->cl_lock);
2810}
2811
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002812static const struct rpc_call_ops nfs4_renew_ops = {
2813 .rpc_call_done = nfs4_renew_done,
2814};
2815
David Howellsadfa6f92006-08-22 20:06:08 -04002816int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{
2818 struct rpc_message msg = {
2819 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2820 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002821 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 };
2823
2824 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002825 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826}
2827
David Howellsadfa6f92006-08-22 20:06:08 -04002828int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
2830 struct rpc_message msg = {
2831 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2832 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002833 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 };
2835 unsigned long now = jiffies;
2836 int status;
2837
2838 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2839 if (status < 0)
2840 return status;
2841 spin_lock(&clp->cl_lock);
2842 if (time_before(clp->cl_last_renewal,now))
2843 clp->cl_last_renewal = now;
2844 spin_unlock(&clp->cl_lock);
2845 return 0;
2846}
2847
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002848static inline int nfs4_server_supports_acls(struct nfs_server *server)
2849{
2850 return (server->caps & NFS_CAP_ACLS)
2851 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2852 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2853}
2854
2855/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2856 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2857 * the stack.
2858 */
2859#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2860
2861static void buf_to_pages(const void *buf, size_t buflen,
2862 struct page **pages, unsigned int *pgbase)
2863{
2864 const void *p = buf;
2865
2866 *pgbase = offset_in_page(buf);
2867 p -= *pgbase;
2868 while (p < buf + buflen) {
2869 *(pages++) = virt_to_page(p);
2870 p += PAGE_CACHE_SIZE;
2871 }
2872}
2873
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002874struct nfs4_cached_acl {
2875 int cached;
2876 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002877 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002878};
2879
2880static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002881{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002882 struct nfs_inode *nfsi = NFS_I(inode);
2883
2884 spin_lock(&inode->i_lock);
2885 kfree(nfsi->nfs4_acl);
2886 nfsi->nfs4_acl = acl;
2887 spin_unlock(&inode->i_lock);
2888}
2889
2890static void nfs4_zap_acl_attr(struct inode *inode)
2891{
2892 nfs4_set_cached_acl(inode, NULL);
2893}
2894
2895static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2896{
2897 struct nfs_inode *nfsi = NFS_I(inode);
2898 struct nfs4_cached_acl *acl;
2899 int ret = -ENOENT;
2900
2901 spin_lock(&inode->i_lock);
2902 acl = nfsi->nfs4_acl;
2903 if (acl == NULL)
2904 goto out;
2905 if (buf == NULL) /* user is just asking for length */
2906 goto out_len;
2907 if (acl->cached == 0)
2908 goto out;
2909 ret = -ERANGE; /* see getxattr(2) man page */
2910 if (acl->len > buflen)
2911 goto out;
2912 memcpy(buf, acl->data, acl->len);
2913out_len:
2914 ret = acl->len;
2915out:
2916 spin_unlock(&inode->i_lock);
2917 return ret;
2918}
2919
2920static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2921{
2922 struct nfs4_cached_acl *acl;
2923
2924 if (buf && acl_len <= PAGE_SIZE) {
2925 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2926 if (acl == NULL)
2927 goto out;
2928 acl->cached = 1;
2929 memcpy(acl->data, buf, acl_len);
2930 } else {
2931 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2932 if (acl == NULL)
2933 goto out;
2934 acl->cached = 0;
2935 }
2936 acl->len = acl_len;
2937out:
2938 nfs4_set_cached_acl(inode, acl);
2939}
2940
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002941static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002942{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002943 struct page *pages[NFS4ACL_MAXPAGES];
2944 struct nfs_getaclargs args = {
2945 .fh = NFS_FH(inode),
2946 .acl_pages = pages,
2947 .acl_len = buflen,
2948 };
Benny Halevy663c79b2009-04-01 09:21:59 -04002949 struct nfs_getaclres res = {
2950 .acl_len = buflen,
2951 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002952 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002953 struct rpc_message msg = {
2954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2955 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04002956 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002957 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002958 struct page *localpage = NULL;
2959 int ret;
2960
2961 if (buflen < PAGE_SIZE) {
2962 /* As long as we're doing a round trip to the server anyway,
2963 * let's be prepared for a page of acl data. */
2964 localpage = alloc_page(GFP_KERNEL);
2965 resp_buf = page_address(localpage);
2966 if (localpage == NULL)
2967 return -ENOMEM;
2968 args.acl_pages[0] = localpage;
2969 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04002970 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002971 } else {
2972 resp_buf = buf;
2973 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2974 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002975 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002976 if (ret)
2977 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04002978 if (res.acl_len > args.acl_len)
2979 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002980 else
Benny Halevy663c79b2009-04-01 09:21:59 -04002981 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002982 if (buf) {
2983 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04002984 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002985 goto out_free;
2986 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04002987 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002988 }
Benny Halevy663c79b2009-04-01 09:21:59 -04002989 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002990out_free:
2991 if (localpage)
2992 __free_page(localpage);
2993 return ret;
2994}
2995
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002996static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2997{
2998 struct nfs4_exception exception = { };
2999 ssize_t ret;
3000 do {
3001 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3002 if (ret >= 0)
3003 break;
3004 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3005 } while (exception.retry);
3006 return ret;
3007}
3008
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003009static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3010{
3011 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003012 int ret;
3013
3014 if (!nfs4_server_supports_acls(server))
3015 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003016 ret = nfs_revalidate_inode(server, inode);
3017 if (ret < 0)
3018 return ret;
Trond Myklebustf41f7412008-06-11 17:39:04 -04003019 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3020 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003021 ret = nfs4_read_cached_acl(inode, buf, buflen);
3022 if (ret != -ENOENT)
3023 return ret;
3024 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003025}
3026
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003027static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003028{
3029 struct nfs_server *server = NFS_SERVER(inode);
3030 struct page *pages[NFS4ACL_MAXPAGES];
3031 struct nfs_setaclargs arg = {
3032 .fh = NFS_FH(inode),
3033 .acl_pages = pages,
3034 .acl_len = buflen,
3035 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003036 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003037 struct rpc_message msg = {
3038 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3039 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003040 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003041 };
3042 int ret;
3043
3044 if (!nfs4_server_supports_acls(server))
3045 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003046 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003047 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003048 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003049 nfs_access_zap_cache(inode);
3050 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003051 return ret;
3052}
3053
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003054static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3055{
3056 struct nfs4_exception exception = { };
3057 int err;
3058 do {
3059 err = nfs4_handle_exception(NFS_SERVER(inode),
3060 __nfs4_proc_set_acl(inode, buf, buflen),
3061 &exception);
3062 } while (exception.retry);
3063 return err;
3064}
3065
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066static int
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003067nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
David Howells7539bba2006-08-22 20:06:09 -04003069 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
3071 if (!clp || task->tk_status >= 0)
3072 return 0;
3073 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003074 case -NFS4ERR_ADMIN_REVOKED:
3075 case -NFS4ERR_BAD_STATEID:
3076 case -NFS4ERR_OPENMODE:
3077 if (state == NULL)
3078 break;
3079 nfs4_state_mark_reclaim_nograce(clp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 case -NFS4ERR_STALE_CLIENTID:
3081 case -NFS4ERR_STALE_STATEID:
3082 case -NFS4ERR_EXPIRED:
Trond Myklebust5d008372008-02-22 16:34:17 -05003083 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 nfs4_schedule_state_recovery(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05003085 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
Trond Myklebustfda13932008-02-22 16:34:12 -05003086 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 task->tk_status = 0;
3088 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 case -NFS4ERR_DELAY:
Trond Myklebust2e96d282008-06-11 16:42:05 -04003090 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003091 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3093 task->tk_status = 0;
3094 return -EAGAIN;
3095 case -NFS4ERR_OLD_STATEID:
3096 task->tk_status = 0;
3097 return -EAGAIN;
3098 }
3099 task->tk_status = nfs4_map_errors(task->tk_status);
3100 return 0;
3101}
3102
David Howellsadfa6f92006-08-22 20:06:08 -04003103int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
3105 nfs4_verifier sc_verifier;
3106 struct nfs4_setclientid setclientid = {
3107 .sc_verifier = &sc_verifier,
3108 .sc_prog = program,
3109 };
3110 struct rpc_message msg = {
3111 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3112 .rpc_argp = &setclientid,
3113 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003114 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 };
Al Virobc4785c2006-10-19 23:28:51 -07003116 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 int loop = 0;
3118 int status;
3119
Al Virobc4785c2006-10-19 23:28:51 -07003120 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3122 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3123
3124 for(;;) {
3125 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003126 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003127 clp->cl_ipaddr,
3128 rpc_peeraddr2str(clp->cl_rpcclient,
3129 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003130 rpc_peeraddr2str(clp->cl_rpcclient,
3131 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003132 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 clp->cl_id_uniquifier);
3134 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003135 sizeof(setclientid.sc_netid),
3136 rpc_peeraddr2str(clp->cl_rpcclient,
3137 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003139 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 clp->cl_ipaddr, port >> 8, port & 255);
3141
3142 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3143 if (status != -NFS4ERR_CLID_INUSE)
3144 break;
3145 if (signalled())
3146 break;
3147 if (loop++ & 1)
3148 ssleep(clp->cl_lease_time + 1);
3149 else
3150 if (++clp->cl_id_uniquifier == 0)
3151 break;
3152 }
3153 return status;
3154}
3155
David Howellsadfa6f92006-08-22 20:06:08 -04003156static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157{
3158 struct nfs_fsinfo fsinfo;
3159 struct rpc_message msg = {
3160 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3161 .rpc_argp = clp,
3162 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003163 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 };
3165 unsigned long now;
3166 int status;
3167
3168 now = jiffies;
3169 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3170 if (status == 0) {
3171 spin_lock(&clp->cl_lock);
3172 clp->cl_lease_time = fsinfo.lease_time * HZ;
3173 clp->cl_last_renewal = now;
3174 spin_unlock(&clp->cl_lock);
3175 }
3176 return status;
3177}
3178
David Howellsadfa6f92006-08-22 20:06:08 -04003179int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003180{
Benny Halevy77e03672008-06-24 20:25:57 +03003181 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003182 int err;
3183 do {
3184 err = _nfs4_proc_setclientid_confirm(clp, cred);
3185 switch (err) {
3186 case 0:
3187 return err;
3188 case -NFS4ERR_RESOURCE:
3189 /* The IBM lawyers misread another document! */
3190 case -NFS4ERR_DELAY:
3191 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3192 }
3193 } while (err == 0);
3194 return err;
3195}
3196
Trond Myklebustfe650402006-01-03 09:55:18 +01003197struct nfs4_delegreturndata {
3198 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003199 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003200 struct nfs_fh fh;
3201 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003202 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003203 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003204 int rpc_status;
3205};
3206
Trond Myklebustfe650402006-01-03 09:55:18 +01003207static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3208{
3209 struct nfs4_delegreturndata *data = calldata;
3210 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003211 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01003212 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01003213}
3214
3215static void nfs4_delegreturn_release(void *calldata)
3216{
Trond Myklebustfe650402006-01-03 09:55:18 +01003217 kfree(calldata);
3218}
3219
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003220static const struct rpc_call_ops nfs4_delegreturn_ops = {
Trond Myklebustfe650402006-01-03 09:55:18 +01003221 .rpc_call_done = nfs4_delegreturn_done,
3222 .rpc_release = nfs4_delegreturn_release,
3223};
3224
Trond Myklebuste6f81072008-01-24 18:14:34 -05003225static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003226{
3227 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003228 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003229 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003230 struct rpc_message msg = {
3231 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3232 .rpc_cred = cred,
3233 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003234 struct rpc_task_setup task_setup_data = {
3235 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003236 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003237 .callback_ops = &nfs4_delegreturn_ops,
3238 .flags = RPC_TASK_ASYNC,
3239 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003240 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003241
3242 data = kmalloc(sizeof(*data), GFP_KERNEL);
3243 if (data == NULL)
3244 return -ENOMEM;
3245 data->args.fhandle = &data->fh;
3246 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003247 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003248 nfs_copy_fh(&data->fh, NFS_FH(inode));
3249 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003250 data->res.fattr = &data->fattr;
3251 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003252 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003253 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003254 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003255 data->rpc_status = 0;
3256
Trond Myklebustc970aa82007-07-14 15:39:59 -04003257 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003258 msg.rpc_argp = &data->args,
3259 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003260 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003261 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003262 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003263 if (!issync)
3264 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003265 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003266 if (status != 0)
3267 goto out;
3268 status = data->rpc_status;
3269 if (status != 0)
3270 goto out;
3271 nfs_refresh_inode(inode, &data->fattr);
3272out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003273 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003274 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275}
3276
Trond Myklebuste6f81072008-01-24 18:14:34 -05003277int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278{
3279 struct nfs_server *server = NFS_SERVER(inode);
3280 struct nfs4_exception exception = { };
3281 int err;
3282 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003283 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 switch (err) {
3285 case -NFS4ERR_STALE_STATEID:
3286 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 case 0:
3288 return 0;
3289 }
3290 err = nfs4_handle_exception(server, err, &exception);
3291 } while (exception.retry);
3292 return err;
3293}
3294
3295#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3296#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3297
3298/*
3299 * sleep, with exponential backoff, and retry the LOCK operation.
3300 */
3301static unsigned long
3302nfs4_set_lock_task_retry(unsigned long timeout)
3303{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003304 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 timeout <<= 1;
3306 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3307 return NFS4_LOCK_MAXTIMEOUT;
3308 return timeout;
3309}
3310
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3312{
3313 struct inode *inode = state->inode;
3314 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003315 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003316 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003318 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003320 struct nfs_lockt_res res = {
3321 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 };
3323 struct rpc_message msg = {
3324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3325 .rpc_argp = &arg,
3326 .rpc_resp = &res,
3327 .rpc_cred = state->owner->so_cred,
3328 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 struct nfs4_lock_state *lsp;
3330 int status;
3331
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003332 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003333 status = nfs4_set_lock_state(state, request);
3334 if (status != 0)
3335 goto out;
3336 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003337 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003338 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003339 switch (status) {
3340 case 0:
3341 request->fl_type = F_UNLCK;
3342 break;
3343 case -NFS4ERR_DENIED:
3344 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003346 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003347out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 return status;
3349}
3350
3351static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3352{
3353 struct nfs4_exception exception = { };
3354 int err;
3355
3356 do {
3357 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3358 _nfs4_proc_getlk(state, cmd, request),
3359 &exception);
3360 } while (exception.retry);
3361 return err;
3362}
3363
3364static int do_vfs_lock(struct file *file, struct file_lock *fl)
3365{
3366 int res = 0;
3367 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3368 case FL_POSIX:
3369 res = posix_lock_file_wait(file, fl);
3370 break;
3371 case FL_FLOCK:
3372 res = flock_lock_file_wait(file, fl);
3373 break;
3374 default:
3375 BUG();
3376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 return res;
3378}
3379
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003380struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003381 struct nfs_locku_args arg;
3382 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003383 struct nfs4_lock_state *lsp;
3384 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003385 struct file_lock fl;
3386 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003387 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003388};
3389
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003390static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3391 struct nfs_open_context *ctx,
3392 struct nfs4_lock_state *lsp,
3393 struct nfs_seqid *seqid)
3394{
3395 struct nfs4_unlockdata *p;
3396 struct inode *inode = lsp->ls_state->inode;
3397
3398 p = kmalloc(sizeof(*p), GFP_KERNEL);
3399 if (p == NULL)
3400 return NULL;
3401 p->arg.fh = NFS_FH(inode);
3402 p->arg.fl = &p->fl;
3403 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003404 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003405 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003406 p->arg.stateid = &lsp->ls_stateid;
3407 p->lsp = lsp;
3408 atomic_inc(&lsp->ls_count);
3409 /* Ensure we don't close file until we're done freeing locks! */
3410 p->ctx = get_nfs_open_context(ctx);
3411 memcpy(&p->fl, fl, sizeof(p->fl));
3412 p->server = NFS_SERVER(inode);
3413 return p;
3414}
3415
Trond Myklebust06f814a2006-01-03 09:55:07 +01003416static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003417{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003418 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003419 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003420 nfs4_put_lock_state(calldata->lsp);
3421 put_nfs_open_context(calldata->ctx);
3422 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003423}
3424
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003425static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003426{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003427 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003428
Trond Myklebust06f814a2006-01-03 09:55:07 +01003429 if (RPC_ASSASSINATED(task))
3430 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003431 switch (task->tk_status) {
3432 case 0:
3433 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003434 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003435 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003436 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003437 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003438 case -NFS4ERR_BAD_STATEID:
3439 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003440 case -NFS4ERR_STALE_STATEID:
3441 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003442 break;
3443 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003444 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003445 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003446 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003447}
3448
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003449static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003450{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003451 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003453 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003454 return;
3455 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003456 /* Note: exit _without_ running nfs4_locku_done */
3457 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003458 return;
3459 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003460 calldata->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003461 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462}
3463
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003464static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003465 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003466 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003467 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003468};
3469
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003470static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3471 struct nfs_open_context *ctx,
3472 struct nfs4_lock_state *lsp,
3473 struct nfs_seqid *seqid)
3474{
3475 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003476 struct rpc_message msg = {
3477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3478 .rpc_cred = ctx->cred,
3479 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003480 struct rpc_task_setup task_setup_data = {
3481 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003482 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003483 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003484 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003485 .flags = RPC_TASK_ASYNC,
3486 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003487
Frank Filz137d6ac2007-07-09 15:32:29 -07003488 /* Ensure this is an unlock - when canceling a lock, the
3489 * canceled lock is passed in, and it won't be an unlock.
3490 */
3491 fl->fl_type = F_UNLCK;
3492
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003493 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3494 if (data == NULL) {
3495 nfs_free_seqid(seqid);
3496 return ERR_PTR(-ENOMEM);
3497 }
3498
Trond Myklebust5138fde2007-07-14 15:40:01 -04003499 msg.rpc_argp = &data->arg,
3500 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003501 task_setup_data.callback_data = data;
3502 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003503}
3504
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3506{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003507 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003508 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003509 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003510 struct rpc_task *task;
3511 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003512 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
Trond Myklebust9b073572006-06-29 16:38:34 -04003514 status = nfs4_set_lock_state(state, request);
3515 /* Unlock _before_ we do the RPC call */
3516 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003517 down_read(&nfsi->rwsem);
3518 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3519 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003520 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003521 }
3522 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003523 if (status != 0)
3524 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003525 /* Is this a delegated lock? */
3526 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003527 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003528 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003529 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003530 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003531 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003532 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003533 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003534 status = PTR_ERR(task);
3535 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003536 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003537 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003538 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003539out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003540 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003541 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542}
3543
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003544struct nfs4_lockdata {
3545 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003546 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003547 struct nfs4_lock_state *lsp;
3548 struct nfs_open_context *ctx;
3549 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003550 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003551 int rpc_status;
3552 int cancelled;
3553};
3554
3555static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3556 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3557{
3558 struct nfs4_lockdata *p;
3559 struct inode *inode = lsp->ls_state->inode;
3560 struct nfs_server *server = NFS_SERVER(inode);
3561
3562 p = kzalloc(sizeof(*p), GFP_KERNEL);
3563 if (p == NULL)
3564 return NULL;
3565
3566 p->arg.fh = NFS_FH(inode);
3567 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003568 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3569 if (p->arg.open_seqid == NULL)
3570 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003571 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3572 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003573 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003574 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003575 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003576 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003577 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003578 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003579 p->lsp = lsp;
3580 atomic_inc(&lsp->ls_count);
3581 p->ctx = get_nfs_open_context(ctx);
3582 memcpy(&p->fl, fl, sizeof(p->fl));
3583 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003584out_free_seqid:
3585 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003586out_free:
3587 kfree(p);
3588 return NULL;
3589}
3590
3591static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3592{
3593 struct nfs4_lockdata *data = calldata;
3594 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
Harvey Harrison3110ff82008-05-02 13:42:44 -07003596 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003597 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3598 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003599 /* Do we need to do an open_to_lock_owner? */
3600 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05003601 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3602 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003603 data->arg.open_stateid = &state->stateid;
3604 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003605 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003606 } else
3607 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003608 data->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003609 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003610 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003611}
3612
3613static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3614{
3615 struct nfs4_lockdata *data = calldata;
3616
Harvey Harrison3110ff82008-05-02 13:42:44 -07003617 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003618
3619 data->rpc_status = task->tk_status;
3620 if (RPC_ASSASSINATED(task))
3621 goto out;
3622 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003623 if (data->rpc_status == 0)
3624 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3625 else
3626 goto out;
3627 }
3628 if (data->rpc_status == 0) {
3629 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3630 sizeof(data->lsp->ls_stateid.data));
3631 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003632 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003633 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003634out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07003635 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003636}
3637
3638static void nfs4_lock_release(void *calldata)
3639{
3640 struct nfs4_lockdata *data = calldata;
3641
Harvey Harrison3110ff82008-05-02 13:42:44 -07003642 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003643 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003644 if (data->cancelled != 0) {
3645 struct rpc_task *task;
3646 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3647 data->arg.lock_seqid);
3648 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003649 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003650 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003651 } else
3652 nfs_free_seqid(data->arg.lock_seqid);
3653 nfs4_put_lock_state(data->lsp);
3654 put_nfs_open_context(data->ctx);
3655 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003656 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003657}
3658
3659static const struct rpc_call_ops nfs4_lock_ops = {
3660 .rpc_call_prepare = nfs4_lock_prepare,
3661 .rpc_call_done = nfs4_lock_done,
3662 .rpc_release = nfs4_lock_release,
3663};
3664
3665static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3666{
3667 struct nfs4_lockdata *data;
3668 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003669 struct rpc_message msg = {
3670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3671 .rpc_cred = state->owner->so_cred,
3672 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003673 struct rpc_task_setup task_setup_data = {
3674 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003675 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003676 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003677 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003678 .flags = RPC_TASK_ASYNC,
3679 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003680 int ret;
3681
Harvey Harrison3110ff82008-05-02 13:42:44 -07003682 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003683 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003684 fl->fl_u.nfs4_fl.owner);
3685 if (data == NULL)
3686 return -ENOMEM;
3687 if (IS_SETLKW(cmd))
3688 data->arg.block = 1;
3689 if (reclaim != 0)
3690 data->arg.reclaim = 1;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003691 msg.rpc_argp = &data->arg,
3692 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003693 task_setup_data.callback_data = data;
3694 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003695 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003696 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003697 ret = nfs4_wait_for_completion_rpc_task(task);
3698 if (ret == 0) {
3699 ret = data->rpc_status;
3700 if (ret == -NFS4ERR_DENIED)
3701 ret = -EAGAIN;
3702 } else
3703 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003704 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003705 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003706 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707}
3708
3709static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3710{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003711 struct nfs_server *server = NFS_SERVER(state->inode);
3712 struct nfs4_exception exception = { };
3713 int err;
3714
3715 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003716 /* Cache the lock if possible... */
3717 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3718 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003719 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3720 if (err != -NFS4ERR_DELAY)
3721 break;
3722 nfs4_handle_exception(server, err, &exception);
3723 } while (exception.retry);
3724 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725}
3726
3727static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3728{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003729 struct nfs_server *server = NFS_SERVER(state->inode);
3730 struct nfs4_exception exception = { };
3731 int err;
3732
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003733 err = nfs4_set_lock_state(state, request);
3734 if (err != 0)
3735 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003736 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003737 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3738 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003739 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3740 if (err != -NFS4ERR_DELAY)
3741 break;
3742 nfs4_handle_exception(server, err, &exception);
3743 } while (exception.retry);
3744 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745}
3746
3747static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3748{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003749 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003750 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 int status;
3752
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003753 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003754 status = nfs4_set_lock_state(state, request);
3755 if (status != 0)
3756 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003757 request->fl_flags |= FL_ACCESS;
3758 status = do_vfs_lock(request->fl_file, request);
3759 if (status < 0)
3760 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003761 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003762 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04003763 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003764 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05003765 request->fl_flags = fl_flags & ~FL_SLEEP;
3766 status = do_vfs_lock(request->fl_file, request);
3767 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003768 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003769 status = _nfs4_do_setlk(state, cmd, request, 0);
3770 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04003771 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003772 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003773 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003774 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07003775 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003776out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05003777 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003778out:
3779 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 return status;
3781}
3782
3783static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3784{
3785 struct nfs4_exception exception = { };
3786 int err;
3787
3788 do {
3789 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3790 _nfs4_proc_setlk(state, cmd, request),
3791 &exception);
3792 } while (exception.retry);
3793 return err;
3794}
3795
3796static int
3797nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3798{
3799 struct nfs_open_context *ctx;
3800 struct nfs4_state *state;
3801 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3802 int status;
3803
3804 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003805 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 state = ctx->state;
3807
3808 if (request->fl_start < 0 || request->fl_end < 0)
3809 return -EINVAL;
3810
3811 if (IS_GETLK(cmd))
3812 return nfs4_proc_getlk(state, F_GETLK, request);
3813
3814 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3815 return -EINVAL;
3816
3817 if (request->fl_type == F_UNLCK)
3818 return nfs4_proc_unlck(state, cmd, request);
3819
3820 do {
3821 status = nfs4_proc_setlk(state, cmd, request);
3822 if ((status != -EAGAIN) || IS_SETLK(cmd))
3823 break;
3824 timeout = nfs4_set_lock_task_retry(timeout);
3825 status = -ERESTARTSYS;
3826 if (signalled())
3827 break;
3828 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 return status;
3830}
3831
Trond Myklebust888e6942005-11-04 15:38:11 -05003832int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3833{
3834 struct nfs_server *server = NFS_SERVER(state->inode);
3835 struct nfs4_exception exception = { };
3836 int err;
3837
3838 err = nfs4_set_lock_state(state, fl);
3839 if (err != 0)
3840 goto out;
3841 do {
3842 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3843 if (err != -NFS4ERR_DELAY)
3844 break;
3845 err = nfs4_handle_exception(server, err, &exception);
3846 } while (exception.retry);
3847out:
3848 return err;
3849}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003850
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003851#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3852
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003853int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3854 size_t buflen, int flags)
3855{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003856 struct inode *inode = dentry->d_inode;
3857
3858 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3859 return -EOPNOTSUPP;
3860
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003861 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003862}
3863
3864/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3865 * and that's what we'll do for e.g. user attributes that haven't been set.
3866 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3867 * attributes in kernel-managed attribute namespaces. */
3868ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3869 size_t buflen)
3870{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003871 struct inode *inode = dentry->d_inode;
3872
3873 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3874 return -EOPNOTSUPP;
3875
3876 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003877}
3878
3879ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3880{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003881 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003882
J. Bruce Fields096455a2006-03-20 23:23:42 -05003883 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3884 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003885 if (buf && buflen < len)
3886 return -ERANGE;
3887 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003888 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3889 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003890}
3891
Trond Myklebust69aaaae2009-03-11 14:10:28 -04003892static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
3893{
3894 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
3895 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
3896 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
3897 return;
3898
3899 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3900 NFS_ATTR_FATTR_NLINK;
3901 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3902 fattr->nlink = 2;
3903}
3904
Trond Myklebust56659e92007-07-17 21:52:39 -04003905int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003906 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04003907{
3908 struct nfs_server *server = NFS_SERVER(dir);
3909 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04003910 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3911 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003912 };
3913 struct nfs4_fs_locations_arg args = {
3914 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05003915 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003916 .page = page,
3917 .bitmask = bitmask,
3918 };
Benny Halevy22958462009-04-01 09:22:02 -04003919 struct nfs4_fs_locations_res res = {
3920 .fs_locations = fs_locations,
3921 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04003922 struct rpc_message msg = {
3923 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3924 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04003925 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003926 };
3927 int status;
3928
Harvey Harrison3110ff82008-05-02 13:42:44 -07003929 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05003930 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04003931 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04003932 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003933 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04003934 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003935 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04003936 return status;
3937}
3938
Andy Adamson557134a2009-04-01 09:21:53 -04003939#ifdef CONFIG_NFS_V4_1
3940/* Destroy the slot table */
3941static void nfs4_destroy_slot_table(struct nfs4_session *session)
3942{
3943 if (session->fc_slot_table.slots == NULL)
3944 return;
3945 kfree(session->fc_slot_table.slots);
3946 session->fc_slot_table.slots = NULL;
3947 return;
3948}
3949
3950struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
3951{
3952 struct nfs4_session *session;
3953 struct nfs4_slot_table *tbl;
3954
3955 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
3956 if (!session)
3957 return NULL;
3958 tbl = &session->fc_slot_table;
3959 spin_lock_init(&tbl->slot_tbl_lock);
3960 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "Slot table");
3961 session->clp = clp;
3962 return session;
3963}
3964
3965void nfs4_destroy_session(struct nfs4_session *session)
3966{
3967 nfs4_destroy_slot_table(session);
3968 kfree(session);
3969}
3970
3971#endif /* CONFIG_NFS_V4_1 */
3972
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05003974 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05003975 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 .recover_open = nfs4_open_reclaim,
3977 .recover_lock = nfs4_lock_reclaim,
3978};
3979
Trond Myklebustb79a4a12008-12-23 15:21:41 -05003980struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05003981 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05003982 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 .recover_open = nfs4_open_expired,
3984 .recover_lock = nfs4_lock_expired,
3985};
3986
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003987static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003988 .permission = nfs_permission,
3989 .getattr = nfs_getattr,
3990 .setattr = nfs_setattr,
3991 .getxattr = nfs4_getxattr,
3992 .setxattr = nfs4_setxattr,
3993 .listxattr = nfs4_listxattr,
3994};
3995
David Howells509de812006-08-22 20:06:11 -04003996const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 .version = 4, /* protocol version */
3998 .dentry_ops = &nfs4_dentry_operations,
3999 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004000 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 .getroot = nfs4_proc_get_root,
4002 .getattr = nfs4_proc_getattr,
4003 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04004004 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 .lookup = nfs4_proc_lookup,
4006 .access = nfs4_proc_access,
4007 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 .create = nfs4_proc_create,
4009 .remove = nfs4_proc_remove,
4010 .unlink_setup = nfs4_proc_unlink_setup,
4011 .unlink_done = nfs4_proc_unlink_done,
4012 .rename = nfs4_proc_rename,
4013 .link = nfs4_proc_link,
4014 .symlink = nfs4_proc_symlink,
4015 .mkdir = nfs4_proc_mkdir,
4016 .rmdir = nfs4_proc_remove,
4017 .readdir = nfs4_proc_readdir,
4018 .mknod = nfs4_proc_mknod,
4019 .statfs = nfs4_proc_statfs,
4020 .fsinfo = nfs4_proc_fsinfo,
4021 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04004022 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 .decode_dirent = nfs4_decode_dirent,
4024 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05004025 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05004027 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05004029 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004031 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04004032 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033};
4034
4035/*
4036 * Local variables:
4037 * c-basic-offset: 8
4038 * End:
4039 */