blob: b28bb19b04f009a255c9ec29a239cab4a0dc7174 [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>
Bryan Schumaker8f70e952011-03-24 17:12:31 +000044#include <linux/sunrpc/gss_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/nfs.h>
46#include <linux/nfs4.h>
47#include <linux/nfs_fs.h>
48#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040049#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070051#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040052#include <linux/module.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040053#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Trond Myklebust4ce79712005-06-22 17:16:21 +000058#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050060#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050061#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040062#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040063#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#define NFSDBG_FACILITY NFSDBG_PROC
66
Trond Myklebust2066fe82006-09-15 08:30:46 -040067#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define NFS4_POLL_RETRY_MAX (15*HZ)
69
Trond Myklebusta78cb572009-08-09 15:06:19 -040070#define NFS4_MAX_LOOP_ON_RECOVER (10)
71
Trond Myklebustcdd4e682006-01-03 09:55:12 +010072struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010073static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080074static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050076static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040077static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040078static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
79 struct nfs_fattr *fattr, struct iattr *sattr,
80 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040081#ifdef CONFIG_NFS_V4_1
82static int nfs41_test_stateid(struct nfs_server *, struct nfs4_state *);
83static int nfs41_free_stateid(struct nfs_server *, struct nfs4_state *);
84#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050086static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Trond Myklebust52567b02009-10-23 14:46:42 -040088 if (err >= -1000)
89 return err;
90 switch (err) {
91 case -NFS4ERR_RESOURCE:
92 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000093 case -NFS4ERR_WRONGSEC:
94 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080095 case -NFS4ERR_BADOWNER:
96 case -NFS4ERR_BADNAME:
97 return -EINVAL;
Trond Myklebust52567b02009-10-23 14:46:42 -040098 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700100 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400101 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400103 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106/*
107 * This is our standard bitmap for GETATTR requests.
108 */
109const u32 nfs4_fattr_bitmap[2] = {
110 FATTR4_WORD0_TYPE
111 | FATTR4_WORD0_CHANGE
112 | FATTR4_WORD0_SIZE
113 | FATTR4_WORD0_FSID
114 | FATTR4_WORD0_FILEID,
115 FATTR4_WORD1_MODE
116 | FATTR4_WORD1_NUMLINKS
117 | FATTR4_WORD1_OWNER
118 | FATTR4_WORD1_OWNER_GROUP
119 | FATTR4_WORD1_RAWDEV
120 | FATTR4_WORD1_SPACE_USED
121 | FATTR4_WORD1_TIME_ACCESS
122 | FATTR4_WORD1_TIME_METADATA
123 | FATTR4_WORD1_TIME_MODIFY
124};
125
126const u32 nfs4_statfs_bitmap[2] = {
127 FATTR4_WORD0_FILES_AVAIL
128 | FATTR4_WORD0_FILES_FREE
129 | FATTR4_WORD0_FILES_TOTAL,
130 FATTR4_WORD1_SPACE_AVAIL
131 | FATTR4_WORD1_SPACE_FREE
132 | FATTR4_WORD1_SPACE_TOTAL
133};
134
Trond Myklebust4ce79712005-06-22 17:16:21 +0000135const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 FATTR4_WORD0_MAXLINK
137 | FATTR4_WORD0_MAXNAME,
138 0
139};
140
Fred Isamandae100c2011-07-30 20:52:37 -0400141const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 | FATTR4_WORD0_MAXREAD
143 | FATTR4_WORD0_MAXWRITE
144 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700145 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400146 | FATTR4_WORD1_FS_LAYOUT_TYPES,
147 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
Manoj Naik830b8e32006-06-09 09:34:25 -0400150const u32 nfs4_fs_locations_bitmap[2] = {
151 FATTR4_WORD0_TYPE
152 | FATTR4_WORD0_CHANGE
153 | FATTR4_WORD0_SIZE
154 | FATTR4_WORD0_FSID
155 | FATTR4_WORD0_FILEID
156 | FATTR4_WORD0_FS_LOCATIONS,
157 FATTR4_WORD1_MODE
158 | FATTR4_WORD1_NUMLINKS
159 | FATTR4_WORD1_OWNER
160 | FATTR4_WORD1_OWNER_GROUP
161 | FATTR4_WORD1_RAWDEV
162 | FATTR4_WORD1_SPACE_USED
163 | FATTR4_WORD1_TIME_ACCESS
164 | FATTR4_WORD1_TIME_METADATA
165 | FATTR4_WORD1_TIME_MODIFY
166 | FATTR4_WORD1_MOUNTED_ON_FILEID
167};
168
Al Virobc4785c2006-10-19 23:28:51 -0700169static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 struct nfs4_readdir_arg *readdir)
171{
Al Viro0dbb4c62006-10-19 23:28:49 -0700172 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 BUG_ON(readdir->count < 80);
175 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000176 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
178 return;
179 }
180
181 readdir->cookie = 0;
182 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
183 if (cookie == 2)
184 return;
185
186 /*
187 * NFSv4 servers do not return entries for '.' and '..'
188 * Therefore, we fake these entries here. We let '.'
189 * have cookie 0 and '..' have cookie 1. Note that
190 * when talking to the server, we always send cookie 0
191 * instead of 1 or 2.
192 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700193 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 if (cookie == 0) {
196 *p++ = xdr_one; /* next */
197 *p++ = xdr_zero; /* cookie, first word */
198 *p++ = xdr_one; /* cookie, second word */
199 *p++ = xdr_one; /* entry len */
200 memcpy(p, ".\0\0\0", 4); /* entry */
201 p++;
202 *p++ = xdr_one; /* bitmap length */
203 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
204 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400205 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207
208 *p++ = xdr_one; /* next */
209 *p++ = xdr_zero; /* cookie, first word */
210 *p++ = xdr_two; /* cookie, second word */
211 *p++ = xdr_two; /* entry len */
212 memcpy(p, "..\0\0", 4); /* entry */
213 p++;
214 *p++ = xdr_one; /* bitmap length */
215 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
216 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400217 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 readdir->pgbase = (char *)p - (char *)start;
220 readdir->count -= readdir->pgbase;
221 kunmap_atomic(start, KM_USER0);
222}
223
Trond Myklebust65de8722008-12-23 15:21:44 -0500224static int nfs4_wait_clnt_recover(struct nfs_client *clp)
225{
226 int res;
227
228 might_sleep();
229
Trond Myklebuste005e802008-12-23 15:21:48 -0500230 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400231 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500232 return res;
233}
234
235static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
236{
237 int res = 0;
238
239 might_sleep();
240
241 if (*timeout <= 0)
242 *timeout = NFS4_POLL_RETRY_MIN;
243 if (*timeout > NFS4_POLL_RETRY_MAX)
244 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100245 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500246 if (fatal_signal_pending(current))
247 res = -ERESTARTSYS;
248 *timeout <<= 1;
249 return res;
250}
251
252/* This is the error handling routine for processes that are allowed
253 * to sleep.
254 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800255static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500256{
257 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500258 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500259 int ret = errorcode;
260
261 exception->retry = 0;
262 switch(errorcode) {
263 case 0:
264 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500265 case -NFS4ERR_ADMIN_REVOKED:
266 case -NFS4ERR_BAD_STATEID:
267 case -NFS4ERR_OPENMODE:
268 if (state == NULL)
269 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500270 nfs4_schedule_stateid_recovery(server, state);
271 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400272 case -NFS4ERR_EXPIRED:
273 if (state != NULL)
274 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500275 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500276 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500277 nfs4_schedule_lease_recovery(clp);
278 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500279#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400280 case -NFS4ERR_BADSESSION:
281 case -NFS4ERR_BADSLOT:
282 case -NFS4ERR_BAD_HIGH_SLOT:
283 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
284 case -NFS4ERR_DEADSESSION:
285 case -NFS4ERR_SEQ_FALSE_RETRY:
286 case -NFS4ERR_SEQ_MISORDERED:
287 dprintk("%s ERROR: %d Reset session\n", __func__,
288 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500289 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400290 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500291 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500292#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500293 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500294 if (exception->timeout > HZ) {
295 /* We have retried a decent amount, time to
296 * fail
297 */
298 ret = -EBUSY;
299 break;
300 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500301 case -NFS4ERR_GRACE:
302 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500303 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500304 ret = nfs4_delay(server->client, &exception->timeout);
305 if (ret != 0)
306 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400307 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500308 case -NFS4ERR_OLD_STATEID:
309 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800310 break;
311 case -NFS4ERR_BADOWNER:
312 /* The following works around a Linux server bug! */
313 case -NFS4ERR_BADNAME:
314 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
315 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
316 exception->retry = 1;
317 printk(KERN_WARNING "NFS: v4 server %s "
318 "does not accept raw "
319 "uid/gids. "
320 "Reenabling the idmapper.\n",
321 server->nfs_client->cl_hostname);
322 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500323 }
324 /* We failed to handle the error */
325 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500326wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500327 ret = nfs4_wait_clnt_recover(clp);
328 if (ret == 0)
329 exception->retry = 1;
330 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500331}
332
333
Trond Myklebust452e9352010-07-31 14:29:06 -0400334static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 spin_lock(&clp->cl_lock);
337 if (time_before(clp->cl_last_renewal,timestamp))
338 clp->cl_last_renewal = timestamp;
339 spin_unlock(&clp->cl_lock);
340}
341
Trond Myklebust452e9352010-07-31 14:29:06 -0400342static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
343{
344 do_renew_lease(server->nfs_client, timestamp);
345}
346
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400347#if defined(CONFIG_NFS_V4_1)
348
Benny Halevy510b8172009-04-01 09:22:14 -0400349/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400350 * nfs4_free_slot - free a slot and efficiently update slot table.
351 *
352 * freeing a slot is trivially done by clearing its respective bit
353 * in the bitmap.
354 * If the freed slotid equals highest_used_slotid we want to update it
355 * so that the server would be able to size down the slot table if needed,
356 * otherwise we know that the highest_used_slotid is still in use.
357 * When updating highest_used_slotid there may be "holes" in the bitmap
358 * so we need to scan down from highest_used_slotid to 0 looking for the now
359 * highest slotid in use.
360 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500361 *
362 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400363 */
364static void
Benny Halevydfb4f3092010-09-24 09:17:01 -0400365nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400366{
Benny Halevydfb4f3092010-09-24 09:17:01 -0400367 int free_slotid = free_slot - tbl->slots;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400368 int slotid = free_slotid;
369
Benny Halevydfb4f3092010-09-24 09:17:01 -0400370 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400371 /* clear used bit in bitmap */
372 __clear_bit(slotid, tbl->used_slots);
373
374 /* update highest_used_slotid when it is freed */
375 if (slotid == tbl->highest_used_slotid) {
376 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500377 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400378 tbl->highest_used_slotid = slotid;
379 else
380 tbl->highest_used_slotid = -1;
381 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400382 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
383 free_slotid, tbl->highest_used_slotid);
384}
385
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800386/*
Andy Adamson42acd022011-01-06 02:04:34 +0000387 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800388 */
Andy Adamson42acd022011-01-06 02:04:34 +0000389static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800390{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800391 struct rpc_task *task;
392
Trond Myklebusta2118c32010-06-16 09:52:26 -0400393 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800394 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
395 if (task)
396 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800397 return;
398 }
399
400 if (ses->fc_slot_table.highest_used_slotid != -1)
401 return;
402
Andy Adamson42acd022011-01-06 02:04:34 +0000403 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
404 complete(&ses->fc_slot_table.complete);
405}
406
407/*
408 * Signal state manager thread if session back channel is drained
409 */
410void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
411{
412 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
413 ses->bc_slot_table.highest_used_slotid != -1)
414 return;
415 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
416 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800417}
418
Trond Myklebustd185a332010-06-16 09:52:25 -0400419static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400420{
421 struct nfs4_slot_table *tbl;
422
Trond Myklebustd185a332010-06-16 09:52:25 -0400423 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400424 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400425 /* just wake up the next guy waiting since
426 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500427 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500428 return;
Andy Adamson13615872009-04-01 09:22:17 -0400429 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500430
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500431 spin_lock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400432 nfs4_free_slot(tbl, res->sr_slot);
Andy Adamson42acd022011-01-06 02:04:34 +0000433 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500434 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400435 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400436}
437
Trond Myklebust14516c32010-07-31 14:29:06 -0400438static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400439{
440 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400441 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400442
443 /*
444 * sr_status remains 1 if an RPC level error occurred. The server
445 * may or may not have processed the sequence operation..
446 * Proceed as if the server received and processed the sequence
447 * operation.
448 */
449 if (res->sr_status == 1)
450 res->sr_status = NFS_OK;
451
Bryan Schumaker468f8612011-04-18 15:57:32 -0400452 /* don't increment the sequence number if the task wasn't sent */
453 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400454 goto out;
455
Andy Adamson691daf32009-12-04 15:55:39 -0500456 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400457 switch (res->sr_status) {
458 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400459 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400460 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400461 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400462 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400463 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500464 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500465 if (res->sr_status_flags != 0)
466 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400467 break;
468 case -NFS4ERR_DELAY:
469 /* The server detected a resend of the RPC call and
470 * returned NFS4ERR_DELAY as per Section 2.10.6.2
471 * of RFC5661.
472 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200473 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400474 __func__,
475 res->sr_slot - res->sr_session->fc_slot_table.slots,
476 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400477 goto out_retry;
478 default:
479 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400480 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400481 }
482out:
483 /* The session may be reset by one of the error handlers. */
484 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400485 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400486 return 1;
487out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400488 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400489 goto out;
490 rpc_delay(task, NFS4_POLL_RETRY_MAX);
491 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400492}
493
Trond Myklebust14516c32010-07-31 14:29:06 -0400494static int nfs4_sequence_done(struct rpc_task *task,
495 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400496{
Trond Myklebust14516c32010-07-31 14:29:06 -0400497 if (res->sr_session == NULL)
498 return 1;
499 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400500}
501
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400502/*
Benny Halevy510b8172009-04-01 09:22:14 -0400503 * nfs4_find_slot - efficiently look for a free slot
504 *
505 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
506 * If found, we mark the slot as used, update the highest_used_slotid,
507 * and respectively set up the sequence operation args.
508 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400509 *
510 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400511 */
512static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800513nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400514{
515 int slotid;
516 u8 ret_id = NFS4_MAX_SLOT_TABLE;
517 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
518
Benny Halevy510b8172009-04-01 09:22:14 -0400519 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
520 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
521 tbl->max_slots);
522 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
523 if (slotid >= tbl->max_slots)
524 goto out;
525 __set_bit(slotid, tbl->used_slots);
526 if (slotid > tbl->highest_used_slotid)
527 tbl->highest_used_slotid = slotid;
528 ret_id = slotid;
529out:
530 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
531 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400532 return ret_id;
533}
534
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000535int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400536 struct nfs4_sequence_args *args,
537 struct nfs4_sequence_res *res,
538 int cache_reply,
539 struct rpc_task *task)
540{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400541 struct nfs4_slot *slot;
542 struct nfs4_slot_table *tbl;
543 u8 slotid;
544
545 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400546 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400547 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400548 return 0;
549
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400550 tbl = &session->fc_slot_table;
551
552 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400553 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800554 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500555 /*
556 * The state manager will wait until the slot table is empty.
557 * Schedule the reset thread
558 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500559 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400560 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500561 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500562 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400563 }
564
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800565 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
566 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
567 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
568 spin_unlock(&tbl->slot_tbl_lock);
569 dprintk("%s enforce FIFO order\n", __func__);
570 return -EAGAIN;
571 }
572
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800573 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400574 if (slotid == NFS4_MAX_SLOT_TABLE) {
575 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
576 spin_unlock(&tbl->slot_tbl_lock);
577 dprintk("<-- %s: no free slots\n", __func__);
578 return -EAGAIN;
579 }
580 spin_unlock(&tbl->slot_tbl_lock);
581
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800582 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400583 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400584 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400585 args->sa_slotid = slotid;
586 args->sa_cache_this = cache_reply;
587
588 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
589
Benny Halevy99fe60d2009-04-01 09:22:29 -0400590 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400591 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400592 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400593 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400594 /*
595 * sr_status is only set in decode_sequence, and so will remain
596 * set to 1 if an rpc level failure occurs.
597 */
598 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400599 return 0;
600}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000601EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400602
Trond Myklebust035168a2010-06-16 09:52:26 -0400603int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604 struct nfs4_sequence_args *args,
605 struct nfs4_sequence_res *res,
606 int cache_reply,
607 struct rpc_task *task)
608{
Trond Myklebust035168a2010-06-16 09:52:26 -0400609 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400610 int ret = 0;
611
Trond Myklebust035168a2010-06-16 09:52:26 -0400612 if (session == NULL) {
613 args->sa_session = NULL;
614 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400615 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400616 }
617
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200618 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400619 __func__, session->clp, session, res->sr_slot ?
620 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400621
622 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400624out:
625 dprintk("<-- %s status=%d\n", __func__, ret);
626 return ret;
627}
628
629struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400630 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400631 struct nfs4_sequence_args *seq_args;
632 struct nfs4_sequence_res *seq_res;
633 int cache_reply;
634};
635
636static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
637{
638 struct nfs41_call_sync_data *data = calldata;
639
Trond Myklebust035168a2010-06-16 09:52:26 -0400640 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
641
642 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400643 data->seq_res, data->cache_reply, task))
644 return;
645 rpc_call_start(task);
646}
647
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800648static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
649{
650 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
651 nfs41_call_sync_prepare(task, calldata);
652}
653
Andy Adamson69ab40c2009-04-01 09:22:19 -0400654static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
655{
656 struct nfs41_call_sync_data *data = calldata;
657
Trond Myklebust14516c32010-07-31 14:29:06 -0400658 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400659}
660
Andy Adamsonce5039c2009-04-01 09:22:13 -0400661struct rpc_call_ops nfs41_call_sync_ops = {
662 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400663 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400664};
665
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800666struct rpc_call_ops nfs41_call_priv_sync_ops = {
667 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
668 .rpc_call_done = nfs41_call_sync_done,
669};
670
Bryan Schumaker7c513052011-03-24 17:12:24 +0000671static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
672 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400673 struct rpc_message *msg,
674 struct nfs4_sequence_args *args,
675 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800676 int cache_reply,
677 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400678{
679 int ret;
680 struct rpc_task *task;
681 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400682 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400683 .seq_args = args,
684 .seq_res = res,
685 .cache_reply = cache_reply,
686 };
687 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000688 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400689 .rpc_message = msg,
690 .callback_ops = &nfs41_call_sync_ops,
691 .callback_data = &data
692 };
693
Benny Halevydfb4f3092010-09-24 09:17:01 -0400694 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800695 if (privileged)
696 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400697 task = rpc_run_task(&task_setup);
698 if (IS_ERR(task))
699 ret = PTR_ERR(task);
700 else {
701 ret = task->tk_status;
702 rpc_put_task(task);
703 }
704 return ret;
705}
706
Bryan Schumaker7c513052011-03-24 17:12:24 +0000707int _nfs4_call_sync_session(struct rpc_clnt *clnt,
708 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400709 struct rpc_message *msg,
710 struct nfs4_sequence_args *args,
711 struct nfs4_sequence_res *res,
712 int cache_reply)
713{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000714 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400715}
716
Trond Myklebustdf896452010-06-16 09:52:26 -0400717#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400718static int nfs4_sequence_done(struct rpc_task *task,
719 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400720{
Trond Myklebust14516c32010-07-31 14:29:06 -0400721 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400722}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400723#endif /* CONFIG_NFS_V4_1 */
724
Bryan Schumaker7c513052011-03-24 17:12:24 +0000725int _nfs4_call_sync(struct rpc_clnt *clnt,
726 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400727 struct rpc_message *msg,
728 struct nfs4_sequence_args *args,
729 struct nfs4_sequence_res *res,
730 int cache_reply)
731{
Benny Halevyf3752972009-04-01 09:22:04 -0400732 args->sa_session = res->sr_session = NULL;
Bryan Schumaker7c513052011-03-24 17:12:24 +0000733 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400734}
735
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000736static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000737int nfs4_call_sync(struct rpc_clnt *clnt,
738 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000739 struct rpc_message *msg,
740 struct nfs4_sequence_args *args,
741 struct nfs4_sequence_res *res,
742 int cache_reply)
743{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000744 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
745 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000746}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400747
Trond Myklebust38478b22006-05-25 01:40:57 -0400748static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Trond Myklebust38478b22006-05-25 01:40:57 -0400750 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Trond Myklebust38478b22006-05-25 01:40:57 -0400752 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400753 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700754 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400755 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700756 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400757 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100760struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400761 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100762 struct nfs_openargs o_arg;
763 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100764 struct nfs_open_confirmargs c_arg;
765 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100766 struct nfs_fattr f_attr;
767 struct nfs_fattr dir_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100768 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400769 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400771 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100772 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100773 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400774 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100775 int rpc_status;
776 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100777};
778
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400779
780static void nfs4_init_opendata_res(struct nfs4_opendata *p)
781{
782 p->o_res.f_attr = &p->f_attr;
783 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400784 p->o_res.seqid = p->o_arg.seqid;
785 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400786 p->o_res.server = p->o_arg.server;
787 nfs_fattr_init(&p->f_attr);
788 nfs_fattr_init(&p->dir_attr);
789}
790
Al Viro82a2c1b2011-06-22 18:30:55 -0400791static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500792 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400793 const struct iattr *attrs,
794 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100795{
Al Viro82a2c1b2011-06-22 18:30:55 -0400796 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100797 struct inode *dir = parent->d_inode;
798 struct nfs_server *server = NFS_SERVER(dir);
799 struct nfs4_opendata *p;
800
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400801 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100802 if (p == NULL)
803 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400804 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100805 if (p->o_arg.seqid == NULL)
806 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400807 nfs_sb_active(dentry->d_sb);
808 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100809 p->dir = parent;
810 p->owner = sp;
811 atomic_inc(&sp->so_count);
812 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500813 p->o_arg.open_flags = flags;
814 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400815 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400816 p->o_arg.id = sp->so_owner_id.id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400817 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818 p->o_arg.server = server;
819 p->o_arg.bitmask = server->attr_bitmask;
820 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400821 if (flags & O_CREAT) {
822 u32 *s;
823
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100824 p->o_arg.u.attrs = &p->attrs;
825 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400826 s = (u32 *) p->o_arg.u.verifier.data;
827 s[0] = jiffies;
828 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100829 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100830 p->c_arg.fh = &p->o_res.fh;
831 p->c_arg.stateid = &p->o_res.stateid;
832 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400833 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400834 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100835 return p;
836err_free:
837 kfree(p);
838err:
839 dput(parent);
840 return NULL;
841}
842
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400843static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100844{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400845 struct nfs4_opendata *p = container_of(kref,
846 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400847 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400848
849 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400850 if (p->state != NULL)
851 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400852 nfs4_put_state_owner(p->owner);
853 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400854 dput(p->dentry);
855 nfs_sb_deactive(sb);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400856 kfree(p);
857}
858
859static void nfs4_opendata_put(struct nfs4_opendata *p)
860{
861 if (p != NULL)
862 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100863}
864
Trond Myklebust06f814a2006-01-03 09:55:07 +0100865static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
866{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100867 int ret;
868
Trond Myklebust06f814a2006-01-03 09:55:07 +0100869 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100870 return ret;
871}
872
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500873static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400874{
875 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500876
877 if (open_mode & O_EXCL)
878 goto out;
879 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400880 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500881 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
882 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400883 break;
884 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500885 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
886 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400887 break;
888 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500889 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
890 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400891 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500892out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400893 return ret;
894}
895
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500896static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400897{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500898 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400899 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500900 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400901 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500902 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400903 return 1;
904}
905
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500906static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100907{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100909 case FMODE_WRITE:
910 state->n_wronly++;
911 break;
912 case FMODE_READ:
913 state->n_rdonly++;
914 break;
915 case FMODE_READ|FMODE_WRITE:
916 state->n_rdwr++;
917 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500918 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100919}
920
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500921static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400922{
923 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
924 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
925 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500926 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400927 case FMODE_READ:
928 set_bit(NFS_O_RDONLY_STATE, &state->flags);
929 break;
930 case FMODE_WRITE:
931 set_bit(NFS_O_WRONLY_STATE, &state->flags);
932 break;
933 case FMODE_READ|FMODE_WRITE:
934 set_bit(NFS_O_RDWR_STATE, &state->flags);
935 }
936}
937
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500938static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400939{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400940 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500941 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400942 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400943}
944
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500945static 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 -0700946{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400947 /*
948 * Protect the call to nfs4_state_set_mode_locked and
949 * serialise the stateid update
950 */
951 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400952 if (deleg_stateid != NULL) {
953 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
954 set_bit(NFS_DELEGATED_STATE, &state->flags);
955 }
956 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500957 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400958 write_sequnlock(&state->seqlock);
959 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500960 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700961 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500964static 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 -0500965{
966 struct nfs_inode *nfsi = NFS_I(state->inode);
967 struct nfs_delegation *deleg_cur;
968 int ret = 0;
969
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500970 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500971
972 rcu_read_lock();
973 deleg_cur = rcu_dereference(nfsi->delegation);
974 if (deleg_cur == NULL)
975 goto no_delegation;
976
977 spin_lock(&deleg_cur->lock);
978 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500979 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500980 goto no_delegation_unlock;
981
982 if (delegation == NULL)
983 delegation = &deleg_cur->stateid;
984 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
985 goto no_delegation_unlock;
986
Trond Myklebustb7391f42008-12-23 15:21:52 -0500987 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500988 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500989 ret = 1;
990no_delegation_unlock:
991 spin_unlock(&deleg_cur->lock);
992no_delegation:
993 rcu_read_unlock();
994
995 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500997 ret = 1;
998 }
999
1000 return ret;
1001}
1002
1003
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001004static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001005{
1006 struct nfs_delegation *delegation;
1007
1008 rcu_read_lock();
1009 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001010 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001011 rcu_read_unlock();
1012 return;
1013 }
1014 rcu_read_unlock();
1015 nfs_inode_return_delegation(inode);
1016}
1017
Trond Myklebust6ee41262007-07-08 14:11:36 -04001018static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001019{
1020 struct nfs4_state *state = opendata->state;
1021 struct nfs_inode *nfsi = NFS_I(state->inode);
1022 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001023 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1024 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001025 nfs4_stateid stateid;
1026 int ret = -EAGAIN;
1027
Trond Myklebustaac00a82007-07-05 19:02:21 -04001028 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001029 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001030 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001031 if (can_open_cached(state, fmode, open_mode)) {
1032 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001033 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001034 goto out_return_state;
1035 }
1036 spin_unlock(&state->owner->so_lock);
1037 }
Trond Myklebust34310432008-12-23 15:21:38 -05001038 rcu_read_lock();
1039 delegation = rcu_dereference(nfsi->delegation);
1040 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001041 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001042 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001043 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001044 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001045 /* Save the delegation */
1046 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1047 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001048 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001049 if (ret != 0)
1050 goto out;
1051 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001052
1053 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001054 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001055 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001056 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001057out:
1058 return ERR_PTR(ret);
1059out_return_state:
1060 atomic_inc(&state->count);
1061 return state;
1062}
1063
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001064static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1065{
1066 struct inode *inode;
1067 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001068 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001069 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001070
Trond Myklebustaac00a82007-07-05 19:02:21 -04001071 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001072 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001073 goto out;
1074 }
1075
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001076 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001077 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001078 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001079 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001080 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001081 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001082 goto err;
1083 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001084 state = nfs4_get_open_state(inode, data->owner);
1085 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001086 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001087 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001088 int delegation_flags = 0;
1089
Trond Myklebust003707c2007-07-05 18:07:55 -04001090 rcu_read_lock();
1091 delegation = rcu_dereference(NFS_I(inode)->delegation);
1092 if (delegation)
1093 delegation_flags = delegation->flags;
1094 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001095 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001096 nfs_inode_set_delegation(state->inode,
1097 data->owner->so_cred,
1098 &data->o_res);
1099 else
1100 nfs_inode_reclaim_delegation(state->inode,
1101 data->owner->so_cred,
1102 &data->o_res);
1103 }
Trond Myklebust34310432008-12-23 15:21:38 -05001104
1105 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001106 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001107 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001108out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001109 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001110err_put_inode:
1111 iput(inode);
1112err:
1113 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001114}
1115
Trond Myklebust864472e2006-01-03 09:55:15 +01001116static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1117{
1118 struct nfs_inode *nfsi = NFS_I(state->inode);
1119 struct nfs_open_context *ctx;
1120
1121 spin_lock(&state->inode->i_lock);
1122 list_for_each_entry(ctx, &nfsi->open_files, list) {
1123 if (ctx->state != state)
1124 continue;
1125 get_nfs_open_context(ctx);
1126 spin_unlock(&state->inode->i_lock);
1127 return ctx;
1128 }
1129 spin_unlock(&state->inode->i_lock);
1130 return ERR_PTR(-ENOENT);
1131}
1132
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001133static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1134{
1135 struct nfs4_opendata *opendata;
1136
Al Viro3d4ff432011-06-22 18:40:12 -04001137 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001138 if (opendata == NULL)
1139 return ERR_PTR(-ENOMEM);
1140 opendata->state = state;
1141 atomic_inc(&state->count);
1142 return opendata;
1143}
1144
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001145static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001146{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001147 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001148 int ret;
1149
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001150 opendata->o_arg.open_flags = 0;
1151 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001152 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1153 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1154 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001155 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001156 if (ret != 0)
1157 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001158 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001159 if (IS_ERR(newstate))
1160 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001161 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001162 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001163 return 0;
1164}
1165
1166static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1167{
Trond Myklebust864472e2006-01-03 09:55:15 +01001168 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001169 int ret;
1170
1171 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001172 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001173 smp_rmb();
1174 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001175 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001176 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001177 if (ret != 0)
1178 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001179 if (newstate != state)
1180 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001181 }
1182 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001183 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001184 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001185 if (ret != 0)
1186 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001187 if (newstate != state)
1188 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001189 }
1190 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001191 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001192 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001193 if (ret != 0)
1194 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001195 if (newstate != state)
1196 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001197 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001198 /*
1199 * We may have performed cached opens for all three recoveries.
1200 * Check if we need to update the current stateid.
1201 */
1202 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1203 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001204 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001205 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1206 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001207 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001208 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001209 return 0;
1210}
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212/*
1213 * OPEN_RECLAIM:
1214 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001216static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001218 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001219 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001220 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 int status;
1222
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001223 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1224 if (IS_ERR(opendata))
1225 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001226 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1227 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001228 rcu_read_lock();
1229 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001230 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001231 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001232 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001233 opendata->o_arg.u.delegation_type = delegation_type;
1234 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001235 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return status;
1237}
1238
Trond Myklebust539cd032007-06-05 11:46:42 -04001239static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 struct nfs_server *server = NFS_SERVER(state->inode);
1242 struct nfs4_exception exception = { };
1243 int err;
1244 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001245 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001246 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001247 break;
1248 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 } while (exception.retry);
1250 return err;
1251}
1252
Trond Myklebust864472e2006-01-03 09:55:15 +01001253static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1254{
1255 struct nfs_open_context *ctx;
1256 int ret;
1257
1258 ctx = nfs4_state_find_open_context(state);
1259 if (IS_ERR(ctx))
1260 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001261 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001262 put_nfs_open_context(ctx);
1263 return ret;
1264}
1265
Trond Myklebust13437e12007-07-06 15:10:43 -04001266static 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 -07001267{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001268 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001269 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001271 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1272 if (IS_ERR(opendata))
1273 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001274 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001275 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001276 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001277 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001278 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001279 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
Trond Myklebust13437e12007-07-06 15:10:43 -04001282int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
1284 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001285 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 int err;
1287 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001288 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 switch (err) {
1290 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001291 case -ENOENT:
1292 case -ESTALE:
1293 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001294 case -NFS4ERR_BADSESSION:
1295 case -NFS4ERR_BADSLOT:
1296 case -NFS4ERR_BAD_HIGH_SLOT:
1297 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1298 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001299 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001300 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 case -NFS4ERR_STALE_CLIENTID:
1302 case -NFS4ERR_STALE_STATEID:
1303 case -NFS4ERR_EXPIRED:
1304 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001305 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001306 goto out;
1307 case -ERESTARTSYS:
1308 /*
1309 * The show must go on: exit, but mark the
1310 * stateid as needing recovery.
1311 */
1312 case -NFS4ERR_ADMIN_REVOKED:
1313 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001314 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001315 case -EKEYEXPIRED:
1316 /*
1317 * User RPCSEC_GSS context has expired.
1318 * We cannot recover this stateid now, so
1319 * skip it and allow recovery thread to
1320 * proceed.
1321 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001322 case -ENOMEM:
1323 err = 0;
1324 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
1326 err = nfs4_handle_exception(server, err, &exception);
1327 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001328out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return err;
1330}
1331
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001332static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1333{
1334 struct nfs4_opendata *data = calldata;
1335
1336 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001337 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001338 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1339 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001340 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001341 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001342 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001343 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001344}
1345
1346static void nfs4_open_confirm_release(void *calldata)
1347{
1348 struct nfs4_opendata *data = calldata;
1349 struct nfs4_state *state = NULL;
1350
1351 /* If this request hasn't been cancelled, do nothing */
1352 if (data->cancelled == 0)
1353 goto out_free;
1354 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001355 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001356 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001357 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001358 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001359 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001360out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001361 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001362}
1363
1364static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001365 .rpc_call_done = nfs4_open_confirm_done,
1366 .rpc_release = nfs4_open_confirm_release,
1367};
1368
1369/*
1370 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1371 */
1372static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1373{
1374 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1375 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001376 struct rpc_message msg = {
1377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1378 .rpc_argp = &data->c_arg,
1379 .rpc_resp = &data->c_res,
1380 .rpc_cred = data->owner->so_cred,
1381 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001382 struct rpc_task_setup task_setup_data = {
1383 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001384 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001385 .callback_ops = &nfs4_open_confirm_ops,
1386 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001387 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001388 .flags = RPC_TASK_ASYNC,
1389 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 int status;
1391
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001392 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001393 data->rpc_done = 0;
1394 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001395 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001396 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001397 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001398 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001399 status = nfs4_wait_for_completion_rpc_task(task);
1400 if (status != 0) {
1401 data->cancelled = 1;
1402 smp_wmb();
1403 } else
1404 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001405 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return status;
1407}
1408
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001409static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001411 struct nfs4_opendata *data = calldata;
1412 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001413
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001414 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1415 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001416 /*
1417 * Check if we still need to send an OPEN call, or if we can use
1418 * a delegation instead.
1419 */
1420 if (data->state != NULL) {
1421 struct nfs_delegation *delegation;
1422
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001423 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001424 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001425 rcu_read_lock();
1426 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1427 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001428 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001429 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001430 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001431 }
1432 rcu_read_unlock();
1433 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001434 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001435 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001436 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001437 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001438 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001439 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1440 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001441 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001442 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001443 &data->o_arg.seq_args,
1444 &data->o_res.seq_res, 1, task))
1445 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001446 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001447 return;
1448out_no_action:
1449 task->tk_action = NULL;
1450
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001451}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001453static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1454{
1455 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1456 nfs4_open_prepare(task, calldata);
1457}
1458
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001459static void nfs4_open_done(struct rpc_task *task, void *calldata)
1460{
1461 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001463 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001464
Trond Myklebust14516c32010-07-31 14:29:06 -04001465 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1466 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001467
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001468 if (task->tk_status == 0) {
1469 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001470 case S_IFREG:
1471 break;
1472 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001473 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001474 break;
1475 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001476 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001477 break;
1478 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001479 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001480 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001481 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001482 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1483 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001484 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001485 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001486}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001487
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001488static void nfs4_open_release(void *calldata)
1489{
1490 struct nfs4_opendata *data = calldata;
1491 struct nfs4_state *state = NULL;
1492
1493 /* If this request hasn't been cancelled, do nothing */
1494 if (data->cancelled == 0)
1495 goto out_free;
1496 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001497 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001498 goto out_free;
1499 /* In case we need an open_confirm, no cleanup! */
1500 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1501 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001502 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001503 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001504 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001505out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001506 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001507}
1508
1509static const struct rpc_call_ops nfs4_open_ops = {
1510 .rpc_call_prepare = nfs4_open_prepare,
1511 .rpc_call_done = nfs4_open_done,
1512 .rpc_release = nfs4_open_release,
1513};
1514
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001515static const struct rpc_call_ops nfs4_recover_open_ops = {
1516 .rpc_call_prepare = nfs4_recover_open_prepare,
1517 .rpc_call_done = nfs4_open_done,
1518 .rpc_release = nfs4_open_release,
1519};
1520
1521static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001522{
1523 struct inode *dir = data->dir->d_inode;
1524 struct nfs_server *server = NFS_SERVER(dir);
1525 struct nfs_openargs *o_arg = &data->o_arg;
1526 struct nfs_openres *o_res = &data->o_res;
1527 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001528 struct rpc_message msg = {
1529 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1530 .rpc_argp = o_arg,
1531 .rpc_resp = o_res,
1532 .rpc_cred = data->owner->so_cred,
1533 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001534 struct rpc_task_setup task_setup_data = {
1535 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001536 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001537 .callback_ops = &nfs4_open_ops,
1538 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001539 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001540 .flags = RPC_TASK_ASYNC,
1541 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001542 int status;
1543
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001544 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001545 data->rpc_done = 0;
1546 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001547 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001548 if (isrecover)
1549 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001550 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001551 if (IS_ERR(task))
1552 return PTR_ERR(task);
1553 status = nfs4_wait_for_completion_rpc_task(task);
1554 if (status != 0) {
1555 data->cancelled = 1;
1556 smp_wmb();
1557 } else
1558 status = data->rpc_status;
1559 rpc_put_task(task);
1560
1561 return status;
1562}
1563
1564static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1565{
1566 struct inode *dir = data->dir->d_inode;
1567 struct nfs_openres *o_res = &data->o_res;
1568 int status;
1569
1570 status = nfs4_run_open_task(data, 1);
1571 if (status != 0 || !data->rpc_done)
1572 return status;
1573
1574 nfs_refresh_inode(dir, o_res->dir_attr);
1575
1576 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1577 status = _nfs4_proc_open_confirm(data);
1578 if (status != 0)
1579 return status;
1580 }
1581
1582 return status;
1583}
1584
1585/*
1586 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1587 */
1588static int _nfs4_proc_open(struct nfs4_opendata *data)
1589{
1590 struct inode *dir = data->dir->d_inode;
1591 struct nfs_server *server = NFS_SERVER(dir);
1592 struct nfs_openargs *o_arg = &data->o_arg;
1593 struct nfs_openres *o_res = &data->o_res;
1594 int status;
1595
1596 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001597 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001598 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001599 if (status != 0) {
1600 if (status == -NFS4ERR_BADNAME &&
1601 !(o_arg->open_flags & O_CREAT))
1602 return -ENOENT;
1603 return status;
1604 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001605
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001606 if (o_arg->open_flags & O_CREAT) {
1607 update_changeattr(dir, &o_res->cinfo);
1608 nfs_post_op_update_inode(dir, o_res->dir_attr);
1609 } else
1610 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001611 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1612 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001614 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001616 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
1618 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001619 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001620 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
Andy Adamsond83217c2011-03-01 01:34:17 +00001623static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001624{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001625 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001626 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001627
Trond Myklebusta78cb572009-08-09 15:06:19 -04001628 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001629 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001630 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001631 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001632 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1633 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001634 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001635 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001636 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001637 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001638 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001639}
1640
Andy Adamsond83217c2011-03-01 01:34:17 +00001641static int nfs4_recover_expired_lease(struct nfs_server *server)
1642{
1643 return nfs4_client_recover_expired_lease(server->nfs_client);
1644}
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646/*
1647 * OPEN_EXPIRED:
1648 * reclaim state on the server after a network partition.
1649 * Assumes caller holds the appropriate lock
1650 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001651static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001653 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001654 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001656 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1657 if (IS_ERR(opendata))
1658 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001659 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001660 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001661 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001662 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001663 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001666static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001667{
Trond Myklebust539cd032007-06-05 11:46:42 -04001668 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001669 struct nfs4_exception exception = { };
1670 int err;
1671
1672 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001673 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001674 switch (err) {
1675 default:
1676 goto out;
1677 case -NFS4ERR_GRACE:
1678 case -NFS4ERR_DELAY:
1679 nfs4_handle_exception(server, err, &exception);
1680 err = 0;
1681 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001682 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001683out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001684 return err;
1685}
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1688{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001690 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Trond Myklebust864472e2006-01-03 09:55:15 +01001692 ctx = nfs4_state_find_open_context(state);
1693 if (IS_ERR(ctx))
1694 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001695 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001696 put_nfs_open_context(ctx);
1697 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698}
1699
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001700#if defined(CONFIG_NFS_V4_1)
1701static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1702{
1703 int status;
1704 struct nfs_server *server = NFS_SERVER(state->inode);
1705
1706 status = nfs41_test_stateid(server, state);
1707 if (status == NFS_OK)
1708 return 0;
1709 nfs41_free_stateid(server, state);
1710 return nfs4_open_expired(sp, state);
1711}
1712#endif
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001715 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1716 * fields corresponding to attributes that were used to store the verifier.
1717 * Make sure we clobber those fields in the later setattr call
1718 */
1719static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1720{
1721 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1722 !(sattr->ia_valid & ATTR_ATIME_SET))
1723 sattr->ia_valid |= ATTR_ATIME;
1724
1725 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1726 !(sattr->ia_valid & ATTR_MTIME_SET))
1727 sattr->ia_valid |= ATTR_MTIME;
1728}
1729
1730/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001731 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001733static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
1735 struct nfs4_state_owner *sp;
1736 struct nfs4_state *state = NULL;
1737 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001738 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001739 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 status = -ENOMEM;
1743 if (!(sp = nfs4_get_state_owner(server, cred))) {
1744 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1745 goto out_err;
1746 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001747 status = nfs4_recover_expired_lease(server);
1748 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001749 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001750 if (dentry->d_inode != NULL)
1751 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001752 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001753 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001754 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001755 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Al Viro82a2c1b2011-06-22 18:30:55 -04001757 if (dentry->d_inode != NULL)
1758 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001759
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001760 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001762 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001764 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001765 status = PTR_ERR(state);
1766 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001767 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001768 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001769 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001770
1771 if (opendata->o_arg.open_flags & O_EXCL) {
1772 nfs4_exclusive_attrset(opendata, sattr);
1773
1774 nfs_fattr_init(opendata->o_res.f_attr);
1775 status = nfs4_do_setattr(state->inode, cred,
1776 opendata->o_res.f_attr, sattr,
1777 state);
1778 if (status == 0)
1779 nfs_setattr_update_inode(state->inode, sattr);
1780 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1781 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001782 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 *res = state;
1785 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001786err_opendata_put:
1787 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001788err_put_state_owner:
1789 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 *res = NULL;
1792 return status;
1793}
1794
1795
Al Viro82a2c1b2011-06-22 18:30:55 -04001796static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
1798 struct nfs4_exception exception = { };
1799 struct nfs4_state *res;
1800 int status;
1801
1802 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001803 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 if (status == 0)
1805 break;
1806 /* NOTE: BAD_SEQID means the server and client disagree about the
1807 * book-keeping w.r.t. state-changing operations
1808 * (OPEN/CLOSE/LOCK/LOCKU...)
1809 * It is actually a sign of a bug on the client or on the server.
1810 *
1811 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001812 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 * have unhashed the old state_owner for us, and that we can
1814 * therefore safely retry using a new one. We should still warn
1815 * the user though...
1816 */
1817 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001818 printk(KERN_WARNING "NFS: v4 server %s "
1819 " returned a bad sequence-id error!\n",
1820 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 exception.retry = 1;
1822 continue;
1823 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001824 /*
1825 * BAD_STATEID on OPEN means that the server cancelled our
1826 * state before it received the OPEN_CONFIRM.
1827 * Recover by retrying the request as per the discussion
1828 * on Page 181 of RFC3530.
1829 */
1830 if (status == -NFS4ERR_BAD_STATEID) {
1831 exception.retry = 1;
1832 continue;
1833 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001834 if (status == -EAGAIN) {
1835 /* We must have found a delegation */
1836 exception.retry = 1;
1837 continue;
1838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1840 status, &exception));
1841 } while (exception.retry);
1842 return res;
1843}
1844
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001845static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1846 struct nfs_fattr *fattr, struct iattr *sattr,
1847 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001849 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001851 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 .iap = sattr,
1853 .server = server,
1854 .bitmask = server->attr_bitmask,
1855 };
1856 struct nfs_setattrres res = {
1857 .fattr = fattr,
1858 .server = server,
1859 };
1860 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001861 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1862 .rpc_argp = &arg,
1863 .rpc_resp = &res,
1864 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001866 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001867 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Trond Myklebust0e574af2005-10-27 22:12:38 -04001869 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001871 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1872 /* Use that stateid */
1873 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001874 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001875 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1877
Bryan Schumaker7c513052011-03-24 17:12:24 +00001878 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001879 if (status == 0 && state != NULL)
1880 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001881 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001884static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1885 struct nfs_fattr *fattr, struct iattr *sattr,
1886 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001888 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 struct nfs4_exception exception = { };
1890 int err;
1891 do {
1892 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001893 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 &exception);
1895 } while (exception.retry);
1896 return err;
1897}
1898
1899struct nfs4_closedata {
1900 struct inode *inode;
1901 struct nfs4_state *state;
1902 struct nfs_closeargs arg;
1903 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001904 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001905 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001906 bool roc;
1907 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908};
1909
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001910static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001911{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001912 struct nfs4_closedata *calldata = data;
1913 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001914 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001915
Fred Isamanf7e89172011-01-06 11:36:32 +00001916 if (calldata->roc)
1917 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001918 nfs4_put_open_state(calldata->state);
1919 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001920 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001921 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001922 kfree(calldata);
1923}
1924
Trond Myklebust88069f72009-12-08 08:33:16 -05001925static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1926 fmode_t fmode)
1927{
1928 spin_lock(&state->owner->so_lock);
1929 if (!(fmode & FMODE_READ))
1930 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1931 if (!(fmode & FMODE_WRITE))
1932 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1933 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1934 spin_unlock(&state->owner->so_lock);
1935}
1936
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001937static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001939 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 struct nfs_server *server = NFS_SERVER(calldata->inode);
1942
Trond Myklebust14516c32010-07-31 14:29:06 -04001943 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1944 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 /* hmm. we are done with the inode, and in the process of freeing
1946 * the state_owner. we keep this around to process errors
1947 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 switch (task->tk_status) {
1949 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001950 if (calldata->roc)
1951 pnfs_roc_set_barrier(state->inode,
1952 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001953 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001954 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001955 nfs4_close_clear_stateid_flags(state,
1956 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 break;
1958 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001959 case -NFS4ERR_OLD_STATEID:
1960 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001962 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001963 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001965 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1966 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001968 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001969 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
1971
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001972static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001974 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001975 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001976 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001977
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001978 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001979 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001980
Trond Myklebust88069f72009-12-08 08:33:16 -05001981 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1982 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001983 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001984 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001985 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001986 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001987 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1988 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1989 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001990 }
1991 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001992 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1993 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1994 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001995 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001996 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001997 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001998
1999 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002000 /* Note: exit _without_ calling nfs4_close_done */
2001 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07002002 return;
2003 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002004
Fred Isamanf7e89172011-01-06 11:36:32 +00002005 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002006 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002007 if (calldata->roc &&
2008 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2009 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2010 task, NULL);
2011 return;
2012 }
2013 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002014
Trond Myklebust516a6af2005-10-27 22:12:41 -04002015 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002016 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002017 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04002018 &calldata->arg.seq_args, &calldata->res.seq_res,
2019 1, task))
2020 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002021 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
2023
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002024static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002025 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002026 .rpc_call_done = nfs4_close_done,
2027 .rpc_release = nfs4_free_closedata,
2028};
2029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030/*
2031 * It is possible for data to be read/written from a mem-mapped file
2032 * after the sys_close call (which hits the vfs layer as a flush).
2033 * This means that we can't safely call nfsv4 close on a file until
2034 * the inode is cleared. This in turn means that we are not good
2035 * NFSv4 citizens - we do not indicate to the server to update the file's
2036 * share state even when we are done with one of the three share
2037 * stateid's in the inode.
2038 *
2039 * NOTE: Caller must be holding the sp->so_owner semaphore!
2040 */
Al Viro643168c2011-06-22 18:20:23 -04002041int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002043 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002045 struct nfs4_state_owner *sp = state->owner;
2046 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002047 struct rpc_message msg = {
2048 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2049 .rpc_cred = state->owner->so_cred,
2050 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002051 struct rpc_task_setup task_setup_data = {
2052 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002053 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002054 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002055 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002056 .flags = RPC_TASK_ASYNC,
2057 };
Trond Myklebust95121352005-10-18 14:20:12 -07002058 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002060 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002062 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002063 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002065 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002066 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002068 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002069 if (calldata->arg.seqid == NULL)
2070 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002071 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002072 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002073 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002074 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002075 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002076 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002077 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002078
Trond Myklebust1174dd12010-12-21 10:52:24 -05002079 msg.rpc_argp = &calldata->arg;
2080 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002081 task_setup_data.callback_data = calldata;
2082 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002083 if (IS_ERR(task))
2084 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002085 status = 0;
2086 if (wait)
2087 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002088 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002089 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002090out_free_calldata:
2091 kfree(calldata);
2092out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002093 if (roc)
2094 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002095 nfs4_put_open_state(state);
2096 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002097 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098}
2099
Trond Myklebust2b484292010-09-17 10:56:51 -04002100static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002101nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 struct nfs4_state *state;
2104
Trond Myklebust565277f2007-10-15 18:17:53 -04002105 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002106 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002107 if (IS_ERR(state))
2108 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002109 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002110 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
Trond Myklebust1185a552009-12-03 15:54:02 -05002113static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002114{
2115 if (ctx->state == NULL)
2116 return;
2117 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002118 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002119 else
Al Viro643168c2011-06-22 18:20:23 -04002120 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002121}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
2123static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2124{
Benny Halevy43652ad2009-04-01 09:21:54 -04002125 struct nfs4_server_caps_arg args = {
2126 .fhandle = fhandle,
2127 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 struct nfs4_server_caps_res res = {};
2129 struct rpc_message msg = {
2130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002131 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 .rpc_resp = &res,
2133 };
2134 int status;
2135
Bryan Schumaker7c513052011-03-24 17:12:24 +00002136 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (status == 0) {
2138 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002139 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2140 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2141 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2142 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2143 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2145 server->caps |= NFS_CAP_ACLS;
2146 if (res.has_links != 0)
2147 server->caps |= NFS_CAP_HARDLINKS;
2148 if (res.has_symlinks != 0)
2149 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002150 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2151 server->caps |= NFS_CAP_FILEID;
2152 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2153 server->caps |= NFS_CAP_MODE;
2154 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2155 server->caps |= NFS_CAP_NLINK;
2156 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2157 server->caps |= NFS_CAP_OWNER;
2158 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2159 server->caps |= NFS_CAP_OWNER_GROUP;
2160 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2161 server->caps |= NFS_CAP_ATIME;
2162 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2163 server->caps |= NFS_CAP_CTIME;
2164 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2165 server->caps |= NFS_CAP_MTIME;
2166
Trond Myklebusta65318b2009-03-11 14:10:28 -04002167 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2168 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2169 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 server->acl_bitmask = res.acl_bitmask;
2171 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 return status;
2174}
2175
Trond Myklebust55a97592006-06-09 09:34:19 -04002176int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
2178 struct nfs4_exception exception = { };
2179 int err;
2180 do {
2181 err = nfs4_handle_exception(server,
2182 _nfs4_server_capabilities(server, fhandle),
2183 &exception);
2184 } while (exception.retry);
2185 return err;
2186}
2187
2188static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2189 struct nfs_fsinfo *info)
2190{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 struct nfs4_lookup_root_arg args = {
2192 .bitmask = nfs4_fattr_bitmap,
2193 };
2194 struct nfs4_lookup_res res = {
2195 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002196 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 .fh = fhandle,
2198 };
2199 struct rpc_message msg = {
2200 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2201 .rpc_argp = &args,
2202 .rpc_resp = &res,
2203 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002204
Trond Myklebust0e574af2005-10-27 22:12:38 -04002205 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002206 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207}
2208
2209static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2210 struct nfs_fsinfo *info)
2211{
2212 struct nfs4_exception exception = { };
2213 int err;
2214 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002215 err = _nfs4_lookup_root(server, fhandle, info);
2216 switch (err) {
2217 case 0:
2218 case -NFS4ERR_WRONGSEC:
2219 break;
2220 default:
2221 err = nfs4_handle_exception(server, err, &exception);
2222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 } while (exception.retry);
2224 return err;
2225}
2226
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002227static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2228 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2229{
2230 struct rpc_auth *auth;
2231 int ret;
2232
2233 auth = rpcauth_create(flavor, server->client);
2234 if (!auth) {
2235 ret = -EIO;
2236 goto out;
2237 }
2238 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002239out:
2240 return ret;
2241}
2242
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002243static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002244 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002246 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002247 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002249 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2250 flav_array[len] = RPC_AUTH_NULL;
2251 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002252
2253 for (i = 0; i < len; i++) {
2254 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002255 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002256 continue;
2257 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002258 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002259 /*
2260 * -EACCESS could mean that the user doesn't have correct permissions
2261 * to access the mount. It could also mean that we tried to mount
2262 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2263 * existing mount programs don't handle -EACCES very well so it should
2264 * be mapped to -EPERM instead.
2265 */
2266 if (status == -EACCES)
2267 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002268 return status;
2269}
2270
2271/*
2272 * get the file handle for the "/" directory on the server
2273 */
2274static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2275 struct nfs_fsinfo *info)
2276{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002277 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002278 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002279 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2280 /*
2281 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2282 * by nfs4_map_errors() as this function exits.
2283 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002284 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 if (status == 0)
2286 status = nfs4_server_capabilities(server, fhandle);
2287 if (status == 0)
2288 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002289 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290}
2291
Andy Adamson533eb462011-06-13 18:25:56 -04002292static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002293/*
2294 * Get locations and (maybe) other attributes of a referral.
2295 * Note that we'll actually follow the referral later when
2296 * we detect fsid mismatch in inode revalidation
2297 */
Andy Adamson533eb462011-06-13 18:25:56 -04002298static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2299 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002300{
2301 int status = -ENOMEM;
2302 struct page *page = NULL;
2303 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002304
2305 page = alloc_page(GFP_KERNEL);
2306 if (page == NULL)
2307 goto out;
2308 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2309 if (locations == NULL)
2310 goto out;
2311
Trond Myklebustc228fd32007-01-13 02:28:11 -05002312 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002313 if (status != 0)
2314 goto out;
2315 /* Make sure server returned a different fsid for the referral */
2316 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002317 dprintk("%s: server did not return a different fsid for"
2318 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002319 status = -EIO;
2320 goto out;
2321 }
Andy Adamson533eb462011-06-13 18:25:56 -04002322 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2323 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002324
Andy Adamson533eb462011-06-13 18:25:56 -04002325 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002326 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002327 memset(fhandle, 0, sizeof(struct nfs_fh));
2328out:
2329 if (page)
2330 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002331 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002332 return status;
2333}
2334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2336{
2337 struct nfs4_getattr_arg args = {
2338 .fh = fhandle,
2339 .bitmask = server->attr_bitmask,
2340 };
2341 struct nfs4_getattr_res res = {
2342 .fattr = fattr,
2343 .server = server,
2344 };
2345 struct rpc_message msg = {
2346 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2347 .rpc_argp = &args,
2348 .rpc_resp = &res,
2349 };
2350
Trond Myklebust0e574af2005-10-27 22:12:38 -04002351 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002352 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353}
2354
2355static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2356{
2357 struct nfs4_exception exception = { };
2358 int err;
2359 do {
2360 err = nfs4_handle_exception(server,
2361 _nfs4_proc_getattr(server, fhandle, fattr),
2362 &exception);
2363 } while (exception.retry);
2364 return err;
2365}
2366
2367/*
2368 * The file is not closed if it is opened due to the a request to change
2369 * the size of the file. The open call will not be needed once the
2370 * VFS layer lookup-intents are implemented.
2371 *
2372 * Close is called when the inode is destroyed.
2373 * If we haven't opened the file for O_WRONLY, we
2374 * need to in the size_change case to obtain a stateid.
2375 *
2376 * Got race?
2377 * Because OPEN is always done by name in nfsv4, it is
2378 * possible that we opened a different file by the same
2379 * name. We can recognize this race condition, but we
2380 * can't do anything about it besides returning an error.
2381 *
2382 * This will be fixed with VFS changes (lookup-intent).
2383 */
2384static int
2385nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2386 struct iattr *sattr)
2387{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002388 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002389 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002390 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 int status;
2392
Benny Halevy8a1636c2010-07-14 15:43:57 -04002393 if (pnfs_ld_layoutret_on_setattr(inode))
2394 pnfs_return_layout(inode);
2395
Trond Myklebust0e574af2005-10-27 22:12:38 -04002396 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Trond Myklebustd5308382005-11-04 15:33:38 -05002398 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002399 if (sattr->ia_valid & ATTR_FILE) {
2400 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002401
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002402 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002403 if (ctx) {
2404 cred = ctx->cred;
2405 state = ctx->state;
2406 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002407 }
2408
2409 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002410 if (status == 0)
2411 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 return status;
2413}
2414
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002415static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2416 const struct qstr *name, struct nfs_fh *fhandle,
2417 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002418{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002419 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002420 int status;
2421 struct nfs4_lookup_arg args = {
2422 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002423 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002424 .name = name,
2425 };
2426 struct nfs4_lookup_res res = {
2427 .server = server,
2428 .fattr = fattr,
2429 .fh = fhandle,
2430 };
2431 struct rpc_message msg = {
2432 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2433 .rpc_argp = &args,
2434 .rpc_resp = &res,
2435 };
2436
2437 nfs_fattr_init(fattr);
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002440 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 dprintk("NFS reply lookup: %d\n", status);
2442 return status;
2443}
2444
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002445void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2446{
2447 memset(fh, 0, sizeof(struct nfs_fh));
2448 fattr->fsid.major = 1;
2449 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2450 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2451 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2452 fattr->nlink = 2;
2453}
2454
Bryan Schumaker7c513052011-03-24 17:12:24 +00002455static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2456 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457{
2458 struct nfs4_exception exception = { };
2459 int err;
2460 do {
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002461 int status;
2462
2463 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2464 switch (status) {
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002465 case -NFS4ERR_BADNAME:
2466 return -ENOENT;
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002467 case -NFS4ERR_MOVED:
Trond Myklebusta6f498a2011-11-07 15:50:32 -05002468 return nfs4_get_referral(dir, name, fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002469 case -NFS4ERR_WRONGSEC:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002470 nfs_fixup_secinfo_attributes(fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002471 }
2472 err = nfs4_handle_exception(NFS_SERVER(dir),
2473 status, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 } while (exception.retry);
2475 return err;
2476}
2477
2478static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2479{
Trond Myklebust76b32992007-08-10 17:45:11 -04002480 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 struct nfs4_accessargs args = {
2482 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002483 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002485 struct nfs4_accessres res = {
2486 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002487 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 struct rpc_message msg = {
2489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2490 .rpc_argp = &args,
2491 .rpc_resp = &res,
2492 .rpc_cred = entry->cred,
2493 };
2494 int mode = entry->mask;
2495 int status;
2496
2497 /*
2498 * Determine which access bits we want to ask for...
2499 */
2500 if (mode & MAY_READ)
2501 args.access |= NFS4_ACCESS_READ;
2502 if (S_ISDIR(inode->i_mode)) {
2503 if (mode & MAY_WRITE)
2504 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2505 if (mode & MAY_EXEC)
2506 args.access |= NFS4_ACCESS_LOOKUP;
2507 } else {
2508 if (mode & MAY_WRITE)
2509 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2510 if (mode & MAY_EXEC)
2511 args.access |= NFS4_ACCESS_EXECUTE;
2512 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002513
2514 res.fattr = nfs_alloc_fattr();
2515 if (res.fattr == NULL)
2516 return -ENOMEM;
2517
Bryan Schumaker7c513052011-03-24 17:12:24 +00002518 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 if (!status) {
2520 entry->mask = 0;
2521 if (res.access & NFS4_ACCESS_READ)
2522 entry->mask |= MAY_READ;
2523 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2524 entry->mask |= MAY_WRITE;
2525 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2526 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002527 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002529 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return status;
2531}
2532
2533static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2534{
2535 struct nfs4_exception exception = { };
2536 int err;
2537 do {
2538 err = nfs4_handle_exception(NFS_SERVER(inode),
2539 _nfs4_proc_access(inode, entry),
2540 &exception);
2541 } while (exception.retry);
2542 return err;
2543}
2544
2545/*
2546 * TODO: For the time being, we don't try to get any attributes
2547 * along with any of the zero-copy operations READ, READDIR,
2548 * READLINK, WRITE.
2549 *
2550 * In the case of the first three, we want to put the GETATTR
2551 * after the read-type operation -- this is because it is hard
2552 * to predict the length of a GETATTR response in v4, and thus
2553 * align the READ data correctly. This means that the GETATTR
2554 * may end up partially falling into the page cache, and we should
2555 * shift it into the 'tail' of the xdr_buf before processing.
2556 * To do this efficiently, we need to know the total length
2557 * of data received, which doesn't seem to be available outside
2558 * of the RPC layer.
2559 *
2560 * In the case of WRITE, we also want to put the GETATTR after
2561 * the operation -- in this case because we want to make sure
2562 * we get the post-operation mtime and size. This means that
2563 * we can't use xdr_encode_pages() as written: we need a variant
2564 * of it which would leave room in the 'tail' iovec.
2565 *
2566 * Both of these changes to the XDR layer would in fact be quite
2567 * minor, but I decided to leave them for a subsequent patch.
2568 */
2569static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2570 unsigned int pgbase, unsigned int pglen)
2571{
2572 struct nfs4_readlink args = {
2573 .fh = NFS_FH(inode),
2574 .pgbase = pgbase,
2575 .pglen = pglen,
2576 .pages = &page,
2577 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002578 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 struct rpc_message msg = {
2580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2581 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002582 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 };
2584
Bryan Schumaker7c513052011-03-24 17:12:24 +00002585 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586}
2587
2588static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2589 unsigned int pgbase, unsigned int pglen)
2590{
2591 struct nfs4_exception exception = { };
2592 int err;
2593 do {
2594 err = nfs4_handle_exception(NFS_SERVER(inode),
2595 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2596 &exception);
2597 } while (exception.retry);
2598 return err;
2599}
2600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601/*
2602 * Got race?
2603 * We will need to arrange for the VFS layer to provide an atomic open.
2604 * Until then, this create/open method is prone to inefficiency and race
2605 * conditions due to the lookup, create, and open VFS calls from sys_open()
2606 * placed on the wire.
2607 *
2608 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2609 * The file will be opened again in the subsequent VFS open call
2610 * (nfs4_proc_file_open).
2611 *
2612 * The open for read will just hang around to be used by any process that
2613 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2614 */
2615
2616static int
2617nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002618 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619{
Al Viro82a2c1b2011-06-22 18:30:55 -04002620 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002622 struct rpc_cred *cred = NULL;
2623 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 int status = 0;
2625
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002626 if (ctx != NULL) {
2627 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002628 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002629 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002631 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002632 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002633 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 if (IS_ERR(state)) {
2635 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002636 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002638 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002639 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002640 if (ctx != NULL)
2641 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002642 else
Al Viro643168c2011-06-22 18:20:23 -04002643 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644out:
2645 return status;
2646}
2647
2648static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2649{
Trond Myklebust16e42952005-10-27 22:12:44 -04002650 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002651 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002653 .name.len = name->len,
2654 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002655 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002657 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002658 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002659 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002661 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2662 .rpc_argp = &args,
2663 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002665 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Trond Myklebustd3468902010-04-16 16:22:50 -04002667 res.dir_attr = nfs_alloc_fattr();
2668 if (res.dir_attr == NULL)
2669 goto out;
2670
Bryan Schumaker7c513052011-03-24 17:12:24 +00002671 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002672 if (status == 0) {
2673 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002674 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002675 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002676 nfs_free_fattr(res.dir_attr);
2677out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 return status;
2679}
2680
2681static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2682{
2683 struct nfs4_exception exception = { };
2684 int err;
2685 do {
2686 err = nfs4_handle_exception(NFS_SERVER(dir),
2687 _nfs4_proc_remove(dir, name),
2688 &exception);
2689 } while (exception.retry);
2690 return err;
2691}
2692
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002693static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002695 struct nfs_server *server = NFS_SERVER(dir);
2696 struct nfs_removeargs *args = msg->rpc_argp;
2697 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
Trond Myklebusta65318b2009-03-11 14:10:28 -04002699 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002700 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002701 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703}
2704
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002705static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002707 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2708
Trond Myklebust14516c32010-07-31 14:29:06 -04002709 if (!nfs4_sequence_done(task, &res->seq_res))
2710 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002711 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002712 return 0;
2713 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002714 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002715 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716}
2717
Jeff Laytond3d41522010-09-17 17:31:57 -04002718static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2719{
2720 struct nfs_server *server = NFS_SERVER(dir);
2721 struct nfs_renameargs *arg = msg->rpc_argp;
2722 struct nfs_renameres *res = msg->rpc_resp;
2723
2724 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2725 arg->bitmask = server->attr_bitmask;
2726 res->server = server;
2727}
2728
2729static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2730 struct inode *new_dir)
2731{
2732 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2733
2734 if (!nfs4_sequence_done(task, &res->seq_res))
2735 return 0;
2736 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2737 return 0;
2738
2739 update_changeattr(old_dir, &res->old_cinfo);
2740 nfs_post_op_update_inode(old_dir, res->old_fattr);
2741 update_changeattr(new_dir, &res->new_cinfo);
2742 nfs_post_op_update_inode(new_dir, res->new_fattr);
2743 return 1;
2744}
2745
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2747 struct inode *new_dir, struct qstr *new_name)
2748{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002749 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002750 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 .old_dir = NFS_FH(old_dir),
2752 .new_dir = NFS_FH(new_dir),
2753 .old_name = old_name,
2754 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002755 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002757 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002758 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002759 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 struct rpc_message msg = {
2761 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2762 .rpc_argp = &arg,
2763 .rpc_resp = &res,
2764 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002765 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Trond Myklebust011fff72010-04-16 16:22:49 -04002767 res.old_fattr = nfs_alloc_fattr();
2768 res.new_fattr = nfs_alloc_fattr();
2769 if (res.old_fattr == NULL || res.new_fattr == NULL)
2770 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Bryan Schumaker7c513052011-03-24 17:12:24 +00002772 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 if (!status) {
2774 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002775 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002777 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002779out:
2780 nfs_free_fattr(res.new_fattr);
2781 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 return status;
2783}
2784
2785static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2786 struct inode *new_dir, struct qstr *new_name)
2787{
2788 struct nfs4_exception exception = { };
2789 int err;
2790 do {
2791 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2792 _nfs4_proc_rename(old_dir, old_name,
2793 new_dir, new_name),
2794 &exception);
2795 } while (exception.retry);
2796 return err;
2797}
2798
2799static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2800{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002801 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 struct nfs4_link_arg arg = {
2803 .fh = NFS_FH(inode),
2804 .dir_fh = NFS_FH(dir),
2805 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002806 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002808 struct nfs4_link_res res = {
2809 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002810 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 struct rpc_message msg = {
2812 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2813 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002814 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002816 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Trond Myklebust136f2622010-04-16 16:22:49 -04002818 res.fattr = nfs_alloc_fattr();
2819 res.dir_attr = nfs_alloc_fattr();
2820 if (res.fattr == NULL || res.dir_attr == NULL)
2821 goto out;
2822
Bryan Schumaker7c513052011-03-24 17:12:24 +00002823 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002824 if (!status) {
2825 update_changeattr(dir, &res.cinfo);
2826 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002827 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002828 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002829out:
2830 nfs_free_fattr(res.dir_attr);
2831 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 return status;
2833}
2834
2835static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2836{
2837 struct nfs4_exception exception = { };
2838 int err;
2839 do {
2840 err = nfs4_handle_exception(NFS_SERVER(inode),
2841 _nfs4_proc_link(inode, dir, name),
2842 &exception);
2843 } while (exception.retry);
2844 return err;
2845}
2846
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002847struct nfs4_createdata {
2848 struct rpc_message msg;
2849 struct nfs4_create_arg arg;
2850 struct nfs4_create_res res;
2851 struct nfs_fh fh;
2852 struct nfs_fattr fattr;
2853 struct nfs_fattr dir_fattr;
2854};
2855
2856static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2857 struct qstr *name, struct iattr *sattr, u32 ftype)
2858{
2859 struct nfs4_createdata *data;
2860
2861 data = kzalloc(sizeof(*data), GFP_KERNEL);
2862 if (data != NULL) {
2863 struct nfs_server *server = NFS_SERVER(dir);
2864
2865 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2866 data->msg.rpc_argp = &data->arg;
2867 data->msg.rpc_resp = &data->res;
2868 data->arg.dir_fh = NFS_FH(dir);
2869 data->arg.server = server;
2870 data->arg.name = name;
2871 data->arg.attrs = sattr;
2872 data->arg.ftype = ftype;
2873 data->arg.bitmask = server->attr_bitmask;
2874 data->res.server = server;
2875 data->res.fh = &data->fh;
2876 data->res.fattr = &data->fattr;
2877 data->res.dir_fattr = &data->dir_fattr;
2878 nfs_fattr_init(data->res.fattr);
2879 nfs_fattr_init(data->res.dir_fattr);
2880 }
2881 return data;
2882}
2883
2884static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2885{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002886 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002887 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002888 if (status == 0) {
2889 update_changeattr(dir, &data->res.dir_cinfo);
2890 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2891 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2892 }
2893 return status;
2894}
2895
2896static void nfs4_free_createdata(struct nfs4_createdata *data)
2897{
2898 kfree(data);
2899}
2900
Chuck Lever4f390c12006-08-22 20:06:22 -04002901static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002902 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002904 struct nfs4_createdata *data;
2905 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Chuck Lever94a6d752006-08-22 20:06:23 -04002907 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002908 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002909
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002910 status = -ENOMEM;
2911 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2912 if (data == NULL)
2913 goto out;
2914
2915 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2916 data->arg.u.symlink.pages = &page;
2917 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002919 status = nfs4_do_create(dir, dentry, data);
2920
2921 nfs4_free_createdata(data);
2922out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 return status;
2924}
2925
Chuck Lever4f390c12006-08-22 20:06:22 -04002926static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002927 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
2929 struct nfs4_exception exception = { };
2930 int err;
2931 do {
2932 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002933 _nfs4_proc_symlink(dir, dentry, page,
2934 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 &exception);
2936 } while (exception.retry);
2937 return err;
2938}
2939
2940static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2941 struct iattr *sattr)
2942{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002943 struct nfs4_createdata *data;
2944 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002946 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2947 if (data == NULL)
2948 goto out;
2949
2950 status = nfs4_do_create(dir, dentry, data);
2951
2952 nfs4_free_createdata(data);
2953out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 return status;
2955}
2956
2957static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2958 struct iattr *sattr)
2959{
2960 struct nfs4_exception exception = { };
2961 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002962
2963 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 do {
2965 err = nfs4_handle_exception(NFS_SERVER(dir),
2966 _nfs4_proc_mkdir(dir, dentry, sattr),
2967 &exception);
2968 } while (exception.retry);
2969 return err;
2970}
2971
2972static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002973 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974{
2975 struct inode *dir = dentry->d_inode;
2976 struct nfs4_readdir_arg args = {
2977 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002978 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 .pgbase = 0,
2980 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002981 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002982 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 };
2984 struct nfs4_readdir_res res;
2985 struct rpc_message msg = {
2986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2987 .rpc_argp = &args,
2988 .rpc_resp = &res,
2989 .rpc_cred = cred,
2990 };
2991 int status;
2992
Harvey Harrison3110ff82008-05-02 13:42:44 -07002993 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002994 dentry->d_parent->d_name.name,
2995 dentry->d_name.name,
2996 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2998 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00002999 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05003000 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003002 status += args.pgbase;
3003 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003004
3005 nfs_invalidate_atime(dir);
3006
Harvey Harrison3110ff82008-05-02 13:42:44 -07003007 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 return status;
3009}
3010
3011static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003012 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013{
3014 struct nfs4_exception exception = { };
3015 int err;
3016 do {
3017 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3018 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003019 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 &exception);
3021 } while (exception.retry);
3022 return err;
3023}
3024
3025static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3026 struct iattr *sattr, dev_t rdev)
3027{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003028 struct nfs4_createdata *data;
3029 int mode = sattr->ia_mode;
3030 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
3032 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3033 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003034
3035 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3036 if (data == NULL)
3037 goto out;
3038
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003040 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003042 data->arg.ftype = NF4BLK;
3043 data->arg.u.device.specdata1 = MAJOR(rdev);
3044 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 }
3046 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003047 data->arg.ftype = NF4CHR;
3048 data->arg.u.device.specdata1 = MAJOR(rdev);
3049 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003052 status = nfs4_do_create(dir, dentry, data);
3053
3054 nfs4_free_createdata(data);
3055out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 return status;
3057}
3058
3059static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3060 struct iattr *sattr, dev_t rdev)
3061{
3062 struct nfs4_exception exception = { };
3063 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003064
3065 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 do {
3067 err = nfs4_handle_exception(NFS_SERVER(dir),
3068 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3069 &exception);
3070 } while (exception.retry);
3071 return err;
3072}
3073
3074static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3075 struct nfs_fsstat *fsstat)
3076{
3077 struct nfs4_statfs_arg args = {
3078 .fh = fhandle,
3079 .bitmask = server->attr_bitmask,
3080 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003081 struct nfs4_statfs_res res = {
3082 .fsstat = fsstat,
3083 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 struct rpc_message msg = {
3085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3086 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003087 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 };
3089
Trond Myklebust0e574af2005-10-27 22:12:38 -04003090 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003091 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092}
3093
3094static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3095{
3096 struct nfs4_exception exception = { };
3097 int err;
3098 do {
3099 err = nfs4_handle_exception(server,
3100 _nfs4_proc_statfs(server, fhandle, fsstat),
3101 &exception);
3102 } while (exception.retry);
3103 return err;
3104}
3105
3106static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3107 struct nfs_fsinfo *fsinfo)
3108{
3109 struct nfs4_fsinfo_arg args = {
3110 .fh = fhandle,
3111 .bitmask = server->attr_bitmask,
3112 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003113 struct nfs4_fsinfo_res res = {
3114 .fsinfo = fsinfo,
3115 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 struct rpc_message msg = {
3117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3118 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003119 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 };
3121
Bryan Schumaker7c513052011-03-24 17:12:24 +00003122 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123}
3124
3125static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3126{
3127 struct nfs4_exception exception = { };
3128 int err;
3129
3130 do {
3131 err = nfs4_handle_exception(server,
3132 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3133 &exception);
3134 } while (exception.retry);
3135 return err;
3136}
3137
3138static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3139{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003140 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3142}
3143
3144static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3145 struct nfs_pathconf *pathconf)
3146{
3147 struct nfs4_pathconf_arg args = {
3148 .fh = fhandle,
3149 .bitmask = server->attr_bitmask,
3150 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003151 struct nfs4_pathconf_res res = {
3152 .pathconf = pathconf,
3153 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 struct rpc_message msg = {
3155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3156 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003157 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 };
3159
3160 /* None of the pathconf attributes are mandatory to implement */
3161 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3162 memset(pathconf, 0, sizeof(*pathconf));
3163 return 0;
3164 }
3165
Trond Myklebust0e574af2005-10-27 22:12:38 -04003166 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003167 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168}
3169
3170static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3171 struct nfs_pathconf *pathconf)
3172{
3173 struct nfs4_exception exception = { };
3174 int err;
3175
3176 do {
3177 err = nfs4_handle_exception(server,
3178 _nfs4_proc_pathconf(server, fhandle, pathconf),
3179 &exception);
3180 } while (exception.retry);
3181 return err;
3182}
3183
Benny Halevyd20581a2011-05-22 19:52:03 +03003184void __nfs4_read_done_cb(struct nfs_read_data *data)
3185{
3186 nfs_invalidate_atime(data->inode);
3187}
3188
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003189static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
Trond Myklebustec06c092006-03-20 13:44:27 -05003191 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003193 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003194 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003195 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003197
Benny Halevyd20581a2011-05-22 19:52:03 +03003198 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003200 renew_lease(server, data->timestamp);
3201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202}
3203
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003204static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3205{
3206
3207 dprintk("--> %s\n", __func__);
3208
3209 if (!nfs4_sequence_done(task, &data->res.seq_res))
3210 return -EAGAIN;
3211
Benny Halevyd20581a2011-05-22 19:52:03 +03003212 return data->read_done_cb ? data->read_done_cb(task, data) :
3213 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003214}
3215
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003216static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003219 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003220 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003223/* Reset the the nfs_read_data to send the read to the MDS. */
3224void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3225{
3226 dprintk("%s Reset task for i/o through\n", __func__);
3227 put_lseg(data->lseg);
3228 data->lseg = NULL;
3229 /* offsets will differ in the dense stripe case */
3230 data->args.offset = data->mds_offset;
3231 data->ds_clp = NULL;
3232 data->args.fh = NFS_FH(data->inode);
3233 data->read_done_cb = nfs4_read_done_cb;
3234 task->tk_ops = data->mds_ops;
3235 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3236}
3237EXPORT_SYMBOL_GPL(nfs4_reset_read);
3238
Fred Isamanb029bc92011-03-03 15:13:42 +00003239static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 struct inode *inode = data->inode;
3242
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003243 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003244 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003245 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003247 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003249 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003250 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252}
3253
Fred Isamanb029bc92011-03-03 15:13:42 +00003254static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3255{
3256 if (!nfs4_sequence_done(task, &data->res.seq_res))
3257 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003258 return data->write_done_cb ? data->write_done_cb(task, data) :
3259 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003260}
3261
Fred Isamana69aef12011-03-03 15:13:47 +00003262/* Reset the the nfs_write_data to send the write to the MDS. */
3263void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3264{
3265 dprintk("%s Reset task for i/o through\n", __func__);
3266 put_lseg(data->lseg);
3267 data->lseg = NULL;
3268 data->ds_clp = NULL;
3269 data->write_done_cb = nfs4_write_done_cb;
3270 data->args.fh = NFS_FH(data->inode);
3271 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3272 data->args.offset = data->mds_offset;
3273 data->res.fattr = &data->fattr;
3274 task->tk_ops = data->mds_ops;
3275 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3276}
3277EXPORT_SYMBOL_GPL(nfs4_reset_write);
3278
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003279static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003281 struct nfs_server *server = NFS_SERVER(data->inode);
3282
Fred Isaman7ffd1062011-03-03 15:13:46 +00003283 if (data->lseg) {
3284 data->args.bitmask = NULL;
3285 data->res.fattr = NULL;
3286 } else
3287 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003288 if (!data->write_done_cb)
3289 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003290 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 data->timestamp = jiffies;
3292
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003293 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294}
3295
Fred Isaman5f452432011-03-23 13:27:46 +00003296static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003299
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003300 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003301 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003302 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003304 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306}
3307
Fred Isaman5f452432011-03-23 13:27:46 +00003308static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3309{
3310 if (!nfs4_sequence_done(task, &data->res.seq_res))
3311 return -EAGAIN;
3312 return data->write_done_cb(task, data);
3313}
3314
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003315static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003317 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003318
3319 if (data->lseg) {
3320 data->args.bitmask = NULL;
3321 data->res.fattr = NULL;
3322 } else
3323 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003324 if (!data->write_done_cb)
3325 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003326 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003327 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328}
3329
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003330struct nfs4_renewdata {
3331 struct nfs_client *client;
3332 unsigned long timestamp;
3333};
3334
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335/*
3336 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3337 * standalone procedure for queueing an asynchronous RENEW.
3338 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003339static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003340{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003341 struct nfs4_renewdata *data = calldata;
3342 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003343
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003344 if (atomic_read(&clp->cl_count) > 1)
3345 nfs4_schedule_state_renewal(clp);
3346 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003347 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003348}
3349
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003350static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003352 struct nfs4_renewdata *data = calldata;
3353 struct nfs_client *clp = data->client;
3354 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
3356 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003357 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003358 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3359 return;
3360 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003361 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003362 return;
3363 }
3364 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003366 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367}
3368
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003369static const struct rpc_call_ops nfs4_renew_ops = {
3370 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003371 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003372};
3373
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003374static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375{
3376 struct rpc_message msg = {
3377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3378 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003379 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003381 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003383 if (renew_flags == 0)
3384 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003385 if (!atomic_inc_not_zero(&clp->cl_count))
3386 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003387 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003388 if (data == NULL)
3389 return -ENOMEM;
3390 data->client = clp;
3391 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003393 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394}
3395
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003396static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397{
3398 struct rpc_message msg = {
3399 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3400 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003401 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 };
3403 unsigned long now = jiffies;
3404 int status;
3405
3406 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3407 if (status < 0)
3408 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003409 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 return 0;
3411}
3412
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003413static inline int nfs4_server_supports_acls(struct nfs_server *server)
3414{
3415 return (server->caps & NFS_CAP_ACLS)
3416 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3417 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3418}
3419
3420/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3421 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3422 * the stack.
3423 */
3424#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3425
3426static void buf_to_pages(const void *buf, size_t buflen,
3427 struct page **pages, unsigned int *pgbase)
3428{
3429 const void *p = buf;
3430
3431 *pgbase = offset_in_page(buf);
3432 p -= *pgbase;
3433 while (p < buf + buflen) {
3434 *(pages++) = virt_to_page(p);
3435 p += PAGE_CACHE_SIZE;
3436 }
3437}
3438
Neil Hormane9e3d722011-03-04 19:26:03 -05003439static int buf_to_pages_noslab(const void *buf, size_t buflen,
3440 struct page **pages, unsigned int *pgbase)
3441{
3442 struct page *newpage, **spages;
3443 int rc = 0;
3444 size_t len;
3445 spages = pages;
3446
3447 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003448 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003449 newpage = alloc_page(GFP_KERNEL);
3450
3451 if (newpage == NULL)
3452 goto unwind;
3453 memcpy(page_address(newpage), buf, len);
3454 buf += len;
3455 buflen -= len;
3456 *pages++ = newpage;
3457 rc++;
3458 } while (buflen != 0);
3459
3460 return rc;
3461
3462unwind:
3463 for(; rc > 0; rc--)
3464 __free_page(spages[rc-1]);
3465 return -ENOMEM;
3466}
3467
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003468struct nfs4_cached_acl {
3469 int cached;
3470 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003471 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003472};
3473
3474static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003475{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003476 struct nfs_inode *nfsi = NFS_I(inode);
3477
3478 spin_lock(&inode->i_lock);
3479 kfree(nfsi->nfs4_acl);
3480 nfsi->nfs4_acl = acl;
3481 spin_unlock(&inode->i_lock);
3482}
3483
3484static void nfs4_zap_acl_attr(struct inode *inode)
3485{
3486 nfs4_set_cached_acl(inode, NULL);
3487}
3488
3489static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3490{
3491 struct nfs_inode *nfsi = NFS_I(inode);
3492 struct nfs4_cached_acl *acl;
3493 int ret = -ENOENT;
3494
3495 spin_lock(&inode->i_lock);
3496 acl = nfsi->nfs4_acl;
3497 if (acl == NULL)
3498 goto out;
3499 if (buf == NULL) /* user is just asking for length */
3500 goto out_len;
3501 if (acl->cached == 0)
3502 goto out;
3503 ret = -ERANGE; /* see getxattr(2) man page */
3504 if (acl->len > buflen)
3505 goto out;
3506 memcpy(buf, acl->data, acl->len);
3507out_len:
3508 ret = acl->len;
3509out:
3510 spin_unlock(&inode->i_lock);
3511 return ret;
3512}
3513
3514static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3515{
3516 struct nfs4_cached_acl *acl;
3517
3518 if (buf && acl_len <= PAGE_SIZE) {
3519 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3520 if (acl == NULL)
3521 goto out;
3522 acl->cached = 1;
3523 memcpy(acl->data, buf, acl_len);
3524 } else {
3525 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3526 if (acl == NULL)
3527 goto out;
3528 acl->cached = 0;
3529 }
3530 acl->len = acl_len;
3531out:
3532 nfs4_set_cached_acl(inode, acl);
3533}
3534
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003535static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003536{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003537 struct page *pages[NFS4ACL_MAXPAGES];
3538 struct nfs_getaclargs args = {
3539 .fh = NFS_FH(inode),
3540 .acl_pages = pages,
3541 .acl_len = buflen,
3542 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003543 struct nfs_getaclres res = {
3544 .acl_len = buflen,
3545 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003546 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003547 struct rpc_message msg = {
3548 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3549 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003550 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003551 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003552 struct page *localpage = NULL;
3553 int ret;
3554
3555 if (buflen < PAGE_SIZE) {
3556 /* As long as we're doing a round trip to the server anyway,
3557 * let's be prepared for a page of acl data. */
3558 localpage = alloc_page(GFP_KERNEL);
3559 resp_buf = page_address(localpage);
3560 if (localpage == NULL)
3561 return -ENOMEM;
3562 args.acl_pages[0] = localpage;
3563 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003564 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003565 } else {
3566 resp_buf = buf;
3567 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3568 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00003569 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003570 if (ret)
3571 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003572 if (res.acl_len > args.acl_len)
3573 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003574 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003575 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003576 if (buf) {
3577 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003578 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003579 goto out_free;
3580 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003581 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003582 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003583 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003584out_free:
3585 if (localpage)
3586 __free_page(localpage);
3587 return ret;
3588}
3589
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003590static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3591{
3592 struct nfs4_exception exception = { };
3593 ssize_t ret;
3594 do {
3595 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3596 if (ret >= 0)
3597 break;
3598 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3599 } while (exception.retry);
3600 return ret;
3601}
3602
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003603static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3604{
3605 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003606 int ret;
3607
3608 if (!nfs4_server_supports_acls(server))
3609 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003610 ret = nfs_revalidate_inode(server, inode);
3611 if (ret < 0)
3612 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003613 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3614 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003615 ret = nfs4_read_cached_acl(inode, buf, buflen);
3616 if (ret != -ENOENT)
3617 return ret;
3618 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003619}
3620
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003621static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003622{
3623 struct nfs_server *server = NFS_SERVER(inode);
3624 struct page *pages[NFS4ACL_MAXPAGES];
3625 struct nfs_setaclargs arg = {
3626 .fh = NFS_FH(inode),
3627 .acl_pages = pages,
3628 .acl_len = buflen,
3629 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003630 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003631 struct rpc_message msg = {
3632 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3633 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003634 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003635 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003636 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003637
3638 if (!nfs4_server_supports_acls(server))
3639 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003640 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3641 if (i < 0)
3642 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003643 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003644 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003645
3646 /*
3647 * Free each page after tx, so the only ref left is
3648 * held by the network stack
3649 */
3650 for (; i > 0; i--)
3651 put_page(pages[i-1]);
3652
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003653 /*
3654 * Acl update can result in inode attribute update.
3655 * so mark the attribute cache invalid.
3656 */
3657 spin_lock(&inode->i_lock);
3658 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3659 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003660 nfs_access_zap_cache(inode);
3661 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003662 return ret;
3663}
3664
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003665static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3666{
3667 struct nfs4_exception exception = { };
3668 int err;
3669 do {
3670 err = nfs4_handle_exception(NFS_SERVER(inode),
3671 __nfs4_proc_set_acl(inode, buf, buflen),
3672 &exception);
3673 } while (exception.retry);
3674 return err;
3675}
3676
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003678nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003680 struct nfs_client *clp = server->nfs_client;
3681
3682 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 return 0;
3684 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003685 case -NFS4ERR_ADMIN_REVOKED:
3686 case -NFS4ERR_BAD_STATEID:
3687 case -NFS4ERR_OPENMODE:
3688 if (state == NULL)
3689 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003690 nfs4_schedule_stateid_recovery(server, state);
3691 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003692 case -NFS4ERR_EXPIRED:
3693 if (state != NULL)
3694 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003696 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003697 nfs4_schedule_lease_recovery(clp);
3698 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003699#if defined(CONFIG_NFS_V4_1)
3700 case -NFS4ERR_BADSESSION:
3701 case -NFS4ERR_BADSLOT:
3702 case -NFS4ERR_BAD_HIGH_SLOT:
3703 case -NFS4ERR_DEADSESSION:
3704 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3705 case -NFS4ERR_SEQ_FALSE_RETRY:
3706 case -NFS4ERR_SEQ_MISORDERED:
3707 dprintk("%s ERROR %d, Reset session\n", __func__,
3708 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003709 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003710 task->tk_status = 0;
3711 return -EAGAIN;
3712#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003714 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003715 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003716 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3718 task->tk_status = 0;
3719 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003720 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 case -NFS4ERR_OLD_STATEID:
3722 task->tk_status = 0;
3723 return -EAGAIN;
3724 }
3725 task->tk_status = nfs4_map_errors(task->tk_status);
3726 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003727wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003728 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003729 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3730 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3731 task->tk_status = 0;
3732 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733}
3734
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003735int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3736 unsigned short port, struct rpc_cred *cred,
3737 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738{
3739 nfs4_verifier sc_verifier;
3740 struct nfs4_setclientid setclientid = {
3741 .sc_verifier = &sc_verifier,
3742 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003743 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 };
3745 struct rpc_message msg = {
3746 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3747 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003748 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003749 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 };
Al Virobc4785c2006-10-19 23:28:51 -07003751 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 int loop = 0;
3753 int status;
3754
Al Virobc4785c2006-10-19 23:28:51 -07003755 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3757 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3758
3759 for(;;) {
3760 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003761 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003762 clp->cl_ipaddr,
3763 rpc_peeraddr2str(clp->cl_rpcclient,
3764 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003765 rpc_peeraddr2str(clp->cl_rpcclient,
3766 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003767 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 clp->cl_id_uniquifier);
3769 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003770 sizeof(setclientid.sc_netid),
3771 rpc_peeraddr2str(clp->cl_rpcclient,
3772 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003774 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 clp->cl_ipaddr, port >> 8, port & 255);
3776
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003777 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 if (status != -NFS4ERR_CLID_INUSE)
3779 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003780 if (loop != 0) {
3781 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003783 }
3784 ++loop;
3785 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 }
3787 return status;
3788}
3789
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003790int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003791 struct nfs4_setclientid_res *arg,
3792 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793{
3794 struct nfs_fsinfo fsinfo;
3795 struct rpc_message msg = {
3796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003797 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003799 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 };
3801 unsigned long now;
3802 int status;
3803
3804 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003805 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 if (status == 0) {
3807 spin_lock(&clp->cl_lock);
3808 clp->cl_lease_time = fsinfo.lease_time * HZ;
3809 clp->cl_last_renewal = now;
3810 spin_unlock(&clp->cl_lock);
3811 }
3812 return status;
3813}
3814
Trond Myklebustfe650402006-01-03 09:55:18 +01003815struct nfs4_delegreturndata {
3816 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003817 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003818 struct nfs_fh fh;
3819 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003820 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003821 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003822 int rpc_status;
3823};
3824
Trond Myklebustfe650402006-01-03 09:55:18 +01003825static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3826{
3827 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003828
Trond Myklebust14516c32010-07-31 14:29:06 -04003829 if (!nfs4_sequence_done(task, &data->res.seq_res))
3830 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003831
Ricardo Labiaga79708862009-12-07 09:23:21 -05003832 switch (task->tk_status) {
3833 case -NFS4ERR_STALE_STATEID:
3834 case -NFS4ERR_EXPIRED:
3835 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003836 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003837 break;
3838 default:
3839 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3840 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003841 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003842 return;
3843 }
3844 }
3845 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003846}
3847
3848static void nfs4_delegreturn_release(void *calldata)
3849{
Trond Myklebustfe650402006-01-03 09:55:18 +01003850 kfree(calldata);
3851}
3852
Andy Adamson938e1012009-04-01 09:22:28 -04003853#if defined(CONFIG_NFS_V4_1)
3854static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3855{
3856 struct nfs4_delegreturndata *d_data;
3857
3858 d_data = (struct nfs4_delegreturndata *)data;
3859
Trond Myklebust035168a2010-06-16 09:52:26 -04003860 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003861 &d_data->args.seq_args,
3862 &d_data->res.seq_res, 1, task))
3863 return;
3864 rpc_call_start(task);
3865}
3866#endif /* CONFIG_NFS_V4_1 */
3867
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003868static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003869#if defined(CONFIG_NFS_V4_1)
3870 .rpc_call_prepare = nfs4_delegreturn_prepare,
3871#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003872 .rpc_call_done = nfs4_delegreturn_done,
3873 .rpc_release = nfs4_delegreturn_release,
3874};
3875
Trond Myklebuste6f81072008-01-24 18:14:34 -05003876static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003877{
3878 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003879 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003880 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003881 struct rpc_message msg = {
3882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3883 .rpc_cred = cred,
3884 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003885 struct rpc_task_setup task_setup_data = {
3886 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003887 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003888 .callback_ops = &nfs4_delegreturn_ops,
3889 .flags = RPC_TASK_ASYNC,
3890 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003891 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003892
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003893 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003894 if (data == NULL)
3895 return -ENOMEM;
3896 data->args.fhandle = &data->fh;
3897 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003898 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003899 nfs_copy_fh(&data->fh, NFS_FH(inode));
3900 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003901 data->res.fattr = &data->fattr;
3902 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003903 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003904 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003905 data->rpc_status = 0;
3906
Trond Myklebustc970aa82007-07-14 15:39:59 -04003907 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003908 msg.rpc_argp = &data->args;
3909 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003910 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003911 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003912 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003913 if (!issync)
3914 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003915 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003916 if (status != 0)
3917 goto out;
3918 status = data->rpc_status;
3919 if (status != 0)
3920 goto out;
3921 nfs_refresh_inode(inode, &data->fattr);
3922out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003923 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003924 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925}
3926
Trond Myklebuste6f81072008-01-24 18:14:34 -05003927int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928{
3929 struct nfs_server *server = NFS_SERVER(inode);
3930 struct nfs4_exception exception = { };
3931 int err;
3932 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003933 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 switch (err) {
3935 case -NFS4ERR_STALE_STATEID:
3936 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 case 0:
3938 return 0;
3939 }
3940 err = nfs4_handle_exception(server, err, &exception);
3941 } while (exception.retry);
3942 return err;
3943}
3944
3945#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3946#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3947
3948/*
3949 * sleep, with exponential backoff, and retry the LOCK operation.
3950 */
3951static unsigned long
3952nfs4_set_lock_task_retry(unsigned long timeout)
3953{
Jeff Laytond3103102011-12-01 22:44:39 +01003954 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 timeout <<= 1;
3956 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3957 return NFS4_LOCK_MAXTIMEOUT;
3958 return timeout;
3959}
3960
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3962{
3963 struct inode *inode = state->inode;
3964 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003965 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003966 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003968 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003970 struct nfs_lockt_res res = {
3971 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 };
3973 struct rpc_message msg = {
3974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3975 .rpc_argp = &arg,
3976 .rpc_resp = &res,
3977 .rpc_cred = state->owner->so_cred,
3978 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 struct nfs4_lock_state *lsp;
3980 int status;
3981
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003982 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003983 status = nfs4_set_lock_state(state, request);
3984 if (status != 0)
3985 goto out;
3986 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003987 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05003988 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003989 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003990 switch (status) {
3991 case 0:
3992 request->fl_type = F_UNLCK;
3993 break;
3994 case -NFS4ERR_DENIED:
3995 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003997 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003998out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 return status;
4000}
4001
4002static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4003{
4004 struct nfs4_exception exception = { };
4005 int err;
4006
4007 do {
4008 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4009 _nfs4_proc_getlk(state, cmd, request),
4010 &exception);
4011 } while (exception.retry);
4012 return err;
4013}
4014
4015static int do_vfs_lock(struct file *file, struct file_lock *fl)
4016{
4017 int res = 0;
4018 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4019 case FL_POSIX:
4020 res = posix_lock_file_wait(file, fl);
4021 break;
4022 case FL_FLOCK:
4023 res = flock_lock_file_wait(file, fl);
4024 break;
4025 default:
4026 BUG();
4027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 return res;
4029}
4030
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004031struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004032 struct nfs_locku_args arg;
4033 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004034 struct nfs4_lock_state *lsp;
4035 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004036 struct file_lock fl;
4037 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004038 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004039};
4040
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004041static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4042 struct nfs_open_context *ctx,
4043 struct nfs4_lock_state *lsp,
4044 struct nfs_seqid *seqid)
4045{
4046 struct nfs4_unlockdata *p;
4047 struct inode *inode = lsp->ls_state->inode;
4048
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004049 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004050 if (p == NULL)
4051 return NULL;
4052 p->arg.fh = NFS_FH(inode);
4053 p->arg.fl = &p->fl;
4054 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004055 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004056 p->arg.stateid = &lsp->ls_stateid;
4057 p->lsp = lsp;
4058 atomic_inc(&lsp->ls_count);
4059 /* Ensure we don't close file until we're done freeing locks! */
4060 p->ctx = get_nfs_open_context(ctx);
4061 memcpy(&p->fl, fl, sizeof(p->fl));
4062 p->server = NFS_SERVER(inode);
4063 return p;
4064}
4065
Trond Myklebust06f814a2006-01-03 09:55:07 +01004066static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004067{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004068 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004069 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004070 nfs4_put_lock_state(calldata->lsp);
4071 put_nfs_open_context(calldata->ctx);
4072 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004073}
4074
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004075static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004076{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004077 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004078
Trond Myklebust14516c32010-07-31 14:29:06 -04004079 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4080 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004081 switch (task->tk_status) {
4082 case 0:
4083 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004084 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004085 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004086 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004087 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004088 case -NFS4ERR_BAD_STATEID:
4089 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004090 case -NFS4ERR_STALE_STATEID:
4091 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004092 break;
4093 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004094 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004095 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004096 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004097}
4098
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004099static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004100{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004101 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004103 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004104 return;
4105 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004106 /* Note: exit _without_ running nfs4_locku_done */
4107 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004108 return;
4109 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004110 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004111 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004112 &calldata->arg.seq_args,
4113 &calldata->res.seq_res, 1, task))
4114 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004115 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116}
4117
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004118static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004119 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004120 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004121 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004122};
4123
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004124static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4125 struct nfs_open_context *ctx,
4126 struct nfs4_lock_state *lsp,
4127 struct nfs_seqid *seqid)
4128{
4129 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004130 struct rpc_message msg = {
4131 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4132 .rpc_cred = ctx->cred,
4133 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004134 struct rpc_task_setup task_setup_data = {
4135 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004136 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004137 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004138 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004139 .flags = RPC_TASK_ASYNC,
4140 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004141
Frank Filz137d6ac2007-07-09 15:32:29 -07004142 /* Ensure this is an unlock - when canceling a lock, the
4143 * canceled lock is passed in, and it won't be an unlock.
4144 */
4145 fl->fl_type = F_UNLCK;
4146
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004147 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4148 if (data == NULL) {
4149 nfs_free_seqid(seqid);
4150 return ERR_PTR(-ENOMEM);
4151 }
4152
Trond Myklebust1174dd12010-12-21 10:52:24 -05004153 msg.rpc_argp = &data->arg;
4154 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004155 task_setup_data.callback_data = data;
4156 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004157}
4158
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4160{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004161 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004162 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004163 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004164 struct rpc_task *task;
4165 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004166 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
Trond Myklebust9b073572006-06-29 16:38:34 -04004168 status = nfs4_set_lock_state(state, request);
4169 /* Unlock _before_ we do the RPC call */
4170 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004171 down_read(&nfsi->rwsem);
4172 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4173 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004174 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004175 }
4176 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004177 if (status != 0)
4178 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004179 /* Is this a delegated lock? */
4180 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004181 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004182 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004183 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004184 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004185 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004186 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004187 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004188 status = PTR_ERR(task);
4189 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004190 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004191 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004192 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004193out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004194 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004195 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196}
4197
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004198struct nfs4_lockdata {
4199 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004200 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004201 struct nfs4_lock_state *lsp;
4202 struct nfs_open_context *ctx;
4203 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004204 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004205 int rpc_status;
4206 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004207 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004208};
4209
4210static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004211 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4212 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004213{
4214 struct nfs4_lockdata *p;
4215 struct inode *inode = lsp->ls_state->inode;
4216 struct nfs_server *server = NFS_SERVER(inode);
4217
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004218 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004219 if (p == NULL)
4220 return NULL;
4221
4222 p->arg.fh = NFS_FH(inode);
4223 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004224 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004225 if (p->arg.open_seqid == NULL)
4226 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004227 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004228 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004229 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004230 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004231 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004232 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004233 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004234 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004235 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004236 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004237 atomic_inc(&lsp->ls_count);
4238 p->ctx = get_nfs_open_context(ctx);
4239 memcpy(&p->fl, fl, sizeof(p->fl));
4240 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004241out_free_seqid:
4242 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004243out_free:
4244 kfree(p);
4245 return NULL;
4246}
4247
4248static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4249{
4250 struct nfs4_lockdata *data = calldata;
4251 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
Harvey Harrison3110ff82008-05-02 13:42:44 -07004253 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004254 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4255 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004256 /* Do we need to do an open_to_lock_owner? */
4257 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004258 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4259 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004260 data->arg.open_stateid = &state->stateid;
4261 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004262 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004263 } else
4264 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004265 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004266 if (nfs4_setup_sequence(data->server,
4267 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004268 &data->res.seq_res, 1, task))
4269 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004270 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004271 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004272}
4273
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004274static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4275{
4276 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4277 nfs4_lock_prepare(task, calldata);
4278}
4279
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004280static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4281{
4282 struct nfs4_lockdata *data = calldata;
4283
Harvey Harrison3110ff82008-05-02 13:42:44 -07004284 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004285
Trond Myklebust14516c32010-07-31 14:29:06 -04004286 if (!nfs4_sequence_done(task, &data->res.seq_res))
4287 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004288
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004289 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004290 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004291 if (data->rpc_status == 0)
4292 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4293 else
4294 goto out;
4295 }
4296 if (data->rpc_status == 0) {
4297 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4298 sizeof(data->lsp->ls_stateid.data));
4299 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004300 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004301 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004302out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004303 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004304}
4305
4306static void nfs4_lock_release(void *calldata)
4307{
4308 struct nfs4_lockdata *data = calldata;
4309
Harvey Harrison3110ff82008-05-02 13:42:44 -07004310 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004311 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004312 if (data->cancelled != 0) {
4313 struct rpc_task *task;
4314 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4315 data->arg.lock_seqid);
4316 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004317 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004318 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004319 } else
4320 nfs_free_seqid(data->arg.lock_seqid);
4321 nfs4_put_lock_state(data->lsp);
4322 put_nfs_open_context(data->ctx);
4323 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004324 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004325}
4326
4327static const struct rpc_call_ops nfs4_lock_ops = {
4328 .rpc_call_prepare = nfs4_lock_prepare,
4329 .rpc_call_done = nfs4_lock_done,
4330 .rpc_release = nfs4_lock_release,
4331};
4332
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004333static const struct rpc_call_ops nfs4_recover_lock_ops = {
4334 .rpc_call_prepare = nfs4_recover_lock_prepare,
4335 .rpc_call_done = nfs4_lock_done,
4336 .rpc_release = nfs4_lock_release,
4337};
4338
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004339static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4340{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004341 switch (error) {
4342 case -NFS4ERR_ADMIN_REVOKED:
4343 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004344 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004345 if (new_lock_owner != 0 ||
4346 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004347 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004348 break;
4349 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004350 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004351 case -NFS4ERR_EXPIRED:
4352 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004353 };
4354}
4355
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004356static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004357{
4358 struct nfs4_lockdata *data;
4359 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004360 struct rpc_message msg = {
4361 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4362 .rpc_cred = state->owner->so_cred,
4363 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004364 struct rpc_task_setup task_setup_data = {
4365 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004366 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004367 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004368 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004369 .flags = RPC_TASK_ASYNC,
4370 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004371 int ret;
4372
Harvey Harrison3110ff82008-05-02 13:42:44 -07004373 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004374 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004375 fl->fl_u.nfs4_fl.owner,
4376 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004377 if (data == NULL)
4378 return -ENOMEM;
4379 if (IS_SETLKW(cmd))
4380 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004381 if (recovery_type > NFS_LOCK_NEW) {
4382 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004383 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004384 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4385 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004386 msg.rpc_argp = &data->arg;
4387 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004388 task_setup_data.callback_data = data;
4389 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004390 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004391 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004392 ret = nfs4_wait_for_completion_rpc_task(task);
4393 if (ret == 0) {
4394 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004395 if (ret)
4396 nfs4_handle_setlk_error(data->server, data->lsp,
4397 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004398 } else
4399 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004400 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004401 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004402 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403}
4404
4405static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4406{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004407 struct nfs_server *server = NFS_SERVER(state->inode);
4408 struct nfs4_exception exception = { };
4409 int err;
4410
4411 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004412 /* Cache the lock if possible... */
4413 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4414 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004415 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004416 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004417 break;
4418 nfs4_handle_exception(server, err, &exception);
4419 } while (exception.retry);
4420 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421}
4422
4423static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4424{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004425 struct nfs_server *server = NFS_SERVER(state->inode);
4426 struct nfs4_exception exception = { };
4427 int err;
4428
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004429 err = nfs4_set_lock_state(state, request);
4430 if (err != 0)
4431 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004432 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004433 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4434 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004435 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004436 switch (err) {
4437 default:
4438 goto out;
4439 case -NFS4ERR_GRACE:
4440 case -NFS4ERR_DELAY:
4441 nfs4_handle_exception(server, err, &exception);
4442 err = 0;
4443 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004444 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004445out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004446 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447}
4448
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004449#if defined(CONFIG_NFS_V4_1)
4450static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4451{
4452 int status;
4453 struct nfs_server *server = NFS_SERVER(state->inode);
4454
4455 status = nfs41_test_stateid(server, state);
4456 if (status == NFS_OK)
4457 return 0;
4458 nfs41_free_stateid(server, state);
4459 return nfs4_lock_expired(state, request);
4460}
4461#endif
4462
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4464{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004465 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004466 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004467 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004469 if ((fl_flags & FL_POSIX) &&
4470 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4471 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004472 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004473 status = nfs4_set_lock_state(state, request);
4474 if (status != 0)
4475 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004476 request->fl_flags |= FL_ACCESS;
4477 status = do_vfs_lock(request->fl_file, request);
4478 if (status < 0)
4479 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004480 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004481 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004482 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004483 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004484 request->fl_flags = fl_flags & ~FL_SLEEP;
4485 status = do_vfs_lock(request->fl_file, request);
4486 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004487 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004488 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004489 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004490 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004491 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004492 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004493 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004494 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004495out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004496 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004497out:
4498 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 return status;
4500}
4501
4502static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4503{
4504 struct nfs4_exception exception = { };
4505 int err;
4506
4507 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004508 err = _nfs4_proc_setlk(state, cmd, request);
4509 if (err == -NFS4ERR_DENIED)
4510 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004512 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 } while (exception.retry);
4514 return err;
4515}
4516
4517static int
4518nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4519{
4520 struct nfs_open_context *ctx;
4521 struct nfs4_state *state;
4522 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4523 int status;
4524
4525 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004526 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 state = ctx->state;
4528
4529 if (request->fl_start < 0 || request->fl_end < 0)
4530 return -EINVAL;
4531
Trond Myklebustd9531262009-07-21 19:22:38 -04004532 if (IS_GETLK(cmd)) {
4533 if (state != NULL)
4534 return nfs4_proc_getlk(state, F_GETLK, request);
4535 return 0;
4536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
4538 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4539 return -EINVAL;
4540
Trond Myklebustd9531262009-07-21 19:22:38 -04004541 if (request->fl_type == F_UNLCK) {
4542 if (state != NULL)
4543 return nfs4_proc_unlck(state, cmd, request);
4544 return 0;
4545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
Trond Myklebustd9531262009-07-21 19:22:38 -04004547 if (state == NULL)
4548 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 do {
4550 status = nfs4_proc_setlk(state, cmd, request);
4551 if ((status != -EAGAIN) || IS_SETLK(cmd))
4552 break;
4553 timeout = nfs4_set_lock_task_retry(timeout);
4554 status = -ERESTARTSYS;
4555 if (signalled())
4556 break;
4557 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 return status;
4559}
4560
Trond Myklebust888e6942005-11-04 15:38:11 -05004561int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4562{
4563 struct nfs_server *server = NFS_SERVER(state->inode);
4564 struct nfs4_exception exception = { };
4565 int err;
4566
4567 err = nfs4_set_lock_state(state, fl);
4568 if (err != 0)
4569 goto out;
4570 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004571 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004572 switch (err) {
4573 default:
4574 printk(KERN_ERR "%s: unhandled error %d.\n",
4575 __func__, err);
4576 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004577 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004578 goto out;
4579 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004580 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004581 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004582 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004583 nfs4_schedule_lease_recovery(server->nfs_client);
4584 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004585 case -NFS4ERR_BADSESSION:
4586 case -NFS4ERR_BADSLOT:
4587 case -NFS4ERR_BAD_HIGH_SLOT:
4588 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4589 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004590 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004591 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004592 case -ERESTARTSYS:
4593 /*
4594 * The show must go on: exit, but mark the
4595 * stateid as needing recovery.
4596 */
4597 case -NFS4ERR_ADMIN_REVOKED:
4598 case -NFS4ERR_BAD_STATEID:
4599 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004600 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004601 err = 0;
4602 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004603 case -EKEYEXPIRED:
4604 /*
4605 * User RPCSEC_GSS context has expired.
4606 * We cannot recover this stateid now, so
4607 * skip it and allow recovery thread to
4608 * proceed.
4609 */
4610 err = 0;
4611 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004612 case -ENOMEM:
4613 case -NFS4ERR_DENIED:
4614 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4615 err = 0;
4616 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004617 case -NFS4ERR_DELAY:
4618 break;
4619 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004620 err = nfs4_handle_exception(server, err, &exception);
4621 } while (exception.retry);
4622out:
4623 return err;
4624}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004625
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004626static void nfs4_release_lockowner_release(void *calldata)
4627{
4628 kfree(calldata);
4629}
4630
4631const struct rpc_call_ops nfs4_release_lockowner_ops = {
4632 .rpc_release = nfs4_release_lockowner_release,
4633};
4634
4635void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4636{
4637 struct nfs_server *server = lsp->ls_state->owner->so_server;
4638 struct nfs_release_lockowner_args *args;
4639 struct rpc_message msg = {
4640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4641 };
4642
4643 if (server->nfs_client->cl_mvops->minor_version != 0)
4644 return;
4645 args = kmalloc(sizeof(*args), GFP_NOFS);
4646 if (!args)
4647 return;
4648 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4649 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004650 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004651 msg.rpc_argp = args;
4652 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4653}
4654
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004655#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4656
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004657static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4658 const void *buf, size_t buflen,
4659 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004660{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004661 if (strcmp(key, "") != 0)
4662 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004663
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004664 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004665}
4666
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004667static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4668 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004669{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004670 if (strcmp(key, "") != 0)
4671 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004672
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004673 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004674}
4675
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004676static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4677 size_t list_len, const char *name,
4678 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004679{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004680 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004681
J. Bruce Fields096455a2006-03-20 23:23:42 -05004682 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4683 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004684
4685 if (list && len <= list_len)
4686 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004687 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004688}
4689
Andy Adamson533eb462011-06-13 18:25:56 -04004690/*
4691 * nfs_fhget will use either the mounted_on_fileid or the fileid
4692 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004693static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4694{
Andy Adamson533eb462011-06-13 18:25:56 -04004695 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4696 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4697 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4698 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004699 return;
4700
4701 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4702 NFS_ATTR_FATTR_NLINK;
4703 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4704 fattr->nlink = 2;
4705}
4706
Trond Myklebust56659e92007-07-17 21:52:39 -04004707int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004708 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004709{
4710 struct nfs_server *server = NFS_SERVER(dir);
4711 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004712 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004713 };
4714 struct nfs4_fs_locations_arg args = {
4715 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004716 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004717 .page = page,
4718 .bitmask = bitmask,
4719 };
Benny Halevy22958462009-04-01 09:22:02 -04004720 struct nfs4_fs_locations_res res = {
4721 .fs_locations = fs_locations,
4722 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004723 struct rpc_message msg = {
4724 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4725 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004726 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004727 };
4728 int status;
4729
Harvey Harrison3110ff82008-05-02 13:42:44 -07004730 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004731
4732 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4733 * is not supported */
4734 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4735 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4736 else
4737 bitmask[0] |= FATTR4_WORD0_FILEID;
4738
Trond Myklebustc228fd32007-01-13 02:28:11 -05004739 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004740 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004741 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004742 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004743 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004744 return status;
4745}
4746
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004747static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4748{
4749 int status;
4750 struct nfs4_secinfo_arg args = {
4751 .dir_fh = NFS_FH(dir),
4752 .name = name,
4753 };
4754 struct nfs4_secinfo_res res = {
4755 .flavors = flavors,
4756 };
4757 struct rpc_message msg = {
4758 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4759 .rpc_argp = &args,
4760 .rpc_resp = &res,
4761 };
4762
4763 dprintk("NFS call secinfo %s\n", name->name);
4764 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4765 dprintk("NFS reply secinfo: %d\n", status);
4766 return status;
4767}
4768
4769int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4770{
4771 struct nfs4_exception exception = { };
4772 int err;
4773 do {
4774 err = nfs4_handle_exception(NFS_SERVER(dir),
4775 _nfs4_proc_secinfo(dir, name, flavors),
4776 &exception);
4777 } while (exception.retry);
4778 return err;
4779}
4780
Andy Adamson557134a2009-04-01 09:21:53 -04004781#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004782/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004783 * Check the exchange flags returned by the server for invalid flags, having
4784 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4785 * DS flags set.
4786 */
4787static int nfs4_check_cl_exchange_flags(u32 flags)
4788{
4789 if (flags & ~EXCHGID4_FLAG_MASK_R)
4790 goto out_inval;
4791 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4792 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4793 goto out_inval;
4794 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4795 goto out_inval;
4796 return NFS_OK;
4797out_inval:
4798 return -NFS4ERR_INVAL;
4799}
4800
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004801static bool
4802nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4803{
4804 if (a->server_scope_sz == b->server_scope_sz &&
4805 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4806 return true;
4807
4808 return false;
4809}
4810
Andy Adamson357f54d2010-12-14 10:11:57 -05004811/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004812 * nfs4_proc_exchange_id()
4813 *
4814 * Since the clientid has expired, all compounds using sessions
4815 * associated with the stale clientid will be returning
4816 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4817 * be in some phase of session reset.
4818 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004819int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004820{
4821 nfs4_verifier verifier;
4822 struct nfs41_exchange_id_args args = {
4823 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004824 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004825 };
4826 struct nfs41_exchange_id_res res = {
4827 .client = clp,
4828 };
4829 int status;
4830 struct rpc_message msg = {
4831 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4832 .rpc_argp = &args,
4833 .rpc_resp = &res,
4834 .rpc_cred = cred,
4835 };
4836 __be32 *p;
4837
4838 dprintk("--> %s\n", __func__);
4839 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004840
Benny Halevy99fe60d2009-04-01 09:22:29 -04004841 p = (u32 *)verifier.data;
4842 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4843 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4844 args.verifier = &verifier;
4845
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004846 args.id_len = scnprintf(args.id, sizeof(args.id),
4847 "%s/%s.%s/%u",
4848 clp->cl_ipaddr,
4849 init_utsname()->nodename,
4850 init_utsname()->domainname,
4851 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004852
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004853 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4854 if (unlikely(!res.server_scope))
4855 return -ENOMEM;
4856
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004857 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004858 if (!status)
4859 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004860
4861 if (!status) {
4862 if (clp->server_scope &&
4863 !nfs41_same_server_scope(clp->server_scope,
4864 res.server_scope)) {
4865 dprintk("%s: server_scope mismatch detected\n",
4866 __func__);
4867 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4868 kfree(clp->server_scope);
4869 clp->server_scope = NULL;
4870 }
4871
4872 if (!clp->server_scope)
4873 clp->server_scope = res.server_scope;
4874 else
4875 kfree(res.server_scope);
4876 }
4877
Benny Halevy99fe60d2009-04-01 09:22:29 -04004878 dprintk("<-- %s status= %d\n", __func__, status);
4879 return status;
4880}
4881
Andy Adamson2050f0c2009-04-01 09:22:30 -04004882struct nfs4_get_lease_time_data {
4883 struct nfs4_get_lease_time_args *args;
4884 struct nfs4_get_lease_time_res *res;
4885 struct nfs_client *clp;
4886};
4887
4888static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4889 void *calldata)
4890{
4891 int ret;
4892 struct nfs4_get_lease_time_data *data =
4893 (struct nfs4_get_lease_time_data *)calldata;
4894
4895 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004896 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004897 /* just setup sequence, do not trigger session recovery
4898 since we're invoked within one */
4899 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004900 &data->args->la_seq_args,
4901 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004902
4903 BUG_ON(ret == -EAGAIN);
4904 rpc_call_start(task);
4905 dprintk("<-- %s\n", __func__);
4906}
4907
4908/*
4909 * Called from nfs4_state_manager thread for session setup, so don't recover
4910 * from sequence operation or clientid errors.
4911 */
4912static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4913{
4914 struct nfs4_get_lease_time_data *data =
4915 (struct nfs4_get_lease_time_data *)calldata;
4916
4917 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004918 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4919 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004920 switch (task->tk_status) {
4921 case -NFS4ERR_DELAY:
4922 case -NFS4ERR_GRACE:
4923 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4924 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4925 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004926 /* fall through */
4927 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004928 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004929 return;
4930 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004931 dprintk("<-- %s\n", __func__);
4932}
4933
4934struct rpc_call_ops nfs4_get_lease_time_ops = {
4935 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4936 .rpc_call_done = nfs4_get_lease_time_done,
4937};
4938
4939int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4940{
4941 struct rpc_task *task;
4942 struct nfs4_get_lease_time_args args;
4943 struct nfs4_get_lease_time_res res = {
4944 .lr_fsinfo = fsinfo,
4945 };
4946 struct nfs4_get_lease_time_data data = {
4947 .args = &args,
4948 .res = &res,
4949 .clp = clp,
4950 };
4951 struct rpc_message msg = {
4952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4953 .rpc_argp = &args,
4954 .rpc_resp = &res,
4955 };
4956 struct rpc_task_setup task_setup = {
4957 .rpc_client = clp->cl_rpcclient,
4958 .rpc_message = &msg,
4959 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004960 .callback_data = &data,
4961 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04004962 };
4963 int status;
4964
Andy Adamson2050f0c2009-04-01 09:22:30 -04004965 dprintk("--> %s\n", __func__);
4966 task = rpc_run_task(&task_setup);
4967
4968 if (IS_ERR(task))
4969 status = PTR_ERR(task);
4970 else {
4971 status = task->tk_status;
4972 rpc_put_task(task);
4973 }
4974 dprintk("<-- %s return %d\n", __func__, status);
4975
4976 return status;
4977}
4978
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004979/*
4980 * Reset a slot table
4981 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004982static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4983 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004984{
Andy Adamson104aeba2010-01-14 17:45:10 -05004985 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004986 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004987 int ret = 0;
4988
Andy Adamson104aeba2010-01-14 17:45:10 -05004989 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4990 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004991
Andy Adamson104aeba2010-01-14 17:45:10 -05004992 /* Does the newly negotiated max_reqs match the existing slot table? */
4993 if (max_reqs != tbl->max_slots) {
4994 ret = -ENOMEM;
4995 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004996 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004997 if (!new)
4998 goto out;
4999 ret = 0;
5000 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005001 }
5002 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05005003 if (new) {
5004 tbl->slots = new;
5005 tbl->max_slots = max_reqs;
5006 }
5007 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005008 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005009 spin_unlock(&tbl->slot_tbl_lock);
5010 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5011 tbl, tbl->slots, tbl->max_slots);
5012out:
5013 dprintk("<-- %s: return %d\n", __func__, ret);
5014 return ret;
5015}
5016
Andy Adamsonfc931582009-04-01 09:22:31 -04005017/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005018 * Reset the forechannel and backchannel slot tables
5019 */
5020static int nfs4_reset_slot_tables(struct nfs4_session *session)
5021{
5022 int status;
5023
5024 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05005025 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005026 if (status)
5027 return status;
5028
5029 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05005030 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005031 return status;
5032}
5033
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005034/* Destroy the slot table */
5035static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5036{
5037 if (session->fc_slot_table.slots != NULL) {
5038 kfree(session->fc_slot_table.slots);
5039 session->fc_slot_table.slots = NULL;
5040 }
5041 if (session->bc_slot_table.slots != NULL) {
5042 kfree(session->bc_slot_table.slots);
5043 session->bc_slot_table.slots = NULL;
5044 }
5045 return;
5046}
5047
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005048/*
Andy Adamsonfc931582009-04-01 09:22:31 -04005049 * Initialize slot table
5050 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005051static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5052 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04005053{
Andy Adamsonfc931582009-04-01 09:22:31 -04005054 struct nfs4_slot *slot;
5055 int ret = -ENOMEM;
5056
5057 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5058
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005059 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04005060
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005061 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04005062 if (!slot)
5063 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04005064 ret = 0;
5065
5066 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04005067 tbl->max_slots = max_slots;
5068 tbl->slots = slot;
5069 tbl->highest_used_slotid = -1; /* no slot is currently used */
5070 spin_unlock(&tbl->slot_tbl_lock);
5071 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5072 tbl, tbl->slots, tbl->max_slots);
5073out:
5074 dprintk("<-- %s: return %d\n", __func__, ret);
5075 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04005076}
5077
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005078/*
5079 * Initialize the forechannel and backchannel tables
5080 */
5081static int nfs4_init_slot_tables(struct nfs4_session *session)
5082{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005083 struct nfs4_slot_table *tbl;
5084 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005085
Trond Myklebustf26468f2009-12-05 19:32:11 -05005086 tbl = &session->fc_slot_table;
5087 if (tbl->slots == NULL) {
5088 status = nfs4_init_slot_table(tbl,
5089 session->fc_attrs.max_reqs, 1);
5090 if (status)
5091 return status;
5092 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005093
Trond Myklebustf26468f2009-12-05 19:32:11 -05005094 tbl = &session->bc_slot_table;
5095 if (tbl->slots == NULL) {
5096 status = nfs4_init_slot_table(tbl,
5097 session->bc_attrs.max_reqs, 0);
5098 if (status)
5099 nfs4_destroy_slot_tables(session);
5100 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005101
5102 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005103}
5104
5105struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5106{
5107 struct nfs4_session *session;
5108 struct nfs4_slot_table *tbl;
5109
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005110 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005111 if (!session)
5112 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005113
Andy Adamson557134a2009-04-01 09:21:53 -04005114 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005115 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04005116 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005117 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005118 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005119
5120 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005121 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005122 spin_lock_init(&tbl->slot_tbl_lock);
5123 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005124 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005125
Trond Myklebust1055d762010-06-16 09:52:27 -04005126 session->session_state = 1<<NFS4_SESSION_INITING;
5127
Andy Adamson557134a2009-04-01 09:21:53 -04005128 session->clp = clp;
5129 return session;
5130}
5131
5132void nfs4_destroy_session(struct nfs4_session *session)
5133{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005134 nfs4_proc_destroy_session(session);
5135 dprintk("%s Destroy backchannel for xprt %p\n",
5136 __func__, session->clp->cl_rpcclient->cl_xprt);
5137 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5138 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005139 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005140 kfree(session);
5141}
5142
Andy Adamsonfc931582009-04-01 09:22:31 -04005143/*
5144 * Initialize the values to be used by the client in CREATE_SESSION
5145 * If nfs4_init_session set the fore channel request and response sizes,
5146 * use them.
5147 *
5148 * Set the back channel max_resp_sz_cached to zero to force the client to
5149 * always set csa_cachethis to FALSE because the current implementation
5150 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5151 */
5152static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5153{
5154 struct nfs4_session *session = args->client->cl_session;
5155 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5156 mxresp_sz = session->fc_attrs.max_resp_sz;
5157
5158 if (mxrqst_sz == 0)
5159 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5160 if (mxresp_sz == 0)
5161 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5162 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005163 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5164 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005165 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5166 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5167
5168 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005169 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005170 __func__,
5171 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005172 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005173
5174 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005175 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5176 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5177 args->bc_attrs.max_resp_sz_cached = 0;
5178 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5179 args->bc_attrs.max_reqs = 1;
5180
5181 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5182 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5183 __func__,
5184 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5185 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5186 args->bc_attrs.max_reqs);
5187}
5188
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005189static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005190{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005191 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5192 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5193
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005194 if (rcvd->max_resp_sz > sent->max_resp_sz)
5195 return -EINVAL;
5196 /*
5197 * Our requested max_ops is the minimum we need; we're not
5198 * prepared to break up compounds into smaller pieces than that.
5199 * So, no point even trying to continue if the server won't
5200 * cooperate:
5201 */
5202 if (rcvd->max_ops < sent->max_ops)
5203 return -EINVAL;
5204 if (rcvd->max_reqs == 0)
5205 return -EINVAL;
5206 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005207}
5208
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005209static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5210{
5211 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5212 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005213
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005214 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5215 return -EINVAL;
5216 if (rcvd->max_resp_sz < sent->max_resp_sz)
5217 return -EINVAL;
5218 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5219 return -EINVAL;
5220 /* These would render the backchannel useless: */
5221 if (rcvd->max_ops == 0)
5222 return -EINVAL;
5223 if (rcvd->max_reqs == 0)
5224 return -EINVAL;
5225 return 0;
5226}
Andy Adamson8d353012009-04-01 09:22:32 -04005227
Andy Adamson8d353012009-04-01 09:22:32 -04005228static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5229 struct nfs4_session *session)
5230{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005231 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005232
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005233 ret = nfs4_verify_fore_channel_attrs(args, session);
5234 if (ret)
5235 return ret;
5236 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005237}
5238
Andy Adamsonfc931582009-04-01 09:22:31 -04005239static int _nfs4_proc_create_session(struct nfs_client *clp)
5240{
5241 struct nfs4_session *session = clp->cl_session;
5242 struct nfs41_create_session_args args = {
5243 .client = clp,
5244 .cb_program = NFS4_CALLBACK,
5245 };
5246 struct nfs41_create_session_res res = {
5247 .client = clp,
5248 };
5249 struct rpc_message msg = {
5250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5251 .rpc_argp = &args,
5252 .rpc_resp = &res,
5253 };
5254 int status;
5255
5256 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005257 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005258
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005259 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005260
Andy Adamson8d353012009-04-01 09:22:32 -04005261 if (!status)
5262 /* Verify the session's negotiated channel_attrs values */
5263 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005264 if (!status) {
5265 /* Increment the clientid slot sequence id */
5266 clp->cl_seqid++;
5267 }
5268
5269 return status;
5270}
5271
5272/*
5273 * Issues a CREATE_SESSION operation to the server.
5274 * It is the responsibility of the caller to verify the session is
5275 * expired before calling this routine.
5276 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005277int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005278{
5279 int status;
5280 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005281 struct nfs4_session *session = clp->cl_session;
5282
5283 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5284
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005285 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005286 if (status)
5287 goto out;
5288
Trond Myklebustf26468f2009-12-05 19:32:11 -05005289 /* Init and reset the fore channel */
5290 status = nfs4_init_slot_tables(session);
5291 dprintk("slot table initialization returned %d\n", status);
5292 if (status)
5293 goto out;
5294 status = nfs4_reset_slot_tables(session);
5295 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005296 if (status)
5297 goto out;
5298
5299 ptr = (unsigned *)&session->sess_id.data[0];
5300 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5301 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005302out:
5303 dprintk("<-- %s\n", __func__);
5304 return status;
5305}
5306
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005307/*
5308 * Issue the over-the-wire RPC DESTROY_SESSION.
5309 * The caller must serialize access to this routine.
5310 */
5311int nfs4_proc_destroy_session(struct nfs4_session *session)
5312{
5313 int status = 0;
5314 struct rpc_message msg;
5315
5316 dprintk("--> nfs4_proc_destroy_session\n");
5317
5318 /* session is still being setup */
5319 if (session->clp->cl_cons_state != NFS_CS_READY)
5320 return status;
5321
5322 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5323 msg.rpc_argp = session;
5324 msg.rpc_resp = NULL;
5325 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005326 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005327
5328 if (status)
5329 printk(KERN_WARNING
5330 "Got error %d from the server on DESTROY_SESSION. "
5331 "Session has been destroyed regardless...\n", status);
5332
5333 dprintk("<-- nfs4_proc_destroy_session\n");
5334 return status;
5335}
5336
Trond Myklebustfccba802009-07-21 16:48:07 -04005337int nfs4_init_session(struct nfs_server *server)
5338{
5339 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005340 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005341 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005342 int ret;
5343
5344 if (!nfs4_has_session(clp))
5345 return 0;
5346
Trond Myklebust1055d762010-06-16 09:52:27 -04005347 session = clp->cl_session;
5348 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5349 return 0;
5350
Andy Adamson68bf05e2009-12-15 12:55:02 -05005351 rsize = server->rsize;
5352 if (rsize == 0)
5353 rsize = NFS_MAX_FILE_IO_SIZE;
5354 wsize = server->wsize;
5355 if (wsize == 0)
5356 wsize = NFS_MAX_FILE_IO_SIZE;
5357
Andy Adamson68bf05e2009-12-15 12:55:02 -05005358 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5359 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005360
Trond Myklebustfccba802009-07-21 16:48:07 -04005361 ret = nfs4_recover_expired_lease(server);
5362 if (!ret)
5363 ret = nfs4_check_client_ready(clp);
5364 return ret;
5365}
5366
Andy Adamsond83217c2011-03-01 01:34:17 +00005367int nfs4_init_ds_session(struct nfs_client *clp)
5368{
5369 struct nfs4_session *session = clp->cl_session;
5370 int ret;
5371
5372 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5373 return 0;
5374
5375 ret = nfs4_client_recover_expired_lease(clp);
5376 if (!ret)
5377 /* Test for the DS role */
5378 if (!is_ds_client(clp))
5379 ret = -ENODEV;
5380 if (!ret)
5381 ret = nfs4_check_client_ready(clp);
5382 return ret;
5383
5384}
5385EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5386
5387
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005388/*
5389 * Renew the cl_session lease.
5390 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005391struct nfs4_sequence_data {
5392 struct nfs_client *clp;
5393 struct nfs4_sequence_args args;
5394 struct nfs4_sequence_res res;
5395};
5396
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005397static void nfs41_sequence_release(void *data)
5398{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005399 struct nfs4_sequence_data *calldata = data;
5400 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005401
Alexandros Batsakis71358402010-02-05 03:45:05 -08005402 if (atomic_read(&clp->cl_count) > 1)
5403 nfs4_schedule_state_renewal(clp);
5404 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005405 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005406}
5407
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005408static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5409{
5410 switch(task->tk_status) {
5411 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005412 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5413 return -EAGAIN;
5414 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005415 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005416 }
5417 return 0;
5418}
5419
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005420static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005421{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005422 struct nfs4_sequence_data *calldata = data;
5423 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005424
Trond Myklebust14516c32010-07-31 14:29:06 -04005425 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5426 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005427
5428 if (task->tk_status < 0) {
5429 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005430 if (atomic_read(&clp->cl_count) == 1)
5431 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005432
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005433 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5434 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005435 return;
5436 }
5437 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005438 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005439out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005440 dprintk("<-- %s\n", __func__);
5441}
5442
5443static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5444{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005445 struct nfs4_sequence_data *calldata = data;
5446 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005447 struct nfs4_sequence_args *args;
5448 struct nfs4_sequence_res *res;
5449
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005450 args = task->tk_msg.rpc_argp;
5451 res = task->tk_msg.rpc_resp;
5452
Trond Myklebust035168a2010-06-16 09:52:26 -04005453 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005454 return;
5455 rpc_call_start(task);
5456}
5457
5458static const struct rpc_call_ops nfs41_sequence_ops = {
5459 .rpc_call_done = nfs41_sequence_call_done,
5460 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005461 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005462};
5463
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005464static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005465{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005466 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005467 struct rpc_message msg = {
5468 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5469 .rpc_cred = cred,
5470 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005471 struct rpc_task_setup task_setup_data = {
5472 .rpc_client = clp->cl_rpcclient,
5473 .rpc_message = &msg,
5474 .callback_ops = &nfs41_sequence_ops,
5475 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5476 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005477
Alexandros Batsakis71358402010-02-05 03:45:05 -08005478 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005479 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005480 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005481 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005482 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005483 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005484 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005485 msg.rpc_argp = &calldata->args;
5486 msg.rpc_resp = &calldata->res;
5487 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005488 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005489
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005490 return rpc_run_task(&task_setup_data);
5491}
5492
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005493static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005494{
5495 struct rpc_task *task;
5496 int ret = 0;
5497
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005498 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5499 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005500 task = _nfs41_proc_sequence(clp, cred);
5501 if (IS_ERR(task))
5502 ret = PTR_ERR(task);
5503 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005504 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005505 dprintk("<-- %s status=%d\n", __func__, ret);
5506 return ret;
5507}
5508
5509static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5510{
5511 struct rpc_task *task;
5512 int ret;
5513
5514 task = _nfs41_proc_sequence(clp, cred);
5515 if (IS_ERR(task)) {
5516 ret = PTR_ERR(task);
5517 goto out;
5518 }
5519 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005520 if (!ret) {
5521 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5522
5523 if (task->tk_status == 0)
5524 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005525 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005526 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005527 rpc_put_task(task);
5528out:
5529 dprintk("<-- %s status=%d\n", __func__, ret);
5530 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005531}
5532
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005533struct nfs4_reclaim_complete_data {
5534 struct nfs_client *clp;
5535 struct nfs41_reclaim_complete_args arg;
5536 struct nfs41_reclaim_complete_res res;
5537};
5538
5539static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5540{
5541 struct nfs4_reclaim_complete_data *calldata = data;
5542
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005543 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005544 if (nfs41_setup_sequence(calldata->clp->cl_session,
5545 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005546 &calldata->res.seq_res, 0, task))
5547 return;
5548
5549 rpc_call_start(task);
5550}
5551
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005552static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5553{
5554 switch(task->tk_status) {
5555 case 0:
5556 case -NFS4ERR_COMPLETE_ALREADY:
5557 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5558 break;
5559 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005560 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005561 /* fall through */
5562 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005563 return -EAGAIN;
5564 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005565 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005566 }
5567 return 0;
5568}
5569
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005570static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5571{
5572 struct nfs4_reclaim_complete_data *calldata = data;
5573 struct nfs_client *clp = calldata->clp;
5574 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5575
5576 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005577 if (!nfs41_sequence_done(task, res))
5578 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005579
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005580 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5581 rpc_restart_call_prepare(task);
5582 return;
5583 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005584 dprintk("<-- %s\n", __func__);
5585}
5586
5587static void nfs4_free_reclaim_complete_data(void *data)
5588{
5589 struct nfs4_reclaim_complete_data *calldata = data;
5590
5591 kfree(calldata);
5592}
5593
5594static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5595 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5596 .rpc_call_done = nfs4_reclaim_complete_done,
5597 .rpc_release = nfs4_free_reclaim_complete_data,
5598};
5599
5600/*
5601 * Issue a global reclaim complete.
5602 */
5603static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5604{
5605 struct nfs4_reclaim_complete_data *calldata;
5606 struct rpc_task *task;
5607 struct rpc_message msg = {
5608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5609 };
5610 struct rpc_task_setup task_setup_data = {
5611 .rpc_client = clp->cl_rpcclient,
5612 .rpc_message = &msg,
5613 .callback_ops = &nfs4_reclaim_complete_call_ops,
5614 .flags = RPC_TASK_ASYNC,
5615 };
5616 int status = -ENOMEM;
5617
5618 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005619 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005620 if (calldata == NULL)
5621 goto out;
5622 calldata->clp = clp;
5623 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005624
5625 msg.rpc_argp = &calldata->arg;
5626 msg.rpc_resp = &calldata->res;
5627 task_setup_data.callback_data = calldata;
5628 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005629 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005630 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005631 goto out;
5632 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005633 status = nfs4_wait_for_completion_rpc_task(task);
5634 if (status == 0)
5635 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005636 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005637 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005638out:
5639 dprintk("<-- %s status=%d\n", __func__, status);
5640 return status;
5641}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005642
5643static void
5644nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5645{
5646 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005647 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005648
5649 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005650 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5651 * right now covering the LAYOUTGET we are about to send.
5652 * However, that is not so catastrophic, and there seems
5653 * to be no way to prevent it completely.
5654 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005655 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5656 &lgp->res.seq_res, 0, task))
5657 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005658 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5659 NFS_I(lgp->args.inode)->layout,
5660 lgp->args.ctx->state)) {
5661 rpc_exit(task, NFS4_OK);
5662 return;
5663 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005664 rpc_call_start(task);
5665}
5666
5667static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5668{
5669 struct nfs4_layoutget *lgp = calldata;
5670 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5671
5672 dprintk("--> %s\n", __func__);
5673
5674 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5675 return;
5676
5677 switch (task->tk_status) {
5678 case 0:
5679 break;
5680 case -NFS4ERR_LAYOUTTRYLATER:
5681 case -NFS4ERR_RECALLCONFLICT:
5682 task->tk_status = -NFS4ERR_DELAY;
5683 /* Fall through */
5684 default:
5685 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5686 rpc_restart_call_prepare(task);
5687 return;
5688 }
5689 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005690 dprintk("<-- %s\n", __func__);
5691}
5692
5693static void nfs4_layoutget_release(void *calldata)
5694{
5695 struct nfs4_layoutget *lgp = calldata;
5696
5697 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005698 put_nfs_open_context(lgp->args.ctx);
5699 kfree(calldata);
5700 dprintk("<-- %s\n", __func__);
5701}
5702
5703static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5704 .rpc_call_prepare = nfs4_layoutget_prepare,
5705 .rpc_call_done = nfs4_layoutget_done,
5706 .rpc_release = nfs4_layoutget_release,
5707};
5708
5709int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5710{
5711 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5712 struct rpc_task *task;
5713 struct rpc_message msg = {
5714 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5715 .rpc_argp = &lgp->args,
5716 .rpc_resp = &lgp->res,
5717 };
5718 struct rpc_task_setup task_setup_data = {
5719 .rpc_client = server->client,
5720 .rpc_message = &msg,
5721 .callback_ops = &nfs4_layoutget_call_ops,
5722 .callback_data = lgp,
5723 .flags = RPC_TASK_ASYNC,
5724 };
5725 int status = 0;
5726
5727 dprintk("--> %s\n", __func__);
5728
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005729 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005730 lgp->res.seq_res.sr_slot = NULL;
5731 task = rpc_run_task(&task_setup_data);
5732 if (IS_ERR(task))
5733 return PTR_ERR(task);
5734 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005735 if (status == 0)
5736 status = task->tk_status;
5737 if (status == 0)
5738 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005739 rpc_put_task(task);
5740 dprintk("<-- %s status=%d\n", __func__, status);
5741 return status;
5742}
5743
Benny Halevycbe82602011-05-22 19:52:37 +03005744static void
5745nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5746{
5747 struct nfs4_layoutreturn *lrp = calldata;
5748
5749 dprintk("--> %s\n", __func__);
5750 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5751 &lrp->res.seq_res, 0, task))
5752 return;
5753 rpc_call_start(task);
5754}
5755
5756static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5757{
5758 struct nfs4_layoutreturn *lrp = calldata;
5759 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005760 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005761
5762 dprintk("--> %s\n", __func__);
5763
5764 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5765 return;
5766
5767 server = NFS_SERVER(lrp->args.inode);
5768 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005769 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03005770 return;
5771 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005772 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005773 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005774 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005775 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005776 } else
5777 BUG_ON(!list_empty(&lo->plh_segs));
5778 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005779 lo->plh_block_lgets--;
5780 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005781 dprintk("<-- %s\n", __func__);
5782}
5783
5784static void nfs4_layoutreturn_release(void *calldata)
5785{
5786 struct nfs4_layoutreturn *lrp = calldata;
5787
5788 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005789 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005790 kfree(calldata);
5791 dprintk("<-- %s\n", __func__);
5792}
5793
5794static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5795 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5796 .rpc_call_done = nfs4_layoutreturn_done,
5797 .rpc_release = nfs4_layoutreturn_release,
5798};
5799
5800int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5801{
5802 struct rpc_task *task;
5803 struct rpc_message msg = {
5804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5805 .rpc_argp = &lrp->args,
5806 .rpc_resp = &lrp->res,
5807 };
5808 struct rpc_task_setup task_setup_data = {
5809 .rpc_client = lrp->clp->cl_rpcclient,
5810 .rpc_message = &msg,
5811 .callback_ops = &nfs4_layoutreturn_call_ops,
5812 .callback_data = lrp,
5813 };
5814 int status;
5815
5816 dprintk("--> %s\n", __func__);
5817 task = rpc_run_task(&task_setup_data);
5818 if (IS_ERR(task))
5819 return PTR_ERR(task);
5820 status = task->tk_status;
5821 dprintk("<-- %s status=%d\n", __func__, status);
5822 rpc_put_task(task);
5823 return status;
5824}
5825
Andy Adamson7f11d8d2011-07-30 20:52:35 -04005826/*
5827 * Retrieve the list of Data Server devices from the MDS.
5828 */
5829static int _nfs4_getdevicelist(struct nfs_server *server,
5830 const struct nfs_fh *fh,
5831 struct pnfs_devicelist *devlist)
5832{
5833 struct nfs4_getdevicelist_args args = {
5834 .fh = fh,
5835 .layoutclass = server->pnfs_curr_ld->id,
5836 };
5837 struct nfs4_getdevicelist_res res = {
5838 .devlist = devlist,
5839 };
5840 struct rpc_message msg = {
5841 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5842 .rpc_argp = &args,
5843 .rpc_resp = &res,
5844 };
5845 int status;
5846
5847 dprintk("--> %s\n", __func__);
5848 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5849 &res.seq_res, 0);
5850 dprintk("<-- %s status=%d\n", __func__, status);
5851 return status;
5852}
5853
5854int nfs4_proc_getdevicelist(struct nfs_server *server,
5855 const struct nfs_fh *fh,
5856 struct pnfs_devicelist *devlist)
5857{
5858 struct nfs4_exception exception = { };
5859 int err;
5860
5861 do {
5862 err = nfs4_handle_exception(server,
5863 _nfs4_getdevicelist(server, fh, devlist),
5864 &exception);
5865 } while (exception.retry);
5866
5867 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5868 err, devlist->num_devs);
5869
5870 return err;
5871}
5872EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5873
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005874static int
5875_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5876{
5877 struct nfs4_getdeviceinfo_args args = {
5878 .pdev = pdev,
5879 };
5880 struct nfs4_getdeviceinfo_res res = {
5881 .pdev = pdev,
5882 };
5883 struct rpc_message msg = {
5884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5885 .rpc_argp = &args,
5886 .rpc_resp = &res,
5887 };
5888 int status;
5889
5890 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005891 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005892 dprintk("<-- %s status=%d\n", __func__, status);
5893
5894 return status;
5895}
5896
5897int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5898{
5899 struct nfs4_exception exception = { };
5900 int err;
5901
5902 do {
5903 err = nfs4_handle_exception(server,
5904 _nfs4_proc_getdeviceinfo(server, pdev),
5905 &exception);
5906 } while (exception.retry);
5907 return err;
5908}
5909EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5910
Andy Adamson863a3c62011-03-23 13:27:54 +00005911static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5912{
5913 struct nfs4_layoutcommit_data *data = calldata;
5914 struct nfs_server *server = NFS_SERVER(data->args.inode);
5915
5916 if (nfs4_setup_sequence(server, &data->args.seq_args,
5917 &data->res.seq_res, 1, task))
5918 return;
5919 rpc_call_start(task);
5920}
5921
5922static void
5923nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5924{
5925 struct nfs4_layoutcommit_data *data = calldata;
5926 struct nfs_server *server = NFS_SERVER(data->args.inode);
5927
5928 if (!nfs4_sequence_done(task, &data->res.seq_res))
5929 return;
5930
5931 switch (task->tk_status) { /* Just ignore these failures */
5932 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5933 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5934 case NFS4ERR_BADLAYOUT: /* no layout */
5935 case NFS4ERR_GRACE: /* loca_recalim always false */
5936 task->tk_status = 0;
5937 }
5938
5939 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005940 rpc_restart_call_prepare(task);
Andy Adamson863a3c62011-03-23 13:27:54 +00005941 return;
5942 }
5943
5944 if (task->tk_status == 0)
5945 nfs_post_op_update_inode_force_wcc(data->args.inode,
5946 data->res.fattr);
5947}
5948
5949static void nfs4_layoutcommit_release(void *calldata)
5950{
5951 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04005952 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07005953 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00005954
Andy Adamsondb29c082011-07-30 20:52:38 -04005955 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00005956 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04005957 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
5958 list_del_init(&lseg->pls_lc_list);
5959 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
5960 &lseg->pls_flags))
5961 put_lseg(lseg);
5962 }
Peng Tao92407e72011-10-23 20:21:17 -07005963
5964 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
5965 smp_mb__after_clear_bit();
5966 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
5967
Andy Adamson863a3c62011-03-23 13:27:54 +00005968 put_rpccred(data->cred);
5969 kfree(data);
5970}
5971
5972static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5973 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5974 .rpc_call_done = nfs4_layoutcommit_done,
5975 .rpc_release = nfs4_layoutcommit_release,
5976};
5977
5978int
Andy Adamsonef311532011-03-12 02:58:10 -05005979nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00005980{
5981 struct rpc_message msg = {
5982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
5983 .rpc_argp = &data->args,
5984 .rpc_resp = &data->res,
5985 .rpc_cred = data->cred,
5986 };
5987 struct rpc_task_setup task_setup_data = {
5988 .task = &data->task,
5989 .rpc_client = NFS_CLIENT(data->args.inode),
5990 .rpc_message = &msg,
5991 .callback_ops = &nfs4_layoutcommit_ops,
5992 .callback_data = data,
5993 .flags = RPC_TASK_ASYNC,
5994 };
5995 struct rpc_task *task;
5996 int status = 0;
5997
5998 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
5999 "lbw: %llu inode %lu\n",
6000 data->task.tk_pid, sync,
6001 data->args.lastbytewritten,
6002 data->args.inode->i_ino);
6003
6004 task = rpc_run_task(&task_setup_data);
6005 if (IS_ERR(task))
6006 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006007 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006008 goto out;
6009 status = nfs4_wait_for_completion_rpc_task(task);
6010 if (status != 0)
6011 goto out;
6012 status = task->tk_status;
6013out:
6014 dprintk("%s: status %d\n", __func__, status);
6015 rpc_put_task(task);
6016 return status;
6017}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006018
6019static int
6020_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6021 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6022{
6023 struct nfs41_secinfo_no_name_args args = {
6024 .style = SECINFO_STYLE_CURRENT_FH,
6025 };
6026 struct nfs4_secinfo_res res = {
6027 .flavors = flavors,
6028 };
6029 struct rpc_message msg = {
6030 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6031 .rpc_argp = &args,
6032 .rpc_resp = &res,
6033 };
6034 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6035}
6036
6037static int
6038nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6039 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6040{
6041 struct nfs4_exception exception = { };
6042 int err;
6043 do {
6044 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6045 switch (err) {
6046 case 0:
6047 case -NFS4ERR_WRONGSEC:
6048 case -NFS4ERR_NOTSUPP:
6049 break;
6050 default:
6051 err = nfs4_handle_exception(server, err, &exception);
6052 }
6053 } while (exception.retry);
6054 return err;
6055}
6056
6057static int
6058nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6059 struct nfs_fsinfo *info)
6060{
6061 int err;
6062 struct page *page;
6063 rpc_authflavor_t flavor;
6064 struct nfs4_secinfo_flavors *flavors;
6065
6066 page = alloc_page(GFP_KERNEL);
6067 if (!page) {
6068 err = -ENOMEM;
6069 goto out;
6070 }
6071
6072 flavors = page_address(page);
6073 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6074
6075 /*
6076 * Fall back on "guess and check" method if
6077 * the server doesn't support SECINFO_NO_NAME
6078 */
6079 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6080 err = nfs4_find_root_sec(server, fhandle, info);
6081 goto out_freepage;
6082 }
6083 if (err)
6084 goto out_freepage;
6085
6086 flavor = nfs_find_best_sec(flavors);
6087 if (err == 0)
6088 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6089
6090out_freepage:
6091 put_page(page);
6092 if (err == -EACCES)
6093 return -EPERM;
6094out:
6095 return err;
6096}
Bryan Schumaker7d974792011-06-02 14:59:08 -04006097static int _nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6098{
6099 int status;
6100 struct nfs41_test_stateid_args args = {
6101 .stateid = &state->stateid,
6102 };
6103 struct nfs41_test_stateid_res res;
6104 struct rpc_message msg = {
6105 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6106 .rpc_argp = &args,
6107 .rpc_resp = &res,
6108 };
6109 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6110 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6111 return status;
6112}
6113
6114static int nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6115{
6116 struct nfs4_exception exception = { };
6117 int err;
6118 do {
6119 err = nfs4_handle_exception(server,
6120 _nfs41_test_stateid(server, state),
6121 &exception);
6122 } while (exception.retry);
6123 return err;
6124}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006125
6126static int _nfs4_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6127{
6128 int status;
6129 struct nfs41_free_stateid_args args = {
6130 .stateid = &state->stateid,
6131 };
6132 struct nfs41_free_stateid_res res;
6133 struct rpc_message msg = {
6134 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6135 .rpc_argp = &args,
6136 .rpc_resp = &res,
6137 };
6138
6139 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6140 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6141 return status;
6142}
6143
6144static int nfs41_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6145{
6146 struct nfs4_exception exception = { };
6147 int err;
6148 do {
6149 err = nfs4_handle_exception(server,
6150 _nfs4_free_stateid(server, state),
6151 &exception);
6152 } while (exception.retry);
6153 return err;
6154}
Andy Adamson557134a2009-04-01 09:21:53 -04006155#endif /* CONFIG_NFS_V4_1 */
6156
Andy Adamson591d71c2009-04-01 09:22:47 -04006157struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006158 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006159 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160 .recover_open = nfs4_open_reclaim,
6161 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006162 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006163 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164};
6165
Andy Adamson591d71c2009-04-01 09:22:47 -04006166#if defined(CONFIG_NFS_V4_1)
6167struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6168 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6169 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6170 .recover_open = nfs4_open_reclaim,
6171 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006172 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006173 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006174 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006175};
6176#endif /* CONFIG_NFS_V4_1 */
6177
6178struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006179 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006180 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 .recover_open = nfs4_open_expired,
6182 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006183 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006184 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185};
6186
Andy Adamson591d71c2009-04-01 09:22:47 -04006187#if defined(CONFIG_NFS_V4_1)
6188struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6189 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6190 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006191 .recover_open = nfs41_open_expired,
6192 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006193 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006194 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006195};
6196#endif /* CONFIG_NFS_V4_1 */
6197
Benny Halevy29fba382009-04-01 09:22:44 -04006198struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6199 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006200 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006201 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006202};
6203
6204#if defined(CONFIG_NFS_V4_1)
6205struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6206 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006207 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006208 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006209};
6210#endif
6211
Trond Myklebust97dc1352010-06-16 09:52:26 -04006212static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6213 .minor_version = 0,
6214 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04006215 .validate_stateid = nfs4_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006216 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006217 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6218 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6219 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006220};
6221
6222#if defined(CONFIG_NFS_V4_1)
6223static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6224 .minor_version = 1,
6225 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04006226 .validate_stateid = nfs41_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006227 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006228 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6229 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6230 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006231};
6232#endif
6233
Trond Myklebust97dc1352010-06-16 09:52:26 -04006234const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6235 [0] = &nfs_v4_0_minor_ops,
6236#if defined(CONFIG_NFS_V4_1)
6237 [1] = &nfs_v4_1_minor_ops,
6238#endif
6239};
6240
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006241static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006242 .permission = nfs_permission,
6243 .getattr = nfs_getattr,
6244 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006245 .getxattr = generic_getxattr,
6246 .setxattr = generic_setxattr,
6247 .listxattr = generic_listxattr,
6248 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006249};
6250
David Howells509de812006-08-22 20:06:11 -04006251const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 .version = 4, /* protocol version */
6253 .dentry_ops = &nfs4_dentry_operations,
6254 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006255 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006256 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 .getroot = nfs4_proc_get_root,
6258 .getattr = nfs4_proc_getattr,
6259 .setattr = nfs4_proc_setattr,
6260 .lookup = nfs4_proc_lookup,
6261 .access = nfs4_proc_access,
6262 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 .create = nfs4_proc_create,
6264 .remove = nfs4_proc_remove,
6265 .unlink_setup = nfs4_proc_unlink_setup,
6266 .unlink_done = nfs4_proc_unlink_done,
6267 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006268 .rename_setup = nfs4_proc_rename_setup,
6269 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270 .link = nfs4_proc_link,
6271 .symlink = nfs4_proc_symlink,
6272 .mkdir = nfs4_proc_mkdir,
6273 .rmdir = nfs4_proc_remove,
6274 .readdir = nfs4_proc_readdir,
6275 .mknod = nfs4_proc_mknod,
6276 .statfs = nfs4_proc_statfs,
6277 .fsinfo = nfs4_proc_fsinfo,
6278 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006279 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 .decode_dirent = nfs4_decode_dirent,
6281 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006282 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006284 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006286 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006288 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006289 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006290 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006291 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006292 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293};
6294
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006295static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6296 .prefix = XATTR_NAME_NFSV4_ACL,
6297 .list = nfs4_xattr_list_nfs4_acl,
6298 .get = nfs4_xattr_get_nfs4_acl,
6299 .set = nfs4_xattr_set_nfs4_acl,
6300};
6301
6302const struct xattr_handler *nfs4_xattr_handlers[] = {
6303 &nfs4_xattr_nfs4_acl_handler,
6304 NULL
6305};
6306
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307/*
6308 * Local variables:
6309 * c-basic-offset: 8
6310 * End:
6311 */