blob: 40da0d5bc5fc1677911ca4061fca99f10cc9555f [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);
273 set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
274 exception->retry = 1;
275 /* FALLTHROUGH */
276#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 }
339 rpc_wake_up_next(&tbl->slot_tbl_waitq);
340 spin_unlock(&tbl->slot_tbl_lock);
341 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
342 free_slotid, tbl->highest_used_slotid);
343}
344
Andy Adamson13615872009-04-01 09:22:17 -0400345void nfs41_sequence_free_slot(const struct nfs_client *clp,
346 struct nfs4_sequence_res *res)
347{
348 struct nfs4_slot_table *tbl;
349
350 if (!nfs4_has_session(clp)) {
351 dprintk("%s: No session\n", __func__);
352 return;
353 }
354 tbl = &clp->cl_session->fc_slot_table;
355 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
356 dprintk("%s: No slot\n", __func__);
357 /* just wake up the next guy waiting since
358 * we may have not consumed a slot after all */
359 rpc_wake_up_next(&tbl->slot_tbl_waitq);
360 return;
361 }
362 nfs4_free_slot(tbl, res->sr_slotid);
363 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
364}
365
Andy Adamsonb0df8062009-04-01 09:22:18 -0400366static void nfs41_sequence_done(struct nfs_client *clp,
367 struct nfs4_sequence_res *res,
368 int rpc_status)
369{
370 unsigned long timestamp;
371 struct nfs4_slot_table *tbl;
372 struct nfs4_slot *slot;
373
374 /*
375 * sr_status remains 1 if an RPC level error occurred. The server
376 * may or may not have processed the sequence operation..
377 * Proceed as if the server received and processed the sequence
378 * operation.
379 */
380 if (res->sr_status == 1)
381 res->sr_status = NFS_OK;
382
383 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
384 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
385 goto out;
386
387 tbl = &clp->cl_session->fc_slot_table;
388 slot = tbl->slots + res->sr_slotid;
389
390 if (res->sr_status == 0) {
391 /* Update the slot's sequence and clientid lease timer */
392 ++slot->seq_nr;
393 timestamp = res->sr_renewal_time;
394 spin_lock(&clp->cl_lock);
395 if (time_before(clp->cl_last_renewal, timestamp))
396 clp->cl_last_renewal = timestamp;
397 spin_unlock(&clp->cl_lock);
398 return;
399 }
400out:
401 /* The session may be reset by one of the error handlers. */
402 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
403 nfs41_sequence_free_slot(clp, res);
404}
405
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400406/*
Benny Halevy510b8172009-04-01 09:22:14 -0400407 * nfs4_find_slot - efficiently look for a free slot
408 *
409 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
410 * If found, we mark the slot as used, update the highest_used_slotid,
411 * and respectively set up the sequence operation args.
412 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400413 *
414 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400415 */
416static u8
417nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
418{
419 int slotid;
420 u8 ret_id = NFS4_MAX_SLOT_TABLE;
421 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
422
Benny Halevy510b8172009-04-01 09:22:14 -0400423 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
424 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
425 tbl->max_slots);
426 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
427 if (slotid >= tbl->max_slots)
428 goto out;
429 __set_bit(slotid, tbl->used_slots);
430 if (slotid > tbl->highest_used_slotid)
431 tbl->highest_used_slotid = slotid;
432 ret_id = slotid;
433out:
434 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
435 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400436 return ret_id;
437}
438
Andy Adamsonb069d942009-04-01 09:22:43 -0400439static int nfs4_recover_session(struct nfs4_session *session)
440{
441 struct nfs_client *clp = session->clp;
Trond Myklebusta78cb572009-08-09 15:06:19 -0400442 unsigned int loop;
Andy Adamsonb069d942009-04-01 09:22:43 -0400443 int ret;
444
Trond Myklebusta78cb572009-08-09 15:06:19 -0400445 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Andy Adamsonb069d942009-04-01 09:22:43 -0400446 ret = nfs4_wait_clnt_recover(clp);
447 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -0400448 break;
Andy Adamsonb069d942009-04-01 09:22:43 -0400449 if (!test_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state))
450 break;
451 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -0400452 ret = -EIO;
Andy Adamsonb069d942009-04-01 09:22:43 -0400453 }
Trond Myklebusta78cb572009-08-09 15:06:19 -0400454 return ret;
Andy Adamsonb069d942009-04-01 09:22:43 -0400455}
456
Andy Adamsonce5039c2009-04-01 09:22:13 -0400457static int nfs41_setup_sequence(struct nfs4_session *session,
458 struct nfs4_sequence_args *args,
459 struct nfs4_sequence_res *res,
460 int cache_reply,
461 struct rpc_task *task)
462{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400463 struct nfs4_slot *slot;
464 struct nfs4_slot_table *tbl;
Andy Adamsonb069d942009-04-01 09:22:43 -0400465 int status = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400466 u8 slotid;
467
468 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400469 /* slot already allocated? */
470 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
471 return 0;
472
473 memset(res, 0, sizeof(*res));
474 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400475 tbl = &session->fc_slot_table;
476
477 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonb069d942009-04-01 09:22:43 -0400478 if (test_bit(NFS4CLNT_SESSION_SETUP, &session->clp->cl_state)) {
479 if (tbl->highest_used_slotid != -1) {
480 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
481 spin_unlock(&tbl->slot_tbl_lock);
482 dprintk("<-- %s: Session reset: draining\n", __func__);
483 return -EAGAIN;
484 }
485
486 /* The slot table is empty; start the reset thread */
487 dprintk("%s Session Reset\n", __func__);
488 spin_unlock(&tbl->slot_tbl_lock);
489 status = nfs4_recover_session(session);
490 if (status)
491 return status;
492 spin_lock(&tbl->slot_tbl_lock);
493 }
494
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400495 slotid = nfs4_find_slot(tbl, task);
496 if (slotid == NFS4_MAX_SLOT_TABLE) {
497 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
498 spin_unlock(&tbl->slot_tbl_lock);
499 dprintk("<-- %s: no free slots\n", __func__);
500 return -EAGAIN;
501 }
502 spin_unlock(&tbl->slot_tbl_lock);
503
504 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400505 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400506 args->sa_slotid = slotid;
507 args->sa_cache_this = cache_reply;
508
509 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
510
Benny Halevy99fe60d2009-04-01 09:22:29 -0400511 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400512 res->sr_slotid = slotid;
513 res->sr_renewal_time = jiffies;
514 /*
515 * sr_status is only set in decode_sequence, and so will remain
516 * set to 1 if an rpc level failure occurs.
517 */
518 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400519 return 0;
520}
521
522int nfs4_setup_sequence(struct nfs_client *clp,
523 struct nfs4_sequence_args *args,
524 struct nfs4_sequence_res *res,
525 int cache_reply,
526 struct rpc_task *task)
527{
528 int ret = 0;
529
530 dprintk("--> %s clp %p session %p sr_slotid %d\n",
531 __func__, clp, clp->cl_session, res->sr_slotid);
532
533 if (!nfs4_has_session(clp))
534 goto out;
535 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
536 task);
537 if (ret != -EAGAIN) {
538 /* terminate rpc task */
539 task->tk_status = ret;
540 task->tk_action = NULL;
541 }
542out:
543 dprintk("<-- %s status=%d\n", __func__, ret);
544 return ret;
545}
546
547struct nfs41_call_sync_data {
548 struct nfs_client *clp;
549 struct nfs4_sequence_args *seq_args;
550 struct nfs4_sequence_res *seq_res;
551 int cache_reply;
552};
553
554static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
555{
556 struct nfs41_call_sync_data *data = calldata;
557
558 dprintk("--> %s data->clp->cl_session %p\n", __func__,
559 data->clp->cl_session);
560 if (nfs4_setup_sequence(data->clp, data->seq_args,
561 data->seq_res, data->cache_reply, task))
562 return;
563 rpc_call_start(task);
564}
565
Andy Adamson69ab40c2009-04-01 09:22:19 -0400566static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
567{
568 struct nfs41_call_sync_data *data = calldata;
569
570 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
571 nfs41_sequence_free_slot(data->clp, data->seq_res);
572}
573
Andy Adamsonce5039c2009-04-01 09:22:13 -0400574struct rpc_call_ops nfs41_call_sync_ops = {
575 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400576 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400577};
578
579static int nfs4_call_sync_sequence(struct nfs_client *clp,
580 struct rpc_clnt *clnt,
581 struct rpc_message *msg,
582 struct nfs4_sequence_args *args,
583 struct nfs4_sequence_res *res,
584 int cache_reply)
585{
586 int ret;
587 struct rpc_task *task;
588 struct nfs41_call_sync_data data = {
589 .clp = clp,
590 .seq_args = args,
591 .seq_res = res,
592 .cache_reply = cache_reply,
593 };
594 struct rpc_task_setup task_setup = {
595 .rpc_client = clnt,
596 .rpc_message = msg,
597 .callback_ops = &nfs41_call_sync_ops,
598 .callback_data = &data
599 };
600
601 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
602 task = rpc_run_task(&task_setup);
603 if (IS_ERR(task))
604 ret = PTR_ERR(task);
605 else {
606 ret = task->tk_status;
607 rpc_put_task(task);
608 }
609 return ret;
610}
611
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400612int _nfs4_call_sync_session(struct nfs_server *server,
613 struct rpc_message *msg,
614 struct nfs4_sequence_args *args,
615 struct nfs4_sequence_res *res,
616 int cache_reply)
617{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400618 return nfs4_call_sync_sequence(server->nfs_client, server->client,
619 msg, args, res, cache_reply);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400620}
621
622#endif /* CONFIG_NFS_V4_1 */
623
624int _nfs4_call_sync(struct nfs_server *server,
625 struct rpc_message *msg,
626 struct nfs4_sequence_args *args,
627 struct nfs4_sequence_res *res,
628 int cache_reply)
629{
Benny Halevyf3752972009-04-01 09:22:04 -0400630 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400631 return rpc_call_sync(server->client, msg, 0);
632}
633
634#define nfs4_call_sync(server, msg, args, res, cache_reply) \
635 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
636 &(res)->seq_res, (cache_reply))
637
Andy Adamsonb0df8062009-04-01 09:22:18 -0400638static void nfs4_sequence_done(const struct nfs_server *server,
639 struct nfs4_sequence_res *res, int rpc_status)
640{
641#ifdef CONFIG_NFS_V4_1
642 if (nfs4_has_session(server->nfs_client))
643 nfs41_sequence_done(server->nfs_client, res, rpc_status);
644#endif /* CONFIG_NFS_V4_1 */
645}
646
647/* no restart, therefore free slot here */
648static void nfs4_sequence_done_free_slot(const struct nfs_server *server,
649 struct nfs4_sequence_res *res,
650 int rpc_status)
651{
652 nfs4_sequence_done(server, res, rpc_status);
653 nfs4_sequence_free_slot(server->nfs_client, res);
654}
655
Trond Myklebust38478b22006-05-25 01:40:57 -0400656static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Trond Myklebust38478b22006-05-25 01:40:57 -0400658 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Trond Myklebust38478b22006-05-25 01:40:57 -0400660 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400661 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
662 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400663 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400664 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400665 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100668struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400669 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100670 struct nfs_openargs o_arg;
671 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100672 struct nfs_open_confirmargs c_arg;
673 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100674 struct nfs_fattr f_attr;
675 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400676 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100677 struct dentry *dir;
678 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400679 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100680 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100681 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400682 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100683 int rpc_status;
684 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100685};
686
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400687
688static void nfs4_init_opendata_res(struct nfs4_opendata *p)
689{
690 p->o_res.f_attr = &p->f_attr;
691 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400692 p->o_res.seqid = p->o_arg.seqid;
693 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400694 p->o_res.server = p->o_arg.server;
695 nfs_fattr_init(&p->f_attr);
696 nfs_fattr_init(&p->dir_attr);
Benny Halevy578e4582009-06-18 22:01:23 -0400697 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400698}
699
Trond Myklebustad389da2007-06-05 12:30:00 -0400700static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500701 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100702 const struct iattr *attrs)
703{
Trond Myklebustad389da2007-06-05 12:30:00 -0400704 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100705 struct inode *dir = parent->d_inode;
706 struct nfs_server *server = NFS_SERVER(dir);
707 struct nfs4_opendata *p;
708
709 p = kzalloc(sizeof(*p), GFP_KERNEL);
710 if (p == NULL)
711 goto err;
712 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
713 if (p->o_arg.seqid == NULL)
714 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400715 p->path.mnt = mntget(path->mnt);
716 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100717 p->dir = parent;
718 p->owner = sp;
719 atomic_inc(&sp->so_count);
720 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500721 p->o_arg.open_flags = flags;
722 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400723 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400724 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400725 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100726 p->o_arg.server = server;
727 p->o_arg.bitmask = server->attr_bitmask;
728 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100729 if (flags & O_EXCL) {
730 u32 *s = (u32 *) p->o_arg.u.verifier.data;
731 s[0] = jiffies;
732 s[1] = current->pid;
733 } else if (flags & O_CREAT) {
734 p->o_arg.u.attrs = &p->attrs;
735 memcpy(&p->attrs, attrs, sizeof(p->attrs));
736 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100737 p->c_arg.fh = &p->o_res.fh;
738 p->c_arg.stateid = &p->o_res.stateid;
739 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400740 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400741 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100742 return p;
743err_free:
744 kfree(p);
745err:
746 dput(parent);
747 return NULL;
748}
749
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400750static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100751{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400752 struct nfs4_opendata *p = container_of(kref,
753 struct nfs4_opendata, kref);
754
755 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400756 if (p->state != NULL)
757 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400758 nfs4_put_state_owner(p->owner);
759 dput(p->dir);
Jan Blunck31f31db2008-05-02 13:42:45 -0700760 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400761 kfree(p);
762}
763
764static void nfs4_opendata_put(struct nfs4_opendata *p)
765{
766 if (p != NULL)
767 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100768}
769
Trond Myklebust06f814a2006-01-03 09:55:07 +0100770static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
771{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100772 int ret;
773
Trond Myklebust06f814a2006-01-03 09:55:07 +0100774 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100775 return ret;
776}
777
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500778static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400779{
780 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500781
782 if (open_mode & O_EXCL)
783 goto out;
784 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400785 case FMODE_READ:
786 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400787 break;
788 case FMODE_WRITE:
789 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400790 break;
791 case FMODE_READ|FMODE_WRITE:
792 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
793 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500794out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400795 return ret;
796}
797
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500798static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400799{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500800 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400801 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500802 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400803 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500804 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400805 return 1;
806}
807
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500808static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100809{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500810 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100811 case FMODE_WRITE:
812 state->n_wronly++;
813 break;
814 case FMODE_READ:
815 state->n_rdonly++;
816 break;
817 case FMODE_READ|FMODE_WRITE:
818 state->n_rdwr++;
819 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500820 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100821}
822
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500823static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400824{
825 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
826 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
827 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500828 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400829 case FMODE_READ:
830 set_bit(NFS_O_RDONLY_STATE, &state->flags);
831 break;
832 case FMODE_WRITE:
833 set_bit(NFS_O_WRONLY_STATE, &state->flags);
834 break;
835 case FMODE_READ|FMODE_WRITE:
836 set_bit(NFS_O_RDWR_STATE, &state->flags);
837 }
838}
839
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500840static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400841{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400842 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500843 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400844 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400845}
846
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500847static 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 -0700848{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400849 /*
850 * Protect the call to nfs4_state_set_mode_locked and
851 * serialise the stateid update
852 */
853 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400854 if (deleg_stateid != NULL) {
855 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
856 set_bit(NFS_DELEGATED_STATE, &state->flags);
857 }
858 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500859 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400860 write_sequnlock(&state->seqlock);
861 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500862 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700863 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500866static 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 -0500867{
868 struct nfs_inode *nfsi = NFS_I(state->inode);
869 struct nfs_delegation *deleg_cur;
870 int ret = 0;
871
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500872 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500873
874 rcu_read_lock();
875 deleg_cur = rcu_dereference(nfsi->delegation);
876 if (deleg_cur == NULL)
877 goto no_delegation;
878
879 spin_lock(&deleg_cur->lock);
880 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500881 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500882 goto no_delegation_unlock;
883
884 if (delegation == NULL)
885 delegation = &deleg_cur->stateid;
886 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
887 goto no_delegation_unlock;
888
Trond Myklebustb7391f42008-12-23 15:21:52 -0500889 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500890 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500891 ret = 1;
892no_delegation_unlock:
893 spin_unlock(&deleg_cur->lock);
894no_delegation:
895 rcu_read_unlock();
896
897 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500898 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500899 ret = 1;
900 }
901
902 return ret;
903}
904
905
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500906static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400907{
908 struct nfs_delegation *delegation;
909
910 rcu_read_lock();
911 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500912 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400913 rcu_read_unlock();
914 return;
915 }
916 rcu_read_unlock();
917 nfs_inode_return_delegation(inode);
918}
919
Trond Myklebust6ee41262007-07-08 14:11:36 -0400920static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400921{
922 struct nfs4_state *state = opendata->state;
923 struct nfs_inode *nfsi = NFS_I(state->inode);
924 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925 int open_mode = opendata->o_arg.open_flags & O_EXCL;
926 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400927 nfs4_stateid stateid;
928 int ret = -EAGAIN;
929
Trond Myklebustaac00a82007-07-05 19:02:21 -0400930 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500931 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400932 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500933 if (can_open_cached(state, fmode, open_mode)) {
934 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400935 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400936 goto out_return_state;
937 }
938 spin_unlock(&state->owner->so_lock);
939 }
Trond Myklebust34310432008-12-23 15:21:38 -0500940 rcu_read_lock();
941 delegation = rcu_dereference(nfsi->delegation);
942 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500943 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500944 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400945 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500946 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400947 /* Save the delegation */
948 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
949 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400950 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400951 if (ret != 0)
952 goto out;
953 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500954
955 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500956 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500957 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400958 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400959out:
960 return ERR_PTR(ret);
961out_return_state:
962 atomic_inc(&state->count);
963 return state;
964}
965
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100966static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
967{
968 struct inode *inode;
969 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400970 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400971 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100972
Trond Myklebustaac00a82007-07-05 19:02:21 -0400973 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400974 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400975 goto out;
976 }
977
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400978 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100979 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400980 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100981 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400982 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500983 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400984 goto err;
985 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100986 state = nfs4_get_open_state(inode, data->owner);
987 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400988 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400989 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400990 int delegation_flags = 0;
991
Trond Myklebust003707c2007-07-05 18:07:55 -0400992 rcu_read_lock();
993 delegation = rcu_dereference(NFS_I(inode)->delegation);
994 if (delegation)
995 delegation_flags = delegation->flags;
996 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -0500997 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400998 nfs_inode_set_delegation(state->inode,
999 data->owner->so_cred,
1000 &data->o_res);
1001 else
1002 nfs_inode_reclaim_delegation(state->inode,
1003 data->owner->so_cred,
1004 &data->o_res);
1005 }
Trond Myklebust34310432008-12-23 15:21:38 -05001006
1007 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001008 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001009 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001010out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001011 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001012err_put_inode:
1013 iput(inode);
1014err:
1015 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001016}
1017
Trond Myklebust864472e2006-01-03 09:55:15 +01001018static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1019{
1020 struct nfs_inode *nfsi = NFS_I(state->inode);
1021 struct nfs_open_context *ctx;
1022
1023 spin_lock(&state->inode->i_lock);
1024 list_for_each_entry(ctx, &nfsi->open_files, list) {
1025 if (ctx->state != state)
1026 continue;
1027 get_nfs_open_context(ctx);
1028 spin_unlock(&state->inode->i_lock);
1029 return ctx;
1030 }
1031 spin_unlock(&state->inode->i_lock);
1032 return ERR_PTR(-ENOENT);
1033}
1034
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001035static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1036{
1037 struct nfs4_opendata *opendata;
1038
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001039 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001040 if (opendata == NULL)
1041 return ERR_PTR(-ENOMEM);
1042 opendata->state = state;
1043 atomic_inc(&state->count);
1044 return opendata;
1045}
1046
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001047static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001048{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001049 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001050 int ret;
1051
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001052 opendata->o_arg.open_flags = 0;
1053 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001054 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1055 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1056 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001057 ret = _nfs4_proc_open(opendata);
1058 if (ret != 0)
1059 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001060 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001061 if (IS_ERR(newstate))
1062 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001063 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001064 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001065 return 0;
1066}
1067
1068static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1069{
Trond Myklebust864472e2006-01-03 09:55:15 +01001070 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001071 int ret;
1072
1073 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001074 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001075 smp_rmb();
1076 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001077 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001078 if (ret != 0)
1079 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001080 if (newstate != state)
1081 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001082 }
1083 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001084 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001085 if (ret != 0)
1086 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001087 if (newstate != state)
1088 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001089 }
1090 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001091 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001092 if (ret != 0)
1093 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001094 if (newstate != state)
1095 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001096 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001097 /*
1098 * We may have performed cached opens for all three recoveries.
1099 * Check if we need to update the current stateid.
1100 */
1101 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1102 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001103 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001104 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1105 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001106 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001107 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001108 return 0;
1109}
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111/*
1112 * OPEN_RECLAIM:
1113 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001115static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001117 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001118 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001119 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 int status;
1121
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001122 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1123 if (IS_ERR(opendata))
1124 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001125 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1126 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001127 rcu_read_lock();
1128 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001129 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001130 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001131 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001132 opendata->o_arg.u.delegation_type = delegation_type;
1133 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001134 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return status;
1136}
1137
Trond Myklebust539cd032007-06-05 11:46:42 -04001138static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
1140 struct nfs_server *server = NFS_SERVER(state->inode);
1141 struct nfs4_exception exception = { };
1142 int err;
1143 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001144 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001145 if (err != -NFS4ERR_DELAY)
1146 break;
1147 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 } while (exception.retry);
1149 return err;
1150}
1151
Trond Myklebust864472e2006-01-03 09:55:15 +01001152static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1153{
1154 struct nfs_open_context *ctx;
1155 int ret;
1156
1157 ctx = nfs4_state_find_open_context(state);
1158 if (IS_ERR(ctx))
1159 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001160 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001161 put_nfs_open_context(ctx);
1162 return ret;
1163}
1164
Trond Myklebust13437e12007-07-06 15:10:43 -04001165static 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 -07001166{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001167 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001168 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001170 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1171 if (IS_ERR(opendata))
1172 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001173 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001174 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001175 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001176 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001177 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001178 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
Trond Myklebust13437e12007-07-06 15:10:43 -04001181int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
1183 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001184 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 int err;
1186 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001187 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 switch (err) {
1189 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001190 case -ENOENT:
1191 case -ESTALE:
1192 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 case -NFS4ERR_STALE_CLIENTID:
1194 case -NFS4ERR_STALE_STATEID:
1195 case -NFS4ERR_EXPIRED:
1196 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001197 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001198 goto out;
1199 case -ERESTARTSYS:
1200 /*
1201 * The show must go on: exit, but mark the
1202 * stateid as needing recovery.
1203 */
1204 case -NFS4ERR_ADMIN_REVOKED:
1205 case -NFS4ERR_BAD_STATEID:
1206 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1207 case -ENOMEM:
1208 err = 0;
1209 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
1211 err = nfs4_handle_exception(server, err, &exception);
1212 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001213out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 return err;
1215}
1216
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001217static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1218{
1219 struct nfs4_opendata *data = calldata;
1220
1221 data->rpc_status = task->tk_status;
1222 if (RPC_ASSASSINATED(task))
1223 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001224 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001225 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1226 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001227 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001228 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001229 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001230 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001231}
1232
1233static void nfs4_open_confirm_release(void *calldata)
1234{
1235 struct nfs4_opendata *data = calldata;
1236 struct nfs4_state *state = NULL;
1237
1238 /* If this request hasn't been cancelled, do nothing */
1239 if (data->cancelled == 0)
1240 goto out_free;
1241 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001242 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001243 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001244 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001245 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001246 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001247out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001248 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001249}
1250
1251static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001252 .rpc_call_done = nfs4_open_confirm_done,
1253 .rpc_release = nfs4_open_confirm_release,
1254};
1255
1256/*
1257 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1258 */
1259static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1260{
1261 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1262 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001263 struct rpc_message msg = {
1264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1265 .rpc_argp = &data->c_arg,
1266 .rpc_resp = &data->c_res,
1267 .rpc_cred = data->owner->so_cred,
1268 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001269 struct rpc_task_setup task_setup_data = {
1270 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001271 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001272 .callback_ops = &nfs4_open_confirm_ops,
1273 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001274 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001275 .flags = RPC_TASK_ASYNC,
1276 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 int status;
1278
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001279 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001280 data->rpc_done = 0;
1281 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001282 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001283 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001284 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001285 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001286 status = nfs4_wait_for_completion_rpc_task(task);
1287 if (status != 0) {
1288 data->cancelled = 1;
1289 smp_wmb();
1290 } else
1291 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001292 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return status;
1294}
1295
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001296static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001298 struct nfs4_opendata *data = calldata;
1299 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001300
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001301 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1302 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001303 /*
1304 * Check if we still need to send an OPEN call, or if we can use
1305 * a delegation instead.
1306 */
1307 if (data->state != NULL) {
1308 struct nfs_delegation *delegation;
1309
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001310 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001311 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001312 rcu_read_lock();
1313 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1314 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001315 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001316 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001317 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001318 }
1319 rcu_read_unlock();
1320 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001321 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001322 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001323 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001324 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001325 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001326 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1327 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001328 data->timestamp = jiffies;
Andy Adamsond8985282009-04-01 09:22:21 -04001329 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1330 &data->o_arg.seq_args,
1331 &data->o_res.seq_res, 1, task))
1332 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001333 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001334 return;
1335out_no_action:
1336 task->tk_action = NULL;
1337
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001338}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001340static void nfs4_open_done(struct rpc_task *task, void *calldata)
1341{
1342 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001344 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001345
1346 nfs4_sequence_done_free_slot(data->o_arg.server, &data->o_res.seq_res,
1347 task->tk_status);
1348
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001349 if (RPC_ASSASSINATED(task))
1350 return;
1351 if (task->tk_status == 0) {
1352 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001353 case S_IFREG:
1354 break;
1355 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001356 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001357 break;
1358 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001359 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001360 break;
1361 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001362 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001363 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001364 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001365 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1366 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001367 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001368 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001369}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001370
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001371static void nfs4_open_release(void *calldata)
1372{
1373 struct nfs4_opendata *data = calldata;
1374 struct nfs4_state *state = NULL;
1375
1376 /* If this request hasn't been cancelled, do nothing */
1377 if (data->cancelled == 0)
1378 goto out_free;
1379 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001380 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001381 goto out_free;
1382 /* In case we need an open_confirm, no cleanup! */
1383 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1384 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001385 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001386 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001387 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001388out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001389 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001390}
1391
1392static const struct rpc_call_ops nfs4_open_ops = {
1393 .rpc_call_prepare = nfs4_open_prepare,
1394 .rpc_call_done = nfs4_open_done,
1395 .rpc_release = nfs4_open_release,
1396};
1397
1398/*
1399 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1400 */
1401static int _nfs4_proc_open(struct nfs4_opendata *data)
1402{
1403 struct inode *dir = data->dir->d_inode;
1404 struct nfs_server *server = NFS_SERVER(dir);
1405 struct nfs_openargs *o_arg = &data->o_arg;
1406 struct nfs_openres *o_res = &data->o_res;
1407 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001408 struct rpc_message msg = {
1409 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1410 .rpc_argp = o_arg,
1411 .rpc_resp = o_res,
1412 .rpc_cred = data->owner->so_cred,
1413 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001414 struct rpc_task_setup task_setup_data = {
1415 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001416 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001417 .callback_ops = &nfs4_open_ops,
1418 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001419 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001420 .flags = RPC_TASK_ASYNC,
1421 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001422 int status;
1423
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001424 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001425 data->rpc_done = 0;
1426 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001427 data->cancelled = 0;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001428 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001429 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001430 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001431 status = nfs4_wait_for_completion_rpc_task(task);
1432 if (status != 0) {
1433 data->cancelled = 1;
1434 smp_wmb();
1435 } else
1436 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001437 rpc_put_task(task);
Trond Myklebust3e309912007-07-07 13:19:59 -04001438 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001439 return status;
1440
Trond Myklebust99367812007-07-17 21:52:41 -04001441 if (o_res->fh.size == 0)
1442 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1443
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001444 if (o_arg->open_flags & O_CREAT) {
1445 update_changeattr(dir, &o_res->cinfo);
1446 nfs_post_op_update_inode(dir, o_res->dir_attr);
1447 } else
1448 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001450 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001452 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
1454 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001455 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
Trond Myklebust10afec92007-05-14 17:16:04 -04001459static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001460{
David Howells7539bba2006-08-22 20:06:09 -04001461 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001462 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001463 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001464
Trond Myklebusta78cb572009-08-09 15:06:19 -04001465 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001466 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001467 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001468 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001469 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1470 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001471 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001472 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001473 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001474 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001475 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001476}
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478/*
1479 * OPEN_EXPIRED:
1480 * reclaim state on the server after a network partition.
1481 * Assumes caller holds the appropriate lock
1482 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001483static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001485 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001486 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001488 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1489 if (IS_ERR(opendata))
1490 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001491 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001492 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001493 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001494 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001495 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497
Trond Myklebust539cd032007-06-05 11:46:42 -04001498static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001499{
Trond Myklebust539cd032007-06-05 11:46:42 -04001500 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001501 struct nfs4_exception exception = { };
1502 int err;
1503
1504 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001505 err = _nfs4_open_expired(ctx, state);
Trond Myklebust027b6ca2008-12-23 16:04:13 -05001506 if (err != -NFS4ERR_DELAY)
1507 break;
1508 nfs4_handle_exception(server, err, &exception);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001509 } while (exception.retry);
1510 return err;
1511}
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1514{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001516 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Trond Myklebust864472e2006-01-03 09:55:15 +01001518 ctx = nfs4_state_find_open_context(state);
1519 if (IS_ERR(ctx))
1520 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001521 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001522 put_nfs_open_context(ctx);
1523 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
1526/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001527 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1528 * fields corresponding to attributes that were used to store the verifier.
1529 * Make sure we clobber those fields in the later setattr call
1530 */
1531static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1532{
1533 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1534 !(sattr->ia_valid & ATTR_ATIME_SET))
1535 sattr->ia_valid |= ATTR_ATIME;
1536
1537 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1538 !(sattr->ia_valid & ATTR_MTIME_SET))
1539 sattr->ia_valid |= ATTR_MTIME;
1540}
1541
1542/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001543 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001545static 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 -07001546{
1547 struct nfs4_state_owner *sp;
1548 struct nfs4_state *state = NULL;
1549 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001550 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001551 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 status = -ENOMEM;
1555 if (!(sp = nfs4_get_state_owner(server, cred))) {
1556 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1557 goto out_err;
1558 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001559 status = nfs4_recover_expired_lease(server);
1560 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001561 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001562 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001563 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001564 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001565 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001566 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001567 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Trond Myklebustaac00a82007-07-05 19:02:21 -04001569 if (path->dentry->d_inode != NULL)
1570 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1571
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001572 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001574 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001576 if (opendata->o_arg.open_flags & O_EXCL)
1577 nfs4_exclusive_attrset(opendata, sattr);
1578
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001579 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001580 status = PTR_ERR(state);
1581 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001582 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001583 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 *res = state;
1586 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001587err_opendata_put:
1588 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001589err_put_state_owner:
1590 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 *res = NULL;
1593 return status;
1594}
1595
1596
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001597static 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 -07001598{
1599 struct nfs4_exception exception = { };
1600 struct nfs4_state *res;
1601 int status;
1602
1603 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001604 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (status == 0)
1606 break;
1607 /* NOTE: BAD_SEQID means the server and client disagree about the
1608 * book-keeping w.r.t. state-changing operations
1609 * (OPEN/CLOSE/LOCK/LOCKU...)
1610 * It is actually a sign of a bug on the client or on the server.
1611 *
1612 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001613 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 * have unhashed the old state_owner for us, and that we can
1615 * therefore safely retry using a new one. We should still warn
1616 * the user though...
1617 */
1618 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001619 printk(KERN_WARNING "NFS: v4 server %s "
1620 " returned a bad sequence-id error!\n",
1621 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 exception.retry = 1;
1623 continue;
1624 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001625 /*
1626 * BAD_STATEID on OPEN means that the server cancelled our
1627 * state before it received the OPEN_CONFIRM.
1628 * Recover by retrying the request as per the discussion
1629 * on Page 181 of RFC3530.
1630 */
1631 if (status == -NFS4ERR_BAD_STATEID) {
1632 exception.retry = 1;
1633 continue;
1634 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001635 if (status == -EAGAIN) {
1636 /* We must have found a delegation */
1637 exception.retry = 1;
1638 continue;
1639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1641 status, &exception));
1642 } while (exception.retry);
1643 return res;
1644}
1645
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001646static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1647 struct nfs_fattr *fattr, struct iattr *sattr,
1648 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001650 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001652 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 .iap = sattr,
1654 .server = server,
1655 .bitmask = server->attr_bitmask,
1656 };
1657 struct nfs_setattrres res = {
1658 .fattr = fattr,
1659 .server = server,
1660 };
1661 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1663 .rpc_argp = &arg,
1664 .rpc_resp = &res,
1665 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001667 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001668 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Trond Myklebust0e574af2005-10-27 22:12:38 -04001670 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001672 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1673 /* Use that stateid */
1674 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001675 nfs4_copy_stateid(&arg.stateid, state, current->files);
1676 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1678
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001679 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001680 if (status == 0 && state != NULL)
1681 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001682 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683}
1684
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001685static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1686 struct nfs_fattr *fattr, struct iattr *sattr,
1687 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001689 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 struct nfs4_exception exception = { };
1691 int err;
1692 do {
1693 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001694 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 &exception);
1696 } while (exception.retry);
1697 return err;
1698}
1699
1700struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001701 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 struct inode *inode;
1703 struct nfs4_state *state;
1704 struct nfs_closeargs arg;
1705 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001706 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001707 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708};
1709
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001710static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001711{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001712 struct nfs4_closedata *calldata = data;
1713 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001714
1715 nfs4_put_open_state(calldata->state);
1716 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001717 nfs4_put_state_owner(sp);
Jan Blunck31f31db2008-05-02 13:42:45 -07001718 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001719 kfree(calldata);
1720}
1721
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001722static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001724 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 struct nfs_server *server = NFS_SERVER(calldata->inode);
1727
Andy Adamson19ddab02009-04-01 09:22:20 -04001728 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001729 if (RPC_ASSASSINATED(task))
1730 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 /* hmm. we are done with the inode, and in the process of freeing
1732 * the state_owner. we keep this around to process errors
1733 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 switch (task->tk_status) {
1735 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001736 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001737 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 break;
1739 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001740 case -NFS4ERR_OLD_STATEID:
1741 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001743 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001744 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001746 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Andy Adamsoneedc0202009-04-01 09:22:41 -04001747 nfs4_restart_rpc(task, server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 return;
1749 }
1750 }
Andy Adamson19ddab02009-04-01 09:22:20 -04001751 nfs4_sequence_free_slot(server->nfs_client, &calldata->res.seq_res);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001752 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
1754
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001755static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001757 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001758 struct nfs4_state *state = calldata->state;
Trond Myklebust003707c2007-07-05 18:07:55 -04001759 int clear_rd, clear_wr, clear_rdwr;
Trond Myklebust95121352005-10-18 14:20:12 -07001760
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001761 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001762 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001763
Trond Myklebust003707c2007-07-05 18:07:55 -04001764 clear_rd = clear_wr = clear_rdwr = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001765 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001766 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001767 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001768 if (state->n_rdonly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001769 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1770 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1771 }
1772 if (state->n_wronly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001773 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1774 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1775 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001776 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001777 spin_unlock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001778 if (!clear_rd && !clear_wr && !clear_rdwr) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001779 /* Note: exit _without_ calling nfs4_close_done */
1780 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001781 return;
1782 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001783 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust45328c32007-07-26 17:47:34 -04001784 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001785 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001786 calldata->arg.fmode = FMODE_READ;
Trond Myklebust45328c32007-07-26 17:47:34 -04001787 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001788 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001789 calldata->arg.fmode = FMODE_WRITE;
Trond Myklebust45328c32007-07-26 17:47:34 -04001790 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001791 calldata->timestamp = jiffies;
Andy Adamson19ddab02009-04-01 09:22:20 -04001792 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1793 &calldata->arg.seq_args, &calldata->res.seq_res,
1794 1, task))
1795 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001796 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
1798
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001799static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001800 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001801 .rpc_call_done = nfs4_close_done,
1802 .rpc_release = nfs4_free_closedata,
1803};
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805/*
1806 * It is possible for data to be read/written from a mem-mapped file
1807 * after the sys_close call (which hits the vfs layer as a flush).
1808 * This means that we can't safely call nfsv4 close on a file until
1809 * the inode is cleared. This in turn means that we are not good
1810 * NFSv4 citizens - we do not indicate to the server to update the file's
1811 * share state even when we are done with one of the three share
1812 * stateid's in the inode.
1813 *
1814 * NOTE: Caller must be holding the sp->so_owner semaphore!
1815 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001816int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001818 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001820 struct nfs4_state_owner *sp = state->owner;
1821 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001822 struct rpc_message msg = {
1823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1824 .rpc_cred = state->owner->so_cred,
1825 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001826 struct rpc_task_setup task_setup_data = {
1827 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001828 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001829 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001830 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001831 .flags = RPC_TASK_ASYNC,
1832 };
Trond Myklebust95121352005-10-18 14:20:12 -07001833 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Andy Adamson19ddab02009-04-01 09:22:20 -04001835 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001837 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001838 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001840 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001841 calldata->arg.stateid = &state->open_stateid;
Benny Halevy34dc1ad2009-04-01 09:22:52 -04001842 if (nfs4_has_session(server->nfs_client))
1843 memset(calldata->arg.stateid->data, 0, 4); /* clear seqid */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001845 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001846 if (calldata->arg.seqid == NULL)
1847 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001848 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001849 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001850 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001851 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001852 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001853 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001854 calldata->path.mnt = mntget(path->mnt);
1855 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001856
Trond Myklebust5138fde2007-07-14 15:40:01 -04001857 msg.rpc_argp = &calldata->arg,
1858 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001859 task_setup_data.callback_data = calldata;
1860 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001861 if (IS_ERR(task))
1862 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001863 status = 0;
1864 if (wait)
1865 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001866 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001867 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001868out_free_calldata:
1869 kfree(calldata);
1870out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001871 nfs4_put_open_state(state);
1872 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001873 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874}
1875
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001876static 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 -07001877{
1878 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001879 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001880
Trond Myklebust1b45c462007-07-03 13:04:56 -04001881 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001882 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001883 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001884 state->owner->so_cred,
1885 nd->intent.open.flags);
1886 if (ret < 0)
1887 goto out_close;
1888 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001889 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001890 if (!IS_ERR(filp)) {
1891 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001892 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001893 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001894 return 0;
1895 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001896 ret = PTR_ERR(filp);
1897out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001898 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001899 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001900}
1901
1902struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1904{
Trond Myklebustad389da2007-06-05 12:30:00 -04001905 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001906 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001907 .dentry = dentry,
1908 };
Jan Blunck4ac91372008-02-14 19:34:32 -08001909 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 struct iattr attr;
1911 struct rpc_cred *cred;
1912 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001913 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001914 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 if (nd->flags & LOOKUP_CREATE) {
1917 attr.ia_mode = nd->intent.open.create_mode;
1918 attr.ia_valid = ATTR_MODE;
1919 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04001920 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 } else {
1922 attr.ia_valid = 0;
1923 BUG_ON(nd->intent.open.flags & O_CREAT);
1924 }
1925
Trond Myklebust98a8e322008-03-12 12:25:28 -04001926 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001928 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04001929 parent = dentry->d_parent;
1930 /* Protect against concurrent sillydeletes */
1931 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001932 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001934 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001935 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001936 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04001937 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1938 }
Trond Myklebust565277f2007-10-15 18:17:53 -04001939 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001940 return (struct dentry *)state;
1941 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001942 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001943 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04001944 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04001945 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001946 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001947 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001948 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949}
1950
1951int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001952nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Trond Myklebustad389da2007-06-05 12:30:00 -04001954 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001955 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001956 .dentry = dentry,
1957 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 struct rpc_cred *cred;
1959 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001960 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Trond Myklebust98a8e322008-03-12 12:25:28 -04001962 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if (IS_ERR(cred))
1964 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001965 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001967 if (IS_ERR(state)) {
1968 switch (PTR_ERR(state)) {
1969 case -EPERM:
1970 case -EACCES:
1971 case -EDQUOT:
1972 case -ENOSPC:
1973 case -EROFS:
1974 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1975 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001976 default:
1977 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001978 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001979 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001980 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001981 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001982 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return 1;
1984 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001985 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001986out_drop:
1987 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 return 0;
1989}
1990
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001991void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1992{
1993 if (ctx->state == NULL)
1994 return;
1995 if (is_sync)
1996 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
1997 else
1998 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
1999}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2002{
Benny Halevy43652ad2009-04-01 09:21:54 -04002003 struct nfs4_server_caps_arg args = {
2004 .fhandle = fhandle,
2005 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 struct nfs4_server_caps_res res = {};
2007 struct rpc_message msg = {
2008 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002009 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 .rpc_resp = &res,
2011 };
2012 int status;
2013
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002014 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (status == 0) {
2016 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002017 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2018 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2019 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2020 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2021 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2023 server->caps |= NFS_CAP_ACLS;
2024 if (res.has_links != 0)
2025 server->caps |= NFS_CAP_HARDLINKS;
2026 if (res.has_symlinks != 0)
2027 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002028 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2029 server->caps |= NFS_CAP_FILEID;
2030 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2031 server->caps |= NFS_CAP_MODE;
2032 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2033 server->caps |= NFS_CAP_NLINK;
2034 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2035 server->caps |= NFS_CAP_OWNER;
2036 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2037 server->caps |= NFS_CAP_OWNER_GROUP;
2038 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2039 server->caps |= NFS_CAP_ATIME;
2040 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2041 server->caps |= NFS_CAP_CTIME;
2042 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2043 server->caps |= NFS_CAP_MTIME;
2044
Trond Myklebusta65318b2009-03-11 14:10:28 -04002045 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2046 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2047 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 server->acl_bitmask = res.acl_bitmask;
2049 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return status;
2052}
2053
Trond Myklebust55a97592006-06-09 09:34:19 -04002054int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
2056 struct nfs4_exception exception = { };
2057 int err;
2058 do {
2059 err = nfs4_handle_exception(server,
2060 _nfs4_server_capabilities(server, fhandle),
2061 &exception);
2062 } while (exception.retry);
2063 return err;
2064}
2065
2066static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2067 struct nfs_fsinfo *info)
2068{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 struct nfs4_lookup_root_arg args = {
2070 .bitmask = nfs4_fattr_bitmap,
2071 };
2072 struct nfs4_lookup_res res = {
2073 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002074 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 .fh = fhandle,
2076 };
2077 struct rpc_message msg = {
2078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2079 .rpc_argp = &args,
2080 .rpc_resp = &res,
2081 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002082
Trond Myklebust0e574af2005-10-27 22:12:38 -04002083 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002084 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
2086
2087static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2088 struct nfs_fsinfo *info)
2089{
2090 struct nfs4_exception exception = { };
2091 int err;
2092 do {
2093 err = nfs4_handle_exception(server,
2094 _nfs4_lookup_root(server, fhandle, info),
2095 &exception);
2096 } while (exception.retry);
2097 return err;
2098}
2099
David Howells54ceac42006-08-22 20:06:13 -04002100/*
2101 * get the file handle for the "/" directory on the server
2102 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002104 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 int status;
2107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (status == 0)
2110 status = nfs4_server_capabilities(server, fhandle);
2111 if (status == 0)
2112 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002113 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
Manoj Naik6b97fd32006-06-09 09:34:29 -04002116/*
2117 * Get locations and (maybe) other attributes of a referral.
2118 * Note that we'll actually follow the referral later when
2119 * we detect fsid mismatch in inode revalidation
2120 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002121static 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 -04002122{
2123 int status = -ENOMEM;
2124 struct page *page = NULL;
2125 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002126
2127 page = alloc_page(GFP_KERNEL);
2128 if (page == NULL)
2129 goto out;
2130 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2131 if (locations == NULL)
2132 goto out;
2133
Trond Myklebustc228fd32007-01-13 02:28:11 -05002134 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002135 if (status != 0)
2136 goto out;
2137 /* Make sure server returned a different fsid for the referral */
2138 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002139 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 -04002140 status = -EIO;
2141 goto out;
2142 }
2143
2144 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2145 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2146 if (!fattr->mode)
2147 fattr->mode = S_IFDIR;
2148 memset(fhandle, 0, sizeof(struct nfs_fh));
2149out:
2150 if (page)
2151 __free_page(page);
2152 if (locations)
2153 kfree(locations);
2154 return status;
2155}
2156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2158{
2159 struct nfs4_getattr_arg args = {
2160 .fh = fhandle,
2161 .bitmask = server->attr_bitmask,
2162 };
2163 struct nfs4_getattr_res res = {
2164 .fattr = fattr,
2165 .server = server,
2166 };
2167 struct rpc_message msg = {
2168 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2169 .rpc_argp = &args,
2170 .rpc_resp = &res,
2171 };
2172
Trond Myklebust0e574af2005-10-27 22:12:38 -04002173 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002174 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175}
2176
2177static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2178{
2179 struct nfs4_exception exception = { };
2180 int err;
2181 do {
2182 err = nfs4_handle_exception(server,
2183 _nfs4_proc_getattr(server, fhandle, fattr),
2184 &exception);
2185 } while (exception.retry);
2186 return err;
2187}
2188
2189/*
2190 * The file is not closed if it is opened due to the a request to change
2191 * the size of the file. The open call will not be needed once the
2192 * VFS layer lookup-intents are implemented.
2193 *
2194 * Close is called when the inode is destroyed.
2195 * If we haven't opened the file for O_WRONLY, we
2196 * need to in the size_change case to obtain a stateid.
2197 *
2198 * Got race?
2199 * Because OPEN is always done by name in nfsv4, it is
2200 * possible that we opened a different file by the same
2201 * name. We can recognize this race condition, but we
2202 * can't do anything about it besides returning an error.
2203 *
2204 * This will be fixed with VFS changes (lookup-intent).
2205 */
2206static int
2207nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2208 struct iattr *sattr)
2209{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002210 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002211 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002212 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 int status;
2214
Trond Myklebust0e574af2005-10-27 22:12:38 -04002215 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Trond Myklebustd5308382005-11-04 15:33:38 -05002217 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002218 if (sattr->ia_valid & ATTR_FILE) {
2219 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002220
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002221 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002222 if (ctx) {
2223 cred = ctx->cred;
2224 state = ctx->state;
2225 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002226 }
2227
2228 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002229 if (status == 0)
2230 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 return status;
2232}
2233
Trond Myklebust56659e92007-07-17 21:52:39 -04002234static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2235 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002236 struct nfs_fattr *fattr)
2237{
2238 int status;
2239 struct nfs4_lookup_arg args = {
2240 .bitmask = server->attr_bitmask,
2241 .dir_fh = dirfh,
2242 .name = name,
2243 };
2244 struct nfs4_lookup_res res = {
2245 .server = server,
2246 .fattr = fattr,
2247 .fh = fhandle,
2248 };
2249 struct rpc_message msg = {
2250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2251 .rpc_argp = &args,
2252 .rpc_resp = &res,
2253 };
2254
2255 nfs_fattr_init(fattr);
2256
2257 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002258 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002259 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002260 return status;
2261}
2262
2263static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2264 struct qstr *name, struct nfs_fh *fhandle,
2265 struct nfs_fattr *fattr)
2266{
2267 struct nfs4_exception exception = { };
2268 int err;
2269 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002270 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2271 /* FIXME: !!!! */
2272 if (err == -NFS4ERR_MOVED) {
2273 err = -EREMOTE;
2274 break;
2275 }
2276 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002277 } while (exception.retry);
2278 return err;
2279}
2280
Trond Myklebust56659e92007-07-17 21:52:39 -04002281static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2283{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002284 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002287 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002288 if (status == -NFS4ERR_MOVED)
2289 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 dprintk("NFS reply lookup: %d\n", status);
2291 return status;
2292}
2293
2294static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2295{
2296 struct nfs4_exception exception = { };
2297 int err;
2298 do {
2299 err = nfs4_handle_exception(NFS_SERVER(dir),
2300 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2301 &exception);
2302 } while (exception.retry);
2303 return err;
2304}
2305
2306static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2307{
Trond Myklebust76b32992007-08-10 17:45:11 -04002308 struct nfs_server *server = NFS_SERVER(inode);
2309 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 struct nfs4_accessargs args = {
2311 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002312 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002314 struct nfs4_accessres res = {
2315 .server = server,
2316 .fattr = &fattr,
2317 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 struct rpc_message msg = {
2319 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2320 .rpc_argp = &args,
2321 .rpc_resp = &res,
2322 .rpc_cred = entry->cred,
2323 };
2324 int mode = entry->mask;
2325 int status;
2326
2327 /*
2328 * Determine which access bits we want to ask for...
2329 */
2330 if (mode & MAY_READ)
2331 args.access |= NFS4_ACCESS_READ;
2332 if (S_ISDIR(inode->i_mode)) {
2333 if (mode & MAY_WRITE)
2334 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2335 if (mode & MAY_EXEC)
2336 args.access |= NFS4_ACCESS_LOOKUP;
2337 } else {
2338 if (mode & MAY_WRITE)
2339 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2340 if (mode & MAY_EXEC)
2341 args.access |= NFS4_ACCESS_EXECUTE;
2342 }
Trond Myklebust76b32992007-08-10 17:45:11 -04002343 nfs_fattr_init(&fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002344 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (!status) {
2346 entry->mask = 0;
2347 if (res.access & NFS4_ACCESS_READ)
2348 entry->mask |= MAY_READ;
2349 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2350 entry->mask |= MAY_WRITE;
2351 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2352 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04002353 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 }
2355 return status;
2356}
2357
2358static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2359{
2360 struct nfs4_exception exception = { };
2361 int err;
2362 do {
2363 err = nfs4_handle_exception(NFS_SERVER(inode),
2364 _nfs4_proc_access(inode, entry),
2365 &exception);
2366 } while (exception.retry);
2367 return err;
2368}
2369
2370/*
2371 * TODO: For the time being, we don't try to get any attributes
2372 * along with any of the zero-copy operations READ, READDIR,
2373 * READLINK, WRITE.
2374 *
2375 * In the case of the first three, we want to put the GETATTR
2376 * after the read-type operation -- this is because it is hard
2377 * to predict the length of a GETATTR response in v4, and thus
2378 * align the READ data correctly. This means that the GETATTR
2379 * may end up partially falling into the page cache, and we should
2380 * shift it into the 'tail' of the xdr_buf before processing.
2381 * To do this efficiently, we need to know the total length
2382 * of data received, which doesn't seem to be available outside
2383 * of the RPC layer.
2384 *
2385 * In the case of WRITE, we also want to put the GETATTR after
2386 * the operation -- in this case because we want to make sure
2387 * we get the post-operation mtime and size. This means that
2388 * we can't use xdr_encode_pages() as written: we need a variant
2389 * of it which would leave room in the 'tail' iovec.
2390 *
2391 * Both of these changes to the XDR layer would in fact be quite
2392 * minor, but I decided to leave them for a subsequent patch.
2393 */
2394static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2395 unsigned int pgbase, unsigned int pglen)
2396{
2397 struct nfs4_readlink args = {
2398 .fh = NFS_FH(inode),
2399 .pgbase = pgbase,
2400 .pglen = pglen,
2401 .pages = &page,
2402 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002403 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 struct rpc_message msg = {
2405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2406 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002407 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 };
2409
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002410 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411}
2412
2413static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2414 unsigned int pgbase, unsigned int pglen)
2415{
2416 struct nfs4_exception exception = { };
2417 int err;
2418 do {
2419 err = nfs4_handle_exception(NFS_SERVER(inode),
2420 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2421 &exception);
2422 } while (exception.retry);
2423 return err;
2424}
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426/*
2427 * Got race?
2428 * We will need to arrange for the VFS layer to provide an atomic open.
2429 * Until then, this create/open method is prone to inefficiency and race
2430 * conditions due to the lookup, create, and open VFS calls from sys_open()
2431 * placed on the wire.
2432 *
2433 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2434 * The file will be opened again in the subsequent VFS open call
2435 * (nfs4_proc_file_open).
2436 *
2437 * The open for read will just hang around to be used by any process that
2438 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2439 */
2440
2441static int
2442nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002443 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
Trond Myklebustad389da2007-06-05 12:30:00 -04002445 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002446 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002447 .dentry = dentry,
2448 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 struct nfs4_state *state;
2450 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002451 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 int status = 0;
2453
Trond Myklebust98a8e322008-03-12 12:25:28 -04002454 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 if (IS_ERR(cred)) {
2456 status = PTR_ERR(cred);
2457 goto out;
2458 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002459 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002460 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 if (IS_ERR(state)) {
2462 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002463 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002465 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002466 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 if (flags & O_EXCL) {
2468 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002469 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002470 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002471 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002472 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002473 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002474 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002475 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002476 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002477 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002478out_putcred:
2479 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480out:
2481 return status;
2482}
2483
2484static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2485{
Trond Myklebust16e42952005-10-27 22:12:44 -04002486 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002487 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002489 .name.len = name->len,
2490 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002491 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002493 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002494 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002495 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2498 .rpc_argp = &args,
2499 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 };
2501 int status;
2502
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002503 nfs_fattr_init(&res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002504 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002505 if (status == 0) {
2506 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002507 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 return status;
2510}
2511
2512static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2513{
2514 struct nfs4_exception exception = { };
2515 int err;
2516 do {
2517 err = nfs4_handle_exception(NFS_SERVER(dir),
2518 _nfs4_proc_remove(dir, name),
2519 &exception);
2520 } while (exception.retry);
2521 return err;
2522}
2523
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002524static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002526 struct nfs_server *server = NFS_SERVER(dir);
2527 struct nfs_removeargs *args = msg->rpc_argp;
2528 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
Trond Myklebusta65318b2009-03-11 14:10:28 -04002530 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002531 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533}
2534
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002535static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002537 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2538
Andy Adamson472cfbd2009-04-01 09:22:24 -04002539 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002540 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002541 return 0;
Andy Adamson472cfbd2009-04-01 09:22:24 -04002542 nfs4_sequence_free_slot(res->server->nfs_client, &res->seq_res);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002543 update_changeattr(dir, &res->cinfo);
2544 nfs_post_op_update_inode(dir, &res->dir_attr);
2545 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546}
2547
2548static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2549 struct inode *new_dir, struct qstr *new_name)
2550{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002551 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 struct nfs4_rename_arg arg = {
2553 .old_dir = NFS_FH(old_dir),
2554 .new_dir = NFS_FH(new_dir),
2555 .old_name = old_name,
2556 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002557 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002559 struct nfs_fattr old_fattr, new_fattr;
2560 struct nfs4_rename_res res = {
2561 .server = server,
2562 .old_fattr = &old_fattr,
2563 .new_fattr = &new_fattr,
2564 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 struct rpc_message msg = {
2566 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2567 .rpc_argp = &arg,
2568 .rpc_resp = &res,
2569 };
2570 int status;
2571
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002572 nfs_fattr_init(res.old_fattr);
2573 nfs_fattr_init(res.new_fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002574 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
2576 if (!status) {
2577 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002578 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002580 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 }
2582 return status;
2583}
2584
2585static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2586 struct inode *new_dir, struct qstr *new_name)
2587{
2588 struct nfs4_exception exception = { };
2589 int err;
2590 do {
2591 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2592 _nfs4_proc_rename(old_dir, old_name,
2593 new_dir, new_name),
2594 &exception);
2595 } while (exception.retry);
2596 return err;
2597}
2598
2599static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2600{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002601 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 struct nfs4_link_arg arg = {
2603 .fh = NFS_FH(inode),
2604 .dir_fh = NFS_FH(dir),
2605 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002606 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002608 struct nfs_fattr fattr, dir_attr;
2609 struct nfs4_link_res res = {
2610 .server = server,
2611 .fattr = &fattr,
2612 .dir_attr = &dir_attr,
2613 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 struct rpc_message msg = {
2615 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2616 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002617 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 };
2619 int status;
2620
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002621 nfs_fattr_init(res.fattr);
2622 nfs_fattr_init(res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002623 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002624 if (!status) {
2625 update_changeattr(dir, &res.cinfo);
2626 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002627 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 return status;
2631}
2632
2633static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2634{
2635 struct nfs4_exception exception = { };
2636 int err;
2637 do {
2638 err = nfs4_handle_exception(NFS_SERVER(inode),
2639 _nfs4_proc_link(inode, dir, name),
2640 &exception);
2641 } while (exception.retry);
2642 return err;
2643}
2644
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002645struct nfs4_createdata {
2646 struct rpc_message msg;
2647 struct nfs4_create_arg arg;
2648 struct nfs4_create_res res;
2649 struct nfs_fh fh;
2650 struct nfs_fattr fattr;
2651 struct nfs_fattr dir_fattr;
2652};
2653
2654static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2655 struct qstr *name, struct iattr *sattr, u32 ftype)
2656{
2657 struct nfs4_createdata *data;
2658
2659 data = kzalloc(sizeof(*data), GFP_KERNEL);
2660 if (data != NULL) {
2661 struct nfs_server *server = NFS_SERVER(dir);
2662
2663 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2664 data->msg.rpc_argp = &data->arg;
2665 data->msg.rpc_resp = &data->res;
2666 data->arg.dir_fh = NFS_FH(dir);
2667 data->arg.server = server;
2668 data->arg.name = name;
2669 data->arg.attrs = sattr;
2670 data->arg.ftype = ftype;
2671 data->arg.bitmask = server->attr_bitmask;
2672 data->res.server = server;
2673 data->res.fh = &data->fh;
2674 data->res.fattr = &data->fattr;
2675 data->res.dir_fattr = &data->dir_fattr;
2676 nfs_fattr_init(data->res.fattr);
2677 nfs_fattr_init(data->res.dir_fattr);
2678 }
2679 return data;
2680}
2681
2682static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2683{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002684 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2685 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002686 if (status == 0) {
2687 update_changeattr(dir, &data->res.dir_cinfo);
2688 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2689 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2690 }
2691 return status;
2692}
2693
2694static void nfs4_free_createdata(struct nfs4_createdata *data)
2695{
2696 kfree(data);
2697}
2698
Chuck Lever4f390c12006-08-22 20:06:22 -04002699static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002700 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002702 struct nfs4_createdata *data;
2703 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Chuck Lever94a6d752006-08-22 20:06:23 -04002705 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002706 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002707
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002708 status = -ENOMEM;
2709 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2710 if (data == NULL)
2711 goto out;
2712
2713 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2714 data->arg.u.symlink.pages = &page;
2715 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002717 status = nfs4_do_create(dir, dentry, data);
2718
2719 nfs4_free_createdata(data);
2720out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 return status;
2722}
2723
Chuck Lever4f390c12006-08-22 20:06:22 -04002724static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002725 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
2727 struct nfs4_exception exception = { };
2728 int err;
2729 do {
2730 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002731 _nfs4_proc_symlink(dir, dentry, page,
2732 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 &exception);
2734 } while (exception.retry);
2735 return err;
2736}
2737
2738static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2739 struct iattr *sattr)
2740{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002741 struct nfs4_createdata *data;
2742 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002744 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2745 if (data == NULL)
2746 goto out;
2747
2748 status = nfs4_do_create(dir, dentry, data);
2749
2750 nfs4_free_createdata(data);
2751out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 return status;
2753}
2754
2755static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2756 struct iattr *sattr)
2757{
2758 struct nfs4_exception exception = { };
2759 int err;
2760 do {
2761 err = nfs4_handle_exception(NFS_SERVER(dir),
2762 _nfs4_proc_mkdir(dir, dentry, sattr),
2763 &exception);
2764 } while (exception.retry);
2765 return err;
2766}
2767
2768static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2769 u64 cookie, struct page *page, unsigned int count, int plus)
2770{
2771 struct inode *dir = dentry->d_inode;
2772 struct nfs4_readdir_arg args = {
2773 .fh = NFS_FH(dir),
2774 .pages = &page,
2775 .pgbase = 0,
2776 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002777 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 };
2779 struct nfs4_readdir_res res;
2780 struct rpc_message msg = {
2781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2782 .rpc_argp = &args,
2783 .rpc_resp = &res,
2784 .rpc_cred = cred,
2785 };
2786 int status;
2787
Harvey Harrison3110ff82008-05-02 13:42:44 -07002788 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002789 dentry->d_parent->d_name.name,
2790 dentry->d_name.name,
2791 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2793 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002794 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 if (status == 0)
2796 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002797
2798 nfs_invalidate_atime(dir);
2799
Harvey Harrison3110ff82008-05-02 13:42:44 -07002800 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 return status;
2802}
2803
2804static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2805 u64 cookie, struct page *page, unsigned int count, int plus)
2806{
2807 struct nfs4_exception exception = { };
2808 int err;
2809 do {
2810 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2811 _nfs4_proc_readdir(dentry, cred, cookie,
2812 page, count, plus),
2813 &exception);
2814 } while (exception.retry);
2815 return err;
2816}
2817
2818static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2819 struct iattr *sattr, dev_t rdev)
2820{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002821 struct nfs4_createdata *data;
2822 int mode = sattr->ia_mode;
2823 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
2825 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2826 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002827
2828 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2829 if (data == NULL)
2830 goto out;
2831
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002833 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002835 data->arg.ftype = NF4BLK;
2836 data->arg.u.device.specdata1 = MAJOR(rdev);
2837 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 }
2839 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002840 data->arg.ftype = NF4CHR;
2841 data->arg.u.device.specdata1 = MAJOR(rdev);
2842 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002845 status = nfs4_do_create(dir, dentry, data);
2846
2847 nfs4_free_createdata(data);
2848out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 return status;
2850}
2851
2852static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2853 struct iattr *sattr, dev_t rdev)
2854{
2855 struct nfs4_exception exception = { };
2856 int err;
2857 do {
2858 err = nfs4_handle_exception(NFS_SERVER(dir),
2859 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2860 &exception);
2861 } while (exception.retry);
2862 return err;
2863}
2864
2865static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2866 struct nfs_fsstat *fsstat)
2867{
2868 struct nfs4_statfs_arg args = {
2869 .fh = fhandle,
2870 .bitmask = server->attr_bitmask,
2871 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002872 struct nfs4_statfs_res res = {
2873 .fsstat = fsstat,
2874 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 struct rpc_message msg = {
2876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2877 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002878 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 };
2880
Trond Myklebust0e574af2005-10-27 22:12:38 -04002881 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002882 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883}
2884
2885static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2886{
2887 struct nfs4_exception exception = { };
2888 int err;
2889 do {
2890 err = nfs4_handle_exception(server,
2891 _nfs4_proc_statfs(server, fhandle, fsstat),
2892 &exception);
2893 } while (exception.retry);
2894 return err;
2895}
2896
2897static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2898 struct nfs_fsinfo *fsinfo)
2899{
2900 struct nfs4_fsinfo_arg args = {
2901 .fh = fhandle,
2902 .bitmask = server->attr_bitmask,
2903 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002904 struct nfs4_fsinfo_res res = {
2905 .fsinfo = fsinfo,
2906 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 struct rpc_message msg = {
2908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2909 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002910 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 };
2912
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002913 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914}
2915
2916static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2917{
2918 struct nfs4_exception exception = { };
2919 int err;
2920
2921 do {
2922 err = nfs4_handle_exception(server,
2923 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2924 &exception);
2925 } while (exception.retry);
2926 return err;
2927}
2928
2929static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2930{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002931 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2933}
2934
2935static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2936 struct nfs_pathconf *pathconf)
2937{
2938 struct nfs4_pathconf_arg args = {
2939 .fh = fhandle,
2940 .bitmask = server->attr_bitmask,
2941 };
Benny Halevyd45b2982009-04-01 09:21:58 -04002942 struct nfs4_pathconf_res res = {
2943 .pathconf = pathconf,
2944 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 struct rpc_message msg = {
2946 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2947 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04002948 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 };
2950
2951 /* None of the pathconf attributes are mandatory to implement */
2952 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2953 memset(pathconf, 0, sizeof(*pathconf));
2954 return 0;
2955 }
2956
Trond Myklebust0e574af2005-10-27 22:12:38 -04002957 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002958 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959}
2960
2961static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2962 struct nfs_pathconf *pathconf)
2963{
2964 struct nfs4_exception exception = { };
2965 int err;
2966
2967 do {
2968 err = nfs4_handle_exception(server,
2969 _nfs4_proc_pathconf(server, fhandle, pathconf),
2970 &exception);
2971 } while (exception.retry);
2972 return err;
2973}
2974
Trond Myklebustec06c092006-03-20 13:44:27 -05002975static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976{
Trond Myklebustec06c092006-03-20 13:44:27 -05002977 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Andy Adamsonf11c88a2009-04-01 09:22:25 -04002979 dprintk("--> %s\n", __func__);
2980
2981 /* nfs4_sequence_free_slot called in the read rpc_call_done */
2982 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
2983
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002984 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Andy Adamsoneedc0202009-04-01 09:22:41 -04002985 nfs4_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05002986 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 }
Trond Myklebust8850df92007-09-28 17:20:07 -04002988
2989 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002991 renew_lease(server, data->timestamp);
2992 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993}
2994
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002995static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002998 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999}
3000
Trond Myklebust788e7a82006-03-20 13:44:27 -05003001static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 struct inode *inode = data->inode;
3004
Andy Adamsondef6ed72009-04-01 09:22:26 -04003005 /* slot is freed in nfs_writeback_done */
3006 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3007 task->tk_status);
3008
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003009 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Andy Adamsoneedc0202009-04-01 09:22:41 -04003010 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003011 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003013 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003015 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003016 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003017 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018}
3019
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003020static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003022 struct nfs_server *server = NFS_SERVER(data->inode);
3023
Trond Myklebusta65318b2009-03-11 14:10:28 -04003024 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003025 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 data->timestamp = jiffies;
3027
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003028 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029}
3030
Trond Myklebust788e7a82006-03-20 13:44:27 -05003031static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 struct inode *inode = data->inode;
3034
Andy Adamson21d9a852009-04-01 09:22:27 -04003035 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3036 task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003037 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Andy Adamsoneedc0202009-04-01 09:22:41 -04003038 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003039 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 }
Andy Adamson21d9a852009-04-01 09:22:27 -04003041 nfs4_sequence_free_slot(NFS_SERVER(inode)->nfs_client,
3042 &data->res.seq_res);
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003043 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003044 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045}
3046
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003047static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003049 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Trond Myklebusta65318b2009-03-11 14:10:28 -04003051 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003052 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003053 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054}
3055
3056/*
3057 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3058 * standalone procedure for queueing an asynchronous RENEW.
3059 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003060static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
David Howellsadfa6f92006-08-22 20:06:08 -04003062 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003063 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
3065 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003066 /* Unless we're shutting down, schedule state recovery! */
3067 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3068 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 return;
3070 }
3071 spin_lock(&clp->cl_lock);
3072 if (time_before(clp->cl_last_renewal,timestamp))
3073 clp->cl_last_renewal = timestamp;
3074 spin_unlock(&clp->cl_lock);
3075}
3076
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003077static const struct rpc_call_ops nfs4_renew_ops = {
3078 .rpc_call_done = nfs4_renew_done,
3079};
3080
David Howellsadfa6f92006-08-22 20:06:08 -04003081int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082{
3083 struct rpc_message msg = {
3084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3085 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003086 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 };
3088
3089 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003090 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091}
3092
David Howellsadfa6f92006-08-22 20:06:08 -04003093int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
3095 struct rpc_message msg = {
3096 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3097 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003098 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 };
3100 unsigned long now = jiffies;
3101 int status;
3102
3103 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3104 if (status < 0)
3105 return status;
3106 spin_lock(&clp->cl_lock);
3107 if (time_before(clp->cl_last_renewal,now))
3108 clp->cl_last_renewal = now;
3109 spin_unlock(&clp->cl_lock);
3110 return 0;
3111}
3112
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003113static inline int nfs4_server_supports_acls(struct nfs_server *server)
3114{
3115 return (server->caps & NFS_CAP_ACLS)
3116 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3117 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3118}
3119
3120/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3121 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3122 * the stack.
3123 */
3124#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3125
3126static void buf_to_pages(const void *buf, size_t buflen,
3127 struct page **pages, unsigned int *pgbase)
3128{
3129 const void *p = buf;
3130
3131 *pgbase = offset_in_page(buf);
3132 p -= *pgbase;
3133 while (p < buf + buflen) {
3134 *(pages++) = virt_to_page(p);
3135 p += PAGE_CACHE_SIZE;
3136 }
3137}
3138
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003139struct nfs4_cached_acl {
3140 int cached;
3141 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003142 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003143};
3144
3145static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003146{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003147 struct nfs_inode *nfsi = NFS_I(inode);
3148
3149 spin_lock(&inode->i_lock);
3150 kfree(nfsi->nfs4_acl);
3151 nfsi->nfs4_acl = acl;
3152 spin_unlock(&inode->i_lock);
3153}
3154
3155static void nfs4_zap_acl_attr(struct inode *inode)
3156{
3157 nfs4_set_cached_acl(inode, NULL);
3158}
3159
3160static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3161{
3162 struct nfs_inode *nfsi = NFS_I(inode);
3163 struct nfs4_cached_acl *acl;
3164 int ret = -ENOENT;
3165
3166 spin_lock(&inode->i_lock);
3167 acl = nfsi->nfs4_acl;
3168 if (acl == NULL)
3169 goto out;
3170 if (buf == NULL) /* user is just asking for length */
3171 goto out_len;
3172 if (acl->cached == 0)
3173 goto out;
3174 ret = -ERANGE; /* see getxattr(2) man page */
3175 if (acl->len > buflen)
3176 goto out;
3177 memcpy(buf, acl->data, acl->len);
3178out_len:
3179 ret = acl->len;
3180out:
3181 spin_unlock(&inode->i_lock);
3182 return ret;
3183}
3184
3185static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3186{
3187 struct nfs4_cached_acl *acl;
3188
3189 if (buf && acl_len <= PAGE_SIZE) {
3190 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3191 if (acl == NULL)
3192 goto out;
3193 acl->cached = 1;
3194 memcpy(acl->data, buf, acl_len);
3195 } else {
3196 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3197 if (acl == NULL)
3198 goto out;
3199 acl->cached = 0;
3200 }
3201 acl->len = acl_len;
3202out:
3203 nfs4_set_cached_acl(inode, acl);
3204}
3205
Trond Myklebust16b42892006-08-24 12:27:15 -04003206static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003207{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003208 struct page *pages[NFS4ACL_MAXPAGES];
3209 struct nfs_getaclargs args = {
3210 .fh = NFS_FH(inode),
3211 .acl_pages = pages,
3212 .acl_len = buflen,
3213 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003214 struct nfs_getaclres res = {
3215 .acl_len = buflen,
3216 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003217 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003218 struct rpc_message msg = {
3219 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3220 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003221 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003222 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003223 struct page *localpage = NULL;
3224 int ret;
3225
3226 if (buflen < PAGE_SIZE) {
3227 /* As long as we're doing a round trip to the server anyway,
3228 * let's be prepared for a page of acl data. */
3229 localpage = alloc_page(GFP_KERNEL);
3230 resp_buf = page_address(localpage);
3231 if (localpage == NULL)
3232 return -ENOMEM;
3233 args.acl_pages[0] = localpage;
3234 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003235 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003236 } else {
3237 resp_buf = buf;
3238 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3239 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003240 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003241 if (ret)
3242 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003243 if (res.acl_len > args.acl_len)
3244 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003245 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003246 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003247 if (buf) {
3248 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003249 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003250 goto out_free;
3251 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003252 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003253 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003254 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003255out_free:
3256 if (localpage)
3257 __free_page(localpage);
3258 return ret;
3259}
3260
Trond Myklebust16b42892006-08-24 12:27:15 -04003261static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3262{
3263 struct nfs4_exception exception = { };
3264 ssize_t ret;
3265 do {
3266 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3267 if (ret >= 0)
3268 break;
3269 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3270 } while (exception.retry);
3271 return ret;
3272}
3273
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003274static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3275{
3276 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003277 int ret;
3278
3279 if (!nfs4_server_supports_acls(server))
3280 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003281 ret = nfs_revalidate_inode(server, inode);
3282 if (ret < 0)
3283 return ret;
3284 ret = nfs4_read_cached_acl(inode, buf, buflen);
3285 if (ret != -ENOENT)
3286 return ret;
3287 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003288}
3289
Trond Myklebust16b42892006-08-24 12:27:15 -04003290static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003291{
3292 struct nfs_server *server = NFS_SERVER(inode);
3293 struct page *pages[NFS4ACL_MAXPAGES];
3294 struct nfs_setaclargs arg = {
3295 .fh = NFS_FH(inode),
3296 .acl_pages = pages,
3297 .acl_len = buflen,
3298 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003299 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003300 struct rpc_message msg = {
3301 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3302 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003303 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003304 };
3305 int ret;
3306
3307 if (!nfs4_server_supports_acls(server))
3308 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003309 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003310 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003311 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003312 nfs_access_zap_cache(inode);
3313 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003314 return ret;
3315}
3316
Trond Myklebust16b42892006-08-24 12:27:15 -04003317static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3318{
3319 struct nfs4_exception exception = { };
3320 int err;
3321 do {
3322 err = nfs4_handle_exception(NFS_SERVER(inode),
3323 __nfs4_proc_set_acl(inode, buf, buflen),
3324 &exception);
3325 } while (exception.retry);
3326 return err;
3327}
3328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329static int
Andy Adamson8328d59f2009-04-01 09:22:35 -04003330_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 -07003331{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 if (!clp || task->tk_status >= 0)
3333 return 0;
3334 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003335 case -NFS4ERR_ADMIN_REVOKED:
3336 case -NFS4ERR_BAD_STATEID:
3337 case -NFS4ERR_OPENMODE:
3338 if (state == NULL)
3339 break;
3340 nfs4_state_mark_reclaim_nograce(clp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 case -NFS4ERR_STALE_CLIENTID:
3342 case -NFS4ERR_STALE_STATEID:
3343 case -NFS4ERR_EXPIRED:
Trond Myklebust5d008372008-02-22 16:34:17 -05003344 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 nfs4_schedule_state_recovery(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05003346 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
Trond Myklebustfda13932008-02-22 16:34:12 -05003347 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 task->tk_status = 0;
3349 return -EAGAIN;
Andy Adamson4745e312009-04-01 09:22:42 -04003350#if defined(CONFIG_NFS_V4_1)
3351 case -NFS4ERR_BADSESSION:
3352 case -NFS4ERR_BADSLOT:
3353 case -NFS4ERR_BAD_HIGH_SLOT:
3354 case -NFS4ERR_DEADSESSION:
3355 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3356 case -NFS4ERR_SEQ_FALSE_RETRY:
3357 case -NFS4ERR_SEQ_MISORDERED:
3358 dprintk("%s ERROR %d, Reset session\n", __func__,
3359 task->tk_status);
3360 set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
3361 task->tk_status = 0;
3362 return -EAGAIN;
3363#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 case -NFS4ERR_DELAY:
Andy Adamson8328d59f2009-04-01 09:22:35 -04003365 if (server)
3366 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003367 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3369 task->tk_status = 0;
3370 return -EAGAIN;
3371 case -NFS4ERR_OLD_STATEID:
3372 task->tk_status = 0;
3373 return -EAGAIN;
3374 }
3375 task->tk_status = nfs4_map_errors(task->tk_status);
3376 return 0;
3377}
3378
Andy Adamson8328d59f2009-04-01 09:22:35 -04003379static int
3380nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3381{
3382 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3383}
3384
David Howellsadfa6f92006-08-22 20:06:08 -04003385int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386{
3387 nfs4_verifier sc_verifier;
3388 struct nfs4_setclientid setclientid = {
3389 .sc_verifier = &sc_verifier,
3390 .sc_prog = program,
3391 };
3392 struct rpc_message msg = {
3393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3394 .rpc_argp = &setclientid,
3395 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003396 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 };
Al Virobc4785c2006-10-19 23:28:51 -07003398 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 int loop = 0;
3400 int status;
3401
Al Virobc4785c2006-10-19 23:28:51 -07003402 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3404 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3405
3406 for(;;) {
3407 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003408 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003409 clp->cl_ipaddr,
3410 rpc_peeraddr2str(clp->cl_rpcclient,
3411 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003412 rpc_peeraddr2str(clp->cl_rpcclient,
3413 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003414 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 clp->cl_id_uniquifier);
3416 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003417 sizeof(setclientid.sc_netid),
3418 rpc_peeraddr2str(clp->cl_rpcclient,
3419 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003421 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 clp->cl_ipaddr, port >> 8, port & 255);
3423
3424 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3425 if (status != -NFS4ERR_CLID_INUSE)
3426 break;
3427 if (signalled())
3428 break;
3429 if (loop++ & 1)
3430 ssleep(clp->cl_lease_time + 1);
3431 else
3432 if (++clp->cl_id_uniquifier == 0)
3433 break;
3434 }
3435 return status;
3436}
3437
David Howellsadfa6f92006-08-22 20:06:08 -04003438static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439{
3440 struct nfs_fsinfo fsinfo;
3441 struct rpc_message msg = {
3442 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3443 .rpc_argp = clp,
3444 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003445 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 };
3447 unsigned long now;
3448 int status;
3449
3450 now = jiffies;
3451 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3452 if (status == 0) {
3453 spin_lock(&clp->cl_lock);
3454 clp->cl_lease_time = fsinfo.lease_time * HZ;
3455 clp->cl_last_renewal = now;
3456 spin_unlock(&clp->cl_lock);
3457 }
3458 return status;
3459}
3460
David Howellsadfa6f92006-08-22 20:06:08 -04003461int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003462{
Benny Halevy77e03672008-06-24 20:25:57 +03003463 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003464 int err;
3465 do {
3466 err = _nfs4_proc_setclientid_confirm(clp, cred);
3467 switch (err) {
3468 case 0:
3469 return err;
3470 case -NFS4ERR_RESOURCE:
3471 /* The IBM lawyers misread another document! */
3472 case -NFS4ERR_DELAY:
3473 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3474 }
3475 } while (err == 0);
3476 return err;
3477}
3478
Trond Myklebustfe650402006-01-03 09:55:18 +01003479struct nfs4_delegreturndata {
3480 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003481 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003482 struct nfs_fh fh;
3483 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003484 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003485 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003486 int rpc_status;
3487};
3488
Trond Myklebustfe650402006-01-03 09:55:18 +01003489static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3490{
3491 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003492
3493 nfs4_sequence_done_free_slot(data->res.server, &data->res.seq_res,
3494 task->tk_status);
3495
Trond Myklebustfe650402006-01-03 09:55:18 +01003496 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003497 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01003498 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01003499}
3500
3501static void nfs4_delegreturn_release(void *calldata)
3502{
Trond Myklebustfe650402006-01-03 09:55:18 +01003503 kfree(calldata);
3504}
3505
Andy Adamson938e1012009-04-01 09:22:28 -04003506#if defined(CONFIG_NFS_V4_1)
3507static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3508{
3509 struct nfs4_delegreturndata *d_data;
3510
3511 d_data = (struct nfs4_delegreturndata *)data;
3512
3513 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3514 &d_data->args.seq_args,
3515 &d_data->res.seq_res, 1, task))
3516 return;
3517 rpc_call_start(task);
3518}
3519#endif /* CONFIG_NFS_V4_1 */
3520
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003521static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003522#if defined(CONFIG_NFS_V4_1)
3523 .rpc_call_prepare = nfs4_delegreturn_prepare,
3524#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003525 .rpc_call_done = nfs4_delegreturn_done,
3526 .rpc_release = nfs4_delegreturn_release,
3527};
3528
Trond Myklebuste6f81072008-01-24 18:14:34 -05003529static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003530{
3531 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003532 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003533 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003534 struct rpc_message msg = {
3535 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3536 .rpc_cred = cred,
3537 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003538 struct rpc_task_setup task_setup_data = {
3539 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003540 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003541 .callback_ops = &nfs4_delegreturn_ops,
3542 .flags = RPC_TASK_ASYNC,
3543 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003544 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003545
Andy Adamson938e1012009-04-01 09:22:28 -04003546 data = kzalloc(sizeof(*data), GFP_KERNEL);
Trond Myklebustfe650402006-01-03 09:55:18 +01003547 if (data == NULL)
3548 return -ENOMEM;
3549 data->args.fhandle = &data->fh;
3550 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003551 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003552 nfs_copy_fh(&data->fh, NFS_FH(inode));
3553 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003554 data->res.fattr = &data->fattr;
3555 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003556 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003557 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003558 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003559 data->rpc_status = 0;
3560
Trond Myklebustc970aa82007-07-14 15:39:59 -04003561 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003562 msg.rpc_argp = &data->args,
3563 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003564 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003565 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003566 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003567 if (!issync)
3568 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003569 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003570 if (status != 0)
3571 goto out;
3572 status = data->rpc_status;
3573 if (status != 0)
3574 goto out;
3575 nfs_refresh_inode(inode, &data->fattr);
3576out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003577 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003578 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579}
3580
Trond Myklebuste6f81072008-01-24 18:14:34 -05003581int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582{
3583 struct nfs_server *server = NFS_SERVER(inode);
3584 struct nfs4_exception exception = { };
3585 int err;
3586 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003587 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 switch (err) {
3589 case -NFS4ERR_STALE_STATEID:
3590 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 case 0:
3592 return 0;
3593 }
3594 err = nfs4_handle_exception(server, err, &exception);
3595 } while (exception.retry);
3596 return err;
3597}
3598
3599#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3600#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3601
3602/*
3603 * sleep, with exponential backoff, and retry the LOCK operation.
3604 */
3605static unsigned long
3606nfs4_set_lock_task_retry(unsigned long timeout)
3607{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003608 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 timeout <<= 1;
3610 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3611 return NFS4_LOCK_MAXTIMEOUT;
3612 return timeout;
3613}
3614
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3616{
3617 struct inode *inode = state->inode;
3618 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003619 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003620 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003622 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003624 struct nfs_lockt_res res = {
3625 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 };
3627 struct rpc_message msg = {
3628 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3629 .rpc_argp = &arg,
3630 .rpc_resp = &res,
3631 .rpc_cred = state->owner->so_cred,
3632 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 struct nfs4_lock_state *lsp;
3634 int status;
3635
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003636 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003637 status = nfs4_set_lock_state(state, request);
3638 if (status != 0)
3639 goto out;
3640 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003641 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003642 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003643 switch (status) {
3644 case 0:
3645 request->fl_type = F_UNLCK;
3646 break;
3647 case -NFS4ERR_DENIED:
3648 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003650 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003651out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 return status;
3653}
3654
3655static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3656{
3657 struct nfs4_exception exception = { };
3658 int err;
3659
3660 do {
3661 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3662 _nfs4_proc_getlk(state, cmd, request),
3663 &exception);
3664 } while (exception.retry);
3665 return err;
3666}
3667
3668static int do_vfs_lock(struct file *file, struct file_lock *fl)
3669{
3670 int res = 0;
3671 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3672 case FL_POSIX:
3673 res = posix_lock_file_wait(file, fl);
3674 break;
3675 case FL_FLOCK:
3676 res = flock_lock_file_wait(file, fl);
3677 break;
3678 default:
3679 BUG();
3680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 return res;
3682}
3683
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003684struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003685 struct nfs_locku_args arg;
3686 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003687 struct nfs4_lock_state *lsp;
3688 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003689 struct file_lock fl;
3690 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003691 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003692};
3693
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003694static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3695 struct nfs_open_context *ctx,
3696 struct nfs4_lock_state *lsp,
3697 struct nfs_seqid *seqid)
3698{
3699 struct nfs4_unlockdata *p;
3700 struct inode *inode = lsp->ls_state->inode;
3701
Andy Adamsona8936932009-04-01 09:22:23 -04003702 p = kzalloc(sizeof(*p), GFP_KERNEL);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003703 if (p == NULL)
3704 return NULL;
3705 p->arg.fh = NFS_FH(inode);
3706 p->arg.fl = &p->fl;
3707 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003708 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003709 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003710 p->arg.stateid = &lsp->ls_stateid;
3711 p->lsp = lsp;
3712 atomic_inc(&lsp->ls_count);
3713 /* Ensure we don't close file until we're done freeing locks! */
3714 p->ctx = get_nfs_open_context(ctx);
3715 memcpy(&p->fl, fl, sizeof(p->fl));
3716 p->server = NFS_SERVER(inode);
3717 return p;
3718}
3719
Trond Myklebust06f814a2006-01-03 09:55:07 +01003720static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003721{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003722 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003723 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003724 nfs4_put_lock_state(calldata->lsp);
3725 put_nfs_open_context(calldata->ctx);
3726 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003727}
3728
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003729static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003730{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003731 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003732
Andy Adamsona8936932009-04-01 09:22:23 -04003733 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3734 task->tk_status);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003735 if (RPC_ASSASSINATED(task))
3736 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003737 switch (task->tk_status) {
3738 case 0:
3739 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003740 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003741 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003742 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003743 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003744 case -NFS4ERR_BAD_STATEID:
3745 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003746 case -NFS4ERR_STALE_STATEID:
3747 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003748 break;
3749 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003750 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Andy Adamsoneedc0202009-04-01 09:22:41 -04003751 nfs4_restart_rpc(task,
3752 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003753 }
Andy Adamsona8936932009-04-01 09:22:23 -04003754 nfs4_sequence_free_slot(calldata->server->nfs_client,
3755 &calldata->res.seq_res);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003756}
3757
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003758static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003759{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003760 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003762 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003763 return;
3764 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003765 /* Note: exit _without_ running nfs4_locku_done */
3766 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003767 return;
3768 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003769 calldata->timestamp = jiffies;
Andy Adamsona8936932009-04-01 09:22:23 -04003770 if (nfs4_setup_sequence(calldata->server->nfs_client,
3771 &calldata->arg.seq_args,
3772 &calldata->res.seq_res, 1, task))
3773 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003774 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775}
3776
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003777static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003778 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003779 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003780 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003781};
3782
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003783static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3784 struct nfs_open_context *ctx,
3785 struct nfs4_lock_state *lsp,
3786 struct nfs_seqid *seqid)
3787{
3788 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003789 struct rpc_message msg = {
3790 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3791 .rpc_cred = ctx->cred,
3792 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003793 struct rpc_task_setup task_setup_data = {
3794 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003795 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003796 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003797 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003798 .flags = RPC_TASK_ASYNC,
3799 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003800
Frank Filz137d6ac2007-07-09 15:32:29 -07003801 /* Ensure this is an unlock - when canceling a lock, the
3802 * canceled lock is passed in, and it won't be an unlock.
3803 */
3804 fl->fl_type = F_UNLCK;
3805
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003806 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3807 if (data == NULL) {
3808 nfs_free_seqid(seqid);
3809 return ERR_PTR(-ENOMEM);
3810 }
3811
Trond Myklebust5138fde2007-07-14 15:40:01 -04003812 msg.rpc_argp = &data->arg,
3813 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003814 task_setup_data.callback_data = data;
3815 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003816}
3817
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3819{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003820 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003821 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003822 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003823 struct rpc_task *task;
3824 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003825 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
Trond Myklebust9b073572006-06-29 16:38:34 -04003827 status = nfs4_set_lock_state(state, request);
3828 /* Unlock _before_ we do the RPC call */
3829 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003830 down_read(&nfsi->rwsem);
3831 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3832 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003833 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003834 }
3835 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003836 if (status != 0)
3837 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003838 /* Is this a delegated lock? */
3839 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003840 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003841 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003842 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003843 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003844 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003845 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003846 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003847 status = PTR_ERR(task);
3848 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003849 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003850 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003851 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003852out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003853 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003854 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855}
3856
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003857struct nfs4_lockdata {
3858 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003859 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003860 struct nfs4_lock_state *lsp;
3861 struct nfs_open_context *ctx;
3862 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003863 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003864 int rpc_status;
3865 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04003866 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003867};
3868
3869static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3870 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3871{
3872 struct nfs4_lockdata *p;
3873 struct inode *inode = lsp->ls_state->inode;
3874 struct nfs_server *server = NFS_SERVER(inode);
3875
3876 p = kzalloc(sizeof(*p), GFP_KERNEL);
3877 if (p == NULL)
3878 return NULL;
3879
3880 p->arg.fh = NFS_FH(inode);
3881 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003882 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3883 if (p->arg.open_seqid == NULL)
3884 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003885 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3886 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003887 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003888 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003889 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003890 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003891 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003892 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003893 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04003894 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003895 atomic_inc(&lsp->ls_count);
3896 p->ctx = get_nfs_open_context(ctx);
3897 memcpy(&p->fl, fl, sizeof(p->fl));
3898 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003899out_free_seqid:
3900 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003901out_free:
3902 kfree(p);
3903 return NULL;
3904}
3905
3906static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3907{
3908 struct nfs4_lockdata *data = calldata;
3909 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
Harvey Harrison3110ff82008-05-02 13:42:44 -07003911 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003912 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3913 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003914 /* Do we need to do an open_to_lock_owner? */
3915 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05003916 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3917 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003918 data->arg.open_stateid = &state->stateid;
3919 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003920 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003921 } else
3922 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003923 data->timestamp = jiffies;
Andy Adamson66179ef2009-04-01 09:22:22 -04003924 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
3925 &data->res.seq_res, 1, task))
3926 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003927 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003928 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003929}
3930
3931static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3932{
3933 struct nfs4_lockdata *data = calldata;
3934
Harvey Harrison3110ff82008-05-02 13:42:44 -07003935 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003936
Andy Adamson66179ef2009-04-01 09:22:22 -04003937 nfs4_sequence_done_free_slot(data->server, &data->res.seq_res,
3938 task->tk_status);
3939
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003940 data->rpc_status = task->tk_status;
3941 if (RPC_ASSASSINATED(task))
3942 goto out;
3943 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003944 if (data->rpc_status == 0)
3945 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3946 else
3947 goto out;
3948 }
3949 if (data->rpc_status == 0) {
3950 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3951 sizeof(data->lsp->ls_stateid.data));
3952 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003953 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003954 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003955out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07003956 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003957}
3958
3959static void nfs4_lock_release(void *calldata)
3960{
3961 struct nfs4_lockdata *data = calldata;
3962
Harvey Harrison3110ff82008-05-02 13:42:44 -07003963 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003964 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003965 if (data->cancelled != 0) {
3966 struct rpc_task *task;
3967 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3968 data->arg.lock_seqid);
3969 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003970 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003971 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003972 } else
3973 nfs_free_seqid(data->arg.lock_seqid);
3974 nfs4_put_lock_state(data->lsp);
3975 put_nfs_open_context(data->ctx);
3976 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003977 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003978}
3979
3980static const struct rpc_call_ops nfs4_lock_ops = {
3981 .rpc_call_prepare = nfs4_lock_prepare,
3982 .rpc_call_done = nfs4_lock_done,
3983 .rpc_release = nfs4_lock_release,
3984};
3985
3986static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3987{
3988 struct nfs4_lockdata *data;
3989 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003990 struct rpc_message msg = {
3991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3992 .rpc_cred = state->owner->so_cred,
3993 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003994 struct rpc_task_setup task_setup_data = {
3995 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003996 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003997 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003998 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003999 .flags = RPC_TASK_ASYNC,
4000 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004001 int ret;
4002
Harvey Harrison3110ff82008-05-02 13:42:44 -07004003 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004004 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004005 fl->fl_u.nfs4_fl.owner);
4006 if (data == NULL)
4007 return -ENOMEM;
4008 if (IS_SETLKW(cmd))
4009 data->arg.block = 1;
4010 if (reclaim != 0)
4011 data->arg.reclaim = 1;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004012 msg.rpc_argp = &data->arg,
4013 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004014 task_setup_data.callback_data = data;
4015 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004016 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004017 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004018 ret = nfs4_wait_for_completion_rpc_task(task);
4019 if (ret == 0) {
4020 ret = data->rpc_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004021 } else
4022 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004023 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004024 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004025 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026}
4027
4028static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4029{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004030 struct nfs_server *server = NFS_SERVER(state->inode);
4031 struct nfs4_exception exception = { };
4032 int err;
4033
4034 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004035 /* Cache the lock if possible... */
4036 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4037 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004038 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
4039 if (err != -NFS4ERR_DELAY)
4040 break;
4041 nfs4_handle_exception(server, err, &exception);
4042 } while (exception.retry);
4043 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044}
4045
4046static int nfs4_lock_expired(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
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004052 err = nfs4_set_lock_state(state, request);
4053 if (err != 0)
4054 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004055 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004056 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4057 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004058 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
4059 if (err != -NFS4ERR_DELAY)
4060 break;
4061 nfs4_handle_exception(server, err, &exception);
4062 } while (exception.retry);
4063 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064}
4065
4066static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4067{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004068 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004069 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 int status;
4071
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004072 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004073 status = nfs4_set_lock_state(state, request);
4074 if (status != 0)
4075 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004076 request->fl_flags |= FL_ACCESS;
4077 status = do_vfs_lock(request->fl_file, request);
4078 if (status < 0)
4079 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004080 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004081 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004082 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004083 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004084 request->fl_flags = fl_flags & ~FL_SLEEP;
4085 status = do_vfs_lock(request->fl_file, request);
4086 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004087 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004088 status = _nfs4_do_setlk(state, cmd, request, 0);
4089 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004090 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004091 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004092 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004093 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004094 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004095out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004096 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004097out:
4098 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 return status;
4100}
4101
4102static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4103{
4104 struct nfs4_exception exception = { };
4105 int err;
4106
4107 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004108 err = _nfs4_proc_setlk(state, cmd, request);
4109 if (err == -NFS4ERR_DENIED)
4110 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004112 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 } while (exception.retry);
4114 return err;
4115}
4116
4117static int
4118nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4119{
4120 struct nfs_open_context *ctx;
4121 struct nfs4_state *state;
4122 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4123 int status;
4124
4125 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004126 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 state = ctx->state;
4128
4129 if (request->fl_start < 0 || request->fl_end < 0)
4130 return -EINVAL;
4131
Trond Myklebustd9531262009-07-21 19:22:38 -04004132 if (IS_GETLK(cmd)) {
4133 if (state != NULL)
4134 return nfs4_proc_getlk(state, F_GETLK, request);
4135 return 0;
4136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137
4138 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4139 return -EINVAL;
4140
Trond Myklebustd9531262009-07-21 19:22:38 -04004141 if (request->fl_type == F_UNLCK) {
4142 if (state != NULL)
4143 return nfs4_proc_unlck(state, cmd, request);
4144 return 0;
4145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
Trond Myklebustd9531262009-07-21 19:22:38 -04004147 if (state == NULL)
4148 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 do {
4150 status = nfs4_proc_setlk(state, cmd, request);
4151 if ((status != -EAGAIN) || IS_SETLK(cmd))
4152 break;
4153 timeout = nfs4_set_lock_task_retry(timeout);
4154 status = -ERESTARTSYS;
4155 if (signalled())
4156 break;
4157 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 return status;
4159}
4160
Trond Myklebust888e6942005-11-04 15:38:11 -05004161int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4162{
4163 struct nfs_server *server = NFS_SERVER(state->inode);
4164 struct nfs4_exception exception = { };
4165 int err;
4166
4167 err = nfs4_set_lock_state(state, fl);
4168 if (err != 0)
4169 goto out;
4170 do {
4171 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
Trond Myklebustd5122202009-06-17 13:22:58 -07004172 switch (err) {
4173 default:
4174 printk(KERN_ERR "%s: unhandled error %d.\n",
4175 __func__, err);
4176 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004177 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004178 goto out;
4179 case -NFS4ERR_EXPIRED:
4180 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004181 case -NFS4ERR_STALE_STATEID:
Trond Myklebustd5122202009-06-17 13:22:58 -07004182 nfs4_schedule_state_recovery(server->nfs_client);
4183 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004184 case -ERESTARTSYS:
4185 /*
4186 * The show must go on: exit, but mark the
4187 * stateid as needing recovery.
4188 */
4189 case -NFS4ERR_ADMIN_REVOKED:
4190 case -NFS4ERR_BAD_STATEID:
4191 case -NFS4ERR_OPENMODE:
4192 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4193 err = 0;
4194 goto out;
4195 case -ENOMEM:
4196 case -NFS4ERR_DENIED:
4197 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4198 err = 0;
4199 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004200 case -NFS4ERR_DELAY:
4201 break;
4202 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004203 err = nfs4_handle_exception(server, err, &exception);
4204 } while (exception.retry);
4205out:
4206 return err;
4207}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004208
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004209#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4210
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004211int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4212 size_t buflen, int flags)
4213{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004214 struct inode *inode = dentry->d_inode;
4215
4216 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4217 return -EOPNOTSUPP;
4218
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004219 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004220}
4221
4222/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4223 * and that's what we'll do for e.g. user attributes that haven't been set.
4224 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4225 * attributes in kernel-managed attribute namespaces. */
4226ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4227 size_t buflen)
4228{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004229 struct inode *inode = dentry->d_inode;
4230
4231 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4232 return -EOPNOTSUPP;
4233
4234 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004235}
4236
4237ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4238{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004239 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004240
J. Bruce Fields096455a2006-03-20 23:23:42 -05004241 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4242 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004243 if (buf && buflen < len)
4244 return -ERANGE;
4245 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004246 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4247 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004248}
4249
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004250static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4251{
4252 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4253 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4254 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4255 return;
4256
4257 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4258 NFS_ATTR_FATTR_NLINK;
4259 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4260 fattr->nlink = 2;
4261}
4262
Trond Myklebust56659e92007-07-17 21:52:39 -04004263int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004264 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004265{
4266 struct nfs_server *server = NFS_SERVER(dir);
4267 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004268 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4269 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004270 };
4271 struct nfs4_fs_locations_arg args = {
4272 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004273 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004274 .page = page,
4275 .bitmask = bitmask,
4276 };
Benny Halevy22958462009-04-01 09:22:02 -04004277 struct nfs4_fs_locations_res res = {
4278 .fs_locations = fs_locations,
4279 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004280 struct rpc_message msg = {
4281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4282 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004283 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004284 };
4285 int status;
4286
Harvey Harrison3110ff82008-05-02 13:42:44 -07004287 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004288 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004289 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004290 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004291 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004292 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004293 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004294 return status;
4295}
4296
Andy Adamson557134a2009-04-01 09:21:53 -04004297#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004298/*
4299 * nfs4_proc_exchange_id()
4300 *
4301 * Since the clientid has expired, all compounds using sessions
4302 * associated with the stale clientid will be returning
4303 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4304 * be in some phase of session reset.
4305 */
4306static int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4307{
4308 nfs4_verifier verifier;
4309 struct nfs41_exchange_id_args args = {
4310 .client = clp,
4311 .flags = clp->cl_exchange_flags,
4312 };
4313 struct nfs41_exchange_id_res res = {
4314 .client = clp,
4315 };
4316 int status;
4317 struct rpc_message msg = {
4318 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4319 .rpc_argp = &args,
4320 .rpc_resp = &res,
4321 .rpc_cred = cred,
4322 };
4323 __be32 *p;
4324
4325 dprintk("--> %s\n", __func__);
4326 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004327
Benny Halevy99fe60d2009-04-01 09:22:29 -04004328 p = (u32 *)verifier.data;
4329 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4330 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4331 args.verifier = &verifier;
4332
4333 while (1) {
4334 args.id_len = scnprintf(args.id, sizeof(args.id),
4335 "%s/%s %u",
4336 clp->cl_ipaddr,
4337 rpc_peeraddr2str(clp->cl_rpcclient,
4338 RPC_DISPLAY_ADDR),
4339 clp->cl_id_uniquifier);
4340
4341 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4342
4343 if (status != NFS4ERR_CLID_INUSE)
4344 break;
4345
4346 if (signalled())
4347 break;
4348
4349 if (++clp->cl_id_uniquifier == 0)
4350 break;
4351 }
4352
4353 dprintk("<-- %s status= %d\n", __func__, status);
4354 return status;
4355}
4356
Andy Adamson2050f0c2009-04-01 09:22:30 -04004357struct nfs4_get_lease_time_data {
4358 struct nfs4_get_lease_time_args *args;
4359 struct nfs4_get_lease_time_res *res;
4360 struct nfs_client *clp;
4361};
4362
4363static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4364 void *calldata)
4365{
4366 int ret;
4367 struct nfs4_get_lease_time_data *data =
4368 (struct nfs4_get_lease_time_data *)calldata;
4369
4370 dprintk("--> %s\n", __func__);
4371 /* just setup sequence, do not trigger session recovery
4372 since we're invoked within one */
4373 ret = nfs41_setup_sequence(data->clp->cl_session,
4374 &data->args->la_seq_args,
4375 &data->res->lr_seq_res, 0, task);
4376
4377 BUG_ON(ret == -EAGAIN);
4378 rpc_call_start(task);
4379 dprintk("<-- %s\n", __func__);
4380}
4381
4382/*
4383 * Called from nfs4_state_manager thread for session setup, so don't recover
4384 * from sequence operation or clientid errors.
4385 */
4386static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4387{
4388 struct nfs4_get_lease_time_data *data =
4389 (struct nfs4_get_lease_time_data *)calldata;
4390
4391 dprintk("--> %s\n", __func__);
4392 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4393 switch (task->tk_status) {
4394 case -NFS4ERR_DELAY:
4395 case -NFS4ERR_GRACE:
4396 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4397 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4398 task->tk_status = 0;
Andy Adamsoneedc0202009-04-01 09:22:41 -04004399 nfs4_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004400 return;
4401 }
4402 nfs41_sequence_free_slot(data->clp, &data->res->lr_seq_res);
4403 dprintk("<-- %s\n", __func__);
4404}
4405
4406struct rpc_call_ops nfs4_get_lease_time_ops = {
4407 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4408 .rpc_call_done = nfs4_get_lease_time_done,
4409};
4410
4411int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4412{
4413 struct rpc_task *task;
4414 struct nfs4_get_lease_time_args args;
4415 struct nfs4_get_lease_time_res res = {
4416 .lr_fsinfo = fsinfo,
4417 };
4418 struct nfs4_get_lease_time_data data = {
4419 .args = &args,
4420 .res = &res,
4421 .clp = clp,
4422 };
4423 struct rpc_message msg = {
4424 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4425 .rpc_argp = &args,
4426 .rpc_resp = &res,
4427 };
4428 struct rpc_task_setup task_setup = {
4429 .rpc_client = clp->cl_rpcclient,
4430 .rpc_message = &msg,
4431 .callback_ops = &nfs4_get_lease_time_ops,
4432 .callback_data = &data
4433 };
4434 int status;
4435
4436 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4437 dprintk("--> %s\n", __func__);
4438 task = rpc_run_task(&task_setup);
4439
4440 if (IS_ERR(task))
4441 status = PTR_ERR(task);
4442 else {
4443 status = task->tk_status;
4444 rpc_put_task(task);
4445 }
4446 dprintk("<-- %s return %d\n", __func__, status);
4447
4448 return status;
4449}
4450
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004451/*
4452 * Reset a slot table
4453 */
4454static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots,
4455 int old_max_slots, int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004456{
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004457 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004458 int ret = 0;
4459
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004460 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004461
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004462 /*
4463 * Until we have dynamic slot table adjustment, insist
4464 * upon the same slot table size
4465 */
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004466 if (max_slots != old_max_slots) {
4467 dprintk("%s reset slot table does't match old\n",
4468 __func__);
4469 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4470 goto out;
4471 }
4472 spin_lock(&tbl->slot_tbl_lock);
4473 for (i = 0; i < max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004474 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004475 tbl->highest_used_slotid = -1;
4476 spin_unlock(&tbl->slot_tbl_lock);
4477 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4478 tbl, tbl->slots, tbl->max_slots);
4479out:
4480 dprintk("<-- %s: return %d\n", __func__, ret);
4481 return ret;
4482}
4483
Andy Adamsonfc931582009-04-01 09:22:31 -04004484/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004485 * Reset the forechannel and backchannel slot tables
4486 */
4487static int nfs4_reset_slot_tables(struct nfs4_session *session)
4488{
4489 int status;
4490
4491 status = nfs4_reset_slot_table(&session->fc_slot_table,
4492 session->fc_attrs.max_reqs,
4493 session->fc_slot_table.max_slots,
4494 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004495 if (status)
4496 return status;
4497
4498 status = nfs4_reset_slot_table(&session->bc_slot_table,
4499 session->bc_attrs.max_reqs,
4500 session->bc_slot_table.max_slots,
4501 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004502 return status;
4503}
4504
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004505/* Destroy the slot table */
4506static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4507{
4508 if (session->fc_slot_table.slots != NULL) {
4509 kfree(session->fc_slot_table.slots);
4510 session->fc_slot_table.slots = NULL;
4511 }
4512 if (session->bc_slot_table.slots != NULL) {
4513 kfree(session->bc_slot_table.slots);
4514 session->bc_slot_table.slots = NULL;
4515 }
4516 return;
4517}
4518
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004519/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004520 * Initialize slot table
4521 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004522static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4523 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004524{
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004525 int i;
Andy Adamsonfc931582009-04-01 09:22:31 -04004526 struct nfs4_slot *slot;
4527 int ret = -ENOMEM;
4528
4529 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4530
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004531 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004532
4533 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4534 if (!slot)
4535 goto out;
4536 for (i = 0; i < max_slots; ++i)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004537 slot[i].seq_nr = ivalue;
Andy Adamsonfc931582009-04-01 09:22:31 -04004538 ret = 0;
4539
4540 spin_lock(&tbl->slot_tbl_lock);
4541 if (tbl->slots != NULL) {
4542 spin_unlock(&tbl->slot_tbl_lock);
4543 dprintk("%s: slot table already initialized. tbl=%p slots=%p\n",
4544 __func__, tbl, tbl->slots);
4545 WARN_ON(1);
4546 goto out_free;
4547 }
4548 tbl->max_slots = max_slots;
4549 tbl->slots = slot;
4550 tbl->highest_used_slotid = -1; /* no slot is currently used */
4551 spin_unlock(&tbl->slot_tbl_lock);
4552 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4553 tbl, tbl->slots, tbl->max_slots);
4554out:
4555 dprintk("<-- %s: return %d\n", __func__, ret);
4556 return ret;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004557
Andy Adamsonfc931582009-04-01 09:22:31 -04004558out_free:
4559 kfree(slot);
4560 goto out;
4561}
4562
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004563/*
4564 * Initialize the forechannel and backchannel tables
4565 */
4566static int nfs4_init_slot_tables(struct nfs4_session *session)
4567{
4568 int status;
4569
4570 status = nfs4_init_slot_table(&session->fc_slot_table,
4571 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004572 if (status)
4573 return status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004574
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004575 status = nfs4_init_slot_table(&session->bc_slot_table,
4576 session->bc_attrs.max_reqs, 0);
4577 if (status)
4578 nfs4_destroy_slot_tables(session);
4579
4580 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004581}
4582
4583struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4584{
4585 struct nfs4_session *session;
4586 struct nfs4_slot_table *tbl;
4587
4588 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4589 if (!session)
4590 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004591
4592 set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
4593 /*
4594 * The create session reply races with the server back
4595 * channel probe. Mark the client NFS_CS_SESSION_INITING
4596 * so that the client back channel can find the
4597 * nfs_client struct
4598 */
4599 clp->cl_cons_state = NFS_CS_SESSION_INITING;
4600
Andy Adamson557134a2009-04-01 09:21:53 -04004601 tbl = &session->fc_slot_table;
4602 spin_lock_init(&tbl->slot_tbl_lock);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004603 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4604
4605 tbl = &session->bc_slot_table;
4606 spin_lock_init(&tbl->slot_tbl_lock);
4607 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4608
Andy Adamson557134a2009-04-01 09:21:53 -04004609 session->clp = clp;
4610 return session;
4611}
4612
4613void nfs4_destroy_session(struct nfs4_session *session)
4614{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004615 nfs4_proc_destroy_session(session);
4616 dprintk("%s Destroy backchannel for xprt %p\n",
4617 __func__, session->clp->cl_rpcclient->cl_xprt);
4618 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4619 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004620 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004621 kfree(session);
4622}
4623
Andy Adamsonfc931582009-04-01 09:22:31 -04004624/*
4625 * Initialize the values to be used by the client in CREATE_SESSION
4626 * If nfs4_init_session set the fore channel request and response sizes,
4627 * use them.
4628 *
4629 * Set the back channel max_resp_sz_cached to zero to force the client to
4630 * always set csa_cachethis to FALSE because the current implementation
4631 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4632 */
4633static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4634{
4635 struct nfs4_session *session = args->client->cl_session;
4636 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4637 mxresp_sz = session->fc_attrs.max_resp_sz;
4638
4639 if (mxrqst_sz == 0)
4640 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4641 if (mxresp_sz == 0)
4642 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4643 /* Fore channel attributes */
4644 args->fc_attrs.headerpadsz = 0;
4645 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4646 args->fc_attrs.max_resp_sz = mxresp_sz;
4647 args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4648 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4649 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4650
4651 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4652 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4653 __func__,
4654 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4655 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4656 args->fc_attrs.max_reqs);
4657
4658 /* Back channel attributes */
4659 args->bc_attrs.headerpadsz = 0;
4660 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4661 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4662 args->bc_attrs.max_resp_sz_cached = 0;
4663 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4664 args->bc_attrs.max_reqs = 1;
4665
4666 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4667 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4668 __func__,
4669 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4670 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4671 args->bc_attrs.max_reqs);
4672}
4673
Andy Adamson8d353012009-04-01 09:22:32 -04004674static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4675{
4676 if (rcvd <= sent)
4677 return 0;
4678 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4679 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4680 return -EINVAL;
4681}
4682
4683#define _verify_fore_channel_attr(_name_) \
4684 _verify_channel_attr("fore", #_name_, \
4685 args->fc_attrs._name_, \
4686 session->fc_attrs._name_)
4687
4688#define _verify_back_channel_attr(_name_) \
4689 _verify_channel_attr("back", #_name_, \
4690 args->bc_attrs._name_, \
4691 session->bc_attrs._name_)
4692
4693/*
4694 * The server is not allowed to increase the fore channel header pad size,
4695 * maximum response size, or maximum number of operations.
4696 *
4697 * The back channel attributes are only negotiatied down: We send what the
4698 * (back channel) server insists upon.
4699 */
4700static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4701 struct nfs4_session *session)
4702{
4703 int ret = 0;
4704
4705 ret |= _verify_fore_channel_attr(headerpadsz);
4706 ret |= _verify_fore_channel_attr(max_resp_sz);
4707 ret |= _verify_fore_channel_attr(max_ops);
4708
4709 ret |= _verify_back_channel_attr(headerpadsz);
4710 ret |= _verify_back_channel_attr(max_rqst_sz);
4711 ret |= _verify_back_channel_attr(max_resp_sz);
4712 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4713 ret |= _verify_back_channel_attr(max_ops);
4714 ret |= _verify_back_channel_attr(max_reqs);
4715
4716 return ret;
4717}
4718
Andy Adamsonfc931582009-04-01 09:22:31 -04004719static int _nfs4_proc_create_session(struct nfs_client *clp)
4720{
4721 struct nfs4_session *session = clp->cl_session;
4722 struct nfs41_create_session_args args = {
4723 .client = clp,
4724 .cb_program = NFS4_CALLBACK,
4725 };
4726 struct nfs41_create_session_res res = {
4727 .client = clp,
4728 };
4729 struct rpc_message msg = {
4730 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4731 .rpc_argp = &args,
4732 .rpc_resp = &res,
4733 };
4734 int status;
4735
4736 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004737 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004738
4739 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4740
Andy Adamson8d353012009-04-01 09:22:32 -04004741 if (!status)
4742 /* Verify the session's negotiated channel_attrs values */
4743 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004744 if (!status) {
4745 /* Increment the clientid slot sequence id */
4746 clp->cl_seqid++;
4747 }
4748
4749 return status;
4750}
4751
4752/*
4753 * Issues a CREATE_SESSION operation to the server.
4754 * It is the responsibility of the caller to verify the session is
4755 * expired before calling this routine.
4756 */
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004757int nfs4_proc_create_session(struct nfs_client *clp, int reset)
Andy Adamsonfc931582009-04-01 09:22:31 -04004758{
4759 int status;
4760 unsigned *ptr;
4761 struct nfs_fsinfo fsinfo;
4762 struct nfs4_session *session = clp->cl_session;
4763
4764 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4765
4766 status = _nfs4_proc_create_session(clp);
4767 if (status)
4768 goto out;
4769
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004770 /* Init or reset the fore channel */
4771 if (reset)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004772 status = nfs4_reset_slot_tables(session);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004773 else
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004774 status = nfs4_init_slot_tables(session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004775 dprintk("fore channel slot table initialization returned %d\n", status);
4776 if (status)
4777 goto out;
4778
4779 ptr = (unsigned *)&session->sess_id.data[0];
4780 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4781 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
4782
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004783 if (reset)
4784 /* Lease time is aleady set */
4785 goto out;
4786
Andy Adamsonfc931582009-04-01 09:22:31 -04004787 /* Get the lease time */
4788 status = nfs4_proc_get_lease_time(clp, &fsinfo);
4789 if (status == 0) {
4790 /* Update lease time and schedule renewal */
4791 spin_lock(&clp->cl_lock);
4792 clp->cl_lease_time = fsinfo.lease_time * HZ;
4793 clp->cl_last_renewal = jiffies;
4794 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
4795 spin_unlock(&clp->cl_lock);
4796
4797 nfs4_schedule_state_renewal(clp);
4798 }
4799out:
4800 dprintk("<-- %s\n", __func__);
4801 return status;
4802}
4803
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004804/*
4805 * Issue the over-the-wire RPC DESTROY_SESSION.
4806 * The caller must serialize access to this routine.
4807 */
4808int nfs4_proc_destroy_session(struct nfs4_session *session)
4809{
4810 int status = 0;
4811 struct rpc_message msg;
4812
4813 dprintk("--> nfs4_proc_destroy_session\n");
4814
4815 /* session is still being setup */
4816 if (session->clp->cl_cons_state != NFS_CS_READY)
4817 return status;
4818
4819 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4820 msg.rpc_argp = session;
4821 msg.rpc_resp = NULL;
4822 msg.rpc_cred = NULL;
4823 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4824
4825 if (status)
4826 printk(KERN_WARNING
4827 "Got error %d from the server on DESTROY_SESSION. "
4828 "Session has been destroyed regardless...\n", status);
4829
4830 dprintk("<-- nfs4_proc_destroy_session\n");
4831 return status;
4832}
4833
Trond Myklebustfccba802009-07-21 16:48:07 -04004834int nfs4_init_session(struct nfs_server *server)
4835{
4836 struct nfs_client *clp = server->nfs_client;
4837 int ret;
4838
4839 if (!nfs4_has_session(clp))
4840 return 0;
4841
4842 clp->cl_session->fc_attrs.max_rqst_sz = server->wsize;
4843 clp->cl_session->fc_attrs.max_resp_sz = server->rsize;
4844 ret = nfs4_recover_expired_lease(server);
4845 if (!ret)
4846 ret = nfs4_check_client_ready(clp);
4847 return ret;
4848}
4849
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004850/*
4851 * Renew the cl_session lease.
4852 */
4853static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
4854{
4855 struct nfs4_sequence_args args;
4856 struct nfs4_sequence_res res;
4857
4858 struct rpc_message msg = {
4859 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4860 .rpc_argp = &args,
4861 .rpc_resp = &res,
4862 .rpc_cred = cred,
4863 };
4864
4865 args.sa_cache_this = 0;
4866
4867 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
4868 &res, 0);
4869}
4870
4871void nfs41_sequence_call_done(struct rpc_task *task, void *data)
4872{
4873 struct nfs_client *clp = (struct nfs_client *)data;
4874
4875 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
4876
4877 if (task->tk_status < 0) {
4878 dprintk("%s ERROR %d\n", __func__, task->tk_status);
4879
4880 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
4881 == -EAGAIN) {
Andy Adamsoneedc0202009-04-01 09:22:41 -04004882 nfs4_restart_rpc(task, clp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004883 return;
4884 }
4885 }
4886 nfs41_sequence_free_slot(clp, task->tk_msg.rpc_resp);
4887 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
4888
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004889 kfree(task->tk_msg.rpc_argp);
4890 kfree(task->tk_msg.rpc_resp);
4891
4892 dprintk("<-- %s\n", __func__);
4893}
4894
4895static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
4896{
4897 struct nfs_client *clp;
4898 struct nfs4_sequence_args *args;
4899 struct nfs4_sequence_res *res;
4900
4901 clp = (struct nfs_client *)data;
4902 args = task->tk_msg.rpc_argp;
4903 res = task->tk_msg.rpc_resp;
4904
4905 if (nfs4_setup_sequence(clp, args, res, 0, task))
4906 return;
4907 rpc_call_start(task);
4908}
4909
4910static const struct rpc_call_ops nfs41_sequence_ops = {
4911 .rpc_call_done = nfs41_sequence_call_done,
4912 .rpc_call_prepare = nfs41_sequence_prepare,
4913};
4914
4915static int nfs41_proc_async_sequence(struct nfs_client *clp,
4916 struct rpc_cred *cred)
4917{
4918 struct nfs4_sequence_args *args;
4919 struct nfs4_sequence_res *res;
4920 struct rpc_message msg = {
4921 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4922 .rpc_cred = cred,
4923 };
4924
4925 args = kzalloc(sizeof(*args), GFP_KERNEL);
4926 if (!args)
4927 return -ENOMEM;
4928 res = kzalloc(sizeof(*res), GFP_KERNEL);
4929 if (!res) {
4930 kfree(args);
4931 return -ENOMEM;
4932 }
4933 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
4934 msg.rpc_argp = args;
4935 msg.rpc_resp = res;
4936
4937 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
4938 &nfs41_sequence_ops, (void *)clp);
4939}
4940
Andy Adamson557134a2009-04-01 09:21:53 -04004941#endif /* CONFIG_NFS_V4_1 */
4942
Andy Adamson591d71c2009-04-01 09:22:47 -04004943struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05004944 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004945 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 .recover_open = nfs4_open_reclaim,
4947 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04004948 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04004949 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950};
4951
Andy Adamson591d71c2009-04-01 09:22:47 -04004952#if defined(CONFIG_NFS_V4_1)
4953struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
4954 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
4955 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
4956 .recover_open = nfs4_open_reclaim,
4957 .recover_lock = nfs4_lock_reclaim,
4958 .establish_clid = nfs4_proc_exchange_id,
Andy Adamsonb4b82602009-04-01 09:22:49 -04004959 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04004960};
4961#endif /* CONFIG_NFS_V4_1 */
4962
4963struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05004964 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004965 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 .recover_open = nfs4_open_expired,
4967 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04004968 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04004969 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970};
4971
Andy Adamson591d71c2009-04-01 09:22:47 -04004972#if defined(CONFIG_NFS_V4_1)
4973struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
4974 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
4975 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
4976 .recover_open = nfs4_open_expired,
4977 .recover_lock = nfs4_lock_expired,
4978 .establish_clid = nfs4_proc_exchange_id,
Andy Adamsonb4b82602009-04-01 09:22:49 -04004979 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04004980};
4981#endif /* CONFIG_NFS_V4_1 */
4982
Benny Halevy29fba382009-04-01 09:22:44 -04004983struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
4984 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04004985 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04004986 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04004987};
4988
4989#if defined(CONFIG_NFS_V4_1)
4990struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
4991 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04004992 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04004993 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04004994};
4995#endif
4996
4997/*
4998 * Per minor version reboot and network partition recovery ops
4999 */
5000
Andy Adamson591d71c2009-04-01 09:22:47 -04005001struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5002 &nfs40_reboot_recovery_ops,
5003#if defined(CONFIG_NFS_V4_1)
5004 &nfs41_reboot_recovery_ops,
5005#endif
5006};
5007
5008struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5009 &nfs40_nograce_recovery_ops,
5010#if defined(CONFIG_NFS_V4_1)
5011 &nfs41_nograce_recovery_ops,
5012#endif
5013};
5014
Benny Halevy29fba382009-04-01 09:22:44 -04005015struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5016 &nfs40_state_renewal_ops,
5017#if defined(CONFIG_NFS_V4_1)
5018 &nfs41_state_renewal_ops,
5019#endif
5020};
5021
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005022static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005023 .permission = nfs_permission,
5024 .getattr = nfs_getattr,
5025 .setattr = nfs_setattr,
5026 .getxattr = nfs4_getxattr,
5027 .setxattr = nfs4_setxattr,
5028 .listxattr = nfs4_listxattr,
5029};
5030
David Howells509de812006-08-22 20:06:11 -04005031const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 .version = 4, /* protocol version */
5033 .dentry_ops = &nfs4_dentry_operations,
5034 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005035 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 .getroot = nfs4_proc_get_root,
5037 .getattr = nfs4_proc_getattr,
5038 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005039 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 .lookup = nfs4_proc_lookup,
5041 .access = nfs4_proc_access,
5042 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 .create = nfs4_proc_create,
5044 .remove = nfs4_proc_remove,
5045 .unlink_setup = nfs4_proc_unlink_setup,
5046 .unlink_done = nfs4_proc_unlink_done,
5047 .rename = nfs4_proc_rename,
5048 .link = nfs4_proc_link,
5049 .symlink = nfs4_proc_symlink,
5050 .mkdir = nfs4_proc_mkdir,
5051 .rmdir = nfs4_proc_remove,
5052 .readdir = nfs4_proc_readdir,
5053 .mknod = nfs4_proc_mknod,
5054 .statfs = nfs4_proc_statfs,
5055 .fsinfo = nfs4_proc_fsinfo,
5056 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005057 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 .decode_dirent = nfs4_decode_dirent,
5059 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005060 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005062 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005064 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005066 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005067 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068};
5069
5070/*
5071 * Local variables:
5072 * c-basic-offset: 8
5073 * End:
5074 */