blob: a3549ce72ab24572d2fafffe3f61f63464c9f024 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/sunrpc/clnt.h>
44#include <linux/nfs.h>
45#include <linux/nfs4.h>
46#include <linux/nfs_fs.h>
47#include <linux/nfs_page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070049#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040050#include <linux/module.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040051#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000052#include <linux/xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Trond Myklebust4ce79712005-06-22 17:16:21 +000054#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050056#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050057#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040058#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040059#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61#define NFSDBG_FACILITY NFSDBG_PROC
62
Trond Myklebust2066fe82006-09-15 08:30:46 -040063#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define NFS4_POLL_RETRY_MAX (15*HZ)
65
Trond Myklebusta78cb572009-08-09 15:06:19 -040066#define NFS4_MAX_LOOP_ON_RECOVER (10)
67
Trond Myklebustcdd4e682006-01-03 09:55:12 +010068struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010069static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080070static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050072static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040073static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
74static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040075static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
76 struct nfs_fattr *fattr, struct iattr *sattr,
77 struct nfs4_state *state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050080static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Trond Myklebust52567b02009-10-23 14:46:42 -040082 if (err >= -1000)
83 return err;
84 switch (err) {
85 case -NFS4ERR_RESOURCE:
86 return -EREMOTEIO;
87 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070089 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040090 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
Trond Myklebust52567b02009-10-23 14:46:42 -040092 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95/*
96 * This is our standard bitmap for GETATTR requests.
97 */
98const u32 nfs4_fattr_bitmap[2] = {
99 FATTR4_WORD0_TYPE
100 | FATTR4_WORD0_CHANGE
101 | FATTR4_WORD0_SIZE
102 | FATTR4_WORD0_FSID
103 | FATTR4_WORD0_FILEID,
104 FATTR4_WORD1_MODE
105 | FATTR4_WORD1_NUMLINKS
106 | FATTR4_WORD1_OWNER
107 | FATTR4_WORD1_OWNER_GROUP
108 | FATTR4_WORD1_RAWDEV
109 | FATTR4_WORD1_SPACE_USED
110 | FATTR4_WORD1_TIME_ACCESS
111 | FATTR4_WORD1_TIME_METADATA
112 | FATTR4_WORD1_TIME_MODIFY
113};
114
115const u32 nfs4_statfs_bitmap[2] = {
116 FATTR4_WORD0_FILES_AVAIL
117 | FATTR4_WORD0_FILES_FREE
118 | FATTR4_WORD0_FILES_TOTAL,
119 FATTR4_WORD1_SPACE_AVAIL
120 | FATTR4_WORD1_SPACE_FREE
121 | FATTR4_WORD1_SPACE_TOTAL
122};
123
Trond Myklebust4ce79712005-06-22 17:16:21 +0000124const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 FATTR4_WORD0_MAXLINK
126 | FATTR4_WORD0_MAXNAME,
127 0
128};
129
130const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
131 | FATTR4_WORD0_MAXREAD
132 | FATTR4_WORD0_MAXWRITE
133 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700134 FATTR4_WORD1_TIME_DELTA
Andy Adamson504913f2010-10-20 00:17:57 -0400135 | FATTR4_WORD1_FS_LAYOUT_TYPES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
Manoj Naik830b8e32006-06-09 09:34:25 -0400138const u32 nfs4_fs_locations_bitmap[2] = {
139 FATTR4_WORD0_TYPE
140 | FATTR4_WORD0_CHANGE
141 | FATTR4_WORD0_SIZE
142 | FATTR4_WORD0_FSID
143 | FATTR4_WORD0_FILEID
144 | FATTR4_WORD0_FS_LOCATIONS,
145 FATTR4_WORD1_MODE
146 | FATTR4_WORD1_NUMLINKS
147 | FATTR4_WORD1_OWNER
148 | FATTR4_WORD1_OWNER_GROUP
149 | FATTR4_WORD1_RAWDEV
150 | FATTR4_WORD1_SPACE_USED
151 | FATTR4_WORD1_TIME_ACCESS
152 | FATTR4_WORD1_TIME_METADATA
153 | FATTR4_WORD1_TIME_MODIFY
154 | FATTR4_WORD1_MOUNTED_ON_FILEID
155};
156
Al Virobc4785c2006-10-19 23:28:51 -0700157static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 struct nfs4_readdir_arg *readdir)
159{
Al Viro0dbb4c62006-10-19 23:28:49 -0700160 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 BUG_ON(readdir->count < 80);
163 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000164 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
166 return;
167 }
168
169 readdir->cookie = 0;
170 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
171 if (cookie == 2)
172 return;
173
174 /*
175 * NFSv4 servers do not return entries for '.' and '..'
176 * Therefore, we fake these entries here. We let '.'
177 * have cookie 0 and '..' have cookie 1. Note that
178 * when talking to the server, we always send cookie 0
179 * instead of 1 or 2.
180 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700181 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 if (cookie == 0) {
184 *p++ = xdr_one; /* next */
185 *p++ = xdr_zero; /* cookie, first word */
186 *p++ = xdr_one; /* cookie, second word */
187 *p++ = xdr_one; /* entry len */
188 memcpy(p, ".\0\0\0", 4); /* entry */
189 p++;
190 *p++ = xdr_one; /* bitmap length */
191 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
192 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400193 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195
196 *p++ = xdr_one; /* next */
197 *p++ = xdr_zero; /* cookie, first word */
198 *p++ = xdr_two; /* cookie, second word */
199 *p++ = xdr_two; /* entry len */
200 memcpy(p, "..\0\0", 4); /* entry */
201 p++;
202 *p++ = xdr_one; /* bitmap length */
203 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
204 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400205 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 readdir->pgbase = (char *)p - (char *)start;
208 readdir->count -= readdir->pgbase;
209 kunmap_atomic(start, KM_USER0);
210}
211
Trond Myklebust65de8722008-12-23 15:21:44 -0500212static int nfs4_wait_clnt_recover(struct nfs_client *clp)
213{
214 int res;
215
216 might_sleep();
217
Trond Myklebuste005e802008-12-23 15:21:48 -0500218 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400219 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500220 return res;
221}
222
223static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
224{
225 int res = 0;
226
227 might_sleep();
228
229 if (*timeout <= 0)
230 *timeout = NFS4_POLL_RETRY_MIN;
231 if (*timeout > NFS4_POLL_RETRY_MAX)
232 *timeout = NFS4_POLL_RETRY_MAX;
233 schedule_timeout_killable(*timeout);
234 if (fatal_signal_pending(current))
235 res = -ERESTARTSYS;
236 *timeout <<= 1;
237 return res;
238}
239
240/* This is the error handling routine for processes that are allowed
241 * to sleep.
242 */
243static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
244{
245 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500246 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500247 int ret = errorcode;
248
249 exception->retry = 0;
250 switch(errorcode) {
251 case 0:
252 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500253 case -NFS4ERR_ADMIN_REVOKED:
254 case -NFS4ERR_BAD_STATEID:
255 case -NFS4ERR_OPENMODE:
256 if (state == NULL)
257 break;
258 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500259 goto do_state_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500260 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500261 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500262 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500263 goto do_state_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500264#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400265 case -NFS4ERR_BADSESSION:
266 case -NFS4ERR_BADSLOT:
267 case -NFS4ERR_BAD_HIGH_SLOT:
268 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
269 case -NFS4ERR_DEADSESSION:
270 case -NFS4ERR_SEQ_FALSE_RETRY:
271 case -NFS4ERR_SEQ_MISORDERED:
272 dprintk("%s ERROR: %d Reset session\n", __func__,
273 errorcode);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500274 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -0400275 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500276 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500277#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500278 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500279 if (exception->timeout > HZ) {
280 /* We have retried a decent amount, time to
281 * fail
282 */
283 ret = -EBUSY;
284 break;
285 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500286 case -NFS4ERR_GRACE:
287 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500288 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500289 ret = nfs4_delay(server->client, &exception->timeout);
290 if (ret != 0)
291 break;
292 case -NFS4ERR_OLD_STATEID:
293 exception->retry = 1;
294 }
295 /* We failed to handle the error */
296 return nfs4_map_errors(ret);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500297do_state_recovery:
298 nfs4_schedule_state_recovery(clp);
299 ret = nfs4_wait_clnt_recover(clp);
300 if (ret == 0)
301 exception->retry = 1;
302 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500303}
304
305
Trond Myklebust452e9352010-07-31 14:29:06 -0400306static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 spin_lock(&clp->cl_lock);
309 if (time_before(clp->cl_last_renewal,timestamp))
310 clp->cl_last_renewal = timestamp;
311 spin_unlock(&clp->cl_lock);
312}
313
Trond Myklebust452e9352010-07-31 14:29:06 -0400314static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
315{
316 do_renew_lease(server->nfs_client, timestamp);
317}
318
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400319#if defined(CONFIG_NFS_V4_1)
320
Benny Halevy510b8172009-04-01 09:22:14 -0400321/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400322 * nfs4_free_slot - free a slot and efficiently update slot table.
323 *
324 * freeing a slot is trivially done by clearing its respective bit
325 * in the bitmap.
326 * If the freed slotid equals highest_used_slotid we want to update it
327 * so that the server would be able to size down the slot table if needed,
328 * otherwise we know that the highest_used_slotid is still in use.
329 * When updating highest_used_slotid there may be "holes" in the bitmap
330 * so we need to scan down from highest_used_slotid to 0 looking for the now
331 * highest slotid in use.
332 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500333 *
334 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400335 */
336static void
Benny Halevydfb4f3092010-09-24 09:17:01 -0400337nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400338{
Benny Halevydfb4f3092010-09-24 09:17:01 -0400339 int free_slotid = free_slot - tbl->slots;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400340 int slotid = free_slotid;
341
Benny Halevydfb4f3092010-09-24 09:17:01 -0400342 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400343 /* clear used bit in bitmap */
344 __clear_bit(slotid, tbl->used_slots);
345
346 /* update highest_used_slotid when it is freed */
347 if (slotid == tbl->highest_used_slotid) {
348 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500349 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400350 tbl->highest_used_slotid = slotid;
351 else
352 tbl->highest_used_slotid = -1;
353 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400354 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
355 free_slotid, tbl->highest_used_slotid);
356}
357
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800358/*
Andy Adamson42acd022011-01-06 02:04:34 +0000359 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800360 */
Andy Adamson42acd022011-01-06 02:04:34 +0000361static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800362{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800363 struct rpc_task *task;
364
Trond Myklebusta2118c32010-06-16 09:52:26 -0400365 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800366 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
367 if (task)
368 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800369 return;
370 }
371
372 if (ses->fc_slot_table.highest_used_slotid != -1)
373 return;
374
Andy Adamson42acd022011-01-06 02:04:34 +0000375 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
376 complete(&ses->fc_slot_table.complete);
377}
378
379/*
380 * Signal state manager thread if session back channel is drained
381 */
382void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
383{
384 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
385 ses->bc_slot_table.highest_used_slotid != -1)
386 return;
387 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
388 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800389}
390
Trond Myklebustd185a332010-06-16 09:52:25 -0400391static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400392{
393 struct nfs4_slot_table *tbl;
394
Trond Myklebustd185a332010-06-16 09:52:25 -0400395 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400396 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400397 /* just wake up the next guy waiting since
398 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500399 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500400 return;
Andy Adamson13615872009-04-01 09:22:17 -0400401 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500402
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500403 spin_lock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400404 nfs4_free_slot(tbl, res->sr_slot);
Andy Adamson42acd022011-01-06 02:04:34 +0000405 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500406 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400407 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400408}
409
Trond Myklebust14516c32010-07-31 14:29:06 -0400410static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400411{
412 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400413 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400414
415 /*
416 * sr_status remains 1 if an RPC level error occurred. The server
417 * may or may not have processed the sequence operation..
418 * Proceed as if the server received and processed the sequence
419 * operation.
420 */
421 if (res->sr_status == 1)
422 res->sr_status = NFS_OK;
423
424 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400425 if (!res->sr_slot)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400426 goto out;
427
Andy Adamson691daf32009-12-04 15:55:39 -0500428 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400429 switch (res->sr_status) {
430 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400431 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400432 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400433 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400434 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400435 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500436 /* Check sequence flags */
Alexandros Batsakis71358402010-02-05 03:45:05 -0800437 if (atomic_read(&clp->cl_count) > 1)
438 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust14516c32010-07-31 14:29:06 -0400439 break;
440 case -NFS4ERR_DELAY:
441 /* The server detected a resend of the RPC call and
442 * returned NFS4ERR_DELAY as per Section 2.10.6.2
443 * of RFC5661.
444 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200445 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400446 __func__,
447 res->sr_slot - res->sr_session->fc_slot_table.slots,
448 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400449 goto out_retry;
450 default:
451 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400452 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400453 }
454out:
455 /* The session may be reset by one of the error handlers. */
456 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400457 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400458 return 1;
459out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400460 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400461 goto out;
462 rpc_delay(task, NFS4_POLL_RETRY_MAX);
463 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400464}
465
Trond Myklebust14516c32010-07-31 14:29:06 -0400466static int nfs4_sequence_done(struct rpc_task *task,
467 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400468{
Trond Myklebust14516c32010-07-31 14:29:06 -0400469 if (res->sr_session == NULL)
470 return 1;
471 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400472}
473
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400474/*
Benny Halevy510b8172009-04-01 09:22:14 -0400475 * nfs4_find_slot - efficiently look for a free slot
476 *
477 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
478 * If found, we mark the slot as used, update the highest_used_slotid,
479 * and respectively set up the sequence operation args.
480 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400481 *
482 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400483 */
484static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800485nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400486{
487 int slotid;
488 u8 ret_id = NFS4_MAX_SLOT_TABLE;
489 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
490
Benny Halevy510b8172009-04-01 09:22:14 -0400491 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
492 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
493 tbl->max_slots);
494 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
495 if (slotid >= tbl->max_slots)
496 goto out;
497 __set_bit(slotid, tbl->used_slots);
498 if (slotid > tbl->highest_used_slotid)
499 tbl->highest_used_slotid = slotid;
500 ret_id = slotid;
501out:
502 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
503 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400504 return ret_id;
505}
506
Andy Adamsonce5039c2009-04-01 09:22:13 -0400507static int nfs41_setup_sequence(struct nfs4_session *session,
508 struct nfs4_sequence_args *args,
509 struct nfs4_sequence_res *res,
510 int cache_reply,
511 struct rpc_task *task)
512{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400513 struct nfs4_slot *slot;
514 struct nfs4_slot_table *tbl;
515 u8 slotid;
516
517 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400518 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400519 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400520 return 0;
521
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400522 tbl = &session->fc_slot_table;
523
524 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400525 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800526 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500527 /*
528 * The state manager will wait until the slot table is empty.
529 * Schedule the reset thread
530 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500531 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400532 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500533 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500534 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400535 }
536
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800537 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
538 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
539 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
540 spin_unlock(&tbl->slot_tbl_lock);
541 dprintk("%s enforce FIFO order\n", __func__);
542 return -EAGAIN;
543 }
544
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800545 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400546 if (slotid == NFS4_MAX_SLOT_TABLE) {
547 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
548 spin_unlock(&tbl->slot_tbl_lock);
549 dprintk("<-- %s: no free slots\n", __func__);
550 return -EAGAIN;
551 }
552 spin_unlock(&tbl->slot_tbl_lock);
553
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800554 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400555 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400556 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400557 args->sa_slotid = slotid;
558 args->sa_cache_this = cache_reply;
559
560 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
561
Benny Halevy99fe60d2009-04-01 09:22:29 -0400562 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400563 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400564 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400565 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400566 /*
567 * sr_status is only set in decode_sequence, and so will remain
568 * set to 1 if an rpc level failure occurs.
569 */
570 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400571 return 0;
572}
573
Trond Myklebust035168a2010-06-16 09:52:26 -0400574int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400575 struct nfs4_sequence_args *args,
576 struct nfs4_sequence_res *res,
577 int cache_reply,
578 struct rpc_task *task)
579{
Trond Myklebust035168a2010-06-16 09:52:26 -0400580 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400581 int ret = 0;
582
Trond Myklebust035168a2010-06-16 09:52:26 -0400583 if (session == NULL) {
584 args->sa_session = NULL;
585 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400586 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400587 }
588
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200589 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400590 __func__, session->clp, session, res->sr_slot ?
591 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400592
593 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400594 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400595out:
596 dprintk("<-- %s status=%d\n", __func__, ret);
597 return ret;
598}
599
600struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400601 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400602 struct nfs4_sequence_args *seq_args;
603 struct nfs4_sequence_res *seq_res;
604 int cache_reply;
605};
606
607static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
608{
609 struct nfs41_call_sync_data *data = calldata;
610
Trond Myklebust035168a2010-06-16 09:52:26 -0400611 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
612
613 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400614 data->seq_res, data->cache_reply, task))
615 return;
616 rpc_call_start(task);
617}
618
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800619static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
620{
621 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
622 nfs41_call_sync_prepare(task, calldata);
623}
624
Andy Adamson69ab40c2009-04-01 09:22:19 -0400625static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
626{
627 struct nfs41_call_sync_data *data = calldata;
628
Trond Myklebust14516c32010-07-31 14:29:06 -0400629 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400630}
631
Andy Adamsonce5039c2009-04-01 09:22:13 -0400632struct rpc_call_ops nfs41_call_sync_ops = {
633 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400634 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400635};
636
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800637struct rpc_call_ops nfs41_call_priv_sync_ops = {
638 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
639 .rpc_call_done = nfs41_call_sync_done,
640};
641
Trond Myklebust035168a2010-06-16 09:52:26 -0400642static int nfs4_call_sync_sequence(struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400643 struct rpc_message *msg,
644 struct nfs4_sequence_args *args,
645 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800646 int cache_reply,
647 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400648{
649 int ret;
650 struct rpc_task *task;
651 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400652 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400653 .seq_args = args,
654 .seq_res = res,
655 .cache_reply = cache_reply,
656 };
657 struct rpc_task_setup task_setup = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400658 .rpc_client = server->client,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400659 .rpc_message = msg,
660 .callback_ops = &nfs41_call_sync_ops,
661 .callback_data = &data
662 };
663
Benny Halevydfb4f3092010-09-24 09:17:01 -0400664 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800665 if (privileged)
666 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400667 task = rpc_run_task(&task_setup);
668 if (IS_ERR(task))
669 ret = PTR_ERR(task);
670 else {
671 ret = task->tk_status;
672 rpc_put_task(task);
673 }
674 return ret;
675}
676
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400677int _nfs4_call_sync_session(struct nfs_server *server,
678 struct rpc_message *msg,
679 struct nfs4_sequence_args *args,
680 struct nfs4_sequence_res *res,
681 int cache_reply)
682{
Trond Myklebust035168a2010-06-16 09:52:26 -0400683 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400684}
685
Trond Myklebustdf896452010-06-16 09:52:26 -0400686#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400687static int nfs4_sequence_done(struct rpc_task *task,
688 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400689{
Trond Myklebust14516c32010-07-31 14:29:06 -0400690 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400691}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400692#endif /* CONFIG_NFS_V4_1 */
693
694int _nfs4_call_sync(struct nfs_server *server,
695 struct rpc_message *msg,
696 struct nfs4_sequence_args *args,
697 struct nfs4_sequence_res *res,
698 int cache_reply)
699{
Benny Halevyf3752972009-04-01 09:22:04 -0400700 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400701 return rpc_call_sync(server->client, msg, 0);
702}
703
704#define nfs4_call_sync(server, msg, args, res, cache_reply) \
Trond Myklebust97dc1352010-06-16 09:52:26 -0400705 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400706 &(res)->seq_res, (cache_reply))
707
Trond Myklebust38478b22006-05-25 01:40:57 -0400708static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Trond Myklebust38478b22006-05-25 01:40:57 -0400710 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Trond Myklebust38478b22006-05-25 01:40:57 -0400712 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400713 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
714 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400715 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400716 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400717 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100720struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400721 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100722 struct nfs_openargs o_arg;
723 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100724 struct nfs_open_confirmargs c_arg;
725 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100726 struct nfs_fattr f_attr;
727 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400728 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100729 struct dentry *dir;
730 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400731 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100732 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100733 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400734 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100735 int rpc_status;
736 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100737};
738
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400739
740static void nfs4_init_opendata_res(struct nfs4_opendata *p)
741{
742 p->o_res.f_attr = &p->f_attr;
743 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400744 p->o_res.seqid = p->o_arg.seqid;
745 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400746 p->o_res.server = p->o_arg.server;
747 nfs_fattr_init(&p->f_attr);
748 nfs_fattr_init(&p->dir_attr);
749}
750
Trond Myklebustad389da2007-06-05 12:30:00 -0400751static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500752 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400753 const struct iattr *attrs,
754 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100755{
Trond Myklebustad389da2007-06-05 12:30:00 -0400756 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100757 struct inode *dir = parent->d_inode;
758 struct nfs_server *server = NFS_SERVER(dir);
759 struct nfs4_opendata *p;
760
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400761 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100762 if (p == NULL)
763 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400764 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100765 if (p->o_arg.seqid == NULL)
766 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500767 path_get(path);
768 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769 p->dir = parent;
770 p->owner = sp;
771 atomic_inc(&sp->so_count);
772 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500773 p->o_arg.open_flags = flags;
774 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400775 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400776 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400777 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100778 p->o_arg.server = server;
779 p->o_arg.bitmask = server->attr_bitmask;
780 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400781 if (flags & O_CREAT) {
782 u32 *s;
783
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100784 p->o_arg.u.attrs = &p->attrs;
785 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400786 s = (u32 *) p->o_arg.u.verifier.data;
787 s[0] = jiffies;
788 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100789 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100790 p->c_arg.fh = &p->o_res.fh;
791 p->c_arg.stateid = &p->o_res.stateid;
792 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400793 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400794 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100795 return p;
796err_free:
797 kfree(p);
798err:
799 dput(parent);
800 return NULL;
801}
802
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400803static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100804{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400805 struct nfs4_opendata *p = container_of(kref,
806 struct nfs4_opendata, kref);
807
808 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400809 if (p->state != NULL)
810 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400811 nfs4_put_state_owner(p->owner);
812 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700813 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400814 kfree(p);
815}
816
817static void nfs4_opendata_put(struct nfs4_opendata *p)
818{
819 if (p != NULL)
820 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100821}
822
Trond Myklebust06f814a2006-01-03 09:55:07 +0100823static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
824{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100825 int ret;
826
Trond Myklebust06f814a2006-01-03 09:55:07 +0100827 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100828 return ret;
829}
830
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500831static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400832{
833 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500834
835 if (open_mode & O_EXCL)
836 goto out;
837 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400838 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500839 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
840 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400841 break;
842 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500843 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
844 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400845 break;
846 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500847 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
848 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400849 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500850out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400851 return ret;
852}
853
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500854static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400855{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500856 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400857 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500858 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400859 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500860 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400861 return 1;
862}
863
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500864static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100865{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500866 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100867 case FMODE_WRITE:
868 state->n_wronly++;
869 break;
870 case FMODE_READ:
871 state->n_rdonly++;
872 break;
873 case FMODE_READ|FMODE_WRITE:
874 state->n_rdwr++;
875 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500876 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100877}
878
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500879static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400880{
881 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
882 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
883 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500884 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400885 case FMODE_READ:
886 set_bit(NFS_O_RDONLY_STATE, &state->flags);
887 break;
888 case FMODE_WRITE:
889 set_bit(NFS_O_WRONLY_STATE, &state->flags);
890 break;
891 case FMODE_READ|FMODE_WRITE:
892 set_bit(NFS_O_RDWR_STATE, &state->flags);
893 }
894}
895
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500896static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400897{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400898 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500899 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400900 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400901}
902
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903static 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 -0700904{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400905 /*
906 * Protect the call to nfs4_state_set_mode_locked and
907 * serialise the stateid update
908 */
909 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400910 if (deleg_stateid != NULL) {
911 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
912 set_bit(NFS_DELEGATED_STATE, &state->flags);
913 }
914 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500915 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400916 write_sequnlock(&state->seqlock);
917 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500918 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700919 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922static 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 -0500923{
924 struct nfs_inode *nfsi = NFS_I(state->inode);
925 struct nfs_delegation *deleg_cur;
926 int ret = 0;
927
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500929
930 rcu_read_lock();
931 deleg_cur = rcu_dereference(nfsi->delegation);
932 if (deleg_cur == NULL)
933 goto no_delegation;
934
935 spin_lock(&deleg_cur->lock);
936 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500938 goto no_delegation_unlock;
939
940 if (delegation == NULL)
941 delegation = &deleg_cur->stateid;
942 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
943 goto no_delegation_unlock;
944
Trond Myklebustb7391f42008-12-23 15:21:52 -0500945 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500947 ret = 1;
948no_delegation_unlock:
949 spin_unlock(&deleg_cur->lock);
950no_delegation:
951 rcu_read_unlock();
952
953 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500955 ret = 1;
956 }
957
958 return ret;
959}
960
961
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500962static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400963{
964 struct nfs_delegation *delegation;
965
966 rcu_read_lock();
967 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500968 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400969 rcu_read_unlock();
970 return;
971 }
972 rcu_read_unlock();
973 nfs_inode_return_delegation(inode);
974}
975
Trond Myklebust6ee41262007-07-08 14:11:36 -0400976static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400977{
978 struct nfs4_state *state = opendata->state;
979 struct nfs_inode *nfsi = NFS_I(state->inode);
980 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500981 int open_mode = opendata->o_arg.open_flags & O_EXCL;
982 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400983 nfs4_stateid stateid;
984 int ret = -EAGAIN;
985
Trond Myklebustaac00a82007-07-05 19:02:21 -0400986 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500987 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400988 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989 if (can_open_cached(state, fmode, open_mode)) {
990 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400991 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400992 goto out_return_state;
993 }
994 spin_unlock(&state->owner->so_lock);
995 }
Trond Myklebust34310432008-12-23 15:21:38 -0500996 rcu_read_lock();
997 delegation = rcu_dereference(nfsi->delegation);
998 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500999 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001000 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001001 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001002 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001003 /* Save the delegation */
1004 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1005 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001006 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001007 if (ret != 0)
1008 goto out;
1009 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001010
1011 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001012 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001013 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001014 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001015out:
1016 return ERR_PTR(ret);
1017out_return_state:
1018 atomic_inc(&state->count);
1019 return state;
1020}
1021
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001022static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1023{
1024 struct inode *inode;
1025 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001026 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001027 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001028
Trond Myklebustaac00a82007-07-05 19:02:21 -04001029 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001030 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001031 goto out;
1032 }
1033
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001034 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001035 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001036 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001037 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001038 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001039 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001040 goto err;
1041 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001042 state = nfs4_get_open_state(inode, data->owner);
1043 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001044 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001045 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001046 int delegation_flags = 0;
1047
Trond Myklebust003707c2007-07-05 18:07:55 -04001048 rcu_read_lock();
1049 delegation = rcu_dereference(NFS_I(inode)->delegation);
1050 if (delegation)
1051 delegation_flags = delegation->flags;
1052 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001053 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001054 nfs_inode_set_delegation(state->inode,
1055 data->owner->so_cred,
1056 &data->o_res);
1057 else
1058 nfs_inode_reclaim_delegation(state->inode,
1059 data->owner->so_cred,
1060 &data->o_res);
1061 }
Trond Myklebust34310432008-12-23 15:21:38 -05001062
1063 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001064 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001065 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001066out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001067 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001068err_put_inode:
1069 iput(inode);
1070err:
1071 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001072}
1073
Trond Myklebust864472e2006-01-03 09:55:15 +01001074static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1075{
1076 struct nfs_inode *nfsi = NFS_I(state->inode);
1077 struct nfs_open_context *ctx;
1078
1079 spin_lock(&state->inode->i_lock);
1080 list_for_each_entry(ctx, &nfsi->open_files, list) {
1081 if (ctx->state != state)
1082 continue;
1083 get_nfs_open_context(ctx);
1084 spin_unlock(&state->inode->i_lock);
1085 return ctx;
1086 }
1087 spin_unlock(&state->inode->i_lock);
1088 return ERR_PTR(-ENOENT);
1089}
1090
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001091static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1092{
1093 struct nfs4_opendata *opendata;
1094
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001095 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001096 if (opendata == NULL)
1097 return ERR_PTR(-ENOMEM);
1098 opendata->state = state;
1099 atomic_inc(&state->count);
1100 return opendata;
1101}
1102
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001103static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001104{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001105 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001106 int ret;
1107
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001108 opendata->o_arg.open_flags = 0;
1109 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001110 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1111 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1112 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001113 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001114 if (ret != 0)
1115 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001116 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001117 if (IS_ERR(newstate))
1118 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001119 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001120 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001121 return 0;
1122}
1123
1124static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1125{
Trond Myklebust864472e2006-01-03 09:55:15 +01001126 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001127 int ret;
1128
1129 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001130 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001131 smp_rmb();
1132 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001133 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001134 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001135 if (ret != 0)
1136 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001137 if (newstate != state)
1138 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001139 }
1140 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001141 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001142 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001143 if (ret != 0)
1144 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001145 if (newstate != state)
1146 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001147 }
1148 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001149 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001150 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001151 if (ret != 0)
1152 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001153 if (newstate != state)
1154 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001155 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001156 /*
1157 * We may have performed cached opens for all three recoveries.
1158 * Check if we need to update the current stateid.
1159 */
1160 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1161 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001162 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001163 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1164 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001165 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001166 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001167 return 0;
1168}
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170/*
1171 * OPEN_RECLAIM:
1172 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001174static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001176 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001177 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001178 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 int status;
1180
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001181 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1182 if (IS_ERR(opendata))
1183 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001184 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1185 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001186 rcu_read_lock();
1187 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001188 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001189 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001190 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001191 opendata->o_arg.u.delegation_type = delegation_type;
1192 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001193 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return status;
1195}
1196
Trond Myklebust539cd032007-06-05 11:46:42 -04001197static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
1199 struct nfs_server *server = NFS_SERVER(state->inode);
1200 struct nfs4_exception exception = { };
1201 int err;
1202 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001203 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001204 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001205 break;
1206 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 } while (exception.retry);
1208 return err;
1209}
1210
Trond Myklebust864472e2006-01-03 09:55:15 +01001211static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1212{
1213 struct nfs_open_context *ctx;
1214 int ret;
1215
1216 ctx = nfs4_state_find_open_context(state);
1217 if (IS_ERR(ctx))
1218 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001219 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001220 put_nfs_open_context(ctx);
1221 return ret;
1222}
1223
Trond Myklebust13437e12007-07-06 15:10:43 -04001224static 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 -07001225{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001226 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001227 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001229 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1230 if (IS_ERR(opendata))
1231 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001232 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001233 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001234 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001235 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001236 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001237 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
Trond Myklebust13437e12007-07-06 15:10:43 -04001240int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001243 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 int err;
1245 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001246 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 switch (err) {
1248 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001249 case -ENOENT:
1250 case -ESTALE:
1251 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001252 case -NFS4ERR_BADSESSION:
1253 case -NFS4ERR_BADSLOT:
1254 case -NFS4ERR_BAD_HIGH_SLOT:
1255 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1256 case -NFS4ERR_DEADSESSION:
1257 nfs4_schedule_state_recovery(
1258 server->nfs_client);
1259 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 case -NFS4ERR_STALE_CLIENTID:
1261 case -NFS4ERR_STALE_STATEID:
1262 case -NFS4ERR_EXPIRED:
1263 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001264 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001265 goto out;
1266 case -ERESTARTSYS:
1267 /*
1268 * The show must go on: exit, but mark the
1269 * stateid as needing recovery.
1270 */
1271 case -NFS4ERR_ADMIN_REVOKED:
1272 case -NFS4ERR_BAD_STATEID:
1273 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001274 case -EKEYEXPIRED:
1275 /*
1276 * User RPCSEC_GSS context has expired.
1277 * We cannot recover this stateid now, so
1278 * skip it and allow recovery thread to
1279 * proceed.
1280 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001281 case -ENOMEM:
1282 err = 0;
1283 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
1285 err = nfs4_handle_exception(server, err, &exception);
1286 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001287out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 return err;
1289}
1290
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001291static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1292{
1293 struct nfs4_opendata *data = calldata;
1294
1295 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001296 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001297 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1298 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001299 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001300 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001301 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001302 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001303}
1304
1305static void nfs4_open_confirm_release(void *calldata)
1306{
1307 struct nfs4_opendata *data = calldata;
1308 struct nfs4_state *state = NULL;
1309
1310 /* If this request hasn't been cancelled, do nothing */
1311 if (data->cancelled == 0)
1312 goto out_free;
1313 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001314 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001315 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001316 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001317 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001318 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001319out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001320 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001321}
1322
1323static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001324 .rpc_call_done = nfs4_open_confirm_done,
1325 .rpc_release = nfs4_open_confirm_release,
1326};
1327
1328/*
1329 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1330 */
1331static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1332{
1333 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1334 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001335 struct rpc_message msg = {
1336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1337 .rpc_argp = &data->c_arg,
1338 .rpc_resp = &data->c_res,
1339 .rpc_cred = data->owner->so_cred,
1340 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001341 struct rpc_task_setup task_setup_data = {
1342 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001343 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001344 .callback_ops = &nfs4_open_confirm_ops,
1345 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001346 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001347 .flags = RPC_TASK_ASYNC,
1348 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 int status;
1350
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001351 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001352 data->rpc_done = 0;
1353 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001354 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001355 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001356 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001357 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001358 status = nfs4_wait_for_completion_rpc_task(task);
1359 if (status != 0) {
1360 data->cancelled = 1;
1361 smp_wmb();
1362 } else
1363 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001364 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return status;
1366}
1367
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001368static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001370 struct nfs4_opendata *data = calldata;
1371 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001372
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001373 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1374 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001375 /*
1376 * Check if we still need to send an OPEN call, or if we can use
1377 * a delegation instead.
1378 */
1379 if (data->state != NULL) {
1380 struct nfs_delegation *delegation;
1381
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001382 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001383 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001384 rcu_read_lock();
1385 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1386 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001387 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001388 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001389 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001390 }
1391 rcu_read_unlock();
1392 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001393 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001394 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001395 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001396 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001397 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001398 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1399 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001400 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001401 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001402 &data->o_arg.seq_args,
1403 &data->o_res.seq_res, 1, task))
1404 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001405 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001406 return;
1407out_no_action:
1408 task->tk_action = NULL;
1409
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001410}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001412static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1413{
1414 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1415 nfs4_open_prepare(task, calldata);
1416}
1417
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001418static void nfs4_open_done(struct rpc_task *task, void *calldata)
1419{
1420 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001422 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001423
Trond Myklebust14516c32010-07-31 14:29:06 -04001424 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1425 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001426
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001427 if (task->tk_status == 0) {
1428 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001429 case S_IFREG:
1430 break;
1431 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001432 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001433 break;
1434 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001435 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001436 break;
1437 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001438 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001439 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001440 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001441 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1442 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001443 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001444 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001445}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001446
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001447static void nfs4_open_release(void *calldata)
1448{
1449 struct nfs4_opendata *data = calldata;
1450 struct nfs4_state *state = NULL;
1451
1452 /* If this request hasn't been cancelled, do nothing */
1453 if (data->cancelled == 0)
1454 goto out_free;
1455 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001456 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001457 goto out_free;
1458 /* In case we need an open_confirm, no cleanup! */
1459 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1460 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001461 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001462 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001463 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001464out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001465 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001466}
1467
1468static const struct rpc_call_ops nfs4_open_ops = {
1469 .rpc_call_prepare = nfs4_open_prepare,
1470 .rpc_call_done = nfs4_open_done,
1471 .rpc_release = nfs4_open_release,
1472};
1473
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001474static const struct rpc_call_ops nfs4_recover_open_ops = {
1475 .rpc_call_prepare = nfs4_recover_open_prepare,
1476 .rpc_call_done = nfs4_open_done,
1477 .rpc_release = nfs4_open_release,
1478};
1479
1480static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001481{
1482 struct inode *dir = data->dir->d_inode;
1483 struct nfs_server *server = NFS_SERVER(dir);
1484 struct nfs_openargs *o_arg = &data->o_arg;
1485 struct nfs_openres *o_res = &data->o_res;
1486 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001487 struct rpc_message msg = {
1488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1489 .rpc_argp = o_arg,
1490 .rpc_resp = o_res,
1491 .rpc_cred = data->owner->so_cred,
1492 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001493 struct rpc_task_setup task_setup_data = {
1494 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001495 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001496 .callback_ops = &nfs4_open_ops,
1497 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001498 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001499 .flags = RPC_TASK_ASYNC,
1500 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001501 int status;
1502
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001503 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001504 data->rpc_done = 0;
1505 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001506 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001507 if (isrecover)
1508 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001509 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001510 if (IS_ERR(task))
1511 return PTR_ERR(task);
1512 status = nfs4_wait_for_completion_rpc_task(task);
1513 if (status != 0) {
1514 data->cancelled = 1;
1515 smp_wmb();
1516 } else
1517 status = data->rpc_status;
1518 rpc_put_task(task);
1519
1520 return status;
1521}
1522
1523static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1524{
1525 struct inode *dir = data->dir->d_inode;
1526 struct nfs_openres *o_res = &data->o_res;
1527 int status;
1528
1529 status = nfs4_run_open_task(data, 1);
1530 if (status != 0 || !data->rpc_done)
1531 return status;
1532
1533 nfs_refresh_inode(dir, o_res->dir_attr);
1534
1535 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1536 status = _nfs4_proc_open_confirm(data);
1537 if (status != 0)
1538 return status;
1539 }
1540
1541 return status;
1542}
1543
1544/*
1545 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1546 */
1547static int _nfs4_proc_open(struct nfs4_opendata *data)
1548{
1549 struct inode *dir = data->dir->d_inode;
1550 struct nfs_server *server = NFS_SERVER(dir);
1551 struct nfs_openargs *o_arg = &data->o_arg;
1552 struct nfs_openres *o_res = &data->o_res;
1553 int status;
1554
1555 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001556 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001557 return status;
1558
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001559 if (o_arg->open_flags & O_CREAT) {
1560 update_changeattr(dir, &o_res->cinfo);
1561 nfs_post_op_update_inode(dir, o_res->dir_attr);
1562 } else
1563 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001564 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1565 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001567 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001569 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
1571 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001572 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001573 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
Trond Myklebust10afec92007-05-14 17:16:04 -04001576static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001577{
David Howells7539bba2006-08-22 20:06:09 -04001578 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001579 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001580 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001581
Trond Myklebusta78cb572009-08-09 15:06:19 -04001582 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001583 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001584 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001585 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001586 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1587 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001588 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001589 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001590 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001591 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001592 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001593}
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595/*
1596 * OPEN_EXPIRED:
1597 * reclaim state on the server after a network partition.
1598 * Assumes caller holds the appropriate lock
1599 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001600static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001602 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001603 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001605 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1606 if (IS_ERR(opendata))
1607 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001608 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001609 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001610 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001611 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001612 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001615static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001616{
Trond Myklebust539cd032007-06-05 11:46:42 -04001617 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001618 struct nfs4_exception exception = { };
1619 int err;
1620
1621 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001622 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001623 switch (err) {
1624 default:
1625 goto out;
1626 case -NFS4ERR_GRACE:
1627 case -NFS4ERR_DELAY:
1628 nfs4_handle_exception(server, err, &exception);
1629 err = 0;
1630 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001631 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001632out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001633 return err;
1634}
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1637{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001639 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Trond Myklebust864472e2006-01-03 09:55:15 +01001641 ctx = nfs4_state_find_open_context(state);
1642 if (IS_ERR(ctx))
1643 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001644 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001645 put_nfs_open_context(ctx);
1646 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
1649/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001650 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1651 * fields corresponding to attributes that were used to store the verifier.
1652 * Make sure we clobber those fields in the later setattr call
1653 */
1654static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1655{
1656 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1657 !(sattr->ia_valid & ATTR_ATIME_SET))
1658 sattr->ia_valid |= ATTR_ATIME;
1659
1660 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1661 !(sattr->ia_valid & ATTR_MTIME_SET))
1662 sattr->ia_valid |= ATTR_MTIME;
1663}
1664
1665/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001666 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001668static 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 -07001669{
1670 struct nfs4_state_owner *sp;
1671 struct nfs4_state *state = NULL;
1672 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001673 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001674 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 status = -ENOMEM;
1678 if (!(sp = nfs4_get_state_owner(server, cred))) {
1679 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1680 goto out_err;
1681 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001682 status = nfs4_recover_expired_lease(server);
1683 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001684 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001685 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001686 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001687 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001688 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001689 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001690 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Trond Myklebustaac00a82007-07-05 19:02:21 -04001692 if (path->dentry->d_inode != NULL)
1693 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1694
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001695 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001697 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001699 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001700 status = PTR_ERR(state);
1701 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001702 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001703 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001704 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001705
1706 if (opendata->o_arg.open_flags & O_EXCL) {
1707 nfs4_exclusive_attrset(opendata, sattr);
1708
1709 nfs_fattr_init(opendata->o_res.f_attr);
1710 status = nfs4_do_setattr(state->inode, cred,
1711 opendata->o_res.f_attr, sattr,
1712 state);
1713 if (status == 0)
1714 nfs_setattr_update_inode(state->inode, sattr);
1715 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1716 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001717 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 *res = state;
1720 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001721err_opendata_put:
1722 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001723err_put_state_owner:
1724 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 *res = NULL;
1727 return status;
1728}
1729
1730
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001731static 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 -07001732{
1733 struct nfs4_exception exception = { };
1734 struct nfs4_state *res;
1735 int status;
1736
1737 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001738 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (status == 0)
1740 break;
1741 /* NOTE: BAD_SEQID means the server and client disagree about the
1742 * book-keeping w.r.t. state-changing operations
1743 * (OPEN/CLOSE/LOCK/LOCKU...)
1744 * It is actually a sign of a bug on the client or on the server.
1745 *
1746 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001747 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 * have unhashed the old state_owner for us, and that we can
1749 * therefore safely retry using a new one. We should still warn
1750 * the user though...
1751 */
1752 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001753 printk(KERN_WARNING "NFS: v4 server %s "
1754 " returned a bad sequence-id error!\n",
1755 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 exception.retry = 1;
1757 continue;
1758 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001759 /*
1760 * BAD_STATEID on OPEN means that the server cancelled our
1761 * state before it received the OPEN_CONFIRM.
1762 * Recover by retrying the request as per the discussion
1763 * on Page 181 of RFC3530.
1764 */
1765 if (status == -NFS4ERR_BAD_STATEID) {
1766 exception.retry = 1;
1767 continue;
1768 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001769 if (status == -EAGAIN) {
1770 /* We must have found a delegation */
1771 exception.retry = 1;
1772 continue;
1773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1775 status, &exception));
1776 } while (exception.retry);
1777 return res;
1778}
1779
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001780static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1781 struct nfs_fattr *fattr, struct iattr *sattr,
1782 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001784 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001786 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 .iap = sattr,
1788 .server = server,
1789 .bitmask = server->attr_bitmask,
1790 };
1791 struct nfs_setattrres res = {
1792 .fattr = fattr,
1793 .server = server,
1794 };
1795 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1797 .rpc_argp = &arg,
1798 .rpc_resp = &res,
1799 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001801 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001802 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Trond Myklebust0e574af2005-10-27 22:12:38 -04001804 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001806 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1807 /* Use that stateid */
1808 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001809 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001810 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1812
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001813 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001814 if (status == 0 && state != NULL)
1815 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001816 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001819static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1820 struct nfs_fattr *fattr, struct iattr *sattr,
1821 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001823 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 struct nfs4_exception exception = { };
1825 int err;
1826 do {
1827 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001828 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 &exception);
1830 } while (exception.retry);
1831 return err;
1832}
1833
1834struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001835 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 struct inode *inode;
1837 struct nfs4_state *state;
1838 struct nfs_closeargs arg;
1839 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001840 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001841 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842};
1843
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001844static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001845{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001846 struct nfs4_closedata *calldata = data;
1847 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001848
1849 nfs4_put_open_state(calldata->state);
1850 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001851 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001852 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001853 kfree(calldata);
1854}
1855
Trond Myklebust88069f72009-12-08 08:33:16 -05001856static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1857 fmode_t fmode)
1858{
1859 spin_lock(&state->owner->so_lock);
1860 if (!(fmode & FMODE_READ))
1861 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1862 if (!(fmode & FMODE_WRITE))
1863 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1864 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1865 spin_unlock(&state->owner->so_lock);
1866}
1867
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001868static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001870 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 struct nfs_server *server = NFS_SERVER(calldata->inode);
1873
Trond Myklebust14516c32010-07-31 14:29:06 -04001874 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1875 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 /* hmm. we are done with the inode, and in the process of freeing
1877 * the state_owner. we keep this around to process errors
1878 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 switch (task->tk_status) {
1880 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001881 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001882 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001883 nfs4_close_clear_stateid_flags(state,
1884 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 break;
1886 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001887 case -NFS4ERR_OLD_STATEID:
1888 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001890 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001891 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001893 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1894 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001896 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001897 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898}
1899
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001900static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001902 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001903 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001904 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001905
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001906 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001907 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001908
Trond Myklebust88069f72009-12-08 08:33:16 -05001909 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1910 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001911 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001912 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001913 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001914 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001915 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1916 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1917 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001918 }
1919 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001920 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1921 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1922 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001923 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001924 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001925 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001926
1927 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001928 /* Note: exit _without_ calling nfs4_close_done */
1929 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001930 return;
1931 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001932
1933 if (calldata->arg.fmode == 0)
1934 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1935
Trond Myklebust516a6af2005-10-27 22:12:41 -04001936 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001937 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001938 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001939 &calldata->arg.seq_args, &calldata->res.seq_res,
1940 1, task))
1941 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001942 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001945static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001946 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001947 .rpc_call_done = nfs4_close_done,
1948 .rpc_release = nfs4_free_closedata,
1949};
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951/*
1952 * It is possible for data to be read/written from a mem-mapped file
1953 * after the sys_close call (which hits the vfs layer as a flush).
1954 * This means that we can't safely call nfsv4 close on a file until
1955 * the inode is cleared. This in turn means that we are not good
1956 * NFSv4 citizens - we do not indicate to the server to update the file's
1957 * share state even when we are done with one of the three share
1958 * stateid's in the inode.
1959 *
1960 * NOTE: Caller must be holding the sp->so_owner semaphore!
1961 */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001962int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001964 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001966 struct nfs4_state_owner *sp = state->owner;
1967 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001968 struct rpc_message msg = {
1969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1970 .rpc_cred = state->owner->so_cred,
1971 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001972 struct rpc_task_setup task_setup_data = {
1973 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001974 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001975 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001976 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001977 .flags = RPC_TASK_ASYNC,
1978 };
Trond Myklebust95121352005-10-18 14:20:12 -07001979 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001981 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001983 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001984 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001986 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001987 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001989 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07001990 if (calldata->arg.seqid == NULL)
1991 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001992 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001993 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001994 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001995 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001996 calldata->res.server = server;
Al Virof6948692010-01-30 13:51:04 -05001997 path_get(path);
1998 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07001999
Trond Myklebust1174dd12010-12-21 10:52:24 -05002000 msg.rpc_argp = &calldata->arg;
2001 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002002 task_setup_data.callback_data = calldata;
2003 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002004 if (IS_ERR(task))
2005 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002006 status = 0;
2007 if (wait)
2008 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002009 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002010 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002011out_free_calldata:
2012 kfree(calldata);
2013out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002014 nfs4_put_open_state(state);
2015 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002016 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
Trond Myklebust2b484292010-09-17 10:56:51 -04002019static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002020nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 struct nfs4_state *state;
2023
Trond Myklebust565277f2007-10-15 18:17:53 -04002024 /* Protect against concurrent sillydeletes */
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002025 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002026 if (IS_ERR(state))
2027 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002028 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002029 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Trond Myklebust1185a552009-12-03 15:54:02 -05002032static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002033{
2034 if (ctx->state == NULL)
2035 return;
2036 if (is_sync)
2037 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2038 else
2039 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2040}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2043{
Benny Halevy43652ad2009-04-01 09:21:54 -04002044 struct nfs4_server_caps_arg args = {
2045 .fhandle = fhandle,
2046 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 struct nfs4_server_caps_res res = {};
2048 struct rpc_message msg = {
2049 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002050 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 .rpc_resp = &res,
2052 };
2053 int status;
2054
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002055 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (status == 0) {
2057 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002058 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2059 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2060 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2061 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2062 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2064 server->caps |= NFS_CAP_ACLS;
2065 if (res.has_links != 0)
2066 server->caps |= NFS_CAP_HARDLINKS;
2067 if (res.has_symlinks != 0)
2068 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002069 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2070 server->caps |= NFS_CAP_FILEID;
2071 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2072 server->caps |= NFS_CAP_MODE;
2073 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2074 server->caps |= NFS_CAP_NLINK;
2075 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2076 server->caps |= NFS_CAP_OWNER;
2077 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2078 server->caps |= NFS_CAP_OWNER_GROUP;
2079 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2080 server->caps |= NFS_CAP_ATIME;
2081 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2082 server->caps |= NFS_CAP_CTIME;
2083 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2084 server->caps |= NFS_CAP_MTIME;
2085
Trond Myklebusta65318b2009-03-11 14:10:28 -04002086 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2087 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2088 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 server->acl_bitmask = res.acl_bitmask;
2090 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 return status;
2093}
2094
Trond Myklebust55a97592006-06-09 09:34:19 -04002095int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
2097 struct nfs4_exception exception = { };
2098 int err;
2099 do {
2100 err = nfs4_handle_exception(server,
2101 _nfs4_server_capabilities(server, fhandle),
2102 &exception);
2103 } while (exception.retry);
2104 return err;
2105}
2106
2107static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2108 struct nfs_fsinfo *info)
2109{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 struct nfs4_lookup_root_arg args = {
2111 .bitmask = nfs4_fattr_bitmap,
2112 };
2113 struct nfs4_lookup_res res = {
2114 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002115 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 .fh = fhandle,
2117 };
2118 struct rpc_message msg = {
2119 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2120 .rpc_argp = &args,
2121 .rpc_resp = &res,
2122 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002123
Trond Myklebust0e574af2005-10-27 22:12:38 -04002124 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002125 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
2128static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2129 struct nfs_fsinfo *info)
2130{
2131 struct nfs4_exception exception = { };
2132 int err;
2133 do {
2134 err = nfs4_handle_exception(server,
2135 _nfs4_lookup_root(server, fhandle, info),
2136 &exception);
2137 } while (exception.retry);
2138 return err;
2139}
2140
David Howells54ceac42006-08-22 20:06:13 -04002141/*
2142 * get the file handle for the "/" directory on the server
2143 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002145 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 int status;
2148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 if (status == 0)
2151 status = nfs4_server_capabilities(server, fhandle);
2152 if (status == 0)
2153 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002154 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
2156
Manoj Naik6b97fd32006-06-09 09:34:29 -04002157/*
2158 * Get locations and (maybe) other attributes of a referral.
2159 * Note that we'll actually follow the referral later when
2160 * we detect fsid mismatch in inode revalidation
2161 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002162static 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 -04002163{
2164 int status = -ENOMEM;
2165 struct page *page = NULL;
2166 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002167
2168 page = alloc_page(GFP_KERNEL);
2169 if (page == NULL)
2170 goto out;
2171 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2172 if (locations == NULL)
2173 goto out;
2174
Trond Myklebustc228fd32007-01-13 02:28:11 -05002175 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002176 if (status != 0)
2177 goto out;
2178 /* Make sure server returned a different fsid for the referral */
2179 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002180 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 -04002181 status = -EIO;
2182 goto out;
2183 }
2184
2185 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2186 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2187 if (!fattr->mode)
2188 fattr->mode = S_IFDIR;
2189 memset(fhandle, 0, sizeof(struct nfs_fh));
2190out:
2191 if (page)
2192 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002193 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002194 return status;
2195}
2196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2198{
2199 struct nfs4_getattr_arg args = {
2200 .fh = fhandle,
2201 .bitmask = server->attr_bitmask,
2202 };
2203 struct nfs4_getattr_res res = {
2204 .fattr = fattr,
2205 .server = server,
2206 };
2207 struct rpc_message msg = {
2208 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2209 .rpc_argp = &args,
2210 .rpc_resp = &res,
2211 };
2212
Trond Myklebust0e574af2005-10-27 22:12:38 -04002213 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002214 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
2217static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2218{
2219 struct nfs4_exception exception = { };
2220 int err;
2221 do {
2222 err = nfs4_handle_exception(server,
2223 _nfs4_proc_getattr(server, fhandle, fattr),
2224 &exception);
2225 } while (exception.retry);
2226 return err;
2227}
2228
2229/*
2230 * The file is not closed if it is opened due to the a request to change
2231 * the size of the file. The open call will not be needed once the
2232 * VFS layer lookup-intents are implemented.
2233 *
2234 * Close is called when the inode is destroyed.
2235 * If we haven't opened the file for O_WRONLY, we
2236 * need to in the size_change case to obtain a stateid.
2237 *
2238 * Got race?
2239 * Because OPEN is always done by name in nfsv4, it is
2240 * possible that we opened a different file by the same
2241 * name. We can recognize this race condition, but we
2242 * can't do anything about it besides returning an error.
2243 *
2244 * This will be fixed with VFS changes (lookup-intent).
2245 */
2246static int
2247nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2248 struct iattr *sattr)
2249{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002250 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002251 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002252 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 int status;
2254
Trond Myklebust0e574af2005-10-27 22:12:38 -04002255 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Trond Myklebustd5308382005-11-04 15:33:38 -05002257 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002258 if (sattr->ia_valid & ATTR_FILE) {
2259 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002260
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002261 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002262 if (ctx) {
2263 cred = ctx->cred;
2264 state = ctx->state;
2265 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002266 }
2267
2268 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002269 if (status == 0)
2270 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 return status;
2272}
2273
Trond Myklebust56659e92007-07-17 21:52:39 -04002274static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2275 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002276 struct nfs_fattr *fattr)
2277{
2278 int status;
2279 struct nfs4_lookup_arg args = {
2280 .bitmask = server->attr_bitmask,
2281 .dir_fh = dirfh,
2282 .name = name,
2283 };
2284 struct nfs4_lookup_res res = {
2285 .server = server,
2286 .fattr = fattr,
2287 .fh = fhandle,
2288 };
2289 struct rpc_message msg = {
2290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2291 .rpc_argp = &args,
2292 .rpc_resp = &res,
2293 };
2294
2295 nfs_fattr_init(fattr);
2296
2297 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002298 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002299 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002300 return status;
2301}
2302
2303static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2304 struct qstr *name, struct nfs_fh *fhandle,
2305 struct nfs_fattr *fattr)
2306{
2307 struct nfs4_exception exception = { };
2308 int err;
2309 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002310 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2311 /* FIXME: !!!! */
2312 if (err == -NFS4ERR_MOVED) {
2313 err = -EREMOTE;
2314 break;
2315 }
2316 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002317 } while (exception.retry);
2318 return err;
2319}
2320
Trond Myklebust56659e92007-07-17 21:52:39 -04002321static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2323{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002324 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
2326 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002327 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002328 if (status == -NFS4ERR_MOVED)
2329 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 dprintk("NFS reply lookup: %d\n", status);
2331 return status;
2332}
2333
2334static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2335{
2336 struct nfs4_exception exception = { };
2337 int err;
2338 do {
2339 err = nfs4_handle_exception(NFS_SERVER(dir),
2340 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2341 &exception);
2342 } while (exception.retry);
2343 return err;
2344}
2345
2346static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2347{
Trond Myklebust76b32992007-08-10 17:45:11 -04002348 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 struct nfs4_accessargs args = {
2350 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002351 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002353 struct nfs4_accessres res = {
2354 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002355 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 struct rpc_message msg = {
2357 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2358 .rpc_argp = &args,
2359 .rpc_resp = &res,
2360 .rpc_cred = entry->cred,
2361 };
2362 int mode = entry->mask;
2363 int status;
2364
2365 /*
2366 * Determine which access bits we want to ask for...
2367 */
2368 if (mode & MAY_READ)
2369 args.access |= NFS4_ACCESS_READ;
2370 if (S_ISDIR(inode->i_mode)) {
2371 if (mode & MAY_WRITE)
2372 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2373 if (mode & MAY_EXEC)
2374 args.access |= NFS4_ACCESS_LOOKUP;
2375 } else {
2376 if (mode & MAY_WRITE)
2377 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2378 if (mode & MAY_EXEC)
2379 args.access |= NFS4_ACCESS_EXECUTE;
2380 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002381
2382 res.fattr = nfs_alloc_fattr();
2383 if (res.fattr == NULL)
2384 return -ENOMEM;
2385
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002386 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if (!status) {
2388 entry->mask = 0;
2389 if (res.access & NFS4_ACCESS_READ)
2390 entry->mask |= MAY_READ;
2391 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2392 entry->mask |= MAY_WRITE;
2393 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2394 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002395 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002397 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 return status;
2399}
2400
2401static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2402{
2403 struct nfs4_exception exception = { };
2404 int err;
2405 do {
2406 err = nfs4_handle_exception(NFS_SERVER(inode),
2407 _nfs4_proc_access(inode, entry),
2408 &exception);
2409 } while (exception.retry);
2410 return err;
2411}
2412
2413/*
2414 * TODO: For the time being, we don't try to get any attributes
2415 * along with any of the zero-copy operations READ, READDIR,
2416 * READLINK, WRITE.
2417 *
2418 * In the case of the first three, we want to put the GETATTR
2419 * after the read-type operation -- this is because it is hard
2420 * to predict the length of a GETATTR response in v4, and thus
2421 * align the READ data correctly. This means that the GETATTR
2422 * may end up partially falling into the page cache, and we should
2423 * shift it into the 'tail' of the xdr_buf before processing.
2424 * To do this efficiently, we need to know the total length
2425 * of data received, which doesn't seem to be available outside
2426 * of the RPC layer.
2427 *
2428 * In the case of WRITE, we also want to put the GETATTR after
2429 * the operation -- in this case because we want to make sure
2430 * we get the post-operation mtime and size. This means that
2431 * we can't use xdr_encode_pages() as written: we need a variant
2432 * of it which would leave room in the 'tail' iovec.
2433 *
2434 * Both of these changes to the XDR layer would in fact be quite
2435 * minor, but I decided to leave them for a subsequent patch.
2436 */
2437static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2438 unsigned int pgbase, unsigned int pglen)
2439{
2440 struct nfs4_readlink args = {
2441 .fh = NFS_FH(inode),
2442 .pgbase = pgbase,
2443 .pglen = pglen,
2444 .pages = &page,
2445 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002446 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 struct rpc_message msg = {
2448 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2449 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002450 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 };
2452
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002453 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454}
2455
2456static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2457 unsigned int pgbase, unsigned int pglen)
2458{
2459 struct nfs4_exception exception = { };
2460 int err;
2461 do {
2462 err = nfs4_handle_exception(NFS_SERVER(inode),
2463 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2464 &exception);
2465 } while (exception.retry);
2466 return err;
2467}
2468
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469/*
2470 * Got race?
2471 * We will need to arrange for the VFS layer to provide an atomic open.
2472 * Until then, this create/open method is prone to inefficiency and race
2473 * conditions due to the lookup, create, and open VFS calls from sys_open()
2474 * placed on the wire.
2475 *
2476 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2477 * The file will be opened again in the subsequent VFS open call
2478 * (nfs4_proc_file_open).
2479 *
2480 * The open for read will just hang around to be used by any process that
2481 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2482 */
2483
2484static int
2485nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002486 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487{
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002488 struct path my_path = {
Trond Myklebustad389da2007-06-05 12:30:00 -04002489 .dentry = dentry,
2490 };
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002491 struct path *path = &my_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002493 struct rpc_cred *cred = NULL;
2494 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 int status = 0;
2496
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002497 if (ctx != NULL) {
2498 cred = ctx->cred;
2499 path = &ctx->path;
2500 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002502 sattr->ia_mode &= ~current_umask();
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002503 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002504 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if (IS_ERR(state)) {
2506 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002507 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002509 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002510 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002511 if (ctx != NULL)
2512 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002513 else
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002514 nfs4_close_sync(path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515out:
2516 return status;
2517}
2518
2519static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2520{
Trond Myklebust16e42952005-10-27 22:12:44 -04002521 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002522 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002524 .name.len = name->len,
2525 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002526 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002528 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002529 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002530 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2533 .rpc_argp = &args,
2534 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002536 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Trond Myklebustd3468902010-04-16 16:22:50 -04002538 res.dir_attr = nfs_alloc_fattr();
2539 if (res.dir_attr == NULL)
2540 goto out;
2541
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002542 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002543 if (status == 0) {
2544 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002545 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002546 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002547 nfs_free_fattr(res.dir_attr);
2548out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 return status;
2550}
2551
2552static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2553{
2554 struct nfs4_exception exception = { };
2555 int err;
2556 do {
2557 err = nfs4_handle_exception(NFS_SERVER(dir),
2558 _nfs4_proc_remove(dir, name),
2559 &exception);
2560 } while (exception.retry);
2561 return err;
2562}
2563
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002564static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002566 struct nfs_server *server = NFS_SERVER(dir);
2567 struct nfs_removeargs *args = msg->rpc_argp;
2568 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Trond Myklebusta65318b2009-03-11 14:10:28 -04002570 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002571 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002572 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574}
2575
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002576static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002578 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2579
Trond Myklebust14516c32010-07-31 14:29:06 -04002580 if (!nfs4_sequence_done(task, &res->seq_res))
2581 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002582 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002583 return 0;
2584 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002585 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002586 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587}
2588
Jeff Laytond3d41522010-09-17 17:31:57 -04002589static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2590{
2591 struct nfs_server *server = NFS_SERVER(dir);
2592 struct nfs_renameargs *arg = msg->rpc_argp;
2593 struct nfs_renameres *res = msg->rpc_resp;
2594
2595 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2596 arg->bitmask = server->attr_bitmask;
2597 res->server = server;
2598}
2599
2600static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2601 struct inode *new_dir)
2602{
2603 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2604
2605 if (!nfs4_sequence_done(task, &res->seq_res))
2606 return 0;
2607 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2608 return 0;
2609
2610 update_changeattr(old_dir, &res->old_cinfo);
2611 nfs_post_op_update_inode(old_dir, res->old_fattr);
2612 update_changeattr(new_dir, &res->new_cinfo);
2613 nfs_post_op_update_inode(new_dir, res->new_fattr);
2614 return 1;
2615}
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2618 struct inode *new_dir, struct qstr *new_name)
2619{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002620 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002621 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 .old_dir = NFS_FH(old_dir),
2623 .new_dir = NFS_FH(new_dir),
2624 .old_name = old_name,
2625 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002626 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002628 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002629 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002630 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 struct rpc_message msg = {
2632 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2633 .rpc_argp = &arg,
2634 .rpc_resp = &res,
2635 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002636 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Trond Myklebust011fff72010-04-16 16:22:49 -04002638 res.old_fattr = nfs_alloc_fattr();
2639 res.new_fattr = nfs_alloc_fattr();
2640 if (res.old_fattr == NULL || res.new_fattr == NULL)
2641 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Trond Myklebust011fff72010-04-16 16:22:49 -04002643 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 if (!status) {
2645 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002646 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002648 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002650out:
2651 nfs_free_fattr(res.new_fattr);
2652 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 return status;
2654}
2655
2656static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2657 struct inode *new_dir, struct qstr *new_name)
2658{
2659 struct nfs4_exception exception = { };
2660 int err;
2661 do {
2662 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2663 _nfs4_proc_rename(old_dir, old_name,
2664 new_dir, new_name),
2665 &exception);
2666 } while (exception.retry);
2667 return err;
2668}
2669
2670static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2671{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002672 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 struct nfs4_link_arg arg = {
2674 .fh = NFS_FH(inode),
2675 .dir_fh = NFS_FH(dir),
2676 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002677 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002679 struct nfs4_link_res res = {
2680 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002681 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 struct rpc_message msg = {
2683 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2684 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002685 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002687 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Trond Myklebust136f2622010-04-16 16:22:49 -04002689 res.fattr = nfs_alloc_fattr();
2690 res.dir_attr = nfs_alloc_fattr();
2691 if (res.fattr == NULL || res.dir_attr == NULL)
2692 goto out;
2693
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002694 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002695 if (!status) {
2696 update_changeattr(dir, &res.cinfo);
2697 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002698 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002699 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002700out:
2701 nfs_free_fattr(res.dir_attr);
2702 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 return status;
2704}
2705
2706static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2707{
2708 struct nfs4_exception exception = { };
2709 int err;
2710 do {
2711 err = nfs4_handle_exception(NFS_SERVER(inode),
2712 _nfs4_proc_link(inode, dir, name),
2713 &exception);
2714 } while (exception.retry);
2715 return err;
2716}
2717
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002718struct nfs4_createdata {
2719 struct rpc_message msg;
2720 struct nfs4_create_arg arg;
2721 struct nfs4_create_res res;
2722 struct nfs_fh fh;
2723 struct nfs_fattr fattr;
2724 struct nfs_fattr dir_fattr;
2725};
2726
2727static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2728 struct qstr *name, struct iattr *sattr, u32 ftype)
2729{
2730 struct nfs4_createdata *data;
2731
2732 data = kzalloc(sizeof(*data), GFP_KERNEL);
2733 if (data != NULL) {
2734 struct nfs_server *server = NFS_SERVER(dir);
2735
2736 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2737 data->msg.rpc_argp = &data->arg;
2738 data->msg.rpc_resp = &data->res;
2739 data->arg.dir_fh = NFS_FH(dir);
2740 data->arg.server = server;
2741 data->arg.name = name;
2742 data->arg.attrs = sattr;
2743 data->arg.ftype = ftype;
2744 data->arg.bitmask = server->attr_bitmask;
2745 data->res.server = server;
2746 data->res.fh = &data->fh;
2747 data->res.fattr = &data->fattr;
2748 data->res.dir_fattr = &data->dir_fattr;
2749 nfs_fattr_init(data->res.fattr);
2750 nfs_fattr_init(data->res.dir_fattr);
2751 }
2752 return data;
2753}
2754
2755static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2756{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002757 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2758 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002759 if (status == 0) {
2760 update_changeattr(dir, &data->res.dir_cinfo);
2761 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2762 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2763 }
2764 return status;
2765}
2766
2767static void nfs4_free_createdata(struct nfs4_createdata *data)
2768{
2769 kfree(data);
2770}
2771
Chuck Lever4f390c12006-08-22 20:06:22 -04002772static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002773 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002775 struct nfs4_createdata *data;
2776 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Chuck Lever94a6d752006-08-22 20:06:23 -04002778 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002779 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002780
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002781 status = -ENOMEM;
2782 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2783 if (data == NULL)
2784 goto out;
2785
2786 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2787 data->arg.u.symlink.pages = &page;
2788 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002790 status = nfs4_do_create(dir, dentry, data);
2791
2792 nfs4_free_createdata(data);
2793out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 return status;
2795}
2796
Chuck Lever4f390c12006-08-22 20:06:22 -04002797static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002798 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
2800 struct nfs4_exception exception = { };
2801 int err;
2802 do {
2803 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002804 _nfs4_proc_symlink(dir, dentry, page,
2805 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 &exception);
2807 } while (exception.retry);
2808 return err;
2809}
2810
2811static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2812 struct iattr *sattr)
2813{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002814 struct nfs4_createdata *data;
2815 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002817 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2818 if (data == NULL)
2819 goto out;
2820
2821 status = nfs4_do_create(dir, dentry, data);
2822
2823 nfs4_free_createdata(data);
2824out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 return status;
2826}
2827
2828static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2829 struct iattr *sattr)
2830{
2831 struct nfs4_exception exception = { };
2832 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002833
2834 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 do {
2836 err = nfs4_handle_exception(NFS_SERVER(dir),
2837 _nfs4_proc_mkdir(dir, dentry, sattr),
2838 &exception);
2839 } while (exception.retry);
2840 return err;
2841}
2842
2843static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002844 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845{
2846 struct inode *dir = dentry->d_inode;
2847 struct nfs4_readdir_arg args = {
2848 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002849 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 .pgbase = 0,
2851 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002852 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002853 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 };
2855 struct nfs4_readdir_res res;
2856 struct rpc_message msg = {
2857 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2858 .rpc_argp = &args,
2859 .rpc_resp = &res,
2860 .rpc_cred = cred,
2861 };
2862 int status;
2863
Harvey Harrison3110ff82008-05-02 13:42:44 -07002864 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002865 dentry->d_parent->d_name.name,
2866 dentry->d_name.name,
2867 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2869 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002870 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05002871 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05002873 status += args.pgbase;
2874 }
Trond Myklebustc4812992007-09-28 17:11:45 -04002875
2876 nfs_invalidate_atime(dir);
2877
Harvey Harrison3110ff82008-05-02 13:42:44 -07002878 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 return status;
2880}
2881
2882static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002883 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
2885 struct nfs4_exception exception = { };
2886 int err;
2887 do {
2888 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2889 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002890 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 &exception);
2892 } while (exception.retry);
2893 return err;
2894}
2895
2896static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2897 struct iattr *sattr, dev_t rdev)
2898{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002899 struct nfs4_createdata *data;
2900 int mode = sattr->ia_mode;
2901 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
2903 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2904 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002905
2906 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2907 if (data == NULL)
2908 goto out;
2909
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002911 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002913 data->arg.ftype = NF4BLK;
2914 data->arg.u.device.specdata1 = MAJOR(rdev);
2915 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 }
2917 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002918 data->arg.ftype = NF4CHR;
2919 data->arg.u.device.specdata1 = MAJOR(rdev);
2920 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002923 status = nfs4_do_create(dir, dentry, data);
2924
2925 nfs4_free_createdata(data);
2926out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 return status;
2928}
2929
2930static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2931 struct iattr *sattr, dev_t rdev)
2932{
2933 struct nfs4_exception exception = { };
2934 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002935
2936 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 do {
2938 err = nfs4_handle_exception(NFS_SERVER(dir),
2939 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2940 &exception);
2941 } while (exception.retry);
2942 return err;
2943}
2944
2945static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2946 struct nfs_fsstat *fsstat)
2947{
2948 struct nfs4_statfs_arg args = {
2949 .fh = fhandle,
2950 .bitmask = server->attr_bitmask,
2951 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002952 struct nfs4_statfs_res res = {
2953 .fsstat = fsstat,
2954 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 struct rpc_message msg = {
2956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2957 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002958 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 };
2960
Trond Myklebust0e574af2005-10-27 22:12:38 -04002961 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002962 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963}
2964
2965static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2966{
2967 struct nfs4_exception exception = { };
2968 int err;
2969 do {
2970 err = nfs4_handle_exception(server,
2971 _nfs4_proc_statfs(server, fhandle, fsstat),
2972 &exception);
2973 } while (exception.retry);
2974 return err;
2975}
2976
2977static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2978 struct nfs_fsinfo *fsinfo)
2979{
2980 struct nfs4_fsinfo_arg args = {
2981 .fh = fhandle,
2982 .bitmask = server->attr_bitmask,
2983 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002984 struct nfs4_fsinfo_res res = {
2985 .fsinfo = fsinfo,
2986 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 struct rpc_message msg = {
2988 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2989 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002990 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 };
2992
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002993 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994}
2995
2996static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2997{
2998 struct nfs4_exception exception = { };
2999 int err;
3000
3001 do {
3002 err = nfs4_handle_exception(server,
3003 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3004 &exception);
3005 } while (exception.retry);
3006 return err;
3007}
3008
3009static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3010{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003011 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3013}
3014
3015static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3016 struct nfs_pathconf *pathconf)
3017{
3018 struct nfs4_pathconf_arg args = {
3019 .fh = fhandle,
3020 .bitmask = server->attr_bitmask,
3021 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003022 struct nfs4_pathconf_res res = {
3023 .pathconf = pathconf,
3024 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 struct rpc_message msg = {
3026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3027 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003028 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 };
3030
3031 /* None of the pathconf attributes are mandatory to implement */
3032 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3033 memset(pathconf, 0, sizeof(*pathconf));
3034 return 0;
3035 }
3036
Trond Myklebust0e574af2005-10-27 22:12:38 -04003037 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003038 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039}
3040
3041static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3042 struct nfs_pathconf *pathconf)
3043{
3044 struct nfs4_exception exception = { };
3045 int err;
3046
3047 do {
3048 err = nfs4_handle_exception(server,
3049 _nfs4_proc_pathconf(server, fhandle, pathconf),
3050 &exception);
3051 } while (exception.retry);
3052 return err;
3053}
3054
Trond Myklebustec06c092006-03-20 13:44:27 -05003055static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
Trond Myklebustec06c092006-03-20 13:44:27 -05003057 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003059 dprintk("--> %s\n", __func__);
3060
Trond Myklebust14516c32010-07-31 14:29:06 -04003061 if (!nfs4_sequence_done(task, &data->res.seq_res))
3062 return -EAGAIN;
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003063
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003064 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003065 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003066 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003068
3069 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003071 renew_lease(server, data->timestamp);
3072 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073}
3074
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003075static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003078 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079}
3080
Trond Myklebust788e7a82006-03-20 13:44:27 -05003081static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 struct inode *inode = data->inode;
3084
Trond Myklebust14516c32010-07-31 14:29:06 -04003085 if (!nfs4_sequence_done(task, &data->res.seq_res))
3086 return -EAGAIN;
Andy Adamsondef6ed72009-04-01 09:22:26 -04003087
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003088 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003089 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003090 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003092 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003094 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003095 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003096 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097}
3098
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003099static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003101 struct nfs_server *server = NFS_SERVER(data->inode);
3102
Trond Myklebusta65318b2009-03-11 14:10:28 -04003103 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003104 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 data->timestamp = jiffies;
3106
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003107 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108}
3109
Trond Myklebust788e7a82006-03-20 13:44:27 -05003110static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 struct inode *inode = data->inode;
3113
Trond Myklebust14516c32010-07-31 14:29:06 -04003114 if (!nfs4_sequence_done(task, &data->res.seq_res))
3115 return -EAGAIN;
3116
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003117 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003118 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003119 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003121 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123}
3124
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003125static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003127 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
Trond Myklebusta65318b2009-03-11 14:10:28 -04003129 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003130 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003131 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132}
3133
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003134struct nfs4_renewdata {
3135 struct nfs_client *client;
3136 unsigned long timestamp;
3137};
3138
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139/*
3140 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3141 * standalone procedure for queueing an asynchronous RENEW.
3142 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003143static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003144{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003145 struct nfs4_renewdata *data = calldata;
3146 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003147
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003148 if (atomic_read(&clp->cl_count) > 1)
3149 nfs4_schedule_state_renewal(clp);
3150 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003151 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003152}
3153
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003154static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003156 struct nfs4_renewdata *data = calldata;
3157 struct nfs_client *clp = data->client;
3158 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
3160 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003161 /* Unless we're shutting down, schedule state recovery! */
3162 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3163 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 return;
3165 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003166 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167}
3168
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003169static const struct rpc_call_ops nfs4_renew_ops = {
3170 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003171 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003172};
3173
David Howellsadfa6f92006-08-22 20:06:08 -04003174int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175{
3176 struct rpc_message msg = {
3177 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3178 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003179 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003181 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003183 if (!atomic_inc_not_zero(&clp->cl_count))
3184 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003185 data = kmalloc(sizeof(*data), GFP_KERNEL);
3186 if (data == NULL)
3187 return -ENOMEM;
3188 data->client = clp;
3189 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003191 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192}
3193
David Howellsadfa6f92006-08-22 20:06:08 -04003194int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195{
3196 struct rpc_message msg = {
3197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3198 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003199 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 };
3201 unsigned long now = jiffies;
3202 int status;
3203
3204 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3205 if (status < 0)
3206 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003207 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 return 0;
3209}
3210
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003211static inline int nfs4_server_supports_acls(struct nfs_server *server)
3212{
3213 return (server->caps & NFS_CAP_ACLS)
3214 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3215 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3216}
3217
3218/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3219 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3220 * the stack.
3221 */
3222#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3223
3224static void buf_to_pages(const void *buf, size_t buflen,
3225 struct page **pages, unsigned int *pgbase)
3226{
3227 const void *p = buf;
3228
3229 *pgbase = offset_in_page(buf);
3230 p -= *pgbase;
3231 while (p < buf + buflen) {
3232 *(pages++) = virt_to_page(p);
3233 p += PAGE_CACHE_SIZE;
3234 }
3235}
3236
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003237struct nfs4_cached_acl {
3238 int cached;
3239 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003240 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003241};
3242
3243static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003244{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003245 struct nfs_inode *nfsi = NFS_I(inode);
3246
3247 spin_lock(&inode->i_lock);
3248 kfree(nfsi->nfs4_acl);
3249 nfsi->nfs4_acl = acl;
3250 spin_unlock(&inode->i_lock);
3251}
3252
3253static void nfs4_zap_acl_attr(struct inode *inode)
3254{
3255 nfs4_set_cached_acl(inode, NULL);
3256}
3257
3258static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3259{
3260 struct nfs_inode *nfsi = NFS_I(inode);
3261 struct nfs4_cached_acl *acl;
3262 int ret = -ENOENT;
3263
3264 spin_lock(&inode->i_lock);
3265 acl = nfsi->nfs4_acl;
3266 if (acl == NULL)
3267 goto out;
3268 if (buf == NULL) /* user is just asking for length */
3269 goto out_len;
3270 if (acl->cached == 0)
3271 goto out;
3272 ret = -ERANGE; /* see getxattr(2) man page */
3273 if (acl->len > buflen)
3274 goto out;
3275 memcpy(buf, acl->data, acl->len);
3276out_len:
3277 ret = acl->len;
3278out:
3279 spin_unlock(&inode->i_lock);
3280 return ret;
3281}
3282
3283static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3284{
3285 struct nfs4_cached_acl *acl;
3286
3287 if (buf && acl_len <= PAGE_SIZE) {
3288 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3289 if (acl == NULL)
3290 goto out;
3291 acl->cached = 1;
3292 memcpy(acl->data, buf, acl_len);
3293 } else {
3294 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3295 if (acl == NULL)
3296 goto out;
3297 acl->cached = 0;
3298 }
3299 acl->len = acl_len;
3300out:
3301 nfs4_set_cached_acl(inode, acl);
3302}
3303
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003304static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003305{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003306 struct page *pages[NFS4ACL_MAXPAGES];
3307 struct nfs_getaclargs args = {
3308 .fh = NFS_FH(inode),
3309 .acl_pages = pages,
3310 .acl_len = buflen,
3311 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003312 struct nfs_getaclres res = {
3313 .acl_len = buflen,
3314 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003315 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003316 struct rpc_message msg = {
3317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3318 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003319 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003320 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003321 struct page *localpage = NULL;
3322 int ret;
3323
3324 if (buflen < PAGE_SIZE) {
3325 /* As long as we're doing a round trip to the server anyway,
3326 * let's be prepared for a page of acl data. */
3327 localpage = alloc_page(GFP_KERNEL);
3328 resp_buf = page_address(localpage);
3329 if (localpage == NULL)
3330 return -ENOMEM;
3331 args.acl_pages[0] = localpage;
3332 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003333 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003334 } else {
3335 resp_buf = buf;
3336 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3337 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003338 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003339 if (ret)
3340 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003341 if (res.acl_len > args.acl_len)
3342 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003343 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003344 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003345 if (buf) {
3346 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003347 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003348 goto out_free;
3349 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003350 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003351 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003352 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003353out_free:
3354 if (localpage)
3355 __free_page(localpage);
3356 return ret;
3357}
3358
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003359static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3360{
3361 struct nfs4_exception exception = { };
3362 ssize_t ret;
3363 do {
3364 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3365 if (ret >= 0)
3366 break;
3367 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3368 } while (exception.retry);
3369 return ret;
3370}
3371
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003372static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3373{
3374 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003375 int ret;
3376
3377 if (!nfs4_server_supports_acls(server))
3378 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003379 ret = nfs_revalidate_inode(server, inode);
3380 if (ret < 0)
3381 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003382 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3383 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003384 ret = nfs4_read_cached_acl(inode, buf, buflen);
3385 if (ret != -ENOENT)
3386 return ret;
3387 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003388}
3389
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003390static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003391{
3392 struct nfs_server *server = NFS_SERVER(inode);
3393 struct page *pages[NFS4ACL_MAXPAGES];
3394 struct nfs_setaclargs arg = {
3395 .fh = NFS_FH(inode),
3396 .acl_pages = pages,
3397 .acl_len = buflen,
3398 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003399 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003400 struct rpc_message msg = {
3401 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3402 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003403 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003404 };
3405 int ret;
3406
3407 if (!nfs4_server_supports_acls(server))
3408 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003409 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003410 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003411 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003412 /*
3413 * Acl update can result in inode attribute update.
3414 * so mark the attribute cache invalid.
3415 */
3416 spin_lock(&inode->i_lock);
3417 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3418 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003419 nfs_access_zap_cache(inode);
3420 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003421 return ret;
3422}
3423
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003424static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3425{
3426 struct nfs4_exception exception = { };
3427 int err;
3428 do {
3429 err = nfs4_handle_exception(NFS_SERVER(inode),
3430 __nfs4_proc_set_acl(inode, buf, buflen),
3431 &exception);
3432 } while (exception.retry);
3433 return err;
3434}
3435
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003437nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003439 struct nfs_client *clp = server->nfs_client;
3440
3441 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 return 0;
3443 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003444 case -NFS4ERR_ADMIN_REVOKED:
3445 case -NFS4ERR_BAD_STATEID:
3446 case -NFS4ERR_OPENMODE:
3447 if (state == NULL)
3448 break;
3449 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003450 goto do_state_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003452 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003454 goto do_state_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003455#if defined(CONFIG_NFS_V4_1)
3456 case -NFS4ERR_BADSESSION:
3457 case -NFS4ERR_BADSLOT:
3458 case -NFS4ERR_BAD_HIGH_SLOT:
3459 case -NFS4ERR_DEADSESSION:
3460 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3461 case -NFS4ERR_SEQ_FALSE_RETRY:
3462 case -NFS4ERR_SEQ_MISORDERED:
3463 dprintk("%s ERROR %d, Reset session\n", __func__,
3464 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003465 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003466 task->tk_status = 0;
3467 return -EAGAIN;
3468#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003470 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003471 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003472 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3474 task->tk_status = 0;
3475 return -EAGAIN;
3476 case -NFS4ERR_OLD_STATEID:
3477 task->tk_status = 0;
3478 return -EAGAIN;
3479 }
3480 task->tk_status = nfs4_map_errors(task->tk_status);
3481 return 0;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003482do_state_recovery:
3483 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3484 nfs4_schedule_state_recovery(clp);
3485 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3486 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3487 task->tk_status = 0;
3488 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489}
3490
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003491int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3492 unsigned short port, struct rpc_cred *cred,
3493 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494{
3495 nfs4_verifier sc_verifier;
3496 struct nfs4_setclientid setclientid = {
3497 .sc_verifier = &sc_verifier,
3498 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003499 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 };
3501 struct rpc_message msg = {
3502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3503 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003504 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003505 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 };
Al Virobc4785c2006-10-19 23:28:51 -07003507 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 int loop = 0;
3509 int status;
3510
Al Virobc4785c2006-10-19 23:28:51 -07003511 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3513 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3514
3515 for(;;) {
3516 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003517 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003518 clp->cl_ipaddr,
3519 rpc_peeraddr2str(clp->cl_rpcclient,
3520 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003521 rpc_peeraddr2str(clp->cl_rpcclient,
3522 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003523 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 clp->cl_id_uniquifier);
3525 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003526 sizeof(setclientid.sc_netid),
3527 rpc_peeraddr2str(clp->cl_rpcclient,
3528 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003530 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 clp->cl_ipaddr, port >> 8, port & 255);
3532
3533 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3534 if (status != -NFS4ERR_CLID_INUSE)
3535 break;
3536 if (signalled())
3537 break;
3538 if (loop++ & 1)
J. Bruce Fields611c96c2010-12-13 19:05:46 -05003539 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 else
3541 if (++clp->cl_id_uniquifier == 0)
3542 break;
3543 }
3544 return status;
3545}
3546
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003547static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3548 struct nfs4_setclientid_res *arg,
3549 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550{
3551 struct nfs_fsinfo fsinfo;
3552 struct rpc_message msg = {
3553 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003554 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003556 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 };
3558 unsigned long now;
3559 int status;
3560
3561 now = jiffies;
3562 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3563 if (status == 0) {
3564 spin_lock(&clp->cl_lock);
3565 clp->cl_lease_time = fsinfo.lease_time * HZ;
3566 clp->cl_last_renewal = now;
3567 spin_unlock(&clp->cl_lock);
3568 }
3569 return status;
3570}
3571
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003572int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3573 struct nfs4_setclientid_res *arg,
3574 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003575{
Benny Halevy77e03672008-06-24 20:25:57 +03003576 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003577 int err;
3578 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003579 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003580 switch (err) {
3581 case 0:
3582 return err;
3583 case -NFS4ERR_RESOURCE:
3584 /* The IBM lawyers misread another document! */
3585 case -NFS4ERR_DELAY:
3586 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3587 }
3588 } while (err == 0);
3589 return err;
3590}
3591
Trond Myklebustfe650402006-01-03 09:55:18 +01003592struct nfs4_delegreturndata {
3593 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003594 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003595 struct nfs_fh fh;
3596 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003597 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003598 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003599 int rpc_status;
3600};
3601
Trond Myklebustfe650402006-01-03 09:55:18 +01003602static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3603{
3604 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003605
Trond Myklebust14516c32010-07-31 14:29:06 -04003606 if (!nfs4_sequence_done(task, &data->res.seq_res))
3607 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003608
Ricardo Labiaga79708862009-12-07 09:23:21 -05003609 switch (task->tk_status) {
3610 case -NFS4ERR_STALE_STATEID:
3611 case -NFS4ERR_EXPIRED:
3612 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003613 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003614 break;
3615 default:
3616 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3617 -EAGAIN) {
3618 nfs_restart_rpc(task, data->res.server->nfs_client);
3619 return;
3620 }
3621 }
3622 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003623}
3624
3625static void nfs4_delegreturn_release(void *calldata)
3626{
Trond Myklebustfe650402006-01-03 09:55:18 +01003627 kfree(calldata);
3628}
3629
Andy Adamson938e1012009-04-01 09:22:28 -04003630#if defined(CONFIG_NFS_V4_1)
3631static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3632{
3633 struct nfs4_delegreturndata *d_data;
3634
3635 d_data = (struct nfs4_delegreturndata *)data;
3636
Trond Myklebust035168a2010-06-16 09:52:26 -04003637 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003638 &d_data->args.seq_args,
3639 &d_data->res.seq_res, 1, task))
3640 return;
3641 rpc_call_start(task);
3642}
3643#endif /* CONFIG_NFS_V4_1 */
3644
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003645static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003646#if defined(CONFIG_NFS_V4_1)
3647 .rpc_call_prepare = nfs4_delegreturn_prepare,
3648#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003649 .rpc_call_done = nfs4_delegreturn_done,
3650 .rpc_release = nfs4_delegreturn_release,
3651};
3652
Trond Myklebuste6f81072008-01-24 18:14:34 -05003653static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003654{
3655 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003656 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003657 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003658 struct rpc_message msg = {
3659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3660 .rpc_cred = cred,
3661 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003662 struct rpc_task_setup task_setup_data = {
3663 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003664 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003665 .callback_ops = &nfs4_delegreturn_ops,
3666 .flags = RPC_TASK_ASYNC,
3667 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003668 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003669
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003670 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003671 if (data == NULL)
3672 return -ENOMEM;
3673 data->args.fhandle = &data->fh;
3674 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003675 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003676 nfs_copy_fh(&data->fh, NFS_FH(inode));
3677 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003678 data->res.fattr = &data->fattr;
3679 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003680 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003681 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003682 data->rpc_status = 0;
3683
Trond Myklebustc970aa82007-07-14 15:39:59 -04003684 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003685 msg.rpc_argp = &data->args;
3686 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003687 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003688 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003689 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003690 if (!issync)
3691 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003692 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003693 if (status != 0)
3694 goto out;
3695 status = data->rpc_status;
3696 if (status != 0)
3697 goto out;
3698 nfs_refresh_inode(inode, &data->fattr);
3699out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003700 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003701 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702}
3703
Trond Myklebuste6f81072008-01-24 18:14:34 -05003704int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705{
3706 struct nfs_server *server = NFS_SERVER(inode);
3707 struct nfs4_exception exception = { };
3708 int err;
3709 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003710 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 switch (err) {
3712 case -NFS4ERR_STALE_STATEID:
3713 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 case 0:
3715 return 0;
3716 }
3717 err = nfs4_handle_exception(server, err, &exception);
3718 } while (exception.retry);
3719 return err;
3720}
3721
3722#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3723#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3724
3725/*
3726 * sleep, with exponential backoff, and retry the LOCK operation.
3727 */
3728static unsigned long
3729nfs4_set_lock_task_retry(unsigned long timeout)
3730{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003731 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 timeout <<= 1;
3733 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3734 return NFS4_LOCK_MAXTIMEOUT;
3735 return timeout;
3736}
3737
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3739{
3740 struct inode *inode = state->inode;
3741 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003742 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003743 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003745 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003747 struct nfs_lockt_res res = {
3748 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 };
3750 struct rpc_message msg = {
3751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3752 .rpc_argp = &arg,
3753 .rpc_resp = &res,
3754 .rpc_cred = state->owner->so_cred,
3755 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 struct nfs4_lock_state *lsp;
3757 int status;
3758
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003759 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003760 status = nfs4_set_lock_state(state, request);
3761 if (status != 0)
3762 goto out;
3763 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003764 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003765 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003766 switch (status) {
3767 case 0:
3768 request->fl_type = F_UNLCK;
3769 break;
3770 case -NFS4ERR_DENIED:
3771 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003773 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003774out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 return status;
3776}
3777
3778static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3779{
3780 struct nfs4_exception exception = { };
3781 int err;
3782
3783 do {
3784 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3785 _nfs4_proc_getlk(state, cmd, request),
3786 &exception);
3787 } while (exception.retry);
3788 return err;
3789}
3790
3791static int do_vfs_lock(struct file *file, struct file_lock *fl)
3792{
3793 int res = 0;
3794 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3795 case FL_POSIX:
3796 res = posix_lock_file_wait(file, fl);
3797 break;
3798 case FL_FLOCK:
3799 res = flock_lock_file_wait(file, fl);
3800 break;
3801 default:
3802 BUG();
3803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 return res;
3805}
3806
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003807struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003808 struct nfs_locku_args arg;
3809 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003810 struct nfs4_lock_state *lsp;
3811 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003812 struct file_lock fl;
3813 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003814 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003815};
3816
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003817static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3818 struct nfs_open_context *ctx,
3819 struct nfs4_lock_state *lsp,
3820 struct nfs_seqid *seqid)
3821{
3822 struct nfs4_unlockdata *p;
3823 struct inode *inode = lsp->ls_state->inode;
3824
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003825 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003826 if (p == NULL)
3827 return NULL;
3828 p->arg.fh = NFS_FH(inode);
3829 p->arg.fl = &p->fl;
3830 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003831 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003832 p->arg.stateid = &lsp->ls_stateid;
3833 p->lsp = lsp;
3834 atomic_inc(&lsp->ls_count);
3835 /* Ensure we don't close file until we're done freeing locks! */
3836 p->ctx = get_nfs_open_context(ctx);
3837 memcpy(&p->fl, fl, sizeof(p->fl));
3838 p->server = NFS_SERVER(inode);
3839 return p;
3840}
3841
Trond Myklebust06f814a2006-01-03 09:55:07 +01003842static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003843{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003844 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003845 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003846 nfs4_put_lock_state(calldata->lsp);
3847 put_nfs_open_context(calldata->ctx);
3848 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003849}
3850
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003851static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003852{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003853 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003854
Trond Myklebust14516c32010-07-31 14:29:06 -04003855 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3856 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003857 switch (task->tk_status) {
3858 case 0:
3859 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003860 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003861 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003862 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003863 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003864 case -NFS4ERR_BAD_STATEID:
3865 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003866 case -NFS4ERR_STALE_STATEID:
3867 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003868 break;
3869 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003870 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003871 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003872 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003873 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003874}
3875
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003876static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003877{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003878 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003880 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003881 return;
3882 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003883 /* Note: exit _without_ running nfs4_locku_done */
3884 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003885 return;
3886 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003887 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04003888 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04003889 &calldata->arg.seq_args,
3890 &calldata->res.seq_res, 1, task))
3891 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003892 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893}
3894
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003895static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003896 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003897 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003898 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003899};
3900
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003901static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3902 struct nfs_open_context *ctx,
3903 struct nfs4_lock_state *lsp,
3904 struct nfs_seqid *seqid)
3905{
3906 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003907 struct rpc_message msg = {
3908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3909 .rpc_cred = ctx->cred,
3910 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003911 struct rpc_task_setup task_setup_data = {
3912 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003913 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003914 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003915 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003916 .flags = RPC_TASK_ASYNC,
3917 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003918
Frank Filz137d6ac2007-07-09 15:32:29 -07003919 /* Ensure this is an unlock - when canceling a lock, the
3920 * canceled lock is passed in, and it won't be an unlock.
3921 */
3922 fl->fl_type = F_UNLCK;
3923
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003924 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3925 if (data == NULL) {
3926 nfs_free_seqid(seqid);
3927 return ERR_PTR(-ENOMEM);
3928 }
3929
Trond Myklebust1174dd12010-12-21 10:52:24 -05003930 msg.rpc_argp = &data->arg;
3931 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003932 task_setup_data.callback_data = data;
3933 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003934}
3935
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3937{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003938 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003939 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003940 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003941 struct rpc_task *task;
3942 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003943 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
Trond Myklebust9b073572006-06-29 16:38:34 -04003945 status = nfs4_set_lock_state(state, request);
3946 /* Unlock _before_ we do the RPC call */
3947 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003948 down_read(&nfsi->rwsem);
3949 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3950 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003951 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003952 }
3953 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003954 if (status != 0)
3955 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003956 /* Is this a delegated lock? */
3957 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003958 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003959 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003960 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04003961 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003962 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003963 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003964 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003965 status = PTR_ERR(task);
3966 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003967 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003968 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003969 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003970out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003971 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003972 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973}
3974
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003975struct nfs4_lockdata {
3976 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003977 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003978 struct nfs4_lock_state *lsp;
3979 struct nfs_open_context *ctx;
3980 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003981 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003982 int rpc_status;
3983 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04003984 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003985};
3986
3987static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003988 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
3989 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003990{
3991 struct nfs4_lockdata *p;
3992 struct inode *inode = lsp->ls_state->inode;
3993 struct nfs_server *server = NFS_SERVER(inode);
3994
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003995 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003996 if (p == NULL)
3997 return NULL;
3998
3999 p->arg.fh = NFS_FH(inode);
4000 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004001 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004002 if (p->arg.open_seqid == NULL)
4003 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004004 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004005 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004006 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004007 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004008 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004009 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004010 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004011 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004012 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004013 atomic_inc(&lsp->ls_count);
4014 p->ctx = get_nfs_open_context(ctx);
4015 memcpy(&p->fl, fl, sizeof(p->fl));
4016 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004017out_free_seqid:
4018 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004019out_free:
4020 kfree(p);
4021 return NULL;
4022}
4023
4024static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4025{
4026 struct nfs4_lockdata *data = calldata;
4027 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
Harvey Harrison3110ff82008-05-02 13:42:44 -07004029 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004030 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4031 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004032 /* Do we need to do an open_to_lock_owner? */
4033 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004034 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4035 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004036 data->arg.open_stateid = &state->stateid;
4037 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004038 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004039 } else
4040 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004041 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004042 if (nfs4_setup_sequence(data->server,
4043 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004044 &data->res.seq_res, 1, task))
4045 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004046 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004047 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004048}
4049
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004050static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4051{
4052 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4053 nfs4_lock_prepare(task, calldata);
4054}
4055
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004056static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4057{
4058 struct nfs4_lockdata *data = calldata;
4059
Harvey Harrison3110ff82008-05-02 13:42:44 -07004060 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004061
Trond Myklebust14516c32010-07-31 14:29:06 -04004062 if (!nfs4_sequence_done(task, &data->res.seq_res))
4063 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004064
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004065 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004066 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004067 if (data->rpc_status == 0)
4068 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4069 else
4070 goto out;
4071 }
4072 if (data->rpc_status == 0) {
4073 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4074 sizeof(data->lsp->ls_stateid.data));
4075 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004076 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004077 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004078out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004079 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004080}
4081
4082static void nfs4_lock_release(void *calldata)
4083{
4084 struct nfs4_lockdata *data = calldata;
4085
Harvey Harrison3110ff82008-05-02 13:42:44 -07004086 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004087 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004088 if (data->cancelled != 0) {
4089 struct rpc_task *task;
4090 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4091 data->arg.lock_seqid);
4092 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004093 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004094 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004095 } else
4096 nfs_free_seqid(data->arg.lock_seqid);
4097 nfs4_put_lock_state(data->lsp);
4098 put_nfs_open_context(data->ctx);
4099 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004100 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004101}
4102
4103static const struct rpc_call_ops nfs4_lock_ops = {
4104 .rpc_call_prepare = nfs4_lock_prepare,
4105 .rpc_call_done = nfs4_lock_done,
4106 .rpc_release = nfs4_lock_release,
4107};
4108
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004109static const struct rpc_call_ops nfs4_recover_lock_ops = {
4110 .rpc_call_prepare = nfs4_recover_lock_prepare,
4111 .rpc_call_done = nfs4_lock_done,
4112 .rpc_release = nfs4_lock_release,
4113};
4114
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004115static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4116{
4117 struct nfs_client *clp = server->nfs_client;
4118 struct nfs4_state *state = lsp->ls_state;
4119
4120 switch (error) {
4121 case -NFS4ERR_ADMIN_REVOKED:
4122 case -NFS4ERR_BAD_STATEID:
4123 case -NFS4ERR_EXPIRED:
4124 if (new_lock_owner != 0 ||
4125 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4126 nfs4_state_mark_reclaim_nograce(clp, state);
4127 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004128 break;
4129 case -NFS4ERR_STALE_STATEID:
4130 if (new_lock_owner != 0 ||
4131 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4132 nfs4_state_mark_reclaim_reboot(clp, state);
4133 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004134 };
4135}
4136
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004137static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004138{
4139 struct nfs4_lockdata *data;
4140 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004141 struct rpc_message msg = {
4142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4143 .rpc_cred = state->owner->so_cred,
4144 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004145 struct rpc_task_setup task_setup_data = {
4146 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004147 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004148 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004149 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004150 .flags = RPC_TASK_ASYNC,
4151 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004152 int ret;
4153
Harvey Harrison3110ff82008-05-02 13:42:44 -07004154 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004155 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004156 fl->fl_u.nfs4_fl.owner,
4157 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004158 if (data == NULL)
4159 return -ENOMEM;
4160 if (IS_SETLKW(cmd))
4161 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004162 if (recovery_type > NFS_LOCK_NEW) {
4163 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004164 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004165 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4166 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004167 msg.rpc_argp = &data->arg;
4168 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004169 task_setup_data.callback_data = data;
4170 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004171 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004172 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004173 ret = nfs4_wait_for_completion_rpc_task(task);
4174 if (ret == 0) {
4175 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004176 if (ret)
4177 nfs4_handle_setlk_error(data->server, data->lsp,
4178 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004179 } else
4180 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004181 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004182 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004183 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184}
4185
4186static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4187{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004188 struct nfs_server *server = NFS_SERVER(state->inode);
4189 struct nfs4_exception exception = { };
4190 int err;
4191
4192 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004193 /* Cache the lock if possible... */
4194 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4195 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004196 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004197 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004198 break;
4199 nfs4_handle_exception(server, err, &exception);
4200 } while (exception.retry);
4201 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202}
4203
4204static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4205{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004206 struct nfs_server *server = NFS_SERVER(state->inode);
4207 struct nfs4_exception exception = { };
4208 int err;
4209
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004210 err = nfs4_set_lock_state(state, request);
4211 if (err != 0)
4212 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004213 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004214 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4215 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004216 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004217 switch (err) {
4218 default:
4219 goto out;
4220 case -NFS4ERR_GRACE:
4221 case -NFS4ERR_DELAY:
4222 nfs4_handle_exception(server, err, &exception);
4223 err = 0;
4224 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004225 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004226out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004227 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228}
4229
4230static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4231{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004232 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004233 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004234 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004236 if ((fl_flags & FL_POSIX) &&
4237 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4238 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004239 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004240 status = nfs4_set_lock_state(state, request);
4241 if (status != 0)
4242 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004243 request->fl_flags |= FL_ACCESS;
4244 status = do_vfs_lock(request->fl_file, request);
4245 if (status < 0)
4246 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004247 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004248 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004249 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004250 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004251 request->fl_flags = fl_flags & ~FL_SLEEP;
4252 status = do_vfs_lock(request->fl_file, request);
4253 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004254 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004255 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004256 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004257 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004258 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004259 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004260 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004261 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004262out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004263 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004264out:
4265 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 return status;
4267}
4268
4269static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4270{
4271 struct nfs4_exception exception = { };
4272 int err;
4273
4274 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004275 err = _nfs4_proc_setlk(state, cmd, request);
4276 if (err == -NFS4ERR_DENIED)
4277 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004279 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 } while (exception.retry);
4281 return err;
4282}
4283
4284static int
4285nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4286{
4287 struct nfs_open_context *ctx;
4288 struct nfs4_state *state;
4289 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4290 int status;
4291
4292 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004293 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 state = ctx->state;
4295
4296 if (request->fl_start < 0 || request->fl_end < 0)
4297 return -EINVAL;
4298
Trond Myklebustd9531262009-07-21 19:22:38 -04004299 if (IS_GETLK(cmd)) {
4300 if (state != NULL)
4301 return nfs4_proc_getlk(state, F_GETLK, request);
4302 return 0;
4303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
4305 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4306 return -EINVAL;
4307
Trond Myklebustd9531262009-07-21 19:22:38 -04004308 if (request->fl_type == F_UNLCK) {
4309 if (state != NULL)
4310 return nfs4_proc_unlck(state, cmd, request);
4311 return 0;
4312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
Trond Myklebustd9531262009-07-21 19:22:38 -04004314 if (state == NULL)
4315 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 do {
4317 status = nfs4_proc_setlk(state, cmd, request);
4318 if ((status != -EAGAIN) || IS_SETLK(cmd))
4319 break;
4320 timeout = nfs4_set_lock_task_retry(timeout);
4321 status = -ERESTARTSYS;
4322 if (signalled())
4323 break;
4324 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 return status;
4326}
4327
Trond Myklebust888e6942005-11-04 15:38:11 -05004328int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4329{
4330 struct nfs_server *server = NFS_SERVER(state->inode);
4331 struct nfs4_exception exception = { };
4332 int err;
4333
4334 err = nfs4_set_lock_state(state, fl);
4335 if (err != 0)
4336 goto out;
4337 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004338 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004339 switch (err) {
4340 default:
4341 printk(KERN_ERR "%s: unhandled error %d.\n",
4342 __func__, err);
4343 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004344 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004345 goto out;
4346 case -NFS4ERR_EXPIRED:
4347 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004348 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004349 case -NFS4ERR_BADSESSION:
4350 case -NFS4ERR_BADSLOT:
4351 case -NFS4ERR_BAD_HIGH_SLOT:
4352 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4353 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004354 nfs4_schedule_state_recovery(server->nfs_client);
4355 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004356 case -ERESTARTSYS:
4357 /*
4358 * The show must go on: exit, but mark the
4359 * stateid as needing recovery.
4360 */
4361 case -NFS4ERR_ADMIN_REVOKED:
4362 case -NFS4ERR_BAD_STATEID:
4363 case -NFS4ERR_OPENMODE:
4364 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4365 err = 0;
4366 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004367 case -EKEYEXPIRED:
4368 /*
4369 * User RPCSEC_GSS context has expired.
4370 * We cannot recover this stateid now, so
4371 * skip it and allow recovery thread to
4372 * proceed.
4373 */
4374 err = 0;
4375 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004376 case -ENOMEM:
4377 case -NFS4ERR_DENIED:
4378 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4379 err = 0;
4380 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004381 case -NFS4ERR_DELAY:
4382 break;
4383 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004384 err = nfs4_handle_exception(server, err, &exception);
4385 } while (exception.retry);
4386out:
4387 return err;
4388}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004389
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004390static void nfs4_release_lockowner_release(void *calldata)
4391{
4392 kfree(calldata);
4393}
4394
4395const struct rpc_call_ops nfs4_release_lockowner_ops = {
4396 .rpc_release = nfs4_release_lockowner_release,
4397};
4398
4399void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4400{
4401 struct nfs_server *server = lsp->ls_state->owner->so_server;
4402 struct nfs_release_lockowner_args *args;
4403 struct rpc_message msg = {
4404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4405 };
4406
4407 if (server->nfs_client->cl_mvops->minor_version != 0)
4408 return;
4409 args = kmalloc(sizeof(*args), GFP_NOFS);
4410 if (!args)
4411 return;
4412 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4413 args->lock_owner.id = lsp->ls_id.id;
4414 msg.rpc_argp = args;
4415 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4416}
4417
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004418#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4419
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004420static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4421 const void *buf, size_t buflen,
4422 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004423{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004424 if (strcmp(key, "") != 0)
4425 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004426
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004427 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004428}
4429
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004430static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4431 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004432{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004433 if (strcmp(key, "") != 0)
4434 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004435
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004436 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004437}
4438
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004439static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4440 size_t list_len, const char *name,
4441 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004442{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004443 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004444
J. Bruce Fields096455a2006-03-20 23:23:42 -05004445 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4446 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004447
4448 if (list && len <= list_len)
4449 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004450 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004451}
4452
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004453static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4454{
4455 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4456 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4457 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4458 return;
4459
4460 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4461 NFS_ATTR_FATTR_NLINK;
4462 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4463 fattr->nlink = 2;
4464}
4465
Trond Myklebust56659e92007-07-17 21:52:39 -04004466int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004467 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004468{
4469 struct nfs_server *server = NFS_SERVER(dir);
4470 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004471 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4472 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004473 };
4474 struct nfs4_fs_locations_arg args = {
4475 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004476 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004477 .page = page,
4478 .bitmask = bitmask,
4479 };
Benny Halevy22958462009-04-01 09:22:02 -04004480 struct nfs4_fs_locations_res res = {
4481 .fs_locations = fs_locations,
4482 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004483 struct rpc_message msg = {
4484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4485 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004486 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004487 };
4488 int status;
4489
Harvey Harrison3110ff82008-05-02 13:42:44 -07004490 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004491 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004492 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004493 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004494 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004495 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004496 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004497 return status;
4498}
4499
Andy Adamson557134a2009-04-01 09:21:53 -04004500#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004501/*
4502 * nfs4_proc_exchange_id()
4503 *
4504 * Since the clientid has expired, all compounds using sessions
4505 * associated with the stale clientid will be returning
4506 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4507 * be in some phase of session reset.
4508 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004509int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004510{
4511 nfs4_verifier verifier;
4512 struct nfs41_exchange_id_args args = {
4513 .client = clp,
4514 .flags = clp->cl_exchange_flags,
4515 };
4516 struct nfs41_exchange_id_res res = {
4517 .client = clp,
4518 };
4519 int status;
4520 struct rpc_message msg = {
4521 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4522 .rpc_argp = &args,
4523 .rpc_resp = &res,
4524 .rpc_cred = cred,
4525 };
4526 __be32 *p;
4527
4528 dprintk("--> %s\n", __func__);
4529 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004530
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004531 /* Remove server-only flags */
4532 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4533
Benny Halevy99fe60d2009-04-01 09:22:29 -04004534 p = (u32 *)verifier.data;
4535 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4536 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4537 args.verifier = &verifier;
4538
4539 while (1) {
4540 args.id_len = scnprintf(args.id, sizeof(args.id),
4541 "%s/%s %u",
4542 clp->cl_ipaddr,
4543 rpc_peeraddr2str(clp->cl_rpcclient,
4544 RPC_DISPLAY_ADDR),
4545 clp->cl_id_uniquifier);
4546
4547 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4548
Andy Adamson180b62a2010-03-02 13:19:36 -05004549 if (status != -NFS4ERR_CLID_INUSE)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004550 break;
4551
4552 if (signalled())
4553 break;
4554
4555 if (++clp->cl_id_uniquifier == 0)
4556 break;
4557 }
4558
4559 dprintk("<-- %s status= %d\n", __func__, status);
4560 return status;
4561}
4562
Andy Adamson2050f0c2009-04-01 09:22:30 -04004563struct nfs4_get_lease_time_data {
4564 struct nfs4_get_lease_time_args *args;
4565 struct nfs4_get_lease_time_res *res;
4566 struct nfs_client *clp;
4567};
4568
4569static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4570 void *calldata)
4571{
4572 int ret;
4573 struct nfs4_get_lease_time_data *data =
4574 (struct nfs4_get_lease_time_data *)calldata;
4575
4576 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004577 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004578 /* just setup sequence, do not trigger session recovery
4579 since we're invoked within one */
4580 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004581 &data->args->la_seq_args,
4582 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004583
4584 BUG_ON(ret == -EAGAIN);
4585 rpc_call_start(task);
4586 dprintk("<-- %s\n", __func__);
4587}
4588
4589/*
4590 * Called from nfs4_state_manager thread for session setup, so don't recover
4591 * from sequence operation or clientid errors.
4592 */
4593static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4594{
4595 struct nfs4_get_lease_time_data *data =
4596 (struct nfs4_get_lease_time_data *)calldata;
4597
4598 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004599 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4600 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004601 switch (task->tk_status) {
4602 case -NFS4ERR_DELAY:
4603 case -NFS4ERR_GRACE:
4604 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4605 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4606 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004607 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004608 return;
4609 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004610 dprintk("<-- %s\n", __func__);
4611}
4612
4613struct rpc_call_ops nfs4_get_lease_time_ops = {
4614 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4615 .rpc_call_done = nfs4_get_lease_time_done,
4616};
4617
4618int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4619{
4620 struct rpc_task *task;
4621 struct nfs4_get_lease_time_args args;
4622 struct nfs4_get_lease_time_res res = {
4623 .lr_fsinfo = fsinfo,
4624 };
4625 struct nfs4_get_lease_time_data data = {
4626 .args = &args,
4627 .res = &res,
4628 .clp = clp,
4629 };
4630 struct rpc_message msg = {
4631 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4632 .rpc_argp = &args,
4633 .rpc_resp = &res,
4634 };
4635 struct rpc_task_setup task_setup = {
4636 .rpc_client = clp->cl_rpcclient,
4637 .rpc_message = &msg,
4638 .callback_ops = &nfs4_get_lease_time_ops,
4639 .callback_data = &data
4640 };
4641 int status;
4642
Andy Adamson2050f0c2009-04-01 09:22:30 -04004643 dprintk("--> %s\n", __func__);
4644 task = rpc_run_task(&task_setup);
4645
4646 if (IS_ERR(task))
4647 status = PTR_ERR(task);
4648 else {
4649 status = task->tk_status;
4650 rpc_put_task(task);
4651 }
4652 dprintk("<-- %s return %d\n", __func__, status);
4653
4654 return status;
4655}
4656
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004657/*
4658 * Reset a slot table
4659 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004660static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4661 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004662{
Andy Adamson104aeba2010-01-14 17:45:10 -05004663 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004664 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004665 int ret = 0;
4666
Andy Adamson104aeba2010-01-14 17:45:10 -05004667 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4668 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004669
Andy Adamson104aeba2010-01-14 17:45:10 -05004670 /* Does the newly negotiated max_reqs match the existing slot table? */
4671 if (max_reqs != tbl->max_slots) {
4672 ret = -ENOMEM;
4673 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004674 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004675 if (!new)
4676 goto out;
4677 ret = 0;
4678 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004679 }
4680 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004681 if (new) {
4682 tbl->slots = new;
4683 tbl->max_slots = max_reqs;
4684 }
4685 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004686 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004687 spin_unlock(&tbl->slot_tbl_lock);
4688 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4689 tbl, tbl->slots, tbl->max_slots);
4690out:
4691 dprintk("<-- %s: return %d\n", __func__, ret);
4692 return ret;
4693}
4694
Andy Adamsonfc931582009-04-01 09:22:31 -04004695/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004696 * Reset the forechannel and backchannel slot tables
4697 */
4698static int nfs4_reset_slot_tables(struct nfs4_session *session)
4699{
4700 int status;
4701
4702 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004703 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004704 if (status)
4705 return status;
4706
4707 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004708 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004709 return status;
4710}
4711
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004712/* Destroy the slot table */
4713static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4714{
4715 if (session->fc_slot_table.slots != NULL) {
4716 kfree(session->fc_slot_table.slots);
4717 session->fc_slot_table.slots = NULL;
4718 }
4719 if (session->bc_slot_table.slots != NULL) {
4720 kfree(session->bc_slot_table.slots);
4721 session->bc_slot_table.slots = NULL;
4722 }
4723 return;
4724}
4725
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004726/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004727 * Initialize slot table
4728 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004729static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4730 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004731{
Andy Adamsonfc931582009-04-01 09:22:31 -04004732 struct nfs4_slot *slot;
4733 int ret = -ENOMEM;
4734
4735 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4736
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004737 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004738
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004739 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004740 if (!slot)
4741 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004742 ret = 0;
4743
4744 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004745 tbl->max_slots = max_slots;
4746 tbl->slots = slot;
4747 tbl->highest_used_slotid = -1; /* no slot is currently used */
4748 spin_unlock(&tbl->slot_tbl_lock);
4749 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4750 tbl, tbl->slots, tbl->max_slots);
4751out:
4752 dprintk("<-- %s: return %d\n", __func__, ret);
4753 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004754}
4755
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004756/*
4757 * Initialize the forechannel and backchannel tables
4758 */
4759static int nfs4_init_slot_tables(struct nfs4_session *session)
4760{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004761 struct nfs4_slot_table *tbl;
4762 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004763
Trond Myklebustf26468f2009-12-05 19:32:11 -05004764 tbl = &session->fc_slot_table;
4765 if (tbl->slots == NULL) {
4766 status = nfs4_init_slot_table(tbl,
4767 session->fc_attrs.max_reqs, 1);
4768 if (status)
4769 return status;
4770 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004771
Trond Myklebustf26468f2009-12-05 19:32:11 -05004772 tbl = &session->bc_slot_table;
4773 if (tbl->slots == NULL) {
4774 status = nfs4_init_slot_table(tbl,
4775 session->bc_attrs.max_reqs, 0);
4776 if (status)
4777 nfs4_destroy_slot_tables(session);
4778 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004779
4780 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004781}
4782
4783struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4784{
4785 struct nfs4_session *session;
4786 struct nfs4_slot_table *tbl;
4787
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004788 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004789 if (!session)
4790 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004791
Andy Adamson557134a2009-04-01 09:21:53 -04004792 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004793 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004794 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004795 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00004796 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004797
4798 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004799 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004800 spin_lock_init(&tbl->slot_tbl_lock);
4801 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00004802 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004803
Trond Myklebust1055d762010-06-16 09:52:27 -04004804 session->session_state = 1<<NFS4_SESSION_INITING;
4805
Andy Adamson557134a2009-04-01 09:21:53 -04004806 session->clp = clp;
4807 return session;
4808}
4809
4810void nfs4_destroy_session(struct nfs4_session *session)
4811{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004812 nfs4_proc_destroy_session(session);
4813 dprintk("%s Destroy backchannel for xprt %p\n",
4814 __func__, session->clp->cl_rpcclient->cl_xprt);
4815 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4816 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004817 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004818 kfree(session);
4819}
4820
Andy Adamsonfc931582009-04-01 09:22:31 -04004821/*
4822 * Initialize the values to be used by the client in CREATE_SESSION
4823 * If nfs4_init_session set the fore channel request and response sizes,
4824 * use them.
4825 *
4826 * Set the back channel max_resp_sz_cached to zero to force the client to
4827 * always set csa_cachethis to FALSE because the current implementation
4828 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4829 */
4830static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4831{
4832 struct nfs4_session *session = args->client->cl_session;
4833 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4834 mxresp_sz = session->fc_attrs.max_resp_sz;
4835
4836 if (mxrqst_sz == 0)
4837 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4838 if (mxresp_sz == 0)
4839 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4840 /* Fore channel attributes */
4841 args->fc_attrs.headerpadsz = 0;
4842 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4843 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004844 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4845 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4846
4847 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004848 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004849 __func__,
4850 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004851 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004852
4853 /* Back channel attributes */
4854 args->bc_attrs.headerpadsz = 0;
4855 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4856 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4857 args->bc_attrs.max_resp_sz_cached = 0;
4858 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4859 args->bc_attrs.max_reqs = 1;
4860
4861 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4862 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4863 __func__,
4864 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4865 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4866 args->bc_attrs.max_reqs);
4867}
4868
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004869static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04004870{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004871 struct nfs4_channel_attrs *sent = &args->fc_attrs;
4872 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
4873
4874 if (rcvd->headerpadsz > sent->headerpadsz)
4875 return -EINVAL;
4876 if (rcvd->max_resp_sz > sent->max_resp_sz)
4877 return -EINVAL;
4878 /*
4879 * Our requested max_ops is the minimum we need; we're not
4880 * prepared to break up compounds into smaller pieces than that.
4881 * So, no point even trying to continue if the server won't
4882 * cooperate:
4883 */
4884 if (rcvd->max_ops < sent->max_ops)
4885 return -EINVAL;
4886 if (rcvd->max_reqs == 0)
4887 return -EINVAL;
4888 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04004889}
4890
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004891static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
4892{
4893 struct nfs4_channel_attrs *sent = &args->bc_attrs;
4894 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04004895
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004896 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
4897 return -EINVAL;
4898 if (rcvd->max_resp_sz < sent->max_resp_sz)
4899 return -EINVAL;
4900 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
4901 return -EINVAL;
4902 /* These would render the backchannel useless: */
4903 if (rcvd->max_ops == 0)
4904 return -EINVAL;
4905 if (rcvd->max_reqs == 0)
4906 return -EINVAL;
4907 return 0;
4908}
Andy Adamson8d353012009-04-01 09:22:32 -04004909
Andy Adamson8d353012009-04-01 09:22:32 -04004910static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4911 struct nfs4_session *session)
4912{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004913 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04004914
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004915 ret = nfs4_verify_fore_channel_attrs(args, session);
4916 if (ret)
4917 return ret;
4918 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04004919}
4920
Andy Adamsonfc931582009-04-01 09:22:31 -04004921static int _nfs4_proc_create_session(struct nfs_client *clp)
4922{
4923 struct nfs4_session *session = clp->cl_session;
4924 struct nfs41_create_session_args args = {
4925 .client = clp,
4926 .cb_program = NFS4_CALLBACK,
4927 };
4928 struct nfs41_create_session_res res = {
4929 .client = clp,
4930 };
4931 struct rpc_message msg = {
4932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4933 .rpc_argp = &args,
4934 .rpc_resp = &res,
4935 };
4936 int status;
4937
4938 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004939 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004940
4941 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4942
Andy Adamson8d353012009-04-01 09:22:32 -04004943 if (!status)
4944 /* Verify the session's negotiated channel_attrs values */
4945 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004946 if (!status) {
4947 /* Increment the clientid slot sequence id */
4948 clp->cl_seqid++;
4949 }
4950
4951 return status;
4952}
4953
4954/*
4955 * Issues a CREATE_SESSION operation to the server.
4956 * It is the responsibility of the caller to verify the session is
4957 * expired before calling this routine.
4958 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004959int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004960{
4961 int status;
4962 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04004963 struct nfs4_session *session = clp->cl_session;
4964
4965 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4966
4967 status = _nfs4_proc_create_session(clp);
4968 if (status)
4969 goto out;
4970
Trond Myklebustf26468f2009-12-05 19:32:11 -05004971 /* Init and reset the fore channel */
4972 status = nfs4_init_slot_tables(session);
4973 dprintk("slot table initialization returned %d\n", status);
4974 if (status)
4975 goto out;
4976 status = nfs4_reset_slot_tables(session);
4977 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04004978 if (status)
4979 goto out;
4980
4981 ptr = (unsigned *)&session->sess_id.data[0];
4982 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4983 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04004984out:
4985 dprintk("<-- %s\n", __func__);
4986 return status;
4987}
4988
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004989/*
4990 * Issue the over-the-wire RPC DESTROY_SESSION.
4991 * The caller must serialize access to this routine.
4992 */
4993int nfs4_proc_destroy_session(struct nfs4_session *session)
4994{
4995 int status = 0;
4996 struct rpc_message msg;
4997
4998 dprintk("--> nfs4_proc_destroy_session\n");
4999
5000 /* session is still being setup */
5001 if (session->clp->cl_cons_state != NFS_CS_READY)
5002 return status;
5003
5004 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5005 msg.rpc_argp = session;
5006 msg.rpc_resp = NULL;
5007 msg.rpc_cred = NULL;
5008 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5009
5010 if (status)
5011 printk(KERN_WARNING
5012 "Got error %d from the server on DESTROY_SESSION. "
5013 "Session has been destroyed regardless...\n", status);
5014
5015 dprintk("<-- nfs4_proc_destroy_session\n");
5016 return status;
5017}
5018
Trond Myklebustfccba802009-07-21 16:48:07 -04005019int nfs4_init_session(struct nfs_server *server)
5020{
5021 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005022 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005023 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005024 int ret;
5025
5026 if (!nfs4_has_session(clp))
5027 return 0;
5028
Trond Myklebust1055d762010-06-16 09:52:27 -04005029 session = clp->cl_session;
5030 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5031 return 0;
5032
Andy Adamson68bf05e2009-12-15 12:55:02 -05005033 rsize = server->rsize;
5034 if (rsize == 0)
5035 rsize = NFS_MAX_FILE_IO_SIZE;
5036 wsize = server->wsize;
5037 if (wsize == 0)
5038 wsize = NFS_MAX_FILE_IO_SIZE;
5039
Andy Adamson68bf05e2009-12-15 12:55:02 -05005040 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5041 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005042
Trond Myklebustfccba802009-07-21 16:48:07 -04005043 ret = nfs4_recover_expired_lease(server);
5044 if (!ret)
5045 ret = nfs4_check_client_ready(clp);
5046 return ret;
5047}
5048
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005049/*
5050 * Renew the cl_session lease.
5051 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005052struct nfs4_sequence_data {
5053 struct nfs_client *clp;
5054 struct nfs4_sequence_args args;
5055 struct nfs4_sequence_res res;
5056};
5057
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005058static void nfs41_sequence_release(void *data)
5059{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005060 struct nfs4_sequence_data *calldata = data;
5061 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005062
Alexandros Batsakis71358402010-02-05 03:45:05 -08005063 if (atomic_read(&clp->cl_count) > 1)
5064 nfs4_schedule_state_renewal(clp);
5065 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005066 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005067}
5068
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005069static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5070{
5071 switch(task->tk_status) {
5072 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005073 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5074 return -EAGAIN;
5075 default:
5076 nfs4_schedule_state_recovery(clp);
5077 }
5078 return 0;
5079}
5080
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005081static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005082{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005083 struct nfs4_sequence_data *calldata = data;
5084 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005085
Trond Myklebust14516c32010-07-31 14:29:06 -04005086 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5087 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005088
5089 if (task->tk_status < 0) {
5090 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005091 if (atomic_read(&clp->cl_count) == 1)
5092 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005093
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005094 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5095 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005096 return;
5097 }
5098 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005099 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005100out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005101 dprintk("<-- %s\n", __func__);
5102}
5103
5104static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5105{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005106 struct nfs4_sequence_data *calldata = data;
5107 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005108 struct nfs4_sequence_args *args;
5109 struct nfs4_sequence_res *res;
5110
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005111 args = task->tk_msg.rpc_argp;
5112 res = task->tk_msg.rpc_resp;
5113
Trond Myklebust035168a2010-06-16 09:52:26 -04005114 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005115 return;
5116 rpc_call_start(task);
5117}
5118
5119static const struct rpc_call_ops nfs41_sequence_ops = {
5120 .rpc_call_done = nfs41_sequence_call_done,
5121 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005122 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005123};
5124
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005125static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005126{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005127 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005128 struct rpc_message msg = {
5129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5130 .rpc_cred = cred,
5131 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005132 struct rpc_task_setup task_setup_data = {
5133 .rpc_client = clp->cl_rpcclient,
5134 .rpc_message = &msg,
5135 .callback_ops = &nfs41_sequence_ops,
5136 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5137 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005138
Alexandros Batsakis71358402010-02-05 03:45:05 -08005139 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005140 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005141 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005142 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005143 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005144 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005145 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005146 msg.rpc_argp = &calldata->args;
5147 msg.rpc_resp = &calldata->res;
5148 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005149 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005150
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005151 return rpc_run_task(&task_setup_data);
5152}
5153
5154static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5155{
5156 struct rpc_task *task;
5157 int ret = 0;
5158
5159 task = _nfs41_proc_sequence(clp, cred);
5160 if (IS_ERR(task))
5161 ret = PTR_ERR(task);
5162 else
5163 rpc_put_task(task);
5164 dprintk("<-- %s status=%d\n", __func__, ret);
5165 return ret;
5166}
5167
5168static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5169{
5170 struct rpc_task *task;
5171 int ret;
5172
5173 task = _nfs41_proc_sequence(clp, cred);
5174 if (IS_ERR(task)) {
5175 ret = PTR_ERR(task);
5176 goto out;
5177 }
5178 ret = rpc_wait_for_completion_task(task);
5179 if (!ret)
5180 ret = task->tk_status;
5181 rpc_put_task(task);
5182out:
5183 dprintk("<-- %s status=%d\n", __func__, ret);
5184 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005185}
5186
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005187struct nfs4_reclaim_complete_data {
5188 struct nfs_client *clp;
5189 struct nfs41_reclaim_complete_args arg;
5190 struct nfs41_reclaim_complete_res res;
5191};
5192
5193static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5194{
5195 struct nfs4_reclaim_complete_data *calldata = data;
5196
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005197 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005198 if (nfs41_setup_sequence(calldata->clp->cl_session,
5199 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005200 &calldata->res.seq_res, 0, task))
5201 return;
5202
5203 rpc_call_start(task);
5204}
5205
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005206static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5207{
5208 switch(task->tk_status) {
5209 case 0:
5210 case -NFS4ERR_COMPLETE_ALREADY:
5211 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5212 break;
5213 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005214 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5215 return -EAGAIN;
5216 default:
5217 nfs4_schedule_state_recovery(clp);
5218 }
5219 return 0;
5220}
5221
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005222static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5223{
5224 struct nfs4_reclaim_complete_data *calldata = data;
5225 struct nfs_client *clp = calldata->clp;
5226 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5227
5228 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005229 if (!nfs41_sequence_done(task, res))
5230 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005231
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005232 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5233 rpc_restart_call_prepare(task);
5234 return;
5235 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005236 dprintk("<-- %s\n", __func__);
5237}
5238
5239static void nfs4_free_reclaim_complete_data(void *data)
5240{
5241 struct nfs4_reclaim_complete_data *calldata = data;
5242
5243 kfree(calldata);
5244}
5245
5246static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5247 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5248 .rpc_call_done = nfs4_reclaim_complete_done,
5249 .rpc_release = nfs4_free_reclaim_complete_data,
5250};
5251
5252/*
5253 * Issue a global reclaim complete.
5254 */
5255static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5256{
5257 struct nfs4_reclaim_complete_data *calldata;
5258 struct rpc_task *task;
5259 struct rpc_message msg = {
5260 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5261 };
5262 struct rpc_task_setup task_setup_data = {
5263 .rpc_client = clp->cl_rpcclient,
5264 .rpc_message = &msg,
5265 .callback_ops = &nfs4_reclaim_complete_call_ops,
5266 .flags = RPC_TASK_ASYNC,
5267 };
5268 int status = -ENOMEM;
5269
5270 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005271 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005272 if (calldata == NULL)
5273 goto out;
5274 calldata->clp = clp;
5275 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005276
5277 msg.rpc_argp = &calldata->arg;
5278 msg.rpc_resp = &calldata->res;
5279 task_setup_data.callback_data = calldata;
5280 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005281 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005282 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005283 goto out;
5284 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005285 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005286 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005287out:
5288 dprintk("<-- %s status=%d\n", __func__, status);
5289 return status;
5290}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005291
5292static void
5293nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5294{
5295 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005296 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005297
5298 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005299 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5300 * right now covering the LAYOUTGET we are about to send.
5301 * However, that is not so catastrophic, and there seems
5302 * to be no way to prevent it completely.
5303 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005304 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5305 &lgp->res.seq_res, 0, task))
5306 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005307 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5308 NFS_I(lgp->args.inode)->layout,
5309 lgp->args.ctx->state)) {
5310 rpc_exit(task, NFS4_OK);
5311 return;
5312 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005313 rpc_call_start(task);
5314}
5315
5316static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5317{
5318 struct nfs4_layoutget *lgp = calldata;
5319 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5320
5321 dprintk("--> %s\n", __func__);
5322
5323 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5324 return;
5325
5326 switch (task->tk_status) {
5327 case 0:
5328 break;
5329 case -NFS4ERR_LAYOUTTRYLATER:
5330 case -NFS4ERR_RECALLCONFLICT:
5331 task->tk_status = -NFS4ERR_DELAY;
5332 /* Fall through */
5333 default:
5334 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5335 rpc_restart_call_prepare(task);
5336 return;
5337 }
5338 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005339 dprintk("<-- %s\n", __func__);
5340}
5341
5342static void nfs4_layoutget_release(void *calldata)
5343{
5344 struct nfs4_layoutget *lgp = calldata;
5345
5346 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005347 if (lgp->res.layout.buf != NULL)
5348 free_page((unsigned long) lgp->res.layout.buf);
5349 put_nfs_open_context(lgp->args.ctx);
5350 kfree(calldata);
5351 dprintk("<-- %s\n", __func__);
5352}
5353
5354static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5355 .rpc_call_prepare = nfs4_layoutget_prepare,
5356 .rpc_call_done = nfs4_layoutget_done,
5357 .rpc_release = nfs4_layoutget_release,
5358};
5359
5360int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5361{
5362 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5363 struct rpc_task *task;
5364 struct rpc_message msg = {
5365 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5366 .rpc_argp = &lgp->args,
5367 .rpc_resp = &lgp->res,
5368 };
5369 struct rpc_task_setup task_setup_data = {
5370 .rpc_client = server->client,
5371 .rpc_message = &msg,
5372 .callback_ops = &nfs4_layoutget_call_ops,
5373 .callback_data = lgp,
5374 .flags = RPC_TASK_ASYNC,
5375 };
5376 int status = 0;
5377
5378 dprintk("--> %s\n", __func__);
5379
5380 lgp->res.layout.buf = (void *)__get_free_page(GFP_NOFS);
5381 if (lgp->res.layout.buf == NULL) {
5382 nfs4_layoutget_release(lgp);
5383 return -ENOMEM;
5384 }
5385
5386 lgp->res.seq_res.sr_slot = NULL;
5387 task = rpc_run_task(&task_setup_data);
5388 if (IS_ERR(task))
5389 return PTR_ERR(task);
5390 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005391 if (status == 0)
5392 status = task->tk_status;
5393 if (status == 0)
5394 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005395 rpc_put_task(task);
5396 dprintk("<-- %s status=%d\n", __func__, status);
5397 return status;
5398}
5399
5400static int
5401_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5402{
5403 struct nfs4_getdeviceinfo_args args = {
5404 .pdev = pdev,
5405 };
5406 struct nfs4_getdeviceinfo_res res = {
5407 .pdev = pdev,
5408 };
5409 struct rpc_message msg = {
5410 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5411 .rpc_argp = &args,
5412 .rpc_resp = &res,
5413 };
5414 int status;
5415
5416 dprintk("--> %s\n", __func__);
5417 status = nfs4_call_sync(server, &msg, &args, &res, 0);
5418 dprintk("<-- %s status=%d\n", __func__, status);
5419
5420 return status;
5421}
5422
5423int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5424{
5425 struct nfs4_exception exception = { };
5426 int err;
5427
5428 do {
5429 err = nfs4_handle_exception(server,
5430 _nfs4_proc_getdeviceinfo(server, pdev),
5431 &exception);
5432 } while (exception.retry);
5433 return err;
5434}
5435EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5436
Andy Adamson557134a2009-04-01 09:21:53 -04005437#endif /* CONFIG_NFS_V4_1 */
5438
Andy Adamson591d71c2009-04-01 09:22:47 -04005439struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005440 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005441 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 .recover_open = nfs4_open_reclaim,
5443 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005444 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005445 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446};
5447
Andy Adamson591d71c2009-04-01 09:22:47 -04005448#if defined(CONFIG_NFS_V4_1)
5449struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5450 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5451 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5452 .recover_open = nfs4_open_reclaim,
5453 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005454 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005455 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005456 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005457};
5458#endif /* CONFIG_NFS_V4_1 */
5459
5460struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005461 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005462 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 .recover_open = nfs4_open_expired,
5464 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005465 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005466 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467};
5468
Andy Adamson591d71c2009-04-01 09:22:47 -04005469#if defined(CONFIG_NFS_V4_1)
5470struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5471 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5472 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5473 .recover_open = nfs4_open_expired,
5474 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005475 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005476 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005477};
5478#endif /* CONFIG_NFS_V4_1 */
5479
Benny Halevy29fba382009-04-01 09:22:44 -04005480struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5481 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005482 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005483 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005484};
5485
5486#if defined(CONFIG_NFS_V4_1)
5487struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5488 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005489 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005490 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005491};
5492#endif
5493
Trond Myklebust97dc1352010-06-16 09:52:26 -04005494static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5495 .minor_version = 0,
5496 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005497 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005498 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5499 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5500 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005501};
5502
5503#if defined(CONFIG_NFS_V4_1)
5504static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5505 .minor_version = 1,
5506 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005507 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005508 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5509 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5510 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005511};
5512#endif
5513
Trond Myklebust97dc1352010-06-16 09:52:26 -04005514const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5515 [0] = &nfs_v4_0_minor_ops,
5516#if defined(CONFIG_NFS_V4_1)
5517 [1] = &nfs_v4_1_minor_ops,
5518#endif
5519};
5520
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005521static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005522 .permission = nfs_permission,
5523 .getattr = nfs_getattr,
5524 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005525 .getxattr = generic_getxattr,
5526 .setxattr = generic_setxattr,
5527 .listxattr = generic_listxattr,
5528 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005529};
5530
David Howells509de812006-08-22 20:06:11 -04005531const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 .version = 4, /* protocol version */
5533 .dentry_ops = &nfs4_dentry_operations,
5534 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005535 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 .getroot = nfs4_proc_get_root,
5537 .getattr = nfs4_proc_getattr,
5538 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005539 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 .lookup = nfs4_proc_lookup,
5541 .access = nfs4_proc_access,
5542 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 .create = nfs4_proc_create,
5544 .remove = nfs4_proc_remove,
5545 .unlink_setup = nfs4_proc_unlink_setup,
5546 .unlink_done = nfs4_proc_unlink_done,
5547 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04005548 .rename_setup = nfs4_proc_rename_setup,
5549 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 .link = nfs4_proc_link,
5551 .symlink = nfs4_proc_symlink,
5552 .mkdir = nfs4_proc_mkdir,
5553 .rmdir = nfs4_proc_remove,
5554 .readdir = nfs4_proc_readdir,
5555 .mknod = nfs4_proc_mknod,
5556 .statfs = nfs4_proc_statfs,
5557 .fsinfo = nfs4_proc_fsinfo,
5558 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005559 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 .decode_dirent = nfs4_decode_dirent,
5561 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005562 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005564 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005566 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005568 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005569 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04005570 .open_context = nfs4_atomic_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571};
5572
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005573static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
5574 .prefix = XATTR_NAME_NFSV4_ACL,
5575 .list = nfs4_xattr_list_nfs4_acl,
5576 .get = nfs4_xattr_get_nfs4_acl,
5577 .set = nfs4_xattr_set_nfs4_acl,
5578};
5579
5580const struct xattr_handler *nfs4_xattr_handlers[] = {
5581 &nfs4_xattr_nfs4_acl_handler,
5582 NULL
5583};
5584
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585/*
5586 * Local variables:
5587 * c-basic-offset: 8
5588 * End:
5589 */