blob: b0b1a5568529d0471c0fd567642a4549ccb9c76f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/sunrpc/clnt.h>
44#include <linux/nfs.h>
45#include <linux/nfs4.h>
46#include <linux/nfs_fs.h>
47#include <linux/nfs_page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070049#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040050#include <linux/module.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040051#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000052#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050053#include <linux/utsname.h>
Neil Hormane9e3d722011-03-04 19:26:03 -050054#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Trond Myklebust4ce79712005-06-22 17:16:21 +000056#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050058#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050059#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040060#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040061#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#define NFSDBG_FACILITY NFSDBG_PROC
64
Trond Myklebust2066fe82006-09-15 08:30:46 -040065#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#define NFS4_POLL_RETRY_MAX (15*HZ)
67
Trond Myklebusta78cb572009-08-09 15:06:19 -040068#define NFS4_MAX_LOOP_ON_RECOVER (10)
69
Trond Myklebustcdd4e682006-01-03 09:55:12 +010070struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010071static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080072static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050074static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040075static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
76static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040077static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
78 struct nfs_fattr *fattr, struct iattr *sattr,
79 struct nfs4_state *state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050082static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Trond Myklebust52567b02009-10-23 14:46:42 -040084 if (err >= -1000)
85 return err;
86 switch (err) {
87 case -NFS4ERR_RESOURCE:
88 return -EREMOTEIO;
89 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070091 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040092 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
Trond Myklebust52567b02009-10-23 14:46:42 -040094 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97/*
98 * This is our standard bitmap for GETATTR requests.
99 */
100const u32 nfs4_fattr_bitmap[2] = {
101 FATTR4_WORD0_TYPE
102 | FATTR4_WORD0_CHANGE
103 | FATTR4_WORD0_SIZE
104 | FATTR4_WORD0_FSID
105 | FATTR4_WORD0_FILEID,
106 FATTR4_WORD1_MODE
107 | FATTR4_WORD1_NUMLINKS
108 | FATTR4_WORD1_OWNER
109 | FATTR4_WORD1_OWNER_GROUP
110 | FATTR4_WORD1_RAWDEV
111 | FATTR4_WORD1_SPACE_USED
112 | FATTR4_WORD1_TIME_ACCESS
113 | FATTR4_WORD1_TIME_METADATA
114 | FATTR4_WORD1_TIME_MODIFY
115};
116
117const u32 nfs4_statfs_bitmap[2] = {
118 FATTR4_WORD0_FILES_AVAIL
119 | FATTR4_WORD0_FILES_FREE
120 | FATTR4_WORD0_FILES_TOTAL,
121 FATTR4_WORD1_SPACE_AVAIL
122 | FATTR4_WORD1_SPACE_FREE
123 | FATTR4_WORD1_SPACE_TOTAL
124};
125
Trond Myklebust4ce79712005-06-22 17:16:21 +0000126const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 FATTR4_WORD0_MAXLINK
128 | FATTR4_WORD0_MAXNAME,
129 0
130};
131
132const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
133 | FATTR4_WORD0_MAXREAD
134 | FATTR4_WORD0_MAXWRITE
135 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700136 FATTR4_WORD1_TIME_DELTA
Andy Adamson504913f2010-10-20 00:17:57 -0400137 | FATTR4_WORD1_FS_LAYOUT_TYPES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
Manoj Naik830b8e32006-06-09 09:34:25 -0400140const u32 nfs4_fs_locations_bitmap[2] = {
141 FATTR4_WORD0_TYPE
142 | FATTR4_WORD0_CHANGE
143 | FATTR4_WORD0_SIZE
144 | FATTR4_WORD0_FSID
145 | FATTR4_WORD0_FILEID
146 | FATTR4_WORD0_FS_LOCATIONS,
147 FATTR4_WORD1_MODE
148 | FATTR4_WORD1_NUMLINKS
149 | FATTR4_WORD1_OWNER
150 | FATTR4_WORD1_OWNER_GROUP
151 | FATTR4_WORD1_RAWDEV
152 | FATTR4_WORD1_SPACE_USED
153 | FATTR4_WORD1_TIME_ACCESS
154 | FATTR4_WORD1_TIME_METADATA
155 | FATTR4_WORD1_TIME_MODIFY
156 | FATTR4_WORD1_MOUNTED_ON_FILEID
157};
158
Al Virobc4785c2006-10-19 23:28:51 -0700159static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 struct nfs4_readdir_arg *readdir)
161{
Al Viro0dbb4c62006-10-19 23:28:49 -0700162 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 BUG_ON(readdir->count < 80);
165 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000166 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
168 return;
169 }
170
171 readdir->cookie = 0;
172 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
173 if (cookie == 2)
174 return;
175
176 /*
177 * NFSv4 servers do not return entries for '.' and '..'
178 * Therefore, we fake these entries here. We let '.'
179 * have cookie 0 and '..' have cookie 1. Note that
180 * when talking to the server, we always send cookie 0
181 * instead of 1 or 2.
182 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700183 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 if (cookie == 0) {
186 *p++ = xdr_one; /* next */
187 *p++ = xdr_zero; /* cookie, first word */
188 *p++ = xdr_one; /* cookie, second word */
189 *p++ = xdr_one; /* entry len */
190 memcpy(p, ".\0\0\0", 4); /* entry */
191 p++;
192 *p++ = xdr_one; /* bitmap length */
193 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
194 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400195 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197
198 *p++ = xdr_one; /* next */
199 *p++ = xdr_zero; /* cookie, first word */
200 *p++ = xdr_two; /* cookie, second word */
201 *p++ = xdr_two; /* entry len */
202 memcpy(p, "..\0\0", 4); /* entry */
203 p++;
204 *p++ = xdr_one; /* bitmap length */
205 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
206 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400207 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 readdir->pgbase = (char *)p - (char *)start;
210 readdir->count -= readdir->pgbase;
211 kunmap_atomic(start, KM_USER0);
212}
213
Trond Myklebust65de8722008-12-23 15:21:44 -0500214static int nfs4_wait_clnt_recover(struct nfs_client *clp)
215{
216 int res;
217
218 might_sleep();
219
Trond Myklebuste005e802008-12-23 15:21:48 -0500220 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400221 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500222 return res;
223}
224
225static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
226{
227 int res = 0;
228
229 might_sleep();
230
231 if (*timeout <= 0)
232 *timeout = NFS4_POLL_RETRY_MIN;
233 if (*timeout > NFS4_POLL_RETRY_MAX)
234 *timeout = NFS4_POLL_RETRY_MAX;
235 schedule_timeout_killable(*timeout);
236 if (fatal_signal_pending(current))
237 res = -ERESTARTSYS;
238 *timeout <<= 1;
239 return res;
240}
241
242/* This is the error handling routine for processes that are allowed
243 * to sleep.
244 */
245static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
246{
247 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500248 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500249 int ret = errorcode;
250
251 exception->retry = 0;
252 switch(errorcode) {
253 case 0:
254 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500255 case -NFS4ERR_ADMIN_REVOKED:
256 case -NFS4ERR_BAD_STATEID:
257 case -NFS4ERR_OPENMODE:
258 if (state == NULL)
259 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500260 nfs4_schedule_stateid_recovery(server, state);
261 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500262 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500263 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500264 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500265 nfs4_schedule_lease_recovery(clp);
266 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500267#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400268 case -NFS4ERR_BADSESSION:
269 case -NFS4ERR_BADSLOT:
270 case -NFS4ERR_BAD_HIGH_SLOT:
271 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
272 case -NFS4ERR_DEADSESSION:
273 case -NFS4ERR_SEQ_FALSE_RETRY:
274 case -NFS4ERR_SEQ_MISORDERED:
275 dprintk("%s ERROR: %d Reset session\n", __func__,
276 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500277 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400278 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500279 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500280#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500281 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500282 if (exception->timeout > HZ) {
283 /* We have retried a decent amount, time to
284 * fail
285 */
286 ret = -EBUSY;
287 break;
288 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500289 case -NFS4ERR_GRACE:
290 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500291 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500292 ret = nfs4_delay(server->client, &exception->timeout);
293 if (ret != 0)
294 break;
295 case -NFS4ERR_OLD_STATEID:
296 exception->retry = 1;
297 }
298 /* We failed to handle the error */
299 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500300wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500301 ret = nfs4_wait_clnt_recover(clp);
302 if (ret == 0)
303 exception->retry = 1;
304 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500305}
306
307
Trond Myklebust452e9352010-07-31 14:29:06 -0400308static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 spin_lock(&clp->cl_lock);
311 if (time_before(clp->cl_last_renewal,timestamp))
312 clp->cl_last_renewal = timestamp;
313 spin_unlock(&clp->cl_lock);
314}
315
Trond Myklebust452e9352010-07-31 14:29:06 -0400316static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
317{
318 do_renew_lease(server->nfs_client, timestamp);
319}
320
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400321#if defined(CONFIG_NFS_V4_1)
322
Benny Halevy510b8172009-04-01 09:22:14 -0400323/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400324 * nfs4_free_slot - free a slot and efficiently update slot table.
325 *
326 * freeing a slot is trivially done by clearing its respective bit
327 * in the bitmap.
328 * If the freed slotid equals highest_used_slotid we want to update it
329 * so that the server would be able to size down the slot table if needed,
330 * otherwise we know that the highest_used_slotid is still in use.
331 * When updating highest_used_slotid there may be "holes" in the bitmap
332 * so we need to scan down from highest_used_slotid to 0 looking for the now
333 * highest slotid in use.
334 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500335 *
336 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400337 */
338static void
Benny Halevydfb4f3092010-09-24 09:17:01 -0400339nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400340{
Benny Halevydfb4f3092010-09-24 09:17:01 -0400341 int free_slotid = free_slot - tbl->slots;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400342 int slotid = free_slotid;
343
Benny Halevydfb4f3092010-09-24 09:17:01 -0400344 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400345 /* clear used bit in bitmap */
346 __clear_bit(slotid, tbl->used_slots);
347
348 /* update highest_used_slotid when it is freed */
349 if (slotid == tbl->highest_used_slotid) {
350 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500351 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400352 tbl->highest_used_slotid = slotid;
353 else
354 tbl->highest_used_slotid = -1;
355 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400356 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
357 free_slotid, tbl->highest_used_slotid);
358}
359
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800360/*
Andy Adamson42acd022011-01-06 02:04:34 +0000361 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800362 */
Andy Adamson42acd022011-01-06 02:04:34 +0000363static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800364{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800365 struct rpc_task *task;
366
Trond Myklebusta2118c32010-06-16 09:52:26 -0400367 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800368 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
369 if (task)
370 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800371 return;
372 }
373
374 if (ses->fc_slot_table.highest_used_slotid != -1)
375 return;
376
Andy Adamson42acd022011-01-06 02:04:34 +0000377 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
378 complete(&ses->fc_slot_table.complete);
379}
380
381/*
382 * Signal state manager thread if session back channel is drained
383 */
384void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
385{
386 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
387 ses->bc_slot_table.highest_used_slotid != -1)
388 return;
389 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
390 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800391}
392
Trond Myklebustd185a332010-06-16 09:52:25 -0400393static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400394{
395 struct nfs4_slot_table *tbl;
396
Trond Myklebustd185a332010-06-16 09:52:25 -0400397 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400398 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400399 /* just wake up the next guy waiting since
400 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500401 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500402 return;
Andy Adamson13615872009-04-01 09:22:17 -0400403 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500404
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500405 spin_lock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400406 nfs4_free_slot(tbl, res->sr_slot);
Andy Adamson42acd022011-01-06 02:04:34 +0000407 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500408 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400409 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400410}
411
Trond Myklebust14516c32010-07-31 14:29:06 -0400412static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400413{
414 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400415 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400416
417 /*
418 * sr_status remains 1 if an RPC level error occurred. The server
419 * may or may not have processed the sequence operation..
420 * Proceed as if the server received and processed the sequence
421 * operation.
422 */
423 if (res->sr_status == 1)
424 res->sr_status = NFS_OK;
425
426 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400427 if (!res->sr_slot)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400428 goto out;
429
Andy Adamson691daf32009-12-04 15:55:39 -0500430 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400431 switch (res->sr_status) {
432 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400433 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400434 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400435 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400436 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400437 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500438 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500439 if (res->sr_status_flags != 0)
440 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400441 break;
442 case -NFS4ERR_DELAY:
443 /* The server detected a resend of the RPC call and
444 * returned NFS4ERR_DELAY as per Section 2.10.6.2
445 * of RFC5661.
446 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200447 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400448 __func__,
449 res->sr_slot - res->sr_session->fc_slot_table.slots,
450 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400451 goto out_retry;
452 default:
453 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400454 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400455 }
456out:
457 /* The session may be reset by one of the error handlers. */
458 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400459 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400460 return 1;
461out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400462 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400463 goto out;
464 rpc_delay(task, NFS4_POLL_RETRY_MAX);
465 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400466}
467
Trond Myklebust14516c32010-07-31 14:29:06 -0400468static int nfs4_sequence_done(struct rpc_task *task,
469 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400470{
Trond Myklebust14516c32010-07-31 14:29:06 -0400471 if (res->sr_session == NULL)
472 return 1;
473 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400474}
475
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400476/*
Benny Halevy510b8172009-04-01 09:22:14 -0400477 * nfs4_find_slot - efficiently look for a free slot
478 *
479 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
480 * If found, we mark the slot as used, update the highest_used_slotid,
481 * and respectively set up the sequence operation args.
482 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400483 *
484 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400485 */
486static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800487nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400488{
489 int slotid;
490 u8 ret_id = NFS4_MAX_SLOT_TABLE;
491 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
492
Benny Halevy510b8172009-04-01 09:22:14 -0400493 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
494 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
495 tbl->max_slots);
496 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
497 if (slotid >= tbl->max_slots)
498 goto out;
499 __set_bit(slotid, tbl->used_slots);
500 if (slotid > tbl->highest_used_slotid)
501 tbl->highest_used_slotid = slotid;
502 ret_id = slotid;
503out:
504 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
505 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400506 return ret_id;
507}
508
Andy Adamsonce5039c2009-04-01 09:22:13 -0400509static int nfs41_setup_sequence(struct nfs4_session *session,
510 struct nfs4_sequence_args *args,
511 struct nfs4_sequence_res *res,
512 int cache_reply,
513 struct rpc_task *task)
514{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400515 struct nfs4_slot *slot;
516 struct nfs4_slot_table *tbl;
517 u8 slotid;
518
519 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400520 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400521 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400522 return 0;
523
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400524 tbl = &session->fc_slot_table;
525
526 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400527 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800528 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500529 /*
530 * The state manager will wait until the slot table is empty.
531 * Schedule the reset thread
532 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500533 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400534 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500535 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500536 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400537 }
538
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800539 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
540 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
541 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
542 spin_unlock(&tbl->slot_tbl_lock);
543 dprintk("%s enforce FIFO order\n", __func__);
544 return -EAGAIN;
545 }
546
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800547 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400548 if (slotid == NFS4_MAX_SLOT_TABLE) {
549 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
550 spin_unlock(&tbl->slot_tbl_lock);
551 dprintk("<-- %s: no free slots\n", __func__);
552 return -EAGAIN;
553 }
554 spin_unlock(&tbl->slot_tbl_lock);
555
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800556 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400557 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400558 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400559 args->sa_slotid = slotid;
560 args->sa_cache_this = cache_reply;
561
562 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
563
Benny Halevy99fe60d2009-04-01 09:22:29 -0400564 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400565 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400566 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400567 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400568 /*
569 * sr_status is only set in decode_sequence, and so will remain
570 * set to 1 if an rpc level failure occurs.
571 */
572 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400573 return 0;
574}
575
Trond Myklebust035168a2010-06-16 09:52:26 -0400576int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400577 struct nfs4_sequence_args *args,
578 struct nfs4_sequence_res *res,
579 int cache_reply,
580 struct rpc_task *task)
581{
Trond Myklebust035168a2010-06-16 09:52:26 -0400582 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400583 int ret = 0;
584
Trond Myklebust035168a2010-06-16 09:52:26 -0400585 if (session == NULL) {
586 args->sa_session = NULL;
587 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400588 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400589 }
590
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200591 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400592 __func__, session->clp, session, res->sr_slot ?
593 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400594
595 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400596 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400597out:
598 dprintk("<-- %s status=%d\n", __func__, ret);
599 return ret;
600}
601
602struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400603 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604 struct nfs4_sequence_args *seq_args;
605 struct nfs4_sequence_res *seq_res;
606 int cache_reply;
607};
608
609static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
610{
611 struct nfs41_call_sync_data *data = calldata;
612
Trond Myklebust035168a2010-06-16 09:52:26 -0400613 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
614
615 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400616 data->seq_res, data->cache_reply, task))
617 return;
618 rpc_call_start(task);
619}
620
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800621static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
622{
623 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
624 nfs41_call_sync_prepare(task, calldata);
625}
626
Andy Adamson69ab40c2009-04-01 09:22:19 -0400627static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
628{
629 struct nfs41_call_sync_data *data = calldata;
630
Trond Myklebust14516c32010-07-31 14:29:06 -0400631 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400632}
633
Andy Adamsonce5039c2009-04-01 09:22:13 -0400634struct rpc_call_ops nfs41_call_sync_ops = {
635 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400636 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400637};
638
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800639struct rpc_call_ops nfs41_call_priv_sync_ops = {
640 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
641 .rpc_call_done = nfs41_call_sync_done,
642};
643
Trond Myklebust035168a2010-06-16 09:52:26 -0400644static int nfs4_call_sync_sequence(struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400645 struct rpc_message *msg,
646 struct nfs4_sequence_args *args,
647 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800648 int cache_reply,
649 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400650{
651 int ret;
652 struct rpc_task *task;
653 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400654 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400655 .seq_args = args,
656 .seq_res = res,
657 .cache_reply = cache_reply,
658 };
659 struct rpc_task_setup task_setup = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400660 .rpc_client = server->client,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400661 .rpc_message = msg,
662 .callback_ops = &nfs41_call_sync_ops,
663 .callback_data = &data
664 };
665
Benny Halevydfb4f3092010-09-24 09:17:01 -0400666 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800667 if (privileged)
668 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400669 task = rpc_run_task(&task_setup);
670 if (IS_ERR(task))
671 ret = PTR_ERR(task);
672 else {
673 ret = task->tk_status;
674 rpc_put_task(task);
675 }
676 return ret;
677}
678
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400679int _nfs4_call_sync_session(struct nfs_server *server,
680 struct rpc_message *msg,
681 struct nfs4_sequence_args *args,
682 struct nfs4_sequence_res *res,
683 int cache_reply)
684{
Trond Myklebust035168a2010-06-16 09:52:26 -0400685 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400686}
687
Trond Myklebustdf896452010-06-16 09:52:26 -0400688#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400689static int nfs4_sequence_done(struct rpc_task *task,
690 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400691{
Trond Myklebust14516c32010-07-31 14:29:06 -0400692 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400693}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400694#endif /* CONFIG_NFS_V4_1 */
695
696int _nfs4_call_sync(struct nfs_server *server,
697 struct rpc_message *msg,
698 struct nfs4_sequence_args *args,
699 struct nfs4_sequence_res *res,
700 int cache_reply)
701{
Benny Halevyf3752972009-04-01 09:22:04 -0400702 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400703 return rpc_call_sync(server->client, msg, 0);
704}
705
706#define nfs4_call_sync(server, msg, args, res, cache_reply) \
Trond Myklebust97dc1352010-06-16 09:52:26 -0400707 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400708 &(res)->seq_res, (cache_reply))
709
Trond Myklebust38478b22006-05-25 01:40:57 -0400710static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Trond Myklebust38478b22006-05-25 01:40:57 -0400712 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Trond Myklebust38478b22006-05-25 01:40:57 -0400714 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400715 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
716 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400717 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400718 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400719 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100722struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400723 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100724 struct nfs_openargs o_arg;
725 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100726 struct nfs_open_confirmargs c_arg;
727 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100728 struct nfs_fattr f_attr;
729 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400730 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100731 struct dentry *dir;
732 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400733 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100734 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100735 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400736 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100737 int rpc_status;
738 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100739};
740
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400741
742static void nfs4_init_opendata_res(struct nfs4_opendata *p)
743{
744 p->o_res.f_attr = &p->f_attr;
745 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400746 p->o_res.seqid = p->o_arg.seqid;
747 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400748 p->o_res.server = p->o_arg.server;
749 nfs_fattr_init(&p->f_attr);
750 nfs_fattr_init(&p->dir_attr);
751}
752
Trond Myklebustad389da2007-06-05 12:30:00 -0400753static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500754 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400755 const struct iattr *attrs,
756 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100757{
Trond Myklebustad389da2007-06-05 12:30:00 -0400758 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100759 struct inode *dir = parent->d_inode;
760 struct nfs_server *server = NFS_SERVER(dir);
761 struct nfs4_opendata *p;
762
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400763 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100764 if (p == NULL)
765 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400766 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767 if (p->o_arg.seqid == NULL)
768 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500769 path_get(path);
770 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100771 p->dir = parent;
772 p->owner = sp;
773 atomic_inc(&sp->so_count);
774 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500775 p->o_arg.open_flags = flags;
776 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400777 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400778 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400779 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100780 p->o_arg.server = server;
781 p->o_arg.bitmask = server->attr_bitmask;
782 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400783 if (flags & O_CREAT) {
784 u32 *s;
785
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100786 p->o_arg.u.attrs = &p->attrs;
787 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400788 s = (u32 *) p->o_arg.u.verifier.data;
789 s[0] = jiffies;
790 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100791 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100792 p->c_arg.fh = &p->o_res.fh;
793 p->c_arg.stateid = &p->o_res.stateid;
794 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400795 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400796 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100797 return p;
798err_free:
799 kfree(p);
800err:
801 dput(parent);
802 return NULL;
803}
804
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400805static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100806{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400807 struct nfs4_opendata *p = container_of(kref,
808 struct nfs4_opendata, kref);
809
810 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400811 if (p->state != NULL)
812 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400813 nfs4_put_state_owner(p->owner);
814 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700815 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400816 kfree(p);
817}
818
819static void nfs4_opendata_put(struct nfs4_opendata *p)
820{
821 if (p != NULL)
822 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823}
824
Trond Myklebust06f814a2006-01-03 09:55:07 +0100825static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
826{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100827 int ret;
828
Trond Myklebust06f814a2006-01-03 09:55:07 +0100829 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100830 return ret;
831}
832
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500833static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400834{
835 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500836
837 if (open_mode & O_EXCL)
838 goto out;
839 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400840 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500841 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
842 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400843 break;
844 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500845 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
846 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400847 break;
848 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500849 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
850 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400851 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500852out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400853 return ret;
854}
855
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500856static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400857{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500858 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400859 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500860 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400861 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500862 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400863 return 1;
864}
865
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500866static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100867{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500868 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100869 case FMODE_WRITE:
870 state->n_wronly++;
871 break;
872 case FMODE_READ:
873 state->n_rdonly++;
874 break;
875 case FMODE_READ|FMODE_WRITE:
876 state->n_rdwr++;
877 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500878 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100879}
880
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500881static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400882{
883 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
884 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
885 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500886 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400887 case FMODE_READ:
888 set_bit(NFS_O_RDONLY_STATE, &state->flags);
889 break;
890 case FMODE_WRITE:
891 set_bit(NFS_O_WRONLY_STATE, &state->flags);
892 break;
893 case FMODE_READ|FMODE_WRITE:
894 set_bit(NFS_O_RDWR_STATE, &state->flags);
895 }
896}
897
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500898static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400899{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400900 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500901 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400902 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400903}
904
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500905static 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 -0700906{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400907 /*
908 * Protect the call to nfs4_state_set_mode_locked and
909 * serialise the stateid update
910 */
911 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400912 if (deleg_stateid != NULL) {
913 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
914 set_bit(NFS_DELEGATED_STATE, &state->flags);
915 }
916 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500917 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400918 write_sequnlock(&state->seqlock);
919 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700921 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924static 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 -0500925{
926 struct nfs_inode *nfsi = NFS_I(state->inode);
927 struct nfs_delegation *deleg_cur;
928 int ret = 0;
929
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500930 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500931
932 rcu_read_lock();
933 deleg_cur = rcu_dereference(nfsi->delegation);
934 if (deleg_cur == NULL)
935 goto no_delegation;
936
937 spin_lock(&deleg_cur->lock);
938 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500939 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500940 goto no_delegation_unlock;
941
942 if (delegation == NULL)
943 delegation = &deleg_cur->stateid;
944 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
945 goto no_delegation_unlock;
946
Trond Myklebustb7391f42008-12-23 15:21:52 -0500947 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500948 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500949 ret = 1;
950no_delegation_unlock:
951 spin_unlock(&deleg_cur->lock);
952no_delegation:
953 rcu_read_unlock();
954
955 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500956 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500957 ret = 1;
958 }
959
960 return ret;
961}
962
963
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500964static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400965{
966 struct nfs_delegation *delegation;
967
968 rcu_read_lock();
969 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500970 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400971 rcu_read_unlock();
972 return;
973 }
974 rcu_read_unlock();
975 nfs_inode_return_delegation(inode);
976}
977
Trond Myklebust6ee41262007-07-08 14:11:36 -0400978static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400979{
980 struct nfs4_state *state = opendata->state;
981 struct nfs_inode *nfsi = NFS_I(state->inode);
982 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500983 int open_mode = opendata->o_arg.open_flags & O_EXCL;
984 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400985 nfs4_stateid stateid;
986 int ret = -EAGAIN;
987
Trond Myklebustaac00a82007-07-05 19:02:21 -0400988 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400990 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500991 if (can_open_cached(state, fmode, open_mode)) {
992 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400993 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400994 goto out_return_state;
995 }
996 spin_unlock(&state->owner->so_lock);
997 }
Trond Myklebust34310432008-12-23 15:21:38 -0500998 rcu_read_lock();
999 delegation = rcu_dereference(nfsi->delegation);
1000 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001001 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001002 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001003 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001004 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001005 /* Save the delegation */
1006 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1007 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001008 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001009 if (ret != 0)
1010 goto out;
1011 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001012
1013 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001014 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001015 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001016 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001017out:
1018 return ERR_PTR(ret);
1019out_return_state:
1020 atomic_inc(&state->count);
1021 return state;
1022}
1023
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001024static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1025{
1026 struct inode *inode;
1027 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001028 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001029 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001030
Trond Myklebustaac00a82007-07-05 19:02:21 -04001031 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001032 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001033 goto out;
1034 }
1035
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001036 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001037 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001038 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001039 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001040 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001041 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001042 goto err;
1043 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001044 state = nfs4_get_open_state(inode, data->owner);
1045 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001046 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001047 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001048 int delegation_flags = 0;
1049
Trond Myklebust003707c2007-07-05 18:07:55 -04001050 rcu_read_lock();
1051 delegation = rcu_dereference(NFS_I(inode)->delegation);
1052 if (delegation)
1053 delegation_flags = delegation->flags;
1054 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001055 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001056 nfs_inode_set_delegation(state->inode,
1057 data->owner->so_cred,
1058 &data->o_res);
1059 else
1060 nfs_inode_reclaim_delegation(state->inode,
1061 data->owner->so_cred,
1062 &data->o_res);
1063 }
Trond Myklebust34310432008-12-23 15:21:38 -05001064
1065 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001066 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001067 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001068out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001069 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001070err_put_inode:
1071 iput(inode);
1072err:
1073 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001074}
1075
Trond Myklebust864472e2006-01-03 09:55:15 +01001076static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1077{
1078 struct nfs_inode *nfsi = NFS_I(state->inode);
1079 struct nfs_open_context *ctx;
1080
1081 spin_lock(&state->inode->i_lock);
1082 list_for_each_entry(ctx, &nfsi->open_files, list) {
1083 if (ctx->state != state)
1084 continue;
1085 get_nfs_open_context(ctx);
1086 spin_unlock(&state->inode->i_lock);
1087 return ctx;
1088 }
1089 spin_unlock(&state->inode->i_lock);
1090 return ERR_PTR(-ENOENT);
1091}
1092
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001093static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1094{
1095 struct nfs4_opendata *opendata;
1096
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001097 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001098 if (opendata == NULL)
1099 return ERR_PTR(-ENOMEM);
1100 opendata->state = state;
1101 atomic_inc(&state->count);
1102 return opendata;
1103}
1104
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001105static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001106{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001107 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001108 int ret;
1109
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001110 opendata->o_arg.open_flags = 0;
1111 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001112 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1113 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1114 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001115 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001116 if (ret != 0)
1117 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001118 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001119 if (IS_ERR(newstate))
1120 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001121 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001122 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001123 return 0;
1124}
1125
1126static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1127{
Trond Myklebust864472e2006-01-03 09:55:15 +01001128 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001129 int ret;
1130
1131 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001132 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001133 smp_rmb();
1134 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001135 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001136 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &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 }
1142 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001143 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001144 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001145 if (ret != 0)
1146 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001147 if (newstate != state)
1148 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001149 }
1150 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001151 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001152 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001153 if (ret != 0)
1154 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001155 if (newstate != state)
1156 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001157 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001158 /*
1159 * We may have performed cached opens for all three recoveries.
1160 * Check if we need to update the current stateid.
1161 */
1162 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1163 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001164 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001165 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1166 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001167 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001168 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001169 return 0;
1170}
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172/*
1173 * OPEN_RECLAIM:
1174 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001176static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001178 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001179 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001180 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 int status;
1182
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001183 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1184 if (IS_ERR(opendata))
1185 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001186 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1187 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001188 rcu_read_lock();
1189 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001190 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001191 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001192 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001193 opendata->o_arg.u.delegation_type = delegation_type;
1194 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001195 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return status;
1197}
1198
Trond Myklebust539cd032007-06-05 11:46:42 -04001199static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 struct nfs_server *server = NFS_SERVER(state->inode);
1202 struct nfs4_exception exception = { };
1203 int err;
1204 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001205 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001206 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001207 break;
1208 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 } while (exception.retry);
1210 return err;
1211}
1212
Trond Myklebust864472e2006-01-03 09:55:15 +01001213static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1214{
1215 struct nfs_open_context *ctx;
1216 int ret;
1217
1218 ctx = nfs4_state_find_open_context(state);
1219 if (IS_ERR(ctx))
1220 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001221 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001222 put_nfs_open_context(ctx);
1223 return ret;
1224}
1225
Trond Myklebust13437e12007-07-06 15:10:43 -04001226static 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 -07001227{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001228 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001229 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001231 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1232 if (IS_ERR(opendata))
1233 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001234 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001235 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001236 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001237 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001238 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001239 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
Trond Myklebust13437e12007-07-06 15:10:43 -04001242int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001245 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 int err;
1247 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001248 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 switch (err) {
1250 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001251 case -ENOENT:
1252 case -ESTALE:
1253 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001254 case -NFS4ERR_BADSESSION:
1255 case -NFS4ERR_BADSLOT:
1256 case -NFS4ERR_BAD_HIGH_SLOT:
1257 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1258 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001259 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001260 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 case -NFS4ERR_STALE_CLIENTID:
1262 case -NFS4ERR_STALE_STATEID:
1263 case -NFS4ERR_EXPIRED:
1264 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001265 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001266 goto out;
1267 case -ERESTARTSYS:
1268 /*
1269 * The show must go on: exit, but mark the
1270 * stateid as needing recovery.
1271 */
1272 case -NFS4ERR_ADMIN_REVOKED:
1273 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001274 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001275 case -EKEYEXPIRED:
1276 /*
1277 * User RPCSEC_GSS context has expired.
1278 * We cannot recover this stateid now, so
1279 * skip it and allow recovery thread to
1280 * proceed.
1281 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001282 case -ENOMEM:
1283 err = 0;
1284 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286 err = nfs4_handle_exception(server, err, &exception);
1287 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001288out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return err;
1290}
1291
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001292static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1293{
1294 struct nfs4_opendata *data = calldata;
1295
1296 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001297 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001298 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1299 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001300 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001301 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001302 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001303 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001304}
1305
1306static void nfs4_open_confirm_release(void *calldata)
1307{
1308 struct nfs4_opendata *data = calldata;
1309 struct nfs4_state *state = NULL;
1310
1311 /* If this request hasn't been cancelled, do nothing */
1312 if (data->cancelled == 0)
1313 goto out_free;
1314 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001315 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001316 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001317 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001318 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001319 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001320out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001321 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001322}
1323
1324static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001325 .rpc_call_done = nfs4_open_confirm_done,
1326 .rpc_release = nfs4_open_confirm_release,
1327};
1328
1329/*
1330 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1331 */
1332static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1333{
1334 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1335 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001336 struct rpc_message msg = {
1337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1338 .rpc_argp = &data->c_arg,
1339 .rpc_resp = &data->c_res,
1340 .rpc_cred = data->owner->so_cred,
1341 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001342 struct rpc_task_setup task_setup_data = {
1343 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001344 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001345 .callback_ops = &nfs4_open_confirm_ops,
1346 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001347 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001348 .flags = RPC_TASK_ASYNC,
1349 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 int status;
1351
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001352 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001353 data->rpc_done = 0;
1354 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001355 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001356 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001357 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001358 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001359 status = nfs4_wait_for_completion_rpc_task(task);
1360 if (status != 0) {
1361 data->cancelled = 1;
1362 smp_wmb();
1363 } else
1364 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001365 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return status;
1367}
1368
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001369static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001371 struct nfs4_opendata *data = calldata;
1372 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001373
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001374 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1375 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001376 /*
1377 * Check if we still need to send an OPEN call, or if we can use
1378 * a delegation instead.
1379 */
1380 if (data->state != NULL) {
1381 struct nfs_delegation *delegation;
1382
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001383 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001384 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001385 rcu_read_lock();
1386 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1387 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001388 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001389 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001390 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001391 }
1392 rcu_read_unlock();
1393 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001394 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001395 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001396 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001397 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001398 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001399 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1400 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001401 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001402 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001403 &data->o_arg.seq_args,
1404 &data->o_res.seq_res, 1, task))
1405 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001406 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001407 return;
1408out_no_action:
1409 task->tk_action = NULL;
1410
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001411}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001413static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1414{
1415 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1416 nfs4_open_prepare(task, calldata);
1417}
1418
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001419static void nfs4_open_done(struct rpc_task *task, void *calldata)
1420{
1421 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001423 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001424
Trond Myklebust14516c32010-07-31 14:29:06 -04001425 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1426 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001427
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001428 if (task->tk_status == 0) {
1429 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001430 case S_IFREG:
1431 break;
1432 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001433 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001434 break;
1435 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001436 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001437 break;
1438 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001439 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001440 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001441 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001442 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1443 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001444 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001445 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001446}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001447
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001448static void nfs4_open_release(void *calldata)
1449{
1450 struct nfs4_opendata *data = calldata;
1451 struct nfs4_state *state = NULL;
1452
1453 /* If this request hasn't been cancelled, do nothing */
1454 if (data->cancelled == 0)
1455 goto out_free;
1456 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001457 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001458 goto out_free;
1459 /* In case we need an open_confirm, no cleanup! */
1460 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1461 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001462 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001463 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001464 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001465out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001466 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001467}
1468
1469static const struct rpc_call_ops nfs4_open_ops = {
1470 .rpc_call_prepare = nfs4_open_prepare,
1471 .rpc_call_done = nfs4_open_done,
1472 .rpc_release = nfs4_open_release,
1473};
1474
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001475static const struct rpc_call_ops nfs4_recover_open_ops = {
1476 .rpc_call_prepare = nfs4_recover_open_prepare,
1477 .rpc_call_done = nfs4_open_done,
1478 .rpc_release = nfs4_open_release,
1479};
1480
1481static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001482{
1483 struct inode *dir = data->dir->d_inode;
1484 struct nfs_server *server = NFS_SERVER(dir);
1485 struct nfs_openargs *o_arg = &data->o_arg;
1486 struct nfs_openres *o_res = &data->o_res;
1487 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001488 struct rpc_message msg = {
1489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1490 .rpc_argp = o_arg,
1491 .rpc_resp = o_res,
1492 .rpc_cred = data->owner->so_cred,
1493 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001494 struct rpc_task_setup task_setup_data = {
1495 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001496 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001497 .callback_ops = &nfs4_open_ops,
1498 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001499 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001500 .flags = RPC_TASK_ASYNC,
1501 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001502 int status;
1503
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001504 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001505 data->rpc_done = 0;
1506 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001507 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001508 if (isrecover)
1509 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001510 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001511 if (IS_ERR(task))
1512 return PTR_ERR(task);
1513 status = nfs4_wait_for_completion_rpc_task(task);
1514 if (status != 0) {
1515 data->cancelled = 1;
1516 smp_wmb();
1517 } else
1518 status = data->rpc_status;
1519 rpc_put_task(task);
1520
1521 return status;
1522}
1523
1524static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1525{
1526 struct inode *dir = data->dir->d_inode;
1527 struct nfs_openres *o_res = &data->o_res;
1528 int status;
1529
1530 status = nfs4_run_open_task(data, 1);
1531 if (status != 0 || !data->rpc_done)
1532 return status;
1533
1534 nfs_refresh_inode(dir, o_res->dir_attr);
1535
1536 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1537 status = _nfs4_proc_open_confirm(data);
1538 if (status != 0)
1539 return status;
1540 }
1541
1542 return status;
1543}
1544
1545/*
1546 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1547 */
1548static int _nfs4_proc_open(struct nfs4_opendata *data)
1549{
1550 struct inode *dir = data->dir->d_inode;
1551 struct nfs_server *server = NFS_SERVER(dir);
1552 struct nfs_openargs *o_arg = &data->o_arg;
1553 struct nfs_openres *o_res = &data->o_res;
1554 int status;
1555
1556 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001557 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001558 return status;
1559
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001560 if (o_arg->open_flags & O_CREAT) {
1561 update_changeattr(dir, &o_res->cinfo);
1562 nfs_post_op_update_inode(dir, o_res->dir_attr);
1563 } else
1564 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001565 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1566 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001568 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001570 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001573 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001574 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
Trond Myklebust10afec92007-05-14 17:16:04 -04001577static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001578{
David Howells7539bba2006-08-22 20:06:09 -04001579 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001580 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001581 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001582
Trond Myklebusta78cb572009-08-09 15:06:19 -04001583 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001584 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001585 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001586 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001587 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1588 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001589 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001590 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001591 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001592 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001593 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001594}
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596/*
1597 * OPEN_EXPIRED:
1598 * reclaim state on the server after a network partition.
1599 * Assumes caller holds the appropriate lock
1600 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001601static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001603 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001604 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001606 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1607 if (IS_ERR(opendata))
1608 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001609 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001610 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001611 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001612 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001613 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
1615
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001616static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001617{
Trond Myklebust539cd032007-06-05 11:46:42 -04001618 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001619 struct nfs4_exception exception = { };
1620 int err;
1621
1622 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001623 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001624 switch (err) {
1625 default:
1626 goto out;
1627 case -NFS4ERR_GRACE:
1628 case -NFS4ERR_DELAY:
1629 nfs4_handle_exception(server, err, &exception);
1630 err = 0;
1631 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001632 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001633out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001634 return err;
1635}
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1638{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001640 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Trond Myklebust864472e2006-01-03 09:55:15 +01001642 ctx = nfs4_state_find_open_context(state);
1643 if (IS_ERR(ctx))
1644 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001645 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001646 put_nfs_open_context(ctx);
1647 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
1649
1650/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001651 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1652 * fields corresponding to attributes that were used to store the verifier.
1653 * Make sure we clobber those fields in the later setattr call
1654 */
1655static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1656{
1657 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1658 !(sattr->ia_valid & ATTR_ATIME_SET))
1659 sattr->ia_valid |= ATTR_ATIME;
1660
1661 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1662 !(sattr->ia_valid & ATTR_MTIME_SET))
1663 sattr->ia_valid |= ATTR_MTIME;
1664}
1665
1666/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001667 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001669static 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 -07001670{
1671 struct nfs4_state_owner *sp;
1672 struct nfs4_state *state = NULL;
1673 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001674 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001675 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 status = -ENOMEM;
1679 if (!(sp = nfs4_get_state_owner(server, cred))) {
1680 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1681 goto out_err;
1682 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001683 status = nfs4_recover_expired_lease(server);
1684 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001685 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001686 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001687 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001688 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001689 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001690 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001691 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Trond Myklebustaac00a82007-07-05 19:02:21 -04001693 if (path->dentry->d_inode != NULL)
1694 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1695
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001696 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001698 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001700 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001701 status = PTR_ERR(state);
1702 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001703 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001704 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001705 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001706
1707 if (opendata->o_arg.open_flags & O_EXCL) {
1708 nfs4_exclusive_attrset(opendata, sattr);
1709
1710 nfs_fattr_init(opendata->o_res.f_attr);
1711 status = nfs4_do_setattr(state->inode, cred,
1712 opendata->o_res.f_attr, sattr,
1713 state);
1714 if (status == 0)
1715 nfs_setattr_update_inode(state->inode, sattr);
1716 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1717 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001718 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 *res = state;
1721 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001722err_opendata_put:
1723 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001724err_put_state_owner:
1725 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 *res = NULL;
1728 return status;
1729}
1730
1731
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001732static 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 -07001733{
1734 struct nfs4_exception exception = { };
1735 struct nfs4_state *res;
1736 int status;
1737
1738 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001739 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (status == 0)
1741 break;
1742 /* NOTE: BAD_SEQID means the server and client disagree about the
1743 * book-keeping w.r.t. state-changing operations
1744 * (OPEN/CLOSE/LOCK/LOCKU...)
1745 * It is actually a sign of a bug on the client or on the server.
1746 *
1747 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001748 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 * have unhashed the old state_owner for us, and that we can
1750 * therefore safely retry using a new one. We should still warn
1751 * the user though...
1752 */
1753 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001754 printk(KERN_WARNING "NFS: v4 server %s "
1755 " returned a bad sequence-id error!\n",
1756 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 exception.retry = 1;
1758 continue;
1759 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001760 /*
1761 * BAD_STATEID on OPEN means that the server cancelled our
1762 * state before it received the OPEN_CONFIRM.
1763 * Recover by retrying the request as per the discussion
1764 * on Page 181 of RFC3530.
1765 */
1766 if (status == -NFS4ERR_BAD_STATEID) {
1767 exception.retry = 1;
1768 continue;
1769 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001770 if (status == -EAGAIN) {
1771 /* We must have found a delegation */
1772 exception.retry = 1;
1773 continue;
1774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1776 status, &exception));
1777 } while (exception.retry);
1778 return res;
1779}
1780
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001781static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1782 struct nfs_fattr *fattr, struct iattr *sattr,
1783 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001785 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001787 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 .iap = sattr,
1789 .server = server,
1790 .bitmask = server->attr_bitmask,
1791 };
1792 struct nfs_setattrres res = {
1793 .fattr = fattr,
1794 .server = server,
1795 };
1796 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001797 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1798 .rpc_argp = &arg,
1799 .rpc_resp = &res,
1800 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001802 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001803 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Trond Myklebust0e574af2005-10-27 22:12:38 -04001805 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001807 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1808 /* Use that stateid */
1809 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001810 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001811 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1813
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001814 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001815 if (status == 0 && state != NULL)
1816 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001817 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001820static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1821 struct nfs_fattr *fattr, struct iattr *sattr,
1822 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001824 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 struct nfs4_exception exception = { };
1826 int err;
1827 do {
1828 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001829 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 &exception);
1831 } while (exception.retry);
1832 return err;
1833}
1834
1835struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001836 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 struct inode *inode;
1838 struct nfs4_state *state;
1839 struct nfs_closeargs arg;
1840 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001841 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001842 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001843 bool roc;
1844 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845};
1846
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001847static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001848{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001849 struct nfs4_closedata *calldata = data;
1850 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001851
Fred Isamanf7e89172011-01-06 11:36:32 +00001852 if (calldata->roc)
1853 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001854 nfs4_put_open_state(calldata->state);
1855 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001856 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001857 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001858 kfree(calldata);
1859}
1860
Trond Myklebust88069f72009-12-08 08:33:16 -05001861static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1862 fmode_t fmode)
1863{
1864 spin_lock(&state->owner->so_lock);
1865 if (!(fmode & FMODE_READ))
1866 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1867 if (!(fmode & FMODE_WRITE))
1868 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1869 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1870 spin_unlock(&state->owner->so_lock);
1871}
1872
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001873static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001875 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 struct nfs_server *server = NFS_SERVER(calldata->inode);
1878
Trond Myklebust14516c32010-07-31 14:29:06 -04001879 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1880 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 /* hmm. we are done with the inode, and in the process of freeing
1882 * the state_owner. we keep this around to process errors
1883 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 switch (task->tk_status) {
1885 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001886 if (calldata->roc)
1887 pnfs_roc_set_barrier(state->inode,
1888 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001889 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001890 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001891 nfs4_close_clear_stateid_flags(state,
1892 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 break;
1894 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001895 case -NFS4ERR_OLD_STATEID:
1896 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001898 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001899 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001901 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1902 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001904 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001905 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001908static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001910 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001911 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001912 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001913
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001914 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001915 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001916
Trond Myklebust88069f72009-12-08 08:33:16 -05001917 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1918 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001919 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001920 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001921 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001922 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001923 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1924 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1925 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001926 }
1927 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001928 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1929 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1930 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001931 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001932 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001933 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001934
1935 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001936 /* Note: exit _without_ calling nfs4_close_done */
1937 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001938 return;
1939 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001940
Fred Isamanf7e89172011-01-06 11:36:32 +00001941 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001942 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00001943 if (calldata->roc &&
1944 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
1945 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
1946 task, NULL);
1947 return;
1948 }
1949 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001950
Trond Myklebust516a6af2005-10-27 22:12:41 -04001951 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001952 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001953 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001954 &calldata->arg.seq_args, &calldata->res.seq_res,
1955 1, task))
1956 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001957 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001960static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001961 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001962 .rpc_call_done = nfs4_close_done,
1963 .rpc_release = nfs4_free_closedata,
1964};
1965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966/*
1967 * It is possible for data to be read/written from a mem-mapped file
1968 * after the sys_close call (which hits the vfs layer as a flush).
1969 * This means that we can't safely call nfsv4 close on a file until
1970 * the inode is cleared. This in turn means that we are not good
1971 * NFSv4 citizens - we do not indicate to the server to update the file's
1972 * share state even when we are done with one of the three share
1973 * stateid's in the inode.
1974 *
1975 * NOTE: Caller must be holding the sp->so_owner semaphore!
1976 */
Fred Isamanf7e89172011-01-06 11:36:32 +00001977int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001979 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001981 struct nfs4_state_owner *sp = state->owner;
1982 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001983 struct rpc_message msg = {
1984 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1985 .rpc_cred = state->owner->so_cred,
1986 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001987 struct rpc_task_setup task_setup_data = {
1988 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001989 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001990 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001991 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001992 .flags = RPC_TASK_ASYNC,
1993 };
Trond Myklebust95121352005-10-18 14:20:12 -07001994 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001996 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001998 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001999 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002001 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002002 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002004 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002005 if (calldata->arg.seqid == NULL)
2006 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002007 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002008 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002009 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002010 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002011 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002012 calldata->roc = roc;
Al Virof6948692010-01-30 13:51:04 -05002013 path_get(path);
2014 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07002015
Trond Myklebust1174dd12010-12-21 10:52:24 -05002016 msg.rpc_argp = &calldata->arg;
2017 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002018 task_setup_data.callback_data = calldata;
2019 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002020 if (IS_ERR(task))
2021 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002022 status = 0;
2023 if (wait)
2024 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002025 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002026 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002027out_free_calldata:
2028 kfree(calldata);
2029out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002030 if (roc)
2031 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002032 nfs4_put_open_state(state);
2033 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002034 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036
Trond Myklebust2b484292010-09-17 10:56:51 -04002037static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002038nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 struct nfs4_state *state;
2041
Trond Myklebust565277f2007-10-15 18:17:53 -04002042 /* Protect against concurrent sillydeletes */
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002043 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002044 if (IS_ERR(state))
2045 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002046 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002047 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
Trond Myklebust1185a552009-12-03 15:54:02 -05002050static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002051{
2052 if (ctx->state == NULL)
2053 return;
2054 if (is_sync)
2055 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2056 else
2057 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2058}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2061{
Benny Halevy43652ad2009-04-01 09:21:54 -04002062 struct nfs4_server_caps_arg args = {
2063 .fhandle = fhandle,
2064 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 struct nfs4_server_caps_res res = {};
2066 struct rpc_message msg = {
2067 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002068 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 .rpc_resp = &res,
2070 };
2071 int status;
2072
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002073 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (status == 0) {
2075 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002076 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2077 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2078 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2079 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2080 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2082 server->caps |= NFS_CAP_ACLS;
2083 if (res.has_links != 0)
2084 server->caps |= NFS_CAP_HARDLINKS;
2085 if (res.has_symlinks != 0)
2086 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002087 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2088 server->caps |= NFS_CAP_FILEID;
2089 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2090 server->caps |= NFS_CAP_MODE;
2091 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2092 server->caps |= NFS_CAP_NLINK;
2093 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2094 server->caps |= NFS_CAP_OWNER;
2095 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2096 server->caps |= NFS_CAP_OWNER_GROUP;
2097 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2098 server->caps |= NFS_CAP_ATIME;
2099 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2100 server->caps |= NFS_CAP_CTIME;
2101 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2102 server->caps |= NFS_CAP_MTIME;
2103
Trond Myklebusta65318b2009-03-11 14:10:28 -04002104 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2105 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2106 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 server->acl_bitmask = res.acl_bitmask;
2108 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 return status;
2111}
2112
Trond Myklebust55a97592006-06-09 09:34:19 -04002113int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
2115 struct nfs4_exception exception = { };
2116 int err;
2117 do {
2118 err = nfs4_handle_exception(server,
2119 _nfs4_server_capabilities(server, fhandle),
2120 &exception);
2121 } while (exception.retry);
2122 return err;
2123}
2124
2125static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2126 struct nfs_fsinfo *info)
2127{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 struct nfs4_lookup_root_arg args = {
2129 .bitmask = nfs4_fattr_bitmap,
2130 };
2131 struct nfs4_lookup_res res = {
2132 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002133 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 .fh = fhandle,
2135 };
2136 struct rpc_message msg = {
2137 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2138 .rpc_argp = &args,
2139 .rpc_resp = &res,
2140 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002141
Trond Myklebust0e574af2005-10-27 22:12:38 -04002142 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002143 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
2146static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2147 struct nfs_fsinfo *info)
2148{
2149 struct nfs4_exception exception = { };
2150 int err;
2151 do {
2152 err = nfs4_handle_exception(server,
2153 _nfs4_lookup_root(server, fhandle, info),
2154 &exception);
2155 } while (exception.retry);
2156 return err;
2157}
2158
David Howells54ceac42006-08-22 20:06:13 -04002159/*
2160 * get the file handle for the "/" directory on the server
2161 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002163 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 int status;
2166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 if (status == 0)
2169 status = nfs4_server_capabilities(server, fhandle);
2170 if (status == 0)
2171 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002172 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173}
2174
Manoj Naik6b97fd32006-06-09 09:34:29 -04002175/*
2176 * Get locations and (maybe) other attributes of a referral.
2177 * Note that we'll actually follow the referral later when
2178 * we detect fsid mismatch in inode revalidation
2179 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002180static 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 -04002181{
2182 int status = -ENOMEM;
2183 struct page *page = NULL;
2184 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002185
2186 page = alloc_page(GFP_KERNEL);
2187 if (page == NULL)
2188 goto out;
2189 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2190 if (locations == NULL)
2191 goto out;
2192
Trond Myklebustc228fd32007-01-13 02:28:11 -05002193 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002194 if (status != 0)
2195 goto out;
2196 /* Make sure server returned a different fsid for the referral */
2197 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002198 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 -04002199 status = -EIO;
2200 goto out;
2201 }
2202
2203 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2204 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2205 if (!fattr->mode)
2206 fattr->mode = S_IFDIR;
2207 memset(fhandle, 0, sizeof(struct nfs_fh));
2208out:
2209 if (page)
2210 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002211 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002212 return status;
2213}
2214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2216{
2217 struct nfs4_getattr_arg args = {
2218 .fh = fhandle,
2219 .bitmask = server->attr_bitmask,
2220 };
2221 struct nfs4_getattr_res res = {
2222 .fattr = fattr,
2223 .server = server,
2224 };
2225 struct rpc_message msg = {
2226 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2227 .rpc_argp = &args,
2228 .rpc_resp = &res,
2229 };
2230
Trond Myklebust0e574af2005-10-27 22:12:38 -04002231 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002232 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
2235static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2236{
2237 struct nfs4_exception exception = { };
2238 int err;
2239 do {
2240 err = nfs4_handle_exception(server,
2241 _nfs4_proc_getattr(server, fhandle, fattr),
2242 &exception);
2243 } while (exception.retry);
2244 return err;
2245}
2246
2247/*
2248 * The file is not closed if it is opened due to the a request to change
2249 * the size of the file. The open call will not be needed once the
2250 * VFS layer lookup-intents are implemented.
2251 *
2252 * Close is called when the inode is destroyed.
2253 * If we haven't opened the file for O_WRONLY, we
2254 * need to in the size_change case to obtain a stateid.
2255 *
2256 * Got race?
2257 * Because OPEN is always done by name in nfsv4, it is
2258 * possible that we opened a different file by the same
2259 * name. We can recognize this race condition, but we
2260 * can't do anything about it besides returning an error.
2261 *
2262 * This will be fixed with VFS changes (lookup-intent).
2263 */
2264static int
2265nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2266 struct iattr *sattr)
2267{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002268 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002269 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002270 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 int status;
2272
Trond Myklebust0e574af2005-10-27 22:12:38 -04002273 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Trond Myklebustd5308382005-11-04 15:33:38 -05002275 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002276 if (sattr->ia_valid & ATTR_FILE) {
2277 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002278
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002279 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002280 if (ctx) {
2281 cred = ctx->cred;
2282 state = ctx->state;
2283 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002284 }
2285
2286 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002287 if (status == 0)
2288 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 return status;
2290}
2291
Trond Myklebust56659e92007-07-17 21:52:39 -04002292static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2293 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002294 struct nfs_fattr *fattr)
2295{
2296 int status;
2297 struct nfs4_lookup_arg args = {
2298 .bitmask = server->attr_bitmask,
2299 .dir_fh = dirfh,
2300 .name = name,
2301 };
2302 struct nfs4_lookup_res res = {
2303 .server = server,
2304 .fattr = fattr,
2305 .fh = fhandle,
2306 };
2307 struct rpc_message msg = {
2308 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2309 .rpc_argp = &args,
2310 .rpc_resp = &res,
2311 };
2312
2313 nfs_fattr_init(fattr);
2314
2315 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002316 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002317 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002318 return status;
2319}
2320
2321static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2322 struct qstr *name, struct nfs_fh *fhandle,
2323 struct nfs_fattr *fattr)
2324{
2325 struct nfs4_exception exception = { };
2326 int err;
2327 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002328 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2329 /* FIXME: !!!! */
2330 if (err == -NFS4ERR_MOVED) {
2331 err = -EREMOTE;
2332 break;
2333 }
2334 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002335 } while (exception.retry);
2336 return err;
2337}
2338
Trond Myklebust56659e92007-07-17 21:52:39 -04002339static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2341{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002342 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
2344 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002345 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002346 if (status == -NFS4ERR_MOVED)
2347 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 dprintk("NFS reply lookup: %d\n", status);
2349 return status;
2350}
2351
2352static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2353{
2354 struct nfs4_exception exception = { };
2355 int err;
2356 do {
2357 err = nfs4_handle_exception(NFS_SERVER(dir),
2358 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2359 &exception);
2360 } while (exception.retry);
2361 return err;
2362}
2363
2364static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2365{
Trond Myklebust76b32992007-08-10 17:45:11 -04002366 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 struct nfs4_accessargs args = {
2368 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002369 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002371 struct nfs4_accessres res = {
2372 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002373 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 struct rpc_message msg = {
2375 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2376 .rpc_argp = &args,
2377 .rpc_resp = &res,
2378 .rpc_cred = entry->cred,
2379 };
2380 int mode = entry->mask;
2381 int status;
2382
2383 /*
2384 * Determine which access bits we want to ask for...
2385 */
2386 if (mode & MAY_READ)
2387 args.access |= NFS4_ACCESS_READ;
2388 if (S_ISDIR(inode->i_mode)) {
2389 if (mode & MAY_WRITE)
2390 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2391 if (mode & MAY_EXEC)
2392 args.access |= NFS4_ACCESS_LOOKUP;
2393 } else {
2394 if (mode & MAY_WRITE)
2395 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2396 if (mode & MAY_EXEC)
2397 args.access |= NFS4_ACCESS_EXECUTE;
2398 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002399
2400 res.fattr = nfs_alloc_fattr();
2401 if (res.fattr == NULL)
2402 return -ENOMEM;
2403
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002404 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 if (!status) {
2406 entry->mask = 0;
2407 if (res.access & NFS4_ACCESS_READ)
2408 entry->mask |= MAY_READ;
2409 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2410 entry->mask |= MAY_WRITE;
2411 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2412 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002413 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002415 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 return status;
2417}
2418
2419static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2420{
2421 struct nfs4_exception exception = { };
2422 int err;
2423 do {
2424 err = nfs4_handle_exception(NFS_SERVER(inode),
2425 _nfs4_proc_access(inode, entry),
2426 &exception);
2427 } while (exception.retry);
2428 return err;
2429}
2430
2431/*
2432 * TODO: For the time being, we don't try to get any attributes
2433 * along with any of the zero-copy operations READ, READDIR,
2434 * READLINK, WRITE.
2435 *
2436 * In the case of the first three, we want to put the GETATTR
2437 * after the read-type operation -- this is because it is hard
2438 * to predict the length of a GETATTR response in v4, and thus
2439 * align the READ data correctly. This means that the GETATTR
2440 * may end up partially falling into the page cache, and we should
2441 * shift it into the 'tail' of the xdr_buf before processing.
2442 * To do this efficiently, we need to know the total length
2443 * of data received, which doesn't seem to be available outside
2444 * of the RPC layer.
2445 *
2446 * In the case of WRITE, we also want to put the GETATTR after
2447 * the operation -- in this case because we want to make sure
2448 * we get the post-operation mtime and size. This means that
2449 * we can't use xdr_encode_pages() as written: we need a variant
2450 * of it which would leave room in the 'tail' iovec.
2451 *
2452 * Both of these changes to the XDR layer would in fact be quite
2453 * minor, but I decided to leave them for a subsequent patch.
2454 */
2455static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2456 unsigned int pgbase, unsigned int pglen)
2457{
2458 struct nfs4_readlink args = {
2459 .fh = NFS_FH(inode),
2460 .pgbase = pgbase,
2461 .pglen = pglen,
2462 .pages = &page,
2463 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002464 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 struct rpc_message msg = {
2466 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2467 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002468 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 };
2470
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002471 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472}
2473
2474static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2475 unsigned int pgbase, unsigned int pglen)
2476{
2477 struct nfs4_exception exception = { };
2478 int err;
2479 do {
2480 err = nfs4_handle_exception(NFS_SERVER(inode),
2481 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2482 &exception);
2483 } while (exception.retry);
2484 return err;
2485}
2486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487/*
2488 * Got race?
2489 * We will need to arrange for the VFS layer to provide an atomic open.
2490 * Until then, this create/open method is prone to inefficiency and race
2491 * conditions due to the lookup, create, and open VFS calls from sys_open()
2492 * placed on the wire.
2493 *
2494 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2495 * The file will be opened again in the subsequent VFS open call
2496 * (nfs4_proc_file_open).
2497 *
2498 * The open for read will just hang around to be used by any process that
2499 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2500 */
2501
2502static int
2503nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002504 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002506 struct path my_path = {
Trond Myklebustad389da2007-06-05 12:30:00 -04002507 .dentry = dentry,
2508 };
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002509 struct path *path = &my_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002511 struct rpc_cred *cred = NULL;
2512 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 int status = 0;
2514
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002515 if (ctx != NULL) {
2516 cred = ctx->cred;
2517 path = &ctx->path;
2518 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002520 sattr->ia_mode &= ~current_umask();
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002521 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002522 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 if (IS_ERR(state)) {
2524 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002525 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002527 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002528 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002529 if (ctx != NULL)
2530 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002531 else
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002532 nfs4_close_sync(path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533out:
2534 return status;
2535}
2536
2537static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2538{
Trond Myklebust16e42952005-10-27 22:12:44 -04002539 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002540 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002542 .name.len = name->len,
2543 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002544 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002546 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002547 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002548 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002550 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2551 .rpc_argp = &args,
2552 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002554 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Trond Myklebustd3468902010-04-16 16:22:50 -04002556 res.dir_attr = nfs_alloc_fattr();
2557 if (res.dir_attr == NULL)
2558 goto out;
2559
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002560 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002561 if (status == 0) {
2562 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002563 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002564 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002565 nfs_free_fattr(res.dir_attr);
2566out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 return status;
2568}
2569
2570static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2571{
2572 struct nfs4_exception exception = { };
2573 int err;
2574 do {
2575 err = nfs4_handle_exception(NFS_SERVER(dir),
2576 _nfs4_proc_remove(dir, name),
2577 &exception);
2578 } while (exception.retry);
2579 return err;
2580}
2581
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002582static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002584 struct nfs_server *server = NFS_SERVER(dir);
2585 struct nfs_removeargs *args = msg->rpc_argp;
2586 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Trond Myklebusta65318b2009-03-11 14:10:28 -04002588 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002589 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002590 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592}
2593
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002594static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002596 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2597
Trond Myklebust14516c32010-07-31 14:29:06 -04002598 if (!nfs4_sequence_done(task, &res->seq_res))
2599 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002600 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002601 return 0;
2602 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002603 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002604 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605}
2606
Jeff Laytond3d41522010-09-17 17:31:57 -04002607static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2608{
2609 struct nfs_server *server = NFS_SERVER(dir);
2610 struct nfs_renameargs *arg = msg->rpc_argp;
2611 struct nfs_renameres *res = msg->rpc_resp;
2612
2613 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2614 arg->bitmask = server->attr_bitmask;
2615 res->server = server;
2616}
2617
2618static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2619 struct inode *new_dir)
2620{
2621 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2622
2623 if (!nfs4_sequence_done(task, &res->seq_res))
2624 return 0;
2625 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2626 return 0;
2627
2628 update_changeattr(old_dir, &res->old_cinfo);
2629 nfs_post_op_update_inode(old_dir, res->old_fattr);
2630 update_changeattr(new_dir, &res->new_cinfo);
2631 nfs_post_op_update_inode(new_dir, res->new_fattr);
2632 return 1;
2633}
2634
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2636 struct inode *new_dir, struct qstr *new_name)
2637{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002638 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002639 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 .old_dir = NFS_FH(old_dir),
2641 .new_dir = NFS_FH(new_dir),
2642 .old_name = old_name,
2643 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002644 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002646 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002647 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002648 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 struct rpc_message msg = {
2650 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2651 .rpc_argp = &arg,
2652 .rpc_resp = &res,
2653 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002654 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Trond Myklebust011fff72010-04-16 16:22:49 -04002656 res.old_fattr = nfs_alloc_fattr();
2657 res.new_fattr = nfs_alloc_fattr();
2658 if (res.old_fattr == NULL || res.new_fattr == NULL)
2659 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Trond Myklebust011fff72010-04-16 16:22:49 -04002661 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 if (!status) {
2663 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002664 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002666 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002668out:
2669 nfs_free_fattr(res.new_fattr);
2670 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 return status;
2672}
2673
2674static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2675 struct inode *new_dir, struct qstr *new_name)
2676{
2677 struct nfs4_exception exception = { };
2678 int err;
2679 do {
2680 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2681 _nfs4_proc_rename(old_dir, old_name,
2682 new_dir, new_name),
2683 &exception);
2684 } while (exception.retry);
2685 return err;
2686}
2687
2688static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2689{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002690 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 struct nfs4_link_arg arg = {
2692 .fh = NFS_FH(inode),
2693 .dir_fh = NFS_FH(dir),
2694 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002695 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002697 struct nfs4_link_res res = {
2698 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002699 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 struct rpc_message msg = {
2701 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2702 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002703 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002705 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Trond Myklebust136f2622010-04-16 16:22:49 -04002707 res.fattr = nfs_alloc_fattr();
2708 res.dir_attr = nfs_alloc_fattr();
2709 if (res.fattr == NULL || res.dir_attr == NULL)
2710 goto out;
2711
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002712 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002713 if (!status) {
2714 update_changeattr(dir, &res.cinfo);
2715 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002716 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002717 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002718out:
2719 nfs_free_fattr(res.dir_attr);
2720 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 return status;
2722}
2723
2724static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2725{
2726 struct nfs4_exception exception = { };
2727 int err;
2728 do {
2729 err = nfs4_handle_exception(NFS_SERVER(inode),
2730 _nfs4_proc_link(inode, dir, name),
2731 &exception);
2732 } while (exception.retry);
2733 return err;
2734}
2735
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002736struct nfs4_createdata {
2737 struct rpc_message msg;
2738 struct nfs4_create_arg arg;
2739 struct nfs4_create_res res;
2740 struct nfs_fh fh;
2741 struct nfs_fattr fattr;
2742 struct nfs_fattr dir_fattr;
2743};
2744
2745static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2746 struct qstr *name, struct iattr *sattr, u32 ftype)
2747{
2748 struct nfs4_createdata *data;
2749
2750 data = kzalloc(sizeof(*data), GFP_KERNEL);
2751 if (data != NULL) {
2752 struct nfs_server *server = NFS_SERVER(dir);
2753
2754 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2755 data->msg.rpc_argp = &data->arg;
2756 data->msg.rpc_resp = &data->res;
2757 data->arg.dir_fh = NFS_FH(dir);
2758 data->arg.server = server;
2759 data->arg.name = name;
2760 data->arg.attrs = sattr;
2761 data->arg.ftype = ftype;
2762 data->arg.bitmask = server->attr_bitmask;
2763 data->res.server = server;
2764 data->res.fh = &data->fh;
2765 data->res.fattr = &data->fattr;
2766 data->res.dir_fattr = &data->dir_fattr;
2767 nfs_fattr_init(data->res.fattr);
2768 nfs_fattr_init(data->res.dir_fattr);
2769 }
2770 return data;
2771}
2772
2773static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2774{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002775 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2776 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002777 if (status == 0) {
2778 update_changeattr(dir, &data->res.dir_cinfo);
2779 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2780 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2781 }
2782 return status;
2783}
2784
2785static void nfs4_free_createdata(struct nfs4_createdata *data)
2786{
2787 kfree(data);
2788}
2789
Chuck Lever4f390c12006-08-22 20:06:22 -04002790static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002791 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002793 struct nfs4_createdata *data;
2794 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
Chuck Lever94a6d752006-08-22 20:06:23 -04002796 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002797 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002798
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002799 status = -ENOMEM;
2800 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2801 if (data == NULL)
2802 goto out;
2803
2804 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2805 data->arg.u.symlink.pages = &page;
2806 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002808 status = nfs4_do_create(dir, dentry, data);
2809
2810 nfs4_free_createdata(data);
2811out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 return status;
2813}
2814
Chuck Lever4f390c12006-08-22 20:06:22 -04002815static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002816 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{
2818 struct nfs4_exception exception = { };
2819 int err;
2820 do {
2821 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002822 _nfs4_proc_symlink(dir, dentry, page,
2823 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 &exception);
2825 } while (exception.retry);
2826 return err;
2827}
2828
2829static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2830 struct iattr *sattr)
2831{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002832 struct nfs4_createdata *data;
2833 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002835 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2836 if (data == NULL)
2837 goto out;
2838
2839 status = nfs4_do_create(dir, dentry, data);
2840
2841 nfs4_free_createdata(data);
2842out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 return status;
2844}
2845
2846static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2847 struct iattr *sattr)
2848{
2849 struct nfs4_exception exception = { };
2850 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002851
2852 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 do {
2854 err = nfs4_handle_exception(NFS_SERVER(dir),
2855 _nfs4_proc_mkdir(dir, dentry, sattr),
2856 &exception);
2857 } while (exception.retry);
2858 return err;
2859}
2860
2861static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002862 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
2864 struct inode *dir = dentry->d_inode;
2865 struct nfs4_readdir_arg args = {
2866 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002867 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 .pgbase = 0,
2869 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002870 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002871 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 };
2873 struct nfs4_readdir_res res;
2874 struct rpc_message msg = {
2875 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2876 .rpc_argp = &args,
2877 .rpc_resp = &res,
2878 .rpc_cred = cred,
2879 };
2880 int status;
2881
Harvey Harrison3110ff82008-05-02 13:42:44 -07002882 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002883 dentry->d_parent->d_name.name,
2884 dentry->d_name.name,
2885 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2887 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002888 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05002889 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05002891 status += args.pgbase;
2892 }
Trond Myklebustc4812992007-09-28 17:11:45 -04002893
2894 nfs_invalidate_atime(dir);
2895
Harvey Harrison3110ff82008-05-02 13:42:44 -07002896 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 return status;
2898}
2899
2900static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002901 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
2903 struct nfs4_exception exception = { };
2904 int err;
2905 do {
2906 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2907 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002908 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 &exception);
2910 } while (exception.retry);
2911 return err;
2912}
2913
2914static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2915 struct iattr *sattr, dev_t rdev)
2916{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002917 struct nfs4_createdata *data;
2918 int mode = sattr->ia_mode;
2919 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
2921 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2922 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002923
2924 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2925 if (data == NULL)
2926 goto out;
2927
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002929 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002931 data->arg.ftype = NF4BLK;
2932 data->arg.u.device.specdata1 = MAJOR(rdev);
2933 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 }
2935 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002936 data->arg.ftype = NF4CHR;
2937 data->arg.u.device.specdata1 = MAJOR(rdev);
2938 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002941 status = nfs4_do_create(dir, dentry, data);
2942
2943 nfs4_free_createdata(data);
2944out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 return status;
2946}
2947
2948static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2949 struct iattr *sattr, dev_t rdev)
2950{
2951 struct nfs4_exception exception = { };
2952 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002953
2954 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 do {
2956 err = nfs4_handle_exception(NFS_SERVER(dir),
2957 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2958 &exception);
2959 } while (exception.retry);
2960 return err;
2961}
2962
2963static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2964 struct nfs_fsstat *fsstat)
2965{
2966 struct nfs4_statfs_arg args = {
2967 .fh = fhandle,
2968 .bitmask = server->attr_bitmask,
2969 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002970 struct nfs4_statfs_res res = {
2971 .fsstat = fsstat,
2972 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 struct rpc_message msg = {
2974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2975 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002976 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 };
2978
Trond Myklebust0e574af2005-10-27 22:12:38 -04002979 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002980 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981}
2982
2983static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2984{
2985 struct nfs4_exception exception = { };
2986 int err;
2987 do {
2988 err = nfs4_handle_exception(server,
2989 _nfs4_proc_statfs(server, fhandle, fsstat),
2990 &exception);
2991 } while (exception.retry);
2992 return err;
2993}
2994
2995static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2996 struct nfs_fsinfo *fsinfo)
2997{
2998 struct nfs4_fsinfo_arg args = {
2999 .fh = fhandle,
3000 .bitmask = server->attr_bitmask,
3001 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003002 struct nfs4_fsinfo_res res = {
3003 .fsinfo = fsinfo,
3004 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 struct rpc_message msg = {
3006 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3007 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003008 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 };
3010
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003011 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012}
3013
3014static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3015{
3016 struct nfs4_exception exception = { };
3017 int err;
3018
3019 do {
3020 err = nfs4_handle_exception(server,
3021 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3022 &exception);
3023 } while (exception.retry);
3024 return err;
3025}
3026
3027static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3028{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003029 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3031}
3032
3033static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3034 struct nfs_pathconf *pathconf)
3035{
3036 struct nfs4_pathconf_arg args = {
3037 .fh = fhandle,
3038 .bitmask = server->attr_bitmask,
3039 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003040 struct nfs4_pathconf_res res = {
3041 .pathconf = pathconf,
3042 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 struct rpc_message msg = {
3044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3045 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003046 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 };
3048
3049 /* None of the pathconf attributes are mandatory to implement */
3050 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3051 memset(pathconf, 0, sizeof(*pathconf));
3052 return 0;
3053 }
3054
Trond Myklebust0e574af2005-10-27 22:12:38 -04003055 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003056 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057}
3058
3059static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3060 struct nfs_pathconf *pathconf)
3061{
3062 struct nfs4_exception exception = { };
3063 int err;
3064
3065 do {
3066 err = nfs4_handle_exception(server,
3067 _nfs4_proc_pathconf(server, fhandle, pathconf),
3068 &exception);
3069 } while (exception.retry);
3070 return err;
3071}
3072
Trond Myklebustec06c092006-03-20 13:44:27 -05003073static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074{
Trond Myklebustec06c092006-03-20 13:44:27 -05003075 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003077 dprintk("--> %s\n", __func__);
3078
Trond Myklebust14516c32010-07-31 14:29:06 -04003079 if (!nfs4_sequence_done(task, &data->res.seq_res))
3080 return -EAGAIN;
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003081
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003082 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003083 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003084 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003086
3087 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003089 renew_lease(server, data->timestamp);
3090 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091}
3092
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003093static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003096 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097}
3098
Trond Myklebust788e7a82006-03-20 13:44:27 -05003099static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 struct inode *inode = data->inode;
3102
Trond Myklebust14516c32010-07-31 14:29:06 -04003103 if (!nfs4_sequence_done(task, &data->res.seq_res))
3104 return -EAGAIN;
Andy Adamsondef6ed72009-04-01 09:22:26 -04003105
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003106 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003107 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003108 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003110 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003112 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003113 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115}
3116
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003117static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003119 struct nfs_server *server = NFS_SERVER(data->inode);
3120
Trond Myklebusta65318b2009-03-11 14:10:28 -04003121 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003122 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 data->timestamp = jiffies;
3124
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003125 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126}
3127
Trond Myklebust788e7a82006-03-20 13:44:27 -05003128static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 struct inode *inode = data->inode;
3131
Trond Myklebust14516c32010-07-31 14:29:06 -04003132 if (!nfs4_sequence_done(task, &data->res.seq_res))
3133 return -EAGAIN;
3134
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003135 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003136 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003137 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003139 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003140 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141}
3142
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003143static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003145 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Trond Myklebusta65318b2009-03-11 14:10:28 -04003147 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003148 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003149 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150}
3151
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003152struct nfs4_renewdata {
3153 struct nfs_client *client;
3154 unsigned long timestamp;
3155};
3156
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157/*
3158 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3159 * standalone procedure for queueing an asynchronous RENEW.
3160 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003161static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003162{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003163 struct nfs4_renewdata *data = calldata;
3164 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003165
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003166 if (atomic_read(&clp->cl_count) > 1)
3167 nfs4_schedule_state_renewal(clp);
3168 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003169 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003170}
3171
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003172static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003174 struct nfs4_renewdata *data = calldata;
3175 struct nfs_client *clp = data->client;
3176 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
3178 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003179 /* Unless we're shutting down, schedule state recovery! */
3180 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003181 nfs4_schedule_lease_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 return;
3183 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003184 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185}
3186
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003187static const struct rpc_call_ops nfs4_renew_ops = {
3188 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003189 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003190};
3191
David Howellsadfa6f92006-08-22 20:06:08 -04003192int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
3194 struct rpc_message msg = {
3195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3196 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003197 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003199 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003201 if (!atomic_inc_not_zero(&clp->cl_count))
3202 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003203 data = kmalloc(sizeof(*data), GFP_KERNEL);
3204 if (data == NULL)
3205 return -ENOMEM;
3206 data->client = clp;
3207 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003209 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210}
3211
David Howellsadfa6f92006-08-22 20:06:08 -04003212int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213{
3214 struct rpc_message msg = {
3215 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3216 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003217 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 };
3219 unsigned long now = jiffies;
3220 int status;
3221
3222 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3223 if (status < 0)
3224 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003225 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 return 0;
3227}
3228
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003229static inline int nfs4_server_supports_acls(struct nfs_server *server)
3230{
3231 return (server->caps & NFS_CAP_ACLS)
3232 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3233 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3234}
3235
3236/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3237 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3238 * the stack.
3239 */
3240#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3241
3242static void buf_to_pages(const void *buf, size_t buflen,
3243 struct page **pages, unsigned int *pgbase)
3244{
3245 const void *p = buf;
3246
3247 *pgbase = offset_in_page(buf);
3248 p -= *pgbase;
3249 while (p < buf + buflen) {
3250 *(pages++) = virt_to_page(p);
3251 p += PAGE_CACHE_SIZE;
3252 }
3253}
3254
Neil Hormane9e3d722011-03-04 19:26:03 -05003255static int buf_to_pages_noslab(const void *buf, size_t buflen,
3256 struct page **pages, unsigned int *pgbase)
3257{
3258 struct page *newpage, **spages;
3259 int rc = 0;
3260 size_t len;
3261 spages = pages;
3262
3263 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003264 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003265 newpage = alloc_page(GFP_KERNEL);
3266
3267 if (newpage == NULL)
3268 goto unwind;
3269 memcpy(page_address(newpage), buf, len);
3270 buf += len;
3271 buflen -= len;
3272 *pages++ = newpage;
3273 rc++;
3274 } while (buflen != 0);
3275
3276 return rc;
3277
3278unwind:
3279 for(; rc > 0; rc--)
3280 __free_page(spages[rc-1]);
3281 return -ENOMEM;
3282}
3283
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003284struct nfs4_cached_acl {
3285 int cached;
3286 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003287 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003288};
3289
3290static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003291{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003292 struct nfs_inode *nfsi = NFS_I(inode);
3293
3294 spin_lock(&inode->i_lock);
3295 kfree(nfsi->nfs4_acl);
3296 nfsi->nfs4_acl = acl;
3297 spin_unlock(&inode->i_lock);
3298}
3299
3300static void nfs4_zap_acl_attr(struct inode *inode)
3301{
3302 nfs4_set_cached_acl(inode, NULL);
3303}
3304
3305static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3306{
3307 struct nfs_inode *nfsi = NFS_I(inode);
3308 struct nfs4_cached_acl *acl;
3309 int ret = -ENOENT;
3310
3311 spin_lock(&inode->i_lock);
3312 acl = nfsi->nfs4_acl;
3313 if (acl == NULL)
3314 goto out;
3315 if (buf == NULL) /* user is just asking for length */
3316 goto out_len;
3317 if (acl->cached == 0)
3318 goto out;
3319 ret = -ERANGE; /* see getxattr(2) man page */
3320 if (acl->len > buflen)
3321 goto out;
3322 memcpy(buf, acl->data, acl->len);
3323out_len:
3324 ret = acl->len;
3325out:
3326 spin_unlock(&inode->i_lock);
3327 return ret;
3328}
3329
3330static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3331{
3332 struct nfs4_cached_acl *acl;
3333
3334 if (buf && acl_len <= PAGE_SIZE) {
3335 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3336 if (acl == NULL)
3337 goto out;
3338 acl->cached = 1;
3339 memcpy(acl->data, buf, acl_len);
3340 } else {
3341 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3342 if (acl == NULL)
3343 goto out;
3344 acl->cached = 0;
3345 }
3346 acl->len = acl_len;
3347out:
3348 nfs4_set_cached_acl(inode, acl);
3349}
3350
Trond Myklebust16b42892006-08-24 12:27:15 -04003351static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003352{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003353 struct page *pages[NFS4ACL_MAXPAGES];
3354 struct nfs_getaclargs args = {
3355 .fh = NFS_FH(inode),
3356 .acl_pages = pages,
3357 .acl_len = buflen,
3358 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003359 struct nfs_getaclres res = {
3360 .acl_len = buflen,
3361 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003362 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003363 struct rpc_message msg = {
3364 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3365 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003366 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003367 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003368 struct page *localpage = NULL;
3369 int ret;
3370
3371 if (buflen < PAGE_SIZE) {
3372 /* As long as we're doing a round trip to the server anyway,
3373 * let's be prepared for a page of acl data. */
3374 localpage = alloc_page(GFP_KERNEL);
3375 resp_buf = page_address(localpage);
3376 if (localpage == NULL)
3377 return -ENOMEM;
3378 args.acl_pages[0] = localpage;
3379 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003380 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003381 } else {
3382 resp_buf = buf;
3383 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3384 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003385 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003386 if (ret)
3387 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003388 if (res.acl_len > args.acl_len)
3389 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003390 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003391 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003392 if (buf) {
3393 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003394 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003395 goto out_free;
3396 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003397 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003398 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003399 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003400out_free:
3401 if (localpage)
3402 __free_page(localpage);
3403 return ret;
3404}
3405
Trond Myklebust16b42892006-08-24 12:27:15 -04003406static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3407{
3408 struct nfs4_exception exception = { };
3409 ssize_t ret;
3410 do {
3411 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3412 if (ret >= 0)
3413 break;
3414 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3415 } while (exception.retry);
3416 return ret;
3417}
3418
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003419static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3420{
3421 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003422 int ret;
3423
3424 if (!nfs4_server_supports_acls(server))
3425 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003426 ret = nfs_revalidate_inode(server, inode);
3427 if (ret < 0)
3428 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003429 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3430 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003431 ret = nfs4_read_cached_acl(inode, buf, buflen);
3432 if (ret != -ENOENT)
3433 return ret;
3434 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003435}
3436
Trond Myklebust16b42892006-08-24 12:27:15 -04003437static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003438{
3439 struct nfs_server *server = NFS_SERVER(inode);
3440 struct page *pages[NFS4ACL_MAXPAGES];
3441 struct nfs_setaclargs arg = {
3442 .fh = NFS_FH(inode),
3443 .acl_pages = pages,
3444 .acl_len = buflen,
3445 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003446 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003447 struct rpc_message msg = {
3448 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3449 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003450 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003451 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003452 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003453
3454 if (!nfs4_server_supports_acls(server))
3455 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003456 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3457 if (i < 0)
3458 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003459 nfs_inode_return_delegation(inode);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003460 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003461
3462 /*
3463 * Free each page after tx, so the only ref left is
3464 * held by the network stack
3465 */
3466 for (; i > 0; i--)
3467 put_page(pages[i-1]);
3468
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003469 /*
3470 * Acl update can result in inode attribute update.
3471 * so mark the attribute cache invalid.
3472 */
3473 spin_lock(&inode->i_lock);
3474 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3475 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003476 nfs_access_zap_cache(inode);
3477 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003478 return ret;
3479}
3480
Trond Myklebust16b42892006-08-24 12:27:15 -04003481static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3482{
3483 struct nfs4_exception exception = { };
3484 int err;
3485 do {
3486 err = nfs4_handle_exception(NFS_SERVER(inode),
3487 __nfs4_proc_set_acl(inode, buf, buflen),
3488 &exception);
3489 } while (exception.retry);
3490 return err;
3491}
3492
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003494nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003496 struct nfs_client *clp = server->nfs_client;
3497
3498 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 return 0;
3500 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003501 case -NFS4ERR_ADMIN_REVOKED:
3502 case -NFS4ERR_BAD_STATEID:
3503 case -NFS4ERR_OPENMODE:
3504 if (state == NULL)
3505 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003506 nfs4_schedule_stateid_recovery(server, state);
3507 goto wait_on_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003509 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003511 nfs4_schedule_lease_recovery(clp);
3512 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003513#if defined(CONFIG_NFS_V4_1)
3514 case -NFS4ERR_BADSESSION:
3515 case -NFS4ERR_BADSLOT:
3516 case -NFS4ERR_BAD_HIGH_SLOT:
3517 case -NFS4ERR_DEADSESSION:
3518 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3519 case -NFS4ERR_SEQ_FALSE_RETRY:
3520 case -NFS4ERR_SEQ_MISORDERED:
3521 dprintk("%s ERROR %d, Reset session\n", __func__,
3522 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003523 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003524 task->tk_status = 0;
3525 return -EAGAIN;
3526#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003528 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003529 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003530 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3532 task->tk_status = 0;
3533 return -EAGAIN;
3534 case -NFS4ERR_OLD_STATEID:
3535 task->tk_status = 0;
3536 return -EAGAIN;
3537 }
3538 task->tk_status = nfs4_map_errors(task->tk_status);
3539 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003540wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003541 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003542 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3543 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3544 task->tk_status = 0;
3545 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546}
3547
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003548int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3549 unsigned short port, struct rpc_cred *cred,
3550 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551{
3552 nfs4_verifier sc_verifier;
3553 struct nfs4_setclientid setclientid = {
3554 .sc_verifier = &sc_verifier,
3555 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003556 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 };
3558 struct rpc_message msg = {
3559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3560 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003561 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003562 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 };
Al Virobc4785c2006-10-19 23:28:51 -07003564 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 int loop = 0;
3566 int status;
3567
Al Virobc4785c2006-10-19 23:28:51 -07003568 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3570 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3571
3572 for(;;) {
3573 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003574 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003575 clp->cl_ipaddr,
3576 rpc_peeraddr2str(clp->cl_rpcclient,
3577 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003578 rpc_peeraddr2str(clp->cl_rpcclient,
3579 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003580 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 clp->cl_id_uniquifier);
3582 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003583 sizeof(setclientid.sc_netid),
3584 rpc_peeraddr2str(clp->cl_rpcclient,
3585 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003587 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 clp->cl_ipaddr, port >> 8, port & 255);
3589
3590 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3591 if (status != -NFS4ERR_CLID_INUSE)
3592 break;
3593 if (signalled())
3594 break;
3595 if (loop++ & 1)
J. Bruce Fields611c96c2010-12-13 19:05:46 -05003596 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 else
3598 if (++clp->cl_id_uniquifier == 0)
3599 break;
3600 }
3601 return status;
3602}
3603
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003604static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3605 struct nfs4_setclientid_res *arg,
3606 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607{
3608 struct nfs_fsinfo fsinfo;
3609 struct rpc_message msg = {
3610 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003611 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003613 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 };
3615 unsigned long now;
3616 int status;
3617
3618 now = jiffies;
3619 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3620 if (status == 0) {
3621 spin_lock(&clp->cl_lock);
3622 clp->cl_lease_time = fsinfo.lease_time * HZ;
3623 clp->cl_last_renewal = now;
3624 spin_unlock(&clp->cl_lock);
3625 }
3626 return status;
3627}
3628
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003629int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3630 struct nfs4_setclientid_res *arg,
3631 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003632{
Benny Halevy77e03672008-06-24 20:25:57 +03003633 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003634 int err;
3635 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003636 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003637 switch (err) {
3638 case 0:
3639 return err;
3640 case -NFS4ERR_RESOURCE:
3641 /* The IBM lawyers misread another document! */
3642 case -NFS4ERR_DELAY:
3643 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3644 }
3645 } while (err == 0);
3646 return err;
3647}
3648
Trond Myklebustfe650402006-01-03 09:55:18 +01003649struct nfs4_delegreturndata {
3650 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003651 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003652 struct nfs_fh fh;
3653 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003654 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003655 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003656 int rpc_status;
3657};
3658
Trond Myklebustfe650402006-01-03 09:55:18 +01003659static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3660{
3661 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003662
Trond Myklebust14516c32010-07-31 14:29:06 -04003663 if (!nfs4_sequence_done(task, &data->res.seq_res))
3664 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003665
Ricardo Labiaga79708862009-12-07 09:23:21 -05003666 switch (task->tk_status) {
3667 case -NFS4ERR_STALE_STATEID:
3668 case -NFS4ERR_EXPIRED:
3669 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003670 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003671 break;
3672 default:
3673 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3674 -EAGAIN) {
3675 nfs_restart_rpc(task, data->res.server->nfs_client);
3676 return;
3677 }
3678 }
3679 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003680}
3681
3682static void nfs4_delegreturn_release(void *calldata)
3683{
Trond Myklebustfe650402006-01-03 09:55:18 +01003684 kfree(calldata);
3685}
3686
Andy Adamson938e1012009-04-01 09:22:28 -04003687#if defined(CONFIG_NFS_V4_1)
3688static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3689{
3690 struct nfs4_delegreturndata *d_data;
3691
3692 d_data = (struct nfs4_delegreturndata *)data;
3693
Trond Myklebust035168a2010-06-16 09:52:26 -04003694 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003695 &d_data->args.seq_args,
3696 &d_data->res.seq_res, 1, task))
3697 return;
3698 rpc_call_start(task);
3699}
3700#endif /* CONFIG_NFS_V4_1 */
3701
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003702static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003703#if defined(CONFIG_NFS_V4_1)
3704 .rpc_call_prepare = nfs4_delegreturn_prepare,
3705#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003706 .rpc_call_done = nfs4_delegreturn_done,
3707 .rpc_release = nfs4_delegreturn_release,
3708};
3709
Trond Myklebuste6f81072008-01-24 18:14:34 -05003710static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003711{
3712 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003713 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003714 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003715 struct rpc_message msg = {
3716 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3717 .rpc_cred = cred,
3718 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003719 struct rpc_task_setup task_setup_data = {
3720 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003721 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003722 .callback_ops = &nfs4_delegreturn_ops,
3723 .flags = RPC_TASK_ASYNC,
3724 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003725 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003726
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003727 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003728 if (data == NULL)
3729 return -ENOMEM;
3730 data->args.fhandle = &data->fh;
3731 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003732 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003733 nfs_copy_fh(&data->fh, NFS_FH(inode));
3734 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003735 data->res.fattr = &data->fattr;
3736 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003737 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003738 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003739 data->rpc_status = 0;
3740
Trond Myklebustc970aa82007-07-14 15:39:59 -04003741 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003742 msg.rpc_argp = &data->args;
3743 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003744 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003745 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003746 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003747 if (!issync)
3748 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003749 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003750 if (status != 0)
3751 goto out;
3752 status = data->rpc_status;
3753 if (status != 0)
3754 goto out;
3755 nfs_refresh_inode(inode, &data->fattr);
3756out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003757 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003758 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759}
3760
Trond Myklebuste6f81072008-01-24 18:14:34 -05003761int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762{
3763 struct nfs_server *server = NFS_SERVER(inode);
3764 struct nfs4_exception exception = { };
3765 int err;
3766 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003767 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 switch (err) {
3769 case -NFS4ERR_STALE_STATEID:
3770 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 case 0:
3772 return 0;
3773 }
3774 err = nfs4_handle_exception(server, err, &exception);
3775 } while (exception.retry);
3776 return err;
3777}
3778
3779#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3780#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3781
3782/*
3783 * sleep, with exponential backoff, and retry the LOCK operation.
3784 */
3785static unsigned long
3786nfs4_set_lock_task_retry(unsigned long timeout)
3787{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003788 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 timeout <<= 1;
3790 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3791 return NFS4_LOCK_MAXTIMEOUT;
3792 return timeout;
3793}
3794
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3796{
3797 struct inode *inode = state->inode;
3798 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003799 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003800 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003802 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003804 struct nfs_lockt_res res = {
3805 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 };
3807 struct rpc_message msg = {
3808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3809 .rpc_argp = &arg,
3810 .rpc_resp = &res,
3811 .rpc_cred = state->owner->so_cred,
3812 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 struct nfs4_lock_state *lsp;
3814 int status;
3815
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003816 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003817 status = nfs4_set_lock_state(state, request);
3818 if (status != 0)
3819 goto out;
3820 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003821 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05003822 arg.lock_owner.s_dev = server->s_dev;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003823 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003824 switch (status) {
3825 case 0:
3826 request->fl_type = F_UNLCK;
3827 break;
3828 case -NFS4ERR_DENIED:
3829 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003831 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003832out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 return status;
3834}
3835
3836static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3837{
3838 struct nfs4_exception exception = { };
3839 int err;
3840
3841 do {
3842 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3843 _nfs4_proc_getlk(state, cmd, request),
3844 &exception);
3845 } while (exception.retry);
3846 return err;
3847}
3848
3849static int do_vfs_lock(struct file *file, struct file_lock *fl)
3850{
3851 int res = 0;
3852 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3853 case FL_POSIX:
3854 res = posix_lock_file_wait(file, fl);
3855 break;
3856 case FL_FLOCK:
3857 res = flock_lock_file_wait(file, fl);
3858 break;
3859 default:
3860 BUG();
3861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 return res;
3863}
3864
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003865struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003866 struct nfs_locku_args arg;
3867 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003868 struct nfs4_lock_state *lsp;
3869 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003870 struct file_lock fl;
3871 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003872 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003873};
3874
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003875static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3876 struct nfs_open_context *ctx,
3877 struct nfs4_lock_state *lsp,
3878 struct nfs_seqid *seqid)
3879{
3880 struct nfs4_unlockdata *p;
3881 struct inode *inode = lsp->ls_state->inode;
3882
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003883 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003884 if (p == NULL)
3885 return NULL;
3886 p->arg.fh = NFS_FH(inode);
3887 p->arg.fl = &p->fl;
3888 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003889 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003890 p->arg.stateid = &lsp->ls_stateid;
3891 p->lsp = lsp;
3892 atomic_inc(&lsp->ls_count);
3893 /* Ensure we don't close file until we're done freeing locks! */
3894 p->ctx = get_nfs_open_context(ctx);
3895 memcpy(&p->fl, fl, sizeof(p->fl));
3896 p->server = NFS_SERVER(inode);
3897 return p;
3898}
3899
Trond Myklebust06f814a2006-01-03 09:55:07 +01003900static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003901{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003902 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003903 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003904 nfs4_put_lock_state(calldata->lsp);
3905 put_nfs_open_context(calldata->ctx);
3906 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003907}
3908
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003909static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003910{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003911 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003912
Trond Myklebust14516c32010-07-31 14:29:06 -04003913 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3914 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003915 switch (task->tk_status) {
3916 case 0:
3917 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003918 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003919 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003920 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003921 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003922 case -NFS4ERR_BAD_STATEID:
3923 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003924 case -NFS4ERR_STALE_STATEID:
3925 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003926 break;
3927 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003928 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003929 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003930 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003931 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003932}
3933
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003934static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003935{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003936 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003938 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003939 return;
3940 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003941 /* Note: exit _without_ running nfs4_locku_done */
3942 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003943 return;
3944 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003945 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04003946 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04003947 &calldata->arg.seq_args,
3948 &calldata->res.seq_res, 1, task))
3949 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003950 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951}
3952
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003953static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003954 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003955 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003956 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003957};
3958
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003959static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3960 struct nfs_open_context *ctx,
3961 struct nfs4_lock_state *lsp,
3962 struct nfs_seqid *seqid)
3963{
3964 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003965 struct rpc_message msg = {
3966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3967 .rpc_cred = ctx->cred,
3968 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003969 struct rpc_task_setup task_setup_data = {
3970 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003971 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003972 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003973 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003974 .flags = RPC_TASK_ASYNC,
3975 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003976
Frank Filz137d6ac2007-07-09 15:32:29 -07003977 /* Ensure this is an unlock - when canceling a lock, the
3978 * canceled lock is passed in, and it won't be an unlock.
3979 */
3980 fl->fl_type = F_UNLCK;
3981
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003982 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3983 if (data == NULL) {
3984 nfs_free_seqid(seqid);
3985 return ERR_PTR(-ENOMEM);
3986 }
3987
Trond Myklebust1174dd12010-12-21 10:52:24 -05003988 msg.rpc_argp = &data->arg;
3989 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003990 task_setup_data.callback_data = data;
3991 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003992}
3993
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3995{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003996 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003997 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003998 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003999 struct rpc_task *task;
4000 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004001 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002
Trond Myklebust9b073572006-06-29 16:38:34 -04004003 status = nfs4_set_lock_state(state, request);
4004 /* Unlock _before_ we do the RPC call */
4005 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004006 down_read(&nfsi->rwsem);
4007 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4008 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004009 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004010 }
4011 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004012 if (status != 0)
4013 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004014 /* Is this a delegated lock? */
4015 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004016 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004017 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004018 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004019 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004020 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004021 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004022 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004023 status = PTR_ERR(task);
4024 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004025 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004026 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004027 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004028out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004029 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004030 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031}
4032
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004033struct nfs4_lockdata {
4034 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004035 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004036 struct nfs4_lock_state *lsp;
4037 struct nfs_open_context *ctx;
4038 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004039 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004040 int rpc_status;
4041 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004042 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004043};
4044
4045static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004046 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4047 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004048{
4049 struct nfs4_lockdata *p;
4050 struct inode *inode = lsp->ls_state->inode;
4051 struct nfs_server *server = NFS_SERVER(inode);
4052
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004053 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004054 if (p == NULL)
4055 return NULL;
4056
4057 p->arg.fh = NFS_FH(inode);
4058 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004059 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004060 if (p->arg.open_seqid == NULL)
4061 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004062 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004063 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004064 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004065 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004066 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004067 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004068 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004069 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004070 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004071 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004072 atomic_inc(&lsp->ls_count);
4073 p->ctx = get_nfs_open_context(ctx);
4074 memcpy(&p->fl, fl, sizeof(p->fl));
4075 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004076out_free_seqid:
4077 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004078out_free:
4079 kfree(p);
4080 return NULL;
4081}
4082
4083static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4084{
4085 struct nfs4_lockdata *data = calldata;
4086 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087
Harvey Harrison3110ff82008-05-02 13:42:44 -07004088 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004089 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4090 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004091 /* Do we need to do an open_to_lock_owner? */
4092 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004093 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4094 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004095 data->arg.open_stateid = &state->stateid;
4096 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004097 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004098 } else
4099 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004100 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004101 if (nfs4_setup_sequence(data->server,
4102 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004103 &data->res.seq_res, 1, task))
4104 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004105 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004106 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004107}
4108
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004109static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4110{
4111 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4112 nfs4_lock_prepare(task, calldata);
4113}
4114
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004115static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4116{
4117 struct nfs4_lockdata *data = calldata;
4118
Harvey Harrison3110ff82008-05-02 13:42:44 -07004119 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004120
Trond Myklebust14516c32010-07-31 14:29:06 -04004121 if (!nfs4_sequence_done(task, &data->res.seq_res))
4122 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004123
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004124 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004125 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004126 if (data->rpc_status == 0)
4127 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4128 else
4129 goto out;
4130 }
4131 if (data->rpc_status == 0) {
4132 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4133 sizeof(data->lsp->ls_stateid.data));
4134 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004135 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004136 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004137out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004138 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004139}
4140
4141static void nfs4_lock_release(void *calldata)
4142{
4143 struct nfs4_lockdata *data = calldata;
4144
Harvey Harrison3110ff82008-05-02 13:42:44 -07004145 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004146 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004147 if (data->cancelled != 0) {
4148 struct rpc_task *task;
4149 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4150 data->arg.lock_seqid);
4151 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004152 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004153 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004154 } else
4155 nfs_free_seqid(data->arg.lock_seqid);
4156 nfs4_put_lock_state(data->lsp);
4157 put_nfs_open_context(data->ctx);
4158 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004159 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004160}
4161
4162static const struct rpc_call_ops nfs4_lock_ops = {
4163 .rpc_call_prepare = nfs4_lock_prepare,
4164 .rpc_call_done = nfs4_lock_done,
4165 .rpc_release = nfs4_lock_release,
4166};
4167
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004168static const struct rpc_call_ops nfs4_recover_lock_ops = {
4169 .rpc_call_prepare = nfs4_recover_lock_prepare,
4170 .rpc_call_done = nfs4_lock_done,
4171 .rpc_release = nfs4_lock_release,
4172};
4173
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004174static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4175{
4176 struct nfs_client *clp = server->nfs_client;
4177 struct nfs4_state *state = lsp->ls_state;
4178
4179 switch (error) {
4180 case -NFS4ERR_ADMIN_REVOKED:
4181 case -NFS4ERR_BAD_STATEID:
4182 case -NFS4ERR_EXPIRED:
4183 if (new_lock_owner != 0 ||
4184 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4185 nfs4_state_mark_reclaim_nograce(clp, state);
4186 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004187 break;
4188 case -NFS4ERR_STALE_STATEID:
4189 if (new_lock_owner != 0 ||
4190 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4191 nfs4_state_mark_reclaim_reboot(clp, state);
4192 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004193 };
4194}
4195
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004196static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004197{
4198 struct nfs4_lockdata *data;
4199 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004200 struct rpc_message msg = {
4201 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4202 .rpc_cred = state->owner->so_cred,
4203 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004204 struct rpc_task_setup task_setup_data = {
4205 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004206 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004207 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004208 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004209 .flags = RPC_TASK_ASYNC,
4210 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004211 int ret;
4212
Harvey Harrison3110ff82008-05-02 13:42:44 -07004213 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004214 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004215 fl->fl_u.nfs4_fl.owner,
4216 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004217 if (data == NULL)
4218 return -ENOMEM;
4219 if (IS_SETLKW(cmd))
4220 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004221 if (recovery_type > NFS_LOCK_NEW) {
4222 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004223 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004224 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4225 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004226 msg.rpc_argp = &data->arg;
4227 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004228 task_setup_data.callback_data = data;
4229 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004230 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004231 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004232 ret = nfs4_wait_for_completion_rpc_task(task);
4233 if (ret == 0) {
4234 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004235 if (ret)
4236 nfs4_handle_setlk_error(data->server, data->lsp,
4237 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004238 } else
4239 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004240 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004241 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004242 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243}
4244
4245static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4246{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004247 struct nfs_server *server = NFS_SERVER(state->inode);
4248 struct nfs4_exception exception = { };
4249 int err;
4250
4251 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004252 /* Cache the lock if possible... */
4253 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4254 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004255 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004256 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004257 break;
4258 nfs4_handle_exception(server, err, &exception);
4259 } while (exception.retry);
4260 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261}
4262
4263static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4264{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004265 struct nfs_server *server = NFS_SERVER(state->inode);
4266 struct nfs4_exception exception = { };
4267 int err;
4268
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004269 err = nfs4_set_lock_state(state, request);
4270 if (err != 0)
4271 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004272 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004273 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4274 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004275 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004276 switch (err) {
4277 default:
4278 goto out;
4279 case -NFS4ERR_GRACE:
4280 case -NFS4ERR_DELAY:
4281 nfs4_handle_exception(server, err, &exception);
4282 err = 0;
4283 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004284 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004285out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004286 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287}
4288
4289static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4290{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004291 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004292 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004293 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004295 if ((fl_flags & FL_POSIX) &&
4296 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4297 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004298 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004299 status = nfs4_set_lock_state(state, request);
4300 if (status != 0)
4301 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004302 request->fl_flags |= FL_ACCESS;
4303 status = do_vfs_lock(request->fl_file, request);
4304 if (status < 0)
4305 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004306 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004307 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004308 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004309 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004310 request->fl_flags = fl_flags & ~FL_SLEEP;
4311 status = do_vfs_lock(request->fl_file, request);
4312 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004313 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004314 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004315 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004316 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004317 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004318 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004319 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004320 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004321out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004322 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004323out:
4324 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 return status;
4326}
4327
4328static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4329{
4330 struct nfs4_exception exception = { };
4331 int err;
4332
4333 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004334 err = _nfs4_proc_setlk(state, cmd, request);
4335 if (err == -NFS4ERR_DENIED)
4336 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004338 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 } while (exception.retry);
4340 return err;
4341}
4342
4343static int
4344nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4345{
4346 struct nfs_open_context *ctx;
4347 struct nfs4_state *state;
4348 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4349 int status;
4350
4351 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004352 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 state = ctx->state;
4354
4355 if (request->fl_start < 0 || request->fl_end < 0)
4356 return -EINVAL;
4357
Trond Myklebustd9531262009-07-21 19:22:38 -04004358 if (IS_GETLK(cmd)) {
4359 if (state != NULL)
4360 return nfs4_proc_getlk(state, F_GETLK, request);
4361 return 0;
4362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
4364 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4365 return -EINVAL;
4366
Trond Myklebustd9531262009-07-21 19:22:38 -04004367 if (request->fl_type == F_UNLCK) {
4368 if (state != NULL)
4369 return nfs4_proc_unlck(state, cmd, request);
4370 return 0;
4371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
Trond Myklebustd9531262009-07-21 19:22:38 -04004373 if (state == NULL)
4374 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 do {
4376 status = nfs4_proc_setlk(state, cmd, request);
4377 if ((status != -EAGAIN) || IS_SETLK(cmd))
4378 break;
4379 timeout = nfs4_set_lock_task_retry(timeout);
4380 status = -ERESTARTSYS;
4381 if (signalled())
4382 break;
4383 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 return status;
4385}
4386
Trond Myklebust888e6942005-11-04 15:38:11 -05004387int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4388{
4389 struct nfs_server *server = NFS_SERVER(state->inode);
4390 struct nfs4_exception exception = { };
4391 int err;
4392
4393 err = nfs4_set_lock_state(state, fl);
4394 if (err != 0)
4395 goto out;
4396 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004397 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004398 switch (err) {
4399 default:
4400 printk(KERN_ERR "%s: unhandled error %d.\n",
4401 __func__, err);
4402 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004403 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004404 goto out;
4405 case -NFS4ERR_EXPIRED:
4406 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004407 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004408 nfs4_schedule_lease_recovery(server->nfs_client);
4409 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004410 case -NFS4ERR_BADSESSION:
4411 case -NFS4ERR_BADSLOT:
4412 case -NFS4ERR_BAD_HIGH_SLOT:
4413 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4414 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004415 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004416 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004417 case -ERESTARTSYS:
4418 /*
4419 * The show must go on: exit, but mark the
4420 * stateid as needing recovery.
4421 */
4422 case -NFS4ERR_ADMIN_REVOKED:
4423 case -NFS4ERR_BAD_STATEID:
4424 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004425 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004426 err = 0;
4427 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004428 case -EKEYEXPIRED:
4429 /*
4430 * User RPCSEC_GSS context has expired.
4431 * We cannot recover this stateid now, so
4432 * skip it and allow recovery thread to
4433 * proceed.
4434 */
4435 err = 0;
4436 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004437 case -ENOMEM:
4438 case -NFS4ERR_DENIED:
4439 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4440 err = 0;
4441 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004442 case -NFS4ERR_DELAY:
4443 break;
4444 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004445 err = nfs4_handle_exception(server, err, &exception);
4446 } while (exception.retry);
4447out:
4448 return err;
4449}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004450
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004451static void nfs4_release_lockowner_release(void *calldata)
4452{
4453 kfree(calldata);
4454}
4455
4456const struct rpc_call_ops nfs4_release_lockowner_ops = {
4457 .rpc_release = nfs4_release_lockowner_release,
4458};
4459
4460void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4461{
4462 struct nfs_server *server = lsp->ls_state->owner->so_server;
4463 struct nfs_release_lockowner_args *args;
4464 struct rpc_message msg = {
4465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4466 };
4467
4468 if (server->nfs_client->cl_mvops->minor_version != 0)
4469 return;
4470 args = kmalloc(sizeof(*args), GFP_NOFS);
4471 if (!args)
4472 return;
4473 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4474 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004475 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004476 msg.rpc_argp = args;
4477 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4478}
4479
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004480#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4481
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004482static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4483 const void *buf, size_t buflen,
4484 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004485{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004486 if (strcmp(key, "") != 0)
4487 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004488
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004489 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004490}
4491
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004492static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4493 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004494{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004495 if (strcmp(key, "") != 0)
4496 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004497
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004498 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004499}
4500
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004501static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4502 size_t list_len, const char *name,
4503 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004504{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004505 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004506
J. Bruce Fields096455a2006-03-20 23:23:42 -05004507 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4508 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004509
4510 if (list && len <= list_len)
4511 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004512 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004513}
4514
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004515static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4516{
4517 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4518 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4519 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4520 return;
4521
4522 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4523 NFS_ATTR_FATTR_NLINK;
4524 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4525 fattr->nlink = 2;
4526}
4527
Trond Myklebust56659e92007-07-17 21:52:39 -04004528int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004529 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004530{
4531 struct nfs_server *server = NFS_SERVER(dir);
4532 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004533 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4534 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004535 };
4536 struct nfs4_fs_locations_arg args = {
4537 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004538 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004539 .page = page,
4540 .bitmask = bitmask,
4541 };
Benny Halevy22958462009-04-01 09:22:02 -04004542 struct nfs4_fs_locations_res res = {
4543 .fs_locations = fs_locations,
4544 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004545 struct rpc_message msg = {
4546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4547 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004548 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004549 };
4550 int status;
4551
Harvey Harrison3110ff82008-05-02 13:42:44 -07004552 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004553 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004554 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004555 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004556 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004557 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004558 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004559 return status;
4560}
4561
Andy Adamson557134a2009-04-01 09:21:53 -04004562#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004563/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004564 * Check the exchange flags returned by the server for invalid flags, having
4565 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4566 * DS flags set.
4567 */
4568static int nfs4_check_cl_exchange_flags(u32 flags)
4569{
4570 if (flags & ~EXCHGID4_FLAG_MASK_R)
4571 goto out_inval;
4572 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4573 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4574 goto out_inval;
4575 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4576 goto out_inval;
4577 return NFS_OK;
4578out_inval:
4579 return -NFS4ERR_INVAL;
4580}
4581
4582/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004583 * nfs4_proc_exchange_id()
4584 *
4585 * Since the clientid has expired, all compounds using sessions
4586 * associated with the stale clientid will be returning
4587 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4588 * be in some phase of session reset.
4589 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004590int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004591{
4592 nfs4_verifier verifier;
4593 struct nfs41_exchange_id_args args = {
4594 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004595 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004596 };
4597 struct nfs41_exchange_id_res res = {
4598 .client = clp,
4599 };
4600 int status;
4601 struct rpc_message msg = {
4602 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4603 .rpc_argp = &args,
4604 .rpc_resp = &res,
4605 .rpc_cred = cred,
4606 };
4607 __be32 *p;
4608
4609 dprintk("--> %s\n", __func__);
4610 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004611
Benny Halevy99fe60d2009-04-01 09:22:29 -04004612 p = (u32 *)verifier.data;
4613 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4614 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4615 args.verifier = &verifier;
4616
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004617 args.id_len = scnprintf(args.id, sizeof(args.id),
4618 "%s/%s.%s/%u",
4619 clp->cl_ipaddr,
4620 init_utsname()->nodename,
4621 init_utsname()->domainname,
4622 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004623
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004624 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4625 if (!status)
4626 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004627 dprintk("<-- %s status= %d\n", __func__, status);
4628 return status;
4629}
4630
Andy Adamson2050f0c2009-04-01 09:22:30 -04004631struct nfs4_get_lease_time_data {
4632 struct nfs4_get_lease_time_args *args;
4633 struct nfs4_get_lease_time_res *res;
4634 struct nfs_client *clp;
4635};
4636
4637static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4638 void *calldata)
4639{
4640 int ret;
4641 struct nfs4_get_lease_time_data *data =
4642 (struct nfs4_get_lease_time_data *)calldata;
4643
4644 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004645 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004646 /* just setup sequence, do not trigger session recovery
4647 since we're invoked within one */
4648 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004649 &data->args->la_seq_args,
4650 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004651
4652 BUG_ON(ret == -EAGAIN);
4653 rpc_call_start(task);
4654 dprintk("<-- %s\n", __func__);
4655}
4656
4657/*
4658 * Called from nfs4_state_manager thread for session setup, so don't recover
4659 * from sequence operation or clientid errors.
4660 */
4661static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4662{
4663 struct nfs4_get_lease_time_data *data =
4664 (struct nfs4_get_lease_time_data *)calldata;
4665
4666 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004667 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4668 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004669 switch (task->tk_status) {
4670 case -NFS4ERR_DELAY:
4671 case -NFS4ERR_GRACE:
4672 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4673 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4674 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004675 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004676 return;
4677 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004678 dprintk("<-- %s\n", __func__);
4679}
4680
4681struct rpc_call_ops nfs4_get_lease_time_ops = {
4682 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4683 .rpc_call_done = nfs4_get_lease_time_done,
4684};
4685
4686int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4687{
4688 struct rpc_task *task;
4689 struct nfs4_get_lease_time_args args;
4690 struct nfs4_get_lease_time_res res = {
4691 .lr_fsinfo = fsinfo,
4692 };
4693 struct nfs4_get_lease_time_data data = {
4694 .args = &args,
4695 .res = &res,
4696 .clp = clp,
4697 };
4698 struct rpc_message msg = {
4699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4700 .rpc_argp = &args,
4701 .rpc_resp = &res,
4702 };
4703 struct rpc_task_setup task_setup = {
4704 .rpc_client = clp->cl_rpcclient,
4705 .rpc_message = &msg,
4706 .callback_ops = &nfs4_get_lease_time_ops,
4707 .callback_data = &data
4708 };
4709 int status;
4710
Andy Adamson2050f0c2009-04-01 09:22:30 -04004711 dprintk("--> %s\n", __func__);
4712 task = rpc_run_task(&task_setup);
4713
4714 if (IS_ERR(task))
4715 status = PTR_ERR(task);
4716 else {
4717 status = task->tk_status;
4718 rpc_put_task(task);
4719 }
4720 dprintk("<-- %s return %d\n", __func__, status);
4721
4722 return status;
4723}
4724
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004725/*
4726 * Reset a slot table
4727 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004728static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4729 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004730{
Andy Adamson104aeba2010-01-14 17:45:10 -05004731 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004732 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004733 int ret = 0;
4734
Andy Adamson104aeba2010-01-14 17:45:10 -05004735 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4736 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004737
Andy Adamson104aeba2010-01-14 17:45:10 -05004738 /* Does the newly negotiated max_reqs match the existing slot table? */
4739 if (max_reqs != tbl->max_slots) {
4740 ret = -ENOMEM;
4741 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004742 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004743 if (!new)
4744 goto out;
4745 ret = 0;
4746 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004747 }
4748 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004749 if (new) {
4750 tbl->slots = new;
4751 tbl->max_slots = max_reqs;
4752 }
4753 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004754 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004755 spin_unlock(&tbl->slot_tbl_lock);
4756 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4757 tbl, tbl->slots, tbl->max_slots);
4758out:
4759 dprintk("<-- %s: return %d\n", __func__, ret);
4760 return ret;
4761}
4762
Andy Adamsonfc931582009-04-01 09:22:31 -04004763/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004764 * Reset the forechannel and backchannel slot tables
4765 */
4766static int nfs4_reset_slot_tables(struct nfs4_session *session)
4767{
4768 int status;
4769
4770 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004771 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004772 if (status)
4773 return status;
4774
4775 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004776 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004777 return status;
4778}
4779
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004780/* Destroy the slot table */
4781static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4782{
4783 if (session->fc_slot_table.slots != NULL) {
4784 kfree(session->fc_slot_table.slots);
4785 session->fc_slot_table.slots = NULL;
4786 }
4787 if (session->bc_slot_table.slots != NULL) {
4788 kfree(session->bc_slot_table.slots);
4789 session->bc_slot_table.slots = NULL;
4790 }
4791 return;
4792}
4793
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004794/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004795 * Initialize slot table
4796 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004797static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4798 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004799{
Andy Adamsonfc931582009-04-01 09:22:31 -04004800 struct nfs4_slot *slot;
4801 int ret = -ENOMEM;
4802
4803 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4804
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004805 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004806
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004807 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004808 if (!slot)
4809 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004810 ret = 0;
4811
4812 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004813 tbl->max_slots = max_slots;
4814 tbl->slots = slot;
4815 tbl->highest_used_slotid = -1; /* no slot is currently used */
4816 spin_unlock(&tbl->slot_tbl_lock);
4817 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4818 tbl, tbl->slots, tbl->max_slots);
4819out:
4820 dprintk("<-- %s: return %d\n", __func__, ret);
4821 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004822}
4823
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004824/*
4825 * Initialize the forechannel and backchannel tables
4826 */
4827static int nfs4_init_slot_tables(struct nfs4_session *session)
4828{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004829 struct nfs4_slot_table *tbl;
4830 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004831
Trond Myklebustf26468f2009-12-05 19:32:11 -05004832 tbl = &session->fc_slot_table;
4833 if (tbl->slots == NULL) {
4834 status = nfs4_init_slot_table(tbl,
4835 session->fc_attrs.max_reqs, 1);
4836 if (status)
4837 return status;
4838 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004839
Trond Myklebustf26468f2009-12-05 19:32:11 -05004840 tbl = &session->bc_slot_table;
4841 if (tbl->slots == NULL) {
4842 status = nfs4_init_slot_table(tbl,
4843 session->bc_attrs.max_reqs, 0);
4844 if (status)
4845 nfs4_destroy_slot_tables(session);
4846 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004847
4848 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004849}
4850
4851struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4852{
4853 struct nfs4_session *session;
4854 struct nfs4_slot_table *tbl;
4855
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004856 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004857 if (!session)
4858 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004859
Andy Adamson557134a2009-04-01 09:21:53 -04004860 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004861 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004862 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004863 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00004864 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004865
4866 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004867 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004868 spin_lock_init(&tbl->slot_tbl_lock);
4869 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00004870 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004871
Trond Myklebust1055d762010-06-16 09:52:27 -04004872 session->session_state = 1<<NFS4_SESSION_INITING;
4873
Andy Adamson557134a2009-04-01 09:21:53 -04004874 session->clp = clp;
4875 return session;
4876}
4877
4878void nfs4_destroy_session(struct nfs4_session *session)
4879{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004880 nfs4_proc_destroy_session(session);
4881 dprintk("%s Destroy backchannel for xprt %p\n",
4882 __func__, session->clp->cl_rpcclient->cl_xprt);
4883 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4884 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004885 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004886 kfree(session);
4887}
4888
Andy Adamsonfc931582009-04-01 09:22:31 -04004889/*
4890 * Initialize the values to be used by the client in CREATE_SESSION
4891 * If nfs4_init_session set the fore channel request and response sizes,
4892 * use them.
4893 *
4894 * Set the back channel max_resp_sz_cached to zero to force the client to
4895 * always set csa_cachethis to FALSE because the current implementation
4896 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4897 */
4898static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4899{
4900 struct nfs4_session *session = args->client->cl_session;
4901 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4902 mxresp_sz = session->fc_attrs.max_resp_sz;
4903
4904 if (mxrqst_sz == 0)
4905 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4906 if (mxresp_sz == 0)
4907 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4908 /* Fore channel attributes */
4909 args->fc_attrs.headerpadsz = 0;
4910 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4911 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004912 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4913 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4914
4915 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004916 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004917 __func__,
4918 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004919 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004920
4921 /* Back channel attributes */
4922 args->bc_attrs.headerpadsz = 0;
4923 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4924 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4925 args->bc_attrs.max_resp_sz_cached = 0;
4926 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4927 args->bc_attrs.max_reqs = 1;
4928
4929 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4930 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4931 __func__,
4932 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4933 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4934 args->bc_attrs.max_reqs);
4935}
4936
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004937static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04004938{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004939 struct nfs4_channel_attrs *sent = &args->fc_attrs;
4940 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
4941
4942 if (rcvd->headerpadsz > sent->headerpadsz)
4943 return -EINVAL;
4944 if (rcvd->max_resp_sz > sent->max_resp_sz)
4945 return -EINVAL;
4946 /*
4947 * Our requested max_ops is the minimum we need; we're not
4948 * prepared to break up compounds into smaller pieces than that.
4949 * So, no point even trying to continue if the server won't
4950 * cooperate:
4951 */
4952 if (rcvd->max_ops < sent->max_ops)
4953 return -EINVAL;
4954 if (rcvd->max_reqs == 0)
4955 return -EINVAL;
4956 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04004957}
4958
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004959static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
4960{
4961 struct nfs4_channel_attrs *sent = &args->bc_attrs;
4962 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04004963
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004964 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
4965 return -EINVAL;
4966 if (rcvd->max_resp_sz < sent->max_resp_sz)
4967 return -EINVAL;
4968 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
4969 return -EINVAL;
4970 /* These would render the backchannel useless: */
4971 if (rcvd->max_ops == 0)
4972 return -EINVAL;
4973 if (rcvd->max_reqs == 0)
4974 return -EINVAL;
4975 return 0;
4976}
Andy Adamson8d353012009-04-01 09:22:32 -04004977
Andy Adamson8d353012009-04-01 09:22:32 -04004978static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4979 struct nfs4_session *session)
4980{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004981 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04004982
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004983 ret = nfs4_verify_fore_channel_attrs(args, session);
4984 if (ret)
4985 return ret;
4986 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04004987}
4988
Andy Adamsonfc931582009-04-01 09:22:31 -04004989static int _nfs4_proc_create_session(struct nfs_client *clp)
4990{
4991 struct nfs4_session *session = clp->cl_session;
4992 struct nfs41_create_session_args args = {
4993 .client = clp,
4994 .cb_program = NFS4_CALLBACK,
4995 };
4996 struct nfs41_create_session_res res = {
4997 .client = clp,
4998 };
4999 struct rpc_message msg = {
5000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5001 .rpc_argp = &args,
5002 .rpc_resp = &res,
5003 };
5004 int status;
5005
5006 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005007 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005008
5009 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5010
Andy Adamson8d353012009-04-01 09:22:32 -04005011 if (!status)
5012 /* Verify the session's negotiated channel_attrs values */
5013 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005014 if (!status) {
5015 /* Increment the clientid slot sequence id */
5016 clp->cl_seqid++;
5017 }
5018
5019 return status;
5020}
5021
5022/*
5023 * Issues a CREATE_SESSION operation to the server.
5024 * It is the responsibility of the caller to verify the session is
5025 * expired before calling this routine.
5026 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005027int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005028{
5029 int status;
5030 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005031 struct nfs4_session *session = clp->cl_session;
Ricardo Labiaga7d6d63d2011-03-09 13:13:44 -05005032 long timeout = 0;
5033 int err;
Andy Adamsonfc931582009-04-01 09:22:31 -04005034
5035 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5036
Ricardo Labiaga7d6d63d2011-03-09 13:13:44 -05005037 do {
5038 status = _nfs4_proc_create_session(clp);
5039 if (status == -NFS4ERR_DELAY) {
5040 err = nfs4_delay(clp->cl_rpcclient, &timeout);
5041 if (err)
5042 status = err;
5043 }
5044 } while (status == -NFS4ERR_DELAY);
5045
Andy Adamsonfc931582009-04-01 09:22:31 -04005046 if (status)
5047 goto out;
5048
Trond Myklebustf26468f2009-12-05 19:32:11 -05005049 /* Init and reset the fore channel */
5050 status = nfs4_init_slot_tables(session);
5051 dprintk("slot table initialization returned %d\n", status);
5052 if (status)
5053 goto out;
5054 status = nfs4_reset_slot_tables(session);
5055 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005056 if (status)
5057 goto out;
5058
5059 ptr = (unsigned *)&session->sess_id.data[0];
5060 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5061 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005062out:
5063 dprintk("<-- %s\n", __func__);
5064 return status;
5065}
5066
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005067/*
5068 * Issue the over-the-wire RPC DESTROY_SESSION.
5069 * The caller must serialize access to this routine.
5070 */
5071int nfs4_proc_destroy_session(struct nfs4_session *session)
5072{
5073 int status = 0;
5074 struct rpc_message msg;
5075
5076 dprintk("--> nfs4_proc_destroy_session\n");
5077
5078 /* session is still being setup */
5079 if (session->clp->cl_cons_state != NFS_CS_READY)
5080 return status;
5081
5082 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5083 msg.rpc_argp = session;
5084 msg.rpc_resp = NULL;
5085 msg.rpc_cred = NULL;
5086 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5087
5088 if (status)
5089 printk(KERN_WARNING
5090 "Got error %d from the server on DESTROY_SESSION. "
5091 "Session has been destroyed regardless...\n", status);
5092
5093 dprintk("<-- nfs4_proc_destroy_session\n");
5094 return status;
5095}
5096
Trond Myklebustfccba802009-07-21 16:48:07 -04005097int nfs4_init_session(struct nfs_server *server)
5098{
5099 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005100 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005101 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005102 int ret;
5103
5104 if (!nfs4_has_session(clp))
5105 return 0;
5106
Trond Myklebust1055d762010-06-16 09:52:27 -04005107 session = clp->cl_session;
5108 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5109 return 0;
5110
Andy Adamson68bf05e2009-12-15 12:55:02 -05005111 rsize = server->rsize;
5112 if (rsize == 0)
5113 rsize = NFS_MAX_FILE_IO_SIZE;
5114 wsize = server->wsize;
5115 if (wsize == 0)
5116 wsize = NFS_MAX_FILE_IO_SIZE;
5117
Andy Adamson68bf05e2009-12-15 12:55:02 -05005118 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5119 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005120
Trond Myklebustfccba802009-07-21 16:48:07 -04005121 ret = nfs4_recover_expired_lease(server);
5122 if (!ret)
5123 ret = nfs4_check_client_ready(clp);
5124 return ret;
5125}
5126
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005127/*
5128 * Renew the cl_session lease.
5129 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005130struct nfs4_sequence_data {
5131 struct nfs_client *clp;
5132 struct nfs4_sequence_args args;
5133 struct nfs4_sequence_res res;
5134};
5135
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005136static void nfs41_sequence_release(void *data)
5137{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005138 struct nfs4_sequence_data *calldata = data;
5139 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005140
Alexandros Batsakis71358402010-02-05 03:45:05 -08005141 if (atomic_read(&clp->cl_count) > 1)
5142 nfs4_schedule_state_renewal(clp);
5143 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005144 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005145}
5146
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005147static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5148{
5149 switch(task->tk_status) {
5150 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005151 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5152 return -EAGAIN;
5153 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005154 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005155 }
5156 return 0;
5157}
5158
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005159static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005160{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005161 struct nfs4_sequence_data *calldata = data;
5162 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005163
Trond Myklebust14516c32010-07-31 14:29:06 -04005164 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5165 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005166
5167 if (task->tk_status < 0) {
5168 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005169 if (atomic_read(&clp->cl_count) == 1)
5170 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005171
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005172 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5173 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005174 return;
5175 }
5176 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005177 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005178out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005179 dprintk("<-- %s\n", __func__);
5180}
5181
5182static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5183{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005184 struct nfs4_sequence_data *calldata = data;
5185 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005186 struct nfs4_sequence_args *args;
5187 struct nfs4_sequence_res *res;
5188
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005189 args = task->tk_msg.rpc_argp;
5190 res = task->tk_msg.rpc_resp;
5191
Trond Myklebust035168a2010-06-16 09:52:26 -04005192 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005193 return;
5194 rpc_call_start(task);
5195}
5196
5197static const struct rpc_call_ops nfs41_sequence_ops = {
5198 .rpc_call_done = nfs41_sequence_call_done,
5199 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005200 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005201};
5202
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005203static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005204{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005205 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005206 struct rpc_message msg = {
5207 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5208 .rpc_cred = cred,
5209 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005210 struct rpc_task_setup task_setup_data = {
5211 .rpc_client = clp->cl_rpcclient,
5212 .rpc_message = &msg,
5213 .callback_ops = &nfs41_sequence_ops,
5214 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5215 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005216
Alexandros Batsakis71358402010-02-05 03:45:05 -08005217 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005218 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005219 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005220 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005221 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005222 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005223 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005224 msg.rpc_argp = &calldata->args;
5225 msg.rpc_resp = &calldata->res;
5226 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005227 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005228
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005229 return rpc_run_task(&task_setup_data);
5230}
5231
5232static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5233{
5234 struct rpc_task *task;
5235 int ret = 0;
5236
5237 task = _nfs41_proc_sequence(clp, cred);
5238 if (IS_ERR(task))
5239 ret = PTR_ERR(task);
5240 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005241 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005242 dprintk("<-- %s status=%d\n", __func__, ret);
5243 return ret;
5244}
5245
5246static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5247{
5248 struct rpc_task *task;
5249 int ret;
5250
5251 task = _nfs41_proc_sequence(clp, cred);
5252 if (IS_ERR(task)) {
5253 ret = PTR_ERR(task);
5254 goto out;
5255 }
5256 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005257 if (!ret) {
5258 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5259
5260 if (task->tk_status == 0)
5261 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005262 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005263 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005264 rpc_put_task(task);
5265out:
5266 dprintk("<-- %s status=%d\n", __func__, ret);
5267 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005268}
5269
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005270struct nfs4_reclaim_complete_data {
5271 struct nfs_client *clp;
5272 struct nfs41_reclaim_complete_args arg;
5273 struct nfs41_reclaim_complete_res res;
5274};
5275
5276static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5277{
5278 struct nfs4_reclaim_complete_data *calldata = data;
5279
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005280 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005281 if (nfs41_setup_sequence(calldata->clp->cl_session,
5282 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005283 &calldata->res.seq_res, 0, task))
5284 return;
5285
5286 rpc_call_start(task);
5287}
5288
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005289static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5290{
5291 switch(task->tk_status) {
5292 case 0:
5293 case -NFS4ERR_COMPLETE_ALREADY:
5294 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5295 break;
5296 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005297 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5298 return -EAGAIN;
5299 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005300 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005301 }
5302 return 0;
5303}
5304
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005305static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5306{
5307 struct nfs4_reclaim_complete_data *calldata = data;
5308 struct nfs_client *clp = calldata->clp;
5309 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5310
5311 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005312 if (!nfs41_sequence_done(task, res))
5313 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005314
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005315 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5316 rpc_restart_call_prepare(task);
5317 return;
5318 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005319 dprintk("<-- %s\n", __func__);
5320}
5321
5322static void nfs4_free_reclaim_complete_data(void *data)
5323{
5324 struct nfs4_reclaim_complete_data *calldata = data;
5325
5326 kfree(calldata);
5327}
5328
5329static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5330 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5331 .rpc_call_done = nfs4_reclaim_complete_done,
5332 .rpc_release = nfs4_free_reclaim_complete_data,
5333};
5334
5335/*
5336 * Issue a global reclaim complete.
5337 */
5338static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5339{
5340 struct nfs4_reclaim_complete_data *calldata;
5341 struct rpc_task *task;
5342 struct rpc_message msg = {
5343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5344 };
5345 struct rpc_task_setup task_setup_data = {
5346 .rpc_client = clp->cl_rpcclient,
5347 .rpc_message = &msg,
5348 .callback_ops = &nfs4_reclaim_complete_call_ops,
5349 .flags = RPC_TASK_ASYNC,
5350 };
5351 int status = -ENOMEM;
5352
5353 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005354 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005355 if (calldata == NULL)
5356 goto out;
5357 calldata->clp = clp;
5358 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005359
5360 msg.rpc_argp = &calldata->arg;
5361 msg.rpc_resp = &calldata->res;
5362 task_setup_data.callback_data = calldata;
5363 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005364 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005365 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005366 goto out;
5367 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005368 status = nfs4_wait_for_completion_rpc_task(task);
5369 if (status == 0)
5370 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005371 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005372 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005373out:
5374 dprintk("<-- %s status=%d\n", __func__, status);
5375 return status;
5376}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005377
5378static void
5379nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5380{
5381 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005382 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005383
5384 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005385 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5386 * right now covering the LAYOUTGET we are about to send.
5387 * However, that is not so catastrophic, and there seems
5388 * to be no way to prevent it completely.
5389 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005390 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5391 &lgp->res.seq_res, 0, task))
5392 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005393 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5394 NFS_I(lgp->args.inode)->layout,
5395 lgp->args.ctx->state)) {
5396 rpc_exit(task, NFS4_OK);
5397 return;
5398 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005399 rpc_call_start(task);
5400}
5401
5402static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5403{
5404 struct nfs4_layoutget *lgp = calldata;
5405 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5406
5407 dprintk("--> %s\n", __func__);
5408
5409 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5410 return;
5411
5412 switch (task->tk_status) {
5413 case 0:
5414 break;
5415 case -NFS4ERR_LAYOUTTRYLATER:
5416 case -NFS4ERR_RECALLCONFLICT:
5417 task->tk_status = -NFS4ERR_DELAY;
5418 /* Fall through */
5419 default:
5420 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5421 rpc_restart_call_prepare(task);
5422 return;
5423 }
5424 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005425 dprintk("<-- %s\n", __func__);
5426}
5427
5428static void nfs4_layoutget_release(void *calldata)
5429{
5430 struct nfs4_layoutget *lgp = calldata;
5431
5432 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005433 if (lgp->res.layout.buf != NULL)
5434 free_page((unsigned long) lgp->res.layout.buf);
5435 put_nfs_open_context(lgp->args.ctx);
5436 kfree(calldata);
5437 dprintk("<-- %s\n", __func__);
5438}
5439
5440static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5441 .rpc_call_prepare = nfs4_layoutget_prepare,
5442 .rpc_call_done = nfs4_layoutget_done,
5443 .rpc_release = nfs4_layoutget_release,
5444};
5445
5446int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5447{
5448 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5449 struct rpc_task *task;
5450 struct rpc_message msg = {
5451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5452 .rpc_argp = &lgp->args,
5453 .rpc_resp = &lgp->res,
5454 };
5455 struct rpc_task_setup task_setup_data = {
5456 .rpc_client = server->client,
5457 .rpc_message = &msg,
5458 .callback_ops = &nfs4_layoutget_call_ops,
5459 .callback_data = lgp,
5460 .flags = RPC_TASK_ASYNC,
5461 };
5462 int status = 0;
5463
5464 dprintk("--> %s\n", __func__);
5465
5466 lgp->res.layout.buf = (void *)__get_free_page(GFP_NOFS);
5467 if (lgp->res.layout.buf == NULL) {
5468 nfs4_layoutget_release(lgp);
5469 return -ENOMEM;
5470 }
5471
5472 lgp->res.seq_res.sr_slot = NULL;
5473 task = rpc_run_task(&task_setup_data);
5474 if (IS_ERR(task))
5475 return PTR_ERR(task);
5476 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005477 if (status == 0)
5478 status = task->tk_status;
5479 if (status == 0)
5480 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005481 rpc_put_task(task);
5482 dprintk("<-- %s status=%d\n", __func__, status);
5483 return status;
5484}
5485
5486static int
5487_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5488{
5489 struct nfs4_getdeviceinfo_args args = {
5490 .pdev = pdev,
5491 };
5492 struct nfs4_getdeviceinfo_res res = {
5493 .pdev = pdev,
5494 };
5495 struct rpc_message msg = {
5496 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5497 .rpc_argp = &args,
5498 .rpc_resp = &res,
5499 };
5500 int status;
5501
5502 dprintk("--> %s\n", __func__);
5503 status = nfs4_call_sync(server, &msg, &args, &res, 0);
5504 dprintk("<-- %s status=%d\n", __func__, status);
5505
5506 return status;
5507}
5508
5509int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5510{
5511 struct nfs4_exception exception = { };
5512 int err;
5513
5514 do {
5515 err = nfs4_handle_exception(server,
5516 _nfs4_proc_getdeviceinfo(server, pdev),
5517 &exception);
5518 } while (exception.retry);
5519 return err;
5520}
5521EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5522
Andy Adamson557134a2009-04-01 09:21:53 -04005523#endif /* CONFIG_NFS_V4_1 */
5524
Andy Adamson591d71c2009-04-01 09:22:47 -04005525struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005526 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005527 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 .recover_open = nfs4_open_reclaim,
5529 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005530 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005531 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532};
5533
Andy Adamson591d71c2009-04-01 09:22:47 -04005534#if defined(CONFIG_NFS_V4_1)
5535struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5536 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5537 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5538 .recover_open = nfs4_open_reclaim,
5539 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005540 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005541 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005542 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005543};
5544#endif /* CONFIG_NFS_V4_1 */
5545
5546struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005547 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005548 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 .recover_open = nfs4_open_expired,
5550 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005551 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005552 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553};
5554
Andy Adamson591d71c2009-04-01 09:22:47 -04005555#if defined(CONFIG_NFS_V4_1)
5556struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5557 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5558 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5559 .recover_open = nfs4_open_expired,
5560 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005561 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005562 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005563};
5564#endif /* CONFIG_NFS_V4_1 */
5565
Benny Halevy29fba382009-04-01 09:22:44 -04005566struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5567 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005568 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005569 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005570};
5571
5572#if defined(CONFIG_NFS_V4_1)
5573struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5574 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005575 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005576 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005577};
5578#endif
5579
Trond Myklebust97dc1352010-06-16 09:52:26 -04005580static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5581 .minor_version = 0,
5582 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005583 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005584 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5585 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5586 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005587};
5588
5589#if defined(CONFIG_NFS_V4_1)
5590static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5591 .minor_version = 1,
5592 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005593 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005594 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5595 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5596 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005597};
5598#endif
5599
Trond Myklebust97dc1352010-06-16 09:52:26 -04005600const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5601 [0] = &nfs_v4_0_minor_ops,
5602#if defined(CONFIG_NFS_V4_1)
5603 [1] = &nfs_v4_1_minor_ops,
5604#endif
5605};
5606
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005607static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005608 .permission = nfs_permission,
5609 .getattr = nfs_getattr,
5610 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005611 .getxattr = generic_getxattr,
5612 .setxattr = generic_setxattr,
5613 .listxattr = generic_listxattr,
5614 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005615};
5616
David Howells509de812006-08-22 20:06:11 -04005617const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 .version = 4, /* protocol version */
5619 .dentry_ops = &nfs4_dentry_operations,
5620 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005621 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 .getroot = nfs4_proc_get_root,
5623 .getattr = nfs4_proc_getattr,
5624 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005625 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 .lookup = nfs4_proc_lookup,
5627 .access = nfs4_proc_access,
5628 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 .create = nfs4_proc_create,
5630 .remove = nfs4_proc_remove,
5631 .unlink_setup = nfs4_proc_unlink_setup,
5632 .unlink_done = nfs4_proc_unlink_done,
5633 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04005634 .rename_setup = nfs4_proc_rename_setup,
5635 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 .link = nfs4_proc_link,
5637 .symlink = nfs4_proc_symlink,
5638 .mkdir = nfs4_proc_mkdir,
5639 .rmdir = nfs4_proc_remove,
5640 .readdir = nfs4_proc_readdir,
5641 .mknod = nfs4_proc_mknod,
5642 .statfs = nfs4_proc_statfs,
5643 .fsinfo = nfs4_proc_fsinfo,
5644 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005645 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 .decode_dirent = nfs4_decode_dirent,
5647 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005648 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005650 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005652 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005654 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005655 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04005656 .open_context = nfs4_atomic_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657};
5658
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005659static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
5660 .prefix = XATTR_NAME_NFSV4_ACL,
5661 .list = nfs4_xattr_list_nfs4_acl,
5662 .get = nfs4_xattr_get_nfs4_acl,
5663 .set = nfs4_xattr_set_nfs4_acl,
5664};
5665
5666const struct xattr_handler *nfs4_xattr_handlers[] = {
5667 &nfs4_xattr_nfs4_acl_handler,
5668 NULL
5669};
5670
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671/*
5672 * Local variables:
5673 * c-basic-offset: 8
5674 * End:
5675 */