blob: ed7c269e25143175a21156a4c5ce784e3efea4c1 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
42#include <linux/sunrpc/clnt.h>
43#include <linux/nfs.h>
44#include <linux/nfs4.h>
45#include <linux/nfs_fs.h>
46#include <linux/nfs_page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070048#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040049#include <linux/module.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040050#include <linux/sunrpc/bc_xprt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Trond Myklebust4ce79712005-06-22 17:16:21 +000052#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050054#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050055#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040056#include "callback.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#define NFSDBG_FACILITY NFSDBG_PROC
59
Trond Myklebust2066fe82006-09-15 08:30:46 -040060#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define NFS4_POLL_RETRY_MAX (15*HZ)
62
Trond Myklebusta78cb572009-08-09 15:06:19 -040063#define NFS4_MAX_LOOP_ON_RECOVER (10)
64
Trond Myklebustcdd4e682006-01-03 09:55:12 +010065struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010066static int _nfs4_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050068static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040069static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
70static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050073static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 if (err < -1000) {
76 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070077 __func__, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return -EIO;
79 }
80 return err;
81}
82
83/*
84 * This is our standard bitmap for GETATTR requests.
85 */
86const u32 nfs4_fattr_bitmap[2] = {
87 FATTR4_WORD0_TYPE
88 | FATTR4_WORD0_CHANGE
89 | FATTR4_WORD0_SIZE
90 | FATTR4_WORD0_FSID
91 | FATTR4_WORD0_FILEID,
92 FATTR4_WORD1_MODE
93 | FATTR4_WORD1_NUMLINKS
94 | FATTR4_WORD1_OWNER
95 | FATTR4_WORD1_OWNER_GROUP
96 | FATTR4_WORD1_RAWDEV
97 | FATTR4_WORD1_SPACE_USED
98 | FATTR4_WORD1_TIME_ACCESS
99 | FATTR4_WORD1_TIME_METADATA
100 | FATTR4_WORD1_TIME_MODIFY
101};
102
103const u32 nfs4_statfs_bitmap[2] = {
104 FATTR4_WORD0_FILES_AVAIL
105 | FATTR4_WORD0_FILES_FREE
106 | FATTR4_WORD0_FILES_TOTAL,
107 FATTR4_WORD1_SPACE_AVAIL
108 | FATTR4_WORD1_SPACE_FREE
109 | FATTR4_WORD1_SPACE_TOTAL
110};
111
Trond Myklebust4ce79712005-06-22 17:16:21 +0000112const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 FATTR4_WORD0_MAXLINK
114 | FATTR4_WORD0_MAXNAME,
115 0
116};
117
118const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
119 | FATTR4_WORD0_MAXREAD
120 | FATTR4_WORD0_MAXWRITE
121 | FATTR4_WORD0_LEASE_TIME,
122 0
123};
124
Manoj Naik830b8e32006-06-09 09:34:25 -0400125const u32 nfs4_fs_locations_bitmap[2] = {
126 FATTR4_WORD0_TYPE
127 | FATTR4_WORD0_CHANGE
128 | FATTR4_WORD0_SIZE
129 | FATTR4_WORD0_FSID
130 | FATTR4_WORD0_FILEID
131 | FATTR4_WORD0_FS_LOCATIONS,
132 FATTR4_WORD1_MODE
133 | FATTR4_WORD1_NUMLINKS
134 | FATTR4_WORD1_OWNER
135 | FATTR4_WORD1_OWNER_GROUP
136 | FATTR4_WORD1_RAWDEV
137 | FATTR4_WORD1_SPACE_USED
138 | FATTR4_WORD1_TIME_ACCESS
139 | FATTR4_WORD1_TIME_METADATA
140 | FATTR4_WORD1_TIME_MODIFY
141 | FATTR4_WORD1_MOUNTED_ON_FILEID
142};
143
Al Virobc4785c2006-10-19 23:28:51 -0700144static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 struct nfs4_readdir_arg *readdir)
146{
Al Viro0dbb4c62006-10-19 23:28:49 -0700147 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 BUG_ON(readdir->count < 80);
150 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000151 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
153 return;
154 }
155
156 readdir->cookie = 0;
157 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
158 if (cookie == 2)
159 return;
160
161 /*
162 * NFSv4 servers do not return entries for '.' and '..'
163 * Therefore, we fake these entries here. We let '.'
164 * have cookie 0 and '..' have cookie 1. Note that
165 * when talking to the server, we always send cookie 0
166 * instead of 1 or 2.
167 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700168 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 if (cookie == 0) {
171 *p++ = xdr_one; /* next */
172 *p++ = xdr_zero; /* cookie, first word */
173 *p++ = xdr_one; /* cookie, second word */
174 *p++ = xdr_one; /* entry len */
175 memcpy(p, ".\0\0\0", 4); /* entry */
176 p++;
177 *p++ = xdr_one; /* bitmap length */
178 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
179 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400180 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182
183 *p++ = xdr_one; /* next */
184 *p++ = xdr_zero; /* cookie, first word */
185 *p++ = xdr_two; /* cookie, second word */
186 *p++ = xdr_two; /* entry len */
187 memcpy(p, "..\0\0", 4); /* entry */
188 p++;
189 *p++ = xdr_one; /* bitmap length */
190 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
191 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400192 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 readdir->pgbase = (char *)p - (char *)start;
195 readdir->count -= readdir->pgbase;
196 kunmap_atomic(start, KM_USER0);
197}
198
Trond Myklebust65de8722008-12-23 15:21:44 -0500199static int nfs4_wait_clnt_recover(struct nfs_client *clp)
200{
201 int res;
202
203 might_sleep();
204
Trond Myklebuste005e802008-12-23 15:21:48 -0500205 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400206 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500207 return res;
208}
209
210static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
211{
212 int res = 0;
213
214 might_sleep();
215
216 if (*timeout <= 0)
217 *timeout = NFS4_POLL_RETRY_MIN;
218 if (*timeout > NFS4_POLL_RETRY_MAX)
219 *timeout = NFS4_POLL_RETRY_MAX;
220 schedule_timeout_killable(*timeout);
221 if (fatal_signal_pending(current))
222 res = -ERESTARTSYS;
223 *timeout <<= 1;
224 return res;
225}
226
227/* This is the error handling routine for processes that are allowed
228 * to sleep.
229 */
230static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
231{
232 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500233 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500234 int ret = errorcode;
235
236 exception->retry = 0;
237 switch(errorcode) {
238 case 0:
239 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500240 case -NFS4ERR_ADMIN_REVOKED:
241 case -NFS4ERR_BAD_STATEID:
242 case -NFS4ERR_OPENMODE:
243 if (state == NULL)
244 break;
245 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500246 case -NFS4ERR_STALE_CLIENTID:
247 case -NFS4ERR_STALE_STATEID:
248 case -NFS4ERR_EXPIRED:
249 nfs4_schedule_state_recovery(clp);
250 ret = nfs4_wait_clnt_recover(clp);
251 if (ret == 0)
252 exception->retry = 1;
Andy Adamson4745e312009-04-01 09:22:42 -0400253#if !defined(CONFIG_NFS_V4_1)
Trond Myklebust65de8722008-12-23 15:21:44 -0500254 break;
Andy Adamson4745e312009-04-01 09:22:42 -0400255#else /* !defined(CONFIG_NFS_V4_1) */
256 if (!nfs4_has_session(server->nfs_client))
257 break;
258 /* FALLTHROUGH */
259 case -NFS4ERR_BADSESSION:
260 case -NFS4ERR_BADSLOT:
261 case -NFS4ERR_BAD_HIGH_SLOT:
262 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
263 case -NFS4ERR_DEADSESSION:
264 case -NFS4ERR_SEQ_FALSE_RETRY:
265 case -NFS4ERR_SEQ_MISORDERED:
266 dprintk("%s ERROR: %d Reset session\n", __func__,
267 errorcode);
268 set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
269 exception->retry = 1;
270 /* FALLTHROUGH */
271#endif /* !defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500272 case -NFS4ERR_FILE_OPEN:
273 case -NFS4ERR_GRACE:
274 case -NFS4ERR_DELAY:
275 ret = nfs4_delay(server->client, &exception->timeout);
276 if (ret != 0)
277 break;
278 case -NFS4ERR_OLD_STATEID:
279 exception->retry = 1;
280 }
281 /* We failed to handle the error */
282 return nfs4_map_errors(ret);
283}
284
285
Trond Myklebust26e976a2006-01-03 09:55:21 +0100286static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
David Howells7539bba2006-08-22 20:06:09 -0400288 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 spin_lock(&clp->cl_lock);
290 if (time_before(clp->cl_last_renewal,timestamp))
291 clp->cl_last_renewal = timestamp;
292 spin_unlock(&clp->cl_lock);
293}
294
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400295#if defined(CONFIG_NFS_V4_1)
296
Benny Halevy510b8172009-04-01 09:22:14 -0400297/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400298 * nfs4_free_slot - free a slot and efficiently update slot table.
299 *
300 * freeing a slot is trivially done by clearing its respective bit
301 * in the bitmap.
302 * If the freed slotid equals highest_used_slotid we want to update it
303 * so that the server would be able to size down the slot table if needed,
304 * otherwise we know that the highest_used_slotid is still in use.
305 * When updating highest_used_slotid there may be "holes" in the bitmap
306 * so we need to scan down from highest_used_slotid to 0 looking for the now
307 * highest slotid in use.
308 * If none found, highest_used_slotid is set to -1.
309 */
310static void
311nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
312{
313 int slotid = free_slotid;
314
315 spin_lock(&tbl->slot_tbl_lock);
316 /* clear used bit in bitmap */
317 __clear_bit(slotid, tbl->used_slots);
318
319 /* update highest_used_slotid when it is freed */
320 if (slotid == tbl->highest_used_slotid) {
321 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
322 if (slotid >= 0 && slotid < tbl->max_slots)
323 tbl->highest_used_slotid = slotid;
324 else
325 tbl->highest_used_slotid = -1;
326 }
327 rpc_wake_up_next(&tbl->slot_tbl_waitq);
328 spin_unlock(&tbl->slot_tbl_lock);
329 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
330 free_slotid, tbl->highest_used_slotid);
331}
332
Andy Adamson13615872009-04-01 09:22:17 -0400333void nfs41_sequence_free_slot(const struct nfs_client *clp,
334 struct nfs4_sequence_res *res)
335{
336 struct nfs4_slot_table *tbl;
337
338 if (!nfs4_has_session(clp)) {
339 dprintk("%s: No session\n", __func__);
340 return;
341 }
342 tbl = &clp->cl_session->fc_slot_table;
343 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
344 dprintk("%s: No slot\n", __func__);
345 /* just wake up the next guy waiting since
346 * we may have not consumed a slot after all */
347 rpc_wake_up_next(&tbl->slot_tbl_waitq);
348 return;
349 }
350 nfs4_free_slot(tbl, res->sr_slotid);
351 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
352}
353
Andy Adamsonb0df8062009-04-01 09:22:18 -0400354static void nfs41_sequence_done(struct nfs_client *clp,
355 struct nfs4_sequence_res *res,
356 int rpc_status)
357{
358 unsigned long timestamp;
359 struct nfs4_slot_table *tbl;
360 struct nfs4_slot *slot;
361
362 /*
363 * sr_status remains 1 if an RPC level error occurred. The server
364 * may or may not have processed the sequence operation..
365 * Proceed as if the server received and processed the sequence
366 * operation.
367 */
368 if (res->sr_status == 1)
369 res->sr_status = NFS_OK;
370
371 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
372 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
373 goto out;
374
375 tbl = &clp->cl_session->fc_slot_table;
376 slot = tbl->slots + res->sr_slotid;
377
378 if (res->sr_status == 0) {
379 /* Update the slot's sequence and clientid lease timer */
380 ++slot->seq_nr;
381 timestamp = res->sr_renewal_time;
382 spin_lock(&clp->cl_lock);
383 if (time_before(clp->cl_last_renewal, timestamp))
384 clp->cl_last_renewal = timestamp;
385 spin_unlock(&clp->cl_lock);
386 return;
387 }
388out:
389 /* The session may be reset by one of the error handlers. */
390 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
391 nfs41_sequence_free_slot(clp, res);
392}
393
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400394/*
Benny Halevy510b8172009-04-01 09:22:14 -0400395 * nfs4_find_slot - efficiently look for a free slot
396 *
397 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
398 * If found, we mark the slot as used, update the highest_used_slotid,
399 * and respectively set up the sequence operation args.
400 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400401 *
402 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400403 */
404static u8
405nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
406{
407 int slotid;
408 u8 ret_id = NFS4_MAX_SLOT_TABLE;
409 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
410
Benny Halevy510b8172009-04-01 09:22:14 -0400411 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
412 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
413 tbl->max_slots);
414 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
415 if (slotid >= tbl->max_slots)
416 goto out;
417 __set_bit(slotid, tbl->used_slots);
418 if (slotid > tbl->highest_used_slotid)
419 tbl->highest_used_slotid = slotid;
420 ret_id = slotid;
421out:
422 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
423 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400424 return ret_id;
425}
426
Andy Adamsonb069d942009-04-01 09:22:43 -0400427static int nfs4_recover_session(struct nfs4_session *session)
428{
429 struct nfs_client *clp = session->clp;
Trond Myklebusta78cb572009-08-09 15:06:19 -0400430 unsigned int loop;
Andy Adamsonb069d942009-04-01 09:22:43 -0400431 int ret;
432
Trond Myklebusta78cb572009-08-09 15:06:19 -0400433 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Andy Adamsonb069d942009-04-01 09:22:43 -0400434 ret = nfs4_wait_clnt_recover(clp);
435 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -0400436 break;
Andy Adamsonb069d942009-04-01 09:22:43 -0400437 if (!test_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state))
438 break;
439 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -0400440 ret = -EIO;
Andy Adamsonb069d942009-04-01 09:22:43 -0400441 }
Trond Myklebusta78cb572009-08-09 15:06:19 -0400442 return ret;
Andy Adamsonb069d942009-04-01 09:22:43 -0400443}
444
Andy Adamsonce5039c2009-04-01 09:22:13 -0400445static int nfs41_setup_sequence(struct nfs4_session *session,
446 struct nfs4_sequence_args *args,
447 struct nfs4_sequence_res *res,
448 int cache_reply,
449 struct rpc_task *task)
450{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400451 struct nfs4_slot *slot;
452 struct nfs4_slot_table *tbl;
Andy Adamsonb069d942009-04-01 09:22:43 -0400453 int status = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400454 u8 slotid;
455
456 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400457 /* slot already allocated? */
458 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
459 return 0;
460
461 memset(res, 0, sizeof(*res));
462 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400463 tbl = &session->fc_slot_table;
464
465 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonb069d942009-04-01 09:22:43 -0400466 if (test_bit(NFS4CLNT_SESSION_SETUP, &session->clp->cl_state)) {
467 if (tbl->highest_used_slotid != -1) {
468 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
469 spin_unlock(&tbl->slot_tbl_lock);
470 dprintk("<-- %s: Session reset: draining\n", __func__);
471 return -EAGAIN;
472 }
473
474 /* The slot table is empty; start the reset thread */
475 dprintk("%s Session Reset\n", __func__);
476 spin_unlock(&tbl->slot_tbl_lock);
477 status = nfs4_recover_session(session);
478 if (status)
479 return status;
480 spin_lock(&tbl->slot_tbl_lock);
481 }
482
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400483 slotid = nfs4_find_slot(tbl, task);
484 if (slotid == NFS4_MAX_SLOT_TABLE) {
485 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
486 spin_unlock(&tbl->slot_tbl_lock);
487 dprintk("<-- %s: no free slots\n", __func__);
488 return -EAGAIN;
489 }
490 spin_unlock(&tbl->slot_tbl_lock);
491
492 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400493 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400494 args->sa_slotid = slotid;
495 args->sa_cache_this = cache_reply;
496
497 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
498
Benny Halevy99fe60d2009-04-01 09:22:29 -0400499 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400500 res->sr_slotid = slotid;
501 res->sr_renewal_time = jiffies;
502 /*
503 * sr_status is only set in decode_sequence, and so will remain
504 * set to 1 if an rpc level failure occurs.
505 */
506 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400507 return 0;
508}
509
510int nfs4_setup_sequence(struct nfs_client *clp,
511 struct nfs4_sequence_args *args,
512 struct nfs4_sequence_res *res,
513 int cache_reply,
514 struct rpc_task *task)
515{
516 int ret = 0;
517
518 dprintk("--> %s clp %p session %p sr_slotid %d\n",
519 __func__, clp, clp->cl_session, res->sr_slotid);
520
521 if (!nfs4_has_session(clp))
522 goto out;
523 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
524 task);
525 if (ret != -EAGAIN) {
526 /* terminate rpc task */
527 task->tk_status = ret;
528 task->tk_action = NULL;
529 }
530out:
531 dprintk("<-- %s status=%d\n", __func__, ret);
532 return ret;
533}
534
535struct nfs41_call_sync_data {
536 struct nfs_client *clp;
537 struct nfs4_sequence_args *seq_args;
538 struct nfs4_sequence_res *seq_res;
539 int cache_reply;
540};
541
542static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
543{
544 struct nfs41_call_sync_data *data = calldata;
545
546 dprintk("--> %s data->clp->cl_session %p\n", __func__,
547 data->clp->cl_session);
548 if (nfs4_setup_sequence(data->clp, data->seq_args,
549 data->seq_res, data->cache_reply, task))
550 return;
551 rpc_call_start(task);
552}
553
Andy Adamson69ab40c2009-04-01 09:22:19 -0400554static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
555{
556 struct nfs41_call_sync_data *data = calldata;
557
558 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
559 nfs41_sequence_free_slot(data->clp, data->seq_res);
560}
561
Andy Adamsonce5039c2009-04-01 09:22:13 -0400562struct rpc_call_ops nfs41_call_sync_ops = {
563 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400564 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400565};
566
567static int nfs4_call_sync_sequence(struct nfs_client *clp,
568 struct rpc_clnt *clnt,
569 struct rpc_message *msg,
570 struct nfs4_sequence_args *args,
571 struct nfs4_sequence_res *res,
572 int cache_reply)
573{
574 int ret;
575 struct rpc_task *task;
576 struct nfs41_call_sync_data data = {
577 .clp = clp,
578 .seq_args = args,
579 .seq_res = res,
580 .cache_reply = cache_reply,
581 };
582 struct rpc_task_setup task_setup = {
583 .rpc_client = clnt,
584 .rpc_message = msg,
585 .callback_ops = &nfs41_call_sync_ops,
586 .callback_data = &data
587 };
588
589 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
590 task = rpc_run_task(&task_setup);
591 if (IS_ERR(task))
592 ret = PTR_ERR(task);
593 else {
594 ret = task->tk_status;
595 rpc_put_task(task);
596 }
597 return ret;
598}
599
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400600int _nfs4_call_sync_session(struct nfs_server *server,
601 struct rpc_message *msg,
602 struct nfs4_sequence_args *args,
603 struct nfs4_sequence_res *res,
604 int cache_reply)
605{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400606 return nfs4_call_sync_sequence(server->nfs_client, server->client,
607 msg, args, res, cache_reply);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400608}
609
610#endif /* CONFIG_NFS_V4_1 */
611
612int _nfs4_call_sync(struct nfs_server *server,
613 struct rpc_message *msg,
614 struct nfs4_sequence_args *args,
615 struct nfs4_sequence_res *res,
616 int cache_reply)
617{
Benny Halevyf3752972009-04-01 09:22:04 -0400618 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400619 return rpc_call_sync(server->client, msg, 0);
620}
621
622#define nfs4_call_sync(server, msg, args, res, cache_reply) \
623 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
624 &(res)->seq_res, (cache_reply))
625
Andy Adamsonb0df8062009-04-01 09:22:18 -0400626static void nfs4_sequence_done(const struct nfs_server *server,
627 struct nfs4_sequence_res *res, int rpc_status)
628{
629#ifdef CONFIG_NFS_V4_1
630 if (nfs4_has_session(server->nfs_client))
631 nfs41_sequence_done(server->nfs_client, res, rpc_status);
632#endif /* CONFIG_NFS_V4_1 */
633}
634
635/* no restart, therefore free slot here */
636static void nfs4_sequence_done_free_slot(const struct nfs_server *server,
637 struct nfs4_sequence_res *res,
638 int rpc_status)
639{
640 nfs4_sequence_done(server, res, rpc_status);
641 nfs4_sequence_free_slot(server->nfs_client, res);
642}
643
Trond Myklebust38478b22006-05-25 01:40:57 -0400644static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Trond Myklebust38478b22006-05-25 01:40:57 -0400646 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Trond Myklebust38478b22006-05-25 01:40:57 -0400648 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400649 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
650 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400651 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400652 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400653 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100656struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400657 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100658 struct nfs_openargs o_arg;
659 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100660 struct nfs_open_confirmargs c_arg;
661 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100662 struct nfs_fattr f_attr;
663 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400664 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100665 struct dentry *dir;
666 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400667 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100668 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100669 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400670 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100671 int rpc_status;
672 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100673};
674
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400675
676static void nfs4_init_opendata_res(struct nfs4_opendata *p)
677{
678 p->o_res.f_attr = &p->f_attr;
679 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400680 p->o_res.seqid = p->o_arg.seqid;
681 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400682 p->o_res.server = p->o_arg.server;
683 nfs_fattr_init(&p->f_attr);
684 nfs_fattr_init(&p->dir_attr);
Benny Halevy578e4582009-06-18 22:01:23 -0400685 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400686}
687
Trond Myklebustad389da2007-06-05 12:30:00 -0400688static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500689 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100690 const struct iattr *attrs)
691{
Trond Myklebustad389da2007-06-05 12:30:00 -0400692 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100693 struct inode *dir = parent->d_inode;
694 struct nfs_server *server = NFS_SERVER(dir);
695 struct nfs4_opendata *p;
696
697 p = kzalloc(sizeof(*p), GFP_KERNEL);
698 if (p == NULL)
699 goto err;
700 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
701 if (p->o_arg.seqid == NULL)
702 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400703 p->path.mnt = mntget(path->mnt);
704 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100705 p->dir = parent;
706 p->owner = sp;
707 atomic_inc(&sp->so_count);
708 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500709 p->o_arg.open_flags = flags;
710 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400711 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400712 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400713 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100714 p->o_arg.server = server;
715 p->o_arg.bitmask = server->attr_bitmask;
716 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100717 if (flags & O_EXCL) {
718 u32 *s = (u32 *) p->o_arg.u.verifier.data;
719 s[0] = jiffies;
720 s[1] = current->pid;
721 } else if (flags & O_CREAT) {
722 p->o_arg.u.attrs = &p->attrs;
723 memcpy(&p->attrs, attrs, sizeof(p->attrs));
724 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100725 p->c_arg.fh = &p->o_res.fh;
726 p->c_arg.stateid = &p->o_res.stateid;
727 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400728 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400729 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100730 return p;
731err_free:
732 kfree(p);
733err:
734 dput(parent);
735 return NULL;
736}
737
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400738static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100739{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400740 struct nfs4_opendata *p = container_of(kref,
741 struct nfs4_opendata, kref);
742
743 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400744 if (p->state != NULL)
745 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400746 nfs4_put_state_owner(p->owner);
747 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700748 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400749 kfree(p);
750}
751
752static void nfs4_opendata_put(struct nfs4_opendata *p)
753{
754 if (p != NULL)
755 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100756}
757
Trond Myklebust06f814a2006-01-03 09:55:07 +0100758static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
759{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100760 int ret;
761
Trond Myklebust06f814a2006-01-03 09:55:07 +0100762 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100763 return ret;
764}
765
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500766static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400767{
768 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500769
770 if (open_mode & O_EXCL)
771 goto out;
772 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400773 case FMODE_READ:
774 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400775 break;
776 case FMODE_WRITE:
777 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400778 break;
779 case FMODE_READ|FMODE_WRITE:
780 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
781 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500782out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400783 return ret;
784}
785
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500786static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400787{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500788 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400789 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500790 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400791 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500792 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400793 return 1;
794}
795
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500796static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100797{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500798 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100799 case FMODE_WRITE:
800 state->n_wronly++;
801 break;
802 case FMODE_READ:
803 state->n_rdonly++;
804 break;
805 case FMODE_READ|FMODE_WRITE:
806 state->n_rdwr++;
807 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500808 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100809}
810
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500811static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400812{
813 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
814 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
815 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500816 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400817 case FMODE_READ:
818 set_bit(NFS_O_RDONLY_STATE, &state->flags);
819 break;
820 case FMODE_WRITE:
821 set_bit(NFS_O_WRONLY_STATE, &state->flags);
822 break;
823 case FMODE_READ|FMODE_WRITE:
824 set_bit(NFS_O_RDWR_STATE, &state->flags);
825 }
826}
827
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500828static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400829{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400830 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500831 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400832 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400833}
834
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500835static 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 -0700836{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400837 /*
838 * Protect the call to nfs4_state_set_mode_locked and
839 * serialise the stateid update
840 */
841 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400842 if (deleg_stateid != NULL) {
843 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
844 set_bit(NFS_DELEGATED_STATE, &state->flags);
845 }
846 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500847 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400848 write_sequnlock(&state->seqlock);
849 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500850 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700851 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500854static 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 -0500855{
856 struct nfs_inode *nfsi = NFS_I(state->inode);
857 struct nfs_delegation *deleg_cur;
858 int ret = 0;
859
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500860 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500861
862 rcu_read_lock();
863 deleg_cur = rcu_dereference(nfsi->delegation);
864 if (deleg_cur == NULL)
865 goto no_delegation;
866
867 spin_lock(&deleg_cur->lock);
868 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500869 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500870 goto no_delegation_unlock;
871
872 if (delegation == NULL)
873 delegation = &deleg_cur->stateid;
874 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
875 goto no_delegation_unlock;
876
Trond Myklebustb7391f42008-12-23 15:21:52 -0500877 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500878 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500879 ret = 1;
880no_delegation_unlock:
881 spin_unlock(&deleg_cur->lock);
882no_delegation:
883 rcu_read_unlock();
884
885 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500886 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500887 ret = 1;
888 }
889
890 return ret;
891}
892
893
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500894static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400895{
896 struct nfs_delegation *delegation;
897
898 rcu_read_lock();
899 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500900 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400901 rcu_read_unlock();
902 return;
903 }
904 rcu_read_unlock();
905 nfs_inode_return_delegation(inode);
906}
907
Trond Myklebust6ee41262007-07-08 14:11:36 -0400908static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400909{
910 struct nfs4_state *state = opendata->state;
911 struct nfs_inode *nfsi = NFS_I(state->inode);
912 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500913 int open_mode = opendata->o_arg.open_flags & O_EXCL;
914 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400915 nfs4_stateid stateid;
916 int ret = -EAGAIN;
917
Trond Myklebustaac00a82007-07-05 19:02:21 -0400918 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500919 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400920 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500921 if (can_open_cached(state, fmode, open_mode)) {
922 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400923 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400924 goto out_return_state;
925 }
926 spin_unlock(&state->owner->so_lock);
927 }
Trond Myklebust34310432008-12-23 15:21:38 -0500928 rcu_read_lock();
929 delegation = rcu_dereference(nfsi->delegation);
930 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500931 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500932 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400933 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500934 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400935 /* Save the delegation */
936 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
937 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400938 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400939 if (ret != 0)
940 goto out;
941 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500942
943 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500944 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500945 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400946 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400947out:
948 return ERR_PTR(ret);
949out_return_state:
950 atomic_inc(&state->count);
951 return state;
952}
953
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100954static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
955{
956 struct inode *inode;
957 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400958 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400959 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100960
Trond Myklebustaac00a82007-07-05 19:02:21 -0400961 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400962 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400963 goto out;
964 }
965
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400966 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100967 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400968 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100969 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400970 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500971 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400972 goto err;
973 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100974 state = nfs4_get_open_state(inode, data->owner);
975 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400976 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400977 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400978 int delegation_flags = 0;
979
Trond Myklebust003707c2007-07-05 18:07:55 -0400980 rcu_read_lock();
981 delegation = rcu_dereference(NFS_I(inode)->delegation);
982 if (delegation)
983 delegation_flags = delegation->flags;
984 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -0500985 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400986 nfs_inode_set_delegation(state->inode,
987 data->owner->so_cred,
988 &data->o_res);
989 else
990 nfs_inode_reclaim_delegation(state->inode,
991 data->owner->so_cred,
992 &data->o_res);
993 }
Trond Myklebust34310432008-12-23 15:21:38 -0500994
995 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100997 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400998out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100999 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001000err_put_inode:
1001 iput(inode);
1002err:
1003 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001004}
1005
Trond Myklebust864472e2006-01-03 09:55:15 +01001006static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1007{
1008 struct nfs_inode *nfsi = NFS_I(state->inode);
1009 struct nfs_open_context *ctx;
1010
1011 spin_lock(&state->inode->i_lock);
1012 list_for_each_entry(ctx, &nfsi->open_files, list) {
1013 if (ctx->state != state)
1014 continue;
1015 get_nfs_open_context(ctx);
1016 spin_unlock(&state->inode->i_lock);
1017 return ctx;
1018 }
1019 spin_unlock(&state->inode->i_lock);
1020 return ERR_PTR(-ENOENT);
1021}
1022
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001023static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1024{
1025 struct nfs4_opendata *opendata;
1026
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001027 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001028 if (opendata == NULL)
1029 return ERR_PTR(-ENOMEM);
1030 opendata->state = state;
1031 atomic_inc(&state->count);
1032 return opendata;
1033}
1034
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001035static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001036{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001037 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001038 int ret;
1039
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001040 opendata->o_arg.open_flags = 0;
1041 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001042 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1043 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1044 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001045 ret = _nfs4_proc_open(opendata);
1046 if (ret != 0)
1047 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001048 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001049 if (IS_ERR(newstate))
1050 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001051 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001052 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001053 return 0;
1054}
1055
1056static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1057{
Trond Myklebust864472e2006-01-03 09:55:15 +01001058 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001059 int ret;
1060
1061 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001062 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001063 smp_rmb();
1064 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001065 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001066 if (ret != 0)
1067 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001068 if (newstate != state)
1069 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001070 }
1071 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001072 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001073 if (ret != 0)
1074 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001075 if (newstate != state)
1076 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001077 }
1078 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001079 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001080 if (ret != 0)
1081 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001082 if (newstate != state)
1083 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001084 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001085 /*
1086 * We may have performed cached opens for all three recoveries.
1087 * Check if we need to update the current stateid.
1088 */
1089 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1090 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001091 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001092 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1093 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001094 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001095 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001096 return 0;
1097}
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099/*
1100 * OPEN_RECLAIM:
1101 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001103static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001105 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001106 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001107 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 int status;
1109
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001110 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1111 if (IS_ERR(opendata))
1112 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001113 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1114 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001115 rcu_read_lock();
1116 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001117 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001118 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001119 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001120 opendata->o_arg.u.delegation_type = delegation_type;
1121 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001122 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return status;
1124}
1125
Trond Myklebust539cd032007-06-05 11:46:42 -04001126static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
1128 struct nfs_server *server = NFS_SERVER(state->inode);
1129 struct nfs4_exception exception = { };
1130 int err;
1131 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001132 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001133 if (err != -NFS4ERR_DELAY)
1134 break;
1135 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 } while (exception.retry);
1137 return err;
1138}
1139
Trond Myklebust864472e2006-01-03 09:55:15 +01001140static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1141{
1142 struct nfs_open_context *ctx;
1143 int ret;
1144
1145 ctx = nfs4_state_find_open_context(state);
1146 if (IS_ERR(ctx))
1147 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001148 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001149 put_nfs_open_context(ctx);
1150 return ret;
1151}
1152
Trond Myklebust13437e12007-07-06 15:10:43 -04001153static 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 -07001154{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001155 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001156 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001158 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1159 if (IS_ERR(opendata))
1160 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001161 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001162 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001163 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001164 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001165 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001166 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
1168
Trond Myklebust13437e12007-07-06 15:10:43 -04001169int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001172 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 int err;
1174 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001175 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 switch (err) {
1177 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001178 case -ENOENT:
1179 case -ESTALE:
1180 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 case -NFS4ERR_STALE_CLIENTID:
1182 case -NFS4ERR_STALE_STATEID:
1183 case -NFS4ERR_EXPIRED:
1184 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001185 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001186 goto out;
1187 case -ERESTARTSYS:
1188 /*
1189 * The show must go on: exit, but mark the
1190 * stateid as needing recovery.
1191 */
1192 case -NFS4ERR_ADMIN_REVOKED:
1193 case -NFS4ERR_BAD_STATEID:
1194 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1195 case -ENOMEM:
1196 err = 0;
1197 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
1199 err = nfs4_handle_exception(server, err, &exception);
1200 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001201out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return err;
1203}
1204
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001205static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1206{
1207 struct nfs4_opendata *data = calldata;
1208
1209 data->rpc_status = task->tk_status;
1210 if (RPC_ASSASSINATED(task))
1211 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001212 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001213 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1214 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001215 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001216 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001217 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001218 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001219}
1220
1221static void nfs4_open_confirm_release(void *calldata)
1222{
1223 struct nfs4_opendata *data = calldata;
1224 struct nfs4_state *state = NULL;
1225
1226 /* If this request hasn't been cancelled, do nothing */
1227 if (data->cancelled == 0)
1228 goto out_free;
1229 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001230 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001231 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001232 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001233 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001234 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001235out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001236 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001237}
1238
1239static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001240 .rpc_call_done = nfs4_open_confirm_done,
1241 .rpc_release = nfs4_open_confirm_release,
1242};
1243
1244/*
1245 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1246 */
1247static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1248{
1249 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1250 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001251 struct rpc_message msg = {
1252 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1253 .rpc_argp = &data->c_arg,
1254 .rpc_resp = &data->c_res,
1255 .rpc_cred = data->owner->so_cred,
1256 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001257 struct rpc_task_setup task_setup_data = {
1258 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001259 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001260 .callback_ops = &nfs4_open_confirm_ops,
1261 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001262 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001263 .flags = RPC_TASK_ASYNC,
1264 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 int status;
1266
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001267 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001268 data->rpc_done = 0;
1269 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001270 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001271 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001272 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001273 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001274 status = nfs4_wait_for_completion_rpc_task(task);
1275 if (status != 0) {
1276 data->cancelled = 1;
1277 smp_wmb();
1278 } else
1279 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001280 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return status;
1282}
1283
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001284static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001286 struct nfs4_opendata *data = calldata;
1287 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001288
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001289 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1290 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001291 /*
1292 * Check if we still need to send an OPEN call, or if we can use
1293 * a delegation instead.
1294 */
1295 if (data->state != NULL) {
1296 struct nfs_delegation *delegation;
1297
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001298 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001299 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001300 rcu_read_lock();
1301 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1302 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001303 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001304 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001305 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001306 }
1307 rcu_read_unlock();
1308 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001309 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001310 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001311 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001312 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001313 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001314 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1315 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001316 data->timestamp = jiffies;
Andy Adamsond8985282009-04-01 09:22:21 -04001317 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1318 &data->o_arg.seq_args,
1319 &data->o_res.seq_res, 1, task))
1320 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001321 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001322 return;
1323out_no_action:
1324 task->tk_action = NULL;
1325
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001326}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001328static void nfs4_open_done(struct rpc_task *task, void *calldata)
1329{
1330 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001332 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001333
1334 nfs4_sequence_done_free_slot(data->o_arg.server, &data->o_res.seq_res,
1335 task->tk_status);
1336
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001337 if (RPC_ASSASSINATED(task))
1338 return;
1339 if (task->tk_status == 0) {
1340 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001341 case S_IFREG:
1342 break;
1343 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001344 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001345 break;
1346 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001347 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001348 break;
1349 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001350 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001351 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001352 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001353 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1354 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001355 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001356 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001357}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001358
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001359static void nfs4_open_release(void *calldata)
1360{
1361 struct nfs4_opendata *data = calldata;
1362 struct nfs4_state *state = NULL;
1363
1364 /* If this request hasn't been cancelled, do nothing */
1365 if (data->cancelled == 0)
1366 goto out_free;
1367 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001368 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001369 goto out_free;
1370 /* In case we need an open_confirm, no cleanup! */
1371 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1372 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001373 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001374 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001375 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001376out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001377 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001378}
1379
1380static const struct rpc_call_ops nfs4_open_ops = {
1381 .rpc_call_prepare = nfs4_open_prepare,
1382 .rpc_call_done = nfs4_open_done,
1383 .rpc_release = nfs4_open_release,
1384};
1385
1386/*
1387 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1388 */
1389static int _nfs4_proc_open(struct nfs4_opendata *data)
1390{
1391 struct inode *dir = data->dir->d_inode;
1392 struct nfs_server *server = NFS_SERVER(dir);
1393 struct nfs_openargs *o_arg = &data->o_arg;
1394 struct nfs_openres *o_res = &data->o_res;
1395 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001396 struct rpc_message msg = {
1397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1398 .rpc_argp = o_arg,
1399 .rpc_resp = o_res,
1400 .rpc_cred = data->owner->so_cred,
1401 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001402 struct rpc_task_setup task_setup_data = {
1403 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001404 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001405 .callback_ops = &nfs4_open_ops,
1406 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001407 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001408 .flags = RPC_TASK_ASYNC,
1409 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001410 int status;
1411
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001412 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001413 data->rpc_done = 0;
1414 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001415 data->cancelled = 0;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001416 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001417 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001418 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001419 status = nfs4_wait_for_completion_rpc_task(task);
1420 if (status != 0) {
1421 data->cancelled = 1;
1422 smp_wmb();
1423 } else
1424 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001425 rpc_put_task(task);
Trond Myklebust3e309912007-07-07 13:19:59 -04001426 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001427 return status;
1428
Trond Myklebust99367812007-07-17 21:52:41 -04001429 if (o_res->fh.size == 0)
1430 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1431
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001432 if (o_arg->open_flags & O_CREAT) {
1433 update_changeattr(dir, &o_res->cinfo);
1434 nfs_post_op_update_inode(dir, o_res->dir_attr);
1435 } else
1436 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001438 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001440 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
1442 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001443 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001444 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
Trond Myklebust10afec92007-05-14 17:16:04 -04001447static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001448{
David Howells7539bba2006-08-22 20:06:09 -04001449 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001450 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001451 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001452
Trond Myklebusta78cb572009-08-09 15:06:19 -04001453 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001454 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001455 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001456 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001457 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1458 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001459 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001460 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001461 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001462 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001463 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001464}
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466/*
1467 * OPEN_EXPIRED:
1468 * reclaim state on the server after a network partition.
1469 * Assumes caller holds the appropriate lock
1470 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001471static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001473 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001474 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001476 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1477 if (IS_ERR(opendata))
1478 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001479 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001480 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001481 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001482 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001483 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
1485
Trond Myklebust539cd032007-06-05 11:46:42 -04001486static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001487{
Trond Myklebust539cd032007-06-05 11:46:42 -04001488 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001489 struct nfs4_exception exception = { };
1490 int err;
1491
1492 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001493 err = _nfs4_open_expired(ctx, state);
Trond Myklebust027b6ca2008-12-23 16:04:13 -05001494 if (err != -NFS4ERR_DELAY)
1495 break;
1496 nfs4_handle_exception(server, err, &exception);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001497 } while (exception.retry);
1498 return err;
1499}
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1502{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001504 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Trond Myklebust864472e2006-01-03 09:55:15 +01001506 ctx = nfs4_state_find_open_context(state);
1507 if (IS_ERR(ctx))
1508 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001509 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001510 put_nfs_open_context(ctx);
1511 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
1513
1514/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001515 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1516 * fields corresponding to attributes that were used to store the verifier.
1517 * Make sure we clobber those fields in the later setattr call
1518 */
1519static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1520{
1521 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1522 !(sattr->ia_valid & ATTR_ATIME_SET))
1523 sattr->ia_valid |= ATTR_ATIME;
1524
1525 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1526 !(sattr->ia_valid & ATTR_MTIME_SET))
1527 sattr->ia_valid |= ATTR_MTIME;
1528}
1529
1530/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001531 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001533static 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 -07001534{
1535 struct nfs4_state_owner *sp;
1536 struct nfs4_state *state = NULL;
1537 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001538 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001539 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 status = -ENOMEM;
1543 if (!(sp = nfs4_get_state_owner(server, cred))) {
1544 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1545 goto out_err;
1546 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001547 status = nfs4_recover_expired_lease(server);
1548 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001549 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001550 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001551 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001552 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001553 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001554 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001555 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Trond Myklebustaac00a82007-07-05 19:02:21 -04001557 if (path->dentry->d_inode != NULL)
1558 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1559
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001560 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001562 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001564 if (opendata->o_arg.open_flags & O_EXCL)
1565 nfs4_exclusive_attrset(opendata, sattr);
1566
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001567 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001568 status = PTR_ERR(state);
1569 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001570 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001571 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 *res = state;
1574 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001575err_opendata_put:
1576 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001577err_put_state_owner:
1578 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 *res = NULL;
1581 return status;
1582}
1583
1584
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001585static 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 -07001586{
1587 struct nfs4_exception exception = { };
1588 struct nfs4_state *res;
1589 int status;
1590
1591 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001592 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (status == 0)
1594 break;
1595 /* NOTE: BAD_SEQID means the server and client disagree about the
1596 * book-keeping w.r.t. state-changing operations
1597 * (OPEN/CLOSE/LOCK/LOCKU...)
1598 * It is actually a sign of a bug on the client or on the server.
1599 *
1600 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001601 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 * have unhashed the old state_owner for us, and that we can
1603 * therefore safely retry using a new one. We should still warn
1604 * the user though...
1605 */
1606 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001607 printk(KERN_WARNING "NFS: v4 server %s "
1608 " returned a bad sequence-id error!\n",
1609 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 exception.retry = 1;
1611 continue;
1612 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001613 /*
1614 * BAD_STATEID on OPEN means that the server cancelled our
1615 * state before it received the OPEN_CONFIRM.
1616 * Recover by retrying the request as per the discussion
1617 * on Page 181 of RFC3530.
1618 */
1619 if (status == -NFS4ERR_BAD_STATEID) {
1620 exception.retry = 1;
1621 continue;
1622 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001623 if (status == -EAGAIN) {
1624 /* We must have found a delegation */
1625 exception.retry = 1;
1626 continue;
1627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1629 status, &exception));
1630 } while (exception.retry);
1631 return res;
1632}
1633
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001634static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1635 struct nfs_fattr *fattr, struct iattr *sattr,
1636 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001638 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001640 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 .iap = sattr,
1642 .server = server,
1643 .bitmask = server->attr_bitmask,
1644 };
1645 struct nfs_setattrres res = {
1646 .fattr = fattr,
1647 .server = server,
1648 };
1649 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001650 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1651 .rpc_argp = &arg,
1652 .rpc_resp = &res,
1653 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001655 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001656 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Trond Myklebust0e574af2005-10-27 22:12:38 -04001658 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001660 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1661 /* Use that stateid */
1662 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001663 nfs4_copy_stateid(&arg.stateid, state, current->files);
1664 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1666
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001667 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001668 if (status == 0 && state != NULL)
1669 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001670 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
1672
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001673static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1674 struct nfs_fattr *fattr, struct iattr *sattr,
1675 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001677 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 struct nfs4_exception exception = { };
1679 int err;
1680 do {
1681 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001682 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 &exception);
1684 } while (exception.retry);
1685 return err;
1686}
1687
1688struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001689 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 struct inode *inode;
1691 struct nfs4_state *state;
1692 struct nfs_closeargs arg;
1693 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001694 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001695 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696};
1697
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001698static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001699{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001700 struct nfs4_closedata *calldata = data;
1701 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001702
1703 nfs4_put_open_state(calldata->state);
1704 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001705 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001706 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001707 kfree(calldata);
1708}
1709
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001710static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001712 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 struct nfs_server *server = NFS_SERVER(calldata->inode);
1715
Andy Adamson19ddab02009-04-01 09:22:20 -04001716 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001717 if (RPC_ASSASSINATED(task))
1718 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 /* hmm. we are done with the inode, and in the process of freeing
1720 * the state_owner. we keep this around to process errors
1721 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 switch (task->tk_status) {
1723 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001724 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001725 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 break;
1727 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001728 case -NFS4ERR_OLD_STATEID:
1729 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001731 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001732 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001734 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Andy Adamsoneedc0202009-04-01 09:22:41 -04001735 nfs4_restart_rpc(task, server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 return;
1737 }
1738 }
Andy Adamson19ddab02009-04-01 09:22:20 -04001739 nfs4_sequence_free_slot(server->nfs_client, &calldata->res.seq_res);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001740 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
1742
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001743static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001745 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001746 struct nfs4_state *state = calldata->state;
Trond Myklebust003707c2007-07-05 18:07:55 -04001747 int clear_rd, clear_wr, clear_rdwr;
Trond Myklebust95121352005-10-18 14:20:12 -07001748
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001749 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001750 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001751
Trond Myklebust003707c2007-07-05 18:07:55 -04001752 clear_rd = clear_wr = clear_rdwr = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001753 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001754 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001755 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001756 if (state->n_rdonly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001757 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1758 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1759 }
1760 if (state->n_wronly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001761 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1762 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1763 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001764 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001765 spin_unlock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001766 if (!clear_rd && !clear_wr && !clear_rdwr) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001767 /* Note: exit _without_ calling nfs4_close_done */
1768 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001769 return;
1770 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001771 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust45328c32007-07-26 17:47:34 -04001772 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001773 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001774 calldata->arg.fmode = FMODE_READ;
Trond Myklebust45328c32007-07-26 17:47:34 -04001775 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001776 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001777 calldata->arg.fmode = FMODE_WRITE;
Trond Myklebust45328c32007-07-26 17:47:34 -04001778 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001779 calldata->timestamp = jiffies;
Andy Adamson19ddab02009-04-01 09:22:20 -04001780 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1781 &calldata->arg.seq_args, &calldata->res.seq_res,
1782 1, task))
1783 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001784 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001787static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001788 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001789 .rpc_call_done = nfs4_close_done,
1790 .rpc_release = nfs4_free_closedata,
1791};
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793/*
1794 * It is possible for data to be read/written from a mem-mapped file
1795 * after the sys_close call (which hits the vfs layer as a flush).
1796 * This means that we can't safely call nfsv4 close on a file until
1797 * the inode is cleared. This in turn means that we are not good
1798 * NFSv4 citizens - we do not indicate to the server to update the file's
1799 * share state even when we are done with one of the three share
1800 * stateid's in the inode.
1801 *
1802 * NOTE: Caller must be holding the sp->so_owner semaphore!
1803 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001804int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001806 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001808 struct nfs4_state_owner *sp = state->owner;
1809 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001810 struct rpc_message msg = {
1811 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1812 .rpc_cred = state->owner->so_cred,
1813 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001814 struct rpc_task_setup task_setup_data = {
1815 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001816 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001817 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001818 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001819 .flags = RPC_TASK_ASYNC,
1820 };
Trond Myklebust95121352005-10-18 14:20:12 -07001821 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Andy Adamson19ddab02009-04-01 09:22:20 -04001823 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001825 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001826 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001828 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001829 calldata->arg.stateid = &state->open_stateid;
Benny Halevy34dc1ad2009-04-01 09:22:52 -04001830 if (nfs4_has_session(server->nfs_client))
1831 memset(calldata->arg.stateid->data, 0, 4); /* clear seqid */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001833 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001834 if (calldata->arg.seqid == NULL)
1835 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001836 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001837 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001838 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001839 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001840 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001841 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001842 calldata->path.mnt = mntget(path->mnt);
1843 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001844
Trond Myklebust5138fde2007-07-14 15:40:01 -04001845 msg.rpc_argp = &calldata->arg,
1846 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001847 task_setup_data.callback_data = calldata;
1848 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001849 if (IS_ERR(task))
1850 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001851 status = 0;
1852 if (wait)
1853 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001854 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001855 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001856out_free_calldata:
1857 kfree(calldata);
1858out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001859 nfs4_put_open_state(state);
1860 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001861 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001864static 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 -07001865{
1866 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001867 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001868
Trond Myklebust1b45c462007-07-03 13:04:56 -04001869 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001870 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001871 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001872 state->owner->so_cred,
1873 nd->intent.open.flags);
1874 if (ret < 0)
1875 goto out_close;
1876 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001877 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001878 if (!IS_ERR(filp)) {
1879 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001880 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001881 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001882 return 0;
1883 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001884 ret = PTR_ERR(filp);
1885out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001886 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001887 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001888}
1889
1890struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1892{
Trond Myklebustad389da2007-06-05 12:30:00 -04001893 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001894 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001895 .dentry = dentry,
1896 };
Jan Blunck4ac91372008-02-14 19:34:32 -08001897 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 struct iattr attr;
1899 struct rpc_cred *cred;
1900 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001901 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001902 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 if (nd->flags & LOOKUP_CREATE) {
1905 attr.ia_mode = nd->intent.open.create_mode;
1906 attr.ia_valid = ATTR_MODE;
1907 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04001908 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 } else {
1910 attr.ia_valid = 0;
1911 BUG_ON(nd->intent.open.flags & O_CREAT);
1912 }
1913
Trond Myklebust98a8e322008-03-12 12:25:28 -04001914 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001916 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04001917 parent = dentry->d_parent;
1918 /* Protect against concurrent sillydeletes */
1919 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001920 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001922 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001923 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001924 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04001925 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1926 }
Trond Myklebust565277f2007-10-15 18:17:53 -04001927 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001928 return (struct dentry *)state;
1929 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001930 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001931 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04001932 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04001933 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001934 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001935 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001936 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937}
1938
1939int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001940nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
Trond Myklebustad389da2007-06-05 12:30:00 -04001942 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001943 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001944 .dentry = dentry,
1945 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 struct rpc_cred *cred;
1947 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001948 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Trond Myklebust98a8e322008-03-12 12:25:28 -04001950 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 if (IS_ERR(cred))
1952 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001953 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001955 if (IS_ERR(state)) {
1956 switch (PTR_ERR(state)) {
1957 case -EPERM:
1958 case -EACCES:
1959 case -EDQUOT:
1960 case -ENOSPC:
1961 case -EROFS:
1962 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1963 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001964 default:
1965 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001966 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001967 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001968 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001969 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001970 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return 1;
1972 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001973 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001974out_drop:
1975 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 return 0;
1977}
1978
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001979void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1980{
1981 if (ctx->state == NULL)
1982 return;
1983 if (is_sync)
1984 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
1985 else
1986 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
1987}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1990{
Benny Halevy43652ad2009-04-01 09:21:54 -04001991 struct nfs4_server_caps_arg args = {
1992 .fhandle = fhandle,
1993 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 struct nfs4_server_caps_res res = {};
1995 struct rpc_message msg = {
1996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04001997 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 .rpc_resp = &res,
1999 };
2000 int status;
2001
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002002 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (status == 0) {
2004 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002005 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2006 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2007 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2008 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2009 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2011 server->caps |= NFS_CAP_ACLS;
2012 if (res.has_links != 0)
2013 server->caps |= NFS_CAP_HARDLINKS;
2014 if (res.has_symlinks != 0)
2015 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002016 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2017 server->caps |= NFS_CAP_FILEID;
2018 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2019 server->caps |= NFS_CAP_MODE;
2020 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2021 server->caps |= NFS_CAP_NLINK;
2022 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2023 server->caps |= NFS_CAP_OWNER;
2024 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2025 server->caps |= NFS_CAP_OWNER_GROUP;
2026 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2027 server->caps |= NFS_CAP_ATIME;
2028 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2029 server->caps |= NFS_CAP_CTIME;
2030 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2031 server->caps |= NFS_CAP_MTIME;
2032
Trond Myklebusta65318b2009-03-11 14:10:28 -04002033 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2034 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2035 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 server->acl_bitmask = res.acl_bitmask;
2037 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 return status;
2040}
2041
Trond Myklebust55a97592006-06-09 09:34:19 -04002042int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
2044 struct nfs4_exception exception = { };
2045 int err;
2046 do {
2047 err = nfs4_handle_exception(server,
2048 _nfs4_server_capabilities(server, fhandle),
2049 &exception);
2050 } while (exception.retry);
2051 return err;
2052}
2053
2054static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2055 struct nfs_fsinfo *info)
2056{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 struct nfs4_lookup_root_arg args = {
2058 .bitmask = nfs4_fattr_bitmap,
2059 };
2060 struct nfs4_lookup_res res = {
2061 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002062 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 .fh = fhandle,
2064 };
2065 struct rpc_message msg = {
2066 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2067 .rpc_argp = &args,
2068 .rpc_resp = &res,
2069 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002070
Trond Myklebust0e574af2005-10-27 22:12:38 -04002071 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002072 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073}
2074
2075static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2076 struct nfs_fsinfo *info)
2077{
2078 struct nfs4_exception exception = { };
2079 int err;
2080 do {
2081 err = nfs4_handle_exception(server,
2082 _nfs4_lookup_root(server, fhandle, info),
2083 &exception);
2084 } while (exception.retry);
2085 return err;
2086}
2087
David Howells54ceac42006-08-22 20:06:13 -04002088/*
2089 * get the file handle for the "/" directory on the server
2090 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002092 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 int status;
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 if (status == 0)
2098 status = nfs4_server_capabilities(server, fhandle);
2099 if (status == 0)
2100 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002101 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102}
2103
Manoj Naik6b97fd32006-06-09 09:34:29 -04002104/*
2105 * Get locations and (maybe) other attributes of a referral.
2106 * Note that we'll actually follow the referral later when
2107 * we detect fsid mismatch in inode revalidation
2108 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002109static 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 -04002110{
2111 int status = -ENOMEM;
2112 struct page *page = NULL;
2113 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002114
2115 page = alloc_page(GFP_KERNEL);
2116 if (page == NULL)
2117 goto out;
2118 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2119 if (locations == NULL)
2120 goto out;
2121
Trond Myklebustc228fd32007-01-13 02:28:11 -05002122 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002123 if (status != 0)
2124 goto out;
2125 /* Make sure server returned a different fsid for the referral */
2126 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002127 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 -04002128 status = -EIO;
2129 goto out;
2130 }
2131
2132 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2133 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2134 if (!fattr->mode)
2135 fattr->mode = S_IFDIR;
2136 memset(fhandle, 0, sizeof(struct nfs_fh));
2137out:
2138 if (page)
2139 __free_page(page);
2140 if (locations)
2141 kfree(locations);
2142 return status;
2143}
2144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2146{
2147 struct nfs4_getattr_arg args = {
2148 .fh = fhandle,
2149 .bitmask = server->attr_bitmask,
2150 };
2151 struct nfs4_getattr_res res = {
2152 .fattr = fattr,
2153 .server = server,
2154 };
2155 struct rpc_message msg = {
2156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2157 .rpc_argp = &args,
2158 .rpc_resp = &res,
2159 };
2160
Trond Myklebust0e574af2005-10-27 22:12:38 -04002161 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002162 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163}
2164
2165static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2166{
2167 struct nfs4_exception exception = { };
2168 int err;
2169 do {
2170 err = nfs4_handle_exception(server,
2171 _nfs4_proc_getattr(server, fhandle, fattr),
2172 &exception);
2173 } while (exception.retry);
2174 return err;
2175}
2176
2177/*
2178 * The file is not closed if it is opened due to the a request to change
2179 * the size of the file. The open call will not be needed once the
2180 * VFS layer lookup-intents are implemented.
2181 *
2182 * Close is called when the inode is destroyed.
2183 * If we haven't opened the file for O_WRONLY, we
2184 * need to in the size_change case to obtain a stateid.
2185 *
2186 * Got race?
2187 * Because OPEN is always done by name in nfsv4, it is
2188 * possible that we opened a different file by the same
2189 * name. We can recognize this race condition, but we
2190 * can't do anything about it besides returning an error.
2191 *
2192 * This will be fixed with VFS changes (lookup-intent).
2193 */
2194static int
2195nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2196 struct iattr *sattr)
2197{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002198 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002199 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002200 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 int status;
2202
Trond Myklebust0e574af2005-10-27 22:12:38 -04002203 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Trond Myklebustd5308382005-11-04 15:33:38 -05002205 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002206 if (sattr->ia_valid & ATTR_FILE) {
2207 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002208
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002209 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002210 if (ctx) {
2211 cred = ctx->cred;
2212 state = ctx->state;
2213 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002214 }
2215
2216 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002217 if (status == 0)
2218 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 return status;
2220}
2221
Trond Myklebust56659e92007-07-17 21:52:39 -04002222static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2223 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002224 struct nfs_fattr *fattr)
2225{
2226 int status;
2227 struct nfs4_lookup_arg args = {
2228 .bitmask = server->attr_bitmask,
2229 .dir_fh = dirfh,
2230 .name = name,
2231 };
2232 struct nfs4_lookup_res res = {
2233 .server = server,
2234 .fattr = fattr,
2235 .fh = fhandle,
2236 };
2237 struct rpc_message msg = {
2238 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2239 .rpc_argp = &args,
2240 .rpc_resp = &res,
2241 };
2242
2243 nfs_fattr_init(fattr);
2244
2245 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002246 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002247 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002248 return status;
2249}
2250
2251static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2252 struct qstr *name, struct nfs_fh *fhandle,
2253 struct nfs_fattr *fattr)
2254{
2255 struct nfs4_exception exception = { };
2256 int err;
2257 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002258 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2259 /* FIXME: !!!! */
2260 if (err == -NFS4ERR_MOVED) {
2261 err = -EREMOTE;
2262 break;
2263 }
2264 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002265 } while (exception.retry);
2266 return err;
2267}
2268
Trond Myklebust56659e92007-07-17 21:52:39 -04002269static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2271{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002272 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
2274 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002275 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002276 if (status == -NFS4ERR_MOVED)
2277 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 dprintk("NFS reply lookup: %d\n", status);
2279 return status;
2280}
2281
2282static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2283{
2284 struct nfs4_exception exception = { };
2285 int err;
2286 do {
2287 err = nfs4_handle_exception(NFS_SERVER(dir),
2288 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2289 &exception);
2290 } while (exception.retry);
2291 return err;
2292}
2293
2294static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2295{
Trond Myklebust76b32992007-08-10 17:45:11 -04002296 struct nfs_server *server = NFS_SERVER(inode);
2297 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 struct nfs4_accessargs args = {
2299 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002300 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002302 struct nfs4_accessres res = {
2303 .server = server,
2304 .fattr = &fattr,
2305 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 struct rpc_message msg = {
2307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2308 .rpc_argp = &args,
2309 .rpc_resp = &res,
2310 .rpc_cred = entry->cred,
2311 };
2312 int mode = entry->mask;
2313 int status;
2314
2315 /*
2316 * Determine which access bits we want to ask for...
2317 */
2318 if (mode & MAY_READ)
2319 args.access |= NFS4_ACCESS_READ;
2320 if (S_ISDIR(inode->i_mode)) {
2321 if (mode & MAY_WRITE)
2322 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2323 if (mode & MAY_EXEC)
2324 args.access |= NFS4_ACCESS_LOOKUP;
2325 } else {
2326 if (mode & MAY_WRITE)
2327 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2328 if (mode & MAY_EXEC)
2329 args.access |= NFS4_ACCESS_EXECUTE;
2330 }
Trond Myklebust76b32992007-08-10 17:45:11 -04002331 nfs_fattr_init(&fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002332 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 if (!status) {
2334 entry->mask = 0;
2335 if (res.access & NFS4_ACCESS_READ)
2336 entry->mask |= MAY_READ;
2337 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2338 entry->mask |= MAY_WRITE;
2339 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2340 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04002341 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 }
2343 return status;
2344}
2345
2346static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2347{
2348 struct nfs4_exception exception = { };
2349 int err;
2350 do {
2351 err = nfs4_handle_exception(NFS_SERVER(inode),
2352 _nfs4_proc_access(inode, entry),
2353 &exception);
2354 } while (exception.retry);
2355 return err;
2356}
2357
2358/*
2359 * TODO: For the time being, we don't try to get any attributes
2360 * along with any of the zero-copy operations READ, READDIR,
2361 * READLINK, WRITE.
2362 *
2363 * In the case of the first three, we want to put the GETATTR
2364 * after the read-type operation -- this is because it is hard
2365 * to predict the length of a GETATTR response in v4, and thus
2366 * align the READ data correctly. This means that the GETATTR
2367 * may end up partially falling into the page cache, and we should
2368 * shift it into the 'tail' of the xdr_buf before processing.
2369 * To do this efficiently, we need to know the total length
2370 * of data received, which doesn't seem to be available outside
2371 * of the RPC layer.
2372 *
2373 * In the case of WRITE, we also want to put the GETATTR after
2374 * the operation -- in this case because we want to make sure
2375 * we get the post-operation mtime and size. This means that
2376 * we can't use xdr_encode_pages() as written: we need a variant
2377 * of it which would leave room in the 'tail' iovec.
2378 *
2379 * Both of these changes to the XDR layer would in fact be quite
2380 * minor, but I decided to leave them for a subsequent patch.
2381 */
2382static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2383 unsigned int pgbase, unsigned int pglen)
2384{
2385 struct nfs4_readlink args = {
2386 .fh = NFS_FH(inode),
2387 .pgbase = pgbase,
2388 .pglen = pglen,
2389 .pages = &page,
2390 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002391 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 struct rpc_message msg = {
2393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2394 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002395 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 };
2397
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002398 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399}
2400
2401static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2402 unsigned int pgbase, unsigned int pglen)
2403{
2404 struct nfs4_exception exception = { };
2405 int err;
2406 do {
2407 err = nfs4_handle_exception(NFS_SERVER(inode),
2408 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2409 &exception);
2410 } while (exception.retry);
2411 return err;
2412}
2413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414/*
2415 * Got race?
2416 * We will need to arrange for the VFS layer to provide an atomic open.
2417 * Until then, this create/open method is prone to inefficiency and race
2418 * conditions due to the lookup, create, and open VFS calls from sys_open()
2419 * placed on the wire.
2420 *
2421 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2422 * The file will be opened again in the subsequent VFS open call
2423 * (nfs4_proc_file_open).
2424 *
2425 * The open for read will just hang around to be used by any process that
2426 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2427 */
2428
2429static int
2430nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002431 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
Trond Myklebustad389da2007-06-05 12:30:00 -04002433 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002434 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002435 .dentry = dentry,
2436 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 struct nfs4_state *state;
2438 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002439 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 int status = 0;
2441
Trond Myklebust98a8e322008-03-12 12:25:28 -04002442 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 if (IS_ERR(cred)) {
2444 status = PTR_ERR(cred);
2445 goto out;
2446 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002447 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002448 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 if (IS_ERR(state)) {
2450 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002451 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002453 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002454 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 if (flags & O_EXCL) {
2456 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002457 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002458 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002459 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002460 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002461 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002462 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002463 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002464 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002465 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002466out_putcred:
2467 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468out:
2469 return status;
2470}
2471
2472static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2473{
Trond Myklebust16e42952005-10-27 22:12:44 -04002474 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002475 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002477 .name.len = name->len,
2478 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002479 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002481 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002482 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002483 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2486 .rpc_argp = &args,
2487 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 };
2489 int status;
2490
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002491 nfs_fattr_init(&res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002492 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002493 if (status == 0) {
2494 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002495 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 return status;
2498}
2499
2500static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2501{
2502 struct nfs4_exception exception = { };
2503 int err;
2504 do {
2505 err = nfs4_handle_exception(NFS_SERVER(dir),
2506 _nfs4_proc_remove(dir, name),
2507 &exception);
2508 } while (exception.retry);
2509 return err;
2510}
2511
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002512static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002514 struct nfs_server *server = NFS_SERVER(dir);
2515 struct nfs_removeargs *args = msg->rpc_argp;
2516 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Trond Myklebusta65318b2009-03-11 14:10:28 -04002518 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002519 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521}
2522
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002523static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002525 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2526
Andy Adamson472cfbd2009-04-01 09:22:24 -04002527 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002528 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002529 return 0;
Andy Adamson472cfbd2009-04-01 09:22:24 -04002530 nfs4_sequence_free_slot(res->server->nfs_client, &res->seq_res);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002531 update_changeattr(dir, &res->cinfo);
2532 nfs_post_op_update_inode(dir, &res->dir_attr);
2533 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534}
2535
2536static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2537 struct inode *new_dir, struct qstr *new_name)
2538{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002539 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 struct nfs4_rename_arg arg = {
2541 .old_dir = NFS_FH(old_dir),
2542 .new_dir = NFS_FH(new_dir),
2543 .old_name = old_name,
2544 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002545 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002547 struct nfs_fattr old_fattr, new_fattr;
2548 struct nfs4_rename_res res = {
2549 .server = server,
2550 .old_fattr = &old_fattr,
2551 .new_fattr = &new_fattr,
2552 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 struct rpc_message msg = {
2554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2555 .rpc_argp = &arg,
2556 .rpc_resp = &res,
2557 };
2558 int status;
2559
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002560 nfs_fattr_init(res.old_fattr);
2561 nfs_fattr_init(res.new_fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002562 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
2564 if (!status) {
2565 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002566 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002568 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 }
2570 return status;
2571}
2572
2573static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2574 struct inode *new_dir, struct qstr *new_name)
2575{
2576 struct nfs4_exception exception = { };
2577 int err;
2578 do {
2579 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2580 _nfs4_proc_rename(old_dir, old_name,
2581 new_dir, new_name),
2582 &exception);
2583 } while (exception.retry);
2584 return err;
2585}
2586
2587static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2588{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002589 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 struct nfs4_link_arg arg = {
2591 .fh = NFS_FH(inode),
2592 .dir_fh = NFS_FH(dir),
2593 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002594 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002596 struct nfs_fattr fattr, dir_attr;
2597 struct nfs4_link_res res = {
2598 .server = server,
2599 .fattr = &fattr,
2600 .dir_attr = &dir_attr,
2601 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 struct rpc_message msg = {
2603 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2604 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002605 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 };
2607 int status;
2608
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002609 nfs_fattr_init(res.fattr);
2610 nfs_fattr_init(res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002611 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002612 if (!status) {
2613 update_changeattr(dir, &res.cinfo);
2614 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002615 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
2618 return status;
2619}
2620
2621static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2622{
2623 struct nfs4_exception exception = { };
2624 int err;
2625 do {
2626 err = nfs4_handle_exception(NFS_SERVER(inode),
2627 _nfs4_proc_link(inode, dir, name),
2628 &exception);
2629 } while (exception.retry);
2630 return err;
2631}
2632
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002633struct nfs4_createdata {
2634 struct rpc_message msg;
2635 struct nfs4_create_arg arg;
2636 struct nfs4_create_res res;
2637 struct nfs_fh fh;
2638 struct nfs_fattr fattr;
2639 struct nfs_fattr dir_fattr;
2640};
2641
2642static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2643 struct qstr *name, struct iattr *sattr, u32 ftype)
2644{
2645 struct nfs4_createdata *data;
2646
2647 data = kzalloc(sizeof(*data), GFP_KERNEL);
2648 if (data != NULL) {
2649 struct nfs_server *server = NFS_SERVER(dir);
2650
2651 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2652 data->msg.rpc_argp = &data->arg;
2653 data->msg.rpc_resp = &data->res;
2654 data->arg.dir_fh = NFS_FH(dir);
2655 data->arg.server = server;
2656 data->arg.name = name;
2657 data->arg.attrs = sattr;
2658 data->arg.ftype = ftype;
2659 data->arg.bitmask = server->attr_bitmask;
2660 data->res.server = server;
2661 data->res.fh = &data->fh;
2662 data->res.fattr = &data->fattr;
2663 data->res.dir_fattr = &data->dir_fattr;
2664 nfs_fattr_init(data->res.fattr);
2665 nfs_fattr_init(data->res.dir_fattr);
2666 }
2667 return data;
2668}
2669
2670static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2671{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002672 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2673 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002674 if (status == 0) {
2675 update_changeattr(dir, &data->res.dir_cinfo);
2676 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2677 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2678 }
2679 return status;
2680}
2681
2682static void nfs4_free_createdata(struct nfs4_createdata *data)
2683{
2684 kfree(data);
2685}
2686
Chuck Lever4f390c12006-08-22 20:06:22 -04002687static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002688 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002690 struct nfs4_createdata *data;
2691 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
Chuck Lever94a6d752006-08-22 20:06:23 -04002693 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002694 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002695
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002696 status = -ENOMEM;
2697 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2698 if (data == NULL)
2699 goto out;
2700
2701 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2702 data->arg.u.symlink.pages = &page;
2703 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002705 status = nfs4_do_create(dir, dentry, data);
2706
2707 nfs4_free_createdata(data);
2708out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 return status;
2710}
2711
Chuck Lever4f390c12006-08-22 20:06:22 -04002712static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002713 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714{
2715 struct nfs4_exception exception = { };
2716 int err;
2717 do {
2718 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002719 _nfs4_proc_symlink(dir, dentry, page,
2720 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 &exception);
2722 } while (exception.retry);
2723 return err;
2724}
2725
2726static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2727 struct iattr *sattr)
2728{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002729 struct nfs4_createdata *data;
2730 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002732 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2733 if (data == NULL)
2734 goto out;
2735
2736 status = nfs4_do_create(dir, dentry, data);
2737
2738 nfs4_free_createdata(data);
2739out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 return status;
2741}
2742
2743static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2744 struct iattr *sattr)
2745{
2746 struct nfs4_exception exception = { };
2747 int err;
2748 do {
2749 err = nfs4_handle_exception(NFS_SERVER(dir),
2750 _nfs4_proc_mkdir(dir, dentry, sattr),
2751 &exception);
2752 } while (exception.retry);
2753 return err;
2754}
2755
2756static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2757 u64 cookie, struct page *page, unsigned int count, int plus)
2758{
2759 struct inode *dir = dentry->d_inode;
2760 struct nfs4_readdir_arg args = {
2761 .fh = NFS_FH(dir),
2762 .pages = &page,
2763 .pgbase = 0,
2764 .count = count,
Trond Myklebusta65318b2009-03-11 14:10:28 -04002765 .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 };
2767 struct nfs4_readdir_res res;
2768 struct rpc_message msg = {
2769 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2770 .rpc_argp = &args,
2771 .rpc_resp = &res,
2772 .rpc_cred = cred,
2773 };
2774 int status;
2775
Harvey Harrison3110ff82008-05-02 13:42:44 -07002776 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002777 dentry->d_parent->d_name.name,
2778 dentry->d_name.name,
2779 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2781 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002782 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 if (status == 0)
2784 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002785
2786 nfs_invalidate_atime(dir);
2787
Harvey Harrison3110ff82008-05-02 13:42:44 -07002788 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 return status;
2790}
2791
2792static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2793 u64 cookie, struct page *page, unsigned int count, int plus)
2794{
2795 struct nfs4_exception exception = { };
2796 int err;
2797 do {
2798 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2799 _nfs4_proc_readdir(dentry, cred, cookie,
2800 page, count, plus),
2801 &exception);
2802 } while (exception.retry);
2803 return err;
2804}
2805
2806static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2807 struct iattr *sattr, dev_t rdev)
2808{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002809 struct nfs4_createdata *data;
2810 int mode = sattr->ia_mode;
2811 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
2813 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2814 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002815
2816 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2817 if (data == NULL)
2818 goto out;
2819
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002821 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002823 data->arg.ftype = NF4BLK;
2824 data->arg.u.device.specdata1 = MAJOR(rdev);
2825 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 }
2827 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002828 data->arg.ftype = NF4CHR;
2829 data->arg.u.device.specdata1 = MAJOR(rdev);
2830 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002833 status = nfs4_do_create(dir, dentry, data);
2834
2835 nfs4_free_createdata(data);
2836out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 return status;
2838}
2839
2840static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2841 struct iattr *sattr, dev_t rdev)
2842{
2843 struct nfs4_exception exception = { };
2844 int err;
2845 do {
2846 err = nfs4_handle_exception(NFS_SERVER(dir),
2847 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2848 &exception);
2849 } while (exception.retry);
2850 return err;
2851}
2852
2853static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2854 struct nfs_fsstat *fsstat)
2855{
2856 struct nfs4_statfs_arg args = {
2857 .fh = fhandle,
2858 .bitmask = server->attr_bitmask,
2859 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002860 struct nfs4_statfs_res res = {
2861 .fsstat = fsstat,
2862 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 struct rpc_message msg = {
2864 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2865 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002866 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 };
2868
Trond Myklebust0e574af2005-10-27 22:12:38 -04002869 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002870 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871}
2872
2873static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2874{
2875 struct nfs4_exception exception = { };
2876 int err;
2877 do {
2878 err = nfs4_handle_exception(server,
2879 _nfs4_proc_statfs(server, fhandle, fsstat),
2880 &exception);
2881 } while (exception.retry);
2882 return err;
2883}
2884
2885static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2886 struct nfs_fsinfo *fsinfo)
2887{
2888 struct nfs4_fsinfo_arg args = {
2889 .fh = fhandle,
2890 .bitmask = server->attr_bitmask,
2891 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002892 struct nfs4_fsinfo_res res = {
2893 .fsinfo = fsinfo,
2894 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 struct rpc_message msg = {
2896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2897 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002898 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 };
2900
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002901 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902}
2903
2904static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2905{
2906 struct nfs4_exception exception = { };
2907 int err;
2908
2909 do {
2910 err = nfs4_handle_exception(server,
2911 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2912 &exception);
2913 } while (exception.retry);
2914 return err;
2915}
2916
2917static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2918{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002919 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2921}
2922
2923static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2924 struct nfs_pathconf *pathconf)
2925{
2926 struct nfs4_pathconf_arg args = {
2927 .fh = fhandle,
2928 .bitmask = server->attr_bitmask,
2929 };
Benny Halevyd45b2982009-04-01 09:21:58 -04002930 struct nfs4_pathconf_res res = {
2931 .pathconf = pathconf,
2932 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 struct rpc_message msg = {
2934 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2935 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04002936 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 };
2938
2939 /* None of the pathconf attributes are mandatory to implement */
2940 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2941 memset(pathconf, 0, sizeof(*pathconf));
2942 return 0;
2943 }
2944
Trond Myklebust0e574af2005-10-27 22:12:38 -04002945 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002946 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947}
2948
2949static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2950 struct nfs_pathconf *pathconf)
2951{
2952 struct nfs4_exception exception = { };
2953 int err;
2954
2955 do {
2956 err = nfs4_handle_exception(server,
2957 _nfs4_proc_pathconf(server, fhandle, pathconf),
2958 &exception);
2959 } while (exception.retry);
2960 return err;
2961}
2962
Trond Myklebustec06c092006-03-20 13:44:27 -05002963static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964{
Trond Myklebustec06c092006-03-20 13:44:27 -05002965 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Andy Adamsonf11c88a2009-04-01 09:22:25 -04002967 dprintk("--> %s\n", __func__);
2968
2969 /* nfs4_sequence_free_slot called in the read rpc_call_done */
2970 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
2971
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002972 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Andy Adamsoneedc0202009-04-01 09:22:41 -04002973 nfs4_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05002974 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 }
Trond Myklebust8850df92007-09-28 17:20:07 -04002976
2977 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002979 renew_lease(server, data->timestamp);
2980 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981}
2982
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002983static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002986 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987}
2988
Trond Myklebust788e7a82006-03-20 13:44:27 -05002989static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 struct inode *inode = data->inode;
2992
Andy Adamsondef6ed72009-04-01 09:22:26 -04002993 /* slot is freed in nfs_writeback_done */
2994 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
2995 task->tk_status);
2996
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002997 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Andy Adamsoneedc0202009-04-01 09:22:41 -04002998 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002999 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003001 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003003 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003004 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003005 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006}
3007
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003008static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003010 struct nfs_server *server = NFS_SERVER(data->inode);
3011
Trond Myklebusta65318b2009-03-11 14:10:28 -04003012 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003013 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 data->timestamp = jiffies;
3015
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003016 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017}
3018
Trond Myklebust788e7a82006-03-20 13:44:27 -05003019static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 struct inode *inode = data->inode;
3022
Andy Adamson21d9a852009-04-01 09:22:27 -04003023 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3024 task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003025 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Andy Adamsoneedc0202009-04-01 09:22:41 -04003026 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003027 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 }
Andy Adamson21d9a852009-04-01 09:22:27 -04003029 nfs4_sequence_free_slot(NFS_SERVER(inode)->nfs_client,
3030 &data->res.seq_res);
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003031 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033}
3034
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003035static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003037 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Trond Myklebusta65318b2009-03-11 14:10:28 -04003039 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003040 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003041 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042}
3043
3044/*
3045 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3046 * standalone procedure for queueing an asynchronous RENEW.
3047 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003048static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
David Howellsadfa6f92006-08-22 20:06:08 -04003050 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003051 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
3053 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003054 /* Unless we're shutting down, schedule state recovery! */
3055 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3056 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 return;
3058 }
3059 spin_lock(&clp->cl_lock);
3060 if (time_before(clp->cl_last_renewal,timestamp))
3061 clp->cl_last_renewal = timestamp;
3062 spin_unlock(&clp->cl_lock);
Benny Halevy29fba382009-04-01 09:22:44 -04003063 dprintk("%s calling put_rpccred on rpc_cred %p\n", __func__,
3064 task->tk_msg.rpc_cred);
3065 put_rpccred(task->tk_msg.rpc_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066}
3067
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003068static const struct rpc_call_ops nfs4_renew_ops = {
3069 .rpc_call_done = nfs4_renew_done,
3070};
3071
David Howellsadfa6f92006-08-22 20:06:08 -04003072int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073{
3074 struct rpc_message msg = {
3075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3076 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003077 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 };
3079
3080 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003081 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082}
3083
David Howellsadfa6f92006-08-22 20:06:08 -04003084int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085{
3086 struct rpc_message msg = {
3087 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3088 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003089 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 };
3091 unsigned long now = jiffies;
3092 int status;
3093
3094 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3095 if (status < 0)
3096 return status;
3097 spin_lock(&clp->cl_lock);
3098 if (time_before(clp->cl_last_renewal,now))
3099 clp->cl_last_renewal = now;
3100 spin_unlock(&clp->cl_lock);
3101 return 0;
3102}
3103
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003104static inline int nfs4_server_supports_acls(struct nfs_server *server)
3105{
3106 return (server->caps & NFS_CAP_ACLS)
3107 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3108 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3109}
3110
3111/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3112 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3113 * the stack.
3114 */
3115#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3116
3117static void buf_to_pages(const void *buf, size_t buflen,
3118 struct page **pages, unsigned int *pgbase)
3119{
3120 const void *p = buf;
3121
3122 *pgbase = offset_in_page(buf);
3123 p -= *pgbase;
3124 while (p < buf + buflen) {
3125 *(pages++) = virt_to_page(p);
3126 p += PAGE_CACHE_SIZE;
3127 }
3128}
3129
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003130struct nfs4_cached_acl {
3131 int cached;
3132 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003133 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003134};
3135
3136static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003137{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003138 struct nfs_inode *nfsi = NFS_I(inode);
3139
3140 spin_lock(&inode->i_lock);
3141 kfree(nfsi->nfs4_acl);
3142 nfsi->nfs4_acl = acl;
3143 spin_unlock(&inode->i_lock);
3144}
3145
3146static void nfs4_zap_acl_attr(struct inode *inode)
3147{
3148 nfs4_set_cached_acl(inode, NULL);
3149}
3150
3151static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3152{
3153 struct nfs_inode *nfsi = NFS_I(inode);
3154 struct nfs4_cached_acl *acl;
3155 int ret = -ENOENT;
3156
3157 spin_lock(&inode->i_lock);
3158 acl = nfsi->nfs4_acl;
3159 if (acl == NULL)
3160 goto out;
3161 if (buf == NULL) /* user is just asking for length */
3162 goto out_len;
3163 if (acl->cached == 0)
3164 goto out;
3165 ret = -ERANGE; /* see getxattr(2) man page */
3166 if (acl->len > buflen)
3167 goto out;
3168 memcpy(buf, acl->data, acl->len);
3169out_len:
3170 ret = acl->len;
3171out:
3172 spin_unlock(&inode->i_lock);
3173 return ret;
3174}
3175
3176static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3177{
3178 struct nfs4_cached_acl *acl;
3179
3180 if (buf && acl_len <= PAGE_SIZE) {
3181 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3182 if (acl == NULL)
3183 goto out;
3184 acl->cached = 1;
3185 memcpy(acl->data, buf, acl_len);
3186 } else {
3187 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3188 if (acl == NULL)
3189 goto out;
3190 acl->cached = 0;
3191 }
3192 acl->len = acl_len;
3193out:
3194 nfs4_set_cached_acl(inode, acl);
3195}
3196
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003197static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003198{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003199 struct page *pages[NFS4ACL_MAXPAGES];
3200 struct nfs_getaclargs args = {
3201 .fh = NFS_FH(inode),
3202 .acl_pages = pages,
3203 .acl_len = buflen,
3204 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003205 struct nfs_getaclres res = {
3206 .acl_len = buflen,
3207 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003208 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003209 struct rpc_message msg = {
3210 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3211 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003212 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003213 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003214 struct page *localpage = NULL;
3215 int ret;
3216
3217 if (buflen < PAGE_SIZE) {
3218 /* As long as we're doing a round trip to the server anyway,
3219 * let's be prepared for a page of acl data. */
3220 localpage = alloc_page(GFP_KERNEL);
3221 resp_buf = page_address(localpage);
3222 if (localpage == NULL)
3223 return -ENOMEM;
3224 args.acl_pages[0] = localpage;
3225 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003226 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003227 } else {
3228 resp_buf = buf;
3229 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3230 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003231 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003232 if (ret)
3233 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003234 if (res.acl_len > args.acl_len)
3235 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003236 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003237 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003238 if (buf) {
3239 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003240 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003241 goto out_free;
3242 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003243 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003244 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003245 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003246out_free:
3247 if (localpage)
3248 __free_page(localpage);
3249 return ret;
3250}
3251
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003252static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3253{
3254 struct nfs4_exception exception = { };
3255 ssize_t ret;
3256 do {
3257 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3258 if (ret >= 0)
3259 break;
3260 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3261 } while (exception.retry);
3262 return ret;
3263}
3264
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003265static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3266{
3267 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003268 int ret;
3269
3270 if (!nfs4_server_supports_acls(server))
3271 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003272 ret = nfs_revalidate_inode(server, inode);
3273 if (ret < 0)
3274 return ret;
3275 ret = nfs4_read_cached_acl(inode, buf, buflen);
3276 if (ret != -ENOENT)
3277 return ret;
3278 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003279}
3280
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003281static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003282{
3283 struct nfs_server *server = NFS_SERVER(inode);
3284 struct page *pages[NFS4ACL_MAXPAGES];
3285 struct nfs_setaclargs arg = {
3286 .fh = NFS_FH(inode),
3287 .acl_pages = pages,
3288 .acl_len = buflen,
3289 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003290 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003291 struct rpc_message msg = {
3292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3293 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003294 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003295 };
3296 int ret;
3297
3298 if (!nfs4_server_supports_acls(server))
3299 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003300 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003301 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003302 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003303 nfs_access_zap_cache(inode);
3304 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003305 return ret;
3306}
3307
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003308static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3309{
3310 struct nfs4_exception exception = { };
3311 int err;
3312 do {
3313 err = nfs4_handle_exception(NFS_SERVER(inode),
3314 __nfs4_proc_set_acl(inode, buf, buflen),
3315 &exception);
3316 } while (exception.retry);
3317 return err;
3318}
3319
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320static int
Andy Adamson8328d592009-04-01 09:22:35 -04003321_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 -07003322{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 if (!clp || task->tk_status >= 0)
3324 return 0;
3325 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003326 case -NFS4ERR_ADMIN_REVOKED:
3327 case -NFS4ERR_BAD_STATEID:
3328 case -NFS4ERR_OPENMODE:
3329 if (state == NULL)
3330 break;
3331 nfs4_state_mark_reclaim_nograce(clp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 case -NFS4ERR_STALE_CLIENTID:
3333 case -NFS4ERR_STALE_STATEID:
3334 case -NFS4ERR_EXPIRED:
Trond Myklebust5d008372008-02-22 16:34:17 -05003335 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 nfs4_schedule_state_recovery(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05003337 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
Trond Myklebustfda13932008-02-22 16:34:12 -05003338 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 task->tk_status = 0;
3340 return -EAGAIN;
Andy Adamson4745e312009-04-01 09:22:42 -04003341#if defined(CONFIG_NFS_V4_1)
3342 case -NFS4ERR_BADSESSION:
3343 case -NFS4ERR_BADSLOT:
3344 case -NFS4ERR_BAD_HIGH_SLOT:
3345 case -NFS4ERR_DEADSESSION:
3346 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3347 case -NFS4ERR_SEQ_FALSE_RETRY:
3348 case -NFS4ERR_SEQ_MISORDERED:
3349 dprintk("%s ERROR %d, Reset session\n", __func__,
3350 task->tk_status);
3351 set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
3352 task->tk_status = 0;
3353 return -EAGAIN;
3354#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 case -NFS4ERR_DELAY:
Andy Adamson8328d592009-04-01 09:22:35 -04003356 if (server)
3357 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003358 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3360 task->tk_status = 0;
3361 return -EAGAIN;
3362 case -NFS4ERR_OLD_STATEID:
3363 task->tk_status = 0;
3364 return -EAGAIN;
3365 }
3366 task->tk_status = nfs4_map_errors(task->tk_status);
3367 return 0;
3368}
3369
Andy Adamson8328d592009-04-01 09:22:35 -04003370static int
3371nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3372{
3373 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3374}
3375
David Howellsadfa6f92006-08-22 20:06:08 -04003376int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377{
3378 nfs4_verifier sc_verifier;
3379 struct nfs4_setclientid setclientid = {
3380 .sc_verifier = &sc_verifier,
3381 .sc_prog = program,
3382 };
3383 struct rpc_message msg = {
3384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3385 .rpc_argp = &setclientid,
3386 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003387 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 };
Al Virobc4785c2006-10-19 23:28:51 -07003389 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 int loop = 0;
3391 int status;
3392
Al Virobc4785c2006-10-19 23:28:51 -07003393 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3395 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3396
3397 for(;;) {
3398 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003399 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003400 clp->cl_ipaddr,
3401 rpc_peeraddr2str(clp->cl_rpcclient,
3402 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003403 rpc_peeraddr2str(clp->cl_rpcclient,
3404 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003405 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 clp->cl_id_uniquifier);
3407 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003408 sizeof(setclientid.sc_netid),
3409 rpc_peeraddr2str(clp->cl_rpcclient,
3410 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003412 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 clp->cl_ipaddr, port >> 8, port & 255);
3414
3415 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3416 if (status != -NFS4ERR_CLID_INUSE)
3417 break;
3418 if (signalled())
3419 break;
3420 if (loop++ & 1)
3421 ssleep(clp->cl_lease_time + 1);
3422 else
3423 if (++clp->cl_id_uniquifier == 0)
3424 break;
3425 }
3426 return status;
3427}
3428
David Howellsadfa6f92006-08-22 20:06:08 -04003429static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430{
3431 struct nfs_fsinfo fsinfo;
3432 struct rpc_message msg = {
3433 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3434 .rpc_argp = clp,
3435 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003436 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 };
3438 unsigned long now;
3439 int status;
3440
3441 now = jiffies;
3442 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3443 if (status == 0) {
3444 spin_lock(&clp->cl_lock);
3445 clp->cl_lease_time = fsinfo.lease_time * HZ;
3446 clp->cl_last_renewal = now;
3447 spin_unlock(&clp->cl_lock);
3448 }
3449 return status;
3450}
3451
David Howellsadfa6f92006-08-22 20:06:08 -04003452int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003453{
Benny Halevy77e03672008-06-24 20:25:57 +03003454 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003455 int err;
3456 do {
3457 err = _nfs4_proc_setclientid_confirm(clp, cred);
3458 switch (err) {
3459 case 0:
3460 return err;
3461 case -NFS4ERR_RESOURCE:
3462 /* The IBM lawyers misread another document! */
3463 case -NFS4ERR_DELAY:
3464 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3465 }
3466 } while (err == 0);
3467 return err;
3468}
3469
Trond Myklebustfe650402006-01-03 09:55:18 +01003470struct nfs4_delegreturndata {
3471 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003472 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003473 struct nfs_fh fh;
3474 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003475 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003476 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003477 int rpc_status;
3478};
3479
Trond Myklebustfe650402006-01-03 09:55:18 +01003480static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3481{
3482 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003483
3484 nfs4_sequence_done_free_slot(data->res.server, &data->res.seq_res,
3485 task->tk_status);
3486
Trond Myklebustfe650402006-01-03 09:55:18 +01003487 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003488 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01003489 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01003490}
3491
3492static void nfs4_delegreturn_release(void *calldata)
3493{
Trond Myklebustfe650402006-01-03 09:55:18 +01003494 kfree(calldata);
3495}
3496
Andy Adamson938e1012009-04-01 09:22:28 -04003497#if defined(CONFIG_NFS_V4_1)
3498static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3499{
3500 struct nfs4_delegreturndata *d_data;
3501
3502 d_data = (struct nfs4_delegreturndata *)data;
3503
3504 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3505 &d_data->args.seq_args,
3506 &d_data->res.seq_res, 1, task))
3507 return;
3508 rpc_call_start(task);
3509}
3510#endif /* CONFIG_NFS_V4_1 */
3511
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003512static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003513#if defined(CONFIG_NFS_V4_1)
3514 .rpc_call_prepare = nfs4_delegreturn_prepare,
3515#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003516 .rpc_call_done = nfs4_delegreturn_done,
3517 .rpc_release = nfs4_delegreturn_release,
3518};
3519
Trond Myklebuste6f81072008-01-24 18:14:34 -05003520static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003521{
3522 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003523 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003524 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003525 struct rpc_message msg = {
3526 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3527 .rpc_cred = cred,
3528 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003529 struct rpc_task_setup task_setup_data = {
3530 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003531 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003532 .callback_ops = &nfs4_delegreturn_ops,
3533 .flags = RPC_TASK_ASYNC,
3534 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003535 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003536
Andy Adamson938e1012009-04-01 09:22:28 -04003537 data = kzalloc(sizeof(*data), GFP_KERNEL);
Trond Myklebustfe650402006-01-03 09:55:18 +01003538 if (data == NULL)
3539 return -ENOMEM;
3540 data->args.fhandle = &data->fh;
3541 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003542 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003543 nfs_copy_fh(&data->fh, NFS_FH(inode));
3544 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003545 data->res.fattr = &data->fattr;
3546 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003547 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003548 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003549 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003550 data->rpc_status = 0;
3551
Trond Myklebustc970aa82007-07-14 15:39:59 -04003552 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003553 msg.rpc_argp = &data->args,
3554 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003555 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003556 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003557 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003558 if (!issync)
3559 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003560 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003561 if (status != 0)
3562 goto out;
3563 status = data->rpc_status;
3564 if (status != 0)
3565 goto out;
3566 nfs_refresh_inode(inode, &data->fattr);
3567out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003568 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003569 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570}
3571
Trond Myklebuste6f81072008-01-24 18:14:34 -05003572int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573{
3574 struct nfs_server *server = NFS_SERVER(inode);
3575 struct nfs4_exception exception = { };
3576 int err;
3577 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003578 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 switch (err) {
3580 case -NFS4ERR_STALE_STATEID:
3581 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 case 0:
3583 return 0;
3584 }
3585 err = nfs4_handle_exception(server, err, &exception);
3586 } while (exception.retry);
3587 return err;
3588}
3589
3590#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3591#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3592
3593/*
3594 * sleep, with exponential backoff, and retry the LOCK operation.
3595 */
3596static unsigned long
3597nfs4_set_lock_task_retry(unsigned long timeout)
3598{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003599 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 timeout <<= 1;
3601 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3602 return NFS4_LOCK_MAXTIMEOUT;
3603 return timeout;
3604}
3605
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3607{
3608 struct inode *inode = state->inode;
3609 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003610 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003611 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003613 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003615 struct nfs_lockt_res res = {
3616 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 };
3618 struct rpc_message msg = {
3619 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3620 .rpc_argp = &arg,
3621 .rpc_resp = &res,
3622 .rpc_cred = state->owner->so_cred,
3623 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 struct nfs4_lock_state *lsp;
3625 int status;
3626
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003627 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003628 status = nfs4_set_lock_state(state, request);
3629 if (status != 0)
3630 goto out;
3631 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003632 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003633 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003634 switch (status) {
3635 case 0:
3636 request->fl_type = F_UNLCK;
3637 break;
3638 case -NFS4ERR_DENIED:
3639 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003641 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003642out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 return status;
3644}
3645
3646static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3647{
3648 struct nfs4_exception exception = { };
3649 int err;
3650
3651 do {
3652 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3653 _nfs4_proc_getlk(state, cmd, request),
3654 &exception);
3655 } while (exception.retry);
3656 return err;
3657}
3658
3659static int do_vfs_lock(struct file *file, struct file_lock *fl)
3660{
3661 int res = 0;
3662 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3663 case FL_POSIX:
3664 res = posix_lock_file_wait(file, fl);
3665 break;
3666 case FL_FLOCK:
3667 res = flock_lock_file_wait(file, fl);
3668 break;
3669 default:
3670 BUG();
3671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 return res;
3673}
3674
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003675struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003676 struct nfs_locku_args arg;
3677 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003678 struct nfs4_lock_state *lsp;
3679 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003680 struct file_lock fl;
3681 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003682 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003683};
3684
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003685static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3686 struct nfs_open_context *ctx,
3687 struct nfs4_lock_state *lsp,
3688 struct nfs_seqid *seqid)
3689{
3690 struct nfs4_unlockdata *p;
3691 struct inode *inode = lsp->ls_state->inode;
3692
Andy Adamsona8936932009-04-01 09:22:23 -04003693 p = kzalloc(sizeof(*p), GFP_KERNEL);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003694 if (p == NULL)
3695 return NULL;
3696 p->arg.fh = NFS_FH(inode);
3697 p->arg.fl = &p->fl;
3698 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003699 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003700 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003701 p->arg.stateid = &lsp->ls_stateid;
3702 p->lsp = lsp;
3703 atomic_inc(&lsp->ls_count);
3704 /* Ensure we don't close file until we're done freeing locks! */
3705 p->ctx = get_nfs_open_context(ctx);
3706 memcpy(&p->fl, fl, sizeof(p->fl));
3707 p->server = NFS_SERVER(inode);
3708 return p;
3709}
3710
Trond Myklebust06f814a2006-01-03 09:55:07 +01003711static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003712{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003713 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003714 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003715 nfs4_put_lock_state(calldata->lsp);
3716 put_nfs_open_context(calldata->ctx);
3717 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003718}
3719
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003720static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003721{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003722 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003723
Andy Adamsona8936932009-04-01 09:22:23 -04003724 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3725 task->tk_status);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003726 if (RPC_ASSASSINATED(task))
3727 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003728 switch (task->tk_status) {
3729 case 0:
3730 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003731 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003732 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003733 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003734 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003735 case -NFS4ERR_BAD_STATEID:
3736 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003737 case -NFS4ERR_STALE_STATEID:
3738 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003739 break;
3740 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003741 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Andy Adamsoneedc0202009-04-01 09:22:41 -04003742 nfs4_restart_rpc(task,
3743 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003744 }
Andy Adamsona8936932009-04-01 09:22:23 -04003745 nfs4_sequence_free_slot(calldata->server->nfs_client,
3746 &calldata->res.seq_res);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003747}
3748
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003749static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003750{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003751 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003753 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003754 return;
3755 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003756 /* Note: exit _without_ running nfs4_locku_done */
3757 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003758 return;
3759 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003760 calldata->timestamp = jiffies;
Andy Adamsona8936932009-04-01 09:22:23 -04003761 if (nfs4_setup_sequence(calldata->server->nfs_client,
3762 &calldata->arg.seq_args,
3763 &calldata->res.seq_res, 1, task))
3764 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003765 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766}
3767
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003768static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003769 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003770 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003771 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003772};
3773
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003774static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3775 struct nfs_open_context *ctx,
3776 struct nfs4_lock_state *lsp,
3777 struct nfs_seqid *seqid)
3778{
3779 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003780 struct rpc_message msg = {
3781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3782 .rpc_cred = ctx->cred,
3783 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003784 struct rpc_task_setup task_setup_data = {
3785 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003786 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003787 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003788 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003789 .flags = RPC_TASK_ASYNC,
3790 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003791
Frank Filz137d6ac2007-07-09 15:32:29 -07003792 /* Ensure this is an unlock - when canceling a lock, the
3793 * canceled lock is passed in, and it won't be an unlock.
3794 */
3795 fl->fl_type = F_UNLCK;
3796
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003797 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3798 if (data == NULL) {
3799 nfs_free_seqid(seqid);
3800 return ERR_PTR(-ENOMEM);
3801 }
3802
Trond Myklebust5138fde2007-07-14 15:40:01 -04003803 msg.rpc_argp = &data->arg,
3804 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003805 task_setup_data.callback_data = data;
3806 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003807}
3808
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3810{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003811 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003812 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003813 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003814 struct rpc_task *task;
3815 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003816 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
Trond Myklebust9b073572006-06-29 16:38:34 -04003818 status = nfs4_set_lock_state(state, request);
3819 /* Unlock _before_ we do the RPC call */
3820 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003821 down_read(&nfsi->rwsem);
3822 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3823 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003824 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003825 }
3826 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003827 if (status != 0)
3828 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003829 /* Is this a delegated lock? */
3830 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003831 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003832 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003833 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003834 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003835 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003836 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003837 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003838 status = PTR_ERR(task);
3839 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003840 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003841 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003842 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003843out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003844 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003845 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846}
3847
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003848struct nfs4_lockdata {
3849 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003850 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003851 struct nfs4_lock_state *lsp;
3852 struct nfs_open_context *ctx;
3853 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003854 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003855 int rpc_status;
3856 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04003857 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003858};
3859
3860static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3861 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3862{
3863 struct nfs4_lockdata *p;
3864 struct inode *inode = lsp->ls_state->inode;
3865 struct nfs_server *server = NFS_SERVER(inode);
3866
3867 p = kzalloc(sizeof(*p), GFP_KERNEL);
3868 if (p == NULL)
3869 return NULL;
3870
3871 p->arg.fh = NFS_FH(inode);
3872 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003873 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3874 if (p->arg.open_seqid == NULL)
3875 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003876 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3877 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003878 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003879 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003880 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003881 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003882 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003883 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003884 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04003885 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003886 atomic_inc(&lsp->ls_count);
3887 p->ctx = get_nfs_open_context(ctx);
3888 memcpy(&p->fl, fl, sizeof(p->fl));
3889 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003890out_free_seqid:
3891 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003892out_free:
3893 kfree(p);
3894 return NULL;
3895}
3896
3897static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3898{
3899 struct nfs4_lockdata *data = calldata;
3900 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901
Harvey Harrison3110ff82008-05-02 13:42:44 -07003902 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003903 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3904 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003905 /* Do we need to do an open_to_lock_owner? */
3906 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05003907 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3908 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003909 data->arg.open_stateid = &state->stateid;
3910 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003911 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003912 } else
3913 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003914 data->timestamp = jiffies;
Andy Adamson66179ef2009-04-01 09:22:22 -04003915 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
3916 &data->res.seq_res, 1, task))
3917 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003918 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003919 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003920}
3921
3922static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3923{
3924 struct nfs4_lockdata *data = calldata;
3925
Harvey Harrison3110ff82008-05-02 13:42:44 -07003926 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003927
Andy Adamson66179ef2009-04-01 09:22:22 -04003928 nfs4_sequence_done_free_slot(data->server, &data->res.seq_res,
3929 task->tk_status);
3930
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003931 data->rpc_status = task->tk_status;
3932 if (RPC_ASSASSINATED(task))
3933 goto out;
3934 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003935 if (data->rpc_status == 0)
3936 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3937 else
3938 goto out;
3939 }
3940 if (data->rpc_status == 0) {
3941 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3942 sizeof(data->lsp->ls_stateid.data));
3943 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003944 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003945 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003946out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07003947 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003948}
3949
3950static void nfs4_lock_release(void *calldata)
3951{
3952 struct nfs4_lockdata *data = calldata;
3953
Harvey Harrison3110ff82008-05-02 13:42:44 -07003954 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003955 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003956 if (data->cancelled != 0) {
3957 struct rpc_task *task;
3958 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3959 data->arg.lock_seqid);
3960 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003961 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003962 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003963 } else
3964 nfs_free_seqid(data->arg.lock_seqid);
3965 nfs4_put_lock_state(data->lsp);
3966 put_nfs_open_context(data->ctx);
3967 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003968 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003969}
3970
3971static const struct rpc_call_ops nfs4_lock_ops = {
3972 .rpc_call_prepare = nfs4_lock_prepare,
3973 .rpc_call_done = nfs4_lock_done,
3974 .rpc_release = nfs4_lock_release,
3975};
3976
3977static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3978{
3979 struct nfs4_lockdata *data;
3980 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003981 struct rpc_message msg = {
3982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3983 .rpc_cred = state->owner->so_cred,
3984 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003985 struct rpc_task_setup task_setup_data = {
3986 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003987 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003988 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003989 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003990 .flags = RPC_TASK_ASYNC,
3991 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003992 int ret;
3993
Harvey Harrison3110ff82008-05-02 13:42:44 -07003994 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003995 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003996 fl->fl_u.nfs4_fl.owner);
3997 if (data == NULL)
3998 return -ENOMEM;
3999 if (IS_SETLKW(cmd))
4000 data->arg.block = 1;
4001 if (reclaim != 0)
4002 data->arg.reclaim = 1;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004003 msg.rpc_argp = &data->arg,
4004 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004005 task_setup_data.callback_data = data;
4006 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004007 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004008 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004009 ret = nfs4_wait_for_completion_rpc_task(task);
4010 if (ret == 0) {
4011 ret = data->rpc_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004012 } else
4013 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004014 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004015 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004016 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017}
4018
4019static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4020{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004021 struct nfs_server *server = NFS_SERVER(state->inode);
4022 struct nfs4_exception exception = { };
4023 int err;
4024
4025 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004026 /* Cache the lock if possible... */
4027 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4028 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004029 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
4030 if (err != -NFS4ERR_DELAY)
4031 break;
4032 nfs4_handle_exception(server, err, &exception);
4033 } while (exception.retry);
4034 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035}
4036
4037static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4038{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004039 struct nfs_server *server = NFS_SERVER(state->inode);
4040 struct nfs4_exception exception = { };
4041 int err;
4042
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004043 err = nfs4_set_lock_state(state, request);
4044 if (err != 0)
4045 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004046 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004047 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4048 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004049 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
4050 if (err != -NFS4ERR_DELAY)
4051 break;
4052 nfs4_handle_exception(server, err, &exception);
4053 } while (exception.retry);
4054 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055}
4056
4057static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4058{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004059 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004060 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 int status;
4062
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004063 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004064 status = nfs4_set_lock_state(state, request);
4065 if (status != 0)
4066 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004067 request->fl_flags |= FL_ACCESS;
4068 status = do_vfs_lock(request->fl_file, request);
4069 if (status < 0)
4070 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004071 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004072 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004073 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004074 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004075 request->fl_flags = fl_flags & ~FL_SLEEP;
4076 status = do_vfs_lock(request->fl_file, request);
4077 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004078 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004079 status = _nfs4_do_setlk(state, cmd, request, 0);
4080 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004081 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004082 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004083 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004084 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004085 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004086out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004087 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004088out:
4089 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 return status;
4091}
4092
4093static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4094{
4095 struct nfs4_exception exception = { };
4096 int err;
4097
4098 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004099 err = _nfs4_proc_setlk(state, cmd, request);
4100 if (err == -NFS4ERR_DENIED)
4101 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004103 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 } while (exception.retry);
4105 return err;
4106}
4107
4108static int
4109nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4110{
4111 struct nfs_open_context *ctx;
4112 struct nfs4_state *state;
4113 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4114 int status;
4115
4116 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004117 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 state = ctx->state;
4119
4120 if (request->fl_start < 0 || request->fl_end < 0)
4121 return -EINVAL;
4122
Trond Myklebustd9531262009-07-21 19:22:38 -04004123 if (IS_GETLK(cmd)) {
4124 if (state != NULL)
4125 return nfs4_proc_getlk(state, F_GETLK, request);
4126 return 0;
4127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128
4129 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4130 return -EINVAL;
4131
Trond Myklebustd9531262009-07-21 19:22:38 -04004132 if (request->fl_type == F_UNLCK) {
4133 if (state != NULL)
4134 return nfs4_proc_unlck(state, cmd, request);
4135 return 0;
4136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137
Trond Myklebustd9531262009-07-21 19:22:38 -04004138 if (state == NULL)
4139 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 do {
4141 status = nfs4_proc_setlk(state, cmd, request);
4142 if ((status != -EAGAIN) || IS_SETLK(cmd))
4143 break;
4144 timeout = nfs4_set_lock_task_retry(timeout);
4145 status = -ERESTARTSYS;
4146 if (signalled())
4147 break;
4148 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 return status;
4150}
4151
Trond Myklebust888e6942005-11-04 15:38:11 -05004152int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4153{
4154 struct nfs_server *server = NFS_SERVER(state->inode);
4155 struct nfs4_exception exception = { };
4156 int err;
4157
4158 err = nfs4_set_lock_state(state, fl);
4159 if (err != 0)
4160 goto out;
4161 do {
4162 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
Trond Myklebustd5122202009-06-17 13:22:58 -07004163 switch (err) {
4164 default:
4165 printk(KERN_ERR "%s: unhandled error %d.\n",
4166 __func__, err);
4167 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004168 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004169 goto out;
4170 case -NFS4ERR_EXPIRED:
4171 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004172 case -NFS4ERR_STALE_STATEID:
Trond Myklebustd5122202009-06-17 13:22:58 -07004173 nfs4_schedule_state_recovery(server->nfs_client);
4174 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004175 case -ERESTARTSYS:
4176 /*
4177 * The show must go on: exit, but mark the
4178 * stateid as needing recovery.
4179 */
4180 case -NFS4ERR_ADMIN_REVOKED:
4181 case -NFS4ERR_BAD_STATEID:
4182 case -NFS4ERR_OPENMODE:
4183 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4184 err = 0;
4185 goto out;
4186 case -ENOMEM:
4187 case -NFS4ERR_DENIED:
4188 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4189 err = 0;
4190 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004191 case -NFS4ERR_DELAY:
4192 break;
4193 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004194 err = nfs4_handle_exception(server, err, &exception);
4195 } while (exception.retry);
4196out:
4197 return err;
4198}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004199
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004200#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4201
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004202int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4203 size_t buflen, int flags)
4204{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004205 struct inode *inode = dentry->d_inode;
4206
4207 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4208 return -EOPNOTSUPP;
4209
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004210 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004211}
4212
4213/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4214 * and that's what we'll do for e.g. user attributes that haven't been set.
4215 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4216 * attributes in kernel-managed attribute namespaces. */
4217ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4218 size_t buflen)
4219{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004220 struct inode *inode = dentry->d_inode;
4221
4222 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4223 return -EOPNOTSUPP;
4224
4225 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004226}
4227
4228ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4229{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004230 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004231
J. Bruce Fields096455a2006-03-20 23:23:42 -05004232 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4233 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004234 if (buf && buflen < len)
4235 return -ERANGE;
4236 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004237 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4238 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004239}
4240
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004241static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4242{
4243 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4244 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4245 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4246 return;
4247
4248 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4249 NFS_ATTR_FATTR_NLINK;
4250 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4251 fattr->nlink = 2;
4252}
4253
Trond Myklebust56659e92007-07-17 21:52:39 -04004254int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004255 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004256{
4257 struct nfs_server *server = NFS_SERVER(dir);
4258 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004259 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4260 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004261 };
4262 struct nfs4_fs_locations_arg args = {
4263 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004264 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004265 .page = page,
4266 .bitmask = bitmask,
4267 };
Benny Halevy22958462009-04-01 09:22:02 -04004268 struct nfs4_fs_locations_res res = {
4269 .fs_locations = fs_locations,
4270 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004271 struct rpc_message msg = {
4272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4273 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004274 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004275 };
4276 int status;
4277
Harvey Harrison3110ff82008-05-02 13:42:44 -07004278 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004279 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004280 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004281 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004282 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004283 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004284 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004285 return status;
4286}
4287
Andy Adamson557134a2009-04-01 09:21:53 -04004288#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004289/*
4290 * nfs4_proc_exchange_id()
4291 *
4292 * Since the clientid has expired, all compounds using sessions
4293 * associated with the stale clientid will be returning
4294 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4295 * be in some phase of session reset.
4296 */
4297static int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4298{
4299 nfs4_verifier verifier;
4300 struct nfs41_exchange_id_args args = {
4301 .client = clp,
4302 .flags = clp->cl_exchange_flags,
4303 };
4304 struct nfs41_exchange_id_res res = {
4305 .client = clp,
4306 };
4307 int status;
4308 struct rpc_message msg = {
4309 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4310 .rpc_argp = &args,
4311 .rpc_resp = &res,
4312 .rpc_cred = cred,
4313 };
4314 __be32 *p;
4315
4316 dprintk("--> %s\n", __func__);
4317 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004318
Benny Halevy99fe60d2009-04-01 09:22:29 -04004319 p = (u32 *)verifier.data;
4320 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4321 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4322 args.verifier = &verifier;
4323
4324 while (1) {
4325 args.id_len = scnprintf(args.id, sizeof(args.id),
4326 "%s/%s %u",
4327 clp->cl_ipaddr,
4328 rpc_peeraddr2str(clp->cl_rpcclient,
4329 RPC_DISPLAY_ADDR),
4330 clp->cl_id_uniquifier);
4331
4332 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4333
4334 if (status != NFS4ERR_CLID_INUSE)
4335 break;
4336
4337 if (signalled())
4338 break;
4339
4340 if (++clp->cl_id_uniquifier == 0)
4341 break;
4342 }
4343
4344 dprintk("<-- %s status= %d\n", __func__, status);
4345 return status;
4346}
4347
Andy Adamson2050f0c2009-04-01 09:22:30 -04004348struct nfs4_get_lease_time_data {
4349 struct nfs4_get_lease_time_args *args;
4350 struct nfs4_get_lease_time_res *res;
4351 struct nfs_client *clp;
4352};
4353
4354static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4355 void *calldata)
4356{
4357 int ret;
4358 struct nfs4_get_lease_time_data *data =
4359 (struct nfs4_get_lease_time_data *)calldata;
4360
4361 dprintk("--> %s\n", __func__);
4362 /* just setup sequence, do not trigger session recovery
4363 since we're invoked within one */
4364 ret = nfs41_setup_sequence(data->clp->cl_session,
4365 &data->args->la_seq_args,
4366 &data->res->lr_seq_res, 0, task);
4367
4368 BUG_ON(ret == -EAGAIN);
4369 rpc_call_start(task);
4370 dprintk("<-- %s\n", __func__);
4371}
4372
4373/*
4374 * Called from nfs4_state_manager thread for session setup, so don't recover
4375 * from sequence operation or clientid errors.
4376 */
4377static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4378{
4379 struct nfs4_get_lease_time_data *data =
4380 (struct nfs4_get_lease_time_data *)calldata;
4381
4382 dprintk("--> %s\n", __func__);
4383 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4384 switch (task->tk_status) {
4385 case -NFS4ERR_DELAY:
4386 case -NFS4ERR_GRACE:
4387 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4388 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4389 task->tk_status = 0;
Andy Adamsoneedc0202009-04-01 09:22:41 -04004390 nfs4_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004391 return;
4392 }
4393 nfs41_sequence_free_slot(data->clp, &data->res->lr_seq_res);
4394 dprintk("<-- %s\n", __func__);
4395}
4396
4397struct rpc_call_ops nfs4_get_lease_time_ops = {
4398 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4399 .rpc_call_done = nfs4_get_lease_time_done,
4400};
4401
4402int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4403{
4404 struct rpc_task *task;
4405 struct nfs4_get_lease_time_args args;
4406 struct nfs4_get_lease_time_res res = {
4407 .lr_fsinfo = fsinfo,
4408 };
4409 struct nfs4_get_lease_time_data data = {
4410 .args = &args,
4411 .res = &res,
4412 .clp = clp,
4413 };
4414 struct rpc_message msg = {
4415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4416 .rpc_argp = &args,
4417 .rpc_resp = &res,
4418 };
4419 struct rpc_task_setup task_setup = {
4420 .rpc_client = clp->cl_rpcclient,
4421 .rpc_message = &msg,
4422 .callback_ops = &nfs4_get_lease_time_ops,
4423 .callback_data = &data
4424 };
4425 int status;
4426
4427 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4428 dprintk("--> %s\n", __func__);
4429 task = rpc_run_task(&task_setup);
4430
4431 if (IS_ERR(task))
4432 status = PTR_ERR(task);
4433 else {
4434 status = task->tk_status;
4435 rpc_put_task(task);
4436 }
4437 dprintk("<-- %s return %d\n", __func__, status);
4438
4439 return status;
4440}
4441
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004442/*
4443 * Reset a slot table
4444 */
4445static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots,
4446 int old_max_slots, int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004447{
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004448 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004449 int ret = 0;
4450
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004451 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004452
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004453 /*
4454 * Until we have dynamic slot table adjustment, insist
4455 * upon the same slot table size
4456 */
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004457 if (max_slots != old_max_slots) {
4458 dprintk("%s reset slot table does't match old\n",
4459 __func__);
4460 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4461 goto out;
4462 }
4463 spin_lock(&tbl->slot_tbl_lock);
4464 for (i = 0; i < max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004465 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004466 tbl->highest_used_slotid = -1;
4467 spin_unlock(&tbl->slot_tbl_lock);
4468 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4469 tbl, tbl->slots, tbl->max_slots);
4470out:
4471 dprintk("<-- %s: return %d\n", __func__, ret);
4472 return ret;
4473}
4474
Andy Adamsonfc931582009-04-01 09:22:31 -04004475/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004476 * Reset the forechannel and backchannel slot tables
4477 */
4478static int nfs4_reset_slot_tables(struct nfs4_session *session)
4479{
4480 int status;
4481
4482 status = nfs4_reset_slot_table(&session->fc_slot_table,
4483 session->fc_attrs.max_reqs,
4484 session->fc_slot_table.max_slots,
4485 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004486 if (status)
4487 return status;
4488
4489 status = nfs4_reset_slot_table(&session->bc_slot_table,
4490 session->bc_attrs.max_reqs,
4491 session->bc_slot_table.max_slots,
4492 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004493 return status;
4494}
4495
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004496/* Destroy the slot table */
4497static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4498{
4499 if (session->fc_slot_table.slots != NULL) {
4500 kfree(session->fc_slot_table.slots);
4501 session->fc_slot_table.slots = NULL;
4502 }
4503 if (session->bc_slot_table.slots != NULL) {
4504 kfree(session->bc_slot_table.slots);
4505 session->bc_slot_table.slots = NULL;
4506 }
4507 return;
4508}
4509
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004510/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004511 * Initialize slot table
4512 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004513static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4514 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004515{
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004516 int i;
Andy Adamsonfc931582009-04-01 09:22:31 -04004517 struct nfs4_slot *slot;
4518 int ret = -ENOMEM;
4519
4520 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4521
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004522 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004523
4524 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4525 if (!slot)
4526 goto out;
4527 for (i = 0; i < max_slots; ++i)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004528 slot[i].seq_nr = ivalue;
Andy Adamsonfc931582009-04-01 09:22:31 -04004529 ret = 0;
4530
4531 spin_lock(&tbl->slot_tbl_lock);
4532 if (tbl->slots != NULL) {
4533 spin_unlock(&tbl->slot_tbl_lock);
4534 dprintk("%s: slot table already initialized. tbl=%p slots=%p\n",
4535 __func__, tbl, tbl->slots);
4536 WARN_ON(1);
4537 goto out_free;
4538 }
4539 tbl->max_slots = max_slots;
4540 tbl->slots = slot;
4541 tbl->highest_used_slotid = -1; /* no slot is currently used */
4542 spin_unlock(&tbl->slot_tbl_lock);
4543 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4544 tbl, tbl->slots, tbl->max_slots);
4545out:
4546 dprintk("<-- %s: return %d\n", __func__, ret);
4547 return ret;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004548
Andy Adamsonfc931582009-04-01 09:22:31 -04004549out_free:
4550 kfree(slot);
4551 goto out;
4552}
4553
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004554/*
4555 * Initialize the forechannel and backchannel tables
4556 */
4557static int nfs4_init_slot_tables(struct nfs4_session *session)
4558{
4559 int status;
4560
4561 status = nfs4_init_slot_table(&session->fc_slot_table,
4562 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004563 if (status)
4564 return status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004565
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004566 status = nfs4_init_slot_table(&session->bc_slot_table,
4567 session->bc_attrs.max_reqs, 0);
4568 if (status)
4569 nfs4_destroy_slot_tables(session);
4570
4571 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004572}
4573
4574struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4575{
4576 struct nfs4_session *session;
4577 struct nfs4_slot_table *tbl;
4578
4579 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4580 if (!session)
4581 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004582
4583 set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
4584 /*
4585 * The create session reply races with the server back
4586 * channel probe. Mark the client NFS_CS_SESSION_INITING
4587 * so that the client back channel can find the
4588 * nfs_client struct
4589 */
4590 clp->cl_cons_state = NFS_CS_SESSION_INITING;
4591
Andy Adamson557134a2009-04-01 09:21:53 -04004592 tbl = &session->fc_slot_table;
4593 spin_lock_init(&tbl->slot_tbl_lock);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004594 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4595
4596 tbl = &session->bc_slot_table;
4597 spin_lock_init(&tbl->slot_tbl_lock);
4598 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4599
Andy Adamson557134a2009-04-01 09:21:53 -04004600 session->clp = clp;
4601 return session;
4602}
4603
4604void nfs4_destroy_session(struct nfs4_session *session)
4605{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004606 nfs4_proc_destroy_session(session);
4607 dprintk("%s Destroy backchannel for xprt %p\n",
4608 __func__, session->clp->cl_rpcclient->cl_xprt);
4609 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4610 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004611 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004612 kfree(session);
4613}
4614
Andy Adamsonfc931582009-04-01 09:22:31 -04004615/*
4616 * Initialize the values to be used by the client in CREATE_SESSION
4617 * If nfs4_init_session set the fore channel request and response sizes,
4618 * use them.
4619 *
4620 * Set the back channel max_resp_sz_cached to zero to force the client to
4621 * always set csa_cachethis to FALSE because the current implementation
4622 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4623 */
4624static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4625{
4626 struct nfs4_session *session = args->client->cl_session;
4627 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4628 mxresp_sz = session->fc_attrs.max_resp_sz;
4629
4630 if (mxrqst_sz == 0)
4631 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4632 if (mxresp_sz == 0)
4633 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4634 /* Fore channel attributes */
4635 args->fc_attrs.headerpadsz = 0;
4636 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4637 args->fc_attrs.max_resp_sz = mxresp_sz;
4638 args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4639 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4640 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4641
4642 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4643 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4644 __func__,
4645 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4646 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4647 args->fc_attrs.max_reqs);
4648
4649 /* Back channel attributes */
4650 args->bc_attrs.headerpadsz = 0;
4651 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4652 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4653 args->bc_attrs.max_resp_sz_cached = 0;
4654 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4655 args->bc_attrs.max_reqs = 1;
4656
4657 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4658 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4659 __func__,
4660 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4661 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4662 args->bc_attrs.max_reqs);
4663}
4664
Andy Adamson8d353012009-04-01 09:22:32 -04004665static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4666{
4667 if (rcvd <= sent)
4668 return 0;
4669 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4670 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4671 return -EINVAL;
4672}
4673
4674#define _verify_fore_channel_attr(_name_) \
4675 _verify_channel_attr("fore", #_name_, \
4676 args->fc_attrs._name_, \
4677 session->fc_attrs._name_)
4678
4679#define _verify_back_channel_attr(_name_) \
4680 _verify_channel_attr("back", #_name_, \
4681 args->bc_attrs._name_, \
4682 session->bc_attrs._name_)
4683
4684/*
4685 * The server is not allowed to increase the fore channel header pad size,
4686 * maximum response size, or maximum number of operations.
4687 *
4688 * The back channel attributes are only negotiatied down: We send what the
4689 * (back channel) server insists upon.
4690 */
4691static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4692 struct nfs4_session *session)
4693{
4694 int ret = 0;
4695
4696 ret |= _verify_fore_channel_attr(headerpadsz);
4697 ret |= _verify_fore_channel_attr(max_resp_sz);
4698 ret |= _verify_fore_channel_attr(max_ops);
4699
4700 ret |= _verify_back_channel_attr(headerpadsz);
4701 ret |= _verify_back_channel_attr(max_rqst_sz);
4702 ret |= _verify_back_channel_attr(max_resp_sz);
4703 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4704 ret |= _verify_back_channel_attr(max_ops);
4705 ret |= _verify_back_channel_attr(max_reqs);
4706
4707 return ret;
4708}
4709
Andy Adamsonfc931582009-04-01 09:22:31 -04004710static int _nfs4_proc_create_session(struct nfs_client *clp)
4711{
4712 struct nfs4_session *session = clp->cl_session;
4713 struct nfs41_create_session_args args = {
4714 .client = clp,
4715 .cb_program = NFS4_CALLBACK,
4716 };
4717 struct nfs41_create_session_res res = {
4718 .client = clp,
4719 };
4720 struct rpc_message msg = {
4721 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4722 .rpc_argp = &args,
4723 .rpc_resp = &res,
4724 };
4725 int status;
4726
4727 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004728 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004729
4730 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4731
Andy Adamson8d353012009-04-01 09:22:32 -04004732 if (!status)
4733 /* Verify the session's negotiated channel_attrs values */
4734 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004735 if (!status) {
4736 /* Increment the clientid slot sequence id */
4737 clp->cl_seqid++;
4738 }
4739
4740 return status;
4741}
4742
4743/*
4744 * Issues a CREATE_SESSION operation to the server.
4745 * It is the responsibility of the caller to verify the session is
4746 * expired before calling this routine.
4747 */
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004748int nfs4_proc_create_session(struct nfs_client *clp, int reset)
Andy Adamsonfc931582009-04-01 09:22:31 -04004749{
4750 int status;
4751 unsigned *ptr;
4752 struct nfs_fsinfo fsinfo;
4753 struct nfs4_session *session = clp->cl_session;
4754
4755 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4756
4757 status = _nfs4_proc_create_session(clp);
4758 if (status)
4759 goto out;
4760
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004761 /* Init or reset the fore channel */
4762 if (reset)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004763 status = nfs4_reset_slot_tables(session);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004764 else
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004765 status = nfs4_init_slot_tables(session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004766 dprintk("fore channel slot table initialization returned %d\n", status);
4767 if (status)
4768 goto out;
4769
4770 ptr = (unsigned *)&session->sess_id.data[0];
4771 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4772 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
4773
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004774 if (reset)
4775 /* Lease time is aleady set */
4776 goto out;
4777
Andy Adamsonfc931582009-04-01 09:22:31 -04004778 /* Get the lease time */
4779 status = nfs4_proc_get_lease_time(clp, &fsinfo);
4780 if (status == 0) {
4781 /* Update lease time and schedule renewal */
4782 spin_lock(&clp->cl_lock);
4783 clp->cl_lease_time = fsinfo.lease_time * HZ;
4784 clp->cl_last_renewal = jiffies;
4785 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
4786 spin_unlock(&clp->cl_lock);
4787
4788 nfs4_schedule_state_renewal(clp);
4789 }
4790out:
4791 dprintk("<-- %s\n", __func__);
4792 return status;
4793}
4794
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004795/*
4796 * Issue the over-the-wire RPC DESTROY_SESSION.
4797 * The caller must serialize access to this routine.
4798 */
4799int nfs4_proc_destroy_session(struct nfs4_session *session)
4800{
4801 int status = 0;
4802 struct rpc_message msg;
4803
4804 dprintk("--> nfs4_proc_destroy_session\n");
4805
4806 /* session is still being setup */
4807 if (session->clp->cl_cons_state != NFS_CS_READY)
4808 return status;
4809
4810 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4811 msg.rpc_argp = session;
4812 msg.rpc_resp = NULL;
4813 msg.rpc_cred = NULL;
4814 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4815
4816 if (status)
4817 printk(KERN_WARNING
4818 "Got error %d from the server on DESTROY_SESSION. "
4819 "Session has been destroyed regardless...\n", status);
4820
4821 dprintk("<-- nfs4_proc_destroy_session\n");
4822 return status;
4823}
4824
Trond Myklebustfccba802009-07-21 16:48:07 -04004825int nfs4_init_session(struct nfs_server *server)
4826{
4827 struct nfs_client *clp = server->nfs_client;
4828 int ret;
4829
4830 if (!nfs4_has_session(clp))
4831 return 0;
4832
4833 clp->cl_session->fc_attrs.max_rqst_sz = server->wsize;
4834 clp->cl_session->fc_attrs.max_resp_sz = server->rsize;
4835 ret = nfs4_recover_expired_lease(server);
4836 if (!ret)
4837 ret = nfs4_check_client_ready(clp);
4838 return ret;
4839}
4840
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004841/*
4842 * Renew the cl_session lease.
4843 */
4844static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
4845{
4846 struct nfs4_sequence_args args;
4847 struct nfs4_sequence_res res;
4848
4849 struct rpc_message msg = {
4850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4851 .rpc_argp = &args,
4852 .rpc_resp = &res,
4853 .rpc_cred = cred,
4854 };
4855
4856 args.sa_cache_this = 0;
4857
4858 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
4859 &res, 0);
4860}
4861
4862void nfs41_sequence_call_done(struct rpc_task *task, void *data)
4863{
4864 struct nfs_client *clp = (struct nfs_client *)data;
4865
4866 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
4867
4868 if (task->tk_status < 0) {
4869 dprintk("%s ERROR %d\n", __func__, task->tk_status);
4870
4871 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
4872 == -EAGAIN) {
Andy Adamsoneedc0202009-04-01 09:22:41 -04004873 nfs4_restart_rpc(task, clp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004874 return;
4875 }
4876 }
4877 nfs41_sequence_free_slot(clp, task->tk_msg.rpc_resp);
4878 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
4879
4880 put_rpccred(task->tk_msg.rpc_cred);
4881 kfree(task->tk_msg.rpc_argp);
4882 kfree(task->tk_msg.rpc_resp);
4883
4884 dprintk("<-- %s\n", __func__);
4885}
4886
4887static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
4888{
4889 struct nfs_client *clp;
4890 struct nfs4_sequence_args *args;
4891 struct nfs4_sequence_res *res;
4892
4893 clp = (struct nfs_client *)data;
4894 args = task->tk_msg.rpc_argp;
4895 res = task->tk_msg.rpc_resp;
4896
4897 if (nfs4_setup_sequence(clp, args, res, 0, task))
4898 return;
4899 rpc_call_start(task);
4900}
4901
4902static const struct rpc_call_ops nfs41_sequence_ops = {
4903 .rpc_call_done = nfs41_sequence_call_done,
4904 .rpc_call_prepare = nfs41_sequence_prepare,
4905};
4906
4907static int nfs41_proc_async_sequence(struct nfs_client *clp,
4908 struct rpc_cred *cred)
4909{
4910 struct nfs4_sequence_args *args;
4911 struct nfs4_sequence_res *res;
4912 struct rpc_message msg = {
4913 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4914 .rpc_cred = cred,
4915 };
4916
4917 args = kzalloc(sizeof(*args), GFP_KERNEL);
4918 if (!args)
4919 return -ENOMEM;
4920 res = kzalloc(sizeof(*res), GFP_KERNEL);
4921 if (!res) {
4922 kfree(args);
4923 return -ENOMEM;
4924 }
4925 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
4926 msg.rpc_argp = args;
4927 msg.rpc_resp = res;
4928
4929 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
4930 &nfs41_sequence_ops, (void *)clp);
4931}
4932
Andy Adamson557134a2009-04-01 09:21:53 -04004933#endif /* CONFIG_NFS_V4_1 */
4934
Andy Adamson591d71c2009-04-01 09:22:47 -04004935struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05004936 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004937 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 .recover_open = nfs4_open_reclaim,
4939 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04004940 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04004941 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942};
4943
Andy Adamson591d71c2009-04-01 09:22:47 -04004944#if defined(CONFIG_NFS_V4_1)
4945struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
4946 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
4947 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
4948 .recover_open = nfs4_open_reclaim,
4949 .recover_lock = nfs4_lock_reclaim,
4950 .establish_clid = nfs4_proc_exchange_id,
Andy Adamsonb4b82602009-04-01 09:22:49 -04004951 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04004952};
4953#endif /* CONFIG_NFS_V4_1 */
4954
4955struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05004956 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004957 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 .recover_open = nfs4_open_expired,
4959 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04004960 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04004961 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962};
4963
Andy Adamson591d71c2009-04-01 09:22:47 -04004964#if defined(CONFIG_NFS_V4_1)
4965struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
4966 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
4967 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
4968 .recover_open = nfs4_open_expired,
4969 .recover_lock = nfs4_lock_expired,
4970 .establish_clid = nfs4_proc_exchange_id,
Andy Adamsonb4b82602009-04-01 09:22:49 -04004971 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04004972};
4973#endif /* CONFIG_NFS_V4_1 */
4974
Benny Halevy29fba382009-04-01 09:22:44 -04004975struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
4976 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04004977 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04004978 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04004979};
4980
4981#if defined(CONFIG_NFS_V4_1)
4982struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
4983 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04004984 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04004985 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04004986};
4987#endif
4988
4989/*
4990 * Per minor version reboot and network partition recovery ops
4991 */
4992
Andy Adamson591d71c2009-04-01 09:22:47 -04004993struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
4994 &nfs40_reboot_recovery_ops,
4995#if defined(CONFIG_NFS_V4_1)
4996 &nfs41_reboot_recovery_ops,
4997#endif
4998};
4999
5000struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5001 &nfs40_nograce_recovery_ops,
5002#if defined(CONFIG_NFS_V4_1)
5003 &nfs41_nograce_recovery_ops,
5004#endif
5005};
5006
Benny Halevy29fba382009-04-01 09:22:44 -04005007struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5008 &nfs40_state_renewal_ops,
5009#if defined(CONFIG_NFS_V4_1)
5010 &nfs41_state_renewal_ops,
5011#endif
5012};
5013
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005014static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005015 .permission = nfs_permission,
5016 .getattr = nfs_getattr,
5017 .setattr = nfs_setattr,
5018 .getxattr = nfs4_getxattr,
5019 .setxattr = nfs4_setxattr,
5020 .listxattr = nfs4_listxattr,
5021};
5022
David Howells509de812006-08-22 20:06:11 -04005023const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 .version = 4, /* protocol version */
5025 .dentry_ops = &nfs4_dentry_operations,
5026 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005027 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 .getroot = nfs4_proc_get_root,
5029 .getattr = nfs4_proc_getattr,
5030 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005031 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 .lookup = nfs4_proc_lookup,
5033 .access = nfs4_proc_access,
5034 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 .create = nfs4_proc_create,
5036 .remove = nfs4_proc_remove,
5037 .unlink_setup = nfs4_proc_unlink_setup,
5038 .unlink_done = nfs4_proc_unlink_done,
5039 .rename = nfs4_proc_rename,
5040 .link = nfs4_proc_link,
5041 .symlink = nfs4_proc_symlink,
5042 .mkdir = nfs4_proc_mkdir,
5043 .rmdir = nfs4_proc_remove,
5044 .readdir = nfs4_proc_readdir,
5045 .mknod = nfs4_proc_mknod,
5046 .statfs = nfs4_proc_statfs,
5047 .fsinfo = nfs4_proc_fsinfo,
5048 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005049 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 .decode_dirent = nfs4_decode_dirent,
5051 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005052 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005054 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005056 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005058 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005059 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060};
5061
5062/*
5063 * Local variables:
5064 * c-basic-offset: 8
5065 * End:
5066 */