blob: a5f1edb45b47068a60bb0ce08a243a5ed8a694c1 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Trond Myklebust4ce79712005-06-22 17:16:21 +000053#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050055#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050056#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040057#include "callback.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define NFSDBG_FACILITY NFSDBG_PROC
60
Trond Myklebust2066fe82006-09-15 08:30:46 -040061#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define NFS4_POLL_RETRY_MAX (15*HZ)
63
Trond Myklebusta78cb572009-08-09 15:06:19 -040064#define NFS4_MAX_LOOP_ON_RECOVER (10)
65
Trond Myklebustcdd4e682006-01-03 09:55:12 +010066struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010067static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080068static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050070static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040071static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
72static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040073static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
74 struct nfs_fattr *fattr, struct iattr *sattr,
75 struct nfs4_state *state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050078static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Trond Myklebust52567b02009-10-23 14:46:42 -040080 if (err >= -1000)
81 return err;
82 switch (err) {
83 case -NFS4ERR_RESOURCE:
84 return -EREMOTEIO;
85 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070087 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040088 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
Trond Myklebust52567b02009-10-23 14:46:42 -040090 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93/*
94 * This is our standard bitmap for GETATTR requests.
95 */
96const u32 nfs4_fattr_bitmap[2] = {
97 FATTR4_WORD0_TYPE
98 | FATTR4_WORD0_CHANGE
99 | FATTR4_WORD0_SIZE
100 | FATTR4_WORD0_FSID
101 | FATTR4_WORD0_FILEID,
102 FATTR4_WORD1_MODE
103 | FATTR4_WORD1_NUMLINKS
104 | FATTR4_WORD1_OWNER
105 | FATTR4_WORD1_OWNER_GROUP
106 | FATTR4_WORD1_RAWDEV
107 | FATTR4_WORD1_SPACE_USED
108 | FATTR4_WORD1_TIME_ACCESS
109 | FATTR4_WORD1_TIME_METADATA
110 | FATTR4_WORD1_TIME_MODIFY
111};
112
113const u32 nfs4_statfs_bitmap[2] = {
114 FATTR4_WORD0_FILES_AVAIL
115 | FATTR4_WORD0_FILES_FREE
116 | FATTR4_WORD0_FILES_TOTAL,
117 FATTR4_WORD1_SPACE_AVAIL
118 | FATTR4_WORD1_SPACE_FREE
119 | FATTR4_WORD1_SPACE_TOTAL
120};
121
Trond Myklebust4ce79712005-06-22 17:16:21 +0000122const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 FATTR4_WORD0_MAXLINK
124 | FATTR4_WORD0_MAXNAME,
125 0
126};
127
128const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
129 | FATTR4_WORD0_MAXREAD
130 | FATTR4_WORD0_MAXWRITE
131 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700132 FATTR4_WORD1_TIME_DELTA
Andy Adamson504913f2010-10-20 00:17:57 -0400133 | FATTR4_WORD1_FS_LAYOUT_TYPES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
Manoj Naik830b8e32006-06-09 09:34:25 -0400136const u32 nfs4_fs_locations_bitmap[2] = {
137 FATTR4_WORD0_TYPE
138 | FATTR4_WORD0_CHANGE
139 | FATTR4_WORD0_SIZE
140 | FATTR4_WORD0_FSID
141 | FATTR4_WORD0_FILEID
142 | FATTR4_WORD0_FS_LOCATIONS,
143 FATTR4_WORD1_MODE
144 | FATTR4_WORD1_NUMLINKS
145 | FATTR4_WORD1_OWNER
146 | FATTR4_WORD1_OWNER_GROUP
147 | FATTR4_WORD1_RAWDEV
148 | FATTR4_WORD1_SPACE_USED
149 | FATTR4_WORD1_TIME_ACCESS
150 | FATTR4_WORD1_TIME_METADATA
151 | FATTR4_WORD1_TIME_MODIFY
152 | FATTR4_WORD1_MOUNTED_ON_FILEID
153};
154
Al Virobc4785c2006-10-19 23:28:51 -0700155static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 struct nfs4_readdir_arg *readdir)
157{
Al Viro0dbb4c62006-10-19 23:28:49 -0700158 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 BUG_ON(readdir->count < 80);
161 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000162 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
164 return;
165 }
166
167 readdir->cookie = 0;
168 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
169 if (cookie == 2)
170 return;
171
172 /*
173 * NFSv4 servers do not return entries for '.' and '..'
174 * Therefore, we fake these entries here. We let '.'
175 * have cookie 0 and '..' have cookie 1. Note that
176 * when talking to the server, we always send cookie 0
177 * instead of 1 or 2.
178 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700179 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 if (cookie == 0) {
182 *p++ = xdr_one; /* next */
183 *p++ = xdr_zero; /* cookie, first word */
184 *p++ = xdr_one; /* cookie, second word */
185 *p++ = xdr_one; /* entry len */
186 memcpy(p, ".\0\0\0", 4); /* entry */
187 p++;
188 *p++ = xdr_one; /* bitmap length */
189 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
190 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400191 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193
194 *p++ = xdr_one; /* next */
195 *p++ = xdr_zero; /* cookie, first word */
196 *p++ = xdr_two; /* cookie, second word */
197 *p++ = xdr_two; /* entry len */
198 memcpy(p, "..\0\0", 4); /* entry */
199 p++;
200 *p++ = xdr_one; /* bitmap length */
201 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
202 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400203 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 readdir->pgbase = (char *)p - (char *)start;
206 readdir->count -= readdir->pgbase;
207 kunmap_atomic(start, KM_USER0);
208}
209
Trond Myklebust65de8722008-12-23 15:21:44 -0500210static int nfs4_wait_clnt_recover(struct nfs_client *clp)
211{
212 int res;
213
214 might_sleep();
215
Trond Myklebuste005e802008-12-23 15:21:48 -0500216 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400217 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500218 return res;
219}
220
221static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
222{
223 int res = 0;
224
225 might_sleep();
226
227 if (*timeout <= 0)
228 *timeout = NFS4_POLL_RETRY_MIN;
229 if (*timeout > NFS4_POLL_RETRY_MAX)
230 *timeout = NFS4_POLL_RETRY_MAX;
231 schedule_timeout_killable(*timeout);
232 if (fatal_signal_pending(current))
233 res = -ERESTARTSYS;
234 *timeout <<= 1;
235 return res;
236}
237
238/* This is the error handling routine for processes that are allowed
239 * to sleep.
240 */
241static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
242{
243 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500244 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500245 int ret = errorcode;
246
247 exception->retry = 0;
248 switch(errorcode) {
249 case 0:
250 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500251 case -NFS4ERR_ADMIN_REVOKED:
252 case -NFS4ERR_BAD_STATEID:
253 case -NFS4ERR_OPENMODE:
254 if (state == NULL)
255 break;
256 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500257 goto do_state_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500258 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500259 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500260 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500261 goto do_state_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500262#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400263 case -NFS4ERR_BADSESSION:
264 case -NFS4ERR_BADSLOT:
265 case -NFS4ERR_BAD_HIGH_SLOT:
266 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
267 case -NFS4ERR_DEADSESSION:
268 case -NFS4ERR_SEQ_FALSE_RETRY:
269 case -NFS4ERR_SEQ_MISORDERED:
270 dprintk("%s ERROR: %d Reset session\n", __func__,
271 errorcode);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500272 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -0400273 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500274 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500275#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500276 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500277 if (exception->timeout > HZ) {
278 /* We have retried a decent amount, time to
279 * fail
280 */
281 ret = -EBUSY;
282 break;
283 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500284 case -NFS4ERR_GRACE:
285 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500286 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500287 ret = nfs4_delay(server->client, &exception->timeout);
288 if (ret != 0)
289 break;
290 case -NFS4ERR_OLD_STATEID:
291 exception->retry = 1;
292 }
293 /* We failed to handle the error */
294 return nfs4_map_errors(ret);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500295do_state_recovery:
296 nfs4_schedule_state_recovery(clp);
297 ret = nfs4_wait_clnt_recover(clp);
298 if (ret == 0)
299 exception->retry = 1;
300 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500301}
302
303
Trond Myklebust452e9352010-07-31 14:29:06 -0400304static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 spin_lock(&clp->cl_lock);
307 if (time_before(clp->cl_last_renewal,timestamp))
308 clp->cl_last_renewal = timestamp;
309 spin_unlock(&clp->cl_lock);
310}
311
Trond Myklebust452e9352010-07-31 14:29:06 -0400312static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
313{
314 do_renew_lease(server->nfs_client, timestamp);
315}
316
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400317#if defined(CONFIG_NFS_V4_1)
318
Benny Halevy510b8172009-04-01 09:22:14 -0400319/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400320 * nfs4_free_slot - free a slot and efficiently update slot table.
321 *
322 * freeing a slot is trivially done by clearing its respective bit
323 * in the bitmap.
324 * If the freed slotid equals highest_used_slotid we want to update it
325 * so that the server would be able to size down the slot table if needed,
326 * otherwise we know that the highest_used_slotid is still in use.
327 * When updating highest_used_slotid there may be "holes" in the bitmap
328 * so we need to scan down from highest_used_slotid to 0 looking for the now
329 * highest slotid in use.
330 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500331 *
332 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400333 */
334static void
Benny Halevydfb4f3092010-09-24 09:17:01 -0400335nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400336{
Benny Halevydfb4f3092010-09-24 09:17:01 -0400337 int free_slotid = free_slot - tbl->slots;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400338 int slotid = free_slotid;
339
Benny Halevydfb4f3092010-09-24 09:17:01 -0400340 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400341 /* clear used bit in bitmap */
342 __clear_bit(slotid, tbl->used_slots);
343
344 /* update highest_used_slotid when it is freed */
345 if (slotid == tbl->highest_used_slotid) {
346 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500347 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400348 tbl->highest_used_slotid = slotid;
349 else
350 tbl->highest_used_slotid = -1;
351 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400352 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
353 free_slotid, tbl->highest_used_slotid);
354}
355
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800356/*
357 * Signal state manager thread if session is drained
358 */
359static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
360{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800361 struct rpc_task *task;
362
Trond Myklebusta2118c32010-06-16 09:52:26 -0400363 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800364 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
365 if (task)
366 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800367 return;
368 }
369
370 if (ses->fc_slot_table.highest_used_slotid != -1)
371 return;
372
373 dprintk("%s COMPLETE: Session Drained\n", __func__);
374 complete(&ses->complete);
375}
376
Trond Myklebustd185a332010-06-16 09:52:25 -0400377static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400378{
379 struct nfs4_slot_table *tbl;
380
Trond Myklebustd185a332010-06-16 09:52:25 -0400381 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400382 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400383 /* just wake up the next guy waiting since
384 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500385 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500386 return;
Andy Adamson13615872009-04-01 09:22:17 -0400387 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500388
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500389 spin_lock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400390 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustd185a332010-06-16 09:52:25 -0400391 nfs41_check_drain_session_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500392 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400393 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400394}
395
Trond Myklebust14516c32010-07-31 14:29:06 -0400396static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400397{
398 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400399 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400400
401 /*
402 * sr_status remains 1 if an RPC level error occurred. The server
403 * may or may not have processed the sequence operation..
404 * Proceed as if the server received and processed the sequence
405 * operation.
406 */
407 if (res->sr_status == 1)
408 res->sr_status = NFS_OK;
409
410 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400411 if (!res->sr_slot)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400412 goto out;
413
Andy Adamson691daf32009-12-04 15:55:39 -0500414 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400415 switch (res->sr_status) {
416 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400417 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400418 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400419 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400420 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400421 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500422 /* Check sequence flags */
Alexandros Batsakis71358402010-02-05 03:45:05 -0800423 if (atomic_read(&clp->cl_count) > 1)
424 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust14516c32010-07-31 14:29:06 -0400425 break;
426 case -NFS4ERR_DELAY:
427 /* The server detected a resend of the RPC call and
428 * returned NFS4ERR_DELAY as per Section 2.10.6.2
429 * of RFC5661.
430 */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400431 dprintk("%s: slot=%ld seq=%d: Operation in progress\n",
432 __func__,
433 res->sr_slot - res->sr_session->fc_slot_table.slots,
434 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400435 goto out_retry;
436 default:
437 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400438 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400439 }
440out:
441 /* The session may be reset by one of the error handlers. */
442 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400443 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400444 return 1;
445out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400446 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400447 goto out;
448 rpc_delay(task, NFS4_POLL_RETRY_MAX);
449 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400450}
451
Trond Myklebust14516c32010-07-31 14:29:06 -0400452static int nfs4_sequence_done(struct rpc_task *task,
453 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400454{
Trond Myklebust14516c32010-07-31 14:29:06 -0400455 if (res->sr_session == NULL)
456 return 1;
457 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400458}
459
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400460/*
Benny Halevy510b8172009-04-01 09:22:14 -0400461 * nfs4_find_slot - efficiently look for a free slot
462 *
463 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
464 * If found, we mark the slot as used, update the highest_used_slotid,
465 * and respectively set up the sequence operation args.
466 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400467 *
468 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400469 */
470static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800471nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400472{
473 int slotid;
474 u8 ret_id = NFS4_MAX_SLOT_TABLE;
475 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
476
Benny Halevy510b8172009-04-01 09:22:14 -0400477 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
478 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
479 tbl->max_slots);
480 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
481 if (slotid >= tbl->max_slots)
482 goto out;
483 __set_bit(slotid, tbl->used_slots);
484 if (slotid > tbl->highest_used_slotid)
485 tbl->highest_used_slotid = slotid;
486 ret_id = slotid;
487out:
488 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
489 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400490 return ret_id;
491}
492
Andy Adamsonce5039c2009-04-01 09:22:13 -0400493static int nfs41_setup_sequence(struct nfs4_session *session,
494 struct nfs4_sequence_args *args,
495 struct nfs4_sequence_res *res,
496 int cache_reply,
497 struct rpc_task *task)
498{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400499 struct nfs4_slot *slot;
500 struct nfs4_slot_table *tbl;
501 u8 slotid;
502
503 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400504 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400505 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400506 return 0;
507
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400508 tbl = &session->fc_slot_table;
509
510 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400511 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800512 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500513 /*
514 * The state manager will wait until the slot table is empty.
515 * Schedule the reset thread
516 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500517 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400518 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500519 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500520 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400521 }
522
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800523 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
524 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
525 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
526 spin_unlock(&tbl->slot_tbl_lock);
527 dprintk("%s enforce FIFO order\n", __func__);
528 return -EAGAIN;
529 }
530
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800531 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400532 if (slotid == NFS4_MAX_SLOT_TABLE) {
533 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
534 spin_unlock(&tbl->slot_tbl_lock);
535 dprintk("<-- %s: no free slots\n", __func__);
536 return -EAGAIN;
537 }
538 spin_unlock(&tbl->slot_tbl_lock);
539
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800540 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400541 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400542 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400543 args->sa_slotid = slotid;
544 args->sa_cache_this = cache_reply;
545
546 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
547
Benny Halevy99fe60d2009-04-01 09:22:29 -0400548 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400549 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400550 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400551 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400552 /*
553 * sr_status is only set in decode_sequence, and so will remain
554 * set to 1 if an rpc level failure occurs.
555 */
556 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400557 return 0;
558}
559
Trond Myklebust035168a2010-06-16 09:52:26 -0400560int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400561 struct nfs4_sequence_args *args,
562 struct nfs4_sequence_res *res,
563 int cache_reply,
564 struct rpc_task *task)
565{
Trond Myklebust035168a2010-06-16 09:52:26 -0400566 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400567 int ret = 0;
568
Trond Myklebust035168a2010-06-16 09:52:26 -0400569 if (session == NULL) {
570 args->sa_session = NULL;
571 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400572 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400573 }
574
Benny Halevydfb4f3092010-09-24 09:17:01 -0400575 dprintk("--> %s clp %p session %p sr_slot %ld\n",
576 __func__, session->clp, session, res->sr_slot ?
577 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400578
579 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400580 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400581out:
582 dprintk("<-- %s status=%d\n", __func__, ret);
583 return ret;
584}
585
586struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400587 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400588 struct nfs4_sequence_args *seq_args;
589 struct nfs4_sequence_res *seq_res;
590 int cache_reply;
591};
592
593static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
594{
595 struct nfs41_call_sync_data *data = calldata;
596
Trond Myklebust035168a2010-06-16 09:52:26 -0400597 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
598
599 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400600 data->seq_res, data->cache_reply, task))
601 return;
602 rpc_call_start(task);
603}
604
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800605static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
606{
607 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
608 nfs41_call_sync_prepare(task, calldata);
609}
610
Andy Adamson69ab40c2009-04-01 09:22:19 -0400611static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
612{
613 struct nfs41_call_sync_data *data = calldata;
614
Trond Myklebust14516c32010-07-31 14:29:06 -0400615 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400616}
617
Andy Adamsonce5039c2009-04-01 09:22:13 -0400618struct rpc_call_ops nfs41_call_sync_ops = {
619 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400620 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400621};
622
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800623struct rpc_call_ops nfs41_call_priv_sync_ops = {
624 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
625 .rpc_call_done = nfs41_call_sync_done,
626};
627
Trond Myklebust035168a2010-06-16 09:52:26 -0400628static int nfs4_call_sync_sequence(struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400629 struct rpc_message *msg,
630 struct nfs4_sequence_args *args,
631 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800632 int cache_reply,
633 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400634{
635 int ret;
636 struct rpc_task *task;
637 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400638 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400639 .seq_args = args,
640 .seq_res = res,
641 .cache_reply = cache_reply,
642 };
643 struct rpc_task_setup task_setup = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400644 .rpc_client = server->client,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400645 .rpc_message = msg,
646 .callback_ops = &nfs41_call_sync_ops,
647 .callback_data = &data
648 };
649
Benny Halevydfb4f3092010-09-24 09:17:01 -0400650 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800651 if (privileged)
652 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400653 task = rpc_run_task(&task_setup);
654 if (IS_ERR(task))
655 ret = PTR_ERR(task);
656 else {
657 ret = task->tk_status;
658 rpc_put_task(task);
659 }
660 return ret;
661}
662
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400663int _nfs4_call_sync_session(struct nfs_server *server,
664 struct rpc_message *msg,
665 struct nfs4_sequence_args *args,
666 struct nfs4_sequence_res *res,
667 int cache_reply)
668{
Trond Myklebust035168a2010-06-16 09:52:26 -0400669 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400670}
671
Trond Myklebustdf896452010-06-16 09:52:26 -0400672#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400673static int nfs4_sequence_done(struct rpc_task *task,
674 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400675{
Trond Myklebust14516c32010-07-31 14:29:06 -0400676 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400677}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400678#endif /* CONFIG_NFS_V4_1 */
679
680int _nfs4_call_sync(struct nfs_server *server,
681 struct rpc_message *msg,
682 struct nfs4_sequence_args *args,
683 struct nfs4_sequence_res *res,
684 int cache_reply)
685{
Benny Halevyf3752972009-04-01 09:22:04 -0400686 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400687 return rpc_call_sync(server->client, msg, 0);
688}
689
690#define nfs4_call_sync(server, msg, args, res, cache_reply) \
Trond Myklebust97dc1352010-06-16 09:52:26 -0400691 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400692 &(res)->seq_res, (cache_reply))
693
Trond Myklebust38478b22006-05-25 01:40:57 -0400694static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Trond Myklebust38478b22006-05-25 01:40:57 -0400696 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Trond Myklebust38478b22006-05-25 01:40:57 -0400698 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400699 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
700 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400701 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400702 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400703 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100706struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400707 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100708 struct nfs_openargs o_arg;
709 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100710 struct nfs_open_confirmargs c_arg;
711 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100712 struct nfs_fattr f_attr;
713 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400714 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100715 struct dentry *dir;
716 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400717 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100718 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100719 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400720 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100721 int rpc_status;
722 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100723};
724
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400725
726static void nfs4_init_opendata_res(struct nfs4_opendata *p)
727{
728 p->o_res.f_attr = &p->f_attr;
729 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400730 p->o_res.seqid = p->o_arg.seqid;
731 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400732 p->o_res.server = p->o_arg.server;
733 nfs_fattr_init(&p->f_attr);
734 nfs_fattr_init(&p->dir_attr);
735}
736
Trond Myklebustad389da2007-06-05 12:30:00 -0400737static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500738 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400739 const struct iattr *attrs,
740 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100741{
Trond Myklebustad389da2007-06-05 12:30:00 -0400742 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100743 struct inode *dir = parent->d_inode;
744 struct nfs_server *server = NFS_SERVER(dir);
745 struct nfs4_opendata *p;
746
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400747 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100748 if (p == NULL)
749 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400750 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100751 if (p->o_arg.seqid == NULL)
752 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500753 path_get(path);
754 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100755 p->dir = parent;
756 p->owner = sp;
757 atomic_inc(&sp->so_count);
758 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500759 p->o_arg.open_flags = flags;
760 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400761 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400762 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400763 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100764 p->o_arg.server = server;
765 p->o_arg.bitmask = server->attr_bitmask;
766 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400767 if (flags & O_CREAT) {
768 u32 *s;
769
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770 p->o_arg.u.attrs = &p->attrs;
771 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400772 s = (u32 *) p->o_arg.u.verifier.data;
773 s[0] = jiffies;
774 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100775 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100776 p->c_arg.fh = &p->o_res.fh;
777 p->c_arg.stateid = &p->o_res.stateid;
778 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400779 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400780 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100781 return p;
782err_free:
783 kfree(p);
784err:
785 dput(parent);
786 return NULL;
787}
788
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400789static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100790{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400791 struct nfs4_opendata *p = container_of(kref,
792 struct nfs4_opendata, kref);
793
794 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400795 if (p->state != NULL)
796 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400797 nfs4_put_state_owner(p->owner);
798 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700799 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400800 kfree(p);
801}
802
803static void nfs4_opendata_put(struct nfs4_opendata *p)
804{
805 if (p != NULL)
806 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100807}
808
Trond Myklebust06f814a2006-01-03 09:55:07 +0100809static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
810{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100811 int ret;
812
Trond Myklebust06f814a2006-01-03 09:55:07 +0100813 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100814 return ret;
815}
816
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500817static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400818{
819 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500820
821 if (open_mode & O_EXCL)
822 goto out;
823 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400824 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500825 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
826 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400827 break;
828 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500829 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
830 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400831 break;
832 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500833 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
834 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400835 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500836out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400837 return ret;
838}
839
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500840static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400841{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500842 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400843 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500844 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400845 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500846 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400847 return 1;
848}
849
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500850static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100851{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500852 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100853 case FMODE_WRITE:
854 state->n_wronly++;
855 break;
856 case FMODE_READ:
857 state->n_rdonly++;
858 break;
859 case FMODE_READ|FMODE_WRITE:
860 state->n_rdwr++;
861 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500862 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100863}
864
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500865static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400866{
867 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
868 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
869 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500870 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400871 case FMODE_READ:
872 set_bit(NFS_O_RDONLY_STATE, &state->flags);
873 break;
874 case FMODE_WRITE:
875 set_bit(NFS_O_WRONLY_STATE, &state->flags);
876 break;
877 case FMODE_READ|FMODE_WRITE:
878 set_bit(NFS_O_RDWR_STATE, &state->flags);
879 }
880}
881
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500882static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400883{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400884 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500885 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400886 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400887}
888
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500889static 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 -0700890{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400891 /*
892 * Protect the call to nfs4_state_set_mode_locked and
893 * serialise the stateid update
894 */
895 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400896 if (deleg_stateid != NULL) {
897 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
898 set_bit(NFS_DELEGATED_STATE, &state->flags);
899 }
900 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500901 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400902 write_sequnlock(&state->seqlock);
903 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500904 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700905 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908static 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 -0500909{
910 struct nfs_inode *nfsi = NFS_I(state->inode);
911 struct nfs_delegation *deleg_cur;
912 int ret = 0;
913
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500914 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500915
916 rcu_read_lock();
917 deleg_cur = rcu_dereference(nfsi->delegation);
918 if (deleg_cur == NULL)
919 goto no_delegation;
920
921 spin_lock(&deleg_cur->lock);
922 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500923 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500924 goto no_delegation_unlock;
925
926 if (delegation == NULL)
927 delegation = &deleg_cur->stateid;
928 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
929 goto no_delegation_unlock;
930
Trond Myklebustb7391f42008-12-23 15:21:52 -0500931 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500932 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500933 ret = 1;
934no_delegation_unlock:
935 spin_unlock(&deleg_cur->lock);
936no_delegation:
937 rcu_read_unlock();
938
939 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500941 ret = 1;
942 }
943
944 return ret;
945}
946
947
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500948static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400949{
950 struct nfs_delegation *delegation;
951
952 rcu_read_lock();
953 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400955 rcu_read_unlock();
956 return;
957 }
958 rcu_read_unlock();
959 nfs_inode_return_delegation(inode);
960}
961
Trond Myklebust6ee41262007-07-08 14:11:36 -0400962static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400963{
964 struct nfs4_state *state = opendata->state;
965 struct nfs_inode *nfsi = NFS_I(state->inode);
966 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500967 int open_mode = opendata->o_arg.open_flags & O_EXCL;
968 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400969 nfs4_stateid stateid;
970 int ret = -EAGAIN;
971
Trond Myklebustaac00a82007-07-05 19:02:21 -0400972 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400974 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500975 if (can_open_cached(state, fmode, open_mode)) {
976 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400977 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400978 goto out_return_state;
979 }
980 spin_unlock(&state->owner->so_lock);
981 }
Trond Myklebust34310432008-12-23 15:21:38 -0500982 rcu_read_lock();
983 delegation = rcu_dereference(nfsi->delegation);
984 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500985 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500986 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400987 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500988 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400989 /* Save the delegation */
990 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
991 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400992 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400993 if (ret != 0)
994 goto out;
995 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500996
997 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500999 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001000 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001001out:
1002 return ERR_PTR(ret);
1003out_return_state:
1004 atomic_inc(&state->count);
1005 return state;
1006}
1007
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001008static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1009{
1010 struct inode *inode;
1011 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001012 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001013 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001014
Trond Myklebustaac00a82007-07-05 19:02:21 -04001015 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001016 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001017 goto out;
1018 }
1019
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001020 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001021 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001022 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001023 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001024 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001025 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001026 goto err;
1027 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001028 state = nfs4_get_open_state(inode, data->owner);
1029 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001030 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001031 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001032 int delegation_flags = 0;
1033
Trond Myklebust003707c2007-07-05 18:07:55 -04001034 rcu_read_lock();
1035 delegation = rcu_dereference(NFS_I(inode)->delegation);
1036 if (delegation)
1037 delegation_flags = delegation->flags;
1038 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001039 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001040 nfs_inode_set_delegation(state->inode,
1041 data->owner->so_cred,
1042 &data->o_res);
1043 else
1044 nfs_inode_reclaim_delegation(state->inode,
1045 data->owner->so_cred,
1046 &data->o_res);
1047 }
Trond Myklebust34310432008-12-23 15:21:38 -05001048
1049 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001050 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001051 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001052out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001053 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001054err_put_inode:
1055 iput(inode);
1056err:
1057 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001058}
1059
Trond Myklebust864472e2006-01-03 09:55:15 +01001060static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1061{
1062 struct nfs_inode *nfsi = NFS_I(state->inode);
1063 struct nfs_open_context *ctx;
1064
1065 spin_lock(&state->inode->i_lock);
1066 list_for_each_entry(ctx, &nfsi->open_files, list) {
1067 if (ctx->state != state)
1068 continue;
1069 get_nfs_open_context(ctx);
1070 spin_unlock(&state->inode->i_lock);
1071 return ctx;
1072 }
1073 spin_unlock(&state->inode->i_lock);
1074 return ERR_PTR(-ENOENT);
1075}
1076
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001077static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1078{
1079 struct nfs4_opendata *opendata;
1080
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001081 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001082 if (opendata == NULL)
1083 return ERR_PTR(-ENOMEM);
1084 opendata->state = state;
1085 atomic_inc(&state->count);
1086 return opendata;
1087}
1088
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001089static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001090{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001091 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001092 int ret;
1093
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001094 opendata->o_arg.open_flags = 0;
1095 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001096 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1097 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1098 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001099 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001100 if (ret != 0)
1101 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001102 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001103 if (IS_ERR(newstate))
1104 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001105 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001106 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001107 return 0;
1108}
1109
1110static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1111{
Trond Myklebust864472e2006-01-03 09:55:15 +01001112 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001113 int ret;
1114
1115 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001116 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001117 smp_rmb();
1118 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001119 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001120 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001121 if (ret != 0)
1122 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001123 if (newstate != state)
1124 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001125 }
1126 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001127 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001128 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001129 if (ret != 0)
1130 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001131 if (newstate != state)
1132 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001133 }
1134 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001135 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001136 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001137 if (ret != 0)
1138 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001139 if (newstate != state)
1140 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001141 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001142 /*
1143 * We may have performed cached opens for all three recoveries.
1144 * Check if we need to update the current stateid.
1145 */
1146 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1147 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001148 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001149 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1150 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001151 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001152 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001153 return 0;
1154}
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156/*
1157 * OPEN_RECLAIM:
1158 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001160static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001162 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001163 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001164 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 int status;
1166
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001167 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1168 if (IS_ERR(opendata))
1169 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001170 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1171 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001172 rcu_read_lock();
1173 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001174 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001175 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001176 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001177 opendata->o_arg.u.delegation_type = delegation_type;
1178 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001179 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return status;
1181}
1182
Trond Myklebust539cd032007-06-05 11:46:42 -04001183static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 struct nfs_server *server = NFS_SERVER(state->inode);
1186 struct nfs4_exception exception = { };
1187 int err;
1188 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001189 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001190 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001191 break;
1192 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 } while (exception.retry);
1194 return err;
1195}
1196
Trond Myklebust864472e2006-01-03 09:55:15 +01001197static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1198{
1199 struct nfs_open_context *ctx;
1200 int ret;
1201
1202 ctx = nfs4_state_find_open_context(state);
1203 if (IS_ERR(ctx))
1204 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001205 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001206 put_nfs_open_context(ctx);
1207 return ret;
1208}
1209
Trond Myklebust13437e12007-07-06 15:10:43 -04001210static 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 -07001211{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001212 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001213 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001215 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1216 if (IS_ERR(opendata))
1217 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001218 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001219 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001220 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001221 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001222 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001223 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
Trond Myklebust13437e12007-07-06 15:10:43 -04001226int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
1228 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001229 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 int err;
1231 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001232 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 switch (err) {
1234 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001235 case -ENOENT:
1236 case -ESTALE:
1237 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001238 case -NFS4ERR_BADSESSION:
1239 case -NFS4ERR_BADSLOT:
1240 case -NFS4ERR_BAD_HIGH_SLOT:
1241 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1242 case -NFS4ERR_DEADSESSION:
1243 nfs4_schedule_state_recovery(
1244 server->nfs_client);
1245 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 case -NFS4ERR_STALE_CLIENTID:
1247 case -NFS4ERR_STALE_STATEID:
1248 case -NFS4ERR_EXPIRED:
1249 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001250 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001251 goto out;
1252 case -ERESTARTSYS:
1253 /*
1254 * The show must go on: exit, but mark the
1255 * stateid as needing recovery.
1256 */
1257 case -NFS4ERR_ADMIN_REVOKED:
1258 case -NFS4ERR_BAD_STATEID:
1259 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001260 case -EKEYEXPIRED:
1261 /*
1262 * User RPCSEC_GSS context has expired.
1263 * We cannot recover this stateid now, so
1264 * skip it and allow recovery thread to
1265 * proceed.
1266 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001267 case -ENOMEM:
1268 err = 0;
1269 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271 err = nfs4_handle_exception(server, err, &exception);
1272 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001273out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return err;
1275}
1276
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001277static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1278{
1279 struct nfs4_opendata *data = calldata;
1280
1281 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001282 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001283 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1284 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001285 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001286 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001287 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001288 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001289}
1290
1291static void nfs4_open_confirm_release(void *calldata)
1292{
1293 struct nfs4_opendata *data = calldata;
1294 struct nfs4_state *state = NULL;
1295
1296 /* If this request hasn't been cancelled, do nothing */
1297 if (data->cancelled == 0)
1298 goto out_free;
1299 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001300 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001301 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001302 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001303 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001304 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001305out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001306 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001307}
1308
1309static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001310 .rpc_call_done = nfs4_open_confirm_done,
1311 .rpc_release = nfs4_open_confirm_release,
1312};
1313
1314/*
1315 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1316 */
1317static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1318{
1319 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1320 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001321 struct rpc_message msg = {
1322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1323 .rpc_argp = &data->c_arg,
1324 .rpc_resp = &data->c_res,
1325 .rpc_cred = data->owner->so_cred,
1326 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001327 struct rpc_task_setup task_setup_data = {
1328 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001329 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001330 .callback_ops = &nfs4_open_confirm_ops,
1331 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001332 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001333 .flags = RPC_TASK_ASYNC,
1334 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 int status;
1336
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001337 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001338 data->rpc_done = 0;
1339 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001340 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001341 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001342 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001343 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001344 status = nfs4_wait_for_completion_rpc_task(task);
1345 if (status != 0) {
1346 data->cancelled = 1;
1347 smp_wmb();
1348 } else
1349 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001350 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return status;
1352}
1353
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001354static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001356 struct nfs4_opendata *data = calldata;
1357 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001358
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001359 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1360 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001361 /*
1362 * Check if we still need to send an OPEN call, or if we can use
1363 * a delegation instead.
1364 */
1365 if (data->state != NULL) {
1366 struct nfs_delegation *delegation;
1367
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001368 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001369 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001370 rcu_read_lock();
1371 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1372 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001373 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001374 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001375 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001376 }
1377 rcu_read_unlock();
1378 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001379 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001380 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001381 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001382 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001383 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001384 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1385 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001386 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001387 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001388 &data->o_arg.seq_args,
1389 &data->o_res.seq_res, 1, task))
1390 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001391 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001392 return;
1393out_no_action:
1394 task->tk_action = NULL;
1395
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001396}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001398static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1399{
1400 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1401 nfs4_open_prepare(task, calldata);
1402}
1403
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001404static void nfs4_open_done(struct rpc_task *task, void *calldata)
1405{
1406 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001408 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001409
Trond Myklebust14516c32010-07-31 14:29:06 -04001410 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1411 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001412
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001413 if (task->tk_status == 0) {
1414 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001415 case S_IFREG:
1416 break;
1417 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001418 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001419 break;
1420 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001421 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001422 break;
1423 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001424 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001425 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001426 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001427 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1428 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001429 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001430 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001431}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001432
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001433static void nfs4_open_release(void *calldata)
1434{
1435 struct nfs4_opendata *data = calldata;
1436 struct nfs4_state *state = NULL;
1437
1438 /* If this request hasn't been cancelled, do nothing */
1439 if (data->cancelled == 0)
1440 goto out_free;
1441 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001442 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001443 goto out_free;
1444 /* In case we need an open_confirm, no cleanup! */
1445 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1446 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001447 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001448 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001449 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001450out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001451 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001452}
1453
1454static const struct rpc_call_ops nfs4_open_ops = {
1455 .rpc_call_prepare = nfs4_open_prepare,
1456 .rpc_call_done = nfs4_open_done,
1457 .rpc_release = nfs4_open_release,
1458};
1459
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001460static const struct rpc_call_ops nfs4_recover_open_ops = {
1461 .rpc_call_prepare = nfs4_recover_open_prepare,
1462 .rpc_call_done = nfs4_open_done,
1463 .rpc_release = nfs4_open_release,
1464};
1465
1466static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001467{
1468 struct inode *dir = data->dir->d_inode;
1469 struct nfs_server *server = NFS_SERVER(dir);
1470 struct nfs_openargs *o_arg = &data->o_arg;
1471 struct nfs_openres *o_res = &data->o_res;
1472 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001473 struct rpc_message msg = {
1474 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1475 .rpc_argp = o_arg,
1476 .rpc_resp = o_res,
1477 .rpc_cred = data->owner->so_cred,
1478 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001479 struct rpc_task_setup task_setup_data = {
1480 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001481 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001482 .callback_ops = &nfs4_open_ops,
1483 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001484 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001485 .flags = RPC_TASK_ASYNC,
1486 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001487 int status;
1488
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001489 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001490 data->rpc_done = 0;
1491 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001492 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001493 if (isrecover)
1494 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001495 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001496 if (IS_ERR(task))
1497 return PTR_ERR(task);
1498 status = nfs4_wait_for_completion_rpc_task(task);
1499 if (status != 0) {
1500 data->cancelled = 1;
1501 smp_wmb();
1502 } else
1503 status = data->rpc_status;
1504 rpc_put_task(task);
1505
1506 return status;
1507}
1508
1509static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1510{
1511 struct inode *dir = data->dir->d_inode;
1512 struct nfs_openres *o_res = &data->o_res;
1513 int status;
1514
1515 status = nfs4_run_open_task(data, 1);
1516 if (status != 0 || !data->rpc_done)
1517 return status;
1518
1519 nfs_refresh_inode(dir, o_res->dir_attr);
1520
1521 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1522 status = _nfs4_proc_open_confirm(data);
1523 if (status != 0)
1524 return status;
1525 }
1526
1527 return status;
1528}
1529
1530/*
1531 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1532 */
1533static int _nfs4_proc_open(struct nfs4_opendata *data)
1534{
1535 struct inode *dir = data->dir->d_inode;
1536 struct nfs_server *server = NFS_SERVER(dir);
1537 struct nfs_openargs *o_arg = &data->o_arg;
1538 struct nfs_openres *o_res = &data->o_res;
1539 int status;
1540
1541 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001542 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001543 return status;
1544
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001545 if (o_arg->open_flags & O_CREAT) {
1546 update_changeattr(dir, &o_res->cinfo);
1547 nfs_post_op_update_inode(dir, o_res->dir_attr);
1548 } else
1549 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001550 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1551 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001553 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001555 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
1557 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001558 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001559 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
Trond Myklebust10afec92007-05-14 17:16:04 -04001562static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001563{
David Howells7539bba2006-08-22 20:06:09 -04001564 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001565 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001566 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001567
Trond Myklebusta78cb572009-08-09 15:06:19 -04001568 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001569 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001570 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001571 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001572 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1573 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001574 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001575 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001576 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001577 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001578 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001579}
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581/*
1582 * OPEN_EXPIRED:
1583 * reclaim state on the server after a network partition.
1584 * Assumes caller holds the appropriate lock
1585 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001586static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001588 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001589 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001591 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1592 if (IS_ERR(opendata))
1593 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001594 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001595 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001596 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001597 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001598 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001601static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001602{
Trond Myklebust539cd032007-06-05 11:46:42 -04001603 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001604 struct nfs4_exception exception = { };
1605 int err;
1606
1607 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001608 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001609 switch (err) {
1610 default:
1611 goto out;
1612 case -NFS4ERR_GRACE:
1613 case -NFS4ERR_DELAY:
1614 nfs4_handle_exception(server, err, &exception);
1615 err = 0;
1616 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001617 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001618out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001619 return err;
1620}
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1623{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001625 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Trond Myklebust864472e2006-01-03 09:55:15 +01001627 ctx = nfs4_state_find_open_context(state);
1628 if (IS_ERR(ctx))
1629 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001630 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001631 put_nfs_open_context(ctx);
1632 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
1634
1635/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001636 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1637 * fields corresponding to attributes that were used to store the verifier.
1638 * Make sure we clobber those fields in the later setattr call
1639 */
1640static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1641{
1642 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1643 !(sattr->ia_valid & ATTR_ATIME_SET))
1644 sattr->ia_valid |= ATTR_ATIME;
1645
1646 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1647 !(sattr->ia_valid & ATTR_MTIME_SET))
1648 sattr->ia_valid |= ATTR_MTIME;
1649}
1650
1651/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001652 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001654static 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 -07001655{
1656 struct nfs4_state_owner *sp;
1657 struct nfs4_state *state = NULL;
1658 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001659 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001660 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 status = -ENOMEM;
1664 if (!(sp = nfs4_get_state_owner(server, cred))) {
1665 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1666 goto out_err;
1667 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001668 status = nfs4_recover_expired_lease(server);
1669 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001670 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001671 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001672 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001673 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001674 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001675 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001676 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Trond Myklebustaac00a82007-07-05 19:02:21 -04001678 if (path->dentry->d_inode != NULL)
1679 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1680
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001681 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001683 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001685 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001686 status = PTR_ERR(state);
1687 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001688 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001689 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001690 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001691
1692 if (opendata->o_arg.open_flags & O_EXCL) {
1693 nfs4_exclusive_attrset(opendata, sattr);
1694
1695 nfs_fattr_init(opendata->o_res.f_attr);
1696 status = nfs4_do_setattr(state->inode, cred,
1697 opendata->o_res.f_attr, sattr,
1698 state);
1699 if (status == 0)
1700 nfs_setattr_update_inode(state->inode, sattr);
1701 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1702 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001703 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 *res = state;
1706 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001707err_opendata_put:
1708 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001709err_put_state_owner:
1710 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 *res = NULL;
1713 return status;
1714}
1715
1716
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001717static 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 -07001718{
1719 struct nfs4_exception exception = { };
1720 struct nfs4_state *res;
1721 int status;
1722
1723 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001724 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (status == 0)
1726 break;
1727 /* NOTE: BAD_SEQID means the server and client disagree about the
1728 * book-keeping w.r.t. state-changing operations
1729 * (OPEN/CLOSE/LOCK/LOCKU...)
1730 * It is actually a sign of a bug on the client or on the server.
1731 *
1732 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001733 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 * have unhashed the old state_owner for us, and that we can
1735 * therefore safely retry using a new one. We should still warn
1736 * the user though...
1737 */
1738 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001739 printk(KERN_WARNING "NFS: v4 server %s "
1740 " returned a bad sequence-id error!\n",
1741 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 exception.retry = 1;
1743 continue;
1744 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001745 /*
1746 * BAD_STATEID on OPEN means that the server cancelled our
1747 * state before it received the OPEN_CONFIRM.
1748 * Recover by retrying the request as per the discussion
1749 * on Page 181 of RFC3530.
1750 */
1751 if (status == -NFS4ERR_BAD_STATEID) {
1752 exception.retry = 1;
1753 continue;
1754 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001755 if (status == -EAGAIN) {
1756 /* We must have found a delegation */
1757 exception.retry = 1;
1758 continue;
1759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1761 status, &exception));
1762 } while (exception.retry);
1763 return res;
1764}
1765
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001766static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1767 struct nfs_fattr *fattr, struct iattr *sattr,
1768 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001770 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001772 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 .iap = sattr,
1774 .server = server,
1775 .bitmask = server->attr_bitmask,
1776 };
1777 struct nfs_setattrres res = {
1778 .fattr = fattr,
1779 .server = server,
1780 };
1781 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1783 .rpc_argp = &arg,
1784 .rpc_resp = &res,
1785 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001787 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001788 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Trond Myklebust0e574af2005-10-27 22:12:38 -04001790 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001792 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1793 /* Use that stateid */
1794 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001795 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001796 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1798
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001799 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001800 if (status == 0 && state != NULL)
1801 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001802 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001805static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1806 struct nfs_fattr *fattr, struct iattr *sattr,
1807 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001809 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 struct nfs4_exception exception = { };
1811 int err;
1812 do {
1813 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001814 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 &exception);
1816 } while (exception.retry);
1817 return err;
1818}
1819
1820struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001821 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 struct inode *inode;
1823 struct nfs4_state *state;
1824 struct nfs_closeargs arg;
1825 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001826 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001827 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828};
1829
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001830static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001831{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001832 struct nfs4_closedata *calldata = data;
1833 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001834
1835 nfs4_put_open_state(calldata->state);
1836 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001837 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001838 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001839 kfree(calldata);
1840}
1841
Trond Myklebust88069f72009-12-08 08:33:16 -05001842static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1843 fmode_t fmode)
1844{
1845 spin_lock(&state->owner->so_lock);
1846 if (!(fmode & FMODE_READ))
1847 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1848 if (!(fmode & FMODE_WRITE))
1849 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1850 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1851 spin_unlock(&state->owner->so_lock);
1852}
1853
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001854static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001856 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 struct nfs_server *server = NFS_SERVER(calldata->inode);
1859
Trond Myklebust14516c32010-07-31 14:29:06 -04001860 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1861 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 /* hmm. we are done with the inode, and in the process of freeing
1863 * the state_owner. we keep this around to process errors
1864 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 switch (task->tk_status) {
1866 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001867 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001868 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001869 nfs4_close_clear_stateid_flags(state,
1870 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 break;
1872 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001873 case -NFS4ERR_OLD_STATEID:
1874 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001876 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001877 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001879 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1880 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001882 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001883 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884}
1885
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001886static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001888 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001889 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001890 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001891
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001892 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001893 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001894
Trond Myklebust88069f72009-12-08 08:33:16 -05001895 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1896 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001897 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001898 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001899 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001900 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001901 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1902 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1903 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001904 }
1905 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001906 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1907 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1908 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001909 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001910 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001911 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001912
1913 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001914 /* Note: exit _without_ calling nfs4_close_done */
1915 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001916 return;
1917 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001918
1919 if (calldata->arg.fmode == 0)
1920 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1921
Trond Myklebust516a6af2005-10-27 22:12:41 -04001922 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001923 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001924 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001925 &calldata->arg.seq_args, &calldata->res.seq_res,
1926 1, task))
1927 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001928 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001931static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001932 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001933 .rpc_call_done = nfs4_close_done,
1934 .rpc_release = nfs4_free_closedata,
1935};
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937/*
1938 * It is possible for data to be read/written from a mem-mapped file
1939 * after the sys_close call (which hits the vfs layer as a flush).
1940 * This means that we can't safely call nfsv4 close on a file until
1941 * the inode is cleared. This in turn means that we are not good
1942 * NFSv4 citizens - we do not indicate to the server to update the file's
1943 * share state even when we are done with one of the three share
1944 * stateid's in the inode.
1945 *
1946 * NOTE: Caller must be holding the sp->so_owner semaphore!
1947 */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001948int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001950 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001952 struct nfs4_state_owner *sp = state->owner;
1953 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001954 struct rpc_message msg = {
1955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1956 .rpc_cred = state->owner->so_cred,
1957 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001958 struct rpc_task_setup task_setup_data = {
1959 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001960 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001961 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001962 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001963 .flags = RPC_TASK_ASYNC,
1964 };
Trond Myklebust95121352005-10-18 14:20:12 -07001965 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001967 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001969 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001970 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001972 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001973 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001975 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07001976 if (calldata->arg.seqid == NULL)
1977 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001978 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001979 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001980 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001981 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001982 calldata->res.server = server;
Al Virof6948692010-01-30 13:51:04 -05001983 path_get(path);
1984 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07001985
Trond Myklebust5138fde2007-07-14 15:40:01 -04001986 msg.rpc_argp = &calldata->arg,
1987 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001988 task_setup_data.callback_data = calldata;
1989 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001990 if (IS_ERR(task))
1991 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001992 status = 0;
1993 if (wait)
1994 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001995 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001996 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001997out_free_calldata:
1998 kfree(calldata);
1999out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002000 nfs4_put_open_state(state);
2001 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002002 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
2004
Trond Myklebust2b484292010-09-17 10:56:51 -04002005static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002006nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 struct nfs4_state *state;
2009
Trond Myklebust565277f2007-10-15 18:17:53 -04002010 /* Protect against concurrent sillydeletes */
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002011 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002012 if (IS_ERR(state))
2013 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002014 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002015 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016}
2017
Trond Myklebust1185a552009-12-03 15:54:02 -05002018static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002019{
2020 if (ctx->state == NULL)
2021 return;
2022 if (is_sync)
2023 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2024 else
2025 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2026}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
2028static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2029{
Benny Halevy43652ad2009-04-01 09:21:54 -04002030 struct nfs4_server_caps_arg args = {
2031 .fhandle = fhandle,
2032 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 struct nfs4_server_caps_res res = {};
2034 struct rpc_message msg = {
2035 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002036 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 .rpc_resp = &res,
2038 };
2039 int status;
2040
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002041 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (status == 0) {
2043 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002044 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2045 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2046 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2047 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2048 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2050 server->caps |= NFS_CAP_ACLS;
2051 if (res.has_links != 0)
2052 server->caps |= NFS_CAP_HARDLINKS;
2053 if (res.has_symlinks != 0)
2054 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002055 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2056 server->caps |= NFS_CAP_FILEID;
2057 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2058 server->caps |= NFS_CAP_MODE;
2059 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2060 server->caps |= NFS_CAP_NLINK;
2061 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2062 server->caps |= NFS_CAP_OWNER;
2063 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2064 server->caps |= NFS_CAP_OWNER_GROUP;
2065 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2066 server->caps |= NFS_CAP_ATIME;
2067 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2068 server->caps |= NFS_CAP_CTIME;
2069 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2070 server->caps |= NFS_CAP_MTIME;
2071
Trond Myklebusta65318b2009-03-11 14:10:28 -04002072 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2073 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2074 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 server->acl_bitmask = res.acl_bitmask;
2076 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return status;
2079}
2080
Trond Myklebust55a97592006-06-09 09:34:19 -04002081int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
2083 struct nfs4_exception exception = { };
2084 int err;
2085 do {
2086 err = nfs4_handle_exception(server,
2087 _nfs4_server_capabilities(server, fhandle),
2088 &exception);
2089 } while (exception.retry);
2090 return err;
2091}
2092
2093static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2094 struct nfs_fsinfo *info)
2095{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 struct nfs4_lookup_root_arg args = {
2097 .bitmask = nfs4_fattr_bitmap,
2098 };
2099 struct nfs4_lookup_res res = {
2100 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002101 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 .fh = fhandle,
2103 };
2104 struct rpc_message msg = {
2105 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2106 .rpc_argp = &args,
2107 .rpc_resp = &res,
2108 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002109
Trond Myklebust0e574af2005-10-27 22:12:38 -04002110 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002111 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
2114static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2115 struct nfs_fsinfo *info)
2116{
2117 struct nfs4_exception exception = { };
2118 int err;
2119 do {
2120 err = nfs4_handle_exception(server,
2121 _nfs4_lookup_root(server, fhandle, info),
2122 &exception);
2123 } while (exception.retry);
2124 return err;
2125}
2126
David Howells54ceac42006-08-22 20:06:13 -04002127/*
2128 * get the file handle for the "/" directory on the server
2129 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002131 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 int status;
2134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 if (status == 0)
2137 status = nfs4_server_capabilities(server, fhandle);
2138 if (status == 0)
2139 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002140 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
2142
Manoj Naik6b97fd32006-06-09 09:34:29 -04002143/*
2144 * Get locations and (maybe) other attributes of a referral.
2145 * Note that we'll actually follow the referral later when
2146 * we detect fsid mismatch in inode revalidation
2147 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002148static 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 -04002149{
2150 int status = -ENOMEM;
2151 struct page *page = NULL;
2152 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002153
2154 page = alloc_page(GFP_KERNEL);
2155 if (page == NULL)
2156 goto out;
2157 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2158 if (locations == NULL)
2159 goto out;
2160
Trond Myklebustc228fd32007-01-13 02:28:11 -05002161 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002162 if (status != 0)
2163 goto out;
2164 /* Make sure server returned a different fsid for the referral */
2165 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002166 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 -04002167 status = -EIO;
2168 goto out;
2169 }
2170
2171 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2172 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2173 if (!fattr->mode)
2174 fattr->mode = S_IFDIR;
2175 memset(fhandle, 0, sizeof(struct nfs_fh));
2176out:
2177 if (page)
2178 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002179 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002180 return status;
2181}
2182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2184{
2185 struct nfs4_getattr_arg args = {
2186 .fh = fhandle,
2187 .bitmask = server->attr_bitmask,
2188 };
2189 struct nfs4_getattr_res res = {
2190 .fattr = fattr,
2191 .server = server,
2192 };
2193 struct rpc_message msg = {
2194 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2195 .rpc_argp = &args,
2196 .rpc_resp = &res,
2197 };
2198
Trond Myklebust0e574af2005-10-27 22:12:38 -04002199 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002200 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}
2202
2203static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2204{
2205 struct nfs4_exception exception = { };
2206 int err;
2207 do {
2208 err = nfs4_handle_exception(server,
2209 _nfs4_proc_getattr(server, fhandle, fattr),
2210 &exception);
2211 } while (exception.retry);
2212 return err;
2213}
2214
2215/*
2216 * The file is not closed if it is opened due to the a request to change
2217 * the size of the file. The open call will not be needed once the
2218 * VFS layer lookup-intents are implemented.
2219 *
2220 * Close is called when the inode is destroyed.
2221 * If we haven't opened the file for O_WRONLY, we
2222 * need to in the size_change case to obtain a stateid.
2223 *
2224 * Got race?
2225 * Because OPEN is always done by name in nfsv4, it is
2226 * possible that we opened a different file by the same
2227 * name. We can recognize this race condition, but we
2228 * can't do anything about it besides returning an error.
2229 *
2230 * This will be fixed with VFS changes (lookup-intent).
2231 */
2232static int
2233nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2234 struct iattr *sattr)
2235{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002236 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002237 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002238 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 int status;
2240
Trond Myklebust0e574af2005-10-27 22:12:38 -04002241 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Trond Myklebustd5308382005-11-04 15:33:38 -05002243 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002244 if (sattr->ia_valid & ATTR_FILE) {
2245 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002246
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002247 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002248 if (ctx) {
2249 cred = ctx->cred;
2250 state = ctx->state;
2251 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002252 }
2253
2254 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002255 if (status == 0)
2256 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 return status;
2258}
2259
Trond Myklebust56659e92007-07-17 21:52:39 -04002260static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2261 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002262 struct nfs_fattr *fattr)
2263{
2264 int status;
2265 struct nfs4_lookup_arg args = {
2266 .bitmask = server->attr_bitmask,
2267 .dir_fh = dirfh,
2268 .name = name,
2269 };
2270 struct nfs4_lookup_res res = {
2271 .server = server,
2272 .fattr = fattr,
2273 .fh = fhandle,
2274 };
2275 struct rpc_message msg = {
2276 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2277 .rpc_argp = &args,
2278 .rpc_resp = &res,
2279 };
2280
2281 nfs_fattr_init(fattr);
2282
2283 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002284 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002285 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002286 return status;
2287}
2288
2289static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2290 struct qstr *name, struct nfs_fh *fhandle,
2291 struct nfs_fattr *fattr)
2292{
2293 struct nfs4_exception exception = { };
2294 int err;
2295 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002296 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2297 /* FIXME: !!!! */
2298 if (err == -NFS4ERR_MOVED) {
2299 err = -EREMOTE;
2300 break;
2301 }
2302 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002303 } while (exception.retry);
2304 return err;
2305}
2306
Trond Myklebust56659e92007-07-17 21:52:39 -04002307static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2309{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002310 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002313 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002314 if (status == -NFS4ERR_MOVED)
2315 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 dprintk("NFS reply lookup: %d\n", status);
2317 return status;
2318}
2319
2320static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2321{
2322 struct nfs4_exception exception = { };
2323 int err;
2324 do {
2325 err = nfs4_handle_exception(NFS_SERVER(dir),
2326 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2327 &exception);
2328 } while (exception.retry);
2329 return err;
2330}
2331
2332static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2333{
Trond Myklebust76b32992007-08-10 17:45:11 -04002334 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 struct nfs4_accessargs args = {
2336 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002337 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002339 struct nfs4_accessres res = {
2340 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002341 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 struct rpc_message msg = {
2343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2344 .rpc_argp = &args,
2345 .rpc_resp = &res,
2346 .rpc_cred = entry->cred,
2347 };
2348 int mode = entry->mask;
2349 int status;
2350
2351 /*
2352 * Determine which access bits we want to ask for...
2353 */
2354 if (mode & MAY_READ)
2355 args.access |= NFS4_ACCESS_READ;
2356 if (S_ISDIR(inode->i_mode)) {
2357 if (mode & MAY_WRITE)
2358 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2359 if (mode & MAY_EXEC)
2360 args.access |= NFS4_ACCESS_LOOKUP;
2361 } else {
2362 if (mode & MAY_WRITE)
2363 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2364 if (mode & MAY_EXEC)
2365 args.access |= NFS4_ACCESS_EXECUTE;
2366 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002367
2368 res.fattr = nfs_alloc_fattr();
2369 if (res.fattr == NULL)
2370 return -ENOMEM;
2371
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002372 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 if (!status) {
2374 entry->mask = 0;
2375 if (res.access & NFS4_ACCESS_READ)
2376 entry->mask |= MAY_READ;
2377 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2378 entry->mask |= MAY_WRITE;
2379 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2380 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002381 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002383 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 return status;
2385}
2386
2387static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2388{
2389 struct nfs4_exception exception = { };
2390 int err;
2391 do {
2392 err = nfs4_handle_exception(NFS_SERVER(inode),
2393 _nfs4_proc_access(inode, entry),
2394 &exception);
2395 } while (exception.retry);
2396 return err;
2397}
2398
2399/*
2400 * TODO: For the time being, we don't try to get any attributes
2401 * along with any of the zero-copy operations READ, READDIR,
2402 * READLINK, WRITE.
2403 *
2404 * In the case of the first three, we want to put the GETATTR
2405 * after the read-type operation -- this is because it is hard
2406 * to predict the length of a GETATTR response in v4, and thus
2407 * align the READ data correctly. This means that the GETATTR
2408 * may end up partially falling into the page cache, and we should
2409 * shift it into the 'tail' of the xdr_buf before processing.
2410 * To do this efficiently, we need to know the total length
2411 * of data received, which doesn't seem to be available outside
2412 * of the RPC layer.
2413 *
2414 * In the case of WRITE, we also want to put the GETATTR after
2415 * the operation -- in this case because we want to make sure
2416 * we get the post-operation mtime and size. This means that
2417 * we can't use xdr_encode_pages() as written: we need a variant
2418 * of it which would leave room in the 'tail' iovec.
2419 *
2420 * Both of these changes to the XDR layer would in fact be quite
2421 * minor, but I decided to leave them for a subsequent patch.
2422 */
2423static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2424 unsigned int pgbase, unsigned int pglen)
2425{
2426 struct nfs4_readlink args = {
2427 .fh = NFS_FH(inode),
2428 .pgbase = pgbase,
2429 .pglen = pglen,
2430 .pages = &page,
2431 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002432 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 struct rpc_message msg = {
2434 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2435 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002436 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 };
2438
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002439 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440}
2441
2442static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2443 unsigned int pgbase, unsigned int pglen)
2444{
2445 struct nfs4_exception exception = { };
2446 int err;
2447 do {
2448 err = nfs4_handle_exception(NFS_SERVER(inode),
2449 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2450 &exception);
2451 } while (exception.retry);
2452 return err;
2453}
2454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455/*
2456 * Got race?
2457 * We will need to arrange for the VFS layer to provide an atomic open.
2458 * Until then, this create/open method is prone to inefficiency and race
2459 * conditions due to the lookup, create, and open VFS calls from sys_open()
2460 * placed on the wire.
2461 *
2462 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2463 * The file will be opened again in the subsequent VFS open call
2464 * (nfs4_proc_file_open).
2465 *
2466 * The open for read will just hang around to be used by any process that
2467 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2468 */
2469
2470static int
2471nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002472 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002474 struct path my_path = {
Trond Myklebustad389da2007-06-05 12:30:00 -04002475 .dentry = dentry,
2476 };
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002477 struct path *path = &my_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002479 struct rpc_cred *cred = NULL;
2480 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 int status = 0;
2482
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002483 if (ctx != NULL) {
2484 cred = ctx->cred;
2485 path = &ctx->path;
2486 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002488 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002489 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 if (IS_ERR(state)) {
2491 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002492 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002494 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002495 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002496 if (ctx != NULL)
2497 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002498 else
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002499 nfs4_close_sync(path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500out:
2501 return status;
2502}
2503
2504static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2505{
Trond Myklebust16e42952005-10-27 22:12:44 -04002506 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002507 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002509 .name.len = name->len,
2510 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002511 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002513 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002514 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002515 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002517 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2518 .rpc_argp = &args,
2519 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002521 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Trond Myklebustd3468902010-04-16 16:22:50 -04002523 res.dir_attr = nfs_alloc_fattr();
2524 if (res.dir_attr == NULL)
2525 goto out;
2526
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002527 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002528 if (status == 0) {
2529 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002530 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002531 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002532 nfs_free_fattr(res.dir_attr);
2533out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 return status;
2535}
2536
2537static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2538{
2539 struct nfs4_exception exception = { };
2540 int err;
2541 do {
2542 err = nfs4_handle_exception(NFS_SERVER(dir),
2543 _nfs4_proc_remove(dir, name),
2544 &exception);
2545 } while (exception.retry);
2546 return err;
2547}
2548
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002549static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002551 struct nfs_server *server = NFS_SERVER(dir);
2552 struct nfs_removeargs *args = msg->rpc_argp;
2553 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Trond Myklebusta65318b2009-03-11 14:10:28 -04002555 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002556 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002557 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559}
2560
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002561static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002563 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2564
Trond Myklebust14516c32010-07-31 14:29:06 -04002565 if (!nfs4_sequence_done(task, &res->seq_res))
2566 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002567 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002568 return 0;
2569 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002570 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002571 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572}
2573
Jeff Laytond3d41522010-09-17 17:31:57 -04002574static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2575{
2576 struct nfs_server *server = NFS_SERVER(dir);
2577 struct nfs_renameargs *arg = msg->rpc_argp;
2578 struct nfs_renameres *res = msg->rpc_resp;
2579
2580 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2581 arg->bitmask = server->attr_bitmask;
2582 res->server = server;
2583}
2584
2585static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2586 struct inode *new_dir)
2587{
2588 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2589
2590 if (!nfs4_sequence_done(task, &res->seq_res))
2591 return 0;
2592 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2593 return 0;
2594
2595 update_changeattr(old_dir, &res->old_cinfo);
2596 nfs_post_op_update_inode(old_dir, res->old_fattr);
2597 update_changeattr(new_dir, &res->new_cinfo);
2598 nfs_post_op_update_inode(new_dir, res->new_fattr);
2599 return 1;
2600}
2601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2603 struct inode *new_dir, struct qstr *new_name)
2604{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002605 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002606 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 .old_dir = NFS_FH(old_dir),
2608 .new_dir = NFS_FH(new_dir),
2609 .old_name = old_name,
2610 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002611 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002613 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002614 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002615 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 struct rpc_message msg = {
2617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2618 .rpc_argp = &arg,
2619 .rpc_resp = &res,
2620 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002621 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Trond Myklebust011fff72010-04-16 16:22:49 -04002623 res.old_fattr = nfs_alloc_fattr();
2624 res.new_fattr = nfs_alloc_fattr();
2625 if (res.old_fattr == NULL || res.new_fattr == NULL)
2626 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Trond Myklebust011fff72010-04-16 16:22:49 -04002628 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 if (!status) {
2630 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002631 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002633 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002635out:
2636 nfs_free_fattr(res.new_fattr);
2637 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 return status;
2639}
2640
2641static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2642 struct inode *new_dir, struct qstr *new_name)
2643{
2644 struct nfs4_exception exception = { };
2645 int err;
2646 do {
2647 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2648 _nfs4_proc_rename(old_dir, old_name,
2649 new_dir, new_name),
2650 &exception);
2651 } while (exception.retry);
2652 return err;
2653}
2654
2655static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2656{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002657 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 struct nfs4_link_arg arg = {
2659 .fh = NFS_FH(inode),
2660 .dir_fh = NFS_FH(dir),
2661 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002662 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002664 struct nfs4_link_res res = {
2665 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002666 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 struct rpc_message msg = {
2668 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2669 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002670 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002672 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Trond Myklebust136f2622010-04-16 16:22:49 -04002674 res.fattr = nfs_alloc_fattr();
2675 res.dir_attr = nfs_alloc_fattr();
2676 if (res.fattr == NULL || res.dir_attr == NULL)
2677 goto out;
2678
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002679 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002680 if (!status) {
2681 update_changeattr(dir, &res.cinfo);
2682 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002683 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002684 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002685out:
2686 nfs_free_fattr(res.dir_attr);
2687 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 return status;
2689}
2690
2691static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2692{
2693 struct nfs4_exception exception = { };
2694 int err;
2695 do {
2696 err = nfs4_handle_exception(NFS_SERVER(inode),
2697 _nfs4_proc_link(inode, dir, name),
2698 &exception);
2699 } while (exception.retry);
2700 return err;
2701}
2702
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002703struct nfs4_createdata {
2704 struct rpc_message msg;
2705 struct nfs4_create_arg arg;
2706 struct nfs4_create_res res;
2707 struct nfs_fh fh;
2708 struct nfs_fattr fattr;
2709 struct nfs_fattr dir_fattr;
2710};
2711
2712static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2713 struct qstr *name, struct iattr *sattr, u32 ftype)
2714{
2715 struct nfs4_createdata *data;
2716
2717 data = kzalloc(sizeof(*data), GFP_KERNEL);
2718 if (data != NULL) {
2719 struct nfs_server *server = NFS_SERVER(dir);
2720
2721 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2722 data->msg.rpc_argp = &data->arg;
2723 data->msg.rpc_resp = &data->res;
2724 data->arg.dir_fh = NFS_FH(dir);
2725 data->arg.server = server;
2726 data->arg.name = name;
2727 data->arg.attrs = sattr;
2728 data->arg.ftype = ftype;
2729 data->arg.bitmask = server->attr_bitmask;
2730 data->res.server = server;
2731 data->res.fh = &data->fh;
2732 data->res.fattr = &data->fattr;
2733 data->res.dir_fattr = &data->dir_fattr;
2734 nfs_fattr_init(data->res.fattr);
2735 nfs_fattr_init(data->res.dir_fattr);
2736 }
2737 return data;
2738}
2739
2740static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2741{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002742 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2743 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002744 if (status == 0) {
2745 update_changeattr(dir, &data->res.dir_cinfo);
2746 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2747 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2748 }
2749 return status;
2750}
2751
2752static void nfs4_free_createdata(struct nfs4_createdata *data)
2753{
2754 kfree(data);
2755}
2756
Chuck Lever4f390c12006-08-22 20:06:22 -04002757static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002758 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002760 struct nfs4_createdata *data;
2761 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
Chuck Lever94a6d752006-08-22 20:06:23 -04002763 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002764 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002765
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002766 status = -ENOMEM;
2767 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2768 if (data == NULL)
2769 goto out;
2770
2771 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2772 data->arg.u.symlink.pages = &page;
2773 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002775 status = nfs4_do_create(dir, dentry, data);
2776
2777 nfs4_free_createdata(data);
2778out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 return status;
2780}
2781
Chuck Lever4f390c12006-08-22 20:06:22 -04002782static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002783 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784{
2785 struct nfs4_exception exception = { };
2786 int err;
2787 do {
2788 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002789 _nfs4_proc_symlink(dir, dentry, page,
2790 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 &exception);
2792 } while (exception.retry);
2793 return err;
2794}
2795
2796static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2797 struct iattr *sattr)
2798{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002799 struct nfs4_createdata *data;
2800 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002802 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2803 if (data == NULL)
2804 goto out;
2805
2806 status = nfs4_do_create(dir, dentry, data);
2807
2808 nfs4_free_createdata(data);
2809out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 return status;
2811}
2812
2813static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2814 struct iattr *sattr)
2815{
2816 struct nfs4_exception exception = { };
2817 int err;
2818 do {
2819 err = nfs4_handle_exception(NFS_SERVER(dir),
2820 _nfs4_proc_mkdir(dir, dentry, sattr),
2821 &exception);
2822 } while (exception.retry);
2823 return err;
2824}
2825
2826static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002827 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
2829 struct inode *dir = dentry->d_inode;
2830 struct nfs4_readdir_arg args = {
2831 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002832 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 .pgbase = 0,
2834 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002835 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002836 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 };
2838 struct nfs4_readdir_res res;
2839 struct rpc_message msg = {
2840 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2841 .rpc_argp = &args,
2842 .rpc_resp = &res,
2843 .rpc_cred = cred,
2844 };
2845 int status;
2846
Harvey Harrison3110ff82008-05-02 13:42:44 -07002847 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002848 dentry->d_parent->d_name.name,
2849 dentry->d_name.name,
2850 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2852 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002853 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 if (status == 0)
2855 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002856
2857 nfs_invalidate_atime(dir);
2858
Harvey Harrison3110ff82008-05-02 13:42:44 -07002859 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 return status;
2861}
2862
2863static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002864 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865{
2866 struct nfs4_exception exception = { };
2867 int err;
2868 do {
2869 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2870 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002871 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 &exception);
2873 } while (exception.retry);
2874 return err;
2875}
2876
2877static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2878 struct iattr *sattr, dev_t rdev)
2879{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002880 struct nfs4_createdata *data;
2881 int mode = sattr->ia_mode;
2882 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
2884 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2885 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002886
2887 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2888 if (data == NULL)
2889 goto out;
2890
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002892 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002894 data->arg.ftype = NF4BLK;
2895 data->arg.u.device.specdata1 = MAJOR(rdev);
2896 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 }
2898 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002899 data->arg.ftype = NF4CHR;
2900 data->arg.u.device.specdata1 = MAJOR(rdev);
2901 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002904 status = nfs4_do_create(dir, dentry, data);
2905
2906 nfs4_free_createdata(data);
2907out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 return status;
2909}
2910
2911static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2912 struct iattr *sattr, dev_t rdev)
2913{
2914 struct nfs4_exception exception = { };
2915 int err;
2916 do {
2917 err = nfs4_handle_exception(NFS_SERVER(dir),
2918 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2919 &exception);
2920 } while (exception.retry);
2921 return err;
2922}
2923
2924static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2925 struct nfs_fsstat *fsstat)
2926{
2927 struct nfs4_statfs_arg args = {
2928 .fh = fhandle,
2929 .bitmask = server->attr_bitmask,
2930 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002931 struct nfs4_statfs_res res = {
2932 .fsstat = fsstat,
2933 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 struct rpc_message msg = {
2935 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2936 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002937 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 };
2939
Trond Myklebust0e574af2005-10-27 22:12:38 -04002940 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002941 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942}
2943
2944static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2945{
2946 struct nfs4_exception exception = { };
2947 int err;
2948 do {
2949 err = nfs4_handle_exception(server,
2950 _nfs4_proc_statfs(server, fhandle, fsstat),
2951 &exception);
2952 } while (exception.retry);
2953 return err;
2954}
2955
2956static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2957 struct nfs_fsinfo *fsinfo)
2958{
2959 struct nfs4_fsinfo_arg args = {
2960 .fh = fhandle,
2961 .bitmask = server->attr_bitmask,
2962 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002963 struct nfs4_fsinfo_res res = {
2964 .fsinfo = fsinfo,
2965 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 struct rpc_message msg = {
2967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2968 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002969 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 };
2971
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002972 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973}
2974
2975static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2976{
2977 struct nfs4_exception exception = { };
2978 int err;
2979
2980 do {
2981 err = nfs4_handle_exception(server,
2982 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2983 &exception);
2984 } while (exception.retry);
2985 return err;
2986}
2987
2988static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2989{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002990 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2992}
2993
2994static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2995 struct nfs_pathconf *pathconf)
2996{
2997 struct nfs4_pathconf_arg args = {
2998 .fh = fhandle,
2999 .bitmask = server->attr_bitmask,
3000 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003001 struct nfs4_pathconf_res res = {
3002 .pathconf = pathconf,
3003 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 struct rpc_message msg = {
3005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3006 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003007 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 };
3009
3010 /* None of the pathconf attributes are mandatory to implement */
3011 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3012 memset(pathconf, 0, sizeof(*pathconf));
3013 return 0;
3014 }
3015
Trond Myklebust0e574af2005-10-27 22:12:38 -04003016 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003017 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018}
3019
3020static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3021 struct nfs_pathconf *pathconf)
3022{
3023 struct nfs4_exception exception = { };
3024 int err;
3025
3026 do {
3027 err = nfs4_handle_exception(server,
3028 _nfs4_proc_pathconf(server, fhandle, pathconf),
3029 &exception);
3030 } while (exception.retry);
3031 return err;
3032}
3033
Trond Myklebustec06c092006-03-20 13:44:27 -05003034static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035{
Trond Myklebustec06c092006-03-20 13:44:27 -05003036 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003038 dprintk("--> %s\n", __func__);
3039
Trond Myklebust14516c32010-07-31 14:29:06 -04003040 if (!nfs4_sequence_done(task, &data->res.seq_res))
3041 return -EAGAIN;
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003042
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003043 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003044 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003045 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003047
3048 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003050 renew_lease(server, data->timestamp);
3051 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052}
3053
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003054static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003057 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058}
3059
Trond Myklebust788e7a82006-03-20 13:44:27 -05003060static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 struct inode *inode = data->inode;
3063
Trond Myklebust14516c32010-07-31 14:29:06 -04003064 if (!nfs4_sequence_done(task, &data->res.seq_res))
3065 return -EAGAIN;
Andy Adamsondef6ed72009-04-01 09:22:26 -04003066
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003067 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003068 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003069 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003071 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003073 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003074 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003075 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076}
3077
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003078static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003080 struct nfs_server *server = NFS_SERVER(data->inode);
3081
Trond Myklebusta65318b2009-03-11 14:10:28 -04003082 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003083 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 data->timestamp = jiffies;
3085
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003086 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087}
3088
Trond Myklebust788e7a82006-03-20 13:44:27 -05003089static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 struct inode *inode = data->inode;
3092
Trond Myklebust14516c32010-07-31 14:29:06 -04003093 if (!nfs4_sequence_done(task, &data->res.seq_res))
3094 return -EAGAIN;
3095
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003096 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003097 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003098 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003100 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102}
3103
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003104static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003106 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Trond Myklebusta65318b2009-03-11 14:10:28 -04003108 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003109 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003110 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111}
3112
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003113struct nfs4_renewdata {
3114 struct nfs_client *client;
3115 unsigned long timestamp;
3116};
3117
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118/*
3119 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3120 * standalone procedure for queueing an asynchronous RENEW.
3121 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003122static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003123{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003124 struct nfs4_renewdata *data = calldata;
3125 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003126
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003127 if (atomic_read(&clp->cl_count) > 1)
3128 nfs4_schedule_state_renewal(clp);
3129 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003130 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003131}
3132
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003133static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003135 struct nfs4_renewdata *data = calldata;
3136 struct nfs_client *clp = data->client;
3137 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
3139 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003140 /* Unless we're shutting down, schedule state recovery! */
3141 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3142 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 return;
3144 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003145 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146}
3147
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003148static const struct rpc_call_ops nfs4_renew_ops = {
3149 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003150 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003151};
3152
David Howellsadfa6f92006-08-22 20:06:08 -04003153int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154{
3155 struct rpc_message msg = {
3156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3157 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003158 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003160 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003162 if (!atomic_inc_not_zero(&clp->cl_count))
3163 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003164 data = kmalloc(sizeof(*data), GFP_KERNEL);
3165 if (data == NULL)
3166 return -ENOMEM;
3167 data->client = clp;
3168 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003170 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171}
3172
David Howellsadfa6f92006-08-22 20:06:08 -04003173int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174{
3175 struct rpc_message msg = {
3176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3177 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003178 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 };
3180 unsigned long now = jiffies;
3181 int status;
3182
3183 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3184 if (status < 0)
3185 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003186 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 return 0;
3188}
3189
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003190static inline int nfs4_server_supports_acls(struct nfs_server *server)
3191{
3192 return (server->caps & NFS_CAP_ACLS)
3193 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3194 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3195}
3196
3197/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3198 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3199 * the stack.
3200 */
3201#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3202
3203static void buf_to_pages(const void *buf, size_t buflen,
3204 struct page **pages, unsigned int *pgbase)
3205{
3206 const void *p = buf;
3207
3208 *pgbase = offset_in_page(buf);
3209 p -= *pgbase;
3210 while (p < buf + buflen) {
3211 *(pages++) = virt_to_page(p);
3212 p += PAGE_CACHE_SIZE;
3213 }
3214}
3215
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003216struct nfs4_cached_acl {
3217 int cached;
3218 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003219 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003220};
3221
3222static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003223{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003224 struct nfs_inode *nfsi = NFS_I(inode);
3225
3226 spin_lock(&inode->i_lock);
3227 kfree(nfsi->nfs4_acl);
3228 nfsi->nfs4_acl = acl;
3229 spin_unlock(&inode->i_lock);
3230}
3231
3232static void nfs4_zap_acl_attr(struct inode *inode)
3233{
3234 nfs4_set_cached_acl(inode, NULL);
3235}
3236
3237static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3238{
3239 struct nfs_inode *nfsi = NFS_I(inode);
3240 struct nfs4_cached_acl *acl;
3241 int ret = -ENOENT;
3242
3243 spin_lock(&inode->i_lock);
3244 acl = nfsi->nfs4_acl;
3245 if (acl == NULL)
3246 goto out;
3247 if (buf == NULL) /* user is just asking for length */
3248 goto out_len;
3249 if (acl->cached == 0)
3250 goto out;
3251 ret = -ERANGE; /* see getxattr(2) man page */
3252 if (acl->len > buflen)
3253 goto out;
3254 memcpy(buf, acl->data, acl->len);
3255out_len:
3256 ret = acl->len;
3257out:
3258 spin_unlock(&inode->i_lock);
3259 return ret;
3260}
3261
3262static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3263{
3264 struct nfs4_cached_acl *acl;
3265
3266 if (buf && acl_len <= PAGE_SIZE) {
3267 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3268 if (acl == NULL)
3269 goto out;
3270 acl->cached = 1;
3271 memcpy(acl->data, buf, acl_len);
3272 } else {
3273 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3274 if (acl == NULL)
3275 goto out;
3276 acl->cached = 0;
3277 }
3278 acl->len = acl_len;
3279out:
3280 nfs4_set_cached_acl(inode, acl);
3281}
3282
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003283static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003284{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003285 struct page *pages[NFS4ACL_MAXPAGES];
3286 struct nfs_getaclargs args = {
3287 .fh = NFS_FH(inode),
3288 .acl_pages = pages,
3289 .acl_len = buflen,
3290 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003291 struct nfs_getaclres res = {
3292 .acl_len = buflen,
3293 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003294 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003295 struct rpc_message msg = {
3296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3297 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003298 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003299 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003300 struct page *localpage = NULL;
3301 int ret;
3302
3303 if (buflen < PAGE_SIZE) {
3304 /* As long as we're doing a round trip to the server anyway,
3305 * let's be prepared for a page of acl data. */
3306 localpage = alloc_page(GFP_KERNEL);
3307 resp_buf = page_address(localpage);
3308 if (localpage == NULL)
3309 return -ENOMEM;
3310 args.acl_pages[0] = localpage;
3311 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003312 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003313 } else {
3314 resp_buf = buf;
3315 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3316 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003317 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003318 if (ret)
3319 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003320 if (res.acl_len > args.acl_len)
3321 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003322 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003323 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003324 if (buf) {
3325 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003326 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003327 goto out_free;
3328 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003329 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003330 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003331 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003332out_free:
3333 if (localpage)
3334 __free_page(localpage);
3335 return ret;
3336}
3337
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003338static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3339{
3340 struct nfs4_exception exception = { };
3341 ssize_t ret;
3342 do {
3343 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3344 if (ret >= 0)
3345 break;
3346 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3347 } while (exception.retry);
3348 return ret;
3349}
3350
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003351static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3352{
3353 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003354 int ret;
3355
3356 if (!nfs4_server_supports_acls(server))
3357 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003358 ret = nfs_revalidate_inode(server, inode);
3359 if (ret < 0)
3360 return ret;
3361 ret = nfs4_read_cached_acl(inode, buf, buflen);
3362 if (ret != -ENOENT)
3363 return ret;
3364 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003365}
3366
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003367static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003368{
3369 struct nfs_server *server = NFS_SERVER(inode);
3370 struct page *pages[NFS4ACL_MAXPAGES];
3371 struct nfs_setaclargs arg = {
3372 .fh = NFS_FH(inode),
3373 .acl_pages = pages,
3374 .acl_len = buflen,
3375 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003376 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003377 struct rpc_message msg = {
3378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3379 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003380 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003381 };
3382 int ret;
3383
3384 if (!nfs4_server_supports_acls(server))
3385 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003386 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003387 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003388 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003389 nfs_access_zap_cache(inode);
3390 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003391 return ret;
3392}
3393
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003394static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3395{
3396 struct nfs4_exception exception = { };
3397 int err;
3398 do {
3399 err = nfs4_handle_exception(NFS_SERVER(inode),
3400 __nfs4_proc_set_acl(inode, buf, buflen),
3401 &exception);
3402 } while (exception.retry);
3403 return err;
3404}
3405
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003407nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003409 struct nfs_client *clp = server->nfs_client;
3410
3411 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 return 0;
3413 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003414 case -NFS4ERR_ADMIN_REVOKED:
3415 case -NFS4ERR_BAD_STATEID:
3416 case -NFS4ERR_OPENMODE:
3417 if (state == NULL)
3418 break;
3419 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003420 goto do_state_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003422 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003424 goto do_state_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003425#if defined(CONFIG_NFS_V4_1)
3426 case -NFS4ERR_BADSESSION:
3427 case -NFS4ERR_BADSLOT:
3428 case -NFS4ERR_BAD_HIGH_SLOT:
3429 case -NFS4ERR_DEADSESSION:
3430 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3431 case -NFS4ERR_SEQ_FALSE_RETRY:
3432 case -NFS4ERR_SEQ_MISORDERED:
3433 dprintk("%s ERROR %d, Reset session\n", __func__,
3434 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003435 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003436 task->tk_status = 0;
3437 return -EAGAIN;
3438#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003440 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003441 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003442 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3444 task->tk_status = 0;
3445 return -EAGAIN;
3446 case -NFS4ERR_OLD_STATEID:
3447 task->tk_status = 0;
3448 return -EAGAIN;
3449 }
3450 task->tk_status = nfs4_map_errors(task->tk_status);
3451 return 0;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003452do_state_recovery:
3453 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3454 nfs4_schedule_state_recovery(clp);
3455 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3456 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3457 task->tk_status = 0;
3458 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459}
3460
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003461int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3462 unsigned short port, struct rpc_cred *cred,
3463 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464{
3465 nfs4_verifier sc_verifier;
3466 struct nfs4_setclientid setclientid = {
3467 .sc_verifier = &sc_verifier,
3468 .sc_prog = program,
3469 };
3470 struct rpc_message msg = {
3471 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3472 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003473 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003474 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 };
Al Virobc4785c2006-10-19 23:28:51 -07003476 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 int loop = 0;
3478 int status;
3479
Al Virobc4785c2006-10-19 23:28:51 -07003480 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3482 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3483
3484 for(;;) {
3485 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003486 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003487 clp->cl_ipaddr,
3488 rpc_peeraddr2str(clp->cl_rpcclient,
3489 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003490 rpc_peeraddr2str(clp->cl_rpcclient,
3491 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003492 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 clp->cl_id_uniquifier);
3494 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003495 sizeof(setclientid.sc_netid),
3496 rpc_peeraddr2str(clp->cl_rpcclient,
3497 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003499 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 clp->cl_ipaddr, port >> 8, port & 255);
3501
3502 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3503 if (status != -NFS4ERR_CLID_INUSE)
3504 break;
3505 if (signalled())
3506 break;
3507 if (loop++ & 1)
3508 ssleep(clp->cl_lease_time + 1);
3509 else
3510 if (++clp->cl_id_uniquifier == 0)
3511 break;
3512 }
3513 return status;
3514}
3515
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003516static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3517 struct nfs4_setclientid_res *arg,
3518 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519{
3520 struct nfs_fsinfo fsinfo;
3521 struct rpc_message msg = {
3522 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003523 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003525 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 };
3527 unsigned long now;
3528 int status;
3529
3530 now = jiffies;
3531 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3532 if (status == 0) {
3533 spin_lock(&clp->cl_lock);
3534 clp->cl_lease_time = fsinfo.lease_time * HZ;
3535 clp->cl_last_renewal = now;
3536 spin_unlock(&clp->cl_lock);
3537 }
3538 return status;
3539}
3540
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003541int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3542 struct nfs4_setclientid_res *arg,
3543 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003544{
Benny Halevy77e03672008-06-24 20:25:57 +03003545 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003546 int err;
3547 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003548 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003549 switch (err) {
3550 case 0:
3551 return err;
3552 case -NFS4ERR_RESOURCE:
3553 /* The IBM lawyers misread another document! */
3554 case -NFS4ERR_DELAY:
3555 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3556 }
3557 } while (err == 0);
3558 return err;
3559}
3560
Trond Myklebustfe650402006-01-03 09:55:18 +01003561struct nfs4_delegreturndata {
3562 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003563 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003564 struct nfs_fh fh;
3565 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003566 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003567 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003568 int rpc_status;
3569};
3570
Trond Myklebustfe650402006-01-03 09:55:18 +01003571static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3572{
3573 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003574
Trond Myklebust14516c32010-07-31 14:29:06 -04003575 if (!nfs4_sequence_done(task, &data->res.seq_res))
3576 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003577
Ricardo Labiaga79708862009-12-07 09:23:21 -05003578 switch (task->tk_status) {
3579 case -NFS4ERR_STALE_STATEID:
3580 case -NFS4ERR_EXPIRED:
3581 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003582 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003583 break;
3584 default:
3585 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3586 -EAGAIN) {
3587 nfs_restart_rpc(task, data->res.server->nfs_client);
3588 return;
3589 }
3590 }
3591 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003592}
3593
3594static void nfs4_delegreturn_release(void *calldata)
3595{
Trond Myklebustfe650402006-01-03 09:55:18 +01003596 kfree(calldata);
3597}
3598
Andy Adamson938e1012009-04-01 09:22:28 -04003599#if defined(CONFIG_NFS_V4_1)
3600static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3601{
3602 struct nfs4_delegreturndata *d_data;
3603
3604 d_data = (struct nfs4_delegreturndata *)data;
3605
Trond Myklebust035168a2010-06-16 09:52:26 -04003606 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003607 &d_data->args.seq_args,
3608 &d_data->res.seq_res, 1, task))
3609 return;
3610 rpc_call_start(task);
3611}
3612#endif /* CONFIG_NFS_V4_1 */
3613
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003614static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003615#if defined(CONFIG_NFS_V4_1)
3616 .rpc_call_prepare = nfs4_delegreturn_prepare,
3617#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003618 .rpc_call_done = nfs4_delegreturn_done,
3619 .rpc_release = nfs4_delegreturn_release,
3620};
3621
Trond Myklebuste6f81072008-01-24 18:14:34 -05003622static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003623{
3624 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003625 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003626 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003627 struct rpc_message msg = {
3628 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3629 .rpc_cred = cred,
3630 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003631 struct rpc_task_setup task_setup_data = {
3632 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003633 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003634 .callback_ops = &nfs4_delegreturn_ops,
3635 .flags = RPC_TASK_ASYNC,
3636 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003637 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003638
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003639 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003640 if (data == NULL)
3641 return -ENOMEM;
3642 data->args.fhandle = &data->fh;
3643 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003644 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003645 nfs_copy_fh(&data->fh, NFS_FH(inode));
3646 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003647 data->res.fattr = &data->fattr;
3648 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003649 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003650 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003651 data->rpc_status = 0;
3652
Trond Myklebustc970aa82007-07-14 15:39:59 -04003653 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003654 msg.rpc_argp = &data->args,
3655 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003656 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003657 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003658 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003659 if (!issync)
3660 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003661 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003662 if (status != 0)
3663 goto out;
3664 status = data->rpc_status;
3665 if (status != 0)
3666 goto out;
3667 nfs_refresh_inode(inode, &data->fattr);
3668out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003669 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003670 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671}
3672
Trond Myklebuste6f81072008-01-24 18:14:34 -05003673int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674{
3675 struct nfs_server *server = NFS_SERVER(inode);
3676 struct nfs4_exception exception = { };
3677 int err;
3678 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003679 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 switch (err) {
3681 case -NFS4ERR_STALE_STATEID:
3682 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 case 0:
3684 return 0;
3685 }
3686 err = nfs4_handle_exception(server, err, &exception);
3687 } while (exception.retry);
3688 return err;
3689}
3690
3691#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3692#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3693
3694/*
3695 * sleep, with exponential backoff, and retry the LOCK operation.
3696 */
3697static unsigned long
3698nfs4_set_lock_task_retry(unsigned long timeout)
3699{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003700 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 timeout <<= 1;
3702 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3703 return NFS4_LOCK_MAXTIMEOUT;
3704 return timeout;
3705}
3706
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3708{
3709 struct inode *inode = state->inode;
3710 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003711 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003712 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003714 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003716 struct nfs_lockt_res res = {
3717 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 };
3719 struct rpc_message msg = {
3720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3721 .rpc_argp = &arg,
3722 .rpc_resp = &res,
3723 .rpc_cred = state->owner->so_cred,
3724 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 struct nfs4_lock_state *lsp;
3726 int status;
3727
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003728 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003729 status = nfs4_set_lock_state(state, request);
3730 if (status != 0)
3731 goto out;
3732 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003733 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003734 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003735 switch (status) {
3736 case 0:
3737 request->fl_type = F_UNLCK;
3738 break;
3739 case -NFS4ERR_DENIED:
3740 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003742 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003743out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 return status;
3745}
3746
3747static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3748{
3749 struct nfs4_exception exception = { };
3750 int err;
3751
3752 do {
3753 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3754 _nfs4_proc_getlk(state, cmd, request),
3755 &exception);
3756 } while (exception.retry);
3757 return err;
3758}
3759
3760static int do_vfs_lock(struct file *file, struct file_lock *fl)
3761{
3762 int res = 0;
3763 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3764 case FL_POSIX:
3765 res = posix_lock_file_wait(file, fl);
3766 break;
3767 case FL_FLOCK:
3768 res = flock_lock_file_wait(file, fl);
3769 break;
3770 default:
3771 BUG();
3772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 return res;
3774}
3775
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003776struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003777 struct nfs_locku_args arg;
3778 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003779 struct nfs4_lock_state *lsp;
3780 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003781 struct file_lock fl;
3782 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003783 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003784};
3785
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003786static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3787 struct nfs_open_context *ctx,
3788 struct nfs4_lock_state *lsp,
3789 struct nfs_seqid *seqid)
3790{
3791 struct nfs4_unlockdata *p;
3792 struct inode *inode = lsp->ls_state->inode;
3793
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003794 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003795 if (p == NULL)
3796 return NULL;
3797 p->arg.fh = NFS_FH(inode);
3798 p->arg.fl = &p->fl;
3799 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003800 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003801 p->arg.stateid = &lsp->ls_stateid;
3802 p->lsp = lsp;
3803 atomic_inc(&lsp->ls_count);
3804 /* Ensure we don't close file until we're done freeing locks! */
3805 p->ctx = get_nfs_open_context(ctx);
3806 memcpy(&p->fl, fl, sizeof(p->fl));
3807 p->server = NFS_SERVER(inode);
3808 return p;
3809}
3810
Trond Myklebust06f814a2006-01-03 09:55:07 +01003811static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003812{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003813 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003814 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003815 nfs4_put_lock_state(calldata->lsp);
3816 put_nfs_open_context(calldata->ctx);
3817 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003818}
3819
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003820static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003821{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003822 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003823
Trond Myklebust14516c32010-07-31 14:29:06 -04003824 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3825 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003826 switch (task->tk_status) {
3827 case 0:
3828 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003829 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003830 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003831 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003832 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003833 case -NFS4ERR_BAD_STATEID:
3834 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003835 case -NFS4ERR_STALE_STATEID:
3836 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003837 break;
3838 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003839 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003840 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003841 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003842 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003843}
3844
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003845static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003846{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003847 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003849 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003850 return;
3851 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003852 /* Note: exit _without_ running nfs4_locku_done */
3853 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003854 return;
3855 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003856 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04003857 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04003858 &calldata->arg.seq_args,
3859 &calldata->res.seq_res, 1, task))
3860 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003861 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862}
3863
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003864static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003865 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003866 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003867 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003868};
3869
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003870static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3871 struct nfs_open_context *ctx,
3872 struct nfs4_lock_state *lsp,
3873 struct nfs_seqid *seqid)
3874{
3875 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003876 struct rpc_message msg = {
3877 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3878 .rpc_cred = ctx->cred,
3879 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003880 struct rpc_task_setup task_setup_data = {
3881 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003882 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003883 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003884 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003885 .flags = RPC_TASK_ASYNC,
3886 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003887
Frank Filz137d6ac2007-07-09 15:32:29 -07003888 /* Ensure this is an unlock - when canceling a lock, the
3889 * canceled lock is passed in, and it won't be an unlock.
3890 */
3891 fl->fl_type = F_UNLCK;
3892
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003893 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3894 if (data == NULL) {
3895 nfs_free_seqid(seqid);
3896 return ERR_PTR(-ENOMEM);
3897 }
3898
Trond Myklebust5138fde2007-07-14 15:40:01 -04003899 msg.rpc_argp = &data->arg,
3900 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003901 task_setup_data.callback_data = data;
3902 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003903}
3904
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3906{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003907 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003908 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003909 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003910 struct rpc_task *task;
3911 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003912 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
Trond Myklebust9b073572006-06-29 16:38:34 -04003914 status = nfs4_set_lock_state(state, request);
3915 /* Unlock _before_ we do the RPC call */
3916 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003917 down_read(&nfsi->rwsem);
3918 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3919 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003920 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003921 }
3922 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003923 if (status != 0)
3924 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003925 /* Is this a delegated lock? */
3926 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003927 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003928 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003929 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04003930 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003931 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003932 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003933 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003934 status = PTR_ERR(task);
3935 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003936 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003937 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003938 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003939out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003940 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003941 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942}
3943
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003944struct nfs4_lockdata {
3945 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003946 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003947 struct nfs4_lock_state *lsp;
3948 struct nfs_open_context *ctx;
3949 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003950 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003951 int rpc_status;
3952 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04003953 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003954};
3955
3956static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003957 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
3958 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003959{
3960 struct nfs4_lockdata *p;
3961 struct inode *inode = lsp->ls_state->inode;
3962 struct nfs_server *server = NFS_SERVER(inode);
3963
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003964 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003965 if (p == NULL)
3966 return NULL;
3967
3968 p->arg.fh = NFS_FH(inode);
3969 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003970 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003971 if (p->arg.open_seqid == NULL)
3972 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003973 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003974 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003975 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003976 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003977 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003978 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003979 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003980 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04003981 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003982 atomic_inc(&lsp->ls_count);
3983 p->ctx = get_nfs_open_context(ctx);
3984 memcpy(&p->fl, fl, sizeof(p->fl));
3985 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003986out_free_seqid:
3987 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003988out_free:
3989 kfree(p);
3990 return NULL;
3991}
3992
3993static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3994{
3995 struct nfs4_lockdata *data = calldata;
3996 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
Harvey Harrison3110ff82008-05-02 13:42:44 -07003998 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003999 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4000 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004001 /* Do we need to do an open_to_lock_owner? */
4002 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004003 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4004 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004005 data->arg.open_stateid = &state->stateid;
4006 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004007 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004008 } else
4009 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004010 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004011 if (nfs4_setup_sequence(data->server,
4012 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004013 &data->res.seq_res, 1, task))
4014 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004015 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004016 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004017}
4018
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004019static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4020{
4021 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4022 nfs4_lock_prepare(task, calldata);
4023}
4024
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004025static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4026{
4027 struct nfs4_lockdata *data = calldata;
4028
Harvey Harrison3110ff82008-05-02 13:42:44 -07004029 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004030
Trond Myklebust14516c32010-07-31 14:29:06 -04004031 if (!nfs4_sequence_done(task, &data->res.seq_res))
4032 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004033
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004034 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004035 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004036 if (data->rpc_status == 0)
4037 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4038 else
4039 goto out;
4040 }
4041 if (data->rpc_status == 0) {
4042 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4043 sizeof(data->lsp->ls_stateid.data));
4044 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004045 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004046 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004047out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004048 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004049}
4050
4051static void nfs4_lock_release(void *calldata)
4052{
4053 struct nfs4_lockdata *data = calldata;
4054
Harvey Harrison3110ff82008-05-02 13:42:44 -07004055 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004056 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004057 if (data->cancelled != 0) {
4058 struct rpc_task *task;
4059 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4060 data->arg.lock_seqid);
4061 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004062 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004063 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004064 } else
4065 nfs_free_seqid(data->arg.lock_seqid);
4066 nfs4_put_lock_state(data->lsp);
4067 put_nfs_open_context(data->ctx);
4068 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004069 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004070}
4071
4072static const struct rpc_call_ops nfs4_lock_ops = {
4073 .rpc_call_prepare = nfs4_lock_prepare,
4074 .rpc_call_done = nfs4_lock_done,
4075 .rpc_release = nfs4_lock_release,
4076};
4077
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004078static const struct rpc_call_ops nfs4_recover_lock_ops = {
4079 .rpc_call_prepare = nfs4_recover_lock_prepare,
4080 .rpc_call_done = nfs4_lock_done,
4081 .rpc_release = nfs4_lock_release,
4082};
4083
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004084static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4085{
4086 struct nfs_client *clp = server->nfs_client;
4087 struct nfs4_state *state = lsp->ls_state;
4088
4089 switch (error) {
4090 case -NFS4ERR_ADMIN_REVOKED:
4091 case -NFS4ERR_BAD_STATEID:
4092 case -NFS4ERR_EXPIRED:
4093 if (new_lock_owner != 0 ||
4094 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4095 nfs4_state_mark_reclaim_nograce(clp, state);
4096 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004097 break;
4098 case -NFS4ERR_STALE_STATEID:
4099 if (new_lock_owner != 0 ||
4100 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4101 nfs4_state_mark_reclaim_reboot(clp, state);
4102 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004103 };
4104}
4105
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004106static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004107{
4108 struct nfs4_lockdata *data;
4109 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004110 struct rpc_message msg = {
4111 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4112 .rpc_cred = state->owner->so_cred,
4113 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004114 struct rpc_task_setup task_setup_data = {
4115 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004116 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004117 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004118 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004119 .flags = RPC_TASK_ASYNC,
4120 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004121 int ret;
4122
Harvey Harrison3110ff82008-05-02 13:42:44 -07004123 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004124 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004125 fl->fl_u.nfs4_fl.owner,
4126 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004127 if (data == NULL)
4128 return -ENOMEM;
4129 if (IS_SETLKW(cmd))
4130 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004131 if (recovery_type > NFS_LOCK_NEW) {
4132 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004133 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004134 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4135 }
Trond Myklebust5138fde2007-07-14 15:40:01 -04004136 msg.rpc_argp = &data->arg,
4137 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004138 task_setup_data.callback_data = data;
4139 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004140 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004141 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004142 ret = nfs4_wait_for_completion_rpc_task(task);
4143 if (ret == 0) {
4144 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004145 if (ret)
4146 nfs4_handle_setlk_error(data->server, data->lsp,
4147 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004148 } else
4149 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004150 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004151 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004152 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153}
4154
4155static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4156{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004157 struct nfs_server *server = NFS_SERVER(state->inode);
4158 struct nfs4_exception exception = { };
4159 int err;
4160
4161 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004162 /* Cache the lock if possible... */
4163 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4164 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004165 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004166 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004167 break;
4168 nfs4_handle_exception(server, err, &exception);
4169 } while (exception.retry);
4170 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171}
4172
4173static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4174{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004175 struct nfs_server *server = NFS_SERVER(state->inode);
4176 struct nfs4_exception exception = { };
4177 int err;
4178
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004179 err = nfs4_set_lock_state(state, request);
4180 if (err != 0)
4181 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004182 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004183 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4184 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004185 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004186 switch (err) {
4187 default:
4188 goto out;
4189 case -NFS4ERR_GRACE:
4190 case -NFS4ERR_DELAY:
4191 nfs4_handle_exception(server, err, &exception);
4192 err = 0;
4193 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004194 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004195out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004196 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197}
4198
4199static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4200{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004201 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004202 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004203 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004205 if ((fl_flags & FL_POSIX) &&
4206 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4207 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004208 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004209 status = nfs4_set_lock_state(state, request);
4210 if (status != 0)
4211 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004212 request->fl_flags |= FL_ACCESS;
4213 status = do_vfs_lock(request->fl_file, request);
4214 if (status < 0)
4215 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004216 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004217 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004218 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004219 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004220 request->fl_flags = fl_flags & ~FL_SLEEP;
4221 status = do_vfs_lock(request->fl_file, request);
4222 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004223 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004224 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004225 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004226 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004227 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004228 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004229 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004230 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004231out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004232 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004233out:
4234 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 return status;
4236}
4237
4238static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4239{
4240 struct nfs4_exception exception = { };
4241 int err;
4242
4243 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004244 err = _nfs4_proc_setlk(state, cmd, request);
4245 if (err == -NFS4ERR_DENIED)
4246 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004248 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 } while (exception.retry);
4250 return err;
4251}
4252
4253static int
4254nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4255{
4256 struct nfs_open_context *ctx;
4257 struct nfs4_state *state;
4258 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4259 int status;
4260
4261 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004262 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 state = ctx->state;
4264
4265 if (request->fl_start < 0 || request->fl_end < 0)
4266 return -EINVAL;
4267
Trond Myklebustd9531262009-07-21 19:22:38 -04004268 if (IS_GETLK(cmd)) {
4269 if (state != NULL)
4270 return nfs4_proc_getlk(state, F_GETLK, request);
4271 return 0;
4272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
4274 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4275 return -EINVAL;
4276
Trond Myklebustd9531262009-07-21 19:22:38 -04004277 if (request->fl_type == F_UNLCK) {
4278 if (state != NULL)
4279 return nfs4_proc_unlck(state, cmd, request);
4280 return 0;
4281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282
Trond Myklebustd9531262009-07-21 19:22:38 -04004283 if (state == NULL)
4284 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 do {
4286 status = nfs4_proc_setlk(state, cmd, request);
4287 if ((status != -EAGAIN) || IS_SETLK(cmd))
4288 break;
4289 timeout = nfs4_set_lock_task_retry(timeout);
4290 status = -ERESTARTSYS;
4291 if (signalled())
4292 break;
4293 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 return status;
4295}
4296
Trond Myklebust888e6942005-11-04 15:38:11 -05004297int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4298{
4299 struct nfs_server *server = NFS_SERVER(state->inode);
4300 struct nfs4_exception exception = { };
4301 int err;
4302
4303 err = nfs4_set_lock_state(state, fl);
4304 if (err != 0)
4305 goto out;
4306 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004307 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004308 switch (err) {
4309 default:
4310 printk(KERN_ERR "%s: unhandled error %d.\n",
4311 __func__, err);
4312 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004313 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004314 goto out;
4315 case -NFS4ERR_EXPIRED:
4316 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004317 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004318 case -NFS4ERR_BADSESSION:
4319 case -NFS4ERR_BADSLOT:
4320 case -NFS4ERR_BAD_HIGH_SLOT:
4321 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4322 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004323 nfs4_schedule_state_recovery(server->nfs_client);
4324 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004325 case -ERESTARTSYS:
4326 /*
4327 * The show must go on: exit, but mark the
4328 * stateid as needing recovery.
4329 */
4330 case -NFS4ERR_ADMIN_REVOKED:
4331 case -NFS4ERR_BAD_STATEID:
4332 case -NFS4ERR_OPENMODE:
4333 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4334 err = 0;
4335 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004336 case -EKEYEXPIRED:
4337 /*
4338 * User RPCSEC_GSS context has expired.
4339 * We cannot recover this stateid now, so
4340 * skip it and allow recovery thread to
4341 * proceed.
4342 */
4343 err = 0;
4344 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004345 case -ENOMEM:
4346 case -NFS4ERR_DENIED:
4347 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4348 err = 0;
4349 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004350 case -NFS4ERR_DELAY:
4351 break;
4352 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004353 err = nfs4_handle_exception(server, err, &exception);
4354 } while (exception.retry);
4355out:
4356 return err;
4357}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004358
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004359static void nfs4_release_lockowner_release(void *calldata)
4360{
4361 kfree(calldata);
4362}
4363
4364const struct rpc_call_ops nfs4_release_lockowner_ops = {
4365 .rpc_release = nfs4_release_lockowner_release,
4366};
4367
4368void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4369{
4370 struct nfs_server *server = lsp->ls_state->owner->so_server;
4371 struct nfs_release_lockowner_args *args;
4372 struct rpc_message msg = {
4373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4374 };
4375
4376 if (server->nfs_client->cl_mvops->minor_version != 0)
4377 return;
4378 args = kmalloc(sizeof(*args), GFP_NOFS);
4379 if (!args)
4380 return;
4381 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4382 args->lock_owner.id = lsp->ls_id.id;
4383 msg.rpc_argp = args;
4384 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4385}
4386
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004387#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4388
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004389int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4390 size_t buflen, int flags)
4391{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004392 struct inode *inode = dentry->d_inode;
4393
4394 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4395 return -EOPNOTSUPP;
4396
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004397 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004398}
4399
4400/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4401 * and that's what we'll do for e.g. user attributes that haven't been set.
4402 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4403 * attributes in kernel-managed attribute namespaces. */
4404ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4405 size_t buflen)
4406{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004407 struct inode *inode = dentry->d_inode;
4408
4409 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4410 return -EOPNOTSUPP;
4411
4412 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004413}
4414
4415ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4416{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004417 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004418
J. Bruce Fields096455a2006-03-20 23:23:42 -05004419 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4420 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004421 if (buf && buflen < len)
4422 return -ERANGE;
4423 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004424 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4425 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004426}
4427
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004428static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4429{
4430 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4431 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4432 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4433 return;
4434
4435 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4436 NFS_ATTR_FATTR_NLINK;
4437 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4438 fattr->nlink = 2;
4439}
4440
Trond Myklebust56659e92007-07-17 21:52:39 -04004441int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004442 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004443{
4444 struct nfs_server *server = NFS_SERVER(dir);
4445 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004446 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4447 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004448 };
4449 struct nfs4_fs_locations_arg args = {
4450 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004451 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004452 .page = page,
4453 .bitmask = bitmask,
4454 };
Benny Halevy22958462009-04-01 09:22:02 -04004455 struct nfs4_fs_locations_res res = {
4456 .fs_locations = fs_locations,
4457 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004458 struct rpc_message msg = {
4459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4460 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004461 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004462 };
4463 int status;
4464
Harvey Harrison3110ff82008-05-02 13:42:44 -07004465 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004466 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004467 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004468 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004469 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004470 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004471 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004472 return status;
4473}
4474
Andy Adamson557134a2009-04-01 09:21:53 -04004475#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004476/*
4477 * nfs4_proc_exchange_id()
4478 *
4479 * Since the clientid has expired, all compounds using sessions
4480 * associated with the stale clientid will be returning
4481 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4482 * be in some phase of session reset.
4483 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004484int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004485{
4486 nfs4_verifier verifier;
4487 struct nfs41_exchange_id_args args = {
4488 .client = clp,
4489 .flags = clp->cl_exchange_flags,
4490 };
4491 struct nfs41_exchange_id_res res = {
4492 .client = clp,
4493 };
4494 int status;
4495 struct rpc_message msg = {
4496 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4497 .rpc_argp = &args,
4498 .rpc_resp = &res,
4499 .rpc_cred = cred,
4500 };
4501 __be32 *p;
4502
4503 dprintk("--> %s\n", __func__);
4504 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004505
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004506 /* Remove server-only flags */
4507 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4508
Benny Halevy99fe60d2009-04-01 09:22:29 -04004509 p = (u32 *)verifier.data;
4510 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4511 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4512 args.verifier = &verifier;
4513
4514 while (1) {
4515 args.id_len = scnprintf(args.id, sizeof(args.id),
4516 "%s/%s %u",
4517 clp->cl_ipaddr,
4518 rpc_peeraddr2str(clp->cl_rpcclient,
4519 RPC_DISPLAY_ADDR),
4520 clp->cl_id_uniquifier);
4521
4522 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4523
Andy Adamson180b62a2010-03-02 13:19:36 -05004524 if (status != -NFS4ERR_CLID_INUSE)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004525 break;
4526
4527 if (signalled())
4528 break;
4529
4530 if (++clp->cl_id_uniquifier == 0)
4531 break;
4532 }
4533
4534 dprintk("<-- %s status= %d\n", __func__, status);
4535 return status;
4536}
4537
Andy Adamson2050f0c2009-04-01 09:22:30 -04004538struct nfs4_get_lease_time_data {
4539 struct nfs4_get_lease_time_args *args;
4540 struct nfs4_get_lease_time_res *res;
4541 struct nfs_client *clp;
4542};
4543
4544static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4545 void *calldata)
4546{
4547 int ret;
4548 struct nfs4_get_lease_time_data *data =
4549 (struct nfs4_get_lease_time_data *)calldata;
4550
4551 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004552 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004553 /* just setup sequence, do not trigger session recovery
4554 since we're invoked within one */
4555 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004556 &data->args->la_seq_args,
4557 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004558
4559 BUG_ON(ret == -EAGAIN);
4560 rpc_call_start(task);
4561 dprintk("<-- %s\n", __func__);
4562}
4563
4564/*
4565 * Called from nfs4_state_manager thread for session setup, so don't recover
4566 * from sequence operation or clientid errors.
4567 */
4568static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4569{
4570 struct nfs4_get_lease_time_data *data =
4571 (struct nfs4_get_lease_time_data *)calldata;
4572
4573 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004574 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4575 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004576 switch (task->tk_status) {
4577 case -NFS4ERR_DELAY:
4578 case -NFS4ERR_GRACE:
4579 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4580 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4581 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004582 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004583 return;
4584 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004585 dprintk("<-- %s\n", __func__);
4586}
4587
4588struct rpc_call_ops nfs4_get_lease_time_ops = {
4589 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4590 .rpc_call_done = nfs4_get_lease_time_done,
4591};
4592
4593int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4594{
4595 struct rpc_task *task;
4596 struct nfs4_get_lease_time_args args;
4597 struct nfs4_get_lease_time_res res = {
4598 .lr_fsinfo = fsinfo,
4599 };
4600 struct nfs4_get_lease_time_data data = {
4601 .args = &args,
4602 .res = &res,
4603 .clp = clp,
4604 };
4605 struct rpc_message msg = {
4606 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4607 .rpc_argp = &args,
4608 .rpc_resp = &res,
4609 };
4610 struct rpc_task_setup task_setup = {
4611 .rpc_client = clp->cl_rpcclient,
4612 .rpc_message = &msg,
4613 .callback_ops = &nfs4_get_lease_time_ops,
4614 .callback_data = &data
4615 };
4616 int status;
4617
Andy Adamson2050f0c2009-04-01 09:22:30 -04004618 dprintk("--> %s\n", __func__);
4619 task = rpc_run_task(&task_setup);
4620
4621 if (IS_ERR(task))
4622 status = PTR_ERR(task);
4623 else {
4624 status = task->tk_status;
4625 rpc_put_task(task);
4626 }
4627 dprintk("<-- %s return %d\n", __func__, status);
4628
4629 return status;
4630}
4631
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004632/*
4633 * Reset a slot table
4634 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004635static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4636 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004637{
Andy Adamson104aeba2010-01-14 17:45:10 -05004638 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004639 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004640 int ret = 0;
4641
Andy Adamson104aeba2010-01-14 17:45:10 -05004642 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4643 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004644
Andy Adamson104aeba2010-01-14 17:45:10 -05004645 /* Does the newly negotiated max_reqs match the existing slot table? */
4646 if (max_reqs != tbl->max_slots) {
4647 ret = -ENOMEM;
4648 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004649 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004650 if (!new)
4651 goto out;
4652 ret = 0;
4653 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004654 }
4655 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004656 if (new) {
4657 tbl->slots = new;
4658 tbl->max_slots = max_reqs;
4659 }
4660 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004661 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004662 spin_unlock(&tbl->slot_tbl_lock);
4663 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4664 tbl, tbl->slots, tbl->max_slots);
4665out:
4666 dprintk("<-- %s: return %d\n", __func__, ret);
4667 return ret;
4668}
4669
Andy Adamsonfc931582009-04-01 09:22:31 -04004670/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004671 * Reset the forechannel and backchannel slot tables
4672 */
4673static int nfs4_reset_slot_tables(struct nfs4_session *session)
4674{
4675 int status;
4676
4677 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004678 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004679 if (status)
4680 return status;
4681
4682 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004683 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004684 return status;
4685}
4686
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004687/* Destroy the slot table */
4688static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4689{
4690 if (session->fc_slot_table.slots != NULL) {
4691 kfree(session->fc_slot_table.slots);
4692 session->fc_slot_table.slots = NULL;
4693 }
4694 if (session->bc_slot_table.slots != NULL) {
4695 kfree(session->bc_slot_table.slots);
4696 session->bc_slot_table.slots = NULL;
4697 }
4698 return;
4699}
4700
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004701/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004702 * Initialize slot table
4703 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004704static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4705 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004706{
Andy Adamsonfc931582009-04-01 09:22:31 -04004707 struct nfs4_slot *slot;
4708 int ret = -ENOMEM;
4709
4710 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4711
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004712 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004713
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004714 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004715 if (!slot)
4716 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004717 ret = 0;
4718
4719 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004720 tbl->max_slots = max_slots;
4721 tbl->slots = slot;
4722 tbl->highest_used_slotid = -1; /* no slot is currently used */
4723 spin_unlock(&tbl->slot_tbl_lock);
4724 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4725 tbl, tbl->slots, tbl->max_slots);
4726out:
4727 dprintk("<-- %s: return %d\n", __func__, ret);
4728 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004729}
4730
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004731/*
4732 * Initialize the forechannel and backchannel tables
4733 */
4734static int nfs4_init_slot_tables(struct nfs4_session *session)
4735{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004736 struct nfs4_slot_table *tbl;
4737 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004738
Trond Myklebustf26468f2009-12-05 19:32:11 -05004739 tbl = &session->fc_slot_table;
4740 if (tbl->slots == NULL) {
4741 status = nfs4_init_slot_table(tbl,
4742 session->fc_attrs.max_reqs, 1);
4743 if (status)
4744 return status;
4745 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004746
Trond Myklebustf26468f2009-12-05 19:32:11 -05004747 tbl = &session->bc_slot_table;
4748 if (tbl->slots == NULL) {
4749 status = nfs4_init_slot_table(tbl,
4750 session->bc_attrs.max_reqs, 0);
4751 if (status)
4752 nfs4_destroy_slot_tables(session);
4753 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004754
4755 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004756}
4757
4758struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4759{
4760 struct nfs4_session *session;
4761 struct nfs4_slot_table *tbl;
4762
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004763 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004764 if (!session)
4765 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004766
Andy Adamsonea028ac2009-12-04 15:55:38 -05004767 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004768
Andy Adamson557134a2009-04-01 09:21:53 -04004769 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004770 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004771 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004772 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004773
4774 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004775 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004776 spin_lock_init(&tbl->slot_tbl_lock);
4777 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4778
Trond Myklebust1055d762010-06-16 09:52:27 -04004779 session->session_state = 1<<NFS4_SESSION_INITING;
4780
Andy Adamson557134a2009-04-01 09:21:53 -04004781 session->clp = clp;
4782 return session;
4783}
4784
4785void nfs4_destroy_session(struct nfs4_session *session)
4786{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004787 nfs4_proc_destroy_session(session);
4788 dprintk("%s Destroy backchannel for xprt %p\n",
4789 __func__, session->clp->cl_rpcclient->cl_xprt);
4790 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4791 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004792 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004793 kfree(session);
4794}
4795
Andy Adamsonfc931582009-04-01 09:22:31 -04004796/*
4797 * Initialize the values to be used by the client in CREATE_SESSION
4798 * If nfs4_init_session set the fore channel request and response sizes,
4799 * use them.
4800 *
4801 * Set the back channel max_resp_sz_cached to zero to force the client to
4802 * always set csa_cachethis to FALSE because the current implementation
4803 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4804 */
4805static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4806{
4807 struct nfs4_session *session = args->client->cl_session;
4808 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4809 mxresp_sz = session->fc_attrs.max_resp_sz;
4810
4811 if (mxrqst_sz == 0)
4812 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4813 if (mxresp_sz == 0)
4814 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4815 /* Fore channel attributes */
4816 args->fc_attrs.headerpadsz = 0;
4817 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4818 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004819 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4820 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4821
4822 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004823 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004824 __func__,
4825 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004826 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004827
4828 /* Back channel attributes */
4829 args->bc_attrs.headerpadsz = 0;
4830 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4831 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4832 args->bc_attrs.max_resp_sz_cached = 0;
4833 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4834 args->bc_attrs.max_reqs = 1;
4835
4836 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4837 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4838 __func__,
4839 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4840 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4841 args->bc_attrs.max_reqs);
4842}
4843
Andy Adamson8d353012009-04-01 09:22:32 -04004844static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4845{
4846 if (rcvd <= sent)
4847 return 0;
4848 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4849 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4850 return -EINVAL;
4851}
4852
4853#define _verify_fore_channel_attr(_name_) \
4854 _verify_channel_attr("fore", #_name_, \
4855 args->fc_attrs._name_, \
4856 session->fc_attrs._name_)
4857
4858#define _verify_back_channel_attr(_name_) \
4859 _verify_channel_attr("back", #_name_, \
4860 args->bc_attrs._name_, \
4861 session->bc_attrs._name_)
4862
4863/*
4864 * The server is not allowed to increase the fore channel header pad size,
4865 * maximum response size, or maximum number of operations.
4866 *
4867 * The back channel attributes are only negotiatied down: We send what the
4868 * (back channel) server insists upon.
4869 */
4870static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4871 struct nfs4_session *session)
4872{
4873 int ret = 0;
4874
4875 ret |= _verify_fore_channel_attr(headerpadsz);
4876 ret |= _verify_fore_channel_attr(max_resp_sz);
4877 ret |= _verify_fore_channel_attr(max_ops);
4878
4879 ret |= _verify_back_channel_attr(headerpadsz);
4880 ret |= _verify_back_channel_attr(max_rqst_sz);
4881 ret |= _verify_back_channel_attr(max_resp_sz);
4882 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4883 ret |= _verify_back_channel_attr(max_ops);
4884 ret |= _verify_back_channel_attr(max_reqs);
4885
4886 return ret;
4887}
4888
Andy Adamsonfc931582009-04-01 09:22:31 -04004889static int _nfs4_proc_create_session(struct nfs_client *clp)
4890{
4891 struct nfs4_session *session = clp->cl_session;
4892 struct nfs41_create_session_args args = {
4893 .client = clp,
4894 .cb_program = NFS4_CALLBACK,
4895 };
4896 struct nfs41_create_session_res res = {
4897 .client = clp,
4898 };
4899 struct rpc_message msg = {
4900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4901 .rpc_argp = &args,
4902 .rpc_resp = &res,
4903 };
4904 int status;
4905
4906 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004907 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004908
4909 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4910
Andy Adamson8d353012009-04-01 09:22:32 -04004911 if (!status)
4912 /* Verify the session's negotiated channel_attrs values */
4913 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004914 if (!status) {
4915 /* Increment the clientid slot sequence id */
4916 clp->cl_seqid++;
4917 }
4918
4919 return status;
4920}
4921
4922/*
4923 * Issues a CREATE_SESSION operation to the server.
4924 * It is the responsibility of the caller to verify the session is
4925 * expired before calling this routine.
4926 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004927int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004928{
4929 int status;
4930 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04004931 struct nfs4_session *session = clp->cl_session;
4932
4933 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4934
4935 status = _nfs4_proc_create_session(clp);
4936 if (status)
4937 goto out;
4938
Trond Myklebustf26468f2009-12-05 19:32:11 -05004939 /* Init and reset the fore channel */
4940 status = nfs4_init_slot_tables(session);
4941 dprintk("slot table initialization returned %d\n", status);
4942 if (status)
4943 goto out;
4944 status = nfs4_reset_slot_tables(session);
4945 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04004946 if (status)
4947 goto out;
4948
4949 ptr = (unsigned *)&session->sess_id.data[0];
4950 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4951 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04004952out:
4953 dprintk("<-- %s\n", __func__);
4954 return status;
4955}
4956
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004957/*
4958 * Issue the over-the-wire RPC DESTROY_SESSION.
4959 * The caller must serialize access to this routine.
4960 */
4961int nfs4_proc_destroy_session(struct nfs4_session *session)
4962{
4963 int status = 0;
4964 struct rpc_message msg;
4965
4966 dprintk("--> nfs4_proc_destroy_session\n");
4967
4968 /* session is still being setup */
4969 if (session->clp->cl_cons_state != NFS_CS_READY)
4970 return status;
4971
4972 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4973 msg.rpc_argp = session;
4974 msg.rpc_resp = NULL;
4975 msg.rpc_cred = NULL;
4976 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4977
4978 if (status)
4979 printk(KERN_WARNING
4980 "Got error %d from the server on DESTROY_SESSION. "
4981 "Session has been destroyed regardless...\n", status);
4982
4983 dprintk("<-- nfs4_proc_destroy_session\n");
4984 return status;
4985}
4986
Trond Myklebustfccba802009-07-21 16:48:07 -04004987int nfs4_init_session(struct nfs_server *server)
4988{
4989 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05004990 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05004991 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04004992 int ret;
4993
4994 if (!nfs4_has_session(clp))
4995 return 0;
4996
Trond Myklebust1055d762010-06-16 09:52:27 -04004997 session = clp->cl_session;
4998 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
4999 return 0;
5000
Andy Adamson68bf05e2009-12-15 12:55:02 -05005001 rsize = server->rsize;
5002 if (rsize == 0)
5003 rsize = NFS_MAX_FILE_IO_SIZE;
5004 wsize = server->wsize;
5005 if (wsize == 0)
5006 wsize = NFS_MAX_FILE_IO_SIZE;
5007
Andy Adamson68bf05e2009-12-15 12:55:02 -05005008 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5009 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005010
Trond Myklebustfccba802009-07-21 16:48:07 -04005011 ret = nfs4_recover_expired_lease(server);
5012 if (!ret)
5013 ret = nfs4_check_client_ready(clp);
5014 return ret;
5015}
5016
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005017/*
5018 * Renew the cl_session lease.
5019 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005020struct nfs4_sequence_data {
5021 struct nfs_client *clp;
5022 struct nfs4_sequence_args args;
5023 struct nfs4_sequence_res res;
5024};
5025
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005026static void nfs41_sequence_release(void *data)
5027{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005028 struct nfs4_sequence_data *calldata = data;
5029 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005030
Alexandros Batsakis71358402010-02-05 03:45:05 -08005031 if (atomic_read(&clp->cl_count) > 1)
5032 nfs4_schedule_state_renewal(clp);
5033 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005034 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005035}
5036
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005037static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5038{
5039 switch(task->tk_status) {
5040 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005041 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5042 return -EAGAIN;
5043 default:
5044 nfs4_schedule_state_recovery(clp);
5045 }
5046 return 0;
5047}
5048
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005049static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005050{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005051 struct nfs4_sequence_data *calldata = data;
5052 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005053
Trond Myklebust14516c32010-07-31 14:29:06 -04005054 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5055 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005056
5057 if (task->tk_status < 0) {
5058 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005059 if (atomic_read(&clp->cl_count) == 1)
5060 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005061
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005062 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5063 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005064 return;
5065 }
5066 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005067 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005068out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005069 dprintk("<-- %s\n", __func__);
5070}
5071
5072static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5073{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005074 struct nfs4_sequence_data *calldata = data;
5075 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005076 struct nfs4_sequence_args *args;
5077 struct nfs4_sequence_res *res;
5078
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005079 args = task->tk_msg.rpc_argp;
5080 res = task->tk_msg.rpc_resp;
5081
Trond Myklebust035168a2010-06-16 09:52:26 -04005082 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005083 return;
5084 rpc_call_start(task);
5085}
5086
5087static const struct rpc_call_ops nfs41_sequence_ops = {
5088 .rpc_call_done = nfs41_sequence_call_done,
5089 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005090 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005091};
5092
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005093static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005094{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005095 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005096 struct rpc_message msg = {
5097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5098 .rpc_cred = cred,
5099 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005100 struct rpc_task_setup task_setup_data = {
5101 .rpc_client = clp->cl_rpcclient,
5102 .rpc_message = &msg,
5103 .callback_ops = &nfs41_sequence_ops,
5104 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5105 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005106
Alexandros Batsakis71358402010-02-05 03:45:05 -08005107 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005108 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005109 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005110 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005111 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005112 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005113 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005114 msg.rpc_argp = &calldata->args;
5115 msg.rpc_resp = &calldata->res;
5116 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005117 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005118
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005119 return rpc_run_task(&task_setup_data);
5120}
5121
5122static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5123{
5124 struct rpc_task *task;
5125 int ret = 0;
5126
5127 task = _nfs41_proc_sequence(clp, cred);
5128 if (IS_ERR(task))
5129 ret = PTR_ERR(task);
5130 else
5131 rpc_put_task(task);
5132 dprintk("<-- %s status=%d\n", __func__, ret);
5133 return ret;
5134}
5135
5136static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5137{
5138 struct rpc_task *task;
5139 int ret;
5140
5141 task = _nfs41_proc_sequence(clp, cred);
5142 if (IS_ERR(task)) {
5143 ret = PTR_ERR(task);
5144 goto out;
5145 }
5146 ret = rpc_wait_for_completion_task(task);
5147 if (!ret)
5148 ret = task->tk_status;
5149 rpc_put_task(task);
5150out:
5151 dprintk("<-- %s status=%d\n", __func__, ret);
5152 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005153}
5154
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005155struct nfs4_reclaim_complete_data {
5156 struct nfs_client *clp;
5157 struct nfs41_reclaim_complete_args arg;
5158 struct nfs41_reclaim_complete_res res;
5159};
5160
5161static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5162{
5163 struct nfs4_reclaim_complete_data *calldata = data;
5164
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005165 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005166 if (nfs41_setup_sequence(calldata->clp->cl_session,
5167 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005168 &calldata->res.seq_res, 0, task))
5169 return;
5170
5171 rpc_call_start(task);
5172}
5173
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005174static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5175{
5176 switch(task->tk_status) {
5177 case 0:
5178 case -NFS4ERR_COMPLETE_ALREADY:
5179 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5180 break;
5181 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005182 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5183 return -EAGAIN;
5184 default:
5185 nfs4_schedule_state_recovery(clp);
5186 }
5187 return 0;
5188}
5189
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005190static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5191{
5192 struct nfs4_reclaim_complete_data *calldata = data;
5193 struct nfs_client *clp = calldata->clp;
5194 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5195
5196 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005197 if (!nfs41_sequence_done(task, res))
5198 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005199
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005200 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5201 rpc_restart_call_prepare(task);
5202 return;
5203 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005204 dprintk("<-- %s\n", __func__);
5205}
5206
5207static void nfs4_free_reclaim_complete_data(void *data)
5208{
5209 struct nfs4_reclaim_complete_data *calldata = data;
5210
5211 kfree(calldata);
5212}
5213
5214static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5215 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5216 .rpc_call_done = nfs4_reclaim_complete_done,
5217 .rpc_release = nfs4_free_reclaim_complete_data,
5218};
5219
5220/*
5221 * Issue a global reclaim complete.
5222 */
5223static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5224{
5225 struct nfs4_reclaim_complete_data *calldata;
5226 struct rpc_task *task;
5227 struct rpc_message msg = {
5228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5229 };
5230 struct rpc_task_setup task_setup_data = {
5231 .rpc_client = clp->cl_rpcclient,
5232 .rpc_message = &msg,
5233 .callback_ops = &nfs4_reclaim_complete_call_ops,
5234 .flags = RPC_TASK_ASYNC,
5235 };
5236 int status = -ENOMEM;
5237
5238 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005239 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005240 if (calldata == NULL)
5241 goto out;
5242 calldata->clp = clp;
5243 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005244
5245 msg.rpc_argp = &calldata->arg;
5246 msg.rpc_resp = &calldata->res;
5247 task_setup_data.callback_data = calldata;
5248 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005249 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005250 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005251 goto out;
5252 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005253 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005254 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005255out:
5256 dprintk("<-- %s status=%d\n", __func__, status);
5257 return status;
5258}
Andy Adamson557134a2009-04-01 09:21:53 -04005259#endif /* CONFIG_NFS_V4_1 */
5260
Andy Adamson591d71c2009-04-01 09:22:47 -04005261struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005262 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005263 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 .recover_open = nfs4_open_reclaim,
5265 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005266 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005267 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268};
5269
Andy Adamson591d71c2009-04-01 09:22:47 -04005270#if defined(CONFIG_NFS_V4_1)
5271struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5272 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5273 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5274 .recover_open = nfs4_open_reclaim,
5275 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005276 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005277 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005278 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005279};
5280#endif /* CONFIG_NFS_V4_1 */
5281
5282struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005283 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005284 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 .recover_open = nfs4_open_expired,
5286 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005287 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005288 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289};
5290
Andy Adamson591d71c2009-04-01 09:22:47 -04005291#if defined(CONFIG_NFS_V4_1)
5292struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5293 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5294 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5295 .recover_open = nfs4_open_expired,
5296 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005297 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005298 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005299};
5300#endif /* CONFIG_NFS_V4_1 */
5301
Benny Halevy29fba382009-04-01 09:22:44 -04005302struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5303 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005304 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005305 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005306};
5307
5308#if defined(CONFIG_NFS_V4_1)
5309struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5310 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005311 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005312 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005313};
5314#endif
5315
Trond Myklebust97dc1352010-06-16 09:52:26 -04005316static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5317 .minor_version = 0,
5318 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005319 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005320 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5321 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5322 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005323};
5324
5325#if defined(CONFIG_NFS_V4_1)
5326static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5327 .minor_version = 1,
5328 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005329 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005330 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5331 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5332 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005333};
5334#endif
5335
Trond Myklebust97dc1352010-06-16 09:52:26 -04005336const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5337 [0] = &nfs_v4_0_minor_ops,
5338#if defined(CONFIG_NFS_V4_1)
5339 [1] = &nfs_v4_1_minor_ops,
5340#endif
5341};
5342
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005343static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005344 .permission = nfs_permission,
5345 .getattr = nfs_getattr,
5346 .setattr = nfs_setattr,
5347 .getxattr = nfs4_getxattr,
5348 .setxattr = nfs4_setxattr,
5349 .listxattr = nfs4_listxattr,
5350};
5351
David Howells509de812006-08-22 20:06:11 -04005352const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 .version = 4, /* protocol version */
5354 .dentry_ops = &nfs4_dentry_operations,
5355 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005356 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 .getroot = nfs4_proc_get_root,
5358 .getattr = nfs4_proc_getattr,
5359 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005360 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 .lookup = nfs4_proc_lookup,
5362 .access = nfs4_proc_access,
5363 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 .create = nfs4_proc_create,
5365 .remove = nfs4_proc_remove,
5366 .unlink_setup = nfs4_proc_unlink_setup,
5367 .unlink_done = nfs4_proc_unlink_done,
5368 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04005369 .rename_setup = nfs4_proc_rename_setup,
5370 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 .link = nfs4_proc_link,
5372 .symlink = nfs4_proc_symlink,
5373 .mkdir = nfs4_proc_mkdir,
5374 .rmdir = nfs4_proc_remove,
5375 .readdir = nfs4_proc_readdir,
5376 .mknod = nfs4_proc_mknod,
5377 .statfs = nfs4_proc_statfs,
5378 .fsinfo = nfs4_proc_fsinfo,
5379 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005380 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 .decode_dirent = nfs4_decode_dirent,
5382 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005383 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005385 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005387 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005389 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005390 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04005391 .open_context = nfs4_atomic_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392};
5393
5394/*
5395 * Local variables:
5396 * c-basic-offset: 8
5397 * End:
5398 */