blob: dfd1ea99e2d63384b3201fc7763f1ca81d4f8cd1 [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{
Trond Myklebust52567b02009-10-23 14:46:42 -040075 if (err >= -1000)
76 return err;
77 switch (err) {
78 case -NFS4ERR_RESOURCE:
79 return -EREMOTEIO;
80 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070082 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040083 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
Trond Myklebust52567b02009-10-23 14:46:42 -040085 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
88/*
89 * This is our standard bitmap for GETATTR requests.
90 */
91const u32 nfs4_fattr_bitmap[2] = {
92 FATTR4_WORD0_TYPE
93 | FATTR4_WORD0_CHANGE
94 | FATTR4_WORD0_SIZE
95 | FATTR4_WORD0_FSID
96 | FATTR4_WORD0_FILEID,
97 FATTR4_WORD1_MODE
98 | FATTR4_WORD1_NUMLINKS
99 | FATTR4_WORD1_OWNER
100 | FATTR4_WORD1_OWNER_GROUP
101 | FATTR4_WORD1_RAWDEV
102 | FATTR4_WORD1_SPACE_USED
103 | FATTR4_WORD1_TIME_ACCESS
104 | FATTR4_WORD1_TIME_METADATA
105 | FATTR4_WORD1_TIME_MODIFY
106};
107
108const u32 nfs4_statfs_bitmap[2] = {
109 FATTR4_WORD0_FILES_AVAIL
110 | FATTR4_WORD0_FILES_FREE
111 | FATTR4_WORD0_FILES_TOTAL,
112 FATTR4_WORD1_SPACE_AVAIL
113 | FATTR4_WORD1_SPACE_FREE
114 | FATTR4_WORD1_SPACE_TOTAL
115};
116
Trond Myklebust4ce79712005-06-22 17:16:21 +0000117const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 FATTR4_WORD0_MAXLINK
119 | FATTR4_WORD0_MAXNAME,
120 0
121};
122
123const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
124 | FATTR4_WORD0_MAXREAD
125 | FATTR4_WORD0_MAXWRITE
126 | FATTR4_WORD0_LEASE_TIME,
127 0
128};
129
Manoj Naik830b8e32006-06-09 09:34:25 -0400130const u32 nfs4_fs_locations_bitmap[2] = {
131 FATTR4_WORD0_TYPE
132 | FATTR4_WORD0_CHANGE
133 | FATTR4_WORD0_SIZE
134 | FATTR4_WORD0_FSID
135 | FATTR4_WORD0_FILEID
136 | FATTR4_WORD0_FS_LOCATIONS,
137 FATTR4_WORD1_MODE
138 | FATTR4_WORD1_NUMLINKS
139 | FATTR4_WORD1_OWNER
140 | FATTR4_WORD1_OWNER_GROUP
141 | FATTR4_WORD1_RAWDEV
142 | FATTR4_WORD1_SPACE_USED
143 | FATTR4_WORD1_TIME_ACCESS
144 | FATTR4_WORD1_TIME_METADATA
145 | FATTR4_WORD1_TIME_MODIFY
146 | FATTR4_WORD1_MOUNTED_ON_FILEID
147};
148
Al Virobc4785c2006-10-19 23:28:51 -0700149static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 struct nfs4_readdir_arg *readdir)
151{
Al Viro0dbb4c62006-10-19 23:28:49 -0700152 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 BUG_ON(readdir->count < 80);
155 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000156 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
158 return;
159 }
160
161 readdir->cookie = 0;
162 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
163 if (cookie == 2)
164 return;
165
166 /*
167 * NFSv4 servers do not return entries for '.' and '..'
168 * Therefore, we fake these entries here. We let '.'
169 * have cookie 0 and '..' have cookie 1. Note that
170 * when talking to the server, we always send cookie 0
171 * instead of 1 or 2.
172 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700173 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 if (cookie == 0) {
176 *p++ = xdr_one; /* next */
177 *p++ = xdr_zero; /* cookie, first word */
178 *p++ = xdr_one; /* cookie, second word */
179 *p++ = xdr_one; /* entry len */
180 memcpy(p, ".\0\0\0", 4); /* entry */
181 p++;
182 *p++ = xdr_one; /* bitmap length */
183 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
184 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400185 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187
188 *p++ = xdr_one; /* next */
189 *p++ = xdr_zero; /* cookie, first word */
190 *p++ = xdr_two; /* cookie, second word */
191 *p++ = xdr_two; /* entry len */
192 memcpy(p, "..\0\0", 4); /* entry */
193 p++;
194 *p++ = xdr_one; /* bitmap length */
195 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
196 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400197 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 readdir->pgbase = (char *)p - (char *)start;
200 readdir->count -= readdir->pgbase;
201 kunmap_atomic(start, KM_USER0);
202}
203
Trond Myklebust65de8722008-12-23 15:21:44 -0500204static int nfs4_wait_clnt_recover(struct nfs_client *clp)
205{
206 int res;
207
208 might_sleep();
209
Trond Myklebuste005e802008-12-23 15:21:48 -0500210 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400211 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500212 return res;
213}
214
215static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
216{
217 int res = 0;
218
219 might_sleep();
220
221 if (*timeout <= 0)
222 *timeout = NFS4_POLL_RETRY_MIN;
223 if (*timeout > NFS4_POLL_RETRY_MAX)
224 *timeout = NFS4_POLL_RETRY_MAX;
225 schedule_timeout_killable(*timeout);
226 if (fatal_signal_pending(current))
227 res = -ERESTARTSYS;
228 *timeout <<= 1;
229 return res;
230}
231
232/* This is the error handling routine for processes that are allowed
233 * to sleep.
234 */
235static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
236{
237 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500238 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500239 int ret = errorcode;
240
241 exception->retry = 0;
242 switch(errorcode) {
243 case 0:
244 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500245 case -NFS4ERR_ADMIN_REVOKED:
246 case -NFS4ERR_BAD_STATEID:
247 case -NFS4ERR_OPENMODE:
248 if (state == NULL)
249 break;
250 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500251 case -NFS4ERR_STALE_CLIENTID:
252 case -NFS4ERR_STALE_STATEID:
253 case -NFS4ERR_EXPIRED:
254 nfs4_schedule_state_recovery(clp);
255 ret = nfs4_wait_clnt_recover(clp);
256 if (ret == 0)
257 exception->retry = 1;
Andy Adamson4745e312009-04-01 09:22:42 -0400258#if !defined(CONFIG_NFS_V4_1)
Trond Myklebust65de8722008-12-23 15:21:44 -0500259 break;
Andy Adamson4745e312009-04-01 09:22:42 -0400260#else /* !defined(CONFIG_NFS_V4_1) */
261 if (!nfs4_has_session(server->nfs_client))
262 break;
263 /* FALLTHROUGH */
264 case -NFS4ERR_BADSESSION:
265 case -NFS4ERR_BADSLOT:
266 case -NFS4ERR_BAD_HIGH_SLOT:
267 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
268 case -NFS4ERR_DEADSESSION:
269 case -NFS4ERR_SEQ_FALSE_RETRY:
270 case -NFS4ERR_SEQ_MISORDERED:
271 dprintk("%s ERROR: %d Reset session\n", __func__,
272 errorcode);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500273 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -0400274 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500275 break;
Andy Adamson4745e312009-04-01 09:22:42 -0400276#endif /* !defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500277 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500278 if (exception->timeout > HZ) {
279 /* We have retried a decent amount, time to
280 * fail
281 */
282 ret = -EBUSY;
283 break;
284 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500285 case -NFS4ERR_GRACE:
286 case -NFS4ERR_DELAY:
287 ret = nfs4_delay(server->client, &exception->timeout);
288 if (ret != 0)
289 break;
290 case -NFS4ERR_OLD_STATEID:
291 exception->retry = 1;
292 }
293 /* We failed to handle the error */
294 return nfs4_map_errors(ret);
295}
296
297
Trond Myklebust26e976a2006-01-03 09:55:21 +0100298static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
David Howells7539bba2006-08-22 20:06:09 -0400300 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 spin_lock(&clp->cl_lock);
302 if (time_before(clp->cl_last_renewal,timestamp))
303 clp->cl_last_renewal = timestamp;
304 spin_unlock(&clp->cl_lock);
305}
306
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400307#if defined(CONFIG_NFS_V4_1)
308
Benny Halevy510b8172009-04-01 09:22:14 -0400309/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400310 * nfs4_free_slot - free a slot and efficiently update slot table.
311 *
312 * freeing a slot is trivially done by clearing its respective bit
313 * in the bitmap.
314 * If the freed slotid equals highest_used_slotid we want to update it
315 * so that the server would be able to size down the slot table if needed,
316 * otherwise we know that the highest_used_slotid is still in use.
317 * When updating highest_used_slotid there may be "holes" in the bitmap
318 * so we need to scan down from highest_used_slotid to 0 looking for the now
319 * highest slotid in use.
320 * If none found, highest_used_slotid is set to -1.
321 */
322static void
323nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
324{
325 int slotid = free_slotid;
326
327 spin_lock(&tbl->slot_tbl_lock);
328 /* clear used bit in bitmap */
329 __clear_bit(slotid, tbl->used_slots);
330
331 /* update highest_used_slotid when it is freed */
332 if (slotid == tbl->highest_used_slotid) {
333 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
334 if (slotid >= 0 && slotid < tbl->max_slots)
335 tbl->highest_used_slotid = slotid;
336 else
337 tbl->highest_used_slotid = -1;
338 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400339 spin_unlock(&tbl->slot_tbl_lock);
340 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
341 free_slotid, tbl->highest_used_slotid);
342}
343
Andy Adamson13615872009-04-01 09:22:17 -0400344void nfs41_sequence_free_slot(const struct nfs_client *clp,
345 struct nfs4_sequence_res *res)
346{
347 struct nfs4_slot_table *tbl;
348
349 if (!nfs4_has_session(clp)) {
350 dprintk("%s: No session\n", __func__);
351 return;
352 }
353 tbl = &clp->cl_session->fc_slot_table;
354 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
Andy Adamson13615872009-04-01 09:22:17 -0400355 /* just wake up the next guy waiting since
356 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500357 dprintk("%s: No slot\n", __func__);
358 } else {
359 nfs4_free_slot(tbl, res->sr_slotid);
360 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamson13615872009-04-01 09:22:17 -0400361 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500362
363 /* Signal state manager thread if session is drained */
364 if (test_bit(NFS4CLNT_SESSION_DRAINING, &clp->cl_state)) {
365 spin_lock(&tbl->slot_tbl_lock);
366 if (tbl->highest_used_slotid == -1) {
367 dprintk("%s COMPLETE: Session Drained\n", __func__);
368 complete(&clp->cl_session->complete);
369 }
370 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson691daf32009-12-04 15:55:39 -0500371 } else {
372 rpc_wake_up_next(&tbl->slot_tbl_waitq);
Andy Adamsonea028ac2009-12-04 15:55:38 -0500373 }
Andy Adamson13615872009-04-01 09:22:17 -0400374}
375
Andy Adamsonb0df8062009-04-01 09:22:18 -0400376static void nfs41_sequence_done(struct nfs_client *clp,
377 struct nfs4_sequence_res *res,
378 int rpc_status)
379{
380 unsigned long timestamp;
381 struct nfs4_slot_table *tbl;
382 struct nfs4_slot *slot;
383
384 /*
385 * sr_status remains 1 if an RPC level error occurred. The server
386 * may or may not have processed the sequence operation..
387 * Proceed as if the server received and processed the sequence
388 * operation.
389 */
390 if (res->sr_status == 1)
391 res->sr_status = NFS_OK;
392
393 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
394 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
395 goto out;
396
Andy Adamson691daf32009-12-04 15:55:39 -0500397 /* Check the SEQUENCE operation status */
Andy Adamsonb0df8062009-04-01 09:22:18 -0400398 if (res->sr_status == 0) {
Andy Adamson691daf32009-12-04 15:55:39 -0500399 tbl = &clp->cl_session->fc_slot_table;
400 slot = tbl->slots + res->sr_slotid;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400401 /* Update the slot's sequence and clientid lease timer */
402 ++slot->seq_nr;
403 timestamp = res->sr_renewal_time;
404 spin_lock(&clp->cl_lock);
405 if (time_before(clp->cl_last_renewal, timestamp))
406 clp->cl_last_renewal = timestamp;
407 spin_unlock(&clp->cl_lock);
408 return;
409 }
410out:
411 /* The session may be reset by one of the error handlers. */
412 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
413 nfs41_sequence_free_slot(clp, res);
414}
415
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400416/*
Benny Halevy510b8172009-04-01 09:22:14 -0400417 * nfs4_find_slot - efficiently look for a free slot
418 *
419 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
420 * If found, we mark the slot as used, update the highest_used_slotid,
421 * and respectively set up the sequence operation args.
422 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400423 *
424 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400425 */
426static u8
427nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
428{
429 int slotid;
430 u8 ret_id = NFS4_MAX_SLOT_TABLE;
431 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
432
Benny Halevy510b8172009-04-01 09:22:14 -0400433 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
434 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
435 tbl->max_slots);
436 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
437 if (slotid >= tbl->max_slots)
438 goto out;
439 __set_bit(slotid, tbl->used_slots);
440 if (slotid > tbl->highest_used_slotid)
441 tbl->highest_used_slotid = slotid;
442 ret_id = slotid;
443out:
444 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
445 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400446 return ret_id;
447}
448
Andy Adamsonce5039c2009-04-01 09:22:13 -0400449static int nfs41_setup_sequence(struct nfs4_session *session,
450 struct nfs4_sequence_args *args,
451 struct nfs4_sequence_res *res,
452 int cache_reply,
453 struct rpc_task *task)
454{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400455 struct nfs4_slot *slot;
456 struct nfs4_slot_table *tbl;
457 u8 slotid;
458
459 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400460 /* slot already allocated? */
461 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
462 return 0;
463
464 memset(res, 0, sizeof(*res));
465 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400466 tbl = &session->fc_slot_table;
467
468 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500469 if (test_bit(NFS4CLNT_SESSION_DRAINING, &session->clp->cl_state)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500470 /*
471 * The state manager will wait until the slot table is empty.
472 * Schedule the reset thread
473 */
474 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500475 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
476 nfs4_schedule_state_manager(session->clp);
Andy Adamsonb069d942009-04-01 09:22:43 -0400477 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson05f0d232009-12-04 15:55:37 -0500478 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400479 }
480
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400481 slotid = nfs4_find_slot(tbl, task);
482 if (slotid == NFS4_MAX_SLOT_TABLE) {
483 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
484 spin_unlock(&tbl->slot_tbl_lock);
485 dprintk("<-- %s: no free slots\n", __func__);
486 return -EAGAIN;
487 }
488 spin_unlock(&tbl->slot_tbl_lock);
489
490 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400491 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400492 args->sa_slotid = slotid;
493 args->sa_cache_this = cache_reply;
494
495 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
496
Benny Halevy99fe60d2009-04-01 09:22:29 -0400497 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400498 res->sr_slotid = slotid;
499 res->sr_renewal_time = jiffies;
500 /*
501 * sr_status is only set in decode_sequence, and so will remain
502 * set to 1 if an rpc level failure occurs.
503 */
504 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400505 return 0;
506}
507
508int nfs4_setup_sequence(struct nfs_client *clp,
509 struct nfs4_sequence_args *args,
510 struct nfs4_sequence_res *res,
511 int cache_reply,
512 struct rpc_task *task)
513{
514 int ret = 0;
515
516 dprintk("--> %s clp %p session %p sr_slotid %d\n",
517 __func__, clp, clp->cl_session, res->sr_slotid);
518
519 if (!nfs4_has_session(clp))
520 goto out;
521 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
522 task);
Andy Adamson2628edd2009-12-04 15:55:35 -0500523 if (ret && ret != -EAGAIN) {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400524 /* terminate rpc task */
525 task->tk_status = ret;
526 task->tk_action = NULL;
527 }
528out:
529 dprintk("<-- %s status=%d\n", __func__, ret);
530 return ret;
531}
532
533struct nfs41_call_sync_data {
534 struct nfs_client *clp;
535 struct nfs4_sequence_args *seq_args;
536 struct nfs4_sequence_res *seq_res;
537 int cache_reply;
538};
539
540static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
541{
542 struct nfs41_call_sync_data *data = calldata;
543
544 dprintk("--> %s data->clp->cl_session %p\n", __func__,
545 data->clp->cl_session);
546 if (nfs4_setup_sequence(data->clp, data->seq_args,
547 data->seq_res, data->cache_reply, task))
548 return;
549 rpc_call_start(task);
550}
551
Andy Adamson69ab40c2009-04-01 09:22:19 -0400552static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
553{
554 struct nfs41_call_sync_data *data = calldata;
555
556 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
557 nfs41_sequence_free_slot(data->clp, data->seq_res);
558}
559
Andy Adamsonce5039c2009-04-01 09:22:13 -0400560struct rpc_call_ops nfs41_call_sync_ops = {
561 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400562 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400563};
564
565static int nfs4_call_sync_sequence(struct nfs_client *clp,
566 struct rpc_clnt *clnt,
567 struct rpc_message *msg,
568 struct nfs4_sequence_args *args,
569 struct nfs4_sequence_res *res,
570 int cache_reply)
571{
572 int ret;
573 struct rpc_task *task;
574 struct nfs41_call_sync_data data = {
575 .clp = clp,
576 .seq_args = args,
577 .seq_res = res,
578 .cache_reply = cache_reply,
579 };
580 struct rpc_task_setup task_setup = {
581 .rpc_client = clnt,
582 .rpc_message = msg,
583 .callback_ops = &nfs41_call_sync_ops,
584 .callback_data = &data
585 };
586
587 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
588 task = rpc_run_task(&task_setup);
589 if (IS_ERR(task))
590 ret = PTR_ERR(task);
591 else {
592 ret = task->tk_status;
593 rpc_put_task(task);
594 }
595 return ret;
596}
597
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400598int _nfs4_call_sync_session(struct nfs_server *server,
599 struct rpc_message *msg,
600 struct nfs4_sequence_args *args,
601 struct nfs4_sequence_res *res,
602 int cache_reply)
603{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604 return nfs4_call_sync_sequence(server->nfs_client, server->client,
605 msg, args, res, cache_reply);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400606}
607
608#endif /* CONFIG_NFS_V4_1 */
609
610int _nfs4_call_sync(struct nfs_server *server,
611 struct rpc_message *msg,
612 struct nfs4_sequence_args *args,
613 struct nfs4_sequence_res *res,
614 int cache_reply)
615{
Benny Halevyf3752972009-04-01 09:22:04 -0400616 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400617 return rpc_call_sync(server->client, msg, 0);
618}
619
620#define nfs4_call_sync(server, msg, args, res, cache_reply) \
621 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
622 &(res)->seq_res, (cache_reply))
623
Andy Adamsonb0df8062009-04-01 09:22:18 -0400624static void nfs4_sequence_done(const struct nfs_server *server,
625 struct nfs4_sequence_res *res, int rpc_status)
626{
627#ifdef CONFIG_NFS_V4_1
628 if (nfs4_has_session(server->nfs_client))
629 nfs41_sequence_done(server->nfs_client, res, rpc_status);
630#endif /* CONFIG_NFS_V4_1 */
631}
632
Andy Adamsone608e792009-12-04 15:55:29 -0500633void nfs4_restart_rpc(struct rpc_task *task, const struct nfs_client *clp,
634 struct nfs4_sequence_res *res)
635{
636#ifdef CONFIG_NFS_V4_1
637 if (nfs4_has_session(clp)) {
638 nfs41_sequence_free_slot(clp, res);
639 rpc_restart_call_prepare(task);
640 return;
641 }
642#endif /* CONFIG_NFS_V4_1 */
643 rpc_restart_call(task);
644}
645
Andy Adamsonb0df8062009-04-01 09:22:18 -0400646/* no restart, therefore free slot here */
647static void nfs4_sequence_done_free_slot(const struct nfs_server *server,
648 struct nfs4_sequence_res *res,
649 int rpc_status)
650{
651 nfs4_sequence_done(server, res, rpc_status);
652 nfs4_sequence_free_slot(server->nfs_client, res);
653}
654
Trond Myklebust38478b22006-05-25 01:40:57 -0400655static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Trond Myklebust38478b22006-05-25 01:40:57 -0400657 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Trond Myklebust38478b22006-05-25 01:40:57 -0400659 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400660 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
661 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400662 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400663 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400664 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100667struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400668 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100669 struct nfs_openargs o_arg;
670 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100671 struct nfs_open_confirmargs c_arg;
672 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100673 struct nfs_fattr f_attr;
674 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400675 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100676 struct dentry *dir;
677 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400678 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100679 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100680 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400681 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100682 int rpc_status;
683 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100684};
685
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400686
687static void nfs4_init_opendata_res(struct nfs4_opendata *p)
688{
689 p->o_res.f_attr = &p->f_attr;
690 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400691 p->o_res.seqid = p->o_arg.seqid;
692 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400693 p->o_res.server = p->o_arg.server;
694 nfs_fattr_init(&p->f_attr);
695 nfs_fattr_init(&p->dir_attr);
Benny Halevy578e4582009-06-18 22:01:23 -0400696 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400697}
698
Trond Myklebustad389da2007-06-05 12:30:00 -0400699static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500700 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100701 const struct iattr *attrs)
702{
Trond Myklebustad389da2007-06-05 12:30:00 -0400703 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100704 struct inode *dir = parent->d_inode;
705 struct nfs_server *server = NFS_SERVER(dir);
706 struct nfs4_opendata *p;
707
708 p = kzalloc(sizeof(*p), GFP_KERNEL);
709 if (p == NULL)
710 goto err;
711 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
712 if (p->o_arg.seqid == NULL)
713 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400714 p->path.mnt = mntget(path->mnt);
715 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100716 p->dir = parent;
717 p->owner = sp;
718 atomic_inc(&sp->so_count);
719 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500720 p->o_arg.open_flags = flags;
721 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400722 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400723 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400724 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100725 p->o_arg.server = server;
726 p->o_arg.bitmask = server->attr_bitmask;
727 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100728 if (flags & O_EXCL) {
Alexandros Batsakis4882ef72009-12-05 13:30:21 -0500729 if (nfs4_has_persistent_session(server->nfs_client)) {
730 /* GUARDED */
731 p->o_arg.u.attrs = &p->attrs;
732 memcpy(&p->attrs, attrs, sizeof(p->attrs));
733 } else { /* EXCLUSIVE4_1 */
734 u32 *s = (u32 *) p->o_arg.u.verifier.data;
735 s[0] = jiffies;
736 s[1] = current->pid;
737 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100738 } else if (flags & O_CREAT) {
739 p->o_arg.u.attrs = &p->attrs;
740 memcpy(&p->attrs, attrs, sizeof(p->attrs));
741 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100742 p->c_arg.fh = &p->o_res.fh;
743 p->c_arg.stateid = &p->o_res.stateid;
744 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400745 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400746 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100747 return p;
748err_free:
749 kfree(p);
750err:
751 dput(parent);
752 return NULL;
753}
754
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400755static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100756{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400757 struct nfs4_opendata *p = container_of(kref,
758 struct nfs4_opendata, kref);
759
760 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400761 if (p->state != NULL)
762 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400763 nfs4_put_state_owner(p->owner);
764 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700765 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400766 kfree(p);
767}
768
769static void nfs4_opendata_put(struct nfs4_opendata *p)
770{
771 if (p != NULL)
772 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100773}
774
Trond Myklebust06f814a2006-01-03 09:55:07 +0100775static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
776{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100777 int ret;
778
Trond Myklebust06f814a2006-01-03 09:55:07 +0100779 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100780 return ret;
781}
782
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500783static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400784{
785 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500786
787 if (open_mode & O_EXCL)
788 goto out;
789 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400790 case FMODE_READ:
791 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400792 break;
793 case FMODE_WRITE:
794 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400795 break;
796 case FMODE_READ|FMODE_WRITE:
797 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
798 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500799out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400800 return ret;
801}
802
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500803static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400804{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500805 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400806 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500807 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400808 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500809 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400810 return 1;
811}
812
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500813static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100814{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500815 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100816 case FMODE_WRITE:
817 state->n_wronly++;
818 break;
819 case FMODE_READ:
820 state->n_rdonly++;
821 break;
822 case FMODE_READ|FMODE_WRITE:
823 state->n_rdwr++;
824 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500825 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100826}
827
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500828static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400829{
830 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
831 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
832 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500833 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400834 case FMODE_READ:
835 set_bit(NFS_O_RDONLY_STATE, &state->flags);
836 break;
837 case FMODE_WRITE:
838 set_bit(NFS_O_WRONLY_STATE, &state->flags);
839 break;
840 case FMODE_READ|FMODE_WRITE:
841 set_bit(NFS_O_RDWR_STATE, &state->flags);
842 }
843}
844
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500845static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400846{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400847 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500848 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400849 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400850}
851
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500852static 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 -0700853{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400854 /*
855 * Protect the call to nfs4_state_set_mode_locked and
856 * serialise the stateid update
857 */
858 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400859 if (deleg_stateid != NULL) {
860 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
861 set_bit(NFS_DELEGATED_STATE, &state->flags);
862 }
863 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500864 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400865 write_sequnlock(&state->seqlock);
866 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500867 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700868 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500871static 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 -0500872{
873 struct nfs_inode *nfsi = NFS_I(state->inode);
874 struct nfs_delegation *deleg_cur;
875 int ret = 0;
876
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500877 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500878
879 rcu_read_lock();
880 deleg_cur = rcu_dereference(nfsi->delegation);
881 if (deleg_cur == NULL)
882 goto no_delegation;
883
884 spin_lock(&deleg_cur->lock);
885 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500886 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500887 goto no_delegation_unlock;
888
889 if (delegation == NULL)
890 delegation = &deleg_cur->stateid;
891 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
892 goto no_delegation_unlock;
893
Trond Myklebustb7391f42008-12-23 15:21:52 -0500894 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500895 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500896 ret = 1;
897no_delegation_unlock:
898 spin_unlock(&deleg_cur->lock);
899no_delegation:
900 rcu_read_unlock();
901
902 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500904 ret = 1;
905 }
906
907 return ret;
908}
909
910
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500911static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400912{
913 struct nfs_delegation *delegation;
914
915 rcu_read_lock();
916 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500917 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400918 rcu_read_unlock();
919 return;
920 }
921 rcu_read_unlock();
922 nfs_inode_return_delegation(inode);
923}
924
Trond Myklebust6ee41262007-07-08 14:11:36 -0400925static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400926{
927 struct nfs4_state *state = opendata->state;
928 struct nfs_inode *nfsi = NFS_I(state->inode);
929 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500930 int open_mode = opendata->o_arg.open_flags & O_EXCL;
931 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400932 nfs4_stateid stateid;
933 int ret = -EAGAIN;
934
Trond Myklebustaac00a82007-07-05 19:02:21 -0400935 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500936 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400937 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500938 if (can_open_cached(state, fmode, open_mode)) {
939 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400940 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400941 goto out_return_state;
942 }
943 spin_unlock(&state->owner->so_lock);
944 }
Trond Myklebust34310432008-12-23 15:21:38 -0500945 rcu_read_lock();
946 delegation = rcu_dereference(nfsi->delegation);
947 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500948 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500949 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400950 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500951 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400952 /* Save the delegation */
953 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
954 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400955 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400956 if (ret != 0)
957 goto out;
958 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500959
960 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500962 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400963 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400964out:
965 return ERR_PTR(ret);
966out_return_state:
967 atomic_inc(&state->count);
968 return state;
969}
970
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100971static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
972{
973 struct inode *inode;
974 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400975 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400976 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100977
Trond Myklebustaac00a82007-07-05 19:02:21 -0400978 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400979 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400980 goto out;
981 }
982
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400983 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100984 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400985 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100986 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400987 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500988 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400989 goto err;
990 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100991 state = nfs4_get_open_state(inode, data->owner);
992 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400993 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400994 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400995 int delegation_flags = 0;
996
Trond Myklebust003707c2007-07-05 18:07:55 -0400997 rcu_read_lock();
998 delegation = rcu_dereference(NFS_I(inode)->delegation);
999 if (delegation)
1000 delegation_flags = delegation->flags;
1001 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001002 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001003 nfs_inode_set_delegation(state->inode,
1004 data->owner->so_cred,
1005 &data->o_res);
1006 else
1007 nfs_inode_reclaim_delegation(state->inode,
1008 data->owner->so_cred,
1009 &data->o_res);
1010 }
Trond Myklebust34310432008-12-23 15:21:38 -05001011
1012 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001013 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001014 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001015out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001016 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001017err_put_inode:
1018 iput(inode);
1019err:
1020 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001021}
1022
Trond Myklebust864472e2006-01-03 09:55:15 +01001023static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1024{
1025 struct nfs_inode *nfsi = NFS_I(state->inode);
1026 struct nfs_open_context *ctx;
1027
1028 spin_lock(&state->inode->i_lock);
1029 list_for_each_entry(ctx, &nfsi->open_files, list) {
1030 if (ctx->state != state)
1031 continue;
1032 get_nfs_open_context(ctx);
1033 spin_unlock(&state->inode->i_lock);
1034 return ctx;
1035 }
1036 spin_unlock(&state->inode->i_lock);
1037 return ERR_PTR(-ENOENT);
1038}
1039
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001040static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1041{
1042 struct nfs4_opendata *opendata;
1043
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001044 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001045 if (opendata == NULL)
1046 return ERR_PTR(-ENOMEM);
1047 opendata->state = state;
1048 atomic_inc(&state->count);
1049 return opendata;
1050}
1051
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001052static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001053{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001054 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001055 int ret;
1056
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001057 opendata->o_arg.open_flags = 0;
1058 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001059 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1060 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1061 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001062 ret = _nfs4_proc_open(opendata);
1063 if (ret != 0)
1064 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001065 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001066 if (IS_ERR(newstate))
1067 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001068 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001069 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001070 return 0;
1071}
1072
1073static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1074{
Trond Myklebust864472e2006-01-03 09:55:15 +01001075 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001076 int ret;
1077
1078 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001079 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001080 smp_rmb();
1081 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001082 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001083 if (ret != 0)
1084 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001085 if (newstate != state)
1086 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001087 }
1088 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001089 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001090 if (ret != 0)
1091 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001092 if (newstate != state)
1093 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001094 }
1095 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001096 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001097 if (ret != 0)
1098 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001099 if (newstate != state)
1100 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001101 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001102 /*
1103 * We may have performed cached opens for all three recoveries.
1104 * Check if we need to update the current stateid.
1105 */
1106 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1107 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001108 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001109 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1110 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001111 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001112 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001113 return 0;
1114}
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116/*
1117 * OPEN_RECLAIM:
1118 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001120static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001122 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001123 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001124 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 int status;
1126
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001127 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1128 if (IS_ERR(opendata))
1129 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001130 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1131 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001132 rcu_read_lock();
1133 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001134 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001135 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001136 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001137 opendata->o_arg.u.delegation_type = delegation_type;
1138 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001139 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return status;
1141}
1142
Trond Myklebust539cd032007-06-05 11:46:42 -04001143static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
1145 struct nfs_server *server = NFS_SERVER(state->inode);
1146 struct nfs4_exception exception = { };
1147 int err;
1148 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001149 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001150 if (err != -NFS4ERR_DELAY)
1151 break;
1152 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 } while (exception.retry);
1154 return err;
1155}
1156
Trond Myklebust864472e2006-01-03 09:55:15 +01001157static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1158{
1159 struct nfs_open_context *ctx;
1160 int ret;
1161
1162 ctx = nfs4_state_find_open_context(state);
1163 if (IS_ERR(ctx))
1164 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001165 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001166 put_nfs_open_context(ctx);
1167 return ret;
1168}
1169
Trond Myklebust13437e12007-07-06 15:10:43 -04001170static 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 -07001171{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001172 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001173 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001175 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1176 if (IS_ERR(opendata))
1177 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001178 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001179 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001180 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001181 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001182 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001183 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
1185
Trond Myklebust13437e12007-07-06 15:10:43 -04001186int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001189 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 int err;
1191 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001192 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 switch (err) {
1194 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001195 case -ENOENT:
1196 case -ESTALE:
1197 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 case -NFS4ERR_STALE_CLIENTID:
1199 case -NFS4ERR_STALE_STATEID:
1200 case -NFS4ERR_EXPIRED:
1201 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001202 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001203 goto out;
1204 case -ERESTARTSYS:
1205 /*
1206 * The show must go on: exit, but mark the
1207 * stateid as needing recovery.
1208 */
1209 case -NFS4ERR_ADMIN_REVOKED:
1210 case -NFS4ERR_BAD_STATEID:
1211 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1212 case -ENOMEM:
1213 err = 0;
1214 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216 err = nfs4_handle_exception(server, err, &exception);
1217 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001218out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return err;
1220}
1221
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001222static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1223{
1224 struct nfs4_opendata *data = calldata;
1225
1226 data->rpc_status = task->tk_status;
1227 if (RPC_ASSASSINATED(task))
1228 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001229 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001230 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1231 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001232 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001233 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001234 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001235 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001236}
1237
1238static void nfs4_open_confirm_release(void *calldata)
1239{
1240 struct nfs4_opendata *data = calldata;
1241 struct nfs4_state *state = NULL;
1242
1243 /* If this request hasn't been cancelled, do nothing */
1244 if (data->cancelled == 0)
1245 goto out_free;
1246 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001247 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001248 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001249 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001250 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001251 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001252out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001253 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001254}
1255
1256static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001257 .rpc_call_done = nfs4_open_confirm_done,
1258 .rpc_release = nfs4_open_confirm_release,
1259};
1260
1261/*
1262 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1263 */
1264static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1265{
1266 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1267 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001268 struct rpc_message msg = {
1269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1270 .rpc_argp = &data->c_arg,
1271 .rpc_resp = &data->c_res,
1272 .rpc_cred = data->owner->so_cred,
1273 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001274 struct rpc_task_setup task_setup_data = {
1275 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001276 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001277 .callback_ops = &nfs4_open_confirm_ops,
1278 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001279 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001280 .flags = RPC_TASK_ASYNC,
1281 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 int status;
1283
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001284 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001285 data->rpc_done = 0;
1286 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001287 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001288 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001289 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001290 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001291 status = nfs4_wait_for_completion_rpc_task(task);
1292 if (status != 0) {
1293 data->cancelled = 1;
1294 smp_wmb();
1295 } else
1296 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001297 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return status;
1299}
1300
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001301static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001303 struct nfs4_opendata *data = calldata;
1304 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001305
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001306 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1307 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001308 /*
1309 * Check if we still need to send an OPEN call, or if we can use
1310 * a delegation instead.
1311 */
1312 if (data->state != NULL) {
1313 struct nfs_delegation *delegation;
1314
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001315 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001316 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001317 rcu_read_lock();
1318 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1319 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001320 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001321 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001322 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001323 }
1324 rcu_read_unlock();
1325 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001326 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001327 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001328 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001329 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001330 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001331 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1332 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001333 data->timestamp = jiffies;
Andy Adamsond8985282009-04-01 09:22:21 -04001334 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1335 &data->o_arg.seq_args,
1336 &data->o_res.seq_res, 1, task))
1337 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001338 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001339 return;
1340out_no_action:
1341 task->tk_action = NULL;
1342
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001343}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001345static void nfs4_open_done(struct rpc_task *task, void *calldata)
1346{
1347 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001349 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001350
1351 nfs4_sequence_done_free_slot(data->o_arg.server, &data->o_res.seq_res,
1352 task->tk_status);
1353
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001354 if (RPC_ASSASSINATED(task))
1355 return;
1356 if (task->tk_status == 0) {
1357 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001358 case S_IFREG:
1359 break;
1360 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001361 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001362 break;
1363 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001364 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001365 break;
1366 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001367 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001368 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001369 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001370 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1371 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001372 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001373 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001374}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001375
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001376static void nfs4_open_release(void *calldata)
1377{
1378 struct nfs4_opendata *data = calldata;
1379 struct nfs4_state *state = NULL;
1380
1381 /* If this request hasn't been cancelled, do nothing */
1382 if (data->cancelled == 0)
1383 goto out_free;
1384 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001385 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001386 goto out_free;
1387 /* In case we need an open_confirm, no cleanup! */
1388 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1389 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001390 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001391 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001392 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001393out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001394 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001395}
1396
1397static const struct rpc_call_ops nfs4_open_ops = {
1398 .rpc_call_prepare = nfs4_open_prepare,
1399 .rpc_call_done = nfs4_open_done,
1400 .rpc_release = nfs4_open_release,
1401};
1402
1403/*
1404 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1405 */
1406static int _nfs4_proc_open(struct nfs4_opendata *data)
1407{
1408 struct inode *dir = data->dir->d_inode;
1409 struct nfs_server *server = NFS_SERVER(dir);
1410 struct nfs_openargs *o_arg = &data->o_arg;
1411 struct nfs_openres *o_res = &data->o_res;
1412 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001413 struct rpc_message msg = {
1414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1415 .rpc_argp = o_arg,
1416 .rpc_resp = o_res,
1417 .rpc_cred = data->owner->so_cred,
1418 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001419 struct rpc_task_setup task_setup_data = {
1420 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001421 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001422 .callback_ops = &nfs4_open_ops,
1423 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001424 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001425 .flags = RPC_TASK_ASYNC,
1426 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001427 int status;
1428
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001429 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001430 data->rpc_done = 0;
1431 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001432 data->cancelled = 0;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001433 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001434 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001435 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001436 status = nfs4_wait_for_completion_rpc_task(task);
1437 if (status != 0) {
1438 data->cancelled = 1;
1439 smp_wmb();
1440 } else
1441 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001442 rpc_put_task(task);
Trond Myklebust3e309912007-07-07 13:19:59 -04001443 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001444 return status;
1445
Trond Myklebust99367812007-07-17 21:52:41 -04001446 if (o_res->fh.size == 0)
1447 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1448
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001449 if (o_arg->open_flags & O_CREAT) {
1450 update_changeattr(dir, &o_res->cinfo);
1451 nfs_post_op_update_inode(dir, o_res->dir_attr);
1452 } else
1453 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001455 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001457 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001460 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001461 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
Trond Myklebust10afec92007-05-14 17:16:04 -04001464static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001465{
David Howells7539bba2006-08-22 20:06:09 -04001466 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001467 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001468 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001469
Trond Myklebusta78cb572009-08-09 15:06:19 -04001470 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001471 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001472 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001473 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001474 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1475 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001476 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001477 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001478 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001479 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001480 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001481}
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483/*
1484 * OPEN_EXPIRED:
1485 * reclaim state on the server after a network partition.
1486 * Assumes caller holds the appropriate lock
1487 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001488static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001490 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001491 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001493 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1494 if (IS_ERR(opendata))
1495 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001496 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001497 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001498 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001499 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001500 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001503static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001504{
Trond Myklebust539cd032007-06-05 11:46:42 -04001505 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001506 struct nfs4_exception exception = { };
1507 int err;
1508
1509 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001510 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001511 switch (err) {
1512 default:
1513 goto out;
1514 case -NFS4ERR_GRACE:
1515 case -NFS4ERR_DELAY:
1516 nfs4_handle_exception(server, err, &exception);
1517 err = 0;
1518 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001519 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001520out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001521 return err;
1522}
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1525{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001527 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Trond Myklebust864472e2006-01-03 09:55:15 +01001529 ctx = nfs4_state_find_open_context(state);
1530 if (IS_ERR(ctx))
1531 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001532 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001533 put_nfs_open_context(ctx);
1534 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
1537/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001538 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1539 * fields corresponding to attributes that were used to store the verifier.
1540 * Make sure we clobber those fields in the later setattr call
1541 */
1542static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1543{
1544 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1545 !(sattr->ia_valid & ATTR_ATIME_SET))
1546 sattr->ia_valid |= ATTR_ATIME;
1547
1548 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1549 !(sattr->ia_valid & ATTR_MTIME_SET))
1550 sattr->ia_valid |= ATTR_MTIME;
1551}
1552
1553/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001554 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001556static 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 -07001557{
1558 struct nfs4_state_owner *sp;
1559 struct nfs4_state *state = NULL;
1560 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001561 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001562 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 status = -ENOMEM;
1566 if (!(sp = nfs4_get_state_owner(server, cred))) {
1567 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1568 goto out_err;
1569 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001570 status = nfs4_recover_expired_lease(server);
1571 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001572 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001573 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001574 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001575 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001576 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001577 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001578 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Trond Myklebustaac00a82007-07-05 19:02:21 -04001580 if (path->dentry->d_inode != NULL)
1581 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1582
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001583 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001585 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001587 if (opendata->o_arg.open_flags & O_EXCL)
1588 nfs4_exclusive_attrset(opendata, sattr);
1589
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001590 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001591 status = PTR_ERR(state);
1592 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001593 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001594 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 *res = state;
1597 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001598err_opendata_put:
1599 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001600err_put_state_owner:
1601 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 *res = NULL;
1604 return status;
1605}
1606
1607
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001608static 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 -07001609{
1610 struct nfs4_exception exception = { };
1611 struct nfs4_state *res;
1612 int status;
1613
1614 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001615 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 if (status == 0)
1617 break;
1618 /* NOTE: BAD_SEQID means the server and client disagree about the
1619 * book-keeping w.r.t. state-changing operations
1620 * (OPEN/CLOSE/LOCK/LOCKU...)
1621 * It is actually a sign of a bug on the client or on the server.
1622 *
1623 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001624 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 * have unhashed the old state_owner for us, and that we can
1626 * therefore safely retry using a new one. We should still warn
1627 * the user though...
1628 */
1629 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001630 printk(KERN_WARNING "NFS: v4 server %s "
1631 " returned a bad sequence-id error!\n",
1632 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 exception.retry = 1;
1634 continue;
1635 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001636 /*
1637 * BAD_STATEID on OPEN means that the server cancelled our
1638 * state before it received the OPEN_CONFIRM.
1639 * Recover by retrying the request as per the discussion
1640 * on Page 181 of RFC3530.
1641 */
1642 if (status == -NFS4ERR_BAD_STATEID) {
1643 exception.retry = 1;
1644 continue;
1645 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001646 if (status == -EAGAIN) {
1647 /* We must have found a delegation */
1648 exception.retry = 1;
1649 continue;
1650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1652 status, &exception));
1653 } while (exception.retry);
1654 return res;
1655}
1656
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001657static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1658 struct nfs_fattr *fattr, struct iattr *sattr,
1659 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001661 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001663 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 .iap = sattr,
1665 .server = server,
1666 .bitmask = server->attr_bitmask,
1667 };
1668 struct nfs_setattrres res = {
1669 .fattr = fattr,
1670 .server = server,
1671 };
1672 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001673 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1674 .rpc_argp = &arg,
1675 .rpc_resp = &res,
1676 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001678 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001679 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Trond Myklebust0e574af2005-10-27 22:12:38 -04001681 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001683 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1684 /* Use that stateid */
1685 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001686 nfs4_copy_stateid(&arg.stateid, state, current->files);
1687 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1689
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001690 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001691 if (status == 0 && state != NULL)
1692 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001693 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001696static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1697 struct nfs_fattr *fattr, struct iattr *sattr,
1698 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001700 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 struct nfs4_exception exception = { };
1702 int err;
1703 do {
1704 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001705 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 &exception);
1707 } while (exception.retry);
1708 return err;
1709}
1710
1711struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001712 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 struct inode *inode;
1714 struct nfs4_state *state;
1715 struct nfs_closeargs arg;
1716 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001717 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001718 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719};
1720
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001721static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001722{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001723 struct nfs4_closedata *calldata = data;
1724 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001725
1726 nfs4_put_open_state(calldata->state);
1727 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001728 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001729 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001730 kfree(calldata);
1731}
1732
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001733static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001735 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 struct nfs_server *server = NFS_SERVER(calldata->inode);
1738
Andy Adamson19ddab02009-04-01 09:22:20 -04001739 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001740 if (RPC_ASSASSINATED(task))
1741 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 /* hmm. we are done with the inode, and in the process of freeing
1743 * the state_owner. we keep this around to process errors
1744 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 switch (task->tk_status) {
1746 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001747 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001748 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 break;
1750 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001751 case -NFS4ERR_OLD_STATEID:
1752 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001754 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001755 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001757 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Andy Adamsone608e792009-12-04 15:55:29 -05001758 nfs4_restart_rpc(task, server->nfs_client,
1759 &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 return;
1761 }
1762 }
Andy Adamson19ddab02009-04-01 09:22:20 -04001763 nfs4_sequence_free_slot(server->nfs_client, &calldata->res.seq_res);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001764 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765}
1766
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001767static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001769 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001770 struct nfs4_state *state = calldata->state;
Trond Myklebust003707c2007-07-05 18:07:55 -04001771 int clear_rd, clear_wr, clear_rdwr;
Trond Myklebust95121352005-10-18 14:20:12 -07001772
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001773 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001774 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001775
Trond Myklebust003707c2007-07-05 18:07:55 -04001776 clear_rd = clear_wr = clear_rdwr = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001777 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001778 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001779 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001780 if (state->n_rdonly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001781 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1782 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1783 }
1784 if (state->n_wronly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001785 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1786 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1787 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001788 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001789 spin_unlock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001790 if (!clear_rd && !clear_wr && !clear_rdwr) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001791 /* Note: exit _without_ calling nfs4_close_done */
1792 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001793 return;
1794 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001795 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust45328c32007-07-26 17:47:34 -04001796 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001797 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001798 calldata->arg.fmode = FMODE_READ;
Trond Myklebust45328c32007-07-26 17:47:34 -04001799 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001800 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001801 calldata->arg.fmode = FMODE_WRITE;
Trond Myklebust45328c32007-07-26 17:47:34 -04001802 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001803 calldata->timestamp = jiffies;
Andy Adamson19ddab02009-04-01 09:22:20 -04001804 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1805 &calldata->arg.seq_args, &calldata->res.seq_res,
1806 1, task))
1807 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001808 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001811static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001812 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001813 .rpc_call_done = nfs4_close_done,
1814 .rpc_release = nfs4_free_closedata,
1815};
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817/*
1818 * It is possible for data to be read/written from a mem-mapped file
1819 * after the sys_close call (which hits the vfs layer as a flush).
1820 * This means that we can't safely call nfsv4 close on a file until
1821 * the inode is cleared. This in turn means that we are not good
1822 * NFSv4 citizens - we do not indicate to the server to update the file's
1823 * share state even when we are done with one of the three share
1824 * stateid's in the inode.
1825 *
1826 * NOTE: Caller must be holding the sp->so_owner semaphore!
1827 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001828int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001830 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001832 struct nfs4_state_owner *sp = state->owner;
1833 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001834 struct rpc_message msg = {
1835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1836 .rpc_cred = state->owner->so_cred,
1837 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001838 struct rpc_task_setup task_setup_data = {
1839 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001840 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001841 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001842 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001843 .flags = RPC_TASK_ASYNC,
1844 };
Trond Myklebust95121352005-10-18 14:20:12 -07001845 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Andy Adamson19ddab02009-04-01 09:22:20 -04001847 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001849 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001850 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001852 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001853 calldata->arg.stateid = &state->open_stateid;
Benny Halevy34dc1ad2009-04-01 09:22:52 -04001854 if (nfs4_has_session(server->nfs_client))
1855 memset(calldata->arg.stateid->data, 0, 4); /* clear seqid */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001857 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001858 if (calldata->arg.seqid == NULL)
1859 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001860 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001861 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001862 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001863 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001864 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001865 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001866 calldata->path.mnt = mntget(path->mnt);
1867 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001868
Trond Myklebust5138fde2007-07-14 15:40:01 -04001869 msg.rpc_argp = &calldata->arg,
1870 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001871 task_setup_data.callback_data = calldata;
1872 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001873 if (IS_ERR(task))
1874 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001875 status = 0;
1876 if (wait)
1877 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001878 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001879 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001880out_free_calldata:
1881 kfree(calldata);
1882out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001883 nfs4_put_open_state(state);
1884 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001885 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001888static 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 -07001889{
1890 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001891 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001892
Trond Myklebust1b45c462007-07-03 13:04:56 -04001893 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001894 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001895 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001896 state->owner->so_cred,
1897 nd->intent.open.flags);
1898 if (ret < 0)
1899 goto out_close;
1900 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001901 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001902 if (!IS_ERR(filp)) {
1903 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001904 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001905 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001906 return 0;
1907 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001908 ret = PTR_ERR(filp);
1909out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001910 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001911 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001912}
1913
1914struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1916{
Trond Myklebustad389da2007-06-05 12:30:00 -04001917 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001918 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001919 .dentry = dentry,
1920 };
Jan Blunck4ac91372008-02-14 19:34:32 -08001921 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 struct iattr attr;
1923 struct rpc_cred *cred;
1924 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001925 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001926 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 if (nd->flags & LOOKUP_CREATE) {
1929 attr.ia_mode = nd->intent.open.create_mode;
1930 attr.ia_valid = ATTR_MODE;
1931 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04001932 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 } else {
1934 attr.ia_valid = 0;
1935 BUG_ON(nd->intent.open.flags & O_CREAT);
1936 }
1937
Trond Myklebust98a8e322008-03-12 12:25:28 -04001938 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001940 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04001941 parent = dentry->d_parent;
1942 /* Protect against concurrent sillydeletes */
1943 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001944 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001946 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001947 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001948 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04001949 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1950 }
Trond Myklebust565277f2007-10-15 18:17:53 -04001951 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001952 return (struct dentry *)state;
1953 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001954 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001955 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04001956 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04001957 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001958 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001959 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001960 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962
1963int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001964nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
Trond Myklebustad389da2007-06-05 12:30:00 -04001966 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001967 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001968 .dentry = dentry,
1969 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 struct rpc_cred *cred;
1971 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001972 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Trond Myklebust98a8e322008-03-12 12:25:28 -04001974 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (IS_ERR(cred))
1976 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001977 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001979 if (IS_ERR(state)) {
1980 switch (PTR_ERR(state)) {
1981 case -EPERM:
1982 case -EACCES:
1983 case -EDQUOT:
1984 case -ENOSPC:
1985 case -EROFS:
1986 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1987 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001988 default:
1989 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001990 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001991 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001992 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001993 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001994 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return 1;
1996 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001997 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001998out_drop:
1999 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 return 0;
2001}
2002
Trond Myklebust1185a552009-12-03 15:54:02 -05002003static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002004{
2005 if (ctx->state == NULL)
2006 return;
2007 if (is_sync)
2008 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2009 else
2010 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2011}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2014{
Benny Halevy43652ad2009-04-01 09:21:54 -04002015 struct nfs4_server_caps_arg args = {
2016 .fhandle = fhandle,
2017 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 struct nfs4_server_caps_res res = {};
2019 struct rpc_message msg = {
2020 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002021 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 .rpc_resp = &res,
2023 };
2024 int status;
2025
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002026 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 if (status == 0) {
2028 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002029 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2030 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2031 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2032 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2033 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2035 server->caps |= NFS_CAP_ACLS;
2036 if (res.has_links != 0)
2037 server->caps |= NFS_CAP_HARDLINKS;
2038 if (res.has_symlinks != 0)
2039 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002040 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2041 server->caps |= NFS_CAP_FILEID;
2042 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2043 server->caps |= NFS_CAP_MODE;
2044 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2045 server->caps |= NFS_CAP_NLINK;
2046 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2047 server->caps |= NFS_CAP_OWNER;
2048 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2049 server->caps |= NFS_CAP_OWNER_GROUP;
2050 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2051 server->caps |= NFS_CAP_ATIME;
2052 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2053 server->caps |= NFS_CAP_CTIME;
2054 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2055 server->caps |= NFS_CAP_MTIME;
2056
Trond Myklebusta65318b2009-03-11 14:10:28 -04002057 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2058 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2059 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 server->acl_bitmask = res.acl_bitmask;
2061 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return status;
2064}
2065
Trond Myklebust55a97592006-06-09 09:34:19 -04002066int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
2068 struct nfs4_exception exception = { };
2069 int err;
2070 do {
2071 err = nfs4_handle_exception(server,
2072 _nfs4_server_capabilities(server, fhandle),
2073 &exception);
2074 } while (exception.retry);
2075 return err;
2076}
2077
2078static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2079 struct nfs_fsinfo *info)
2080{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 struct nfs4_lookup_root_arg args = {
2082 .bitmask = nfs4_fattr_bitmap,
2083 };
2084 struct nfs4_lookup_res res = {
2085 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002086 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 .fh = fhandle,
2088 };
2089 struct rpc_message msg = {
2090 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2091 .rpc_argp = &args,
2092 .rpc_resp = &res,
2093 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002094
Trond Myklebust0e574af2005-10-27 22:12:38 -04002095 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002096 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097}
2098
2099static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2100 struct nfs_fsinfo *info)
2101{
2102 struct nfs4_exception exception = { };
2103 int err;
2104 do {
2105 err = nfs4_handle_exception(server,
2106 _nfs4_lookup_root(server, fhandle, info),
2107 &exception);
2108 } while (exception.retry);
2109 return err;
2110}
2111
David Howells54ceac42006-08-22 20:06:13 -04002112/*
2113 * get the file handle for the "/" directory on the server
2114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002116 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 int status;
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 if (status == 0)
2122 status = nfs4_server_capabilities(server, fhandle);
2123 if (status == 0)
2124 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002125 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
Manoj Naik6b97fd32006-06-09 09:34:29 -04002128/*
2129 * Get locations and (maybe) other attributes of a referral.
2130 * Note that we'll actually follow the referral later when
2131 * we detect fsid mismatch in inode revalidation
2132 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002133static 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 -04002134{
2135 int status = -ENOMEM;
2136 struct page *page = NULL;
2137 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002138
2139 page = alloc_page(GFP_KERNEL);
2140 if (page == NULL)
2141 goto out;
2142 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2143 if (locations == NULL)
2144 goto out;
2145
Trond Myklebustc228fd32007-01-13 02:28:11 -05002146 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002147 if (status != 0)
2148 goto out;
2149 /* Make sure server returned a different fsid for the referral */
2150 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002151 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 -04002152 status = -EIO;
2153 goto out;
2154 }
2155
2156 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2157 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2158 if (!fattr->mode)
2159 fattr->mode = S_IFDIR;
2160 memset(fhandle, 0, sizeof(struct nfs_fh));
2161out:
2162 if (page)
2163 __free_page(page);
2164 if (locations)
2165 kfree(locations);
2166 return status;
2167}
2168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2170{
2171 struct nfs4_getattr_arg args = {
2172 .fh = fhandle,
2173 .bitmask = server->attr_bitmask,
2174 };
2175 struct nfs4_getattr_res res = {
2176 .fattr = fattr,
2177 .server = server,
2178 };
2179 struct rpc_message msg = {
2180 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2181 .rpc_argp = &args,
2182 .rpc_resp = &res,
2183 };
2184
Trond Myklebust0e574af2005-10-27 22:12:38 -04002185 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002186 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187}
2188
2189static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2190{
2191 struct nfs4_exception exception = { };
2192 int err;
2193 do {
2194 err = nfs4_handle_exception(server,
2195 _nfs4_proc_getattr(server, fhandle, fattr),
2196 &exception);
2197 } while (exception.retry);
2198 return err;
2199}
2200
2201/*
2202 * The file is not closed if it is opened due to the a request to change
2203 * the size of the file. The open call will not be needed once the
2204 * VFS layer lookup-intents are implemented.
2205 *
2206 * Close is called when the inode is destroyed.
2207 * If we haven't opened the file for O_WRONLY, we
2208 * need to in the size_change case to obtain a stateid.
2209 *
2210 * Got race?
2211 * Because OPEN is always done by name in nfsv4, it is
2212 * possible that we opened a different file by the same
2213 * name. We can recognize this race condition, but we
2214 * can't do anything about it besides returning an error.
2215 *
2216 * This will be fixed with VFS changes (lookup-intent).
2217 */
2218static int
2219nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2220 struct iattr *sattr)
2221{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002222 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002223 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002224 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 int status;
2226
Trond Myklebust0e574af2005-10-27 22:12:38 -04002227 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Trond Myklebustd5308382005-11-04 15:33:38 -05002229 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002230 if (sattr->ia_valid & ATTR_FILE) {
2231 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002232
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002233 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002234 if (ctx) {
2235 cred = ctx->cred;
2236 state = ctx->state;
2237 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002238 }
2239
2240 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002241 if (status == 0)
2242 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 return status;
2244}
2245
Trond Myklebust56659e92007-07-17 21:52:39 -04002246static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2247 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002248 struct nfs_fattr *fattr)
2249{
2250 int status;
2251 struct nfs4_lookup_arg args = {
2252 .bitmask = server->attr_bitmask,
2253 .dir_fh = dirfh,
2254 .name = name,
2255 };
2256 struct nfs4_lookup_res res = {
2257 .server = server,
2258 .fattr = fattr,
2259 .fh = fhandle,
2260 };
2261 struct rpc_message msg = {
2262 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2263 .rpc_argp = &args,
2264 .rpc_resp = &res,
2265 };
2266
2267 nfs_fattr_init(fattr);
2268
2269 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002270 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002271 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002272 return status;
2273}
2274
2275static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2276 struct qstr *name, struct nfs_fh *fhandle,
2277 struct nfs_fattr *fattr)
2278{
2279 struct nfs4_exception exception = { };
2280 int err;
2281 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002282 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2283 /* FIXME: !!!! */
2284 if (err == -NFS4ERR_MOVED) {
2285 err = -EREMOTE;
2286 break;
2287 }
2288 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002289 } while (exception.retry);
2290 return err;
2291}
2292
Trond Myklebust56659e92007-07-17 21:52:39 -04002293static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2295{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002296 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002299 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002300 if (status == -NFS4ERR_MOVED)
2301 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 dprintk("NFS reply lookup: %d\n", status);
2303 return status;
2304}
2305
2306static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2307{
2308 struct nfs4_exception exception = { };
2309 int err;
2310 do {
2311 err = nfs4_handle_exception(NFS_SERVER(dir),
2312 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2313 &exception);
2314 } while (exception.retry);
2315 return err;
2316}
2317
2318static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2319{
Trond Myklebust76b32992007-08-10 17:45:11 -04002320 struct nfs_server *server = NFS_SERVER(inode);
2321 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 struct nfs4_accessargs args = {
2323 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002324 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002326 struct nfs4_accessres res = {
2327 .server = server,
2328 .fattr = &fattr,
2329 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 struct rpc_message msg = {
2331 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2332 .rpc_argp = &args,
2333 .rpc_resp = &res,
2334 .rpc_cred = entry->cred,
2335 };
2336 int mode = entry->mask;
2337 int status;
2338
2339 /*
2340 * Determine which access bits we want to ask for...
2341 */
2342 if (mode & MAY_READ)
2343 args.access |= NFS4_ACCESS_READ;
2344 if (S_ISDIR(inode->i_mode)) {
2345 if (mode & MAY_WRITE)
2346 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2347 if (mode & MAY_EXEC)
2348 args.access |= NFS4_ACCESS_LOOKUP;
2349 } else {
2350 if (mode & MAY_WRITE)
2351 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2352 if (mode & MAY_EXEC)
2353 args.access |= NFS4_ACCESS_EXECUTE;
2354 }
Trond Myklebust76b32992007-08-10 17:45:11 -04002355 nfs_fattr_init(&fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002356 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 if (!status) {
2358 entry->mask = 0;
2359 if (res.access & NFS4_ACCESS_READ)
2360 entry->mask |= MAY_READ;
2361 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2362 entry->mask |= MAY_WRITE;
2363 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2364 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04002365 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 }
2367 return status;
2368}
2369
2370static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2371{
2372 struct nfs4_exception exception = { };
2373 int err;
2374 do {
2375 err = nfs4_handle_exception(NFS_SERVER(inode),
2376 _nfs4_proc_access(inode, entry),
2377 &exception);
2378 } while (exception.retry);
2379 return err;
2380}
2381
2382/*
2383 * TODO: For the time being, we don't try to get any attributes
2384 * along with any of the zero-copy operations READ, READDIR,
2385 * READLINK, WRITE.
2386 *
2387 * In the case of the first three, we want to put the GETATTR
2388 * after the read-type operation -- this is because it is hard
2389 * to predict the length of a GETATTR response in v4, and thus
2390 * align the READ data correctly. This means that the GETATTR
2391 * may end up partially falling into the page cache, and we should
2392 * shift it into the 'tail' of the xdr_buf before processing.
2393 * To do this efficiently, we need to know the total length
2394 * of data received, which doesn't seem to be available outside
2395 * of the RPC layer.
2396 *
2397 * In the case of WRITE, we also want to put the GETATTR after
2398 * the operation -- in this case because we want to make sure
2399 * we get the post-operation mtime and size. This means that
2400 * we can't use xdr_encode_pages() as written: we need a variant
2401 * of it which would leave room in the 'tail' iovec.
2402 *
2403 * Both of these changes to the XDR layer would in fact be quite
2404 * minor, but I decided to leave them for a subsequent patch.
2405 */
2406static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2407 unsigned int pgbase, unsigned int pglen)
2408{
2409 struct nfs4_readlink args = {
2410 .fh = NFS_FH(inode),
2411 .pgbase = pgbase,
2412 .pglen = pglen,
2413 .pages = &page,
2414 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002415 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 struct rpc_message msg = {
2417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2418 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002419 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 };
2421
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002422 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423}
2424
2425static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2426 unsigned int pgbase, unsigned int pglen)
2427{
2428 struct nfs4_exception exception = { };
2429 int err;
2430 do {
2431 err = nfs4_handle_exception(NFS_SERVER(inode),
2432 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2433 &exception);
2434 } while (exception.retry);
2435 return err;
2436}
2437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438/*
2439 * Got race?
2440 * We will need to arrange for the VFS layer to provide an atomic open.
2441 * Until then, this create/open method is prone to inefficiency and race
2442 * conditions due to the lookup, create, and open VFS calls from sys_open()
2443 * placed on the wire.
2444 *
2445 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2446 * The file will be opened again in the subsequent VFS open call
2447 * (nfs4_proc_file_open).
2448 *
2449 * The open for read will just hang around to be used by any process that
2450 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2451 */
2452
2453static int
2454nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002455 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
Trond Myklebustad389da2007-06-05 12:30:00 -04002457 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002458 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002459 .dentry = dentry,
2460 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 struct nfs4_state *state;
2462 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002463 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 int status = 0;
2465
Trond Myklebust98a8e322008-03-12 12:25:28 -04002466 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 if (IS_ERR(cred)) {
2468 status = PTR_ERR(cred);
2469 goto out;
2470 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002471 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002472 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 if (IS_ERR(state)) {
2474 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002475 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002477 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002478 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 if (flags & O_EXCL) {
2480 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002481 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002482 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002483 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002484 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002485 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002486 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002487 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002488 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002489 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002490out_putcred:
2491 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492out:
2493 return status;
2494}
2495
2496static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2497{
Trond Myklebust16e42952005-10-27 22:12:44 -04002498 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002499 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002501 .name.len = name->len,
2502 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002503 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002505 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002506 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002507 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2510 .rpc_argp = &args,
2511 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 };
2513 int status;
2514
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002515 nfs_fattr_init(&res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002516 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002517 if (status == 0) {
2518 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002519 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 return status;
2522}
2523
2524static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2525{
2526 struct nfs4_exception exception = { };
2527 int err;
2528 do {
2529 err = nfs4_handle_exception(NFS_SERVER(dir),
2530 _nfs4_proc_remove(dir, name),
2531 &exception);
2532 } while (exception.retry);
2533 return err;
2534}
2535
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002536static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002538 struct nfs_server *server = NFS_SERVER(dir);
2539 struct nfs_removeargs *args = msg->rpc_argp;
2540 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Trond Myklebusta65318b2009-03-11 14:10:28 -04002542 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002543 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
2546
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002547static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002549 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2550
Andy Adamson472cfbd2009-04-01 09:22:24 -04002551 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002552 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002553 return 0;
Andy Adamson472cfbd2009-04-01 09:22:24 -04002554 nfs4_sequence_free_slot(res->server->nfs_client, &res->seq_res);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002555 update_changeattr(dir, &res->cinfo);
2556 nfs_post_op_update_inode(dir, &res->dir_attr);
2557 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558}
2559
2560static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2561 struct inode *new_dir, struct qstr *new_name)
2562{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002563 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 struct nfs4_rename_arg arg = {
2565 .old_dir = NFS_FH(old_dir),
2566 .new_dir = NFS_FH(new_dir),
2567 .old_name = old_name,
2568 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002569 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002571 struct nfs_fattr old_fattr, new_fattr;
2572 struct nfs4_rename_res res = {
2573 .server = server,
2574 .old_fattr = &old_fattr,
2575 .new_fattr = &new_fattr,
2576 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 struct rpc_message msg = {
2578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2579 .rpc_argp = &arg,
2580 .rpc_resp = &res,
2581 };
2582 int status;
2583
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002584 nfs_fattr_init(res.old_fattr);
2585 nfs_fattr_init(res.new_fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002586 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
2588 if (!status) {
2589 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002590 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002592 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 }
2594 return status;
2595}
2596
2597static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2598 struct inode *new_dir, struct qstr *new_name)
2599{
2600 struct nfs4_exception exception = { };
2601 int err;
2602 do {
2603 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2604 _nfs4_proc_rename(old_dir, old_name,
2605 new_dir, new_name),
2606 &exception);
2607 } while (exception.retry);
2608 return err;
2609}
2610
2611static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2612{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002613 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 struct nfs4_link_arg arg = {
2615 .fh = NFS_FH(inode),
2616 .dir_fh = NFS_FH(dir),
2617 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002618 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002620 struct nfs_fattr fattr, dir_attr;
2621 struct nfs4_link_res res = {
2622 .server = server,
2623 .fattr = &fattr,
2624 .dir_attr = &dir_attr,
2625 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 struct rpc_message msg = {
2627 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2628 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002629 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 };
2631 int status;
2632
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002633 nfs_fattr_init(res.fattr);
2634 nfs_fattr_init(res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002635 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002636 if (!status) {
2637 update_changeattr(dir, &res.cinfo);
2638 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002639 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
2642 return status;
2643}
2644
2645static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2646{
2647 struct nfs4_exception exception = { };
2648 int err;
2649 do {
2650 err = nfs4_handle_exception(NFS_SERVER(inode),
2651 _nfs4_proc_link(inode, dir, name),
2652 &exception);
2653 } while (exception.retry);
2654 return err;
2655}
2656
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002657struct nfs4_createdata {
2658 struct rpc_message msg;
2659 struct nfs4_create_arg arg;
2660 struct nfs4_create_res res;
2661 struct nfs_fh fh;
2662 struct nfs_fattr fattr;
2663 struct nfs_fattr dir_fattr;
2664};
2665
2666static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2667 struct qstr *name, struct iattr *sattr, u32 ftype)
2668{
2669 struct nfs4_createdata *data;
2670
2671 data = kzalloc(sizeof(*data), GFP_KERNEL);
2672 if (data != NULL) {
2673 struct nfs_server *server = NFS_SERVER(dir);
2674
2675 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2676 data->msg.rpc_argp = &data->arg;
2677 data->msg.rpc_resp = &data->res;
2678 data->arg.dir_fh = NFS_FH(dir);
2679 data->arg.server = server;
2680 data->arg.name = name;
2681 data->arg.attrs = sattr;
2682 data->arg.ftype = ftype;
2683 data->arg.bitmask = server->attr_bitmask;
2684 data->res.server = server;
2685 data->res.fh = &data->fh;
2686 data->res.fattr = &data->fattr;
2687 data->res.dir_fattr = &data->dir_fattr;
2688 nfs_fattr_init(data->res.fattr);
2689 nfs_fattr_init(data->res.dir_fattr);
2690 }
2691 return data;
2692}
2693
2694static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2695{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002696 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2697 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002698 if (status == 0) {
2699 update_changeattr(dir, &data->res.dir_cinfo);
2700 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2701 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2702 }
2703 return status;
2704}
2705
2706static void nfs4_free_createdata(struct nfs4_createdata *data)
2707{
2708 kfree(data);
2709}
2710
Chuck Lever4f390c12006-08-22 20:06:22 -04002711static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002712 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002714 struct nfs4_createdata *data;
2715 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
Chuck Lever94a6d752006-08-22 20:06:23 -04002717 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002718 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002719
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002720 status = -ENOMEM;
2721 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2722 if (data == NULL)
2723 goto out;
2724
2725 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2726 data->arg.u.symlink.pages = &page;
2727 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002729 status = nfs4_do_create(dir, dentry, data);
2730
2731 nfs4_free_createdata(data);
2732out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 return status;
2734}
2735
Chuck Lever4f390c12006-08-22 20:06:22 -04002736static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002737 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
2739 struct nfs4_exception exception = { };
2740 int err;
2741 do {
2742 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002743 _nfs4_proc_symlink(dir, dentry, page,
2744 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 &exception);
2746 } while (exception.retry);
2747 return err;
2748}
2749
2750static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2751 struct iattr *sattr)
2752{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002753 struct nfs4_createdata *data;
2754 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002756 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2757 if (data == NULL)
2758 goto out;
2759
2760 status = nfs4_do_create(dir, dentry, data);
2761
2762 nfs4_free_createdata(data);
2763out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 return status;
2765}
2766
2767static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2768 struct iattr *sattr)
2769{
2770 struct nfs4_exception exception = { };
2771 int err;
2772 do {
2773 err = nfs4_handle_exception(NFS_SERVER(dir),
2774 _nfs4_proc_mkdir(dir, dentry, sattr),
2775 &exception);
2776 } while (exception.retry);
2777 return err;
2778}
2779
2780static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2781 u64 cookie, struct page *page, unsigned int count, int plus)
2782{
2783 struct inode *dir = dentry->d_inode;
2784 struct nfs4_readdir_arg args = {
2785 .fh = NFS_FH(dir),
2786 .pages = &page,
2787 .pgbase = 0,
2788 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002789 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 };
2791 struct nfs4_readdir_res res;
2792 struct rpc_message msg = {
2793 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2794 .rpc_argp = &args,
2795 .rpc_resp = &res,
2796 .rpc_cred = cred,
2797 };
2798 int status;
2799
Harvey Harrison3110ff82008-05-02 13:42:44 -07002800 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002801 dentry->d_parent->d_name.name,
2802 dentry->d_name.name,
2803 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2805 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002806 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 if (status == 0)
2808 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002809
2810 nfs_invalidate_atime(dir);
2811
Harvey Harrison3110ff82008-05-02 13:42:44 -07002812 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 return status;
2814}
2815
2816static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2817 u64 cookie, struct page *page, unsigned int count, int plus)
2818{
2819 struct nfs4_exception exception = { };
2820 int err;
2821 do {
2822 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2823 _nfs4_proc_readdir(dentry, cred, cookie,
2824 page, count, plus),
2825 &exception);
2826 } while (exception.retry);
2827 return err;
2828}
2829
2830static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2831 struct iattr *sattr, dev_t rdev)
2832{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002833 struct nfs4_createdata *data;
2834 int mode = sattr->ia_mode;
2835 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
2837 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2838 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002839
2840 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2841 if (data == NULL)
2842 goto out;
2843
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002845 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002847 data->arg.ftype = NF4BLK;
2848 data->arg.u.device.specdata1 = MAJOR(rdev);
2849 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 }
2851 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002852 data->arg.ftype = NF4CHR;
2853 data->arg.u.device.specdata1 = MAJOR(rdev);
2854 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002857 status = nfs4_do_create(dir, dentry, data);
2858
2859 nfs4_free_createdata(data);
2860out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 return status;
2862}
2863
2864static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2865 struct iattr *sattr, dev_t rdev)
2866{
2867 struct nfs4_exception exception = { };
2868 int err;
2869 do {
2870 err = nfs4_handle_exception(NFS_SERVER(dir),
2871 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2872 &exception);
2873 } while (exception.retry);
2874 return err;
2875}
2876
2877static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2878 struct nfs_fsstat *fsstat)
2879{
2880 struct nfs4_statfs_arg args = {
2881 .fh = fhandle,
2882 .bitmask = server->attr_bitmask,
2883 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002884 struct nfs4_statfs_res res = {
2885 .fsstat = fsstat,
2886 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 struct rpc_message msg = {
2888 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2889 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002890 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 };
2892
Trond Myklebust0e574af2005-10-27 22:12:38 -04002893 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002894 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895}
2896
2897static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2898{
2899 struct nfs4_exception exception = { };
2900 int err;
2901 do {
2902 err = nfs4_handle_exception(server,
2903 _nfs4_proc_statfs(server, fhandle, fsstat),
2904 &exception);
2905 } while (exception.retry);
2906 return err;
2907}
2908
2909static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2910 struct nfs_fsinfo *fsinfo)
2911{
2912 struct nfs4_fsinfo_arg args = {
2913 .fh = fhandle,
2914 .bitmask = server->attr_bitmask,
2915 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002916 struct nfs4_fsinfo_res res = {
2917 .fsinfo = fsinfo,
2918 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 struct rpc_message msg = {
2920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2921 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002922 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 };
2924
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002925 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926}
2927
2928static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2929{
2930 struct nfs4_exception exception = { };
2931 int err;
2932
2933 do {
2934 err = nfs4_handle_exception(server,
2935 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2936 &exception);
2937 } while (exception.retry);
2938 return err;
2939}
2940
2941static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2942{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002943 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2945}
2946
2947static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2948 struct nfs_pathconf *pathconf)
2949{
2950 struct nfs4_pathconf_arg args = {
2951 .fh = fhandle,
2952 .bitmask = server->attr_bitmask,
2953 };
Benny Halevyd45b2982009-04-01 09:21:58 -04002954 struct nfs4_pathconf_res res = {
2955 .pathconf = pathconf,
2956 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 struct rpc_message msg = {
2958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2959 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04002960 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 };
2962
2963 /* None of the pathconf attributes are mandatory to implement */
2964 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2965 memset(pathconf, 0, sizeof(*pathconf));
2966 return 0;
2967 }
2968
Trond Myklebust0e574af2005-10-27 22:12:38 -04002969 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002970 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971}
2972
2973static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2974 struct nfs_pathconf *pathconf)
2975{
2976 struct nfs4_exception exception = { };
2977 int err;
2978
2979 do {
2980 err = nfs4_handle_exception(server,
2981 _nfs4_proc_pathconf(server, fhandle, pathconf),
2982 &exception);
2983 } while (exception.retry);
2984 return err;
2985}
2986
Trond Myklebustec06c092006-03-20 13:44:27 -05002987static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
Trond Myklebustec06c092006-03-20 13:44:27 -05002989 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Andy Adamsonf11c88a2009-04-01 09:22:25 -04002991 dprintk("--> %s\n", __func__);
2992
2993 /* nfs4_sequence_free_slot called in the read rpc_call_done */
2994 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
2995
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002996 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Andy Adamsone608e792009-12-04 15:55:29 -05002997 nfs4_restart_rpc(task, server->nfs_client, &data->res.seq_res);
Trond Myklebustec06c092006-03-20 13:44:27 -05002998 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003000
3001 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003003 renew_lease(server, data->timestamp);
Andy Adamson36bbe342009-12-04 15:55:30 -05003004 else if (task->tk_status < 0)
3005 nfs4_sequence_free_slot(server->nfs_client, &data->res.seq_res);
3006
Trond Myklebustec06c092006-03-20 13:44:27 -05003007 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008}
3009
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003010static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003013 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014}
3015
Trond Myklebust788e7a82006-03-20 13:44:27 -05003016static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 struct inode *inode = data->inode;
3019
Andy Adamsondef6ed72009-04-01 09:22:26 -04003020 /* slot is freed in nfs_writeback_done */
3021 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3022 task->tk_status);
3023
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003024 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Andy Adamsone608e792009-12-04 15:55:29 -05003025 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client,
3026 &data->res.seq_res);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003027 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003029 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003031 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003032 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003033 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034}
3035
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003036static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003038 struct nfs_server *server = NFS_SERVER(data->inode);
3039
Trond Myklebusta65318b2009-03-11 14:10:28 -04003040 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003041 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 data->timestamp = jiffies;
3043
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003044 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045}
3046
Trond Myklebust788e7a82006-03-20 13:44:27 -05003047static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 struct inode *inode = data->inode;
3050
Andy Adamson21d9a852009-04-01 09:22:27 -04003051 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3052 task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003053 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Andy Adamsone608e792009-12-04 15:55:29 -05003054 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client,
3055 &data->res.seq_res);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003056 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 }
Andy Adamson21d9a852009-04-01 09:22:27 -04003058 nfs4_sequence_free_slot(NFS_SERVER(inode)->nfs_client,
3059 &data->res.seq_res);
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003060 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003061 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062}
3063
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003064static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003066 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
Trond Myklebusta65318b2009-03-11 14:10:28 -04003068 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003069 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003070 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071}
3072
3073/*
3074 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3075 * standalone procedure for queueing an asynchronous RENEW.
3076 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003077static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078{
David Howellsadfa6f92006-08-22 20:06:08 -04003079 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003080 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
3082 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003083 /* Unless we're shutting down, schedule state recovery! */
3084 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3085 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 return;
3087 }
3088 spin_lock(&clp->cl_lock);
3089 if (time_before(clp->cl_last_renewal,timestamp))
3090 clp->cl_last_renewal = timestamp;
3091 spin_unlock(&clp->cl_lock);
3092}
3093
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003094static const struct rpc_call_ops nfs4_renew_ops = {
3095 .rpc_call_done = nfs4_renew_done,
3096};
3097
David Howellsadfa6f92006-08-22 20:06:08 -04003098int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
3100 struct rpc_message msg = {
3101 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3102 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003103 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 };
3105
3106 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003107 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108}
3109
David Howellsadfa6f92006-08-22 20:06:08 -04003110int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
3112 struct rpc_message msg = {
3113 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3114 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003115 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 };
3117 unsigned long now = jiffies;
3118 int status;
3119
3120 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3121 if (status < 0)
3122 return status;
3123 spin_lock(&clp->cl_lock);
3124 if (time_before(clp->cl_last_renewal,now))
3125 clp->cl_last_renewal = now;
3126 spin_unlock(&clp->cl_lock);
3127 return 0;
3128}
3129
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003130static inline int nfs4_server_supports_acls(struct nfs_server *server)
3131{
3132 return (server->caps & NFS_CAP_ACLS)
3133 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3134 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3135}
3136
3137/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3138 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3139 * the stack.
3140 */
3141#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3142
3143static void buf_to_pages(const void *buf, size_t buflen,
3144 struct page **pages, unsigned int *pgbase)
3145{
3146 const void *p = buf;
3147
3148 *pgbase = offset_in_page(buf);
3149 p -= *pgbase;
3150 while (p < buf + buflen) {
3151 *(pages++) = virt_to_page(p);
3152 p += PAGE_CACHE_SIZE;
3153 }
3154}
3155
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003156struct nfs4_cached_acl {
3157 int cached;
3158 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003159 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003160};
3161
3162static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003163{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003164 struct nfs_inode *nfsi = NFS_I(inode);
3165
3166 spin_lock(&inode->i_lock);
3167 kfree(nfsi->nfs4_acl);
3168 nfsi->nfs4_acl = acl;
3169 spin_unlock(&inode->i_lock);
3170}
3171
3172static void nfs4_zap_acl_attr(struct inode *inode)
3173{
3174 nfs4_set_cached_acl(inode, NULL);
3175}
3176
3177static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3178{
3179 struct nfs_inode *nfsi = NFS_I(inode);
3180 struct nfs4_cached_acl *acl;
3181 int ret = -ENOENT;
3182
3183 spin_lock(&inode->i_lock);
3184 acl = nfsi->nfs4_acl;
3185 if (acl == NULL)
3186 goto out;
3187 if (buf == NULL) /* user is just asking for length */
3188 goto out_len;
3189 if (acl->cached == 0)
3190 goto out;
3191 ret = -ERANGE; /* see getxattr(2) man page */
3192 if (acl->len > buflen)
3193 goto out;
3194 memcpy(buf, acl->data, acl->len);
3195out_len:
3196 ret = acl->len;
3197out:
3198 spin_unlock(&inode->i_lock);
3199 return ret;
3200}
3201
3202static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3203{
3204 struct nfs4_cached_acl *acl;
3205
3206 if (buf && acl_len <= PAGE_SIZE) {
3207 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3208 if (acl == NULL)
3209 goto out;
3210 acl->cached = 1;
3211 memcpy(acl->data, buf, acl_len);
3212 } else {
3213 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3214 if (acl == NULL)
3215 goto out;
3216 acl->cached = 0;
3217 }
3218 acl->len = acl_len;
3219out:
3220 nfs4_set_cached_acl(inode, acl);
3221}
3222
Trond Myklebust16b42892006-08-24 12:27:15 -04003223static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003224{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003225 struct page *pages[NFS4ACL_MAXPAGES];
3226 struct nfs_getaclargs args = {
3227 .fh = NFS_FH(inode),
3228 .acl_pages = pages,
3229 .acl_len = buflen,
3230 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003231 struct nfs_getaclres res = {
3232 .acl_len = buflen,
3233 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003234 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003235 struct rpc_message msg = {
3236 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3237 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003238 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003239 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003240 struct page *localpage = NULL;
3241 int ret;
3242
3243 if (buflen < PAGE_SIZE) {
3244 /* As long as we're doing a round trip to the server anyway,
3245 * let's be prepared for a page of acl data. */
3246 localpage = alloc_page(GFP_KERNEL);
3247 resp_buf = page_address(localpage);
3248 if (localpage == NULL)
3249 return -ENOMEM;
3250 args.acl_pages[0] = localpage;
3251 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003252 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003253 } else {
3254 resp_buf = buf;
3255 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3256 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003257 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003258 if (ret)
3259 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003260 if (res.acl_len > args.acl_len)
3261 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003262 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003263 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003264 if (buf) {
3265 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003266 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003267 goto out_free;
3268 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003269 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003270 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003271 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003272out_free:
3273 if (localpage)
3274 __free_page(localpage);
3275 return ret;
3276}
3277
Trond Myklebust16b42892006-08-24 12:27:15 -04003278static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3279{
3280 struct nfs4_exception exception = { };
3281 ssize_t ret;
3282 do {
3283 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3284 if (ret >= 0)
3285 break;
3286 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3287 } while (exception.retry);
3288 return ret;
3289}
3290
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003291static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3292{
3293 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003294 int ret;
3295
3296 if (!nfs4_server_supports_acls(server))
3297 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003298 ret = nfs_revalidate_inode(server, inode);
3299 if (ret < 0)
3300 return ret;
3301 ret = nfs4_read_cached_acl(inode, buf, buflen);
3302 if (ret != -ENOENT)
3303 return ret;
3304 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003305}
3306
Trond Myklebust16b42892006-08-24 12:27:15 -04003307static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003308{
3309 struct nfs_server *server = NFS_SERVER(inode);
3310 struct page *pages[NFS4ACL_MAXPAGES];
3311 struct nfs_setaclargs arg = {
3312 .fh = NFS_FH(inode),
3313 .acl_pages = pages,
3314 .acl_len = buflen,
3315 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003316 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003317 struct rpc_message msg = {
3318 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3319 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003320 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003321 };
3322 int ret;
3323
3324 if (!nfs4_server_supports_acls(server))
3325 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003326 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003327 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003328 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003329 nfs_access_zap_cache(inode);
3330 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003331 return ret;
3332}
3333
Trond Myklebust16b42892006-08-24 12:27:15 -04003334static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3335{
3336 struct nfs4_exception exception = { };
3337 int err;
3338 do {
3339 err = nfs4_handle_exception(NFS_SERVER(inode),
3340 __nfs4_proc_set_acl(inode, buf, buflen),
3341 &exception);
3342 } while (exception.retry);
3343 return err;
3344}
3345
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346static int
Andy Adamson8328d592009-04-01 09:22:35 -04003347_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 -07003348{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 if (!clp || task->tk_status >= 0)
3350 return 0;
3351 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003352 case -NFS4ERR_ADMIN_REVOKED:
3353 case -NFS4ERR_BAD_STATEID:
3354 case -NFS4ERR_OPENMODE:
3355 if (state == NULL)
3356 break;
3357 nfs4_state_mark_reclaim_nograce(clp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 case -NFS4ERR_STALE_CLIENTID:
3359 case -NFS4ERR_STALE_STATEID:
3360 case -NFS4ERR_EXPIRED:
Trond Myklebust5d008372008-02-22 16:34:17 -05003361 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 nfs4_schedule_state_recovery(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05003363 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
Trond Myklebustfda13932008-02-22 16:34:12 -05003364 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 task->tk_status = 0;
3366 return -EAGAIN;
Andy Adamson4745e312009-04-01 09:22:42 -04003367#if defined(CONFIG_NFS_V4_1)
3368 case -NFS4ERR_BADSESSION:
3369 case -NFS4ERR_BADSLOT:
3370 case -NFS4ERR_BAD_HIGH_SLOT:
3371 case -NFS4ERR_DEADSESSION:
3372 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3373 case -NFS4ERR_SEQ_FALSE_RETRY:
3374 case -NFS4ERR_SEQ_MISORDERED:
3375 dprintk("%s ERROR %d, Reset session\n", __func__,
3376 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003377 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003378 task->tk_status = 0;
3379 return -EAGAIN;
3380#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 case -NFS4ERR_DELAY:
Andy Adamson8328d592009-04-01 09:22:35 -04003382 if (server)
3383 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003384 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3386 task->tk_status = 0;
3387 return -EAGAIN;
3388 case -NFS4ERR_OLD_STATEID:
3389 task->tk_status = 0;
3390 return -EAGAIN;
3391 }
3392 task->tk_status = nfs4_map_errors(task->tk_status);
3393 return 0;
3394}
3395
Andy Adamson8328d592009-04-01 09:22:35 -04003396static int
3397nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3398{
3399 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3400}
3401
David Howellsadfa6f92006-08-22 20:06:08 -04003402int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403{
3404 nfs4_verifier sc_verifier;
3405 struct nfs4_setclientid setclientid = {
3406 .sc_verifier = &sc_verifier,
3407 .sc_prog = program,
3408 };
3409 struct rpc_message msg = {
3410 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3411 .rpc_argp = &setclientid,
3412 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003413 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 };
Al Virobc4785c2006-10-19 23:28:51 -07003415 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 int loop = 0;
3417 int status;
3418
Al Virobc4785c2006-10-19 23:28:51 -07003419 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3421 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3422
3423 for(;;) {
3424 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003425 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003426 clp->cl_ipaddr,
3427 rpc_peeraddr2str(clp->cl_rpcclient,
3428 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003429 rpc_peeraddr2str(clp->cl_rpcclient,
3430 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003431 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 clp->cl_id_uniquifier);
3433 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003434 sizeof(setclientid.sc_netid),
3435 rpc_peeraddr2str(clp->cl_rpcclient,
3436 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003438 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 clp->cl_ipaddr, port >> 8, port & 255);
3440
3441 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3442 if (status != -NFS4ERR_CLID_INUSE)
3443 break;
3444 if (signalled())
3445 break;
3446 if (loop++ & 1)
3447 ssleep(clp->cl_lease_time + 1);
3448 else
3449 if (++clp->cl_id_uniquifier == 0)
3450 break;
3451 }
3452 return status;
3453}
3454
David Howellsadfa6f92006-08-22 20:06:08 -04003455static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456{
3457 struct nfs_fsinfo fsinfo;
3458 struct rpc_message msg = {
3459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3460 .rpc_argp = clp,
3461 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003462 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 };
3464 unsigned long now;
3465 int status;
3466
3467 now = jiffies;
3468 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3469 if (status == 0) {
3470 spin_lock(&clp->cl_lock);
3471 clp->cl_lease_time = fsinfo.lease_time * HZ;
3472 clp->cl_last_renewal = now;
3473 spin_unlock(&clp->cl_lock);
3474 }
3475 return status;
3476}
3477
David Howellsadfa6f92006-08-22 20:06:08 -04003478int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003479{
Benny Halevy77e03672008-06-24 20:25:57 +03003480 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003481 int err;
3482 do {
3483 err = _nfs4_proc_setclientid_confirm(clp, cred);
3484 switch (err) {
3485 case 0:
3486 return err;
3487 case -NFS4ERR_RESOURCE:
3488 /* The IBM lawyers misread another document! */
3489 case -NFS4ERR_DELAY:
3490 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3491 }
3492 } while (err == 0);
3493 return err;
3494}
3495
Trond Myklebustfe650402006-01-03 09:55:18 +01003496struct nfs4_delegreturndata {
3497 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003498 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003499 struct nfs_fh fh;
3500 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003501 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003502 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003503 int rpc_status;
3504};
3505
Trond Myklebustfe650402006-01-03 09:55:18 +01003506static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3507{
3508 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003509
3510 nfs4_sequence_done_free_slot(data->res.server, &data->res.seq_res,
3511 task->tk_status);
3512
Trond Myklebustfe650402006-01-03 09:55:18 +01003513 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003514 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01003515 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01003516}
3517
3518static void nfs4_delegreturn_release(void *calldata)
3519{
Trond Myklebustfe650402006-01-03 09:55:18 +01003520 kfree(calldata);
3521}
3522
Andy Adamson938e1012009-04-01 09:22:28 -04003523#if defined(CONFIG_NFS_V4_1)
3524static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3525{
3526 struct nfs4_delegreturndata *d_data;
3527
3528 d_data = (struct nfs4_delegreturndata *)data;
3529
3530 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3531 &d_data->args.seq_args,
3532 &d_data->res.seq_res, 1, task))
3533 return;
3534 rpc_call_start(task);
3535}
3536#endif /* CONFIG_NFS_V4_1 */
3537
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003538static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003539#if defined(CONFIG_NFS_V4_1)
3540 .rpc_call_prepare = nfs4_delegreturn_prepare,
3541#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003542 .rpc_call_done = nfs4_delegreturn_done,
3543 .rpc_release = nfs4_delegreturn_release,
3544};
3545
Trond Myklebuste6f81072008-01-24 18:14:34 -05003546static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003547{
3548 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003549 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003550 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003551 struct rpc_message msg = {
3552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3553 .rpc_cred = cred,
3554 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003555 struct rpc_task_setup task_setup_data = {
3556 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003557 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003558 .callback_ops = &nfs4_delegreturn_ops,
3559 .flags = RPC_TASK_ASYNC,
3560 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003561 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003562
Andy Adamson938e1012009-04-01 09:22:28 -04003563 data = kzalloc(sizeof(*data), GFP_KERNEL);
Trond Myklebustfe650402006-01-03 09:55:18 +01003564 if (data == NULL)
3565 return -ENOMEM;
3566 data->args.fhandle = &data->fh;
3567 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003568 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003569 nfs_copy_fh(&data->fh, NFS_FH(inode));
3570 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003571 data->res.fattr = &data->fattr;
3572 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003573 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003574 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003575 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003576 data->rpc_status = 0;
3577
Trond Myklebustc970aa82007-07-14 15:39:59 -04003578 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003579 msg.rpc_argp = &data->args,
3580 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003581 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003582 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003583 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003584 if (!issync)
3585 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003586 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003587 if (status != 0)
3588 goto out;
3589 status = data->rpc_status;
3590 if (status != 0)
3591 goto out;
3592 nfs_refresh_inode(inode, &data->fattr);
3593out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003594 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003595 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596}
3597
Trond Myklebuste6f81072008-01-24 18:14:34 -05003598int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599{
3600 struct nfs_server *server = NFS_SERVER(inode);
3601 struct nfs4_exception exception = { };
3602 int err;
3603 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003604 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 switch (err) {
3606 case -NFS4ERR_STALE_STATEID:
3607 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 case 0:
3609 return 0;
3610 }
3611 err = nfs4_handle_exception(server, err, &exception);
3612 } while (exception.retry);
3613 return err;
3614}
3615
3616#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3617#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3618
3619/*
3620 * sleep, with exponential backoff, and retry the LOCK operation.
3621 */
3622static unsigned long
3623nfs4_set_lock_task_retry(unsigned long timeout)
3624{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003625 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 timeout <<= 1;
3627 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3628 return NFS4_LOCK_MAXTIMEOUT;
3629 return timeout;
3630}
3631
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3633{
3634 struct inode *inode = state->inode;
3635 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003636 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003637 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003639 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003641 struct nfs_lockt_res res = {
3642 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 };
3644 struct rpc_message msg = {
3645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3646 .rpc_argp = &arg,
3647 .rpc_resp = &res,
3648 .rpc_cred = state->owner->so_cred,
3649 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 struct nfs4_lock_state *lsp;
3651 int status;
3652
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003653 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003654 status = nfs4_set_lock_state(state, request);
3655 if (status != 0)
3656 goto out;
3657 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003658 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003659 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003660 switch (status) {
3661 case 0:
3662 request->fl_type = F_UNLCK;
3663 break;
3664 case -NFS4ERR_DENIED:
3665 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003667 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003668out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 return status;
3670}
3671
3672static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3673{
3674 struct nfs4_exception exception = { };
3675 int err;
3676
3677 do {
3678 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3679 _nfs4_proc_getlk(state, cmd, request),
3680 &exception);
3681 } while (exception.retry);
3682 return err;
3683}
3684
3685static int do_vfs_lock(struct file *file, struct file_lock *fl)
3686{
3687 int res = 0;
3688 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3689 case FL_POSIX:
3690 res = posix_lock_file_wait(file, fl);
3691 break;
3692 case FL_FLOCK:
3693 res = flock_lock_file_wait(file, fl);
3694 break;
3695 default:
3696 BUG();
3697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 return res;
3699}
3700
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003701struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003702 struct nfs_locku_args arg;
3703 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003704 struct nfs4_lock_state *lsp;
3705 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003706 struct file_lock fl;
3707 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003708 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003709};
3710
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003711static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3712 struct nfs_open_context *ctx,
3713 struct nfs4_lock_state *lsp,
3714 struct nfs_seqid *seqid)
3715{
3716 struct nfs4_unlockdata *p;
3717 struct inode *inode = lsp->ls_state->inode;
3718
Andy Adamsona8936932009-04-01 09:22:23 -04003719 p = kzalloc(sizeof(*p), GFP_KERNEL);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003720 if (p == NULL)
3721 return NULL;
3722 p->arg.fh = NFS_FH(inode);
3723 p->arg.fl = &p->fl;
3724 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003725 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003726 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003727 p->arg.stateid = &lsp->ls_stateid;
3728 p->lsp = lsp;
3729 atomic_inc(&lsp->ls_count);
3730 /* Ensure we don't close file until we're done freeing locks! */
3731 p->ctx = get_nfs_open_context(ctx);
3732 memcpy(&p->fl, fl, sizeof(p->fl));
3733 p->server = NFS_SERVER(inode);
3734 return p;
3735}
3736
Trond Myklebust06f814a2006-01-03 09:55:07 +01003737static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003738{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003739 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003740 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003741 nfs4_put_lock_state(calldata->lsp);
3742 put_nfs_open_context(calldata->ctx);
3743 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003744}
3745
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003746static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003747{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003748 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003749
Andy Adamsona8936932009-04-01 09:22:23 -04003750 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3751 task->tk_status);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003752 if (RPC_ASSASSINATED(task))
3753 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003754 switch (task->tk_status) {
3755 case 0:
3756 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003757 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003758 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003759 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003760 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003761 case -NFS4ERR_BAD_STATEID:
3762 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003763 case -NFS4ERR_STALE_STATEID:
3764 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003765 break;
3766 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003767 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Andy Adamsoneedc0202009-04-01 09:22:41 -04003768 nfs4_restart_rpc(task,
Andy Adamsone608e792009-12-04 15:55:29 -05003769 calldata->server->nfs_client,
3770 &calldata->res.seq_res);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003771 }
Andy Adamsona8936932009-04-01 09:22:23 -04003772 nfs4_sequence_free_slot(calldata->server->nfs_client,
3773 &calldata->res.seq_res);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003774}
3775
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003776static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003777{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003778 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003780 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003781 return;
3782 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003783 /* Note: exit _without_ running nfs4_locku_done */
3784 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003785 return;
3786 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003787 calldata->timestamp = jiffies;
Andy Adamsona8936932009-04-01 09:22:23 -04003788 if (nfs4_setup_sequence(calldata->server->nfs_client,
3789 &calldata->arg.seq_args,
3790 &calldata->res.seq_res, 1, task))
3791 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003792 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793}
3794
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003795static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003796 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003797 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003798 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003799};
3800
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003801static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3802 struct nfs_open_context *ctx,
3803 struct nfs4_lock_state *lsp,
3804 struct nfs_seqid *seqid)
3805{
3806 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003807 struct rpc_message msg = {
3808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3809 .rpc_cred = ctx->cred,
3810 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003811 struct rpc_task_setup task_setup_data = {
3812 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003813 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003814 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003815 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003816 .flags = RPC_TASK_ASYNC,
3817 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003818
Frank Filz137d6ac2007-07-09 15:32:29 -07003819 /* Ensure this is an unlock - when canceling a lock, the
3820 * canceled lock is passed in, and it won't be an unlock.
3821 */
3822 fl->fl_type = F_UNLCK;
3823
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003824 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3825 if (data == NULL) {
3826 nfs_free_seqid(seqid);
3827 return ERR_PTR(-ENOMEM);
3828 }
3829
Trond Myklebust5138fde2007-07-14 15:40:01 -04003830 msg.rpc_argp = &data->arg,
3831 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003832 task_setup_data.callback_data = data;
3833 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003834}
3835
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3837{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003838 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003839 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003840 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003841 struct rpc_task *task;
3842 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003843 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
Trond Myklebust9b073572006-06-29 16:38:34 -04003845 status = nfs4_set_lock_state(state, request);
3846 /* Unlock _before_ we do the RPC call */
3847 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003848 down_read(&nfsi->rwsem);
3849 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3850 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003851 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003852 }
3853 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003854 if (status != 0)
3855 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003856 /* Is this a delegated lock? */
3857 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003858 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003859 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003860 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003861 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003862 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003863 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003864 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003865 status = PTR_ERR(task);
3866 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003867 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003868 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003869 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003870out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003871 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003872 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873}
3874
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003875struct nfs4_lockdata {
3876 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003877 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003878 struct nfs4_lock_state *lsp;
3879 struct nfs_open_context *ctx;
3880 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003881 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003882 int rpc_status;
3883 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04003884 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003885};
3886
3887static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3888 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3889{
3890 struct nfs4_lockdata *p;
3891 struct inode *inode = lsp->ls_state->inode;
3892 struct nfs_server *server = NFS_SERVER(inode);
3893
3894 p = kzalloc(sizeof(*p), GFP_KERNEL);
3895 if (p == NULL)
3896 return NULL;
3897
3898 p->arg.fh = NFS_FH(inode);
3899 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003900 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3901 if (p->arg.open_seqid == NULL)
3902 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003903 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3904 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003905 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003906 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003907 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003908 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003909 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003910 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003911 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04003912 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003913 atomic_inc(&lsp->ls_count);
3914 p->ctx = get_nfs_open_context(ctx);
3915 memcpy(&p->fl, fl, sizeof(p->fl));
3916 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003917out_free_seqid:
3918 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003919out_free:
3920 kfree(p);
3921 return NULL;
3922}
3923
3924static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3925{
3926 struct nfs4_lockdata *data = calldata;
3927 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928
Harvey Harrison3110ff82008-05-02 13:42:44 -07003929 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003930 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3931 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003932 /* Do we need to do an open_to_lock_owner? */
3933 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05003934 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3935 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003936 data->arg.open_stateid = &state->stateid;
3937 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003938 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003939 } else
3940 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003941 data->timestamp = jiffies;
Andy Adamson66179ef2009-04-01 09:22:22 -04003942 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
3943 &data->res.seq_res, 1, task))
3944 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003945 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003946 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003947}
3948
3949static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3950{
3951 struct nfs4_lockdata *data = calldata;
3952
Harvey Harrison3110ff82008-05-02 13:42:44 -07003953 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003954
Andy Adamson66179ef2009-04-01 09:22:22 -04003955 nfs4_sequence_done_free_slot(data->server, &data->res.seq_res,
3956 task->tk_status);
3957
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003958 data->rpc_status = task->tk_status;
3959 if (RPC_ASSASSINATED(task))
3960 goto out;
3961 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003962 if (data->rpc_status == 0)
3963 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3964 else
3965 goto out;
3966 }
3967 if (data->rpc_status == 0) {
3968 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3969 sizeof(data->lsp->ls_stateid.data));
3970 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003971 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003972 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003973out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07003974 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003975}
3976
3977static void nfs4_lock_release(void *calldata)
3978{
3979 struct nfs4_lockdata *data = calldata;
3980
Harvey Harrison3110ff82008-05-02 13:42:44 -07003981 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003982 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003983 if (data->cancelled != 0) {
3984 struct rpc_task *task;
3985 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3986 data->arg.lock_seqid);
3987 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003988 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003989 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003990 } else
3991 nfs_free_seqid(data->arg.lock_seqid);
3992 nfs4_put_lock_state(data->lsp);
3993 put_nfs_open_context(data->ctx);
3994 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003995 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003996}
3997
3998static const struct rpc_call_ops nfs4_lock_ops = {
3999 .rpc_call_prepare = nfs4_lock_prepare,
4000 .rpc_call_done = nfs4_lock_done,
4001 .rpc_release = nfs4_lock_release,
4002};
4003
4004static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
4005{
4006 struct nfs4_lockdata *data;
4007 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004008 struct rpc_message msg = {
4009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4010 .rpc_cred = state->owner->so_cred,
4011 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004012 struct rpc_task_setup task_setup_data = {
4013 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004014 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004015 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004016 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004017 .flags = RPC_TASK_ASYNC,
4018 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004019 int ret;
4020
Harvey Harrison3110ff82008-05-02 13:42:44 -07004021 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004022 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004023 fl->fl_u.nfs4_fl.owner);
4024 if (data == NULL)
4025 return -ENOMEM;
4026 if (IS_SETLKW(cmd))
4027 data->arg.block = 1;
4028 if (reclaim != 0)
4029 data->arg.reclaim = 1;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004030 msg.rpc_argp = &data->arg,
4031 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004032 task_setup_data.callback_data = data;
4033 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004034 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004035 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004036 ret = nfs4_wait_for_completion_rpc_task(task);
4037 if (ret == 0) {
4038 ret = data->rpc_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004039 } else
4040 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004041 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004042 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004043 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044}
4045
4046static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4047{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004048 struct nfs_server *server = NFS_SERVER(state->inode);
4049 struct nfs4_exception exception = { };
4050 int err;
4051
4052 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004053 /* Cache the lock if possible... */
4054 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4055 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004056 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
4057 if (err != -NFS4ERR_DELAY)
4058 break;
4059 nfs4_handle_exception(server, err, &exception);
4060 } while (exception.retry);
4061 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062}
4063
4064static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4065{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004066 struct nfs_server *server = NFS_SERVER(state->inode);
4067 struct nfs4_exception exception = { };
4068 int err;
4069
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004070 err = nfs4_set_lock_state(state, request);
4071 if (err != 0)
4072 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004073 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004074 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4075 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004076 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004077 switch (err) {
4078 default:
4079 goto out;
4080 case -NFS4ERR_GRACE:
4081 case -NFS4ERR_DELAY:
4082 nfs4_handle_exception(server, err, &exception);
4083 err = 0;
4084 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004085 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004086out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004087 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088}
4089
4090static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4091{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004092 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004093 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 int status;
4095
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004096 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004097 status = nfs4_set_lock_state(state, request);
4098 if (status != 0)
4099 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004100 request->fl_flags |= FL_ACCESS;
4101 status = do_vfs_lock(request->fl_file, request);
4102 if (status < 0)
4103 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004104 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004105 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004106 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004107 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004108 request->fl_flags = fl_flags & ~FL_SLEEP;
4109 status = do_vfs_lock(request->fl_file, request);
4110 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004111 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004112 status = _nfs4_do_setlk(state, cmd, request, 0);
4113 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004114 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004115 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004116 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004117 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004118 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004119out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004120 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004121out:
4122 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 return status;
4124}
4125
4126static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4127{
4128 struct nfs4_exception exception = { };
4129 int err;
4130
4131 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004132 err = _nfs4_proc_setlk(state, cmd, request);
4133 if (err == -NFS4ERR_DENIED)
4134 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004136 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 } while (exception.retry);
4138 return err;
4139}
4140
4141static int
4142nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4143{
4144 struct nfs_open_context *ctx;
4145 struct nfs4_state *state;
4146 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4147 int status;
4148
4149 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004150 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 state = ctx->state;
4152
4153 if (request->fl_start < 0 || request->fl_end < 0)
4154 return -EINVAL;
4155
Trond Myklebustd9531262009-07-21 19:22:38 -04004156 if (IS_GETLK(cmd)) {
4157 if (state != NULL)
4158 return nfs4_proc_getlk(state, F_GETLK, request);
4159 return 0;
4160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
4162 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4163 return -EINVAL;
4164
Trond Myklebustd9531262009-07-21 19:22:38 -04004165 if (request->fl_type == F_UNLCK) {
4166 if (state != NULL)
4167 return nfs4_proc_unlck(state, cmd, request);
4168 return 0;
4169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170
Trond Myklebustd9531262009-07-21 19:22:38 -04004171 if (state == NULL)
4172 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 do {
4174 status = nfs4_proc_setlk(state, cmd, request);
4175 if ((status != -EAGAIN) || IS_SETLK(cmd))
4176 break;
4177 timeout = nfs4_set_lock_task_retry(timeout);
4178 status = -ERESTARTSYS;
4179 if (signalled())
4180 break;
4181 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 return status;
4183}
4184
Trond Myklebust888e6942005-11-04 15:38:11 -05004185int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4186{
4187 struct nfs_server *server = NFS_SERVER(state->inode);
4188 struct nfs4_exception exception = { };
4189 int err;
4190
4191 err = nfs4_set_lock_state(state, fl);
4192 if (err != 0)
4193 goto out;
4194 do {
4195 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
Trond Myklebustd5122202009-06-17 13:22:58 -07004196 switch (err) {
4197 default:
4198 printk(KERN_ERR "%s: unhandled error %d.\n",
4199 __func__, err);
4200 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004201 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004202 goto out;
4203 case -NFS4ERR_EXPIRED:
4204 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004205 case -NFS4ERR_STALE_STATEID:
Trond Myklebustd5122202009-06-17 13:22:58 -07004206 nfs4_schedule_state_recovery(server->nfs_client);
4207 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004208 case -ERESTARTSYS:
4209 /*
4210 * The show must go on: exit, but mark the
4211 * stateid as needing recovery.
4212 */
4213 case -NFS4ERR_ADMIN_REVOKED:
4214 case -NFS4ERR_BAD_STATEID:
4215 case -NFS4ERR_OPENMODE:
4216 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4217 err = 0;
4218 goto out;
4219 case -ENOMEM:
4220 case -NFS4ERR_DENIED:
4221 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4222 err = 0;
4223 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004224 case -NFS4ERR_DELAY:
4225 break;
4226 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004227 err = nfs4_handle_exception(server, err, &exception);
4228 } while (exception.retry);
4229out:
4230 return err;
4231}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004232
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004233#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4234
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004235int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4236 size_t buflen, int flags)
4237{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004238 struct inode *inode = dentry->d_inode;
4239
4240 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4241 return -EOPNOTSUPP;
4242
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004243 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004244}
4245
4246/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4247 * and that's what we'll do for e.g. user attributes that haven't been set.
4248 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4249 * attributes in kernel-managed attribute namespaces. */
4250ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4251 size_t buflen)
4252{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004253 struct inode *inode = dentry->d_inode;
4254
4255 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4256 return -EOPNOTSUPP;
4257
4258 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004259}
4260
4261ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4262{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004263 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004264
J. Bruce Fields096455a2006-03-20 23:23:42 -05004265 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4266 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004267 if (buf && buflen < len)
4268 return -ERANGE;
4269 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004270 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4271 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004272}
4273
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004274static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4275{
4276 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4277 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4278 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4279 return;
4280
4281 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4282 NFS_ATTR_FATTR_NLINK;
4283 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4284 fattr->nlink = 2;
4285}
4286
Trond Myklebust56659e92007-07-17 21:52:39 -04004287int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004288 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004289{
4290 struct nfs_server *server = NFS_SERVER(dir);
4291 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004292 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4293 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004294 };
4295 struct nfs4_fs_locations_arg args = {
4296 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004297 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004298 .page = page,
4299 .bitmask = bitmask,
4300 };
Benny Halevy22958462009-04-01 09:22:02 -04004301 struct nfs4_fs_locations_res res = {
4302 .fs_locations = fs_locations,
4303 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004304 struct rpc_message msg = {
4305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4306 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004307 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004308 };
4309 int status;
4310
Harvey Harrison3110ff82008-05-02 13:42:44 -07004311 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004312 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004313 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004314 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004315 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004316 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004317 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004318 return status;
4319}
4320
Andy Adamson557134a2009-04-01 09:21:53 -04004321#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004322/*
4323 * nfs4_proc_exchange_id()
4324 *
4325 * Since the clientid has expired, all compounds using sessions
4326 * associated with the stale clientid will be returning
4327 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4328 * be in some phase of session reset.
4329 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004330int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004331{
4332 nfs4_verifier verifier;
4333 struct nfs41_exchange_id_args args = {
4334 .client = clp,
4335 .flags = clp->cl_exchange_flags,
4336 };
4337 struct nfs41_exchange_id_res res = {
4338 .client = clp,
4339 };
4340 int status;
4341 struct rpc_message msg = {
4342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4343 .rpc_argp = &args,
4344 .rpc_resp = &res,
4345 .rpc_cred = cred,
4346 };
4347 __be32 *p;
4348
4349 dprintk("--> %s\n", __func__);
4350 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004351
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004352 /* Remove server-only flags */
4353 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4354
Benny Halevy99fe60d2009-04-01 09:22:29 -04004355 p = (u32 *)verifier.data;
4356 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4357 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4358 args.verifier = &verifier;
4359
4360 while (1) {
4361 args.id_len = scnprintf(args.id, sizeof(args.id),
4362 "%s/%s %u",
4363 clp->cl_ipaddr,
4364 rpc_peeraddr2str(clp->cl_rpcclient,
4365 RPC_DISPLAY_ADDR),
4366 clp->cl_id_uniquifier);
4367
4368 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4369
4370 if (status != NFS4ERR_CLID_INUSE)
4371 break;
4372
4373 if (signalled())
4374 break;
4375
4376 if (++clp->cl_id_uniquifier == 0)
4377 break;
4378 }
4379
4380 dprintk("<-- %s status= %d\n", __func__, status);
4381 return status;
4382}
4383
Andy Adamson2050f0c2009-04-01 09:22:30 -04004384struct nfs4_get_lease_time_data {
4385 struct nfs4_get_lease_time_args *args;
4386 struct nfs4_get_lease_time_res *res;
4387 struct nfs_client *clp;
4388};
4389
4390static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4391 void *calldata)
4392{
4393 int ret;
4394 struct nfs4_get_lease_time_data *data =
4395 (struct nfs4_get_lease_time_data *)calldata;
4396
4397 dprintk("--> %s\n", __func__);
4398 /* just setup sequence, do not trigger session recovery
4399 since we're invoked within one */
4400 ret = nfs41_setup_sequence(data->clp->cl_session,
4401 &data->args->la_seq_args,
4402 &data->res->lr_seq_res, 0, task);
4403
4404 BUG_ON(ret == -EAGAIN);
4405 rpc_call_start(task);
4406 dprintk("<-- %s\n", __func__);
4407}
4408
4409/*
4410 * Called from nfs4_state_manager thread for session setup, so don't recover
4411 * from sequence operation or clientid errors.
4412 */
4413static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4414{
4415 struct nfs4_get_lease_time_data *data =
4416 (struct nfs4_get_lease_time_data *)calldata;
4417
4418 dprintk("--> %s\n", __func__);
4419 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4420 switch (task->tk_status) {
4421 case -NFS4ERR_DELAY:
4422 case -NFS4ERR_GRACE:
4423 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4424 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4425 task->tk_status = 0;
Andy Adamson1d9ddde92009-12-04 15:55:27 -05004426 rpc_restart_call(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004427 return;
4428 }
4429 nfs41_sequence_free_slot(data->clp, &data->res->lr_seq_res);
4430 dprintk("<-- %s\n", __func__);
4431}
4432
4433struct rpc_call_ops nfs4_get_lease_time_ops = {
4434 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4435 .rpc_call_done = nfs4_get_lease_time_done,
4436};
4437
4438int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4439{
4440 struct rpc_task *task;
4441 struct nfs4_get_lease_time_args args;
4442 struct nfs4_get_lease_time_res res = {
4443 .lr_fsinfo = fsinfo,
4444 };
4445 struct nfs4_get_lease_time_data data = {
4446 .args = &args,
4447 .res = &res,
4448 .clp = clp,
4449 };
4450 struct rpc_message msg = {
4451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4452 .rpc_argp = &args,
4453 .rpc_resp = &res,
4454 };
4455 struct rpc_task_setup task_setup = {
4456 .rpc_client = clp->cl_rpcclient,
4457 .rpc_message = &msg,
4458 .callback_ops = &nfs4_get_lease_time_ops,
4459 .callback_data = &data
4460 };
4461 int status;
4462
4463 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4464 dprintk("--> %s\n", __func__);
4465 task = rpc_run_task(&task_setup);
4466
4467 if (IS_ERR(task))
4468 status = PTR_ERR(task);
4469 else {
4470 status = task->tk_status;
4471 rpc_put_task(task);
4472 }
4473 dprintk("<-- %s return %d\n", __func__, status);
4474
4475 return status;
4476}
4477
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004478/*
4479 * Reset a slot table
4480 */
4481static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots,
4482 int old_max_slots, int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004483{
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004484 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004485 int ret = 0;
4486
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004487 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004488
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004489 /*
4490 * Until we have dynamic slot table adjustment, insist
4491 * upon the same slot table size
4492 */
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004493 if (max_slots != old_max_slots) {
4494 dprintk("%s reset slot table does't match old\n",
4495 __func__);
4496 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4497 goto out;
4498 }
4499 spin_lock(&tbl->slot_tbl_lock);
4500 for (i = 0; i < max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004501 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004502 tbl->highest_used_slotid = -1;
4503 spin_unlock(&tbl->slot_tbl_lock);
4504 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4505 tbl, tbl->slots, tbl->max_slots);
4506out:
4507 dprintk("<-- %s: return %d\n", __func__, ret);
4508 return ret;
4509}
4510
Andy Adamsonfc931582009-04-01 09:22:31 -04004511/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004512 * Reset the forechannel and backchannel slot tables
4513 */
4514static int nfs4_reset_slot_tables(struct nfs4_session *session)
4515{
4516 int status;
4517
4518 status = nfs4_reset_slot_table(&session->fc_slot_table,
4519 session->fc_attrs.max_reqs,
4520 session->fc_slot_table.max_slots,
4521 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004522 if (status)
4523 return status;
Andy Adamsonea028ac2009-12-04 15:55:38 -05004524 init_completion(&session->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004525
4526 status = nfs4_reset_slot_table(&session->bc_slot_table,
4527 session->bc_attrs.max_reqs,
4528 session->bc_slot_table.max_slots,
4529 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004530 return status;
4531}
4532
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004533/* Destroy the slot table */
4534static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4535{
4536 if (session->fc_slot_table.slots != NULL) {
4537 kfree(session->fc_slot_table.slots);
4538 session->fc_slot_table.slots = NULL;
4539 }
4540 if (session->bc_slot_table.slots != NULL) {
4541 kfree(session->bc_slot_table.slots);
4542 session->bc_slot_table.slots = NULL;
4543 }
4544 return;
4545}
4546
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004547/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004548 * Initialize slot table
4549 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004550static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4551 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004552{
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004553 int i;
Andy Adamsonfc931582009-04-01 09:22:31 -04004554 struct nfs4_slot *slot;
4555 int ret = -ENOMEM;
4556
4557 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4558
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004559 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004560
4561 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4562 if (!slot)
4563 goto out;
4564 for (i = 0; i < max_slots; ++i)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004565 slot[i].seq_nr = ivalue;
Andy Adamsonfc931582009-04-01 09:22:31 -04004566 ret = 0;
4567
4568 spin_lock(&tbl->slot_tbl_lock);
4569 if (tbl->slots != NULL) {
4570 spin_unlock(&tbl->slot_tbl_lock);
4571 dprintk("%s: slot table already initialized. tbl=%p slots=%p\n",
4572 __func__, tbl, tbl->slots);
4573 WARN_ON(1);
4574 goto out_free;
4575 }
4576 tbl->max_slots = max_slots;
4577 tbl->slots = slot;
4578 tbl->highest_used_slotid = -1; /* no slot is currently used */
4579 spin_unlock(&tbl->slot_tbl_lock);
4580 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4581 tbl, tbl->slots, tbl->max_slots);
4582out:
4583 dprintk("<-- %s: return %d\n", __func__, ret);
4584 return ret;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004585
Andy Adamsonfc931582009-04-01 09:22:31 -04004586out_free:
4587 kfree(slot);
4588 goto out;
4589}
4590
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004591/*
4592 * Initialize the forechannel and backchannel tables
4593 */
4594static int nfs4_init_slot_tables(struct nfs4_session *session)
4595{
4596 int status;
4597
4598 status = nfs4_init_slot_table(&session->fc_slot_table,
4599 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004600 if (status)
4601 return status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004602
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004603 status = nfs4_init_slot_table(&session->bc_slot_table,
4604 session->bc_attrs.max_reqs, 0);
4605 if (status)
4606 nfs4_destroy_slot_tables(session);
4607
4608 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004609}
4610
4611struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4612{
4613 struct nfs4_session *session;
4614 struct nfs4_slot_table *tbl;
4615
4616 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4617 if (!session)
4618 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004619
Andy Adamson76db6d92009-04-01 09:22:38 -04004620 /*
4621 * The create session reply races with the server back
4622 * channel probe. Mark the client NFS_CS_SESSION_INITING
4623 * so that the client back channel can find the
4624 * nfs_client struct
4625 */
4626 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamsonea028ac2009-12-04 15:55:38 -05004627 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004628
Andy Adamson557134a2009-04-01 09:21:53 -04004629 tbl = &session->fc_slot_table;
4630 spin_lock_init(&tbl->slot_tbl_lock);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004631 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4632
4633 tbl = &session->bc_slot_table;
4634 spin_lock_init(&tbl->slot_tbl_lock);
4635 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4636
Andy Adamson557134a2009-04-01 09:21:53 -04004637 session->clp = clp;
4638 return session;
4639}
4640
4641void nfs4_destroy_session(struct nfs4_session *session)
4642{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004643 nfs4_proc_destroy_session(session);
4644 dprintk("%s Destroy backchannel for xprt %p\n",
4645 __func__, session->clp->cl_rpcclient->cl_xprt);
4646 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4647 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004648 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004649 kfree(session);
4650}
4651
Andy Adamsonfc931582009-04-01 09:22:31 -04004652/*
4653 * Initialize the values to be used by the client in CREATE_SESSION
4654 * If nfs4_init_session set the fore channel request and response sizes,
4655 * use them.
4656 *
4657 * Set the back channel max_resp_sz_cached to zero to force the client to
4658 * always set csa_cachethis to FALSE because the current implementation
4659 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4660 */
4661static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4662{
4663 struct nfs4_session *session = args->client->cl_session;
4664 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4665 mxresp_sz = session->fc_attrs.max_resp_sz;
4666
4667 if (mxrqst_sz == 0)
4668 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4669 if (mxresp_sz == 0)
4670 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4671 /* Fore channel attributes */
4672 args->fc_attrs.headerpadsz = 0;
4673 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4674 args->fc_attrs.max_resp_sz = mxresp_sz;
4675 args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4676 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4677 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4678
4679 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4680 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4681 __func__,
4682 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4683 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4684 args->fc_attrs.max_reqs);
4685
4686 /* Back channel attributes */
4687 args->bc_attrs.headerpadsz = 0;
4688 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4689 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4690 args->bc_attrs.max_resp_sz_cached = 0;
4691 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4692 args->bc_attrs.max_reqs = 1;
4693
4694 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4695 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4696 __func__,
4697 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4698 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4699 args->bc_attrs.max_reqs);
4700}
4701
Andy Adamson8d353012009-04-01 09:22:32 -04004702static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4703{
4704 if (rcvd <= sent)
4705 return 0;
4706 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4707 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4708 return -EINVAL;
4709}
4710
4711#define _verify_fore_channel_attr(_name_) \
4712 _verify_channel_attr("fore", #_name_, \
4713 args->fc_attrs._name_, \
4714 session->fc_attrs._name_)
4715
4716#define _verify_back_channel_attr(_name_) \
4717 _verify_channel_attr("back", #_name_, \
4718 args->bc_attrs._name_, \
4719 session->bc_attrs._name_)
4720
4721/*
4722 * The server is not allowed to increase the fore channel header pad size,
4723 * maximum response size, or maximum number of operations.
4724 *
4725 * The back channel attributes are only negotiatied down: We send what the
4726 * (back channel) server insists upon.
4727 */
4728static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4729 struct nfs4_session *session)
4730{
4731 int ret = 0;
4732
4733 ret |= _verify_fore_channel_attr(headerpadsz);
4734 ret |= _verify_fore_channel_attr(max_resp_sz);
4735 ret |= _verify_fore_channel_attr(max_ops);
4736
4737 ret |= _verify_back_channel_attr(headerpadsz);
4738 ret |= _verify_back_channel_attr(max_rqst_sz);
4739 ret |= _verify_back_channel_attr(max_resp_sz);
4740 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4741 ret |= _verify_back_channel_attr(max_ops);
4742 ret |= _verify_back_channel_attr(max_reqs);
4743
4744 return ret;
4745}
4746
Andy Adamsonfc931582009-04-01 09:22:31 -04004747static int _nfs4_proc_create_session(struct nfs_client *clp)
4748{
4749 struct nfs4_session *session = clp->cl_session;
4750 struct nfs41_create_session_args args = {
4751 .client = clp,
4752 .cb_program = NFS4_CALLBACK,
4753 };
4754 struct nfs41_create_session_res res = {
4755 .client = clp,
4756 };
4757 struct rpc_message msg = {
4758 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4759 .rpc_argp = &args,
4760 .rpc_resp = &res,
4761 };
4762 int status;
4763
4764 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004765 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004766
4767 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4768
Andy Adamson8d353012009-04-01 09:22:32 -04004769 if (!status)
4770 /* Verify the session's negotiated channel_attrs values */
4771 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004772 if (!status) {
4773 /* Increment the clientid slot sequence id */
4774 clp->cl_seqid++;
4775 }
4776
4777 return status;
4778}
4779
4780/*
4781 * Issues a CREATE_SESSION operation to the server.
4782 * It is the responsibility of the caller to verify the session is
4783 * expired before calling this routine.
4784 */
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004785int nfs4_proc_create_session(struct nfs_client *clp, int reset)
Andy Adamsonfc931582009-04-01 09:22:31 -04004786{
4787 int status;
4788 unsigned *ptr;
4789 struct nfs_fsinfo fsinfo;
4790 struct nfs4_session *session = clp->cl_session;
4791
4792 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4793
4794 status = _nfs4_proc_create_session(clp);
4795 if (status)
4796 goto out;
4797
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004798 /* Init or reset the fore channel */
4799 if (reset)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004800 status = nfs4_reset_slot_tables(session);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004801 else
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004802 status = nfs4_init_slot_tables(session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004803 dprintk("fore channel slot table initialization returned %d\n", status);
4804 if (status)
4805 goto out;
4806
4807 ptr = (unsigned *)&session->sess_id.data[0];
4808 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4809 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
4810
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004811 if (reset)
4812 /* Lease time is aleady set */
4813 goto out;
4814
Andy Adamsonfc931582009-04-01 09:22:31 -04004815 /* Get the lease time */
4816 status = nfs4_proc_get_lease_time(clp, &fsinfo);
4817 if (status == 0) {
4818 /* Update lease time and schedule renewal */
4819 spin_lock(&clp->cl_lock);
4820 clp->cl_lease_time = fsinfo.lease_time * HZ;
4821 clp->cl_last_renewal = jiffies;
4822 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
4823 spin_unlock(&clp->cl_lock);
4824
4825 nfs4_schedule_state_renewal(clp);
4826 }
4827out:
4828 dprintk("<-- %s\n", __func__);
4829 return status;
4830}
4831
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004832/*
4833 * Issue the over-the-wire RPC DESTROY_SESSION.
4834 * The caller must serialize access to this routine.
4835 */
4836int nfs4_proc_destroy_session(struct nfs4_session *session)
4837{
4838 int status = 0;
4839 struct rpc_message msg;
4840
4841 dprintk("--> nfs4_proc_destroy_session\n");
4842
4843 /* session is still being setup */
4844 if (session->clp->cl_cons_state != NFS_CS_READY)
4845 return status;
4846
4847 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4848 msg.rpc_argp = session;
4849 msg.rpc_resp = NULL;
4850 msg.rpc_cred = NULL;
4851 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4852
4853 if (status)
4854 printk(KERN_WARNING
4855 "Got error %d from the server on DESTROY_SESSION. "
4856 "Session has been destroyed regardless...\n", status);
4857
4858 dprintk("<-- nfs4_proc_destroy_session\n");
4859 return status;
4860}
4861
Trond Myklebustfccba802009-07-21 16:48:07 -04004862int nfs4_init_session(struct nfs_server *server)
4863{
4864 struct nfs_client *clp = server->nfs_client;
4865 int ret;
4866
4867 if (!nfs4_has_session(clp))
4868 return 0;
4869
4870 clp->cl_session->fc_attrs.max_rqst_sz = server->wsize;
4871 clp->cl_session->fc_attrs.max_resp_sz = server->rsize;
4872 ret = nfs4_recover_expired_lease(server);
4873 if (!ret)
4874 ret = nfs4_check_client_ready(clp);
4875 return ret;
4876}
4877
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004878/*
4879 * Renew the cl_session lease.
4880 */
4881static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
4882{
4883 struct nfs4_sequence_args args;
4884 struct nfs4_sequence_res res;
4885
4886 struct rpc_message msg = {
4887 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4888 .rpc_argp = &args,
4889 .rpc_resp = &res,
4890 .rpc_cred = cred,
4891 };
4892
4893 args.sa_cache_this = 0;
4894
4895 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
4896 &res, 0);
4897}
4898
4899void nfs41_sequence_call_done(struct rpc_task *task, void *data)
4900{
4901 struct nfs_client *clp = (struct nfs_client *)data;
4902
4903 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
4904
4905 if (task->tk_status < 0) {
4906 dprintk("%s ERROR %d\n", __func__, task->tk_status);
4907
4908 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
4909 == -EAGAIN) {
Andy Adamsone608e792009-12-04 15:55:29 -05004910 nfs4_restart_rpc(task, clp, task->tk_msg.rpc_resp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004911 return;
4912 }
4913 }
4914 nfs41_sequence_free_slot(clp, task->tk_msg.rpc_resp);
4915 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
4916
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004917 kfree(task->tk_msg.rpc_argp);
4918 kfree(task->tk_msg.rpc_resp);
4919
4920 dprintk("<-- %s\n", __func__);
4921}
4922
4923static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
4924{
4925 struct nfs_client *clp;
4926 struct nfs4_sequence_args *args;
4927 struct nfs4_sequence_res *res;
4928
4929 clp = (struct nfs_client *)data;
4930 args = task->tk_msg.rpc_argp;
4931 res = task->tk_msg.rpc_resp;
4932
4933 if (nfs4_setup_sequence(clp, args, res, 0, task))
4934 return;
4935 rpc_call_start(task);
4936}
4937
4938static const struct rpc_call_ops nfs41_sequence_ops = {
4939 .rpc_call_done = nfs41_sequence_call_done,
4940 .rpc_call_prepare = nfs41_sequence_prepare,
4941};
4942
4943static int nfs41_proc_async_sequence(struct nfs_client *clp,
4944 struct rpc_cred *cred)
4945{
4946 struct nfs4_sequence_args *args;
4947 struct nfs4_sequence_res *res;
4948 struct rpc_message msg = {
4949 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4950 .rpc_cred = cred,
4951 };
4952
4953 args = kzalloc(sizeof(*args), GFP_KERNEL);
4954 if (!args)
4955 return -ENOMEM;
4956 res = kzalloc(sizeof(*res), GFP_KERNEL);
4957 if (!res) {
4958 kfree(args);
4959 return -ENOMEM;
4960 }
4961 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
4962 msg.rpc_argp = args;
4963 msg.rpc_resp = res;
4964
4965 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
4966 &nfs41_sequence_ops, (void *)clp);
4967}
4968
Andy Adamson557134a2009-04-01 09:21:53 -04004969#endif /* CONFIG_NFS_V4_1 */
4970
Andy Adamson591d71c2009-04-01 09:22:47 -04004971struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05004972 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004973 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 .recover_open = nfs4_open_reclaim,
4975 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04004976 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04004977 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978};
4979
Andy Adamson591d71c2009-04-01 09:22:47 -04004980#if defined(CONFIG_NFS_V4_1)
4981struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
4982 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
4983 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
4984 .recover_open = nfs4_open_reclaim,
4985 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05004986 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04004987 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04004988};
4989#endif /* CONFIG_NFS_V4_1 */
4990
4991struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05004992 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004993 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 .recover_open = nfs4_open_expired,
4995 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04004996 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04004997 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998};
4999
Andy Adamson591d71c2009-04-01 09:22:47 -04005000#if defined(CONFIG_NFS_V4_1)
5001struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5002 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5003 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5004 .recover_open = nfs4_open_expired,
5005 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005006 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005007 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005008};
5009#endif /* CONFIG_NFS_V4_1 */
5010
Benny Halevy29fba382009-04-01 09:22:44 -04005011struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5012 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005013 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005014 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005015};
5016
5017#if defined(CONFIG_NFS_V4_1)
5018struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5019 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005020 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005021 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005022};
5023#endif
5024
5025/*
5026 * Per minor version reboot and network partition recovery ops
5027 */
5028
Andy Adamson591d71c2009-04-01 09:22:47 -04005029struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5030 &nfs40_reboot_recovery_ops,
5031#if defined(CONFIG_NFS_V4_1)
5032 &nfs41_reboot_recovery_ops,
5033#endif
5034};
5035
5036struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5037 &nfs40_nograce_recovery_ops,
5038#if defined(CONFIG_NFS_V4_1)
5039 &nfs41_nograce_recovery_ops,
5040#endif
5041};
5042
Benny Halevy29fba382009-04-01 09:22:44 -04005043struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5044 &nfs40_state_renewal_ops,
5045#if defined(CONFIG_NFS_V4_1)
5046 &nfs41_state_renewal_ops,
5047#endif
5048};
5049
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005050static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005051 .permission = nfs_permission,
5052 .getattr = nfs_getattr,
5053 .setattr = nfs_setattr,
5054 .getxattr = nfs4_getxattr,
5055 .setxattr = nfs4_setxattr,
5056 .listxattr = nfs4_listxattr,
5057};
5058
David Howells509de812006-08-22 20:06:11 -04005059const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 .version = 4, /* protocol version */
5061 .dentry_ops = &nfs4_dentry_operations,
5062 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005063 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 .getroot = nfs4_proc_get_root,
5065 .getattr = nfs4_proc_getattr,
5066 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005067 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 .lookup = nfs4_proc_lookup,
5069 .access = nfs4_proc_access,
5070 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 .create = nfs4_proc_create,
5072 .remove = nfs4_proc_remove,
5073 .unlink_setup = nfs4_proc_unlink_setup,
5074 .unlink_done = nfs4_proc_unlink_done,
5075 .rename = nfs4_proc_rename,
5076 .link = nfs4_proc_link,
5077 .symlink = nfs4_proc_symlink,
5078 .mkdir = nfs4_proc_mkdir,
5079 .rmdir = nfs4_proc_remove,
5080 .readdir = nfs4_proc_readdir,
5081 .mknod = nfs4_proc_mknod,
5082 .statfs = nfs4_proc_statfs,
5083 .fsinfo = nfs4_proc_fsinfo,
5084 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005085 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 .decode_dirent = nfs4_decode_dirent,
5087 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005088 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005090 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005092 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005094 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005095 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096};
5097
5098/*
5099 * Local variables:
5100 * c-basic-offset: 8
5101 * End:
5102 */