blob: ed6c8899806d8815f7a5fa53a6138f791523c2f5 [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>
Benny Halevy99fe60d2009-04-01 09:22:29 -040051#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Trond Myklebust4ce79712005-06-22 17:16:21 +000053#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050055#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050056#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040057#include "callback.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define NFSDBG_FACILITY NFSDBG_PROC
60
Trond Myklebust2066fe82006-09-15 08:30:46 -040061#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define NFS4_POLL_RETRY_MAX (15*HZ)
63
Trond Myklebustcdd4e682006-01-03 09:55:12 +010064struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010065static int _nfs4_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050067static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040068static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
69static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050072static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
74 if (err < -1000) {
75 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070076 __func__, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return -EIO;
78 }
79 return err;
80}
81
82/*
83 * This is our standard bitmap for GETATTR requests.
84 */
85const u32 nfs4_fattr_bitmap[2] = {
86 FATTR4_WORD0_TYPE
87 | FATTR4_WORD0_CHANGE
88 | FATTR4_WORD0_SIZE
89 | FATTR4_WORD0_FSID
90 | FATTR4_WORD0_FILEID,
91 FATTR4_WORD1_MODE
92 | FATTR4_WORD1_NUMLINKS
93 | FATTR4_WORD1_OWNER
94 | FATTR4_WORD1_OWNER_GROUP
95 | FATTR4_WORD1_RAWDEV
96 | FATTR4_WORD1_SPACE_USED
97 | FATTR4_WORD1_TIME_ACCESS
98 | FATTR4_WORD1_TIME_METADATA
99 | FATTR4_WORD1_TIME_MODIFY
100};
101
102const u32 nfs4_statfs_bitmap[2] = {
103 FATTR4_WORD0_FILES_AVAIL
104 | FATTR4_WORD0_FILES_FREE
105 | FATTR4_WORD0_FILES_TOTAL,
106 FATTR4_WORD1_SPACE_AVAIL
107 | FATTR4_WORD1_SPACE_FREE
108 | FATTR4_WORD1_SPACE_TOTAL
109};
110
Trond Myklebust4ce79712005-06-22 17:16:21 +0000111const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 FATTR4_WORD0_MAXLINK
113 | FATTR4_WORD0_MAXNAME,
114 0
115};
116
117const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
118 | FATTR4_WORD0_MAXREAD
119 | FATTR4_WORD0_MAXWRITE
120 | FATTR4_WORD0_LEASE_TIME,
121 0
122};
123
Manoj Naik830b8e32006-06-09 09:34:25 -0400124const u32 nfs4_fs_locations_bitmap[2] = {
125 FATTR4_WORD0_TYPE
126 | FATTR4_WORD0_CHANGE
127 | FATTR4_WORD0_SIZE
128 | FATTR4_WORD0_FSID
129 | FATTR4_WORD0_FILEID
130 | FATTR4_WORD0_FS_LOCATIONS,
131 FATTR4_WORD1_MODE
132 | FATTR4_WORD1_NUMLINKS
133 | FATTR4_WORD1_OWNER
134 | FATTR4_WORD1_OWNER_GROUP
135 | FATTR4_WORD1_RAWDEV
136 | FATTR4_WORD1_SPACE_USED
137 | FATTR4_WORD1_TIME_ACCESS
138 | FATTR4_WORD1_TIME_METADATA
139 | FATTR4_WORD1_TIME_MODIFY
140 | FATTR4_WORD1_MOUNTED_ON_FILEID
141};
142
Al Virobc4785c2006-10-19 23:28:51 -0700143static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 struct nfs4_readdir_arg *readdir)
145{
Al Viro0dbb4c62006-10-19 23:28:49 -0700146 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 BUG_ON(readdir->count < 80);
149 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000150 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
152 return;
153 }
154
155 readdir->cookie = 0;
156 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
157 if (cookie == 2)
158 return;
159
160 /*
161 * NFSv4 servers do not return entries for '.' and '..'
162 * Therefore, we fake these entries here. We let '.'
163 * have cookie 0 and '..' have cookie 1. Note that
164 * when talking to the server, we always send cookie 0
165 * instead of 1 or 2.
166 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700167 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 if (cookie == 0) {
170 *p++ = xdr_one; /* next */
171 *p++ = xdr_zero; /* cookie, first word */
172 *p++ = xdr_one; /* cookie, second word */
173 *p++ = xdr_one; /* entry len */
174 memcpy(p, ".\0\0\0", 4); /* entry */
175 p++;
176 *p++ = xdr_one; /* bitmap length */
177 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
178 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400179 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181
182 *p++ = xdr_one; /* next */
183 *p++ = xdr_zero; /* cookie, first word */
184 *p++ = xdr_two; /* cookie, second word */
185 *p++ = xdr_two; /* entry len */
186 memcpy(p, "..\0\0", 4); /* entry */
187 p++;
188 *p++ = xdr_one; /* bitmap length */
189 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
190 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400191 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 readdir->pgbase = (char *)p - (char *)start;
194 readdir->count -= readdir->pgbase;
195 kunmap_atomic(start, KM_USER0);
196}
197
Trond Myklebust65de8722008-12-23 15:21:44 -0500198static int nfs4_wait_clnt_recover(struct nfs_client *clp)
199{
200 int res;
201
202 might_sleep();
203
Trond Myklebuste005e802008-12-23 15:21:48 -0500204 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400205 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500206 return res;
207}
208
209static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
210{
211 int res = 0;
212
213 might_sleep();
214
215 if (*timeout <= 0)
216 *timeout = NFS4_POLL_RETRY_MIN;
217 if (*timeout > NFS4_POLL_RETRY_MAX)
218 *timeout = NFS4_POLL_RETRY_MAX;
219 schedule_timeout_killable(*timeout);
220 if (fatal_signal_pending(current))
221 res = -ERESTARTSYS;
222 *timeout <<= 1;
223 return res;
224}
225
226/* This is the error handling routine for processes that are allowed
227 * to sleep.
228 */
229static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
230{
231 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500232 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500233 int ret = errorcode;
234
235 exception->retry = 0;
236 switch(errorcode) {
237 case 0:
238 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500239 case -NFS4ERR_ADMIN_REVOKED:
240 case -NFS4ERR_BAD_STATEID:
241 case -NFS4ERR_OPENMODE:
242 if (state == NULL)
243 break;
244 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500245 case -NFS4ERR_STALE_CLIENTID:
246 case -NFS4ERR_STALE_STATEID:
247 case -NFS4ERR_EXPIRED:
248 nfs4_schedule_state_recovery(clp);
249 ret = nfs4_wait_clnt_recover(clp);
250 if (ret == 0)
251 exception->retry = 1;
Andy Adamson4745e312009-04-01 09:22:42 -0400252#if !defined(CONFIG_NFS_V4_1)
Trond Myklebust65de8722008-12-23 15:21:44 -0500253 break;
Andy Adamson4745e312009-04-01 09:22:42 -0400254#else /* !defined(CONFIG_NFS_V4_1) */
255 if (!nfs4_has_session(server->nfs_client))
256 break;
257 /* FALLTHROUGH */
258 case -NFS4ERR_BADSESSION:
259 case -NFS4ERR_BADSLOT:
260 case -NFS4ERR_BAD_HIGH_SLOT:
261 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
262 case -NFS4ERR_DEADSESSION:
263 case -NFS4ERR_SEQ_FALSE_RETRY:
264 case -NFS4ERR_SEQ_MISORDERED:
265 dprintk("%s ERROR: %d Reset session\n", __func__,
266 errorcode);
267 set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
268 exception->retry = 1;
269 /* FALLTHROUGH */
270#endif /* !defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500271 case -NFS4ERR_FILE_OPEN:
272 case -NFS4ERR_GRACE:
273 case -NFS4ERR_DELAY:
274 ret = nfs4_delay(server->client, &exception->timeout);
275 if (ret != 0)
276 break;
277 case -NFS4ERR_OLD_STATEID:
278 exception->retry = 1;
279 }
280 /* We failed to handle the error */
281 return nfs4_map_errors(ret);
282}
283
284
Trond Myklebust26e976a2006-01-03 09:55:21 +0100285static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
David Howells7539bba2006-08-22 20:06:09 -0400287 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 spin_lock(&clp->cl_lock);
289 if (time_before(clp->cl_last_renewal,timestamp))
290 clp->cl_last_renewal = timestamp;
291 spin_unlock(&clp->cl_lock);
292}
293
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400294#if defined(CONFIG_NFS_V4_1)
295
Benny Halevy510b8172009-04-01 09:22:14 -0400296/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400297 * nfs4_free_slot - free a slot and efficiently update slot table.
298 *
299 * freeing a slot is trivially done by clearing its respective bit
300 * in the bitmap.
301 * If the freed slotid equals highest_used_slotid we want to update it
302 * so that the server would be able to size down the slot table if needed,
303 * otherwise we know that the highest_used_slotid is still in use.
304 * When updating highest_used_slotid there may be "holes" in the bitmap
305 * so we need to scan down from highest_used_slotid to 0 looking for the now
306 * highest slotid in use.
307 * If none found, highest_used_slotid is set to -1.
308 */
309static void
310nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
311{
312 int slotid = free_slotid;
313
314 spin_lock(&tbl->slot_tbl_lock);
315 /* clear used bit in bitmap */
316 __clear_bit(slotid, tbl->used_slots);
317
318 /* update highest_used_slotid when it is freed */
319 if (slotid == tbl->highest_used_slotid) {
320 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
321 if (slotid >= 0 && slotid < tbl->max_slots)
322 tbl->highest_used_slotid = slotid;
323 else
324 tbl->highest_used_slotid = -1;
325 }
326 rpc_wake_up_next(&tbl->slot_tbl_waitq);
327 spin_unlock(&tbl->slot_tbl_lock);
328 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
329 free_slotid, tbl->highest_used_slotid);
330}
331
Andy Adamson13615872009-04-01 09:22:17 -0400332void nfs41_sequence_free_slot(const struct nfs_client *clp,
333 struct nfs4_sequence_res *res)
334{
335 struct nfs4_slot_table *tbl;
336
337 if (!nfs4_has_session(clp)) {
338 dprintk("%s: No session\n", __func__);
339 return;
340 }
341 tbl = &clp->cl_session->fc_slot_table;
342 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
343 dprintk("%s: No slot\n", __func__);
344 /* just wake up the next guy waiting since
345 * we may have not consumed a slot after all */
346 rpc_wake_up_next(&tbl->slot_tbl_waitq);
347 return;
348 }
349 nfs4_free_slot(tbl, res->sr_slotid);
350 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
351}
352
Andy Adamsonb0df8062009-04-01 09:22:18 -0400353static void nfs41_sequence_done(struct nfs_client *clp,
354 struct nfs4_sequence_res *res,
355 int rpc_status)
356{
357 unsigned long timestamp;
358 struct nfs4_slot_table *tbl;
359 struct nfs4_slot *slot;
360
361 /*
362 * sr_status remains 1 if an RPC level error occurred. The server
363 * may or may not have processed the sequence operation..
364 * Proceed as if the server received and processed the sequence
365 * operation.
366 */
367 if (res->sr_status == 1)
368 res->sr_status = NFS_OK;
369
370 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
371 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
372 goto out;
373
374 tbl = &clp->cl_session->fc_slot_table;
375 slot = tbl->slots + res->sr_slotid;
376
377 if (res->sr_status == 0) {
378 /* Update the slot's sequence and clientid lease timer */
379 ++slot->seq_nr;
380 timestamp = res->sr_renewal_time;
381 spin_lock(&clp->cl_lock);
382 if (time_before(clp->cl_last_renewal, timestamp))
383 clp->cl_last_renewal = timestamp;
384 spin_unlock(&clp->cl_lock);
385 return;
386 }
387out:
388 /* The session may be reset by one of the error handlers. */
389 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
390 nfs41_sequence_free_slot(clp, res);
391}
392
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400393/*
Benny Halevy510b8172009-04-01 09:22:14 -0400394 * nfs4_find_slot - efficiently look for a free slot
395 *
396 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
397 * If found, we mark the slot as used, update the highest_used_slotid,
398 * and respectively set up the sequence operation args.
399 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400400 *
401 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400402 */
403static u8
404nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
405{
406 int slotid;
407 u8 ret_id = NFS4_MAX_SLOT_TABLE;
408 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
409
Benny Halevy510b8172009-04-01 09:22:14 -0400410 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
411 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
412 tbl->max_slots);
413 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
414 if (slotid >= tbl->max_slots)
415 goto out;
416 __set_bit(slotid, tbl->used_slots);
417 if (slotid > tbl->highest_used_slotid)
418 tbl->highest_used_slotid = slotid;
419 ret_id = slotid;
420out:
421 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
422 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400423 return ret_id;
424}
425
Andy Adamsonce5039c2009-04-01 09:22:13 -0400426static int nfs41_setup_sequence(struct nfs4_session *session,
427 struct nfs4_sequence_args *args,
428 struct nfs4_sequence_res *res,
429 int cache_reply,
430 struct rpc_task *task)
431{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400432 struct nfs4_slot *slot;
433 struct nfs4_slot_table *tbl;
434 u8 slotid;
435
436 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400437 /* slot already allocated? */
438 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
439 return 0;
440
441 memset(res, 0, sizeof(*res));
442 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400443 tbl = &session->fc_slot_table;
444
445 spin_lock(&tbl->slot_tbl_lock);
446 slotid = nfs4_find_slot(tbl, task);
447 if (slotid == NFS4_MAX_SLOT_TABLE) {
448 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
449 spin_unlock(&tbl->slot_tbl_lock);
450 dprintk("<-- %s: no free slots\n", __func__);
451 return -EAGAIN;
452 }
453 spin_unlock(&tbl->slot_tbl_lock);
454
455 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400456 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400457 args->sa_slotid = slotid;
458 args->sa_cache_this = cache_reply;
459
460 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
461
Benny Halevy99fe60d2009-04-01 09:22:29 -0400462 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400463 res->sr_slotid = slotid;
464 res->sr_renewal_time = jiffies;
465 /*
466 * sr_status is only set in decode_sequence, and so will remain
467 * set to 1 if an rpc level failure occurs.
468 */
469 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400470 return 0;
471}
472
473int nfs4_setup_sequence(struct nfs_client *clp,
474 struct nfs4_sequence_args *args,
475 struct nfs4_sequence_res *res,
476 int cache_reply,
477 struct rpc_task *task)
478{
479 int ret = 0;
480
481 dprintk("--> %s clp %p session %p sr_slotid %d\n",
482 __func__, clp, clp->cl_session, res->sr_slotid);
483
484 if (!nfs4_has_session(clp))
485 goto out;
486 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
487 task);
488 if (ret != -EAGAIN) {
489 /* terminate rpc task */
490 task->tk_status = ret;
491 task->tk_action = NULL;
492 }
493out:
494 dprintk("<-- %s status=%d\n", __func__, ret);
495 return ret;
496}
497
498struct nfs41_call_sync_data {
499 struct nfs_client *clp;
500 struct nfs4_sequence_args *seq_args;
501 struct nfs4_sequence_res *seq_res;
502 int cache_reply;
503};
504
505static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
506{
507 struct nfs41_call_sync_data *data = calldata;
508
509 dprintk("--> %s data->clp->cl_session %p\n", __func__,
510 data->clp->cl_session);
511 if (nfs4_setup_sequence(data->clp, data->seq_args,
512 data->seq_res, data->cache_reply, task))
513 return;
514 rpc_call_start(task);
515}
516
Andy Adamson69ab40c2009-04-01 09:22:19 -0400517static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
518{
519 struct nfs41_call_sync_data *data = calldata;
520
521 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
522 nfs41_sequence_free_slot(data->clp, data->seq_res);
523}
524
Andy Adamsonce5039c2009-04-01 09:22:13 -0400525struct rpc_call_ops nfs41_call_sync_ops = {
526 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400527 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400528};
529
530static int nfs4_call_sync_sequence(struct nfs_client *clp,
531 struct rpc_clnt *clnt,
532 struct rpc_message *msg,
533 struct nfs4_sequence_args *args,
534 struct nfs4_sequence_res *res,
535 int cache_reply)
536{
537 int ret;
538 struct rpc_task *task;
539 struct nfs41_call_sync_data data = {
540 .clp = clp,
541 .seq_args = args,
542 .seq_res = res,
543 .cache_reply = cache_reply,
544 };
545 struct rpc_task_setup task_setup = {
546 .rpc_client = clnt,
547 .rpc_message = msg,
548 .callback_ops = &nfs41_call_sync_ops,
549 .callback_data = &data
550 };
551
552 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
553 task = rpc_run_task(&task_setup);
554 if (IS_ERR(task))
555 ret = PTR_ERR(task);
556 else {
557 ret = task->tk_status;
558 rpc_put_task(task);
559 }
560 return ret;
561}
562
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400563int _nfs4_call_sync_session(struct nfs_server *server,
564 struct rpc_message *msg,
565 struct nfs4_sequence_args *args,
566 struct nfs4_sequence_res *res,
567 int cache_reply)
568{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400569 return nfs4_call_sync_sequence(server->nfs_client, server->client,
570 msg, args, res, cache_reply);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400571}
572
573#endif /* CONFIG_NFS_V4_1 */
574
575int _nfs4_call_sync(struct nfs_server *server,
576 struct rpc_message *msg,
577 struct nfs4_sequence_args *args,
578 struct nfs4_sequence_res *res,
579 int cache_reply)
580{
Benny Halevyf3752972009-04-01 09:22:04 -0400581 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400582 return rpc_call_sync(server->client, msg, 0);
583}
584
585#define nfs4_call_sync(server, msg, args, res, cache_reply) \
586 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
587 &(res)->seq_res, (cache_reply))
588
Andy Adamsonb0df8062009-04-01 09:22:18 -0400589static void nfs4_sequence_done(const struct nfs_server *server,
590 struct nfs4_sequence_res *res, int rpc_status)
591{
592#ifdef CONFIG_NFS_V4_1
593 if (nfs4_has_session(server->nfs_client))
594 nfs41_sequence_done(server->nfs_client, res, rpc_status);
595#endif /* CONFIG_NFS_V4_1 */
596}
597
598/* no restart, therefore free slot here */
599static void nfs4_sequence_done_free_slot(const struct nfs_server *server,
600 struct nfs4_sequence_res *res,
601 int rpc_status)
602{
603 nfs4_sequence_done(server, res, rpc_status);
604 nfs4_sequence_free_slot(server->nfs_client, res);
605}
606
Trond Myklebust38478b22006-05-25 01:40:57 -0400607static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Trond Myklebust38478b22006-05-25 01:40:57 -0400609 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Trond Myklebust38478b22006-05-25 01:40:57 -0400611 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400612 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
613 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400614 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400615 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400616 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100619struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400620 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100621 struct nfs_openargs o_arg;
622 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100623 struct nfs_open_confirmargs c_arg;
624 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100625 struct nfs_fattr f_attr;
626 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400627 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100628 struct dentry *dir;
629 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400630 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100631 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100632 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400633 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100634 int rpc_status;
635 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100636};
637
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400638
639static void nfs4_init_opendata_res(struct nfs4_opendata *p)
640{
641 p->o_res.f_attr = &p->f_attr;
642 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400643 p->o_res.seqid = p->o_arg.seqid;
644 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400645 p->o_res.server = p->o_arg.server;
646 nfs_fattr_init(&p->f_attr);
647 nfs_fattr_init(&p->dir_attr);
648}
649
Trond Myklebustad389da2007-06-05 12:30:00 -0400650static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500651 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100652 const struct iattr *attrs)
653{
Trond Myklebustad389da2007-06-05 12:30:00 -0400654 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100655 struct inode *dir = parent->d_inode;
656 struct nfs_server *server = NFS_SERVER(dir);
657 struct nfs4_opendata *p;
658
659 p = kzalloc(sizeof(*p), GFP_KERNEL);
660 if (p == NULL)
661 goto err;
662 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
663 if (p->o_arg.seqid == NULL)
664 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400665 p->path.mnt = mntget(path->mnt);
666 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100667 p->dir = parent;
668 p->owner = sp;
669 atomic_inc(&sp->so_count);
670 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500671 p->o_arg.open_flags = flags;
672 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400673 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400674 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400675 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100676 p->o_arg.server = server;
677 p->o_arg.bitmask = server->attr_bitmask;
678 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Andy Adamson5f7dbd52009-04-01 09:22:05 -0400679 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100680 if (flags & O_EXCL) {
681 u32 *s = (u32 *) p->o_arg.u.verifier.data;
682 s[0] = jiffies;
683 s[1] = current->pid;
684 } else if (flags & O_CREAT) {
685 p->o_arg.u.attrs = &p->attrs;
686 memcpy(&p->attrs, attrs, sizeof(p->attrs));
687 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100688 p->c_arg.fh = &p->o_res.fh;
689 p->c_arg.stateid = &p->o_res.stateid;
690 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400691 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400692 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100693 return p;
694err_free:
695 kfree(p);
696err:
697 dput(parent);
698 return NULL;
699}
700
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400701static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100702{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400703 struct nfs4_opendata *p = container_of(kref,
704 struct nfs4_opendata, kref);
705
706 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400707 if (p->state != NULL)
708 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400709 nfs4_put_state_owner(p->owner);
710 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700711 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400712 kfree(p);
713}
714
715static void nfs4_opendata_put(struct nfs4_opendata *p)
716{
717 if (p != NULL)
718 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100719}
720
Trond Myklebust06f814a2006-01-03 09:55:07 +0100721static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
722{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100723 int ret;
724
Trond Myklebust06f814a2006-01-03 09:55:07 +0100725 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100726 return ret;
727}
728
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500729static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400730{
731 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500732
733 if (open_mode & O_EXCL)
734 goto out;
735 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400736 case FMODE_READ:
737 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400738 break;
739 case FMODE_WRITE:
740 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400741 break;
742 case FMODE_READ|FMODE_WRITE:
743 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
744 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500745out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400746 return ret;
747}
748
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500749static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400750{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500751 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400752 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500753 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400754 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500755 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400756 return 1;
757}
758
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500759static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100760{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500761 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100762 case FMODE_WRITE:
763 state->n_wronly++;
764 break;
765 case FMODE_READ:
766 state->n_rdonly++;
767 break;
768 case FMODE_READ|FMODE_WRITE:
769 state->n_rdwr++;
770 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500771 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100772}
773
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500774static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400775{
776 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
777 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
778 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500779 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400780 case FMODE_READ:
781 set_bit(NFS_O_RDONLY_STATE, &state->flags);
782 break;
783 case FMODE_WRITE:
784 set_bit(NFS_O_WRONLY_STATE, &state->flags);
785 break;
786 case FMODE_READ|FMODE_WRITE:
787 set_bit(NFS_O_RDWR_STATE, &state->flags);
788 }
789}
790
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500791static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400792{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400793 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500794 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400795 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400796}
797
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500798static 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 -0700799{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400800 /*
801 * Protect the call to nfs4_state_set_mode_locked and
802 * serialise the stateid update
803 */
804 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400805 if (deleg_stateid != NULL) {
806 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
807 set_bit(NFS_DELEGATED_STATE, &state->flags);
808 }
809 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500810 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400811 write_sequnlock(&state->seqlock);
812 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500813 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700814 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500817static 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 -0500818{
819 struct nfs_inode *nfsi = NFS_I(state->inode);
820 struct nfs_delegation *deleg_cur;
821 int ret = 0;
822
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500823 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500824
825 rcu_read_lock();
826 deleg_cur = rcu_dereference(nfsi->delegation);
827 if (deleg_cur == NULL)
828 goto no_delegation;
829
830 spin_lock(&deleg_cur->lock);
831 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500832 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500833 goto no_delegation_unlock;
834
835 if (delegation == NULL)
836 delegation = &deleg_cur->stateid;
837 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
838 goto no_delegation_unlock;
839
Trond Myklebustb7391f42008-12-23 15:21:52 -0500840 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500841 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500842 ret = 1;
843no_delegation_unlock:
844 spin_unlock(&deleg_cur->lock);
845no_delegation:
846 rcu_read_unlock();
847
848 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500849 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500850 ret = 1;
851 }
852
853 return ret;
854}
855
856
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500857static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400858{
859 struct nfs_delegation *delegation;
860
861 rcu_read_lock();
862 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500863 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400864 rcu_read_unlock();
865 return;
866 }
867 rcu_read_unlock();
868 nfs_inode_return_delegation(inode);
869}
870
Trond Myklebust6ee41262007-07-08 14:11:36 -0400871static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400872{
873 struct nfs4_state *state = opendata->state;
874 struct nfs_inode *nfsi = NFS_I(state->inode);
875 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500876 int open_mode = opendata->o_arg.open_flags & O_EXCL;
877 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400878 nfs4_stateid stateid;
879 int ret = -EAGAIN;
880
Trond Myklebustaac00a82007-07-05 19:02:21 -0400881 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500882 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400883 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500884 if (can_open_cached(state, fmode, open_mode)) {
885 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400886 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400887 goto out_return_state;
888 }
889 spin_unlock(&state->owner->so_lock);
890 }
Trond Myklebust34310432008-12-23 15:21:38 -0500891 rcu_read_lock();
892 delegation = rcu_dereference(nfsi->delegation);
893 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500894 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500895 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400896 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500897 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400898 /* Save the delegation */
899 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
900 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400901 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400902 if (ret != 0)
903 goto out;
904 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500905
906 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500907 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500908 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400909 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400910out:
911 return ERR_PTR(ret);
912out_return_state:
913 atomic_inc(&state->count);
914 return state;
915}
916
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100917static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
918{
919 struct inode *inode;
920 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400921 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400922 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100923
Trond Myklebustaac00a82007-07-05 19:02:21 -0400924 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400925 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400926 goto out;
927 }
928
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400929 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100930 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400931 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100932 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400933 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500934 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400935 goto err;
936 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100937 state = nfs4_get_open_state(inode, data->owner);
938 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400939 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400940 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400941 int delegation_flags = 0;
942
Trond Myklebust003707c2007-07-05 18:07:55 -0400943 rcu_read_lock();
944 delegation = rcu_dereference(NFS_I(inode)->delegation);
945 if (delegation)
946 delegation_flags = delegation->flags;
947 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -0500948 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400949 nfs_inode_set_delegation(state->inode,
950 data->owner->so_cred,
951 &data->o_res);
952 else
953 nfs_inode_reclaim_delegation(state->inode,
954 data->owner->so_cred,
955 &data->o_res);
956 }
Trond Myklebust34310432008-12-23 15:21:38 -0500957
958 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100960 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400961out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100962 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400963err_put_inode:
964 iput(inode);
965err:
966 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100967}
968
Trond Myklebust864472e2006-01-03 09:55:15 +0100969static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
970{
971 struct nfs_inode *nfsi = NFS_I(state->inode);
972 struct nfs_open_context *ctx;
973
974 spin_lock(&state->inode->i_lock);
975 list_for_each_entry(ctx, &nfsi->open_files, list) {
976 if (ctx->state != state)
977 continue;
978 get_nfs_open_context(ctx);
979 spin_unlock(&state->inode->i_lock);
980 return ctx;
981 }
982 spin_unlock(&state->inode->i_lock);
983 return ERR_PTR(-ENOENT);
984}
985
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400986static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
987{
988 struct nfs4_opendata *opendata;
989
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500990 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400991 if (opendata == NULL)
992 return ERR_PTR(-ENOMEM);
993 opendata->state = state;
994 atomic_inc(&state->count);
995 return opendata;
996}
997
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +0100999{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001000 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001001 int ret;
1002
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001003 opendata->o_arg.open_flags = 0;
1004 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001005 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1006 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1007 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001008 ret = _nfs4_proc_open(opendata);
1009 if (ret != 0)
1010 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001011 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001012 if (IS_ERR(newstate))
1013 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001014 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001015 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001016 return 0;
1017}
1018
1019static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1020{
Trond Myklebust864472e2006-01-03 09:55:15 +01001021 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001022 int ret;
1023
1024 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001025 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001026 smp_rmb();
1027 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001028 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001029 if (ret != 0)
1030 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001031 if (newstate != state)
1032 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001033 }
1034 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001035 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001036 if (ret != 0)
1037 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001038 if (newstate != state)
1039 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001040 }
1041 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001042 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001043 if (ret != 0)
1044 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001045 if (newstate != state)
1046 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001047 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001048 /*
1049 * We may have performed cached opens for all three recoveries.
1050 * Check if we need to update the current stateid.
1051 */
1052 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1053 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001054 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001055 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1056 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001057 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001058 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001059 return 0;
1060}
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062/*
1063 * OPEN_RECLAIM:
1064 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001066static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001068 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001069 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001070 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 int status;
1072
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001073 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1074 if (IS_ERR(opendata))
1075 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001076 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1077 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001078 rcu_read_lock();
1079 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001080 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001081 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001082 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001083 opendata->o_arg.u.delegation_type = delegation_type;
1084 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001085 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return status;
1087}
1088
Trond Myklebust539cd032007-06-05 11:46:42 -04001089static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 struct nfs_server *server = NFS_SERVER(state->inode);
1092 struct nfs4_exception exception = { };
1093 int err;
1094 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001095 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001096 if (err != -NFS4ERR_DELAY)
1097 break;
1098 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 } while (exception.retry);
1100 return err;
1101}
1102
Trond Myklebust864472e2006-01-03 09:55:15 +01001103static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1104{
1105 struct nfs_open_context *ctx;
1106 int ret;
1107
1108 ctx = nfs4_state_find_open_context(state);
1109 if (IS_ERR(ctx))
1110 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001111 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001112 put_nfs_open_context(ctx);
1113 return ret;
1114}
1115
Trond Myklebust13437e12007-07-06 15:10:43 -04001116static 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 -07001117{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001118 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001119 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001121 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1122 if (IS_ERR(opendata))
1123 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001124 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001125 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001126 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001127 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001128 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001129 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
Trond Myklebust13437e12007-07-06 15:10:43 -04001132int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
1134 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001135 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 int err;
1137 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001138 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 switch (err) {
1140 case 0:
1141 return err;
1142 case -NFS4ERR_STALE_CLIENTID:
1143 case -NFS4ERR_STALE_STATEID:
1144 case -NFS4ERR_EXPIRED:
1145 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001146 nfs4_schedule_state_recovery(server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return err;
1148 }
1149 err = nfs4_handle_exception(server, err, &exception);
1150 } while (exception.retry);
1151 return err;
1152}
1153
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001154static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1155{
1156 struct nfs4_opendata *data = calldata;
1157
1158 data->rpc_status = task->tk_status;
1159 if (RPC_ASSASSINATED(task))
1160 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001161 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001162 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1163 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001164 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001165 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001166 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001167 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001168}
1169
1170static void nfs4_open_confirm_release(void *calldata)
1171{
1172 struct nfs4_opendata *data = calldata;
1173 struct nfs4_state *state = NULL;
1174
1175 /* If this request hasn't been cancelled, do nothing */
1176 if (data->cancelled == 0)
1177 goto out_free;
1178 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001179 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001180 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001181 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001182 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001183 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001184out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001185 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001186}
1187
1188static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001189 .rpc_call_done = nfs4_open_confirm_done,
1190 .rpc_release = nfs4_open_confirm_release,
1191};
1192
1193/*
1194 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1195 */
1196static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1197{
1198 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1199 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001200 struct rpc_message msg = {
1201 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1202 .rpc_argp = &data->c_arg,
1203 .rpc_resp = &data->c_res,
1204 .rpc_cred = data->owner->so_cred,
1205 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001206 struct rpc_task_setup task_setup_data = {
1207 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001208 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001209 .callback_ops = &nfs4_open_confirm_ops,
1210 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001211 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001212 .flags = RPC_TASK_ASYNC,
1213 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 int status;
1215
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001216 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001217 data->rpc_done = 0;
1218 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001219 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001220 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001221 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001222 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001223 status = nfs4_wait_for_completion_rpc_task(task);
1224 if (status != 0) {
1225 data->cancelled = 1;
1226 smp_wmb();
1227 } else
1228 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001229 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return status;
1231}
1232
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001233static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001235 struct nfs4_opendata *data = calldata;
1236 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001237
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001238 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1239 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001240 /*
1241 * Check if we still need to send an OPEN call, or if we can use
1242 * a delegation instead.
1243 */
1244 if (data->state != NULL) {
1245 struct nfs_delegation *delegation;
1246
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001247 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001248 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001249 rcu_read_lock();
1250 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1251 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001252 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001253 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001254 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001255 }
1256 rcu_read_unlock();
1257 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001258 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001259 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001260 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001261 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001262 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001263 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1264 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001265 data->timestamp = jiffies;
Andy Adamsond8985282009-04-01 09:22:21 -04001266 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1267 &data->o_arg.seq_args,
1268 &data->o_res.seq_res, 1, task))
1269 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001270 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001271 return;
1272out_no_action:
1273 task->tk_action = NULL;
1274
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001275}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001277static void nfs4_open_done(struct rpc_task *task, void *calldata)
1278{
1279 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001281 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001282
1283 nfs4_sequence_done_free_slot(data->o_arg.server, &data->o_res.seq_res,
1284 task->tk_status);
1285
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001286 if (RPC_ASSASSINATED(task))
1287 return;
1288 if (task->tk_status == 0) {
1289 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001290 case S_IFREG:
1291 break;
1292 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001293 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001294 break;
1295 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001296 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001297 break;
1298 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001299 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001300 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001301 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001302 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1303 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001304 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001305 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001306}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001307
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001308static void nfs4_open_release(void *calldata)
1309{
1310 struct nfs4_opendata *data = calldata;
1311 struct nfs4_state *state = NULL;
1312
1313 /* If this request hasn't been cancelled, do nothing */
1314 if (data->cancelled == 0)
1315 goto out_free;
1316 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001317 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001318 goto out_free;
1319 /* In case we need an open_confirm, no cleanup! */
1320 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1321 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001322 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001323 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001324 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001325out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001326 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001327}
1328
1329static const struct rpc_call_ops nfs4_open_ops = {
1330 .rpc_call_prepare = nfs4_open_prepare,
1331 .rpc_call_done = nfs4_open_done,
1332 .rpc_release = nfs4_open_release,
1333};
1334
1335/*
1336 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1337 */
1338static int _nfs4_proc_open(struct nfs4_opendata *data)
1339{
1340 struct inode *dir = data->dir->d_inode;
1341 struct nfs_server *server = NFS_SERVER(dir);
1342 struct nfs_openargs *o_arg = &data->o_arg;
1343 struct nfs_openres *o_res = &data->o_res;
1344 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001345 struct rpc_message msg = {
1346 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1347 .rpc_argp = o_arg,
1348 .rpc_resp = o_res,
1349 .rpc_cred = data->owner->so_cred,
1350 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001351 struct rpc_task_setup task_setup_data = {
1352 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001353 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001354 .callback_ops = &nfs4_open_ops,
1355 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001356 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001357 .flags = RPC_TASK_ASYNC,
1358 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001359 int status;
1360
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001361 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001362 data->rpc_done = 0;
1363 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001364 data->cancelled = 0;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001365 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001366 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001367 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001368 status = nfs4_wait_for_completion_rpc_task(task);
1369 if (status != 0) {
1370 data->cancelled = 1;
1371 smp_wmb();
1372 } else
1373 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001374 rpc_put_task(task);
Trond Myklebust3e309912007-07-07 13:19:59 -04001375 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001376 return status;
1377
Trond Myklebust99367812007-07-17 21:52:41 -04001378 if (o_res->fh.size == 0)
1379 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1380
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001381 if (o_arg->open_flags & O_CREAT) {
1382 update_changeattr(dir, &o_res->cinfo);
1383 nfs_post_op_update_inode(dir, o_res->dir_attr);
1384 } else
1385 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001387 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001389 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001392 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001393 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395
Trond Myklebust10afec92007-05-14 17:16:04 -04001396static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001397{
David Howells7539bba2006-08-22 20:06:09 -04001398 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6b309542006-09-14 14:03:14 -04001399 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001400
Trond Myklebust6b309542006-09-14 14:03:14 -04001401 for (;;) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001402 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001403 if (ret != 0)
1404 return ret;
Trond Myklebuste598d842008-12-23 15:21:42 -05001405 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1406 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001407 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001408 nfs4_schedule_state_recovery(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001409 }
1410 return 0;
Trond Myklebust58d97142006-01-03 09:55:24 +01001411}
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413/*
1414 * OPEN_EXPIRED:
1415 * reclaim state on the server after a network partition.
1416 * Assumes caller holds the appropriate lock
1417 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001418static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001420 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001421 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001423 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1424 if (IS_ERR(opendata))
1425 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001426 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001427 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001428 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001429 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001430 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
Trond Myklebust539cd032007-06-05 11:46:42 -04001433static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001434{
Trond Myklebust539cd032007-06-05 11:46:42 -04001435 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001436 struct nfs4_exception exception = { };
1437 int err;
1438
1439 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001440 err = _nfs4_open_expired(ctx, state);
Trond Myklebust027b6ca2008-12-23 16:04:13 -05001441 if (err != -NFS4ERR_DELAY)
1442 break;
1443 nfs4_handle_exception(server, err, &exception);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001444 } while (exception.retry);
1445 return err;
1446}
1447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1449{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001451 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Trond Myklebust864472e2006-01-03 09:55:15 +01001453 ctx = nfs4_state_find_open_context(state);
1454 if (IS_ERR(ctx))
1455 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001456 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001457 put_nfs_open_context(ctx);
1458 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
1461/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001462 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1463 * fields corresponding to attributes that were used to store the verifier.
1464 * Make sure we clobber those fields in the later setattr call
1465 */
1466static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1467{
1468 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1469 !(sattr->ia_valid & ATTR_ATIME_SET))
1470 sattr->ia_valid |= ATTR_ATIME;
1471
1472 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1473 !(sattr->ia_valid & ATTR_MTIME_SET))
1474 sattr->ia_valid |= ATTR_MTIME;
1475}
1476
1477/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001478 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001480static 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 -07001481{
1482 struct nfs4_state_owner *sp;
1483 struct nfs4_state *state = NULL;
1484 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001485 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001486 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 status = -ENOMEM;
1490 if (!(sp = nfs4_get_state_owner(server, cred))) {
1491 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1492 goto out_err;
1493 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001494 status = nfs4_recover_expired_lease(server);
1495 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001496 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001497 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001498 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001499 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001500 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001501 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001502 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Trond Myklebustaac00a82007-07-05 19:02:21 -04001504 if (path->dentry->d_inode != NULL)
1505 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1506
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001507 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001509 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001511 if (opendata->o_arg.open_flags & O_EXCL)
1512 nfs4_exclusive_attrset(opendata, sattr);
1513
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001514 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001515 status = PTR_ERR(state);
1516 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001517 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001518 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 *res = state;
1521 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001522err_opendata_put:
1523 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001524err_put_state_owner:
1525 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 *res = NULL;
1528 return status;
1529}
1530
1531
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001532static 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 -07001533{
1534 struct nfs4_exception exception = { };
1535 struct nfs4_state *res;
1536 int status;
1537
1538 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001539 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (status == 0)
1541 break;
1542 /* NOTE: BAD_SEQID means the server and client disagree about the
1543 * book-keeping w.r.t. state-changing operations
1544 * (OPEN/CLOSE/LOCK/LOCKU...)
1545 * It is actually a sign of a bug on the client or on the server.
1546 *
1547 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001548 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 * have unhashed the old state_owner for us, and that we can
1550 * therefore safely retry using a new one. We should still warn
1551 * the user though...
1552 */
1553 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001554 printk(KERN_WARNING "NFS: v4 server %s "
1555 " returned a bad sequence-id error!\n",
1556 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 exception.retry = 1;
1558 continue;
1559 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001560 /*
1561 * BAD_STATEID on OPEN means that the server cancelled our
1562 * state before it received the OPEN_CONFIRM.
1563 * Recover by retrying the request as per the discussion
1564 * on Page 181 of RFC3530.
1565 */
1566 if (status == -NFS4ERR_BAD_STATEID) {
1567 exception.retry = 1;
1568 continue;
1569 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001570 if (status == -EAGAIN) {
1571 /* We must have found a delegation */
1572 exception.retry = 1;
1573 continue;
1574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1576 status, &exception));
1577 } while (exception.retry);
1578 return res;
1579}
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 nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001587 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 .iap = sattr,
1589 .server = server,
1590 .bitmask = server->attr_bitmask,
1591 };
1592 struct nfs_setattrres res = {
1593 .fattr = fattr,
1594 .server = server,
1595 };
1596 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1598 .rpc_argp = &arg,
1599 .rpc_resp = &res,
1600 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001602 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001603 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Trond Myklebust0e574af2005-10-27 22:12:38 -04001605 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001607 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1608 /* Use that stateid */
1609 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001610 nfs4_copy_stateid(&arg.stateid, state, current->files);
1611 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1613
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001614 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001615 if (status == 0 && state != NULL)
1616 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001617 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001620static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1621 struct nfs_fattr *fattr, struct iattr *sattr,
1622 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001624 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 struct nfs4_exception exception = { };
1626 int err;
1627 do {
1628 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001629 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 &exception);
1631 } while (exception.retry);
1632 return err;
1633}
1634
1635struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001636 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 struct inode *inode;
1638 struct nfs4_state *state;
1639 struct nfs_closeargs arg;
1640 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001641 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001642 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643};
1644
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001645static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001646{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001647 struct nfs4_closedata *calldata = data;
1648 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001649
1650 nfs4_put_open_state(calldata->state);
1651 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001652 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001653 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001654 kfree(calldata);
1655}
1656
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001657static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001659 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 struct nfs_server *server = NFS_SERVER(calldata->inode);
1662
Andy Adamson19ddab02009-04-01 09:22:20 -04001663 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001664 if (RPC_ASSASSINATED(task))
1665 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 /* hmm. we are done with the inode, and in the process of freeing
1667 * the state_owner. we keep this around to process errors
1668 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 switch (task->tk_status) {
1670 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001671 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001672 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 break;
1674 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001675 case -NFS4ERR_OLD_STATEID:
1676 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001678 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001679 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001681 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Andy Adamsoneedc0202009-04-01 09:22:41 -04001682 nfs4_restart_rpc(task, server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return;
1684 }
1685 }
Andy Adamson19ddab02009-04-01 09:22:20 -04001686 nfs4_sequence_free_slot(server->nfs_client, &calldata->res.seq_res);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001687 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001690static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001692 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001693 struct nfs4_state *state = calldata->state;
Trond Myklebust003707c2007-07-05 18:07:55 -04001694 int clear_rd, clear_wr, clear_rdwr;
Trond Myklebust95121352005-10-18 14:20:12 -07001695
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001696 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001697 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001698
Trond Myklebust003707c2007-07-05 18:07:55 -04001699 clear_rd = clear_wr = clear_rdwr = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001700 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001701 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001702 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001703 if (state->n_rdonly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001704 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1705 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1706 }
1707 if (state->n_wronly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001708 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1709 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1710 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001711 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001712 spin_unlock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001713 if (!clear_rd && !clear_wr && !clear_rdwr) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001714 /* Note: exit _without_ calling nfs4_close_done */
1715 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001716 return;
1717 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001718 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust45328c32007-07-26 17:47:34 -04001719 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001720 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001721 calldata->arg.fmode = FMODE_READ;
Trond Myklebust45328c32007-07-26 17:47:34 -04001722 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001723 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001724 calldata->arg.fmode = FMODE_WRITE;
Trond Myklebust45328c32007-07-26 17:47:34 -04001725 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001726 calldata->timestamp = jiffies;
Andy Adamson19ddab02009-04-01 09:22:20 -04001727 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1728 &calldata->arg.seq_args, &calldata->res.seq_res,
1729 1, task))
1730 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001731 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
1733
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001734static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001735 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001736 .rpc_call_done = nfs4_close_done,
1737 .rpc_release = nfs4_free_closedata,
1738};
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740/*
1741 * It is possible for data to be read/written from a mem-mapped file
1742 * after the sys_close call (which hits the vfs layer as a flush).
1743 * This means that we can't safely call nfsv4 close on a file until
1744 * the inode is cleared. This in turn means that we are not good
1745 * NFSv4 citizens - we do not indicate to the server to update the file's
1746 * share state even when we are done with one of the three share
1747 * stateid's in the inode.
1748 *
1749 * NOTE: Caller must be holding the sp->so_owner semaphore!
1750 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001751int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001753 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001755 struct nfs4_state_owner *sp = state->owner;
1756 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001757 struct rpc_message msg = {
1758 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1759 .rpc_cred = state->owner->so_cred,
1760 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001761 struct rpc_task_setup task_setup_data = {
1762 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001763 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001764 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001765 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001766 .flags = RPC_TASK_ASYNC,
1767 };
Trond Myklebust95121352005-10-18 14:20:12 -07001768 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Andy Adamson19ddab02009-04-01 09:22:20 -04001770 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001772 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001773 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001775 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001776 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001778 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001779 if (calldata->arg.seqid == NULL)
1780 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001781 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001782 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001783 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001784 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001785 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001786 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001787 calldata->path.mnt = mntget(path->mnt);
1788 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001789
Trond Myklebust5138fde2007-07-14 15:40:01 -04001790 msg.rpc_argp = &calldata->arg,
1791 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001792 task_setup_data.callback_data = calldata;
1793 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001794 if (IS_ERR(task))
1795 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001796 status = 0;
1797 if (wait)
1798 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001799 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001800 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001801out_free_calldata:
1802 kfree(calldata);
1803out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001804 nfs4_put_open_state(state);
1805 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001806 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001809static 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 -07001810{
1811 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001812 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001813
Trond Myklebust1b45c462007-07-03 13:04:56 -04001814 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001815 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001816 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001817 state->owner->so_cred,
1818 nd->intent.open.flags);
1819 if (ret < 0)
1820 goto out_close;
1821 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001822 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001823 if (!IS_ERR(filp)) {
1824 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001825 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001826 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001827 return 0;
1828 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001829 ret = PTR_ERR(filp);
1830out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001831 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001832 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001833}
1834
1835struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1837{
Trond Myklebustad389da2007-06-05 12:30:00 -04001838 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001839 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001840 .dentry = dentry,
1841 };
Jan Blunck4ac91372008-02-14 19:34:32 -08001842 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 struct iattr attr;
1844 struct rpc_cred *cred;
1845 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001846 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001847 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 if (nd->flags & LOOKUP_CREATE) {
1850 attr.ia_mode = nd->intent.open.create_mode;
1851 attr.ia_valid = ATTR_MODE;
1852 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04001853 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 } else {
1855 attr.ia_valid = 0;
1856 BUG_ON(nd->intent.open.flags & O_CREAT);
1857 }
1858
Trond Myklebust98a8e322008-03-12 12:25:28 -04001859 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001861 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04001862 parent = dentry->d_parent;
1863 /* Protect against concurrent sillydeletes */
1864 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001865 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001867 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001868 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001869 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04001870 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1871 }
Trond Myklebust565277f2007-10-15 18:17:53 -04001872 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001873 return (struct dentry *)state;
1874 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001875 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001876 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04001877 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04001878 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001879 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001880 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001881 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883
1884int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001885nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Trond Myklebustad389da2007-06-05 12:30:00 -04001887 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001888 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001889 .dentry = dentry,
1890 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 struct rpc_cred *cred;
1892 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001893 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Trond Myklebust98a8e322008-03-12 12:25:28 -04001895 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if (IS_ERR(cred))
1897 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001898 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001900 if (IS_ERR(state)) {
1901 switch (PTR_ERR(state)) {
1902 case -EPERM:
1903 case -EACCES:
1904 case -EDQUOT:
1905 case -ENOSPC:
1906 case -EROFS:
1907 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1908 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001909 default:
1910 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001911 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001912 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001913 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001914 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001915 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return 1;
1917 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001918 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001919out_drop:
1920 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 return 0;
1922}
1923
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001924void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1925{
1926 if (ctx->state == NULL)
1927 return;
1928 if (is_sync)
1929 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
1930 else
1931 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
1932}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1935{
Benny Halevy43652ad2009-04-01 09:21:54 -04001936 struct nfs4_server_caps_arg args = {
1937 .fhandle = fhandle,
1938 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 struct nfs4_server_caps_res res = {};
1940 struct rpc_message msg = {
1941 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04001942 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 .rpc_resp = &res,
1944 };
1945 int status;
1946
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001947 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (status == 0) {
1949 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1950 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1951 server->caps |= NFS_CAP_ACLS;
1952 if (res.has_links != 0)
1953 server->caps |= NFS_CAP_HARDLINKS;
1954 if (res.has_symlinks != 0)
1955 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001956 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
1957 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
1958 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 server->acl_bitmask = res.acl_bitmask;
1960 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return status;
1963}
1964
Trond Myklebust55a97592006-06-09 09:34:19 -04001965int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
1967 struct nfs4_exception exception = { };
1968 int err;
1969 do {
1970 err = nfs4_handle_exception(server,
1971 _nfs4_server_capabilities(server, fhandle),
1972 &exception);
1973 } while (exception.retry);
1974 return err;
1975}
1976
1977static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1978 struct nfs_fsinfo *info)
1979{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 struct nfs4_lookup_root_arg args = {
1981 .bitmask = nfs4_fattr_bitmap,
1982 };
1983 struct nfs4_lookup_res res = {
1984 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001985 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 .fh = fhandle,
1987 };
1988 struct rpc_message msg = {
1989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1990 .rpc_argp = &args,
1991 .rpc_resp = &res,
1992 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001993 nfs_fattr_init(info->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001994 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995}
1996
1997static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1998 struct nfs_fsinfo *info)
1999{
2000 struct nfs4_exception exception = { };
2001 int err;
2002 do {
2003 err = nfs4_handle_exception(server,
2004 _nfs4_lookup_root(server, fhandle, info),
2005 &exception);
2006 } while (exception.retry);
2007 return err;
2008}
2009
David Howells54ceac42006-08-22 20:06:13 -04002010/*
2011 * get the file handle for the "/" directory on the server
2012 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002014 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 int status;
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (status == 0)
2020 status = nfs4_server_capabilities(server, fhandle);
2021 if (status == 0)
2022 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002023 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
Manoj Naik6b97fd32006-06-09 09:34:29 -04002026/*
2027 * Get locations and (maybe) other attributes of a referral.
2028 * Note that we'll actually follow the referral later when
2029 * we detect fsid mismatch in inode revalidation
2030 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002031static 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 -04002032{
2033 int status = -ENOMEM;
2034 struct page *page = NULL;
2035 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002036
2037 page = alloc_page(GFP_KERNEL);
2038 if (page == NULL)
2039 goto out;
2040 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2041 if (locations == NULL)
2042 goto out;
2043
Trond Myklebustc228fd32007-01-13 02:28:11 -05002044 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002045 if (status != 0)
2046 goto out;
2047 /* Make sure server returned a different fsid for the referral */
2048 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002049 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 -04002050 status = -EIO;
2051 goto out;
2052 }
2053
2054 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2055 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2056 if (!fattr->mode)
2057 fattr->mode = S_IFDIR;
2058 memset(fhandle, 0, sizeof(struct nfs_fh));
2059out:
2060 if (page)
2061 __free_page(page);
2062 if (locations)
2063 kfree(locations);
2064 return status;
2065}
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2068{
2069 struct nfs4_getattr_arg args = {
2070 .fh = fhandle,
2071 .bitmask = server->attr_bitmask,
2072 };
2073 struct nfs4_getattr_res res = {
2074 .fattr = fattr,
2075 .server = server,
2076 };
2077 struct rpc_message msg = {
2078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2079 .rpc_argp = &args,
2080 .rpc_resp = &res,
2081 };
2082
Trond Myklebust0e574af2005-10-27 22:12:38 -04002083 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002084 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
2086
2087static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2088{
2089 struct nfs4_exception exception = { };
2090 int err;
2091 do {
2092 err = nfs4_handle_exception(server,
2093 _nfs4_proc_getattr(server, fhandle, fattr),
2094 &exception);
2095 } while (exception.retry);
2096 return err;
2097}
2098
2099/*
2100 * The file is not closed if it is opened due to the a request to change
2101 * the size of the file. The open call will not be needed once the
2102 * VFS layer lookup-intents are implemented.
2103 *
2104 * Close is called when the inode is destroyed.
2105 * If we haven't opened the file for O_WRONLY, we
2106 * need to in the size_change case to obtain a stateid.
2107 *
2108 * Got race?
2109 * Because OPEN is always done by name in nfsv4, it is
2110 * possible that we opened a different file by the same
2111 * name. We can recognize this race condition, but we
2112 * can't do anything about it besides returning an error.
2113 *
2114 * This will be fixed with VFS changes (lookup-intent).
2115 */
2116static int
2117nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2118 struct iattr *sattr)
2119{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002120 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002121 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002122 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 int status;
2124
Trond Myklebust0e574af2005-10-27 22:12:38 -04002125 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Trond Myklebustd5308382005-11-04 15:33:38 -05002127 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002128 if (sattr->ia_valid & ATTR_FILE) {
2129 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002130
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002131 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002132 if (ctx) {
2133 cred = ctx->cred;
2134 state = ctx->state;
2135 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002136 }
2137
2138 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002139 if (status == 0)
2140 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return status;
2142}
2143
Trond Myklebust56659e92007-07-17 21:52:39 -04002144static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2145 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002146 struct nfs_fattr *fattr)
2147{
2148 int status;
2149 struct nfs4_lookup_arg args = {
2150 .bitmask = server->attr_bitmask,
2151 .dir_fh = dirfh,
2152 .name = name,
2153 };
2154 struct nfs4_lookup_res res = {
2155 .server = server,
2156 .fattr = fattr,
2157 .fh = fhandle,
2158 };
2159 struct rpc_message msg = {
2160 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2161 .rpc_argp = &args,
2162 .rpc_resp = &res,
2163 };
2164
2165 nfs_fattr_init(fattr);
2166
2167 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002168 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002169 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002170 return status;
2171}
2172
2173static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2174 struct qstr *name, struct nfs_fh *fhandle,
2175 struct nfs_fattr *fattr)
2176{
2177 struct nfs4_exception exception = { };
2178 int err;
2179 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002180 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2181 /* FIXME: !!!! */
2182 if (err == -NFS4ERR_MOVED) {
2183 err = -EREMOTE;
2184 break;
2185 }
2186 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002187 } while (exception.retry);
2188 return err;
2189}
2190
Trond Myklebust56659e92007-07-17 21:52:39 -04002191static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2193{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002194 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002197 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002198 if (status == -NFS4ERR_MOVED)
2199 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 dprintk("NFS reply lookup: %d\n", status);
2201 return status;
2202}
2203
2204static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2205{
2206 struct nfs4_exception exception = { };
2207 int err;
2208 do {
2209 err = nfs4_handle_exception(NFS_SERVER(dir),
2210 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2211 &exception);
2212 } while (exception.retry);
2213 return err;
2214}
2215
2216static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2217{
Trond Myklebust76b32992007-08-10 17:45:11 -04002218 struct nfs_server *server = NFS_SERVER(inode);
2219 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 struct nfs4_accessargs args = {
2221 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002222 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002224 struct nfs4_accessres res = {
2225 .server = server,
2226 .fattr = &fattr,
2227 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 struct rpc_message msg = {
2229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2230 .rpc_argp = &args,
2231 .rpc_resp = &res,
2232 .rpc_cred = entry->cred,
2233 };
2234 int mode = entry->mask;
2235 int status;
2236
2237 /*
2238 * Determine which access bits we want to ask for...
2239 */
2240 if (mode & MAY_READ)
2241 args.access |= NFS4_ACCESS_READ;
2242 if (S_ISDIR(inode->i_mode)) {
2243 if (mode & MAY_WRITE)
2244 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2245 if (mode & MAY_EXEC)
2246 args.access |= NFS4_ACCESS_LOOKUP;
2247 } else {
2248 if (mode & MAY_WRITE)
2249 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2250 if (mode & MAY_EXEC)
2251 args.access |= NFS4_ACCESS_EXECUTE;
2252 }
Trond Myklebust76b32992007-08-10 17:45:11 -04002253 nfs_fattr_init(&fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002254 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 if (!status) {
2256 entry->mask = 0;
2257 if (res.access & NFS4_ACCESS_READ)
2258 entry->mask |= MAY_READ;
2259 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2260 entry->mask |= MAY_WRITE;
2261 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2262 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04002263 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 }
2265 return status;
2266}
2267
2268static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2269{
2270 struct nfs4_exception exception = { };
2271 int err;
2272 do {
2273 err = nfs4_handle_exception(NFS_SERVER(inode),
2274 _nfs4_proc_access(inode, entry),
2275 &exception);
2276 } while (exception.retry);
2277 return err;
2278}
2279
2280/*
2281 * TODO: For the time being, we don't try to get any attributes
2282 * along with any of the zero-copy operations READ, READDIR,
2283 * READLINK, WRITE.
2284 *
2285 * In the case of the first three, we want to put the GETATTR
2286 * after the read-type operation -- this is because it is hard
2287 * to predict the length of a GETATTR response in v4, and thus
2288 * align the READ data correctly. This means that the GETATTR
2289 * may end up partially falling into the page cache, and we should
2290 * shift it into the 'tail' of the xdr_buf before processing.
2291 * To do this efficiently, we need to know the total length
2292 * of data received, which doesn't seem to be available outside
2293 * of the RPC layer.
2294 *
2295 * In the case of WRITE, we also want to put the GETATTR after
2296 * the operation -- in this case because we want to make sure
2297 * we get the post-operation mtime and size. This means that
2298 * we can't use xdr_encode_pages() as written: we need a variant
2299 * of it which would leave room in the 'tail' iovec.
2300 *
2301 * Both of these changes to the XDR layer would in fact be quite
2302 * minor, but I decided to leave them for a subsequent patch.
2303 */
2304static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2305 unsigned int pgbase, unsigned int pglen)
2306{
2307 struct nfs4_readlink args = {
2308 .fh = NFS_FH(inode),
2309 .pgbase = pgbase,
2310 .pglen = pglen,
2311 .pages = &page,
2312 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002313 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 struct rpc_message msg = {
2315 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2316 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002317 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 };
2319
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002320 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321}
2322
2323static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2324 unsigned int pgbase, unsigned int pglen)
2325{
2326 struct nfs4_exception exception = { };
2327 int err;
2328 do {
2329 err = nfs4_handle_exception(NFS_SERVER(inode),
2330 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2331 &exception);
2332 } while (exception.retry);
2333 return err;
2334}
2335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336/*
2337 * Got race?
2338 * We will need to arrange for the VFS layer to provide an atomic open.
2339 * Until then, this create/open method is prone to inefficiency and race
2340 * conditions due to the lookup, create, and open VFS calls from sys_open()
2341 * placed on the wire.
2342 *
2343 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2344 * The file will be opened again in the subsequent VFS open call
2345 * (nfs4_proc_file_open).
2346 *
2347 * The open for read will just hang around to be used by any process that
2348 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2349 */
2350
2351static int
2352nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002353 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354{
Trond Myklebustad389da2007-06-05 12:30:00 -04002355 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002356 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002357 .dentry = dentry,
2358 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 struct nfs4_state *state;
2360 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002361 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 int status = 0;
2363
Trond Myklebust98a8e322008-03-12 12:25:28 -04002364 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (IS_ERR(cred)) {
2366 status = PTR_ERR(cred);
2367 goto out;
2368 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002369 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002370 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 if (IS_ERR(state)) {
2372 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002373 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002375 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002376 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 if (flags & O_EXCL) {
2378 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002379 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002380 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002381 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002382 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002383 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002384 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002385 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002386 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002387 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002388out_putcred:
2389 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390out:
2391 return status;
2392}
2393
2394static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2395{
Trond Myklebust16e42952005-10-27 22:12:44 -04002396 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002397 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002399 .name.len = name->len,
2400 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002401 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002403 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002404 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002405 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002407 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2408 .rpc_argp = &args,
2409 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 };
2411 int status;
2412
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002413 nfs_fattr_init(&res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002414 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002415 if (status == 0) {
2416 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002417 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 return status;
2420}
2421
2422static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2423{
2424 struct nfs4_exception exception = { };
2425 int err;
2426 do {
2427 err = nfs4_handle_exception(NFS_SERVER(dir),
2428 _nfs4_proc_remove(dir, name),
2429 &exception);
2430 } while (exception.retry);
2431 return err;
2432}
2433
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002434static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002436 struct nfs_server *server = NFS_SERVER(dir);
2437 struct nfs_removeargs *args = msg->rpc_argp;
2438 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Trond Myklebusta65318b2009-03-11 14:10:28 -04002440 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002441 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443}
2444
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002445static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002447 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2448
Andy Adamson472cfbd2009-04-01 09:22:24 -04002449 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002450 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002451 return 0;
Andy Adamson472cfbd2009-04-01 09:22:24 -04002452 nfs4_sequence_free_slot(res->server->nfs_client, &res->seq_res);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002453 update_changeattr(dir, &res->cinfo);
2454 nfs_post_op_update_inode(dir, &res->dir_attr);
2455 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
2457
2458static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2459 struct inode *new_dir, struct qstr *new_name)
2460{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002461 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 struct nfs4_rename_arg arg = {
2463 .old_dir = NFS_FH(old_dir),
2464 .new_dir = NFS_FH(new_dir),
2465 .old_name = old_name,
2466 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002467 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002469 struct nfs_fattr old_fattr, new_fattr;
2470 struct nfs4_rename_res res = {
2471 .server = server,
2472 .old_fattr = &old_fattr,
2473 .new_fattr = &new_fattr,
2474 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 struct rpc_message msg = {
2476 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2477 .rpc_argp = &arg,
2478 .rpc_resp = &res,
2479 };
2480 int status;
2481
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002482 nfs_fattr_init(res.old_fattr);
2483 nfs_fattr_init(res.new_fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002484 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
2486 if (!status) {
2487 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002488 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002490 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
2492 return status;
2493}
2494
2495static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2496 struct inode *new_dir, struct qstr *new_name)
2497{
2498 struct nfs4_exception exception = { };
2499 int err;
2500 do {
2501 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2502 _nfs4_proc_rename(old_dir, old_name,
2503 new_dir, new_name),
2504 &exception);
2505 } while (exception.retry);
2506 return err;
2507}
2508
2509static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2510{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002511 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 struct nfs4_link_arg arg = {
2513 .fh = NFS_FH(inode),
2514 .dir_fh = NFS_FH(dir),
2515 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002516 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002518 struct nfs_fattr fattr, dir_attr;
2519 struct nfs4_link_res res = {
2520 .server = server,
2521 .fattr = &fattr,
2522 .dir_attr = &dir_attr,
2523 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 struct rpc_message msg = {
2525 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2526 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002527 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 };
2529 int status;
2530
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002531 nfs_fattr_init(res.fattr);
2532 nfs_fattr_init(res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002533 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002534 if (!status) {
2535 update_changeattr(dir, &res.cinfo);
2536 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002537 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
2540 return status;
2541}
2542
2543static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2544{
2545 struct nfs4_exception exception = { };
2546 int err;
2547 do {
2548 err = nfs4_handle_exception(NFS_SERVER(inode),
2549 _nfs4_proc_link(inode, dir, name),
2550 &exception);
2551 } while (exception.retry);
2552 return err;
2553}
2554
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002555struct nfs4_createdata {
2556 struct rpc_message msg;
2557 struct nfs4_create_arg arg;
2558 struct nfs4_create_res res;
2559 struct nfs_fh fh;
2560 struct nfs_fattr fattr;
2561 struct nfs_fattr dir_fattr;
2562};
2563
2564static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2565 struct qstr *name, struct iattr *sattr, u32 ftype)
2566{
2567 struct nfs4_createdata *data;
2568
2569 data = kzalloc(sizeof(*data), GFP_KERNEL);
2570 if (data != NULL) {
2571 struct nfs_server *server = NFS_SERVER(dir);
2572
2573 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2574 data->msg.rpc_argp = &data->arg;
2575 data->msg.rpc_resp = &data->res;
2576 data->arg.dir_fh = NFS_FH(dir);
2577 data->arg.server = server;
2578 data->arg.name = name;
2579 data->arg.attrs = sattr;
2580 data->arg.ftype = ftype;
2581 data->arg.bitmask = server->attr_bitmask;
2582 data->res.server = server;
2583 data->res.fh = &data->fh;
2584 data->res.fattr = &data->fattr;
2585 data->res.dir_fattr = &data->dir_fattr;
2586 nfs_fattr_init(data->res.fattr);
2587 nfs_fattr_init(data->res.dir_fattr);
2588 }
2589 return data;
2590}
2591
2592static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2593{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002594 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2595 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002596 if (status == 0) {
2597 update_changeattr(dir, &data->res.dir_cinfo);
2598 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2599 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2600 }
2601 return status;
2602}
2603
2604static void nfs4_free_createdata(struct nfs4_createdata *data)
2605{
2606 kfree(data);
2607}
2608
Chuck Lever4f390c12006-08-22 20:06:22 -04002609static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002610 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002612 struct nfs4_createdata *data;
2613 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Chuck Lever94a6d752006-08-22 20:06:23 -04002615 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002616 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002617
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002618 status = -ENOMEM;
2619 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2620 if (data == NULL)
2621 goto out;
2622
2623 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2624 data->arg.u.symlink.pages = &page;
2625 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002627 status = nfs4_do_create(dir, dentry, data);
2628
2629 nfs4_free_createdata(data);
2630out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 return status;
2632}
2633
Chuck Lever4f390c12006-08-22 20:06:22 -04002634static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002635 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
2637 struct nfs4_exception exception = { };
2638 int err;
2639 do {
2640 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002641 _nfs4_proc_symlink(dir, dentry, page,
2642 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 &exception);
2644 } while (exception.retry);
2645 return err;
2646}
2647
2648static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2649 struct iattr *sattr)
2650{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002651 struct nfs4_createdata *data;
2652 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002654 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2655 if (data == NULL)
2656 goto out;
2657
2658 status = nfs4_do_create(dir, dentry, data);
2659
2660 nfs4_free_createdata(data);
2661out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 return status;
2663}
2664
2665static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2666 struct iattr *sattr)
2667{
2668 struct nfs4_exception exception = { };
2669 int err;
2670 do {
2671 err = nfs4_handle_exception(NFS_SERVER(dir),
2672 _nfs4_proc_mkdir(dir, dentry, sattr),
2673 &exception);
2674 } while (exception.retry);
2675 return err;
2676}
2677
2678static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2679 u64 cookie, struct page *page, unsigned int count, int plus)
2680{
2681 struct inode *dir = dentry->d_inode;
2682 struct nfs4_readdir_arg args = {
2683 .fh = NFS_FH(dir),
2684 .pages = &page,
2685 .pgbase = 0,
2686 .count = count,
Trond Myklebusta65318b2009-03-11 14:10:28 -04002687 .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 };
2689 struct nfs4_readdir_res res;
2690 struct rpc_message msg = {
2691 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2692 .rpc_argp = &args,
2693 .rpc_resp = &res,
2694 .rpc_cred = cred,
2695 };
2696 int status;
2697
Harvey Harrison3110ff82008-05-02 13:42:44 -07002698 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002699 dentry->d_parent->d_name.name,
2700 dentry->d_name.name,
2701 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2703 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002704 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 if (status == 0)
2706 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002707
2708 nfs_invalidate_atime(dir);
2709
Harvey Harrison3110ff82008-05-02 13:42:44 -07002710 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 return status;
2712}
2713
2714static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2715 u64 cookie, struct page *page, unsigned int count, int plus)
2716{
2717 struct nfs4_exception exception = { };
2718 int err;
2719 do {
2720 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2721 _nfs4_proc_readdir(dentry, cred, cookie,
2722 page, count, plus),
2723 &exception);
2724 } while (exception.retry);
2725 return err;
2726}
2727
2728static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2729 struct iattr *sattr, dev_t rdev)
2730{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002731 struct nfs4_createdata *data;
2732 int mode = sattr->ia_mode;
2733 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
2735 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2736 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002737
2738 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2739 if (data == NULL)
2740 goto out;
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002743 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002745 data->arg.ftype = NF4BLK;
2746 data->arg.u.device.specdata1 = MAJOR(rdev);
2747 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 }
2749 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002750 data->arg.ftype = NF4CHR;
2751 data->arg.u.device.specdata1 = MAJOR(rdev);
2752 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002755 status = nfs4_do_create(dir, dentry, data);
2756
2757 nfs4_free_createdata(data);
2758out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 return status;
2760}
2761
2762static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2763 struct iattr *sattr, dev_t rdev)
2764{
2765 struct nfs4_exception exception = { };
2766 int err;
2767 do {
2768 err = nfs4_handle_exception(NFS_SERVER(dir),
2769 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2770 &exception);
2771 } while (exception.retry);
2772 return err;
2773}
2774
2775static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2776 struct nfs_fsstat *fsstat)
2777{
2778 struct nfs4_statfs_arg args = {
2779 .fh = fhandle,
2780 .bitmask = server->attr_bitmask,
2781 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002782 struct nfs4_statfs_res res = {
2783 .fsstat = fsstat,
2784 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 struct rpc_message msg = {
2786 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2787 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002788 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 };
2790
Trond Myklebust0e574af2005-10-27 22:12:38 -04002791 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002792 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793}
2794
2795static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2796{
2797 struct nfs4_exception exception = { };
2798 int err;
2799 do {
2800 err = nfs4_handle_exception(server,
2801 _nfs4_proc_statfs(server, fhandle, fsstat),
2802 &exception);
2803 } while (exception.retry);
2804 return err;
2805}
2806
2807static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2808 struct nfs_fsinfo *fsinfo)
2809{
2810 struct nfs4_fsinfo_arg args = {
2811 .fh = fhandle,
2812 .bitmask = server->attr_bitmask,
2813 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002814 struct nfs4_fsinfo_res res = {
2815 .fsinfo = fsinfo,
2816 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 struct rpc_message msg = {
2818 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2819 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002820 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 };
2822
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002823 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824}
2825
2826static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2827{
2828 struct nfs4_exception exception = { };
2829 int err;
2830
2831 do {
2832 err = nfs4_handle_exception(server,
2833 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2834 &exception);
2835 } while (exception.retry);
2836 return err;
2837}
2838
2839static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2840{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002841 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2843}
2844
2845static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2846 struct nfs_pathconf *pathconf)
2847{
2848 struct nfs4_pathconf_arg args = {
2849 .fh = fhandle,
2850 .bitmask = server->attr_bitmask,
2851 };
Benny Halevyd45b2982009-04-01 09:21:58 -04002852 struct nfs4_pathconf_res res = {
2853 .pathconf = pathconf,
2854 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 struct rpc_message msg = {
2856 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2857 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04002858 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 };
2860
2861 /* None of the pathconf attributes are mandatory to implement */
2862 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2863 memset(pathconf, 0, sizeof(*pathconf));
2864 return 0;
2865 }
2866
Trond Myklebust0e574af2005-10-27 22:12:38 -04002867 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002868 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869}
2870
2871static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2872 struct nfs_pathconf *pathconf)
2873{
2874 struct nfs4_exception exception = { };
2875 int err;
2876
2877 do {
2878 err = nfs4_handle_exception(server,
2879 _nfs4_proc_pathconf(server, fhandle, pathconf),
2880 &exception);
2881 } while (exception.retry);
2882 return err;
2883}
2884
Trond Myklebustec06c092006-03-20 13:44:27 -05002885static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886{
Trond Myklebustec06c092006-03-20 13:44:27 -05002887 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Andy Adamsonf11c88a2009-04-01 09:22:25 -04002889 dprintk("--> %s\n", __func__);
2890
2891 /* nfs4_sequence_free_slot called in the read rpc_call_done */
2892 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
2893
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002894 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Andy Adamsoneedc0202009-04-01 09:22:41 -04002895 nfs4_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05002896 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 }
Trond Myklebust8850df92007-09-28 17:20:07 -04002898
2899 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002901 renew_lease(server, data->timestamp);
2902 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903}
2904
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002905static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002908 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909}
2910
Trond Myklebust788e7a82006-03-20 13:44:27 -05002911static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 struct inode *inode = data->inode;
2914
Andy Adamsondef6ed72009-04-01 09:22:26 -04002915 /* slot is freed in nfs_writeback_done */
2916 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
2917 task->tk_status);
2918
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002919 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Andy Adamsoneedc0202009-04-01 09:22:41 -04002920 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002921 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002923 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04002925 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002926 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05002927 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928}
2929
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002930static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002932 struct nfs_server *server = NFS_SERVER(data->inode);
2933
Trond Myklebusta65318b2009-03-11 14:10:28 -04002934 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002935 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 data->timestamp = jiffies;
2937
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002938 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939}
2940
Trond Myklebust788e7a82006-03-20 13:44:27 -05002941static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 struct inode *inode = data->inode;
2944
Andy Adamson21d9a852009-04-01 09:22:27 -04002945 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
2946 task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002947 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Andy Adamsoneedc0202009-04-01 09:22:41 -04002948 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002949 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 }
Andy Adamson21d9a852009-04-01 09:22:27 -04002951 nfs4_sequence_free_slot(NFS_SERVER(inode)->nfs_client,
2952 &data->res.seq_res);
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04002953 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002954 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955}
2956
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002957static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958{
Trond Myklebust788e7a82006-03-20 13:44:27 -05002959 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Trond Myklebusta65318b2009-03-11 14:10:28 -04002961 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002962 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002963 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964}
2965
2966/*
2967 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2968 * standalone procedure for queueing an asynchronous RENEW.
2969 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002970static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971{
David Howellsadfa6f92006-08-22 20:06:08 -04002972 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002973 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
2975 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04002976 /* Unless we're shutting down, schedule state recovery! */
2977 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
2978 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 return;
2980 }
2981 spin_lock(&clp->cl_lock);
2982 if (time_before(clp->cl_last_renewal,timestamp))
2983 clp->cl_last_renewal = timestamp;
2984 spin_unlock(&clp->cl_lock);
2985}
2986
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002987static const struct rpc_call_ops nfs4_renew_ops = {
2988 .rpc_call_done = nfs4_renew_done,
2989};
2990
David Howellsadfa6f92006-08-22 20:06:08 -04002991int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
2993 struct rpc_message msg = {
2994 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2995 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002996 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 };
2998
2999 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003000 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001}
3002
David Howellsadfa6f92006-08-22 20:06:08 -04003003int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004{
3005 struct rpc_message msg = {
3006 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3007 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003008 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 };
3010 unsigned long now = jiffies;
3011 int status;
3012
3013 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3014 if (status < 0)
3015 return status;
3016 spin_lock(&clp->cl_lock);
3017 if (time_before(clp->cl_last_renewal,now))
3018 clp->cl_last_renewal = now;
3019 spin_unlock(&clp->cl_lock);
3020 return 0;
3021}
3022
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003023static inline int nfs4_server_supports_acls(struct nfs_server *server)
3024{
3025 return (server->caps & NFS_CAP_ACLS)
3026 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3027 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3028}
3029
3030/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3031 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3032 * the stack.
3033 */
3034#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3035
3036static void buf_to_pages(const void *buf, size_t buflen,
3037 struct page **pages, unsigned int *pgbase)
3038{
3039 const void *p = buf;
3040
3041 *pgbase = offset_in_page(buf);
3042 p -= *pgbase;
3043 while (p < buf + buflen) {
3044 *(pages++) = virt_to_page(p);
3045 p += PAGE_CACHE_SIZE;
3046 }
3047}
3048
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003049struct nfs4_cached_acl {
3050 int cached;
3051 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003052 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003053};
3054
3055static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003056{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003057 struct nfs_inode *nfsi = NFS_I(inode);
3058
3059 spin_lock(&inode->i_lock);
3060 kfree(nfsi->nfs4_acl);
3061 nfsi->nfs4_acl = acl;
3062 spin_unlock(&inode->i_lock);
3063}
3064
3065static void nfs4_zap_acl_attr(struct inode *inode)
3066{
3067 nfs4_set_cached_acl(inode, NULL);
3068}
3069
3070static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3071{
3072 struct nfs_inode *nfsi = NFS_I(inode);
3073 struct nfs4_cached_acl *acl;
3074 int ret = -ENOENT;
3075
3076 spin_lock(&inode->i_lock);
3077 acl = nfsi->nfs4_acl;
3078 if (acl == NULL)
3079 goto out;
3080 if (buf == NULL) /* user is just asking for length */
3081 goto out_len;
3082 if (acl->cached == 0)
3083 goto out;
3084 ret = -ERANGE; /* see getxattr(2) man page */
3085 if (acl->len > buflen)
3086 goto out;
3087 memcpy(buf, acl->data, acl->len);
3088out_len:
3089 ret = acl->len;
3090out:
3091 spin_unlock(&inode->i_lock);
3092 return ret;
3093}
3094
3095static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3096{
3097 struct nfs4_cached_acl *acl;
3098
3099 if (buf && acl_len <= PAGE_SIZE) {
3100 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3101 if (acl == NULL)
3102 goto out;
3103 acl->cached = 1;
3104 memcpy(acl->data, buf, acl_len);
3105 } else {
3106 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3107 if (acl == NULL)
3108 goto out;
3109 acl->cached = 0;
3110 }
3111 acl->len = acl_len;
3112out:
3113 nfs4_set_cached_acl(inode, acl);
3114}
3115
Trond Myklebust16b42892006-08-24 12:27:15 -04003116static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003117{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003118 struct page *pages[NFS4ACL_MAXPAGES];
3119 struct nfs_getaclargs args = {
3120 .fh = NFS_FH(inode),
3121 .acl_pages = pages,
3122 .acl_len = buflen,
3123 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003124 struct nfs_getaclres res = {
3125 .acl_len = buflen,
3126 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003127 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003128 struct rpc_message msg = {
3129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3130 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003131 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003132 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003133 struct page *localpage = NULL;
3134 int ret;
3135
3136 if (buflen < PAGE_SIZE) {
3137 /* As long as we're doing a round trip to the server anyway,
3138 * let's be prepared for a page of acl data. */
3139 localpage = alloc_page(GFP_KERNEL);
3140 resp_buf = page_address(localpage);
3141 if (localpage == NULL)
3142 return -ENOMEM;
3143 args.acl_pages[0] = localpage;
3144 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003145 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003146 } else {
3147 resp_buf = buf;
3148 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3149 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003150 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003151 if (ret)
3152 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003153 if (res.acl_len > args.acl_len)
3154 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003155 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003156 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003157 if (buf) {
3158 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003159 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003160 goto out_free;
3161 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003162 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003163 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003164 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003165out_free:
3166 if (localpage)
3167 __free_page(localpage);
3168 return ret;
3169}
3170
Trond Myklebust16b42892006-08-24 12:27:15 -04003171static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3172{
3173 struct nfs4_exception exception = { };
3174 ssize_t ret;
3175 do {
3176 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3177 if (ret >= 0)
3178 break;
3179 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3180 } while (exception.retry);
3181 return ret;
3182}
3183
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003184static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3185{
3186 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003187 int ret;
3188
3189 if (!nfs4_server_supports_acls(server))
3190 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003191 ret = nfs_revalidate_inode(server, inode);
3192 if (ret < 0)
3193 return ret;
Trond Myklebustf41f7412008-06-11 17:39:04 -04003194 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3195 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003196 ret = nfs4_read_cached_acl(inode, buf, buflen);
3197 if (ret != -ENOENT)
3198 return ret;
3199 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003200}
3201
Trond Myklebust16b42892006-08-24 12:27:15 -04003202static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003203{
3204 struct nfs_server *server = NFS_SERVER(inode);
3205 struct page *pages[NFS4ACL_MAXPAGES];
3206 struct nfs_setaclargs arg = {
3207 .fh = NFS_FH(inode),
3208 .acl_pages = pages,
3209 .acl_len = buflen,
3210 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003211 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003212 struct rpc_message msg = {
3213 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3214 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003215 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003216 };
3217 int ret;
3218
3219 if (!nfs4_server_supports_acls(server))
3220 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003221 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003222 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003223 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003224 nfs_access_zap_cache(inode);
3225 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003226 return ret;
3227}
3228
Trond Myklebust16b42892006-08-24 12:27:15 -04003229static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3230{
3231 struct nfs4_exception exception = { };
3232 int err;
3233 do {
3234 err = nfs4_handle_exception(NFS_SERVER(inode),
3235 __nfs4_proc_set_acl(inode, buf, buflen),
3236 &exception);
3237 } while (exception.retry);
3238 return err;
3239}
3240
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241static int
Andy Adamson8328d592009-04-01 09:22:35 -04003242_nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs_client *clp, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 if (!clp || task->tk_status >= 0)
3245 return 0;
3246 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003247 case -NFS4ERR_ADMIN_REVOKED:
3248 case -NFS4ERR_BAD_STATEID:
3249 case -NFS4ERR_OPENMODE:
3250 if (state == NULL)
3251 break;
3252 nfs4_state_mark_reclaim_nograce(clp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 case -NFS4ERR_STALE_CLIENTID:
3254 case -NFS4ERR_STALE_STATEID:
3255 case -NFS4ERR_EXPIRED:
Trond Myklebust5d008372008-02-22 16:34:17 -05003256 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 nfs4_schedule_state_recovery(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05003258 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
Trond Myklebustfda13932008-02-22 16:34:12 -05003259 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 task->tk_status = 0;
3261 return -EAGAIN;
Andy Adamson4745e312009-04-01 09:22:42 -04003262#if defined(CONFIG_NFS_V4_1)
3263 case -NFS4ERR_BADSESSION:
3264 case -NFS4ERR_BADSLOT:
3265 case -NFS4ERR_BAD_HIGH_SLOT:
3266 case -NFS4ERR_DEADSESSION:
3267 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3268 case -NFS4ERR_SEQ_FALSE_RETRY:
3269 case -NFS4ERR_SEQ_MISORDERED:
3270 dprintk("%s ERROR %d, Reset session\n", __func__,
3271 task->tk_status);
3272 set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
3273 task->tk_status = 0;
3274 return -EAGAIN;
3275#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 case -NFS4ERR_DELAY:
Andy Adamson8328d592009-04-01 09:22:35 -04003277 if (server)
3278 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003279 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3281 task->tk_status = 0;
3282 return -EAGAIN;
3283 case -NFS4ERR_OLD_STATEID:
3284 task->tk_status = 0;
3285 return -EAGAIN;
3286 }
3287 task->tk_status = nfs4_map_errors(task->tk_status);
3288 return 0;
3289}
3290
Andy Adamson8328d592009-04-01 09:22:35 -04003291static int
3292nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3293{
3294 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3295}
3296
David Howellsadfa6f92006-08-22 20:06:08 -04003297int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
3299 nfs4_verifier sc_verifier;
3300 struct nfs4_setclientid setclientid = {
3301 .sc_verifier = &sc_verifier,
3302 .sc_prog = program,
3303 };
3304 struct rpc_message msg = {
3305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3306 .rpc_argp = &setclientid,
3307 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003308 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 };
Al Virobc4785c2006-10-19 23:28:51 -07003310 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 int loop = 0;
3312 int status;
3313
Al Virobc4785c2006-10-19 23:28:51 -07003314 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3316 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3317
3318 for(;;) {
3319 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003320 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003321 clp->cl_ipaddr,
3322 rpc_peeraddr2str(clp->cl_rpcclient,
3323 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003324 rpc_peeraddr2str(clp->cl_rpcclient,
3325 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003326 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 clp->cl_id_uniquifier);
3328 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003329 sizeof(setclientid.sc_netid),
3330 rpc_peeraddr2str(clp->cl_rpcclient,
3331 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003333 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 clp->cl_ipaddr, port >> 8, port & 255);
3335
3336 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3337 if (status != -NFS4ERR_CLID_INUSE)
3338 break;
3339 if (signalled())
3340 break;
3341 if (loop++ & 1)
3342 ssleep(clp->cl_lease_time + 1);
3343 else
3344 if (++clp->cl_id_uniquifier == 0)
3345 break;
3346 }
3347 return status;
3348}
3349
David Howellsadfa6f92006-08-22 20:06:08 -04003350static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351{
3352 struct nfs_fsinfo fsinfo;
3353 struct rpc_message msg = {
3354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3355 .rpc_argp = clp,
3356 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003357 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 };
3359 unsigned long now;
3360 int status;
3361
3362 now = jiffies;
3363 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3364 if (status == 0) {
3365 spin_lock(&clp->cl_lock);
3366 clp->cl_lease_time = fsinfo.lease_time * HZ;
3367 clp->cl_last_renewal = now;
3368 spin_unlock(&clp->cl_lock);
3369 }
3370 return status;
3371}
3372
David Howellsadfa6f92006-08-22 20:06:08 -04003373int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003374{
Benny Halevy77e03672008-06-24 20:25:57 +03003375 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003376 int err;
3377 do {
3378 err = _nfs4_proc_setclientid_confirm(clp, cred);
3379 switch (err) {
3380 case 0:
3381 return err;
3382 case -NFS4ERR_RESOURCE:
3383 /* The IBM lawyers misread another document! */
3384 case -NFS4ERR_DELAY:
3385 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3386 }
3387 } while (err == 0);
3388 return err;
3389}
3390
Trond Myklebustfe650402006-01-03 09:55:18 +01003391struct nfs4_delegreturndata {
3392 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003393 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003394 struct nfs_fh fh;
3395 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003396 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003397 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003398 int rpc_status;
3399};
3400
Trond Myklebustfe650402006-01-03 09:55:18 +01003401static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3402{
3403 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003404
3405 nfs4_sequence_done_free_slot(data->res.server, &data->res.seq_res,
3406 task->tk_status);
3407
Trond Myklebustfe650402006-01-03 09:55:18 +01003408 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003409 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01003410 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01003411}
3412
3413static void nfs4_delegreturn_release(void *calldata)
3414{
Trond Myklebustfe650402006-01-03 09:55:18 +01003415 kfree(calldata);
3416}
3417
Andy Adamson938e1012009-04-01 09:22:28 -04003418#if defined(CONFIG_NFS_V4_1)
3419static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3420{
3421 struct nfs4_delegreturndata *d_data;
3422
3423 d_data = (struct nfs4_delegreturndata *)data;
3424
3425 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3426 &d_data->args.seq_args,
3427 &d_data->res.seq_res, 1, task))
3428 return;
3429 rpc_call_start(task);
3430}
3431#endif /* CONFIG_NFS_V4_1 */
3432
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003433static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003434#if defined(CONFIG_NFS_V4_1)
3435 .rpc_call_prepare = nfs4_delegreturn_prepare,
3436#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003437 .rpc_call_done = nfs4_delegreturn_done,
3438 .rpc_release = nfs4_delegreturn_release,
3439};
3440
Trond Myklebuste6f81072008-01-24 18:14:34 -05003441static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003442{
3443 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003444 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003445 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003446 struct rpc_message msg = {
3447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3448 .rpc_cred = cred,
3449 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003450 struct rpc_task_setup task_setup_data = {
3451 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003452 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003453 .callback_ops = &nfs4_delegreturn_ops,
3454 .flags = RPC_TASK_ASYNC,
3455 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003456 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003457
Andy Adamson938e1012009-04-01 09:22:28 -04003458 data = kzalloc(sizeof(*data), GFP_KERNEL);
Trond Myklebustfe650402006-01-03 09:55:18 +01003459 if (data == NULL)
3460 return -ENOMEM;
3461 data->args.fhandle = &data->fh;
3462 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003463 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003464 nfs_copy_fh(&data->fh, NFS_FH(inode));
3465 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003466 data->res.fattr = &data->fattr;
3467 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003468 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003469 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003470 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003471 data->rpc_status = 0;
3472
Trond Myklebustc970aa82007-07-14 15:39:59 -04003473 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003474 msg.rpc_argp = &data->args,
3475 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003476 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003477 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003478 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003479 if (!issync)
3480 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003481 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003482 if (status != 0)
3483 goto out;
3484 status = data->rpc_status;
3485 if (status != 0)
3486 goto out;
3487 nfs_refresh_inode(inode, &data->fattr);
3488out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003489 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003490 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491}
3492
Trond Myklebuste6f81072008-01-24 18:14:34 -05003493int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494{
3495 struct nfs_server *server = NFS_SERVER(inode);
3496 struct nfs4_exception exception = { };
3497 int err;
3498 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003499 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 switch (err) {
3501 case -NFS4ERR_STALE_STATEID:
3502 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 case 0:
3504 return 0;
3505 }
3506 err = nfs4_handle_exception(server, err, &exception);
3507 } while (exception.retry);
3508 return err;
3509}
3510
3511#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3512#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3513
3514/*
3515 * sleep, with exponential backoff, and retry the LOCK operation.
3516 */
3517static unsigned long
3518nfs4_set_lock_task_retry(unsigned long timeout)
3519{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003520 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 timeout <<= 1;
3522 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3523 return NFS4_LOCK_MAXTIMEOUT;
3524 return timeout;
3525}
3526
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3528{
3529 struct inode *inode = state->inode;
3530 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003531 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003532 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003534 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003536 struct nfs_lockt_res res = {
3537 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 };
3539 struct rpc_message msg = {
3540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3541 .rpc_argp = &arg,
3542 .rpc_resp = &res,
3543 .rpc_cred = state->owner->so_cred,
3544 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 struct nfs4_lock_state *lsp;
3546 int status;
3547
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003548 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003549 status = nfs4_set_lock_state(state, request);
3550 if (status != 0)
3551 goto out;
3552 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003553 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003554 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003555 switch (status) {
3556 case 0:
3557 request->fl_type = F_UNLCK;
3558 break;
3559 case -NFS4ERR_DENIED:
3560 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003562 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003563out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 return status;
3565}
3566
3567static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3568{
3569 struct nfs4_exception exception = { };
3570 int err;
3571
3572 do {
3573 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3574 _nfs4_proc_getlk(state, cmd, request),
3575 &exception);
3576 } while (exception.retry);
3577 return err;
3578}
3579
3580static int do_vfs_lock(struct file *file, struct file_lock *fl)
3581{
3582 int res = 0;
3583 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3584 case FL_POSIX:
3585 res = posix_lock_file_wait(file, fl);
3586 break;
3587 case FL_FLOCK:
3588 res = flock_lock_file_wait(file, fl);
3589 break;
3590 default:
3591 BUG();
3592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 return res;
3594}
3595
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003596struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003597 struct nfs_locku_args arg;
3598 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003599 struct nfs4_lock_state *lsp;
3600 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003601 struct file_lock fl;
3602 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003603 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003604};
3605
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003606static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3607 struct nfs_open_context *ctx,
3608 struct nfs4_lock_state *lsp,
3609 struct nfs_seqid *seqid)
3610{
3611 struct nfs4_unlockdata *p;
3612 struct inode *inode = lsp->ls_state->inode;
3613
Andy Adamsona8936932009-04-01 09:22:23 -04003614 p = kzalloc(sizeof(*p), GFP_KERNEL);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003615 if (p == NULL)
3616 return NULL;
3617 p->arg.fh = NFS_FH(inode);
3618 p->arg.fl = &p->fl;
3619 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003620 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003621 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003622 p->arg.stateid = &lsp->ls_stateid;
3623 p->lsp = lsp;
3624 atomic_inc(&lsp->ls_count);
3625 /* Ensure we don't close file until we're done freeing locks! */
3626 p->ctx = get_nfs_open_context(ctx);
3627 memcpy(&p->fl, fl, sizeof(p->fl));
3628 p->server = NFS_SERVER(inode);
3629 return p;
3630}
3631
Trond Myklebust06f814a2006-01-03 09:55:07 +01003632static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003633{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003634 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003635 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003636 nfs4_put_lock_state(calldata->lsp);
3637 put_nfs_open_context(calldata->ctx);
3638 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003639}
3640
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003641static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003642{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003643 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003644
Andy Adamsona8936932009-04-01 09:22:23 -04003645 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3646 task->tk_status);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003647 if (RPC_ASSASSINATED(task))
3648 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003649 switch (task->tk_status) {
3650 case 0:
3651 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003652 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003653 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003654 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003655 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003656 case -NFS4ERR_BAD_STATEID:
3657 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003658 case -NFS4ERR_STALE_STATEID:
3659 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003660 break;
3661 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003662 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Andy Adamsoneedc0202009-04-01 09:22:41 -04003663 nfs4_restart_rpc(task,
3664 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003665 }
Andy Adamsona8936932009-04-01 09:22:23 -04003666 nfs4_sequence_free_slot(calldata->server->nfs_client,
3667 &calldata->res.seq_res);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003668}
3669
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003670static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003671{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003672 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003674 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003675 return;
3676 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003677 /* Note: exit _without_ running nfs4_locku_done */
3678 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003679 return;
3680 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003681 calldata->timestamp = jiffies;
Andy Adamsona8936932009-04-01 09:22:23 -04003682 if (nfs4_setup_sequence(calldata->server->nfs_client,
3683 &calldata->arg.seq_args,
3684 &calldata->res.seq_res, 1, task))
3685 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003686 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687}
3688
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003689static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003690 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003691 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003692 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003693};
3694
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003695static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3696 struct nfs_open_context *ctx,
3697 struct nfs4_lock_state *lsp,
3698 struct nfs_seqid *seqid)
3699{
3700 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003701 struct rpc_message msg = {
3702 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3703 .rpc_cred = ctx->cred,
3704 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003705 struct rpc_task_setup task_setup_data = {
3706 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003707 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003708 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003709 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003710 .flags = RPC_TASK_ASYNC,
3711 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003712
Frank Filz137d6ac2007-07-09 15:32:29 -07003713 /* Ensure this is an unlock - when canceling a lock, the
3714 * canceled lock is passed in, and it won't be an unlock.
3715 */
3716 fl->fl_type = F_UNLCK;
3717
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003718 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3719 if (data == NULL) {
3720 nfs_free_seqid(seqid);
3721 return ERR_PTR(-ENOMEM);
3722 }
3723
Trond Myklebust5138fde2007-07-14 15:40:01 -04003724 msg.rpc_argp = &data->arg,
3725 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003726 task_setup_data.callback_data = data;
3727 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003728}
3729
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3731{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003732 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003733 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003734 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003735 struct rpc_task *task;
3736 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003737 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
Trond Myklebust9b073572006-06-29 16:38:34 -04003739 status = nfs4_set_lock_state(state, request);
3740 /* Unlock _before_ we do the RPC call */
3741 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003742 down_read(&nfsi->rwsem);
3743 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3744 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003745 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003746 }
3747 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003748 if (status != 0)
3749 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003750 /* Is this a delegated lock? */
3751 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003752 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003753 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003754 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003755 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003756 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003757 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003758 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003759 status = PTR_ERR(task);
3760 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003761 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003762 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003763 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003764out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003765 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003766 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767}
3768
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003769struct nfs4_lockdata {
3770 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003771 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003772 struct nfs4_lock_state *lsp;
3773 struct nfs_open_context *ctx;
3774 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003775 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003776 int rpc_status;
3777 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04003778 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003779};
3780
3781static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3782 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3783{
3784 struct nfs4_lockdata *p;
3785 struct inode *inode = lsp->ls_state->inode;
3786 struct nfs_server *server = NFS_SERVER(inode);
3787
3788 p = kzalloc(sizeof(*p), GFP_KERNEL);
3789 if (p == NULL)
3790 return NULL;
3791
3792 p->arg.fh = NFS_FH(inode);
3793 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003794 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3795 if (p->arg.open_seqid == NULL)
3796 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003797 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3798 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003799 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003800 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003801 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003802 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003803 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003804 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003805 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04003806 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003807 atomic_inc(&lsp->ls_count);
3808 p->ctx = get_nfs_open_context(ctx);
3809 memcpy(&p->fl, fl, sizeof(p->fl));
3810 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003811out_free_seqid:
3812 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003813out_free:
3814 kfree(p);
3815 return NULL;
3816}
3817
3818static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3819{
3820 struct nfs4_lockdata *data = calldata;
3821 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822
Harvey Harrison3110ff82008-05-02 13:42:44 -07003823 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003824 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3825 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003826 /* Do we need to do an open_to_lock_owner? */
3827 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05003828 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3829 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003830 data->arg.open_stateid = &state->stateid;
3831 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003832 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003833 } else
3834 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003835 data->timestamp = jiffies;
Andy Adamson66179ef2009-04-01 09:22:22 -04003836 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
3837 &data->res.seq_res, 1, task))
3838 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003839 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003840 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003841}
3842
3843static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3844{
3845 struct nfs4_lockdata *data = calldata;
3846
Harvey Harrison3110ff82008-05-02 13:42:44 -07003847 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003848
Andy Adamson66179ef2009-04-01 09:22:22 -04003849 nfs4_sequence_done_free_slot(data->server, &data->res.seq_res,
3850 task->tk_status);
3851
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003852 data->rpc_status = task->tk_status;
3853 if (RPC_ASSASSINATED(task))
3854 goto out;
3855 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003856 if (data->rpc_status == 0)
3857 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3858 else
3859 goto out;
3860 }
3861 if (data->rpc_status == 0) {
3862 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3863 sizeof(data->lsp->ls_stateid.data));
3864 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003865 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003866 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003867out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07003868 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003869}
3870
3871static void nfs4_lock_release(void *calldata)
3872{
3873 struct nfs4_lockdata *data = calldata;
3874
Harvey Harrison3110ff82008-05-02 13:42:44 -07003875 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003876 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003877 if (data->cancelled != 0) {
3878 struct rpc_task *task;
3879 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3880 data->arg.lock_seqid);
3881 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003882 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003883 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003884 } else
3885 nfs_free_seqid(data->arg.lock_seqid);
3886 nfs4_put_lock_state(data->lsp);
3887 put_nfs_open_context(data->ctx);
3888 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003889 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003890}
3891
3892static const struct rpc_call_ops nfs4_lock_ops = {
3893 .rpc_call_prepare = nfs4_lock_prepare,
3894 .rpc_call_done = nfs4_lock_done,
3895 .rpc_release = nfs4_lock_release,
3896};
3897
3898static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3899{
3900 struct nfs4_lockdata *data;
3901 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003902 struct rpc_message msg = {
3903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3904 .rpc_cred = state->owner->so_cred,
3905 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003906 struct rpc_task_setup task_setup_data = {
3907 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003908 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003909 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003910 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003911 .flags = RPC_TASK_ASYNC,
3912 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003913 int ret;
3914
Harvey Harrison3110ff82008-05-02 13:42:44 -07003915 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003916 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003917 fl->fl_u.nfs4_fl.owner);
3918 if (data == NULL)
3919 return -ENOMEM;
3920 if (IS_SETLKW(cmd))
3921 data->arg.block = 1;
3922 if (reclaim != 0)
3923 data->arg.reclaim = 1;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003924 msg.rpc_argp = &data->arg,
3925 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003926 task_setup_data.callback_data = data;
3927 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003928 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003929 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003930 ret = nfs4_wait_for_completion_rpc_task(task);
3931 if (ret == 0) {
3932 ret = data->rpc_status;
3933 if (ret == -NFS4ERR_DENIED)
3934 ret = -EAGAIN;
3935 } else
3936 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003937 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003938 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003939 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940}
3941
3942static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3943{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003944 struct nfs_server *server = NFS_SERVER(state->inode);
3945 struct nfs4_exception exception = { };
3946 int err;
3947
3948 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003949 /* Cache the lock if possible... */
3950 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3951 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003952 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3953 if (err != -NFS4ERR_DELAY)
3954 break;
3955 nfs4_handle_exception(server, err, &exception);
3956 } while (exception.retry);
3957 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958}
3959
3960static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3961{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003962 struct nfs_server *server = NFS_SERVER(state->inode);
3963 struct nfs4_exception exception = { };
3964 int err;
3965
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003966 err = nfs4_set_lock_state(state, request);
3967 if (err != 0)
3968 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003969 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003970 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3971 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003972 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3973 if (err != -NFS4ERR_DELAY)
3974 break;
3975 nfs4_handle_exception(server, err, &exception);
3976 } while (exception.retry);
3977 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978}
3979
3980static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3981{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003982 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003983 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 int status;
3985
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003986 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003987 status = nfs4_set_lock_state(state, request);
3988 if (status != 0)
3989 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003990 request->fl_flags |= FL_ACCESS;
3991 status = do_vfs_lock(request->fl_file, request);
3992 if (status < 0)
3993 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003994 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003995 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04003996 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003997 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05003998 request->fl_flags = fl_flags & ~FL_SLEEP;
3999 status = do_vfs_lock(request->fl_file, request);
4000 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004001 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004002 status = _nfs4_do_setlk(state, cmd, request, 0);
4003 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004004 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004005 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004006 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004007 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004008 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004009out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004010 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004011out:
4012 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 return status;
4014}
4015
4016static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4017{
4018 struct nfs4_exception exception = { };
4019 int err;
4020
4021 do {
4022 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4023 _nfs4_proc_setlk(state, cmd, request),
4024 &exception);
4025 } while (exception.retry);
4026 return err;
4027}
4028
4029static int
4030nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4031{
4032 struct nfs_open_context *ctx;
4033 struct nfs4_state *state;
4034 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4035 int status;
4036
4037 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004038 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 state = ctx->state;
4040
4041 if (request->fl_start < 0 || request->fl_end < 0)
4042 return -EINVAL;
4043
4044 if (IS_GETLK(cmd))
4045 return nfs4_proc_getlk(state, F_GETLK, request);
4046
4047 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4048 return -EINVAL;
4049
4050 if (request->fl_type == F_UNLCK)
4051 return nfs4_proc_unlck(state, cmd, request);
4052
4053 do {
4054 status = nfs4_proc_setlk(state, cmd, request);
4055 if ((status != -EAGAIN) || IS_SETLK(cmd))
4056 break;
4057 timeout = nfs4_set_lock_task_retry(timeout);
4058 status = -ERESTARTSYS;
4059 if (signalled())
4060 break;
4061 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 return status;
4063}
4064
Trond Myklebust888e6942005-11-04 15:38:11 -05004065int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4066{
4067 struct nfs_server *server = NFS_SERVER(state->inode);
4068 struct nfs4_exception exception = { };
4069 int err;
4070
4071 err = nfs4_set_lock_state(state, fl);
4072 if (err != 0)
4073 goto out;
4074 do {
4075 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
4076 if (err != -NFS4ERR_DELAY)
4077 break;
4078 err = nfs4_handle_exception(server, err, &exception);
4079 } while (exception.retry);
4080out:
4081 return err;
4082}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004083
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004084#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4085
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004086int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4087 size_t buflen, int flags)
4088{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004089 struct inode *inode = dentry->d_inode;
4090
4091 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4092 return -EOPNOTSUPP;
4093
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004094 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004095}
4096
4097/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4098 * and that's what we'll do for e.g. user attributes that haven't been set.
4099 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4100 * attributes in kernel-managed attribute namespaces. */
4101ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4102 size_t buflen)
4103{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004104 struct inode *inode = dentry->d_inode;
4105
4106 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4107 return -EOPNOTSUPP;
4108
4109 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004110}
4111
4112ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4113{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004114 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004115
J. Bruce Fields096455a2006-03-20 23:23:42 -05004116 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4117 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004118 if (buf && buflen < len)
4119 return -ERANGE;
4120 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004121 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4122 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004123}
4124
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004125static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4126{
4127 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4128 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4129 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4130 return;
4131
4132 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4133 NFS_ATTR_FATTR_NLINK;
4134 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4135 fattr->nlink = 2;
4136}
4137
Trond Myklebust56659e92007-07-17 21:52:39 -04004138int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004139 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004140{
4141 struct nfs_server *server = NFS_SERVER(dir);
4142 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004143 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4144 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004145 };
4146 struct nfs4_fs_locations_arg args = {
4147 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004148 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004149 .page = page,
4150 .bitmask = bitmask,
4151 };
Benny Halevy22958462009-04-01 09:22:02 -04004152 struct nfs4_fs_locations_res res = {
4153 .fs_locations = fs_locations,
4154 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004155 struct rpc_message msg = {
4156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4157 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004158 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004159 };
4160 int status;
4161
Harvey Harrison3110ff82008-05-02 13:42:44 -07004162 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004163 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004164 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004165 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004166 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004167 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004168 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004169 return status;
4170}
4171
Andy Adamson557134a2009-04-01 09:21:53 -04004172#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004173/*
4174 * nfs4_proc_exchange_id()
4175 *
4176 * Since the clientid has expired, all compounds using sessions
4177 * associated with the stale clientid will be returning
4178 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4179 * be in some phase of session reset.
4180 */
4181static int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4182{
4183 nfs4_verifier verifier;
4184 struct nfs41_exchange_id_args args = {
4185 .client = clp,
4186 .flags = clp->cl_exchange_flags,
4187 };
4188 struct nfs41_exchange_id_res res = {
4189 .client = clp,
4190 };
4191 int status;
4192 struct rpc_message msg = {
4193 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4194 .rpc_argp = &args,
4195 .rpc_resp = &res,
4196 .rpc_cred = cred,
4197 };
4198 __be32 *p;
4199
4200 dprintk("--> %s\n", __func__);
4201 BUG_ON(clp == NULL);
4202 p = (u32 *)verifier.data;
4203 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4204 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4205 args.verifier = &verifier;
4206
4207 while (1) {
4208 args.id_len = scnprintf(args.id, sizeof(args.id),
4209 "%s/%s %u",
4210 clp->cl_ipaddr,
4211 rpc_peeraddr2str(clp->cl_rpcclient,
4212 RPC_DISPLAY_ADDR),
4213 clp->cl_id_uniquifier);
4214
4215 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4216
4217 if (status != NFS4ERR_CLID_INUSE)
4218 break;
4219
4220 if (signalled())
4221 break;
4222
4223 if (++clp->cl_id_uniquifier == 0)
4224 break;
4225 }
4226
4227 dprintk("<-- %s status= %d\n", __func__, status);
4228 return status;
4229}
4230
Andy Adamson2050f0c2009-04-01 09:22:30 -04004231struct nfs4_get_lease_time_data {
4232 struct nfs4_get_lease_time_args *args;
4233 struct nfs4_get_lease_time_res *res;
4234 struct nfs_client *clp;
4235};
4236
4237static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4238 void *calldata)
4239{
4240 int ret;
4241 struct nfs4_get_lease_time_data *data =
4242 (struct nfs4_get_lease_time_data *)calldata;
4243
4244 dprintk("--> %s\n", __func__);
4245 /* just setup sequence, do not trigger session recovery
4246 since we're invoked within one */
4247 ret = nfs41_setup_sequence(data->clp->cl_session,
4248 &data->args->la_seq_args,
4249 &data->res->lr_seq_res, 0, task);
4250
4251 BUG_ON(ret == -EAGAIN);
4252 rpc_call_start(task);
4253 dprintk("<-- %s\n", __func__);
4254}
4255
4256/*
4257 * Called from nfs4_state_manager thread for session setup, so don't recover
4258 * from sequence operation or clientid errors.
4259 */
4260static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4261{
4262 struct nfs4_get_lease_time_data *data =
4263 (struct nfs4_get_lease_time_data *)calldata;
4264
4265 dprintk("--> %s\n", __func__);
4266 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4267 switch (task->tk_status) {
4268 case -NFS4ERR_DELAY:
4269 case -NFS4ERR_GRACE:
4270 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4271 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4272 task->tk_status = 0;
Andy Adamsoneedc0202009-04-01 09:22:41 -04004273 nfs4_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004274 return;
4275 }
4276 nfs41_sequence_free_slot(data->clp, &data->res->lr_seq_res);
4277 dprintk("<-- %s\n", __func__);
4278}
4279
4280struct rpc_call_ops nfs4_get_lease_time_ops = {
4281 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4282 .rpc_call_done = nfs4_get_lease_time_done,
4283};
4284
4285int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4286{
4287 struct rpc_task *task;
4288 struct nfs4_get_lease_time_args args;
4289 struct nfs4_get_lease_time_res res = {
4290 .lr_fsinfo = fsinfo,
4291 };
4292 struct nfs4_get_lease_time_data data = {
4293 .args = &args,
4294 .res = &res,
4295 .clp = clp,
4296 };
4297 struct rpc_message msg = {
4298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4299 .rpc_argp = &args,
4300 .rpc_resp = &res,
4301 };
4302 struct rpc_task_setup task_setup = {
4303 .rpc_client = clp->cl_rpcclient,
4304 .rpc_message = &msg,
4305 .callback_ops = &nfs4_get_lease_time_ops,
4306 .callback_data = &data
4307 };
4308 int status;
4309
4310 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4311 dprintk("--> %s\n", __func__);
4312 task = rpc_run_task(&task_setup);
4313
4314 if (IS_ERR(task))
4315 status = PTR_ERR(task);
4316 else {
4317 status = task->tk_status;
4318 rpc_put_task(task);
4319 }
4320 dprintk("<-- %s return %d\n", __func__, status);
4321
4322 return status;
4323}
4324
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004325/* Reset a slot table */
4326static int nfs4_reset_slot_table(struct nfs4_session *session)
4327{
4328 struct nfs4_slot_table *tbl = &session->fc_slot_table;
4329 int i, max_slots = session->fc_attrs.max_reqs;
4330 int old_max_slots = session->fc_slot_table.max_slots;
4331 int ret = 0;
4332
4333 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__,
4334 session->fc_attrs.max_reqs, tbl);
4335
4336 /* Until we have dynamic slot table adjustment, insist
4337 * upon the same slot table size */
4338 if (max_slots != old_max_slots) {
4339 dprintk("%s reset slot table does't match old\n",
4340 __func__);
4341 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4342 goto out;
4343 }
4344 spin_lock(&tbl->slot_tbl_lock);
4345 for (i = 0; i < max_slots; ++i)
4346 tbl->slots[i].seq_nr = 1;
4347 tbl->highest_used_slotid = -1;
4348 spin_unlock(&tbl->slot_tbl_lock);
4349 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4350 tbl, tbl->slots, tbl->max_slots);
4351out:
4352 dprintk("<-- %s: return %d\n", __func__, ret);
4353 return ret;
4354}
4355
Andy Adamsonfc931582009-04-01 09:22:31 -04004356/*
4357 * Initialize slot table
4358 */
4359static int nfs4_init_slot_table(struct nfs4_session *session)
4360{
4361 struct nfs4_slot_table *tbl = &session->fc_slot_table;
4362 int i, max_slots = session->fc_attrs.max_reqs;
4363 struct nfs4_slot *slot;
4364 int ret = -ENOMEM;
4365
4366 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4367
4368 dprintk("--> %s: max_reqs=%u\n", __func__,
4369 session->fc_attrs.max_reqs);
4370
4371 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4372 if (!slot)
4373 goto out;
4374 for (i = 0; i < max_slots; ++i)
4375 slot[i].seq_nr = 1;
4376 ret = 0;
4377
4378 spin_lock(&tbl->slot_tbl_lock);
4379 if (tbl->slots != NULL) {
4380 spin_unlock(&tbl->slot_tbl_lock);
4381 dprintk("%s: slot table already initialized. tbl=%p slots=%p\n",
4382 __func__, tbl, tbl->slots);
4383 WARN_ON(1);
4384 goto out_free;
4385 }
4386 tbl->max_slots = max_slots;
4387 tbl->slots = slot;
4388 tbl->highest_used_slotid = -1; /* no slot is currently used */
4389 spin_unlock(&tbl->slot_tbl_lock);
4390 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4391 tbl, tbl->slots, tbl->max_slots);
4392out:
4393 dprintk("<-- %s: return %d\n", __func__, ret);
4394 return ret;
4395out_free:
4396 kfree(slot);
4397 goto out;
4398}
4399
Andy Adamson557134a2009-04-01 09:21:53 -04004400/* Destroy the slot table */
4401static void nfs4_destroy_slot_table(struct nfs4_session *session)
4402{
4403 if (session->fc_slot_table.slots == NULL)
4404 return;
4405 kfree(session->fc_slot_table.slots);
4406 session->fc_slot_table.slots = NULL;
4407 return;
4408}
4409
4410struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4411{
4412 struct nfs4_session *session;
4413 struct nfs4_slot_table *tbl;
4414
4415 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4416 if (!session)
4417 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004418
4419 set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
4420 /*
4421 * The create session reply races with the server back
4422 * channel probe. Mark the client NFS_CS_SESSION_INITING
4423 * so that the client back channel can find the
4424 * nfs_client struct
4425 */
4426 clp->cl_cons_state = NFS_CS_SESSION_INITING;
4427
Andy Adamson557134a2009-04-01 09:21:53 -04004428 tbl = &session->fc_slot_table;
4429 spin_lock_init(&tbl->slot_tbl_lock);
4430 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "Slot table");
4431 session->clp = clp;
4432 return session;
4433}
4434
4435void nfs4_destroy_session(struct nfs4_session *session)
4436{
4437 nfs4_destroy_slot_table(session);
4438 kfree(session);
4439}
4440
Andy Adamsonfc931582009-04-01 09:22:31 -04004441/*
4442 * Initialize the values to be used by the client in CREATE_SESSION
4443 * If nfs4_init_session set the fore channel request and response sizes,
4444 * use them.
4445 *
4446 * Set the back channel max_resp_sz_cached to zero to force the client to
4447 * always set csa_cachethis to FALSE because the current implementation
4448 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4449 */
4450static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4451{
4452 struct nfs4_session *session = args->client->cl_session;
4453 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4454 mxresp_sz = session->fc_attrs.max_resp_sz;
4455
4456 if (mxrqst_sz == 0)
4457 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4458 if (mxresp_sz == 0)
4459 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4460 /* Fore channel attributes */
4461 args->fc_attrs.headerpadsz = 0;
4462 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4463 args->fc_attrs.max_resp_sz = mxresp_sz;
4464 args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4465 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4466 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4467
4468 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4469 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4470 __func__,
4471 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4472 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4473 args->fc_attrs.max_reqs);
4474
4475 /* Back channel attributes */
4476 args->bc_attrs.headerpadsz = 0;
4477 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4478 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4479 args->bc_attrs.max_resp_sz_cached = 0;
4480 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4481 args->bc_attrs.max_reqs = 1;
4482
4483 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4484 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4485 __func__,
4486 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4487 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4488 args->bc_attrs.max_reqs);
4489}
4490
Andy Adamson8d353012009-04-01 09:22:32 -04004491static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4492{
4493 if (rcvd <= sent)
4494 return 0;
4495 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4496 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4497 return -EINVAL;
4498}
4499
4500#define _verify_fore_channel_attr(_name_) \
4501 _verify_channel_attr("fore", #_name_, \
4502 args->fc_attrs._name_, \
4503 session->fc_attrs._name_)
4504
4505#define _verify_back_channel_attr(_name_) \
4506 _verify_channel_attr("back", #_name_, \
4507 args->bc_attrs._name_, \
4508 session->bc_attrs._name_)
4509
4510/*
4511 * The server is not allowed to increase the fore channel header pad size,
4512 * maximum response size, or maximum number of operations.
4513 *
4514 * The back channel attributes are only negotiatied down: We send what the
4515 * (back channel) server insists upon.
4516 */
4517static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4518 struct nfs4_session *session)
4519{
4520 int ret = 0;
4521
4522 ret |= _verify_fore_channel_attr(headerpadsz);
4523 ret |= _verify_fore_channel_attr(max_resp_sz);
4524 ret |= _verify_fore_channel_attr(max_ops);
4525
4526 ret |= _verify_back_channel_attr(headerpadsz);
4527 ret |= _verify_back_channel_attr(max_rqst_sz);
4528 ret |= _verify_back_channel_attr(max_resp_sz);
4529 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4530 ret |= _verify_back_channel_attr(max_ops);
4531 ret |= _verify_back_channel_attr(max_reqs);
4532
4533 return ret;
4534}
4535
Andy Adamsonfc931582009-04-01 09:22:31 -04004536static int _nfs4_proc_create_session(struct nfs_client *clp)
4537{
4538 struct nfs4_session *session = clp->cl_session;
4539 struct nfs41_create_session_args args = {
4540 .client = clp,
4541 .cb_program = NFS4_CALLBACK,
4542 };
4543 struct nfs41_create_session_res res = {
4544 .client = clp,
4545 };
4546 struct rpc_message msg = {
4547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4548 .rpc_argp = &args,
4549 .rpc_resp = &res,
4550 };
4551 int status;
4552
4553 nfs4_init_channel_attrs(&args);
4554 args.flags = (SESSION4_PERSIST);
4555
4556 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4557
Andy Adamson8d353012009-04-01 09:22:32 -04004558 if (!status)
4559 /* Verify the session's negotiated channel_attrs values */
4560 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004561 if (!status) {
4562 /* Increment the clientid slot sequence id */
4563 clp->cl_seqid++;
4564 }
4565
4566 return status;
4567}
4568
4569/*
4570 * Issues a CREATE_SESSION operation to the server.
4571 * It is the responsibility of the caller to verify the session is
4572 * expired before calling this routine.
4573 */
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004574int nfs4_proc_create_session(struct nfs_client *clp, int reset)
Andy Adamsonfc931582009-04-01 09:22:31 -04004575{
4576 int status;
4577 unsigned *ptr;
4578 struct nfs_fsinfo fsinfo;
4579 struct nfs4_session *session = clp->cl_session;
4580
4581 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4582
4583 status = _nfs4_proc_create_session(clp);
4584 if (status)
4585 goto out;
4586
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004587 /* Init or reset the fore channel */
4588 if (reset)
4589 status = nfs4_reset_slot_table(session);
4590 else
4591 status = nfs4_init_slot_table(session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004592 dprintk("fore channel slot table initialization returned %d\n", status);
4593 if (status)
4594 goto out;
4595
4596 ptr = (unsigned *)&session->sess_id.data[0];
4597 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4598 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
4599
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004600 if (reset)
4601 /* Lease time is aleady set */
4602 goto out;
4603
Andy Adamsonfc931582009-04-01 09:22:31 -04004604 /* Get the lease time */
4605 status = nfs4_proc_get_lease_time(clp, &fsinfo);
4606 if (status == 0) {
4607 /* Update lease time and schedule renewal */
4608 spin_lock(&clp->cl_lock);
4609 clp->cl_lease_time = fsinfo.lease_time * HZ;
4610 clp->cl_last_renewal = jiffies;
4611 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
4612 spin_unlock(&clp->cl_lock);
4613
4614 nfs4_schedule_state_renewal(clp);
4615 }
4616out:
4617 dprintk("<-- %s\n", __func__);
4618 return status;
4619}
4620
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004621/*
4622 * Issue the over-the-wire RPC DESTROY_SESSION.
4623 * The caller must serialize access to this routine.
4624 */
4625int nfs4_proc_destroy_session(struct nfs4_session *session)
4626{
4627 int status = 0;
4628 struct rpc_message msg;
4629
4630 dprintk("--> nfs4_proc_destroy_session\n");
4631
4632 /* session is still being setup */
4633 if (session->clp->cl_cons_state != NFS_CS_READY)
4634 return status;
4635
4636 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4637 msg.rpc_argp = session;
4638 msg.rpc_resp = NULL;
4639 msg.rpc_cred = NULL;
4640 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4641
4642 if (status)
4643 printk(KERN_WARNING
4644 "Got error %d from the server on DESTROY_SESSION. "
4645 "Session has been destroyed regardless...\n", status);
4646
4647 dprintk("<-- nfs4_proc_destroy_session\n");
4648 return status;
4649}
4650
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004651/*
4652 * Renew the cl_session lease.
4653 */
4654static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
4655{
4656 struct nfs4_sequence_args args;
4657 struct nfs4_sequence_res res;
4658
4659 struct rpc_message msg = {
4660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4661 .rpc_argp = &args,
4662 .rpc_resp = &res,
4663 .rpc_cred = cred,
4664 };
4665
4666 args.sa_cache_this = 0;
4667
4668 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
4669 &res, 0);
4670}
4671
4672void nfs41_sequence_call_done(struct rpc_task *task, void *data)
4673{
4674 struct nfs_client *clp = (struct nfs_client *)data;
4675
4676 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
4677
4678 if (task->tk_status < 0) {
4679 dprintk("%s ERROR %d\n", __func__, task->tk_status);
4680
4681 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
4682 == -EAGAIN) {
Andy Adamsoneedc0202009-04-01 09:22:41 -04004683 nfs4_restart_rpc(task, clp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004684 return;
4685 }
4686 }
4687 nfs41_sequence_free_slot(clp, task->tk_msg.rpc_resp);
4688 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
4689
4690 put_rpccred(task->tk_msg.rpc_cred);
4691 kfree(task->tk_msg.rpc_argp);
4692 kfree(task->tk_msg.rpc_resp);
4693
4694 dprintk("<-- %s\n", __func__);
4695}
4696
4697static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
4698{
4699 struct nfs_client *clp;
4700 struct nfs4_sequence_args *args;
4701 struct nfs4_sequence_res *res;
4702
4703 clp = (struct nfs_client *)data;
4704 args = task->tk_msg.rpc_argp;
4705 res = task->tk_msg.rpc_resp;
4706
4707 if (nfs4_setup_sequence(clp, args, res, 0, task))
4708 return;
4709 rpc_call_start(task);
4710}
4711
4712static const struct rpc_call_ops nfs41_sequence_ops = {
4713 .rpc_call_done = nfs41_sequence_call_done,
4714 .rpc_call_prepare = nfs41_sequence_prepare,
4715};
4716
4717static int nfs41_proc_async_sequence(struct nfs_client *clp,
4718 struct rpc_cred *cred)
4719{
4720 struct nfs4_sequence_args *args;
4721 struct nfs4_sequence_res *res;
4722 struct rpc_message msg = {
4723 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4724 .rpc_cred = cred,
4725 };
4726
4727 args = kzalloc(sizeof(*args), GFP_KERNEL);
4728 if (!args)
4729 return -ENOMEM;
4730 res = kzalloc(sizeof(*res), GFP_KERNEL);
4731 if (!res) {
4732 kfree(args);
4733 return -ENOMEM;
4734 }
4735 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
4736 msg.rpc_argp = args;
4737 msg.rpc_resp = res;
4738
4739 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
4740 &nfs41_sequence_ops, (void *)clp);
4741}
4742
Andy Adamson557134a2009-04-01 09:21:53 -04004743#endif /* CONFIG_NFS_V4_1 */
4744
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05004746 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004747 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 .recover_open = nfs4_open_reclaim,
4749 .recover_lock = nfs4_lock_reclaim,
4750};
4751
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004752struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05004753 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004754 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 .recover_open = nfs4_open_expired,
4756 .recover_lock = nfs4_lock_expired,
4757};
4758
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004759static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004760 .permission = nfs_permission,
4761 .getattr = nfs_getattr,
4762 .setattr = nfs_setattr,
4763 .getxattr = nfs4_getxattr,
4764 .setxattr = nfs4_setxattr,
4765 .listxattr = nfs4_listxattr,
4766};
4767
David Howells509de812006-08-22 20:06:11 -04004768const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 .version = 4, /* protocol version */
4770 .dentry_ops = &nfs4_dentry_operations,
4771 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004772 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 .getroot = nfs4_proc_get_root,
4774 .getattr = nfs4_proc_getattr,
4775 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04004776 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 .lookup = nfs4_proc_lookup,
4778 .access = nfs4_proc_access,
4779 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 .create = nfs4_proc_create,
4781 .remove = nfs4_proc_remove,
4782 .unlink_setup = nfs4_proc_unlink_setup,
4783 .unlink_done = nfs4_proc_unlink_done,
4784 .rename = nfs4_proc_rename,
4785 .link = nfs4_proc_link,
4786 .symlink = nfs4_proc_symlink,
4787 .mkdir = nfs4_proc_mkdir,
4788 .rmdir = nfs4_proc_remove,
4789 .readdir = nfs4_proc_readdir,
4790 .mknod = nfs4_proc_mknod,
4791 .statfs = nfs4_proc_statfs,
4792 .fsinfo = nfs4_proc_fsinfo,
4793 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04004794 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 .decode_dirent = nfs4_decode_dirent,
4796 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05004797 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05004799 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05004801 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004803 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04004804 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805};
4806
4807/*
4808 * Local variables:
4809 * c-basic-offset: 8
4810 * End:
4811 */