blob: 188f2ce593ec2ddabe981bd97606e58d202c6253 [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/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400277 * nfs4_free_slot - free a slot and efficiently update slot table.
278 *
279 * freeing a slot is trivially done by clearing its respective bit
280 * in the bitmap.
281 * If the freed slotid equals highest_used_slotid we want to update it
282 * so that the server would be able to size down the slot table if needed,
283 * otherwise we know that the highest_used_slotid is still in use.
284 * When updating highest_used_slotid there may be "holes" in the bitmap
285 * so we need to scan down from highest_used_slotid to 0 looking for the now
286 * highest slotid in use.
287 * If none found, highest_used_slotid is set to -1.
288 */
289static void
290nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
291{
292 int slotid = free_slotid;
293
294 spin_lock(&tbl->slot_tbl_lock);
295 /* clear used bit in bitmap */
296 __clear_bit(slotid, tbl->used_slots);
297
298 /* update highest_used_slotid when it is freed */
299 if (slotid == tbl->highest_used_slotid) {
300 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
301 if (slotid >= 0 && slotid < tbl->max_slots)
302 tbl->highest_used_slotid = slotid;
303 else
304 tbl->highest_used_slotid = -1;
305 }
306 rpc_wake_up_next(&tbl->slot_tbl_waitq);
307 spin_unlock(&tbl->slot_tbl_lock);
308 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
309 free_slotid, tbl->highest_used_slotid);
310}
311
Andy Adamson13615872009-04-01 09:22:17 -0400312void nfs41_sequence_free_slot(const struct nfs_client *clp,
313 struct nfs4_sequence_res *res)
314{
315 struct nfs4_slot_table *tbl;
316
317 if (!nfs4_has_session(clp)) {
318 dprintk("%s: No session\n", __func__);
319 return;
320 }
321 tbl = &clp->cl_session->fc_slot_table;
322 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
323 dprintk("%s: No slot\n", __func__);
324 /* just wake up the next guy waiting since
325 * we may have not consumed a slot after all */
326 rpc_wake_up_next(&tbl->slot_tbl_waitq);
327 return;
328 }
329 nfs4_free_slot(tbl, res->sr_slotid);
330 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
331}
332
Andy Adamsonb0df8062009-04-01 09:22:18 -0400333static void nfs41_sequence_done(struct nfs_client *clp,
334 struct nfs4_sequence_res *res,
335 int rpc_status)
336{
337 unsigned long timestamp;
338 struct nfs4_slot_table *tbl;
339 struct nfs4_slot *slot;
340
341 /*
342 * sr_status remains 1 if an RPC level error occurred. The server
343 * may or may not have processed the sequence operation..
344 * Proceed as if the server received and processed the sequence
345 * operation.
346 */
347 if (res->sr_status == 1)
348 res->sr_status = NFS_OK;
349
350 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
351 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
352 goto out;
353
354 tbl = &clp->cl_session->fc_slot_table;
355 slot = tbl->slots + res->sr_slotid;
356
357 if (res->sr_status == 0) {
358 /* Update the slot's sequence and clientid lease timer */
359 ++slot->seq_nr;
360 timestamp = res->sr_renewal_time;
361 spin_lock(&clp->cl_lock);
362 if (time_before(clp->cl_last_renewal, timestamp))
363 clp->cl_last_renewal = timestamp;
364 spin_unlock(&clp->cl_lock);
365 return;
366 }
367out:
368 /* The session may be reset by one of the error handlers. */
369 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
370 nfs41_sequence_free_slot(clp, res);
371}
372
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400373/*
Benny Halevy510b8172009-04-01 09:22:14 -0400374 * nfs4_find_slot - efficiently look for a free slot
375 *
376 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
377 * If found, we mark the slot as used, update the highest_used_slotid,
378 * and respectively set up the sequence operation args.
379 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400380 *
381 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400382 */
383static u8
384nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
385{
386 int slotid;
387 u8 ret_id = NFS4_MAX_SLOT_TABLE;
388 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
389
Benny Halevy510b8172009-04-01 09:22:14 -0400390 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
391 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
392 tbl->max_slots);
393 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
394 if (slotid >= tbl->max_slots)
395 goto out;
396 __set_bit(slotid, tbl->used_slots);
397 if (slotid > tbl->highest_used_slotid)
398 tbl->highest_used_slotid = slotid;
399 ret_id = slotid;
400out:
401 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
402 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400403 return ret_id;
404}
405
Andy Adamsonce5039c2009-04-01 09:22:13 -0400406static int nfs41_setup_sequence(struct nfs4_session *session,
407 struct nfs4_sequence_args *args,
408 struct nfs4_sequence_res *res,
409 int cache_reply,
410 struct rpc_task *task)
411{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400412 struct nfs4_slot *slot;
413 struct nfs4_slot_table *tbl;
414 u8 slotid;
415
416 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400417 /* slot already allocated? */
418 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
419 return 0;
420
421 memset(res, 0, sizeof(*res));
422 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400423 tbl = &session->fc_slot_table;
424
425 spin_lock(&tbl->slot_tbl_lock);
426 slotid = nfs4_find_slot(tbl, task);
427 if (slotid == NFS4_MAX_SLOT_TABLE) {
428 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
429 spin_unlock(&tbl->slot_tbl_lock);
430 dprintk("<-- %s: no free slots\n", __func__);
431 return -EAGAIN;
432 }
433 spin_unlock(&tbl->slot_tbl_lock);
434
435 slot = tbl->slots + slotid;
436 args->sa_slotid = slotid;
437 args->sa_cache_this = cache_reply;
438
439 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
440
441 res->sr_slotid = slotid;
442 res->sr_renewal_time = jiffies;
443 /*
444 * sr_status is only set in decode_sequence, and so will remain
445 * set to 1 if an rpc level failure occurs.
446 */
447 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400448 return 0;
449}
450
451int nfs4_setup_sequence(struct nfs_client *clp,
452 struct nfs4_sequence_args *args,
453 struct nfs4_sequence_res *res,
454 int cache_reply,
455 struct rpc_task *task)
456{
457 int ret = 0;
458
459 dprintk("--> %s clp %p session %p sr_slotid %d\n",
460 __func__, clp, clp->cl_session, res->sr_slotid);
461
462 if (!nfs4_has_session(clp))
463 goto out;
464 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
465 task);
466 if (ret != -EAGAIN) {
467 /* terminate rpc task */
468 task->tk_status = ret;
469 task->tk_action = NULL;
470 }
471out:
472 dprintk("<-- %s status=%d\n", __func__, ret);
473 return ret;
474}
475
476struct nfs41_call_sync_data {
477 struct nfs_client *clp;
478 struct nfs4_sequence_args *seq_args;
479 struct nfs4_sequence_res *seq_res;
480 int cache_reply;
481};
482
483static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
484{
485 struct nfs41_call_sync_data *data = calldata;
486
487 dprintk("--> %s data->clp->cl_session %p\n", __func__,
488 data->clp->cl_session);
489 if (nfs4_setup_sequence(data->clp, data->seq_args,
490 data->seq_res, data->cache_reply, task))
491 return;
492 rpc_call_start(task);
493}
494
495struct rpc_call_ops nfs41_call_sync_ops = {
496 .rpc_call_prepare = nfs41_call_sync_prepare,
497};
498
499static int nfs4_call_sync_sequence(struct nfs_client *clp,
500 struct rpc_clnt *clnt,
501 struct rpc_message *msg,
502 struct nfs4_sequence_args *args,
503 struct nfs4_sequence_res *res,
504 int cache_reply)
505{
506 int ret;
507 struct rpc_task *task;
508 struct nfs41_call_sync_data data = {
509 .clp = clp,
510 .seq_args = args,
511 .seq_res = res,
512 .cache_reply = cache_reply,
513 };
514 struct rpc_task_setup task_setup = {
515 .rpc_client = clnt,
516 .rpc_message = msg,
517 .callback_ops = &nfs41_call_sync_ops,
518 .callback_data = &data
519 };
520
521 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
522 task = rpc_run_task(&task_setup);
523 if (IS_ERR(task))
524 ret = PTR_ERR(task);
525 else {
526 ret = task->tk_status;
527 rpc_put_task(task);
528 }
529 return ret;
530}
531
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400532int _nfs4_call_sync_session(struct nfs_server *server,
533 struct rpc_message *msg,
534 struct nfs4_sequence_args *args,
535 struct nfs4_sequence_res *res,
536 int cache_reply)
537{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400538 return nfs4_call_sync_sequence(server->nfs_client, server->client,
539 msg, args, res, cache_reply);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400540}
541
542#endif /* CONFIG_NFS_V4_1 */
543
544int _nfs4_call_sync(struct nfs_server *server,
545 struct rpc_message *msg,
546 struct nfs4_sequence_args *args,
547 struct nfs4_sequence_res *res,
548 int cache_reply)
549{
Benny Halevyf3752972009-04-01 09:22:04 -0400550 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400551 return rpc_call_sync(server->client, msg, 0);
552}
553
554#define nfs4_call_sync(server, msg, args, res, cache_reply) \
555 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
556 &(res)->seq_res, (cache_reply))
557
Andy Adamsonb0df8062009-04-01 09:22:18 -0400558static void nfs4_sequence_done(const struct nfs_server *server,
559 struct nfs4_sequence_res *res, int rpc_status)
560{
561#ifdef CONFIG_NFS_V4_1
562 if (nfs4_has_session(server->nfs_client))
563 nfs41_sequence_done(server->nfs_client, res, rpc_status);
564#endif /* CONFIG_NFS_V4_1 */
565}
566
567/* no restart, therefore free slot here */
568static void nfs4_sequence_done_free_slot(const struct nfs_server *server,
569 struct nfs4_sequence_res *res,
570 int rpc_status)
571{
572 nfs4_sequence_done(server, res, rpc_status);
573 nfs4_sequence_free_slot(server->nfs_client, res);
574}
575
Trond Myklebust38478b22006-05-25 01:40:57 -0400576static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Trond Myklebust38478b22006-05-25 01:40:57 -0400578 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Trond Myklebust38478b22006-05-25 01:40:57 -0400580 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400581 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
582 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400583 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400584 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400585 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100588struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400589 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100590 struct nfs_openargs o_arg;
591 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100592 struct nfs_open_confirmargs c_arg;
593 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100594 struct nfs_fattr f_attr;
595 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400596 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100597 struct dentry *dir;
598 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400599 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100600 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100601 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400602 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100603 int rpc_status;
604 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100605};
606
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400607
608static void nfs4_init_opendata_res(struct nfs4_opendata *p)
609{
610 p->o_res.f_attr = &p->f_attr;
611 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400612 p->o_res.seqid = p->o_arg.seqid;
613 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400614 p->o_res.server = p->o_arg.server;
615 nfs_fattr_init(&p->f_attr);
616 nfs_fattr_init(&p->dir_attr);
617}
618
Trond Myklebustad389da2007-06-05 12:30:00 -0400619static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500620 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100621 const struct iattr *attrs)
622{
Trond Myklebustad389da2007-06-05 12:30:00 -0400623 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100624 struct inode *dir = parent->d_inode;
625 struct nfs_server *server = NFS_SERVER(dir);
626 struct nfs4_opendata *p;
627
628 p = kzalloc(sizeof(*p), GFP_KERNEL);
629 if (p == NULL)
630 goto err;
631 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
632 if (p->o_arg.seqid == NULL)
633 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400634 p->path.mnt = mntget(path->mnt);
635 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100636 p->dir = parent;
637 p->owner = sp;
638 atomic_inc(&sp->so_count);
639 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500640 p->o_arg.open_flags = flags;
641 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400642 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400643 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400644 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100645 p->o_arg.server = server;
646 p->o_arg.bitmask = server->attr_bitmask;
647 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Andy Adamson5f7dbd52009-04-01 09:22:05 -0400648 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100649 if (flags & O_EXCL) {
650 u32 *s = (u32 *) p->o_arg.u.verifier.data;
651 s[0] = jiffies;
652 s[1] = current->pid;
653 } else if (flags & O_CREAT) {
654 p->o_arg.u.attrs = &p->attrs;
655 memcpy(&p->attrs, attrs, sizeof(p->attrs));
656 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100657 p->c_arg.fh = &p->o_res.fh;
658 p->c_arg.stateid = &p->o_res.stateid;
659 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400660 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400661 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100662 return p;
663err_free:
664 kfree(p);
665err:
666 dput(parent);
667 return NULL;
668}
669
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400670static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100671{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400672 struct nfs4_opendata *p = container_of(kref,
673 struct nfs4_opendata, kref);
674
675 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400676 if (p->state != NULL)
677 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400678 nfs4_put_state_owner(p->owner);
679 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700680 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400681 kfree(p);
682}
683
684static void nfs4_opendata_put(struct nfs4_opendata *p)
685{
686 if (p != NULL)
687 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100688}
689
Trond Myklebust06f814a2006-01-03 09:55:07 +0100690static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
691{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100692 int ret;
693
Trond Myklebust06f814a2006-01-03 09:55:07 +0100694 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100695 return ret;
696}
697
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500698static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400699{
700 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500701
702 if (open_mode & O_EXCL)
703 goto out;
704 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400705 case FMODE_READ:
706 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400707 break;
708 case FMODE_WRITE:
709 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400710 break;
711 case FMODE_READ|FMODE_WRITE:
712 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
713 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500714out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400715 return ret;
716}
717
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500718static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400719{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500720 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400721 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500722 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400723 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500724 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400725 return 1;
726}
727
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500728static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100729{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500730 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100731 case FMODE_WRITE:
732 state->n_wronly++;
733 break;
734 case FMODE_READ:
735 state->n_rdonly++;
736 break;
737 case FMODE_READ|FMODE_WRITE:
738 state->n_rdwr++;
739 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500740 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100741}
742
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500743static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400744{
745 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
746 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
747 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500748 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400749 case FMODE_READ:
750 set_bit(NFS_O_RDONLY_STATE, &state->flags);
751 break;
752 case FMODE_WRITE:
753 set_bit(NFS_O_WRONLY_STATE, &state->flags);
754 break;
755 case FMODE_READ|FMODE_WRITE:
756 set_bit(NFS_O_RDWR_STATE, &state->flags);
757 }
758}
759
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500760static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400761{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400762 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500763 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400764 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400765}
766
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500767static 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 -0700768{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400769 /*
770 * Protect the call to nfs4_state_set_mode_locked and
771 * serialise the stateid update
772 */
773 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400774 if (deleg_stateid != NULL) {
775 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
776 set_bit(NFS_DELEGATED_STATE, &state->flags);
777 }
778 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500779 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400780 write_sequnlock(&state->seqlock);
781 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500782 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700783 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500786static 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 -0500787{
788 struct nfs_inode *nfsi = NFS_I(state->inode);
789 struct nfs_delegation *deleg_cur;
790 int ret = 0;
791
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500792 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500793
794 rcu_read_lock();
795 deleg_cur = rcu_dereference(nfsi->delegation);
796 if (deleg_cur == NULL)
797 goto no_delegation;
798
799 spin_lock(&deleg_cur->lock);
800 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500801 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500802 goto no_delegation_unlock;
803
804 if (delegation == NULL)
805 delegation = &deleg_cur->stateid;
806 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
807 goto no_delegation_unlock;
808
Trond Myklebustb7391f42008-12-23 15:21:52 -0500809 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500810 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500811 ret = 1;
812no_delegation_unlock:
813 spin_unlock(&deleg_cur->lock);
814no_delegation:
815 rcu_read_unlock();
816
817 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500818 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500819 ret = 1;
820 }
821
822 return ret;
823}
824
825
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500826static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400827{
828 struct nfs_delegation *delegation;
829
830 rcu_read_lock();
831 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500832 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400833 rcu_read_unlock();
834 return;
835 }
836 rcu_read_unlock();
837 nfs_inode_return_delegation(inode);
838}
839
Trond Myklebust6ee41262007-07-08 14:11:36 -0400840static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400841{
842 struct nfs4_state *state = opendata->state;
843 struct nfs_inode *nfsi = NFS_I(state->inode);
844 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500845 int open_mode = opendata->o_arg.open_flags & O_EXCL;
846 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400847 nfs4_stateid stateid;
848 int ret = -EAGAIN;
849
Trond Myklebustaac00a82007-07-05 19:02:21 -0400850 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500851 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400852 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500853 if (can_open_cached(state, fmode, open_mode)) {
854 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400855 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400856 goto out_return_state;
857 }
858 spin_unlock(&state->owner->so_lock);
859 }
Trond Myklebust34310432008-12-23 15:21:38 -0500860 rcu_read_lock();
861 delegation = rcu_dereference(nfsi->delegation);
862 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500863 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500864 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400865 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500866 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400867 /* Save the delegation */
868 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
869 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400870 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400871 if (ret != 0)
872 goto out;
873 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500874
875 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500876 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500877 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400878 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400879out:
880 return ERR_PTR(ret);
881out_return_state:
882 atomic_inc(&state->count);
883 return state;
884}
885
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100886static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
887{
888 struct inode *inode;
889 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400890 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400891 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100892
Trond Myklebustaac00a82007-07-05 19:02:21 -0400893 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400894 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400895 goto out;
896 }
897
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400898 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100899 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400900 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100901 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400902 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500903 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400904 goto err;
905 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100906 state = nfs4_get_open_state(inode, data->owner);
907 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400908 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400909 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400910 int delegation_flags = 0;
911
Trond Myklebust003707c2007-07-05 18:07:55 -0400912 rcu_read_lock();
913 delegation = rcu_dereference(NFS_I(inode)->delegation);
914 if (delegation)
915 delegation_flags = delegation->flags;
916 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -0500917 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400918 nfs_inode_set_delegation(state->inode,
919 data->owner->so_cred,
920 &data->o_res);
921 else
922 nfs_inode_reclaim_delegation(state->inode,
923 data->owner->so_cred,
924 &data->o_res);
925 }
Trond Myklebust34310432008-12-23 15:21:38 -0500926
927 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100929 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400930out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100931 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400932err_put_inode:
933 iput(inode);
934err:
935 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100936}
937
Trond Myklebust864472e2006-01-03 09:55:15 +0100938static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
939{
940 struct nfs_inode *nfsi = NFS_I(state->inode);
941 struct nfs_open_context *ctx;
942
943 spin_lock(&state->inode->i_lock);
944 list_for_each_entry(ctx, &nfsi->open_files, list) {
945 if (ctx->state != state)
946 continue;
947 get_nfs_open_context(ctx);
948 spin_unlock(&state->inode->i_lock);
949 return ctx;
950 }
951 spin_unlock(&state->inode->i_lock);
952 return ERR_PTR(-ENOENT);
953}
954
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400955static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
956{
957 struct nfs4_opendata *opendata;
958
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400960 if (opendata == NULL)
961 return ERR_PTR(-ENOMEM);
962 opendata->state = state;
963 atomic_inc(&state->count);
964 return opendata;
965}
966
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500967static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +0100968{
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400969 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100970 int ret;
971
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500972 opendata->o_arg.open_flags = 0;
973 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400974 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
975 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
976 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100977 ret = _nfs4_proc_open(opendata);
978 if (ret != 0)
979 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400980 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400981 if (IS_ERR(newstate))
982 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500983 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400984 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100985 return 0;
986}
987
988static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
989{
Trond Myklebust864472e2006-01-03 09:55:15 +0100990 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100991 int ret;
992
993 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400994 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100995 smp_rmb();
996 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400997 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100998 if (ret != 0)
999 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001000 if (newstate != state)
1001 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001002 }
1003 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001004 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001005 if (ret != 0)
1006 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001007 if (newstate != state)
1008 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001009 }
1010 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001011 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001012 if (ret != 0)
1013 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001014 if (newstate != state)
1015 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001016 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001017 /*
1018 * We may have performed cached opens for all three recoveries.
1019 * Check if we need to update the current stateid.
1020 */
1021 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1022 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001023 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001024 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1025 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001026 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001027 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001028 return 0;
1029}
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031/*
1032 * OPEN_RECLAIM:
1033 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001035static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001037 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001038 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001039 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 int status;
1041
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001042 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1043 if (IS_ERR(opendata))
1044 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001045 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1046 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001047 rcu_read_lock();
1048 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001049 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001050 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001051 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001052 opendata->o_arg.u.delegation_type = delegation_type;
1053 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001054 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return status;
1056}
1057
Trond Myklebust539cd032007-06-05 11:46:42 -04001058static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
1060 struct nfs_server *server = NFS_SERVER(state->inode);
1061 struct nfs4_exception exception = { };
1062 int err;
1063 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001064 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001065 if (err != -NFS4ERR_DELAY)
1066 break;
1067 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 } while (exception.retry);
1069 return err;
1070}
1071
Trond Myklebust864472e2006-01-03 09:55:15 +01001072static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1073{
1074 struct nfs_open_context *ctx;
1075 int ret;
1076
1077 ctx = nfs4_state_find_open_context(state);
1078 if (IS_ERR(ctx))
1079 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001080 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001081 put_nfs_open_context(ctx);
1082 return ret;
1083}
1084
Trond Myklebust13437e12007-07-06 15:10:43 -04001085static 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 -07001086{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001087 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001088 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001090 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1091 if (IS_ERR(opendata))
1092 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001093 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001094 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001095 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001096 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001097 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001098 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
Trond Myklebust13437e12007-07-06 15:10:43 -04001101int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001104 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 int err;
1106 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001107 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 switch (err) {
1109 case 0:
1110 return err;
1111 case -NFS4ERR_STALE_CLIENTID:
1112 case -NFS4ERR_STALE_STATEID:
1113 case -NFS4ERR_EXPIRED:
1114 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001115 nfs4_schedule_state_recovery(server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return err;
1117 }
1118 err = nfs4_handle_exception(server, err, &exception);
1119 } while (exception.retry);
1120 return err;
1121}
1122
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001123static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1124{
1125 struct nfs4_opendata *data = calldata;
1126
1127 data->rpc_status = task->tk_status;
1128 if (RPC_ASSASSINATED(task))
1129 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001130 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001131 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1132 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001133 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001134 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001135 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001136 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001137}
1138
1139static void nfs4_open_confirm_release(void *calldata)
1140{
1141 struct nfs4_opendata *data = calldata;
1142 struct nfs4_state *state = NULL;
1143
1144 /* If this request hasn't been cancelled, do nothing */
1145 if (data->cancelled == 0)
1146 goto out_free;
1147 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001148 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001149 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001150 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001151 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001152 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001153out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001154 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001155}
1156
1157static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001158 .rpc_call_done = nfs4_open_confirm_done,
1159 .rpc_release = nfs4_open_confirm_release,
1160};
1161
1162/*
1163 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1164 */
1165static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1166{
1167 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1168 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001169 struct rpc_message msg = {
1170 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1171 .rpc_argp = &data->c_arg,
1172 .rpc_resp = &data->c_res,
1173 .rpc_cred = data->owner->so_cred,
1174 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001175 struct rpc_task_setup task_setup_data = {
1176 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001177 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001178 .callback_ops = &nfs4_open_confirm_ops,
1179 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001180 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001181 .flags = RPC_TASK_ASYNC,
1182 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 int status;
1184
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001185 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001186 data->rpc_done = 0;
1187 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001188 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001189 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001190 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001191 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001192 status = nfs4_wait_for_completion_rpc_task(task);
1193 if (status != 0) {
1194 data->cancelled = 1;
1195 smp_wmb();
1196 } else
1197 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001198 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 return status;
1200}
1201
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001202static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001204 struct nfs4_opendata *data = calldata;
1205 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001206
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001207 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1208 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001209 /*
1210 * Check if we still need to send an OPEN call, or if we can use
1211 * a delegation instead.
1212 */
1213 if (data->state != NULL) {
1214 struct nfs_delegation *delegation;
1215
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001216 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001217 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001218 rcu_read_lock();
1219 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1220 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001221 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001222 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001223 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001224 }
1225 rcu_read_unlock();
1226 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001227 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001228 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001229 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001230 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001231 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001232 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1233 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001234 data->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001235 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001236 return;
1237out_no_action:
1238 task->tk_action = NULL;
1239
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001240}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001242static void nfs4_open_done(struct rpc_task *task, void *calldata)
1243{
1244 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001246 data->rpc_status = task->tk_status;
1247 if (RPC_ASSASSINATED(task))
1248 return;
1249 if (task->tk_status == 0) {
1250 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001251 case S_IFREG:
1252 break;
1253 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001254 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001255 break;
1256 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001257 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001258 break;
1259 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001260 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001261 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001262 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001263 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1264 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001265 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001266 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001267}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001268
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001269static void nfs4_open_release(void *calldata)
1270{
1271 struct nfs4_opendata *data = calldata;
1272 struct nfs4_state *state = NULL;
1273
1274 /* If this request hasn't been cancelled, do nothing */
1275 if (data->cancelled == 0)
1276 goto out_free;
1277 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001278 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001279 goto out_free;
1280 /* In case we need an open_confirm, no cleanup! */
1281 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1282 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001283 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001284 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001285 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001286out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001287 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001288}
1289
1290static const struct rpc_call_ops nfs4_open_ops = {
1291 .rpc_call_prepare = nfs4_open_prepare,
1292 .rpc_call_done = nfs4_open_done,
1293 .rpc_release = nfs4_open_release,
1294};
1295
1296/*
1297 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1298 */
1299static int _nfs4_proc_open(struct nfs4_opendata *data)
1300{
1301 struct inode *dir = data->dir->d_inode;
1302 struct nfs_server *server = NFS_SERVER(dir);
1303 struct nfs_openargs *o_arg = &data->o_arg;
1304 struct nfs_openres *o_res = &data->o_res;
1305 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001306 struct rpc_message msg = {
1307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1308 .rpc_argp = o_arg,
1309 .rpc_resp = o_res,
1310 .rpc_cred = data->owner->so_cred,
1311 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001312 struct rpc_task_setup task_setup_data = {
1313 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001314 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001315 .callback_ops = &nfs4_open_ops,
1316 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001317 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001318 .flags = RPC_TASK_ASYNC,
1319 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001320 int status;
1321
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001322 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001323 data->rpc_done = 0;
1324 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001325 data->cancelled = 0;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001326 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001327 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001328 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001329 status = nfs4_wait_for_completion_rpc_task(task);
1330 if (status != 0) {
1331 data->cancelled = 1;
1332 smp_wmb();
1333 } else
1334 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001335 rpc_put_task(task);
Trond Myklebust3e309912007-07-07 13:19:59 -04001336 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001337 return status;
1338
Trond Myklebust99367812007-07-17 21:52:41 -04001339 if (o_res->fh.size == 0)
1340 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1341
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001342 if (o_arg->open_flags & O_CREAT) {
1343 update_changeattr(dir, &o_res->cinfo);
1344 nfs_post_op_update_inode(dir, o_res->dir_attr);
1345 } else
1346 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001348 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001350 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001353 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001354 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
Trond Myklebust10afec902007-05-14 17:16:04 -04001357static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001358{
David Howells7539bba2006-08-22 20:06:09 -04001359 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6b309542006-09-14 14:03:14 -04001360 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001361
Trond Myklebust6b309542006-09-14 14:03:14 -04001362 for (;;) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001363 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001364 if (ret != 0)
1365 return ret;
Trond Myklebuste598d842008-12-23 15:21:42 -05001366 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1367 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001368 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001369 nfs4_schedule_state_recovery(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001370 }
1371 return 0;
Trond Myklebust58d97142006-01-03 09:55:24 +01001372}
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374/*
1375 * OPEN_EXPIRED:
1376 * reclaim state on the server after a network partition.
1377 * Assumes caller holds the appropriate lock
1378 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001379static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001381 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001382 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001384 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1385 if (IS_ERR(opendata))
1386 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001387 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001388 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001389 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001390 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001391 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392}
1393
Trond Myklebust539cd032007-06-05 11:46:42 -04001394static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001395{
Trond Myklebust539cd032007-06-05 11:46:42 -04001396 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001397 struct nfs4_exception exception = { };
1398 int err;
1399
1400 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001401 err = _nfs4_open_expired(ctx, state);
Trond Myklebust027b6ca2008-12-23 16:04:13 -05001402 if (err != -NFS4ERR_DELAY)
1403 break;
1404 nfs4_handle_exception(server, err, &exception);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001405 } while (exception.retry);
1406 return err;
1407}
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1410{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001412 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Trond Myklebust864472e2006-01-03 09:55:15 +01001414 ctx = nfs4_state_find_open_context(state);
1415 if (IS_ERR(ctx))
1416 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001417 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001418 put_nfs_open_context(ctx);
1419 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
1422/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001423 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1424 * fields corresponding to attributes that were used to store the verifier.
1425 * Make sure we clobber those fields in the later setattr call
1426 */
1427static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1428{
1429 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1430 !(sattr->ia_valid & ATTR_ATIME_SET))
1431 sattr->ia_valid |= ATTR_ATIME;
1432
1433 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1434 !(sattr->ia_valid & ATTR_MTIME_SET))
1435 sattr->ia_valid |= ATTR_MTIME;
1436}
1437
1438/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001439 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001441static 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 -07001442{
1443 struct nfs4_state_owner *sp;
1444 struct nfs4_state *state = NULL;
1445 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001446 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001447 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 status = -ENOMEM;
1451 if (!(sp = nfs4_get_state_owner(server, cred))) {
1452 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1453 goto out_err;
1454 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001455 status = nfs4_recover_expired_lease(server);
1456 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001457 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001458 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001459 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001460 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001461 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001462 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001463 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Trond Myklebustaac00a82007-07-05 19:02:21 -04001465 if (path->dentry->d_inode != NULL)
1466 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1467
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001468 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001470 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001472 if (opendata->o_arg.open_flags & O_EXCL)
1473 nfs4_exclusive_attrset(opendata, sattr);
1474
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001475 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001476 status = PTR_ERR(state);
1477 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001478 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001479 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 *res = state;
1482 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001483err_opendata_put:
1484 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001485err_put_state_owner:
1486 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 *res = NULL;
1489 return status;
1490}
1491
1492
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001493static 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 -07001494{
1495 struct nfs4_exception exception = { };
1496 struct nfs4_state *res;
1497 int status;
1498
1499 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001500 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (status == 0)
1502 break;
1503 /* NOTE: BAD_SEQID means the server and client disagree about the
1504 * book-keeping w.r.t. state-changing operations
1505 * (OPEN/CLOSE/LOCK/LOCKU...)
1506 * It is actually a sign of a bug on the client or on the server.
1507 *
1508 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001509 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 * have unhashed the old state_owner for us, and that we can
1511 * therefore safely retry using a new one. We should still warn
1512 * the user though...
1513 */
1514 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001515 printk(KERN_WARNING "NFS: v4 server %s "
1516 " returned a bad sequence-id error!\n",
1517 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 exception.retry = 1;
1519 continue;
1520 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001521 /*
1522 * BAD_STATEID on OPEN means that the server cancelled our
1523 * state before it received the OPEN_CONFIRM.
1524 * Recover by retrying the request as per the discussion
1525 * on Page 181 of RFC3530.
1526 */
1527 if (status == -NFS4ERR_BAD_STATEID) {
1528 exception.retry = 1;
1529 continue;
1530 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001531 if (status == -EAGAIN) {
1532 /* We must have found a delegation */
1533 exception.retry = 1;
1534 continue;
1535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1537 status, &exception));
1538 } while (exception.retry);
1539 return res;
1540}
1541
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001542static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1543 struct nfs_fattr *fattr, struct iattr *sattr,
1544 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001546 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001548 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 .iap = sattr,
1550 .server = server,
1551 .bitmask = server->attr_bitmask,
1552 };
1553 struct nfs_setattrres res = {
1554 .fattr = fattr,
1555 .server = server,
1556 };
1557 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1559 .rpc_argp = &arg,
1560 .rpc_resp = &res,
1561 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001563 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001564 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Trond Myklebust0e574af2005-10-27 22:12:38 -04001566 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001568 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1569 /* Use that stateid */
1570 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001571 nfs4_copy_stateid(&arg.stateid, state, current->files);
1572 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1574
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001575 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001576 if (status == 0 && state != NULL)
1577 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001578 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001581static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1582 struct nfs_fattr *fattr, struct iattr *sattr,
1583 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001585 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 struct nfs4_exception exception = { };
1587 int err;
1588 do {
1589 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001590 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 &exception);
1592 } while (exception.retry);
1593 return err;
1594}
1595
1596struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001597 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 struct inode *inode;
1599 struct nfs4_state *state;
1600 struct nfs_closeargs arg;
1601 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001602 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001603 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604};
1605
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001606static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001607{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001608 struct nfs4_closedata *calldata = data;
1609 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001610
1611 nfs4_put_open_state(calldata->state);
1612 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001613 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001614 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001615 kfree(calldata);
1616}
1617
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001618static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001620 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 struct nfs_server *server = NFS_SERVER(calldata->inode);
1623
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001624 if (RPC_ASSASSINATED(task))
1625 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 /* hmm. we are done with the inode, and in the process of freeing
1627 * the state_owner. we keep this around to process errors
1628 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 switch (task->tk_status) {
1630 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001631 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001632 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 break;
1634 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001635 case -NFS4ERR_OLD_STATEID:
1636 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001638 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001639 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001641 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 rpc_restart_call(task);
1643 return;
1644 }
1645 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001646 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001649static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001651 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001652 struct nfs4_state *state = calldata->state;
Trond Myklebust003707c2007-07-05 18:07:55 -04001653 int clear_rd, clear_wr, clear_rdwr;
Trond Myklebust95121352005-10-18 14:20:12 -07001654
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001655 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001656 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001657
Trond Myklebust003707c2007-07-05 18:07:55 -04001658 clear_rd = clear_wr = clear_rdwr = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001659 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001660 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001661 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001662 if (state->n_rdonly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001663 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1664 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1665 }
1666 if (state->n_wronly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001667 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1668 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1669 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001670 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001671 spin_unlock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001672 if (!clear_rd && !clear_wr && !clear_rdwr) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001673 /* Note: exit _without_ calling nfs4_close_done */
1674 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001675 return;
1676 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001677 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust45328c32007-07-26 17:47:34 -04001678 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001679 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001680 calldata->arg.fmode = FMODE_READ;
Trond Myklebust45328c32007-07-26 17:47:34 -04001681 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001682 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001683 calldata->arg.fmode = FMODE_WRITE;
Trond Myklebust45328c32007-07-26 17:47:34 -04001684 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001685 calldata->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001686 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001689static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001690 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001691 .rpc_call_done = nfs4_close_done,
1692 .rpc_release = nfs4_free_closedata,
1693};
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695/*
1696 * It is possible for data to be read/written from a mem-mapped file
1697 * after the sys_close call (which hits the vfs layer as a flush).
1698 * This means that we can't safely call nfsv4 close on a file until
1699 * the inode is cleared. This in turn means that we are not good
1700 * NFSv4 citizens - we do not indicate to the server to update the file's
1701 * share state even when we are done with one of the three share
1702 * stateid's in the inode.
1703 *
1704 * NOTE: Caller must be holding the sp->so_owner semaphore!
1705 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001706int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001708 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001710 struct nfs4_state_owner *sp = state->owner;
1711 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001712 struct rpc_message msg = {
1713 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1714 .rpc_cred = state->owner->so_cred,
1715 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001716 struct rpc_task_setup task_setup_data = {
1717 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001718 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001719 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001720 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001721 .flags = RPC_TASK_ASYNC,
1722 };
Trond Myklebust95121352005-10-18 14:20:12 -07001723 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Trond Myklebust95121352005-10-18 14:20:12 -07001725 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001727 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001728 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001730 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001731 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001733 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001734 if (calldata->arg.seqid == NULL)
1735 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001736 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001737 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001738 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001739 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001740 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001741 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001742 calldata->path.mnt = mntget(path->mnt);
1743 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001744
Trond Myklebust5138fde2007-07-14 15:40:01 -04001745 msg.rpc_argp = &calldata->arg,
1746 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001747 task_setup_data.callback_data = calldata;
1748 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001749 if (IS_ERR(task))
1750 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001751 status = 0;
1752 if (wait)
1753 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001754 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001755 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001756out_free_calldata:
1757 kfree(calldata);
1758out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001759 nfs4_put_open_state(state);
1760 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001761 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
1763
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001764static 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 -07001765{
1766 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001767 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001768
Trond Myklebust1b45c462007-07-03 13:04:56 -04001769 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001770 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001771 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001772 state->owner->so_cred,
1773 nd->intent.open.flags);
1774 if (ret < 0)
1775 goto out_close;
1776 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001777 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001778 if (!IS_ERR(filp)) {
1779 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001780 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001781 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001782 return 0;
1783 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001784 ret = PTR_ERR(filp);
1785out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001786 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001787 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001788}
1789
1790struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1792{
Trond Myklebustad389da2007-06-05 12:30:00 -04001793 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001794 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001795 .dentry = dentry,
1796 };
Jan Blunck4ac91372008-02-14 19:34:32 -08001797 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 struct iattr attr;
1799 struct rpc_cred *cred;
1800 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001801 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001802 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804 if (nd->flags & LOOKUP_CREATE) {
1805 attr.ia_mode = nd->intent.open.create_mode;
1806 attr.ia_valid = ATTR_MODE;
1807 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04001808 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 } else {
1810 attr.ia_valid = 0;
1811 BUG_ON(nd->intent.open.flags & O_CREAT);
1812 }
1813
Trond Myklebust98a8e322008-03-12 12:25:28 -04001814 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001816 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04001817 parent = dentry->d_parent;
1818 /* Protect against concurrent sillydeletes */
1819 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001820 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001822 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001823 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001824 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04001825 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1826 }
Trond Myklebust565277f2007-10-15 18:17:53 -04001827 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001828 return (struct dentry *)state;
1829 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001830 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001831 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04001832 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04001833 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001834 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001835 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001836 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
1838
1839int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001840nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
Trond Myklebustad389da2007-06-05 12:30:00 -04001842 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001843 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001844 .dentry = dentry,
1845 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 struct rpc_cred *cred;
1847 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001848 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Trond Myklebust98a8e322008-03-12 12:25:28 -04001850 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 if (IS_ERR(cred))
1852 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001853 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001855 if (IS_ERR(state)) {
1856 switch (PTR_ERR(state)) {
1857 case -EPERM:
1858 case -EACCES:
1859 case -EDQUOT:
1860 case -ENOSPC:
1861 case -EROFS:
1862 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1863 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001864 default:
1865 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001866 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001867 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001868 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001869 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001870 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 return 1;
1872 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001873 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001874out_drop:
1875 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return 0;
1877}
1878
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001879void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1880{
1881 if (ctx->state == NULL)
1882 return;
1883 if (is_sync)
1884 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
1885 else
1886 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
1887}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1890{
Benny Halevy43652ad2009-04-01 09:21:54 -04001891 struct nfs4_server_caps_arg args = {
1892 .fhandle = fhandle,
1893 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 struct nfs4_server_caps_res res = {};
1895 struct rpc_message msg = {
1896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04001897 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 .rpc_resp = &res,
1899 };
1900 int status;
1901
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001902 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (status == 0) {
1904 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1905 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1906 server->caps |= NFS_CAP_ACLS;
1907 if (res.has_links != 0)
1908 server->caps |= NFS_CAP_HARDLINKS;
1909 if (res.has_symlinks != 0)
1910 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001911 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
1912 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
1913 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 server->acl_bitmask = res.acl_bitmask;
1915 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 return status;
1918}
1919
Trond Myklebust55a97592006-06-09 09:34:19 -04001920int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
1922 struct nfs4_exception exception = { };
1923 int err;
1924 do {
1925 err = nfs4_handle_exception(server,
1926 _nfs4_server_capabilities(server, fhandle),
1927 &exception);
1928 } while (exception.retry);
1929 return err;
1930}
1931
1932static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1933 struct nfs_fsinfo *info)
1934{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 struct nfs4_lookup_root_arg args = {
1936 .bitmask = nfs4_fattr_bitmap,
1937 };
1938 struct nfs4_lookup_res res = {
1939 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001940 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 .fh = fhandle,
1942 };
1943 struct rpc_message msg = {
1944 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1945 .rpc_argp = &args,
1946 .rpc_resp = &res,
1947 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001948 nfs_fattr_init(info->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001949 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
1951
1952static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1953 struct nfs_fsinfo *info)
1954{
1955 struct nfs4_exception exception = { };
1956 int err;
1957 do {
1958 err = nfs4_handle_exception(server,
1959 _nfs4_lookup_root(server, fhandle, info),
1960 &exception);
1961 } while (exception.retry);
1962 return err;
1963}
1964
David Howells54ceac42006-08-22 20:06:13 -04001965/*
1966 * get the file handle for the "/" directory on the server
1967 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04001969 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 int status;
1972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (status == 0)
1975 status = nfs4_server_capabilities(server, fhandle);
1976 if (status == 0)
1977 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08001978 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
Manoj Naik6b97fd32006-06-09 09:34:29 -04001981/*
1982 * Get locations and (maybe) other attributes of a referral.
1983 * Note that we'll actually follow the referral later when
1984 * we detect fsid mismatch in inode revalidation
1985 */
Trond Myklebust56659e92007-07-17 21:52:39 -04001986static 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 -04001987{
1988 int status = -ENOMEM;
1989 struct page *page = NULL;
1990 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04001991
1992 page = alloc_page(GFP_KERNEL);
1993 if (page == NULL)
1994 goto out;
1995 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1996 if (locations == NULL)
1997 goto out;
1998
Trond Myklebustc228fd32007-01-13 02:28:11 -05001999 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002000 if (status != 0)
2001 goto out;
2002 /* Make sure server returned a different fsid for the referral */
2003 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002004 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 -04002005 status = -EIO;
2006 goto out;
2007 }
2008
2009 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2010 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2011 if (!fattr->mode)
2012 fattr->mode = S_IFDIR;
2013 memset(fhandle, 0, sizeof(struct nfs_fh));
2014out:
2015 if (page)
2016 __free_page(page);
2017 if (locations)
2018 kfree(locations);
2019 return status;
2020}
2021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2023{
2024 struct nfs4_getattr_arg args = {
2025 .fh = fhandle,
2026 .bitmask = server->attr_bitmask,
2027 };
2028 struct nfs4_getattr_res res = {
2029 .fattr = fattr,
2030 .server = server,
2031 };
2032 struct rpc_message msg = {
2033 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2034 .rpc_argp = &args,
2035 .rpc_resp = &res,
2036 };
2037
Trond Myklebust0e574af2005-10-27 22:12:38 -04002038 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002039 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
2042static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2043{
2044 struct nfs4_exception exception = { };
2045 int err;
2046 do {
2047 err = nfs4_handle_exception(server,
2048 _nfs4_proc_getattr(server, fhandle, fattr),
2049 &exception);
2050 } while (exception.retry);
2051 return err;
2052}
2053
2054/*
2055 * The file is not closed if it is opened due to the a request to change
2056 * the size of the file. The open call will not be needed once the
2057 * VFS layer lookup-intents are implemented.
2058 *
2059 * Close is called when the inode is destroyed.
2060 * If we haven't opened the file for O_WRONLY, we
2061 * need to in the size_change case to obtain a stateid.
2062 *
2063 * Got race?
2064 * Because OPEN is always done by name in nfsv4, it is
2065 * possible that we opened a different file by the same
2066 * name. We can recognize this race condition, but we
2067 * can't do anything about it besides returning an error.
2068 *
2069 * This will be fixed with VFS changes (lookup-intent).
2070 */
2071static int
2072nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2073 struct iattr *sattr)
2074{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002075 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002076 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002077 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 int status;
2079
Trond Myklebust0e574af2005-10-27 22:12:38 -04002080 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Trond Myklebustd5308382005-11-04 15:33:38 -05002082 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002083 if (sattr->ia_valid & ATTR_FILE) {
2084 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002085
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002086 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002087 if (ctx) {
2088 cred = ctx->cred;
2089 state = ctx->state;
2090 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002091 }
2092
2093 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002094 if (status == 0)
2095 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 return status;
2097}
2098
Trond Myklebust56659e92007-07-17 21:52:39 -04002099static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2100 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002101 struct nfs_fattr *fattr)
2102{
2103 int status;
2104 struct nfs4_lookup_arg args = {
2105 .bitmask = server->attr_bitmask,
2106 .dir_fh = dirfh,
2107 .name = name,
2108 };
2109 struct nfs4_lookup_res res = {
2110 .server = server,
2111 .fattr = fattr,
2112 .fh = fhandle,
2113 };
2114 struct rpc_message msg = {
2115 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2116 .rpc_argp = &args,
2117 .rpc_resp = &res,
2118 };
2119
2120 nfs_fattr_init(fattr);
2121
2122 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002123 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002124 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002125 return status;
2126}
2127
2128static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2129 struct qstr *name, struct nfs_fh *fhandle,
2130 struct nfs_fattr *fattr)
2131{
2132 struct nfs4_exception exception = { };
2133 int err;
2134 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002135 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2136 /* FIXME: !!!! */
2137 if (err == -NFS4ERR_MOVED) {
2138 err = -EREMOTE;
2139 break;
2140 }
2141 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002142 } while (exception.retry);
2143 return err;
2144}
2145
Trond Myklebust56659e92007-07-17 21:52:39 -04002146static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2148{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002149 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002152 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002153 if (status == -NFS4ERR_MOVED)
2154 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 dprintk("NFS reply lookup: %d\n", status);
2156 return status;
2157}
2158
2159static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2160{
2161 struct nfs4_exception exception = { };
2162 int err;
2163 do {
2164 err = nfs4_handle_exception(NFS_SERVER(dir),
2165 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2166 &exception);
2167 } while (exception.retry);
2168 return err;
2169}
2170
2171static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2172{
Trond Myklebust76b32992007-08-10 17:45:11 -04002173 struct nfs_server *server = NFS_SERVER(inode);
2174 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 struct nfs4_accessargs args = {
2176 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002177 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002179 struct nfs4_accessres res = {
2180 .server = server,
2181 .fattr = &fattr,
2182 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 struct rpc_message msg = {
2184 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2185 .rpc_argp = &args,
2186 .rpc_resp = &res,
2187 .rpc_cred = entry->cred,
2188 };
2189 int mode = entry->mask;
2190 int status;
2191
2192 /*
2193 * Determine which access bits we want to ask for...
2194 */
2195 if (mode & MAY_READ)
2196 args.access |= NFS4_ACCESS_READ;
2197 if (S_ISDIR(inode->i_mode)) {
2198 if (mode & MAY_WRITE)
2199 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2200 if (mode & MAY_EXEC)
2201 args.access |= NFS4_ACCESS_LOOKUP;
2202 } else {
2203 if (mode & MAY_WRITE)
2204 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2205 if (mode & MAY_EXEC)
2206 args.access |= NFS4_ACCESS_EXECUTE;
2207 }
Trond Myklebust76b32992007-08-10 17:45:11 -04002208 nfs_fattr_init(&fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002209 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 if (!status) {
2211 entry->mask = 0;
2212 if (res.access & NFS4_ACCESS_READ)
2213 entry->mask |= MAY_READ;
2214 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2215 entry->mask |= MAY_WRITE;
2216 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2217 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04002218 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 }
2220 return status;
2221}
2222
2223static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2224{
2225 struct nfs4_exception exception = { };
2226 int err;
2227 do {
2228 err = nfs4_handle_exception(NFS_SERVER(inode),
2229 _nfs4_proc_access(inode, entry),
2230 &exception);
2231 } while (exception.retry);
2232 return err;
2233}
2234
2235/*
2236 * TODO: For the time being, we don't try to get any attributes
2237 * along with any of the zero-copy operations READ, READDIR,
2238 * READLINK, WRITE.
2239 *
2240 * In the case of the first three, we want to put the GETATTR
2241 * after the read-type operation -- this is because it is hard
2242 * to predict the length of a GETATTR response in v4, and thus
2243 * align the READ data correctly. This means that the GETATTR
2244 * may end up partially falling into the page cache, and we should
2245 * shift it into the 'tail' of the xdr_buf before processing.
2246 * To do this efficiently, we need to know the total length
2247 * of data received, which doesn't seem to be available outside
2248 * of the RPC layer.
2249 *
2250 * In the case of WRITE, we also want to put the GETATTR after
2251 * the operation -- in this case because we want to make sure
2252 * we get the post-operation mtime and size. This means that
2253 * we can't use xdr_encode_pages() as written: we need a variant
2254 * of it which would leave room in the 'tail' iovec.
2255 *
2256 * Both of these changes to the XDR layer would in fact be quite
2257 * minor, but I decided to leave them for a subsequent patch.
2258 */
2259static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2260 unsigned int pgbase, unsigned int pglen)
2261{
2262 struct nfs4_readlink args = {
2263 .fh = NFS_FH(inode),
2264 .pgbase = pgbase,
2265 .pglen = pglen,
2266 .pages = &page,
2267 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002268 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 struct rpc_message msg = {
2270 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2271 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002272 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 };
2274
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002275 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276}
2277
2278static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2279 unsigned int pgbase, unsigned int pglen)
2280{
2281 struct nfs4_exception exception = { };
2282 int err;
2283 do {
2284 err = nfs4_handle_exception(NFS_SERVER(inode),
2285 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2286 &exception);
2287 } while (exception.retry);
2288 return err;
2289}
2290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291/*
2292 * Got race?
2293 * We will need to arrange for the VFS layer to provide an atomic open.
2294 * Until then, this create/open method is prone to inefficiency and race
2295 * conditions due to the lookup, create, and open VFS calls from sys_open()
2296 * placed on the wire.
2297 *
2298 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2299 * The file will be opened again in the subsequent VFS open call
2300 * (nfs4_proc_file_open).
2301 *
2302 * The open for read will just hang around to be used by any process that
2303 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2304 */
2305
2306static int
2307nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002308 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
Trond Myklebustad389da2007-06-05 12:30:00 -04002310 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002311 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002312 .dentry = dentry,
2313 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 struct nfs4_state *state;
2315 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002316 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 int status = 0;
2318
Trond Myklebust98a8e322008-03-12 12:25:28 -04002319 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 if (IS_ERR(cred)) {
2321 status = PTR_ERR(cred);
2322 goto out;
2323 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002324 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002325 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 if (IS_ERR(state)) {
2327 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002328 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002330 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002331 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 if (flags & O_EXCL) {
2333 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002334 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002335 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002336 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002337 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002338 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002339 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002340 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002341 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002342 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002343out_putcred:
2344 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345out:
2346 return status;
2347}
2348
2349static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2350{
Trond Myklebust16e42952005-10-27 22:12:44 -04002351 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002352 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002354 .name.len = name->len,
2355 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002356 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002358 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002359 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002360 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002362 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2363 .rpc_argp = &args,
2364 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 };
2366 int status;
2367
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002368 nfs_fattr_init(&res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002369 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002370 if (status == 0) {
2371 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002372 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 return status;
2375}
2376
2377static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2378{
2379 struct nfs4_exception exception = { };
2380 int err;
2381 do {
2382 err = nfs4_handle_exception(NFS_SERVER(dir),
2383 _nfs4_proc_remove(dir, name),
2384 &exception);
2385 } while (exception.retry);
2386 return err;
2387}
2388
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002389static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002391 struct nfs_server *server = NFS_SERVER(dir);
2392 struct nfs_removeargs *args = msg->rpc_argp;
2393 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Trond Myklebusta65318b2009-03-11 14:10:28 -04002395 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002396 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398}
2399
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002400static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002402 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2403
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002404 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002405 return 0;
2406 update_changeattr(dir, &res->cinfo);
2407 nfs_post_op_update_inode(dir, &res->dir_attr);
2408 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409}
2410
2411static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2412 struct inode *new_dir, struct qstr *new_name)
2413{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002414 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 struct nfs4_rename_arg arg = {
2416 .old_dir = NFS_FH(old_dir),
2417 .new_dir = NFS_FH(new_dir),
2418 .old_name = old_name,
2419 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002420 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002422 struct nfs_fattr old_fattr, new_fattr;
2423 struct nfs4_rename_res res = {
2424 .server = server,
2425 .old_fattr = &old_fattr,
2426 .new_fattr = &new_fattr,
2427 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 struct rpc_message msg = {
2429 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2430 .rpc_argp = &arg,
2431 .rpc_resp = &res,
2432 };
2433 int status;
2434
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002435 nfs_fattr_init(res.old_fattr);
2436 nfs_fattr_init(res.new_fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002437 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
2439 if (!status) {
2440 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002441 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002443 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
2445 return status;
2446}
2447
2448static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2449 struct inode *new_dir, struct qstr *new_name)
2450{
2451 struct nfs4_exception exception = { };
2452 int err;
2453 do {
2454 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2455 _nfs4_proc_rename(old_dir, old_name,
2456 new_dir, new_name),
2457 &exception);
2458 } while (exception.retry);
2459 return err;
2460}
2461
2462static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2463{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002464 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 struct nfs4_link_arg arg = {
2466 .fh = NFS_FH(inode),
2467 .dir_fh = NFS_FH(dir),
2468 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002469 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002471 struct nfs_fattr fattr, dir_attr;
2472 struct nfs4_link_res res = {
2473 .server = server,
2474 .fattr = &fattr,
2475 .dir_attr = &dir_attr,
2476 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 struct rpc_message msg = {
2478 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2479 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002480 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 };
2482 int status;
2483
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002484 nfs_fattr_init(res.fattr);
2485 nfs_fattr_init(res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002486 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002487 if (!status) {
2488 update_changeattr(dir, &res.cinfo);
2489 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002490 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493 return status;
2494}
2495
2496static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2497{
2498 struct nfs4_exception exception = { };
2499 int err;
2500 do {
2501 err = nfs4_handle_exception(NFS_SERVER(inode),
2502 _nfs4_proc_link(inode, dir, name),
2503 &exception);
2504 } while (exception.retry);
2505 return err;
2506}
2507
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002508struct nfs4_createdata {
2509 struct rpc_message msg;
2510 struct nfs4_create_arg arg;
2511 struct nfs4_create_res res;
2512 struct nfs_fh fh;
2513 struct nfs_fattr fattr;
2514 struct nfs_fattr dir_fattr;
2515};
2516
2517static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2518 struct qstr *name, struct iattr *sattr, u32 ftype)
2519{
2520 struct nfs4_createdata *data;
2521
2522 data = kzalloc(sizeof(*data), GFP_KERNEL);
2523 if (data != NULL) {
2524 struct nfs_server *server = NFS_SERVER(dir);
2525
2526 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2527 data->msg.rpc_argp = &data->arg;
2528 data->msg.rpc_resp = &data->res;
2529 data->arg.dir_fh = NFS_FH(dir);
2530 data->arg.server = server;
2531 data->arg.name = name;
2532 data->arg.attrs = sattr;
2533 data->arg.ftype = ftype;
2534 data->arg.bitmask = server->attr_bitmask;
2535 data->res.server = server;
2536 data->res.fh = &data->fh;
2537 data->res.fattr = &data->fattr;
2538 data->res.dir_fattr = &data->dir_fattr;
2539 nfs_fattr_init(data->res.fattr);
2540 nfs_fattr_init(data->res.dir_fattr);
2541 }
2542 return data;
2543}
2544
2545static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2546{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002547 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2548 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002549 if (status == 0) {
2550 update_changeattr(dir, &data->res.dir_cinfo);
2551 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2552 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2553 }
2554 return status;
2555}
2556
2557static void nfs4_free_createdata(struct nfs4_createdata *data)
2558{
2559 kfree(data);
2560}
2561
Chuck Lever4f390c12006-08-22 20:06:22 -04002562static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002563 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002565 struct nfs4_createdata *data;
2566 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Chuck Lever94a6d752006-08-22 20:06:23 -04002568 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002569 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002570
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002571 status = -ENOMEM;
2572 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2573 if (data == NULL)
2574 goto out;
2575
2576 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2577 data->arg.u.symlink.pages = &page;
2578 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002580 status = nfs4_do_create(dir, dentry, data);
2581
2582 nfs4_free_createdata(data);
2583out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 return status;
2585}
2586
Chuck Lever4f390c12006-08-22 20:06:22 -04002587static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002588 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
2590 struct nfs4_exception exception = { };
2591 int err;
2592 do {
2593 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002594 _nfs4_proc_symlink(dir, dentry, page,
2595 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 &exception);
2597 } while (exception.retry);
2598 return err;
2599}
2600
2601static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2602 struct iattr *sattr)
2603{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002604 struct nfs4_createdata *data;
2605 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002607 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2608 if (data == NULL)
2609 goto out;
2610
2611 status = nfs4_do_create(dir, dentry, data);
2612
2613 nfs4_free_createdata(data);
2614out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 return status;
2616}
2617
2618static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2619 struct iattr *sattr)
2620{
2621 struct nfs4_exception exception = { };
2622 int err;
2623 do {
2624 err = nfs4_handle_exception(NFS_SERVER(dir),
2625 _nfs4_proc_mkdir(dir, dentry, sattr),
2626 &exception);
2627 } while (exception.retry);
2628 return err;
2629}
2630
2631static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2632 u64 cookie, struct page *page, unsigned int count, int plus)
2633{
2634 struct inode *dir = dentry->d_inode;
2635 struct nfs4_readdir_arg args = {
2636 .fh = NFS_FH(dir),
2637 .pages = &page,
2638 .pgbase = 0,
2639 .count = count,
Trond Myklebusta65318b2009-03-11 14:10:28 -04002640 .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 };
2642 struct nfs4_readdir_res res;
2643 struct rpc_message msg = {
2644 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2645 .rpc_argp = &args,
2646 .rpc_resp = &res,
2647 .rpc_cred = cred,
2648 };
2649 int status;
2650
Harvey Harrison3110ff82008-05-02 13:42:44 -07002651 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002652 dentry->d_parent->d_name.name,
2653 dentry->d_name.name,
2654 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2656 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002657 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 if (status == 0)
2659 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002660
2661 nfs_invalidate_atime(dir);
2662
Harvey Harrison3110ff82008-05-02 13:42:44 -07002663 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 return status;
2665}
2666
2667static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2668 u64 cookie, struct page *page, unsigned int count, int plus)
2669{
2670 struct nfs4_exception exception = { };
2671 int err;
2672 do {
2673 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2674 _nfs4_proc_readdir(dentry, cred, cookie,
2675 page, count, plus),
2676 &exception);
2677 } while (exception.retry);
2678 return err;
2679}
2680
2681static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2682 struct iattr *sattr, dev_t rdev)
2683{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002684 struct nfs4_createdata *data;
2685 int mode = sattr->ia_mode;
2686 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
2688 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2689 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002690
2691 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2692 if (data == NULL)
2693 goto out;
2694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002696 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002698 data->arg.ftype = NF4BLK;
2699 data->arg.u.device.specdata1 = MAJOR(rdev);
2700 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 }
2702 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002703 data->arg.ftype = NF4CHR;
2704 data->arg.u.device.specdata1 = MAJOR(rdev);
2705 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002708 status = nfs4_do_create(dir, dentry, data);
2709
2710 nfs4_free_createdata(data);
2711out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 return status;
2713}
2714
2715static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2716 struct iattr *sattr, dev_t rdev)
2717{
2718 struct nfs4_exception exception = { };
2719 int err;
2720 do {
2721 err = nfs4_handle_exception(NFS_SERVER(dir),
2722 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2723 &exception);
2724 } while (exception.retry);
2725 return err;
2726}
2727
2728static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2729 struct nfs_fsstat *fsstat)
2730{
2731 struct nfs4_statfs_arg args = {
2732 .fh = fhandle,
2733 .bitmask = server->attr_bitmask,
2734 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002735 struct nfs4_statfs_res res = {
2736 .fsstat = fsstat,
2737 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 struct rpc_message msg = {
2739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2740 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002741 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 };
2743
Trond Myklebust0e574af2005-10-27 22:12:38 -04002744 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002745 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746}
2747
2748static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2749{
2750 struct nfs4_exception exception = { };
2751 int err;
2752 do {
2753 err = nfs4_handle_exception(server,
2754 _nfs4_proc_statfs(server, fhandle, fsstat),
2755 &exception);
2756 } while (exception.retry);
2757 return err;
2758}
2759
2760static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2761 struct nfs_fsinfo *fsinfo)
2762{
2763 struct nfs4_fsinfo_arg args = {
2764 .fh = fhandle,
2765 .bitmask = server->attr_bitmask,
2766 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002767 struct nfs4_fsinfo_res res = {
2768 .fsinfo = fsinfo,
2769 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 struct rpc_message msg = {
2771 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2772 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002773 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 };
2775
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002776 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777}
2778
2779static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2780{
2781 struct nfs4_exception exception = { };
2782 int err;
2783
2784 do {
2785 err = nfs4_handle_exception(server,
2786 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2787 &exception);
2788 } while (exception.retry);
2789 return err;
2790}
2791
2792static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2793{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002794 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2796}
2797
2798static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2799 struct nfs_pathconf *pathconf)
2800{
2801 struct nfs4_pathconf_arg args = {
2802 .fh = fhandle,
2803 .bitmask = server->attr_bitmask,
2804 };
Benny Halevyd45b2982009-04-01 09:21:58 -04002805 struct nfs4_pathconf_res res = {
2806 .pathconf = pathconf,
2807 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 struct rpc_message msg = {
2809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2810 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04002811 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 };
2813
2814 /* None of the pathconf attributes are mandatory to implement */
2815 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2816 memset(pathconf, 0, sizeof(*pathconf));
2817 return 0;
2818 }
2819
Trond Myklebust0e574af2005-10-27 22:12:38 -04002820 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002821 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822}
2823
2824static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2825 struct nfs_pathconf *pathconf)
2826{
2827 struct nfs4_exception exception = { };
2828 int err;
2829
2830 do {
2831 err = nfs4_handle_exception(server,
2832 _nfs4_proc_pathconf(server, fhandle, pathconf),
2833 &exception);
2834 } while (exception.retry);
2835 return err;
2836}
2837
Trond Myklebustec06c092006-03-20 13:44:27 -05002838static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
Trond Myklebustec06c092006-03-20 13:44:27 -05002840 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002842 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 rpc_restart_call(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05002844 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
Trond Myklebust8850df92007-09-28 17:20:07 -04002846
2847 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002849 renew_lease(server, data->timestamp);
2850 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851}
2852
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002853static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002856 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857}
2858
Trond Myklebust788e7a82006-03-20 13:44:27 -05002859static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 struct inode *inode = data->inode;
2862
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002863 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002865 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002867 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04002869 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002870 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05002871 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872}
2873
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002874static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002876 struct nfs_server *server = NFS_SERVER(data->inode);
2877
Trond Myklebusta65318b2009-03-11 14:10:28 -04002878 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002879 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 data->timestamp = jiffies;
2881
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002882 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883}
2884
Trond Myklebust788e7a82006-03-20 13:44:27 -05002885static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 struct inode *inode = data->inode;
2888
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002889 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002891 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04002893 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002894 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895}
2896
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002897static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898{
Trond Myklebust788e7a82006-03-20 13:44:27 -05002899 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
Trond Myklebusta65318b2009-03-11 14:10:28 -04002901 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002902 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002903 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904}
2905
2906/*
2907 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2908 * standalone procedure for queueing an asynchronous RENEW.
2909 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002910static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911{
David Howellsadfa6f92006-08-22 20:06:08 -04002912 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002913 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
2915 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04002916 /* Unless we're shutting down, schedule state recovery! */
2917 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
2918 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 return;
2920 }
2921 spin_lock(&clp->cl_lock);
2922 if (time_before(clp->cl_last_renewal,timestamp))
2923 clp->cl_last_renewal = timestamp;
2924 spin_unlock(&clp->cl_lock);
2925}
2926
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002927static const struct rpc_call_ops nfs4_renew_ops = {
2928 .rpc_call_done = nfs4_renew_done,
2929};
2930
David Howellsadfa6f92006-08-22 20:06:08 -04002931int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932{
2933 struct rpc_message msg = {
2934 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2935 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002936 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 };
2938
2939 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002940 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941}
2942
David Howellsadfa6f92006-08-22 20:06:08 -04002943int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944{
2945 struct rpc_message msg = {
2946 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2947 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002948 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 };
2950 unsigned long now = jiffies;
2951 int status;
2952
2953 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2954 if (status < 0)
2955 return status;
2956 spin_lock(&clp->cl_lock);
2957 if (time_before(clp->cl_last_renewal,now))
2958 clp->cl_last_renewal = now;
2959 spin_unlock(&clp->cl_lock);
2960 return 0;
2961}
2962
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002963static inline int nfs4_server_supports_acls(struct nfs_server *server)
2964{
2965 return (server->caps & NFS_CAP_ACLS)
2966 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2967 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2968}
2969
2970/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2971 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2972 * the stack.
2973 */
2974#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2975
2976static void buf_to_pages(const void *buf, size_t buflen,
2977 struct page **pages, unsigned int *pgbase)
2978{
2979 const void *p = buf;
2980
2981 *pgbase = offset_in_page(buf);
2982 p -= *pgbase;
2983 while (p < buf + buflen) {
2984 *(pages++) = virt_to_page(p);
2985 p += PAGE_CACHE_SIZE;
2986 }
2987}
2988
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002989struct nfs4_cached_acl {
2990 int cached;
2991 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002992 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002993};
2994
2995static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002996{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002997 struct nfs_inode *nfsi = NFS_I(inode);
2998
2999 spin_lock(&inode->i_lock);
3000 kfree(nfsi->nfs4_acl);
3001 nfsi->nfs4_acl = acl;
3002 spin_unlock(&inode->i_lock);
3003}
3004
3005static void nfs4_zap_acl_attr(struct inode *inode)
3006{
3007 nfs4_set_cached_acl(inode, NULL);
3008}
3009
3010static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3011{
3012 struct nfs_inode *nfsi = NFS_I(inode);
3013 struct nfs4_cached_acl *acl;
3014 int ret = -ENOENT;
3015
3016 spin_lock(&inode->i_lock);
3017 acl = nfsi->nfs4_acl;
3018 if (acl == NULL)
3019 goto out;
3020 if (buf == NULL) /* user is just asking for length */
3021 goto out_len;
3022 if (acl->cached == 0)
3023 goto out;
3024 ret = -ERANGE; /* see getxattr(2) man page */
3025 if (acl->len > buflen)
3026 goto out;
3027 memcpy(buf, acl->data, acl->len);
3028out_len:
3029 ret = acl->len;
3030out:
3031 spin_unlock(&inode->i_lock);
3032 return ret;
3033}
3034
3035static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3036{
3037 struct nfs4_cached_acl *acl;
3038
3039 if (buf && acl_len <= PAGE_SIZE) {
3040 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3041 if (acl == NULL)
3042 goto out;
3043 acl->cached = 1;
3044 memcpy(acl->data, buf, acl_len);
3045 } else {
3046 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3047 if (acl == NULL)
3048 goto out;
3049 acl->cached = 0;
3050 }
3051 acl->len = acl_len;
3052out:
3053 nfs4_set_cached_acl(inode, acl);
3054}
3055
Trond Myklebust16b42892006-08-24 12:27:15 -04003056static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003057{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003058 struct page *pages[NFS4ACL_MAXPAGES];
3059 struct nfs_getaclargs args = {
3060 .fh = NFS_FH(inode),
3061 .acl_pages = pages,
3062 .acl_len = buflen,
3063 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003064 struct nfs_getaclres res = {
3065 .acl_len = buflen,
3066 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003067 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003068 struct rpc_message msg = {
3069 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3070 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003071 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003072 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003073 struct page *localpage = NULL;
3074 int ret;
3075
3076 if (buflen < PAGE_SIZE) {
3077 /* As long as we're doing a round trip to the server anyway,
3078 * let's be prepared for a page of acl data. */
3079 localpage = alloc_page(GFP_KERNEL);
3080 resp_buf = page_address(localpage);
3081 if (localpage == NULL)
3082 return -ENOMEM;
3083 args.acl_pages[0] = localpage;
3084 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003085 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003086 } else {
3087 resp_buf = buf;
3088 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3089 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003090 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003091 if (ret)
3092 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003093 if (res.acl_len > args.acl_len)
3094 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003095 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003096 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003097 if (buf) {
3098 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003099 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003100 goto out_free;
3101 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003102 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003103 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003104 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003105out_free:
3106 if (localpage)
3107 __free_page(localpage);
3108 return ret;
3109}
3110
Trond Myklebust16b42892006-08-24 12:27:15 -04003111static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3112{
3113 struct nfs4_exception exception = { };
3114 ssize_t ret;
3115 do {
3116 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3117 if (ret >= 0)
3118 break;
3119 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3120 } while (exception.retry);
3121 return ret;
3122}
3123
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003124static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3125{
3126 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003127 int ret;
3128
3129 if (!nfs4_server_supports_acls(server))
3130 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003131 ret = nfs_revalidate_inode(server, inode);
3132 if (ret < 0)
3133 return ret;
Trond Myklebustf41f7412008-06-11 17:39:04 -04003134 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3135 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003136 ret = nfs4_read_cached_acl(inode, buf, buflen);
3137 if (ret != -ENOENT)
3138 return ret;
3139 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003140}
3141
Trond Myklebust16b42892006-08-24 12:27:15 -04003142static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003143{
3144 struct nfs_server *server = NFS_SERVER(inode);
3145 struct page *pages[NFS4ACL_MAXPAGES];
3146 struct nfs_setaclargs arg = {
3147 .fh = NFS_FH(inode),
3148 .acl_pages = pages,
3149 .acl_len = buflen,
3150 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003151 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003152 struct rpc_message msg = {
3153 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3154 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003155 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003156 };
3157 int ret;
3158
3159 if (!nfs4_server_supports_acls(server))
3160 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003161 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003162 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003163 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003164 nfs_access_zap_cache(inode);
3165 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003166 return ret;
3167}
3168
Trond Myklebust16b42892006-08-24 12:27:15 -04003169static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3170{
3171 struct nfs4_exception exception = { };
3172 int err;
3173 do {
3174 err = nfs4_handle_exception(NFS_SERVER(inode),
3175 __nfs4_proc_set_acl(inode, buf, buflen),
3176 &exception);
3177 } while (exception.retry);
3178 return err;
3179}
3180
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181static int
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003182nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183{
David Howells7539bba2006-08-22 20:06:09 -04003184 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
3186 if (!clp || task->tk_status >= 0)
3187 return 0;
3188 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003189 case -NFS4ERR_ADMIN_REVOKED:
3190 case -NFS4ERR_BAD_STATEID:
3191 case -NFS4ERR_OPENMODE:
3192 if (state == NULL)
3193 break;
3194 nfs4_state_mark_reclaim_nograce(clp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 case -NFS4ERR_STALE_CLIENTID:
3196 case -NFS4ERR_STALE_STATEID:
3197 case -NFS4ERR_EXPIRED:
Trond Myklebust5d008372008-02-22 16:34:17 -05003198 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 nfs4_schedule_state_recovery(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05003200 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
Trond Myklebustfda13932008-02-22 16:34:12 -05003201 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 task->tk_status = 0;
3203 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 case -NFS4ERR_DELAY:
Trond Myklebust2e96d282008-06-11 16:42:05 -04003205 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003206 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3208 task->tk_status = 0;
3209 return -EAGAIN;
3210 case -NFS4ERR_OLD_STATEID:
3211 task->tk_status = 0;
3212 return -EAGAIN;
3213 }
3214 task->tk_status = nfs4_map_errors(task->tk_status);
3215 return 0;
3216}
3217
David Howellsadfa6f92006-08-22 20:06:08 -04003218int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219{
3220 nfs4_verifier sc_verifier;
3221 struct nfs4_setclientid setclientid = {
3222 .sc_verifier = &sc_verifier,
3223 .sc_prog = program,
3224 };
3225 struct rpc_message msg = {
3226 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3227 .rpc_argp = &setclientid,
3228 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003229 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 };
Al Virobc4785c2006-10-19 23:28:51 -07003231 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 int loop = 0;
3233 int status;
3234
Al Virobc4785c2006-10-19 23:28:51 -07003235 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3237 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3238
3239 for(;;) {
3240 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003241 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003242 clp->cl_ipaddr,
3243 rpc_peeraddr2str(clp->cl_rpcclient,
3244 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003245 rpc_peeraddr2str(clp->cl_rpcclient,
3246 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003247 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 clp->cl_id_uniquifier);
3249 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003250 sizeof(setclientid.sc_netid),
3251 rpc_peeraddr2str(clp->cl_rpcclient,
3252 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003254 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 clp->cl_ipaddr, port >> 8, port & 255);
3256
3257 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3258 if (status != -NFS4ERR_CLID_INUSE)
3259 break;
3260 if (signalled())
3261 break;
3262 if (loop++ & 1)
3263 ssleep(clp->cl_lease_time + 1);
3264 else
3265 if (++clp->cl_id_uniquifier == 0)
3266 break;
3267 }
3268 return status;
3269}
3270
David Howellsadfa6f92006-08-22 20:06:08 -04003271static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272{
3273 struct nfs_fsinfo fsinfo;
3274 struct rpc_message msg = {
3275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3276 .rpc_argp = clp,
3277 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003278 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 };
3280 unsigned long now;
3281 int status;
3282
3283 now = jiffies;
3284 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3285 if (status == 0) {
3286 spin_lock(&clp->cl_lock);
3287 clp->cl_lease_time = fsinfo.lease_time * HZ;
3288 clp->cl_last_renewal = now;
3289 spin_unlock(&clp->cl_lock);
3290 }
3291 return status;
3292}
3293
David Howellsadfa6f92006-08-22 20:06:08 -04003294int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003295{
Benny Halevy77e03672008-06-24 20:25:57 +03003296 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003297 int err;
3298 do {
3299 err = _nfs4_proc_setclientid_confirm(clp, cred);
3300 switch (err) {
3301 case 0:
3302 return err;
3303 case -NFS4ERR_RESOURCE:
3304 /* The IBM lawyers misread another document! */
3305 case -NFS4ERR_DELAY:
3306 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3307 }
3308 } while (err == 0);
3309 return err;
3310}
3311
Trond Myklebustfe650402006-01-03 09:55:18 +01003312struct nfs4_delegreturndata {
3313 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003314 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003315 struct nfs_fh fh;
3316 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003317 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003318 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003319 int rpc_status;
3320};
3321
Trond Myklebustfe650402006-01-03 09:55:18 +01003322static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3323{
3324 struct nfs4_delegreturndata *data = calldata;
3325 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003326 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01003327 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01003328}
3329
3330static void nfs4_delegreturn_release(void *calldata)
3331{
Trond Myklebustfe650402006-01-03 09:55:18 +01003332 kfree(calldata);
3333}
3334
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003335static const struct rpc_call_ops nfs4_delegreturn_ops = {
Trond Myklebustfe650402006-01-03 09:55:18 +01003336 .rpc_call_done = nfs4_delegreturn_done,
3337 .rpc_release = nfs4_delegreturn_release,
3338};
3339
Trond Myklebuste6f81072008-01-24 18:14:34 -05003340static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003341{
3342 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003343 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003344 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003345 struct rpc_message msg = {
3346 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3347 .rpc_cred = cred,
3348 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003349 struct rpc_task_setup task_setup_data = {
3350 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003351 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003352 .callback_ops = &nfs4_delegreturn_ops,
3353 .flags = RPC_TASK_ASYNC,
3354 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003355 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003356
3357 data = kmalloc(sizeof(*data), GFP_KERNEL);
3358 if (data == NULL)
3359 return -ENOMEM;
3360 data->args.fhandle = &data->fh;
3361 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003362 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003363 nfs_copy_fh(&data->fh, NFS_FH(inode));
3364 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003365 data->res.fattr = &data->fattr;
3366 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003367 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003368 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003369 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003370 data->rpc_status = 0;
3371
Trond Myklebustc970aa82007-07-14 15:39:59 -04003372 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003373 msg.rpc_argp = &data->args,
3374 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003375 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003376 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003377 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003378 if (!issync)
3379 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003380 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003381 if (status != 0)
3382 goto out;
3383 status = data->rpc_status;
3384 if (status != 0)
3385 goto out;
3386 nfs_refresh_inode(inode, &data->fattr);
3387out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003388 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003389 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390}
3391
Trond Myklebuste6f81072008-01-24 18:14:34 -05003392int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393{
3394 struct nfs_server *server = NFS_SERVER(inode);
3395 struct nfs4_exception exception = { };
3396 int err;
3397 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003398 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 switch (err) {
3400 case -NFS4ERR_STALE_STATEID:
3401 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 case 0:
3403 return 0;
3404 }
3405 err = nfs4_handle_exception(server, err, &exception);
3406 } while (exception.retry);
3407 return err;
3408}
3409
3410#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3411#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3412
3413/*
3414 * sleep, with exponential backoff, and retry the LOCK operation.
3415 */
3416static unsigned long
3417nfs4_set_lock_task_retry(unsigned long timeout)
3418{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003419 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 timeout <<= 1;
3421 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3422 return NFS4_LOCK_MAXTIMEOUT;
3423 return timeout;
3424}
3425
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3427{
3428 struct inode *inode = state->inode;
3429 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003430 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003431 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003433 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003435 struct nfs_lockt_res res = {
3436 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 };
3438 struct rpc_message msg = {
3439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3440 .rpc_argp = &arg,
3441 .rpc_resp = &res,
3442 .rpc_cred = state->owner->so_cred,
3443 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 struct nfs4_lock_state *lsp;
3445 int status;
3446
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003447 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003448 status = nfs4_set_lock_state(state, request);
3449 if (status != 0)
3450 goto out;
3451 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003452 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003453 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003454 switch (status) {
3455 case 0:
3456 request->fl_type = F_UNLCK;
3457 break;
3458 case -NFS4ERR_DENIED:
3459 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003461 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003462out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 return status;
3464}
3465
3466static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3467{
3468 struct nfs4_exception exception = { };
3469 int err;
3470
3471 do {
3472 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3473 _nfs4_proc_getlk(state, cmd, request),
3474 &exception);
3475 } while (exception.retry);
3476 return err;
3477}
3478
3479static int do_vfs_lock(struct file *file, struct file_lock *fl)
3480{
3481 int res = 0;
3482 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3483 case FL_POSIX:
3484 res = posix_lock_file_wait(file, fl);
3485 break;
3486 case FL_FLOCK:
3487 res = flock_lock_file_wait(file, fl);
3488 break;
3489 default:
3490 BUG();
3491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 return res;
3493}
3494
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003495struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003496 struct nfs_locku_args arg;
3497 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003498 struct nfs4_lock_state *lsp;
3499 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003500 struct file_lock fl;
3501 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003502 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003503};
3504
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003505static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3506 struct nfs_open_context *ctx,
3507 struct nfs4_lock_state *lsp,
3508 struct nfs_seqid *seqid)
3509{
3510 struct nfs4_unlockdata *p;
3511 struct inode *inode = lsp->ls_state->inode;
3512
3513 p = kmalloc(sizeof(*p), GFP_KERNEL);
3514 if (p == NULL)
3515 return NULL;
3516 p->arg.fh = NFS_FH(inode);
3517 p->arg.fl = &p->fl;
3518 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003519 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003520 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003521 p->arg.stateid = &lsp->ls_stateid;
3522 p->lsp = lsp;
3523 atomic_inc(&lsp->ls_count);
3524 /* Ensure we don't close file until we're done freeing locks! */
3525 p->ctx = get_nfs_open_context(ctx);
3526 memcpy(&p->fl, fl, sizeof(p->fl));
3527 p->server = NFS_SERVER(inode);
3528 return p;
3529}
3530
Trond Myklebust06f814a2006-01-03 09:55:07 +01003531static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003532{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003533 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003534 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003535 nfs4_put_lock_state(calldata->lsp);
3536 put_nfs_open_context(calldata->ctx);
3537 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003538}
3539
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003540static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003541{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003542 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003543
Trond Myklebust06f814a2006-01-03 09:55:07 +01003544 if (RPC_ASSASSINATED(task))
3545 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003546 switch (task->tk_status) {
3547 case 0:
3548 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003549 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003550 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003551 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003552 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003553 case -NFS4ERR_BAD_STATEID:
3554 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003555 case -NFS4ERR_STALE_STATEID:
3556 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003557 break;
3558 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003559 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003560 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003561 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003562}
3563
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003564static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003565{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003566 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003568 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003569 return;
3570 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003571 /* Note: exit _without_ running nfs4_locku_done */
3572 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003573 return;
3574 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003575 calldata->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003576 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577}
3578
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003579static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003580 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003581 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003582 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003583};
3584
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003585static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3586 struct nfs_open_context *ctx,
3587 struct nfs4_lock_state *lsp,
3588 struct nfs_seqid *seqid)
3589{
3590 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003591 struct rpc_message msg = {
3592 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3593 .rpc_cred = ctx->cred,
3594 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003595 struct rpc_task_setup task_setup_data = {
3596 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003597 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003598 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003599 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003600 .flags = RPC_TASK_ASYNC,
3601 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003602
Frank Filz137d6ac2007-07-09 15:32:29 -07003603 /* Ensure this is an unlock - when canceling a lock, the
3604 * canceled lock is passed in, and it won't be an unlock.
3605 */
3606 fl->fl_type = F_UNLCK;
3607
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003608 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3609 if (data == NULL) {
3610 nfs_free_seqid(seqid);
3611 return ERR_PTR(-ENOMEM);
3612 }
3613
Trond Myklebust5138fde2007-07-14 15:40:01 -04003614 msg.rpc_argp = &data->arg,
3615 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003616 task_setup_data.callback_data = data;
3617 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003618}
3619
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3621{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003622 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003623 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003624 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003625 struct rpc_task *task;
3626 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003627 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628
Trond Myklebust9b073572006-06-29 16:38:34 -04003629 status = nfs4_set_lock_state(state, request);
3630 /* Unlock _before_ we do the RPC call */
3631 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003632 down_read(&nfsi->rwsem);
3633 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3634 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003635 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003636 }
3637 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003638 if (status != 0)
3639 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003640 /* Is this a delegated lock? */
3641 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003642 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003643 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003644 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003645 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003646 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003647 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003648 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003649 status = PTR_ERR(task);
3650 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003651 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003652 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003653 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003654out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003655 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003656 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657}
3658
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003659struct nfs4_lockdata {
3660 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003661 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003662 struct nfs4_lock_state *lsp;
3663 struct nfs_open_context *ctx;
3664 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003665 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003666 int rpc_status;
3667 int cancelled;
3668};
3669
3670static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3671 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3672{
3673 struct nfs4_lockdata *p;
3674 struct inode *inode = lsp->ls_state->inode;
3675 struct nfs_server *server = NFS_SERVER(inode);
3676
3677 p = kzalloc(sizeof(*p), GFP_KERNEL);
3678 if (p == NULL)
3679 return NULL;
3680
3681 p->arg.fh = NFS_FH(inode);
3682 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003683 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3684 if (p->arg.open_seqid == NULL)
3685 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003686 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3687 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003688 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003689 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003690 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003691 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003692 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003693 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003694 p->lsp = lsp;
3695 atomic_inc(&lsp->ls_count);
3696 p->ctx = get_nfs_open_context(ctx);
3697 memcpy(&p->fl, fl, sizeof(p->fl));
3698 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003699out_free_seqid:
3700 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003701out_free:
3702 kfree(p);
3703 return NULL;
3704}
3705
3706static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3707{
3708 struct nfs4_lockdata *data = calldata;
3709 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
Harvey Harrison3110ff82008-05-02 13:42:44 -07003711 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003712 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3713 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003714 /* Do we need to do an open_to_lock_owner? */
3715 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05003716 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3717 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003718 data->arg.open_stateid = &state->stateid;
3719 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003720 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003721 } else
3722 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003723 data->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003724 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003725 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003726}
3727
3728static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3729{
3730 struct nfs4_lockdata *data = calldata;
3731
Harvey Harrison3110ff82008-05-02 13:42:44 -07003732 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003733
3734 data->rpc_status = task->tk_status;
3735 if (RPC_ASSASSINATED(task))
3736 goto out;
3737 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003738 if (data->rpc_status == 0)
3739 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3740 else
3741 goto out;
3742 }
3743 if (data->rpc_status == 0) {
3744 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3745 sizeof(data->lsp->ls_stateid.data));
3746 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003747 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003748 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003749out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07003750 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003751}
3752
3753static void nfs4_lock_release(void *calldata)
3754{
3755 struct nfs4_lockdata *data = calldata;
3756
Harvey Harrison3110ff82008-05-02 13:42:44 -07003757 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003758 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003759 if (data->cancelled != 0) {
3760 struct rpc_task *task;
3761 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3762 data->arg.lock_seqid);
3763 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003764 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003765 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003766 } else
3767 nfs_free_seqid(data->arg.lock_seqid);
3768 nfs4_put_lock_state(data->lsp);
3769 put_nfs_open_context(data->ctx);
3770 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003771 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003772}
3773
3774static const struct rpc_call_ops nfs4_lock_ops = {
3775 .rpc_call_prepare = nfs4_lock_prepare,
3776 .rpc_call_done = nfs4_lock_done,
3777 .rpc_release = nfs4_lock_release,
3778};
3779
3780static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3781{
3782 struct nfs4_lockdata *data;
3783 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003784 struct rpc_message msg = {
3785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3786 .rpc_cred = state->owner->so_cred,
3787 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003788 struct rpc_task_setup task_setup_data = {
3789 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003790 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003791 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003792 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003793 .flags = RPC_TASK_ASYNC,
3794 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003795 int ret;
3796
Harvey Harrison3110ff82008-05-02 13:42:44 -07003797 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003798 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003799 fl->fl_u.nfs4_fl.owner);
3800 if (data == NULL)
3801 return -ENOMEM;
3802 if (IS_SETLKW(cmd))
3803 data->arg.block = 1;
3804 if (reclaim != 0)
3805 data->arg.reclaim = 1;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003806 msg.rpc_argp = &data->arg,
3807 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003808 task_setup_data.callback_data = data;
3809 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003810 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003811 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003812 ret = nfs4_wait_for_completion_rpc_task(task);
3813 if (ret == 0) {
3814 ret = data->rpc_status;
3815 if (ret == -NFS4ERR_DENIED)
3816 ret = -EAGAIN;
3817 } else
3818 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003819 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003820 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003821 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822}
3823
3824static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3825{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003826 struct nfs_server *server = NFS_SERVER(state->inode);
3827 struct nfs4_exception exception = { };
3828 int err;
3829
3830 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003831 /* Cache the lock if possible... */
3832 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3833 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003834 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3835 if (err != -NFS4ERR_DELAY)
3836 break;
3837 nfs4_handle_exception(server, err, &exception);
3838 } while (exception.retry);
3839 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840}
3841
3842static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3843{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003844 struct nfs_server *server = NFS_SERVER(state->inode);
3845 struct nfs4_exception exception = { };
3846 int err;
3847
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003848 err = nfs4_set_lock_state(state, request);
3849 if (err != 0)
3850 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003851 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003852 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3853 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003854 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3855 if (err != -NFS4ERR_DELAY)
3856 break;
3857 nfs4_handle_exception(server, err, &exception);
3858 } while (exception.retry);
3859 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860}
3861
3862static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3863{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003864 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003865 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 int status;
3867
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003868 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003869 status = nfs4_set_lock_state(state, request);
3870 if (status != 0)
3871 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003872 request->fl_flags |= FL_ACCESS;
3873 status = do_vfs_lock(request->fl_file, request);
3874 if (status < 0)
3875 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003876 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003877 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04003878 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003879 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05003880 request->fl_flags = fl_flags & ~FL_SLEEP;
3881 status = do_vfs_lock(request->fl_file, request);
3882 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003883 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003884 status = _nfs4_do_setlk(state, cmd, request, 0);
3885 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04003886 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003887 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003888 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003889 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07003890 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003891out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05003892 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003893out:
3894 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 return status;
3896}
3897
3898static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3899{
3900 struct nfs4_exception exception = { };
3901 int err;
3902
3903 do {
3904 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3905 _nfs4_proc_setlk(state, cmd, request),
3906 &exception);
3907 } while (exception.retry);
3908 return err;
3909}
3910
3911static int
3912nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3913{
3914 struct nfs_open_context *ctx;
3915 struct nfs4_state *state;
3916 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3917 int status;
3918
3919 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003920 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 state = ctx->state;
3922
3923 if (request->fl_start < 0 || request->fl_end < 0)
3924 return -EINVAL;
3925
3926 if (IS_GETLK(cmd))
3927 return nfs4_proc_getlk(state, F_GETLK, request);
3928
3929 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3930 return -EINVAL;
3931
3932 if (request->fl_type == F_UNLCK)
3933 return nfs4_proc_unlck(state, cmd, request);
3934
3935 do {
3936 status = nfs4_proc_setlk(state, cmd, request);
3937 if ((status != -EAGAIN) || IS_SETLK(cmd))
3938 break;
3939 timeout = nfs4_set_lock_task_retry(timeout);
3940 status = -ERESTARTSYS;
3941 if (signalled())
3942 break;
3943 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 return status;
3945}
3946
Trond Myklebust888e6942005-11-04 15:38:11 -05003947int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3948{
3949 struct nfs_server *server = NFS_SERVER(state->inode);
3950 struct nfs4_exception exception = { };
3951 int err;
3952
3953 err = nfs4_set_lock_state(state, fl);
3954 if (err != 0)
3955 goto out;
3956 do {
3957 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3958 if (err != -NFS4ERR_DELAY)
3959 break;
3960 err = nfs4_handle_exception(server, err, &exception);
3961 } while (exception.retry);
3962out:
3963 return err;
3964}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003965
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003966#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3967
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003968int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3969 size_t buflen, int flags)
3970{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003971 struct inode *inode = dentry->d_inode;
3972
3973 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3974 return -EOPNOTSUPP;
3975
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003976 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003977}
3978
3979/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3980 * and that's what we'll do for e.g. user attributes that haven't been set.
3981 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3982 * attributes in kernel-managed attribute namespaces. */
3983ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3984 size_t buflen)
3985{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003986 struct inode *inode = dentry->d_inode;
3987
3988 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3989 return -EOPNOTSUPP;
3990
3991 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003992}
3993
3994ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3995{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003996 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003997
J. Bruce Fields096455a2006-03-20 23:23:42 -05003998 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3999 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004000 if (buf && buflen < len)
4001 return -ERANGE;
4002 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004003 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4004 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004005}
4006
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004007static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4008{
4009 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4010 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4011 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4012 return;
4013
4014 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4015 NFS_ATTR_FATTR_NLINK;
4016 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4017 fattr->nlink = 2;
4018}
4019
Trond Myklebust56659e92007-07-17 21:52:39 -04004020int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004021 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004022{
4023 struct nfs_server *server = NFS_SERVER(dir);
4024 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004025 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4026 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004027 };
4028 struct nfs4_fs_locations_arg args = {
4029 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004030 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004031 .page = page,
4032 .bitmask = bitmask,
4033 };
Benny Halevy22958462009-04-01 09:22:02 -04004034 struct nfs4_fs_locations_res res = {
4035 .fs_locations = fs_locations,
4036 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004037 struct rpc_message msg = {
4038 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4039 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004040 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004041 };
4042 int status;
4043
Harvey Harrison3110ff82008-05-02 13:42:44 -07004044 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004045 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004046 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004047 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004048 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004049 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004050 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004051 return status;
4052}
4053
Andy Adamson557134a2009-04-01 09:21:53 -04004054#ifdef CONFIG_NFS_V4_1
4055/* Destroy the slot table */
4056static void nfs4_destroy_slot_table(struct nfs4_session *session)
4057{
4058 if (session->fc_slot_table.slots == NULL)
4059 return;
4060 kfree(session->fc_slot_table.slots);
4061 session->fc_slot_table.slots = NULL;
4062 return;
4063}
4064
4065struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4066{
4067 struct nfs4_session *session;
4068 struct nfs4_slot_table *tbl;
4069
4070 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4071 if (!session)
4072 return NULL;
4073 tbl = &session->fc_slot_table;
4074 spin_lock_init(&tbl->slot_tbl_lock);
4075 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "Slot table");
4076 session->clp = clp;
4077 return session;
4078}
4079
4080void nfs4_destroy_session(struct nfs4_session *session)
4081{
4082 nfs4_destroy_slot_table(session);
4083 kfree(session);
4084}
4085
4086#endif /* CONFIG_NFS_V4_1 */
4087
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05004089 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004090 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 .recover_open = nfs4_open_reclaim,
4092 .recover_lock = nfs4_lock_reclaim,
4093};
4094
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004095struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05004096 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004097 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 .recover_open = nfs4_open_expired,
4099 .recover_lock = nfs4_lock_expired,
4100};
4101
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004102static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004103 .permission = nfs_permission,
4104 .getattr = nfs_getattr,
4105 .setattr = nfs_setattr,
4106 .getxattr = nfs4_getxattr,
4107 .setxattr = nfs4_setxattr,
4108 .listxattr = nfs4_listxattr,
4109};
4110
David Howells509de812006-08-22 20:06:11 -04004111const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 .version = 4, /* protocol version */
4113 .dentry_ops = &nfs4_dentry_operations,
4114 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004115 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 .getroot = nfs4_proc_get_root,
4117 .getattr = nfs4_proc_getattr,
4118 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04004119 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 .lookup = nfs4_proc_lookup,
4121 .access = nfs4_proc_access,
4122 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 .create = nfs4_proc_create,
4124 .remove = nfs4_proc_remove,
4125 .unlink_setup = nfs4_proc_unlink_setup,
4126 .unlink_done = nfs4_proc_unlink_done,
4127 .rename = nfs4_proc_rename,
4128 .link = nfs4_proc_link,
4129 .symlink = nfs4_proc_symlink,
4130 .mkdir = nfs4_proc_mkdir,
4131 .rmdir = nfs4_proc_remove,
4132 .readdir = nfs4_proc_readdir,
4133 .mknod = nfs4_proc_mknod,
4134 .statfs = nfs4_proc_statfs,
4135 .fsinfo = nfs4_proc_fsinfo,
4136 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04004137 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 .decode_dirent = nfs4_decode_dirent,
4139 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05004140 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05004142 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05004144 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004146 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04004147 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148};
4149
4150/*
4151 * Local variables:
4152 * c-basic-offset: 8
4153 * End:
4154 */