blob: ff7571f12bb8bb74a7aa6e52fd6715de83d67e2c [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>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
Bryan Schumaker8f70e952011-03-24 17:12:31 +000046#include <linux/sunrpc/gss_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050055#include <linux/nfs_idmap.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040056#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000057#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050058#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010059#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Trond Myklebust4ce79712005-06-22 17:16:21 +000061#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050063#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050064#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040065#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040066#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define NFSDBG_FACILITY NFSDBG_PROC
69
Trond Myklebust2066fe82006-09-15 08:30:46 -040070#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFS4_POLL_RETRY_MAX (15*HZ)
72
Trond Myklebusta78cb572009-08-09 15:06:19 -040073#define NFS4_MAX_LOOP_ON_RECOVER (10)
74
Trond Myklebustef159e92012-02-06 19:50:40 -050075static unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
76
Trond Myklebustcdd4e682006-01-03 09:55:12 +010077struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010078static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080079static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050081static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050082static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Trond Myklebust99367812007-07-17 21:52:41 -040083static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040084static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
86 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040087#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050088static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
89static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040090#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050092static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Trond Myklebust52567b02009-10-23 14:46:42 -040094 if (err >= -1000)
95 return err;
96 switch (err) {
97 case -NFS4ERR_RESOURCE:
98 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000099 case -NFS4ERR_WRONGSEC:
100 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800101 case -NFS4ERR_BADOWNER:
102 case -NFS4ERR_BADNAME:
103 return -EINVAL;
Trond Myklebust52567b02009-10-23 14:46:42 -0400104 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700106 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400107 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400109 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112/*
113 * This is our standard bitmap for GETATTR requests.
114 */
115const u32 nfs4_fattr_bitmap[2] = {
116 FATTR4_WORD0_TYPE
117 | FATTR4_WORD0_CHANGE
118 | FATTR4_WORD0_SIZE
119 | FATTR4_WORD0_FSID
120 | FATTR4_WORD0_FILEID,
121 FATTR4_WORD1_MODE
122 | FATTR4_WORD1_NUMLINKS
123 | FATTR4_WORD1_OWNER
124 | FATTR4_WORD1_OWNER_GROUP
125 | FATTR4_WORD1_RAWDEV
126 | FATTR4_WORD1_SPACE_USED
127 | FATTR4_WORD1_TIME_ACCESS
128 | FATTR4_WORD1_TIME_METADATA
129 | FATTR4_WORD1_TIME_MODIFY
130};
131
132const u32 nfs4_statfs_bitmap[2] = {
133 FATTR4_WORD0_FILES_AVAIL
134 | FATTR4_WORD0_FILES_FREE
135 | FATTR4_WORD0_FILES_TOTAL,
136 FATTR4_WORD1_SPACE_AVAIL
137 | FATTR4_WORD1_SPACE_FREE
138 | FATTR4_WORD1_SPACE_TOTAL
139};
140
Trond Myklebust4ce79712005-06-22 17:16:21 +0000141const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 FATTR4_WORD0_MAXLINK
143 | FATTR4_WORD0_MAXNAME,
144 0
145};
146
Fred Isamandae100c2011-07-30 20:52:37 -0400147const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 | FATTR4_WORD0_MAXREAD
149 | FATTR4_WORD0_MAXWRITE
150 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700151 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400152 | FATTR4_WORD1_FS_LAYOUT_TYPES,
153 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154};
155
Manoj Naik830b8e32006-06-09 09:34:25 -0400156const u32 nfs4_fs_locations_bitmap[2] = {
157 FATTR4_WORD0_TYPE
158 | FATTR4_WORD0_CHANGE
159 | FATTR4_WORD0_SIZE
160 | FATTR4_WORD0_FSID
161 | FATTR4_WORD0_FILEID
162 | FATTR4_WORD0_FS_LOCATIONS,
163 FATTR4_WORD1_MODE
164 | FATTR4_WORD1_NUMLINKS
165 | FATTR4_WORD1_OWNER
166 | FATTR4_WORD1_OWNER_GROUP
167 | FATTR4_WORD1_RAWDEV
168 | FATTR4_WORD1_SPACE_USED
169 | FATTR4_WORD1_TIME_ACCESS
170 | FATTR4_WORD1_TIME_METADATA
171 | FATTR4_WORD1_TIME_MODIFY
172 | FATTR4_WORD1_MOUNTED_ON_FILEID
173};
174
Al Virobc4785c2006-10-19 23:28:51 -0700175static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 struct nfs4_readdir_arg *readdir)
177{
Al Viro0dbb4c62006-10-19 23:28:49 -0700178 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 BUG_ON(readdir->count < 80);
181 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000182 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
184 return;
185 }
186
187 readdir->cookie = 0;
188 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
189 if (cookie == 2)
190 return;
191
192 /*
193 * NFSv4 servers do not return entries for '.' and '..'
194 * Therefore, we fake these entries here. We let '.'
195 * have cookie 0 and '..' have cookie 1. Note that
196 * when talking to the server, we always send cookie 0
197 * instead of 1 or 2.
198 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800199 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 if (cookie == 0) {
202 *p++ = xdr_one; /* next */
203 *p++ = xdr_zero; /* cookie, first word */
204 *p++ = xdr_one; /* cookie, second word */
205 *p++ = xdr_one; /* entry len */
206 memcpy(p, ".\0\0\0", 4); /* entry */
207 p++;
208 *p++ = xdr_one; /* bitmap length */
209 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
210 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400211 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213
214 *p++ = xdr_one; /* next */
215 *p++ = xdr_zero; /* cookie, first word */
216 *p++ = xdr_two; /* cookie, second word */
217 *p++ = xdr_two; /* entry len */
218 memcpy(p, "..\0\0", 4); /* entry */
219 p++;
220 *p++ = xdr_one; /* bitmap length */
221 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
222 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400223 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 readdir->pgbase = (char *)p - (char *)start;
226 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800227 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Trond Myklebust65de8722008-12-23 15:21:44 -0500230static int nfs4_wait_clnt_recover(struct nfs_client *clp)
231{
232 int res;
233
234 might_sleep();
235
Trond Myklebuste005e802008-12-23 15:21:48 -0500236 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400237 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500238 return res;
239}
240
241static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
242{
243 int res = 0;
244
245 might_sleep();
246
247 if (*timeout <= 0)
248 *timeout = NFS4_POLL_RETRY_MIN;
249 if (*timeout > NFS4_POLL_RETRY_MAX)
250 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100251 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500252 if (fatal_signal_pending(current))
253 res = -ERESTARTSYS;
254 *timeout <<= 1;
255 return res;
256}
257
258/* This is the error handling routine for processes that are allowed
259 * to sleep.
260 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800261static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500262{
263 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500264 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500265 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500266 int ret = errorcode;
267
268 exception->retry = 0;
269 switch(errorcode) {
270 case 0:
271 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500272 case -NFS4ERR_OPENMODE:
Trond Myklebust14977482012-03-27 18:31:25 -0400273 if (inode && nfs_have_delegation(inode, FMODE_READ)) {
Trond Myklebust3114ea72012-03-07 16:39:06 -0500274 nfs_inode_return_delegation(inode);
275 exception->retry = 1;
276 return 0;
277 }
278 if (state == NULL)
279 break;
280 nfs4_schedule_stateid_recovery(server, state);
281 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500282 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500283 case -NFS4ERR_ADMIN_REVOKED:
284 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500285 if (state == NULL)
286 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400287 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500288 nfs4_schedule_stateid_recovery(server, state);
289 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400290 case -NFS4ERR_EXPIRED:
291 if (state != NULL)
292 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500293 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500294 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500295 nfs4_schedule_lease_recovery(clp);
296 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500297#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400298 case -NFS4ERR_BADSESSION:
299 case -NFS4ERR_BADSLOT:
300 case -NFS4ERR_BAD_HIGH_SLOT:
301 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
302 case -NFS4ERR_DEADSESSION:
303 case -NFS4ERR_SEQ_FALSE_RETRY:
304 case -NFS4ERR_SEQ_MISORDERED:
305 dprintk("%s ERROR: %d Reset session\n", __func__,
306 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500307 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400308 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500309 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500310#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500311 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500312 if (exception->timeout > HZ) {
313 /* We have retried a decent amount, time to
314 * fail
315 */
316 ret = -EBUSY;
317 break;
318 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500319 case -NFS4ERR_GRACE:
320 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500321 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500322 ret = nfs4_delay(server->client, &exception->timeout);
323 if (ret != 0)
324 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400325 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500326 case -NFS4ERR_OLD_STATEID:
327 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800328 break;
329 case -NFS4ERR_BADOWNER:
330 /* The following works around a Linux server bug! */
331 case -NFS4ERR_BADNAME:
332 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
333 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
334 exception->retry = 1;
335 printk(KERN_WARNING "NFS: v4 server %s "
336 "does not accept raw "
337 "uid/gids. "
338 "Reenabling the idmapper.\n",
339 server->nfs_client->cl_hostname);
340 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500341 }
342 /* We failed to handle the error */
343 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500344wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500345 ret = nfs4_wait_clnt_recover(clp);
346 if (ret == 0)
347 exception->retry = 1;
348 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500349}
350
351
Trond Myklebust452e9352010-07-31 14:29:06 -0400352static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 spin_lock(&clp->cl_lock);
355 if (time_before(clp->cl_last_renewal,timestamp))
356 clp->cl_last_renewal = timestamp;
357 spin_unlock(&clp->cl_lock);
358}
359
Trond Myklebust452e9352010-07-31 14:29:06 -0400360static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
361{
362 do_renew_lease(server->nfs_client, timestamp);
363}
364
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400365#if defined(CONFIG_NFS_V4_1)
366
Benny Halevy510b8172009-04-01 09:22:14 -0400367/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400368 * nfs4_free_slot - free a slot and efficiently update slot table.
369 *
370 * freeing a slot is trivially done by clearing its respective bit
371 * in the bitmap.
372 * If the freed slotid equals highest_used_slotid we want to update it
373 * so that the server would be able to size down the slot table if needed,
374 * otherwise we know that the highest_used_slotid is still in use.
375 * When updating highest_used_slotid there may be "holes" in the bitmap
376 * so we need to scan down from highest_used_slotid to 0 looking for the now
377 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500378 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500379 *
380 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400381 */
382static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500383nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400384{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500385 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400386 /* clear used bit in bitmap */
387 __clear_bit(slotid, tbl->used_slots);
388
389 /* update highest_used_slotid when it is freed */
390 if (slotid == tbl->highest_used_slotid) {
391 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500392 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400393 tbl->highest_used_slotid = slotid;
394 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500395 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400396 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500397 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
398 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400399}
400
Trond Myklebust961a8282012-01-17 22:57:37 -0500401bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
402{
403 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
404 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400405}
406
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800407/*
Andy Adamson42acd022011-01-06 02:04:34 +0000408 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800409 */
Andy Adamson42acd022011-01-06 02:04:34 +0000410static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800411{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400412 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500413 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
414 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800415 return;
416 }
417
Trond Myklebust45d43c22012-02-06 19:38:51 -0500418 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800419 return;
420
Andy Adamson42acd022011-01-06 02:04:34 +0000421 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
422 complete(&ses->fc_slot_table.complete);
423}
424
425/*
426 * Signal state manager thread if session back channel is drained
427 */
428void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
429{
430 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500431 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000432 return;
433 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
434 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800435}
436
Trond Myklebustd185a332010-06-16 09:52:25 -0400437static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400438{
439 struct nfs4_slot_table *tbl;
440
Trond Myklebustd185a332010-06-16 09:52:25 -0400441 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400442 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400443 /* just wake up the next guy waiting since
444 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500445 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500446 return;
Andy Adamson13615872009-04-01 09:22:17 -0400447 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500448
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500449 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500450 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000451 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500452 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400453 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400454}
455
Trond Myklebust14516c32010-07-31 14:29:06 -0400456static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400457{
458 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400459 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400460
461 /*
462 * sr_status remains 1 if an RPC level error occurred. The server
463 * may or may not have processed the sequence operation..
464 * Proceed as if the server received and processed the sequence
465 * operation.
466 */
467 if (res->sr_status == 1)
468 res->sr_status = NFS_OK;
469
Bryan Schumaker468f8612011-04-18 15:57:32 -0400470 /* don't increment the sequence number if the task wasn't sent */
471 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400472 goto out;
473
Andy Adamson691daf32009-12-04 15:55:39 -0500474 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400475 switch (res->sr_status) {
476 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400477 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400478 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400479 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400480 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400481 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500482 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500483 if (res->sr_status_flags != 0)
484 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400485 break;
486 case -NFS4ERR_DELAY:
487 /* The server detected a resend of the RPC call and
488 * returned NFS4ERR_DELAY as per Section 2.10.6.2
489 * of RFC5661.
490 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200491 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400492 __func__,
493 res->sr_slot - res->sr_session->fc_slot_table.slots,
494 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400495 goto out_retry;
496 default:
497 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400498 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400499 }
500out:
501 /* The session may be reset by one of the error handlers. */
502 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400503 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400504 return 1;
505out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400506 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400507 goto out;
508 rpc_delay(task, NFS4_POLL_RETRY_MAX);
509 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400510}
511
Trond Myklebust14516c32010-07-31 14:29:06 -0400512static int nfs4_sequence_done(struct rpc_task *task,
513 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400514{
Trond Myklebust14516c32010-07-31 14:29:06 -0400515 if (res->sr_session == NULL)
516 return 1;
517 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400518}
519
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400520/*
Benny Halevy510b8172009-04-01 09:22:14 -0400521 * nfs4_find_slot - efficiently look for a free slot
522 *
523 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
524 * If found, we mark the slot as used, update the highest_used_slotid,
525 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500526 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400527 *
528 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400529 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500530static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800531nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400532{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500533 u32 slotid;
534 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400535
Trond Myklebust45d43c22012-02-06 19:38:51 -0500536 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400537 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
538 tbl->max_slots);
539 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
540 if (slotid >= tbl->max_slots)
541 goto out;
542 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500543 if (slotid > tbl->highest_used_slotid ||
544 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400545 tbl->highest_used_slotid = slotid;
546 ret_id = slotid;
547out:
548 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
549 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400550 return ret_id;
551}
552
Trond Myklebust9d12b212012-01-17 22:04:25 -0500553static void nfs41_init_sequence(struct nfs4_sequence_args *args,
554 struct nfs4_sequence_res *res, int cache_reply)
555{
556 args->sa_session = NULL;
557 args->sa_cache_this = 0;
558 if (cache_reply)
559 args->sa_cache_this = 1;
560 res->sr_session = NULL;
561 res->sr_slot = NULL;
562}
563
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000564int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400565 struct nfs4_sequence_args *args,
566 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400567 struct rpc_task *task)
568{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400569 struct nfs4_slot *slot;
570 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500571 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400572
573 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400574 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400575 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400576 return 0;
577
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400578 tbl = &session->fc_slot_table;
579
580 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400581 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800582 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500583 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500584 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400585 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500586 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500587 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400588 }
589
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800590 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
591 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
592 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
593 spin_unlock(&tbl->slot_tbl_lock);
594 dprintk("%s enforce FIFO order\n", __func__);
595 return -EAGAIN;
596 }
597
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800598 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500599 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400600 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
601 spin_unlock(&tbl->slot_tbl_lock);
602 dprintk("<-- %s: no free slots\n", __func__);
603 return -EAGAIN;
604 }
605 spin_unlock(&tbl->slot_tbl_lock);
606
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800607 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400608 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400609 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400610 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400611
612 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
613
Benny Halevy99fe60d2009-04-01 09:22:29 -0400614 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400615 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400616 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400617 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400618 /*
619 * sr_status is only set in decode_sequence, and so will remain
620 * set to 1 if an rpc level failure occurs.
621 */
622 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623 return 0;
624}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000625EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400626
Trond Myklebust035168a2010-06-16 09:52:26 -0400627int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400628 struct nfs4_sequence_args *args,
629 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400630 struct rpc_task *task)
631{
Trond Myklebust035168a2010-06-16 09:52:26 -0400632 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400633 int ret = 0;
634
Trond Myklebust9d12b212012-01-17 22:04:25 -0500635 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400636 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400637
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200638 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400639 __func__, session->clp, session, res->sr_slot ?
640 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400641
Trond Myklebust9d12b212012-01-17 22:04:25 -0500642 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400643out:
644 dprintk("<-- %s status=%d\n", __func__, ret);
645 return ret;
646}
647
648struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400649 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400650 struct nfs4_sequence_args *seq_args;
651 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400652};
653
654static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
655{
656 struct nfs41_call_sync_data *data = calldata;
657
Trond Myklebust035168a2010-06-16 09:52:26 -0400658 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
659
660 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500661 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400662 return;
663 rpc_call_start(task);
664}
665
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800666static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
667{
668 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
669 nfs41_call_sync_prepare(task, calldata);
670}
671
Andy Adamson69ab40c2009-04-01 09:22:19 -0400672static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
673{
674 struct nfs41_call_sync_data *data = calldata;
675
Trond Myklebust14516c32010-07-31 14:29:06 -0400676 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400677}
678
Trond Myklebust17280172012-03-11 13:11:00 -0400679static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400680 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400681 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400682};
683
Trond Myklebust17280172012-03-11 13:11:00 -0400684static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800685 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
686 .rpc_call_done = nfs41_call_sync_done,
687};
688
Bryan Schumaker7c513052011-03-24 17:12:24 +0000689static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
690 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400691 struct rpc_message *msg,
692 struct nfs4_sequence_args *args,
693 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800694 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400695{
696 int ret;
697 struct rpc_task *task;
698 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400699 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400700 .seq_args = args,
701 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400702 };
703 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000704 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400705 .rpc_message = msg,
706 .callback_ops = &nfs41_call_sync_ops,
707 .callback_data = &data
708 };
709
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800710 if (privileged)
711 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400712 task = rpc_run_task(&task_setup);
713 if (IS_ERR(task))
714 ret = PTR_ERR(task);
715 else {
716 ret = task->tk_status;
717 rpc_put_task(task);
718 }
719 return ret;
720}
721
Bryan Schumaker7c513052011-03-24 17:12:24 +0000722int _nfs4_call_sync_session(struct rpc_clnt *clnt,
723 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400724 struct rpc_message *msg,
725 struct nfs4_sequence_args *args,
726 struct nfs4_sequence_res *res,
727 int cache_reply)
728{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500729 nfs41_init_sequence(args, res, cache_reply);
730 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400731}
732
Trond Myklebustdf896452010-06-16 09:52:26 -0400733#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500734static inline
735void nfs41_init_sequence(struct nfs4_sequence_args *args,
736 struct nfs4_sequence_res *res, int cache_reply)
737{
738}
739
Trond Myklebust14516c32010-07-31 14:29:06 -0400740static int nfs4_sequence_done(struct rpc_task *task,
741 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400742{
Trond Myklebust14516c32010-07-31 14:29:06 -0400743 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400744}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400745#endif /* CONFIG_NFS_V4_1 */
746
Bryan Schumaker7c513052011-03-24 17:12:24 +0000747int _nfs4_call_sync(struct rpc_clnt *clnt,
748 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400749 struct rpc_message *msg,
750 struct nfs4_sequence_args *args,
751 struct nfs4_sequence_res *res,
752 int cache_reply)
753{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500754 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000755 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400756}
757
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000758static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000759int nfs4_call_sync(struct rpc_clnt *clnt,
760 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000761 struct rpc_message *msg,
762 struct nfs4_sequence_args *args,
763 struct nfs4_sequence_res *res,
764 int cache_reply)
765{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000766 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
767 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000768}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400769
Trond Myklebust38478b22006-05-25 01:40:57 -0400770static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Trond Myklebust38478b22006-05-25 01:40:57 -0400772 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Trond Myklebust38478b22006-05-25 01:40:57 -0400774 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400775 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700776 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400777 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700778 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400779 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100782struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400783 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100784 struct nfs_openargs o_arg;
785 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100786 struct nfs_open_confirmargs c_arg;
787 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500788 struct nfs4_string owner_name;
789 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100790 struct nfs_fattr f_attr;
791 struct nfs_fattr dir_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100792 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400793 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100794 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400795 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100796 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100797 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400798 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100799 int rpc_status;
800 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100801};
802
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400803
804static void nfs4_init_opendata_res(struct nfs4_opendata *p)
805{
806 p->o_res.f_attr = &p->f_attr;
807 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400808 p->o_res.seqid = p->o_arg.seqid;
809 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400810 p->o_res.server = p->o_arg.server;
811 nfs_fattr_init(&p->f_attr);
812 nfs_fattr_init(&p->dir_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500813 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400814}
815
Al Viro82a2c1b2011-06-22 18:30:55 -0400816static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500817 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400818 const struct iattr *attrs,
819 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100820{
Al Viro82a2c1b2011-06-22 18:30:55 -0400821 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100822 struct inode *dir = parent->d_inode;
823 struct nfs_server *server = NFS_SERVER(dir);
824 struct nfs4_opendata *p;
825
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400826 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100827 if (p == NULL)
828 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400829 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100830 if (p->o_arg.seqid == NULL)
831 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400832 nfs_sb_active(dentry->d_sb);
833 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100834 p->dir = parent;
835 p->owner = sp;
836 atomic_inc(&sp->so_count);
837 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500838 p->o_arg.open_flags = flags;
839 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400840 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400841 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
842 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400843 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100844 p->o_arg.server = server;
845 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500846 p->o_arg.dir_bitmask = server->cache_consistency_bitmask;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100847 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500848 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500849 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400850
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100851 p->o_arg.u.attrs = &p->attrs;
852 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500853
854 verf[0] = jiffies;
855 verf[1] = current->pid;
856 memcpy(p->o_arg.u.verifier.data, verf,
857 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100858 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100859 p->c_arg.fh = &p->o_res.fh;
860 p->c_arg.stateid = &p->o_res.stateid;
861 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400862 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400863 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100864 return p;
865err_free:
866 kfree(p);
867err:
868 dput(parent);
869 return NULL;
870}
871
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400872static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100873{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400874 struct nfs4_opendata *p = container_of(kref,
875 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400876 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400877
878 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400879 if (p->state != NULL)
880 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400881 nfs4_put_state_owner(p->owner);
882 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400883 dput(p->dentry);
884 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500885 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400886 kfree(p);
887}
888
889static void nfs4_opendata_put(struct nfs4_opendata *p)
890{
891 if (p != NULL)
892 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100893}
894
Trond Myklebust06f814a2006-01-03 09:55:07 +0100895static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
896{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100897 int ret;
898
Trond Myklebust06f814a2006-01-03 09:55:07 +0100899 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100900 return ret;
901}
902
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400904{
905 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500906
Trond Myklebust536e43d2012-01-17 22:04:26 -0500907 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908 goto out;
909 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400910 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500911 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
912 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400913 break;
914 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500915 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
916 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400917 break;
918 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500919 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
920 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400921 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400923 return ret;
924}
925
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500926static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400927{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500928 if (delegation == NULL)
929 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500930 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400931 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500932 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400933 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500934 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400935 return 1;
936}
937
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500938static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100939{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100941 case FMODE_WRITE:
942 state->n_wronly++;
943 break;
944 case FMODE_READ:
945 state->n_rdonly++;
946 break;
947 case FMODE_READ|FMODE_WRITE:
948 state->n_rdwr++;
949 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500950 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100951}
952
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500953static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400954{
955 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500956 nfs4_stateid_copy(&state->stateid, stateid);
957 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500958 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400959 case FMODE_READ:
960 set_bit(NFS_O_RDONLY_STATE, &state->flags);
961 break;
962 case FMODE_WRITE:
963 set_bit(NFS_O_WRONLY_STATE, &state->flags);
964 break;
965 case FMODE_READ|FMODE_WRITE:
966 set_bit(NFS_O_RDWR_STATE, &state->flags);
967 }
968}
969
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500970static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400971{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400972 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400974 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400975}
976
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977static 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 -0700978{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400979 /*
980 * Protect the call to nfs4_state_set_mode_locked and
981 * serialise the stateid update
982 */
983 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400984 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500985 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400986 set_bit(NFS_DELEGATED_STATE, &state->flags);
987 }
988 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400990 write_sequnlock(&state->seqlock);
991 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700993 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996static 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 -0500997{
998 struct nfs_inode *nfsi = NFS_I(state->inode);
999 struct nfs_delegation *deleg_cur;
1000 int ret = 0;
1001
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001002 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001003
1004 rcu_read_lock();
1005 deleg_cur = rcu_dereference(nfsi->delegation);
1006 if (deleg_cur == NULL)
1007 goto no_delegation;
1008
1009 spin_lock(&deleg_cur->lock);
1010 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001012 goto no_delegation_unlock;
1013
1014 if (delegation == NULL)
1015 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001016 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001017 goto no_delegation_unlock;
1018
Trond Myklebustb7391f42008-12-23 15:21:52 -05001019 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001020 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001021 ret = 1;
1022no_delegation_unlock:
1023 spin_unlock(&deleg_cur->lock);
1024no_delegation:
1025 rcu_read_unlock();
1026
1027 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001028 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001029 ret = 1;
1030 }
1031
1032 return ret;
1033}
1034
1035
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001036static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001037{
1038 struct nfs_delegation *delegation;
1039
1040 rcu_read_lock();
1041 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001042 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001043 rcu_read_unlock();
1044 return;
1045 }
1046 rcu_read_unlock();
1047 nfs_inode_return_delegation(inode);
1048}
1049
Trond Myklebust6ee41262007-07-08 14:11:36 -04001050static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001051{
1052 struct nfs4_state *state = opendata->state;
1053 struct nfs_inode *nfsi = NFS_I(state->inode);
1054 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001055 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001056 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001057 nfs4_stateid stateid;
1058 int ret = -EAGAIN;
1059
Trond Myklebustaac00a82007-07-05 19:02:21 -04001060 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001061 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001062 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001063 if (can_open_cached(state, fmode, open_mode)) {
1064 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001065 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001066 goto out_return_state;
1067 }
1068 spin_unlock(&state->owner->so_lock);
1069 }
Trond Myklebust34310432008-12-23 15:21:38 -05001070 rcu_read_lock();
1071 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001072 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001073 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001074 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001075 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001076 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001077 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001078 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001079 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001080 if (ret != 0)
1081 goto out;
1082 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001083
1084 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001085 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001086 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001087 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001088out:
1089 return ERR_PTR(ret);
1090out_return_state:
1091 atomic_inc(&state->count);
1092 return state;
1093}
1094
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001095static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1096{
1097 struct inode *inode;
1098 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001099 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001100 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001101
Trond Myklebustaac00a82007-07-05 19:02:21 -04001102 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001103 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001104 goto out;
1105 }
1106
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001107 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001108 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001109 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001110 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001111 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001112 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001113 goto err;
1114 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001115 state = nfs4_get_open_state(inode, data->owner);
1116 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001117 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001118 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001119 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001120 int delegation_flags = 0;
1121
Trond Myklebust003707c2007-07-05 18:07:55 -04001122 rcu_read_lock();
1123 delegation = rcu_dereference(NFS_I(inode)->delegation);
1124 if (delegation)
1125 delegation_flags = delegation->flags;
1126 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001127 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1128 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1129 "returning a delegation for "
1130 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001131 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001132 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001133 nfs_inode_set_delegation(state->inode,
1134 data->owner->so_cred,
1135 &data->o_res);
1136 else
1137 nfs_inode_reclaim_delegation(state->inode,
1138 data->owner->so_cred,
1139 &data->o_res);
1140 }
Trond Myklebust34310432008-12-23 15:21:38 -05001141
1142 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001143 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001144 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001145out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001146 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001147err_put_inode:
1148 iput(inode);
1149err:
1150 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001151}
1152
Trond Myklebust864472e2006-01-03 09:55:15 +01001153static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1154{
1155 struct nfs_inode *nfsi = NFS_I(state->inode);
1156 struct nfs_open_context *ctx;
1157
1158 spin_lock(&state->inode->i_lock);
1159 list_for_each_entry(ctx, &nfsi->open_files, list) {
1160 if (ctx->state != state)
1161 continue;
1162 get_nfs_open_context(ctx);
1163 spin_unlock(&state->inode->i_lock);
1164 return ctx;
1165 }
1166 spin_unlock(&state->inode->i_lock);
1167 return ERR_PTR(-ENOENT);
1168}
1169
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001170static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1171{
1172 struct nfs4_opendata *opendata;
1173
Al Viro3d4ff432011-06-22 18:40:12 -04001174 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001175 if (opendata == NULL)
1176 return ERR_PTR(-ENOMEM);
1177 opendata->state = state;
1178 atomic_inc(&state->count);
1179 return opendata;
1180}
1181
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001182static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001183{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001184 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001185 int ret;
1186
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001187 opendata->o_arg.open_flags = 0;
1188 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001189 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1190 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1191 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001192 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001193 if (ret != 0)
1194 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001195 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001196 if (IS_ERR(newstate))
1197 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001198 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001199 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001200 return 0;
1201}
1202
1203static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1204{
Trond Myklebust864472e2006-01-03 09:55:15 +01001205 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001206 int ret;
1207
1208 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001209 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001210 smp_rmb();
1211 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001212 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001213 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001214 if (ret != 0)
1215 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001216 if (newstate != state)
1217 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001218 }
1219 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001220 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001221 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001222 if (ret != 0)
1223 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001224 if (newstate != state)
1225 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001226 }
1227 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001228 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001229 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001230 if (ret != 0)
1231 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001232 if (newstate != state)
1233 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001234 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001235 /*
1236 * We may have performed cached opens for all three recoveries.
1237 * Check if we need to update the current stateid.
1238 */
1239 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001240 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001241 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001242 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001243 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001244 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001245 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001246 return 0;
1247}
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249/*
1250 * OPEN_RECLAIM:
1251 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001253static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001255 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001256 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001257 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 int status;
1259
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001260 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1261 if (IS_ERR(opendata))
1262 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001263 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1264 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001265 rcu_read_lock();
1266 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001267 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001268 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001269 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001270 opendata->o_arg.u.delegation_type = delegation_type;
1271 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001272 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return status;
1274}
1275
Trond Myklebust539cd032007-06-05 11:46:42 -04001276static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 struct nfs_server *server = NFS_SERVER(state->inode);
1279 struct nfs4_exception exception = { };
1280 int err;
1281 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001282 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001283 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001284 break;
1285 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 } while (exception.retry);
1287 return err;
1288}
1289
Trond Myklebust864472e2006-01-03 09:55:15 +01001290static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1291{
1292 struct nfs_open_context *ctx;
1293 int ret;
1294
1295 ctx = nfs4_state_find_open_context(state);
1296 if (IS_ERR(ctx))
1297 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001298 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001299 put_nfs_open_context(ctx);
1300 return ret;
1301}
1302
Trond Myklebust13437e12007-07-06 15:10:43 -04001303static 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 -07001304{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001305 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001306 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001308 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1309 if (IS_ERR(opendata))
1310 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001311 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001312 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001313 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001314 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001315 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
Trond Myklebust13437e12007-07-06 15:10:43 -04001318int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001321 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 int err;
1323 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001324 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 switch (err) {
1326 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001327 case -ENOENT:
1328 case -ESTALE:
1329 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001330 case -NFS4ERR_BADSESSION:
1331 case -NFS4ERR_BADSLOT:
1332 case -NFS4ERR_BAD_HIGH_SLOT:
1333 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1334 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001335 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001336 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 case -NFS4ERR_STALE_CLIENTID:
1338 case -NFS4ERR_STALE_STATEID:
1339 case -NFS4ERR_EXPIRED:
1340 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001341 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001342 goto out;
1343 case -ERESTARTSYS:
1344 /*
1345 * The show must go on: exit, but mark the
1346 * stateid as needing recovery.
1347 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001348 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001349 case -NFS4ERR_ADMIN_REVOKED:
1350 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001351 nfs_inode_find_state_and_recover(state->inode,
1352 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001353 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001354 case -EKEYEXPIRED:
1355 /*
1356 * User RPCSEC_GSS context has expired.
1357 * We cannot recover this stateid now, so
1358 * skip it and allow recovery thread to
1359 * proceed.
1360 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001361 case -ENOMEM:
1362 err = 0;
1363 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365 err = nfs4_handle_exception(server, err, &exception);
1366 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001367out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return err;
1369}
1370
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001371static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1372{
1373 struct nfs4_opendata *data = calldata;
1374
1375 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001376 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001377 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001378 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001379 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001380 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001381 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001382}
1383
1384static void nfs4_open_confirm_release(void *calldata)
1385{
1386 struct nfs4_opendata *data = calldata;
1387 struct nfs4_state *state = NULL;
1388
1389 /* If this request hasn't been cancelled, do nothing */
1390 if (data->cancelled == 0)
1391 goto out_free;
1392 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001393 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001394 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001395 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001396 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001397 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001398out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001399 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001400}
1401
1402static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001403 .rpc_call_done = nfs4_open_confirm_done,
1404 .rpc_release = nfs4_open_confirm_release,
1405};
1406
1407/*
1408 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1409 */
1410static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1411{
1412 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1413 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001414 struct rpc_message msg = {
1415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1416 .rpc_argp = &data->c_arg,
1417 .rpc_resp = &data->c_res,
1418 .rpc_cred = data->owner->so_cred,
1419 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001420 struct rpc_task_setup task_setup_data = {
1421 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001422 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001423 .callback_ops = &nfs4_open_confirm_ops,
1424 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001425 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001426 .flags = RPC_TASK_ASYNC,
1427 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 int status;
1429
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001430 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001431 data->rpc_done = 0;
1432 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001433 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001434 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001435 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001436 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001437 status = nfs4_wait_for_completion_rpc_task(task);
1438 if (status != 0) {
1439 data->cancelled = 1;
1440 smp_wmb();
1441 } else
1442 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001443 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return status;
1445}
1446
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001447static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001449 struct nfs4_opendata *data = calldata;
1450 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001451
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001452 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1453 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001454 /*
1455 * Check if we still need to send an OPEN call, or if we can use
1456 * a delegation instead.
1457 */
1458 if (data->state != NULL) {
1459 struct nfs_delegation *delegation;
1460
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001461 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001462 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001463 rcu_read_lock();
1464 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001465 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1466 can_open_delegated(delegation, data->o_arg.fmode))
1467 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001468 rcu_read_unlock();
1469 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001470 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001471 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001472 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001473 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001474 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1475 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001476 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001477 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001478 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001479 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001480 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001481 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001482 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001483unlock_no_action:
1484 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001485out_no_action:
1486 task->tk_action = NULL;
1487
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001488}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001490static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1491{
1492 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1493 nfs4_open_prepare(task, calldata);
1494}
1495
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001496static void nfs4_open_done(struct rpc_task *task, void *calldata)
1497{
1498 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001500 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001501
Trond Myklebust14516c32010-07-31 14:29:06 -04001502 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1503 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001504
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001505 if (task->tk_status == 0) {
1506 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001507 case S_IFREG:
1508 break;
1509 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001510 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001511 break;
1512 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001513 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001514 break;
1515 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001516 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001517 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001518 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001519 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1520 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001521 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001522 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001523}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001524
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525static void nfs4_open_release(void *calldata)
1526{
1527 struct nfs4_opendata *data = calldata;
1528 struct nfs4_state *state = NULL;
1529
1530 /* If this request hasn't been cancelled, do nothing */
1531 if (data->cancelled == 0)
1532 goto out_free;
1533 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001534 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001535 goto out_free;
1536 /* In case we need an open_confirm, no cleanup! */
1537 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1538 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001539 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001540 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001541 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001542out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001543 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001544}
1545
1546static const struct rpc_call_ops nfs4_open_ops = {
1547 .rpc_call_prepare = nfs4_open_prepare,
1548 .rpc_call_done = nfs4_open_done,
1549 .rpc_release = nfs4_open_release,
1550};
1551
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001552static const struct rpc_call_ops nfs4_recover_open_ops = {
1553 .rpc_call_prepare = nfs4_recover_open_prepare,
1554 .rpc_call_done = nfs4_open_done,
1555 .rpc_release = nfs4_open_release,
1556};
1557
1558static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001559{
1560 struct inode *dir = data->dir->d_inode;
1561 struct nfs_server *server = NFS_SERVER(dir);
1562 struct nfs_openargs *o_arg = &data->o_arg;
1563 struct nfs_openres *o_res = &data->o_res;
1564 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001565 struct rpc_message msg = {
1566 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1567 .rpc_argp = o_arg,
1568 .rpc_resp = o_res,
1569 .rpc_cred = data->owner->so_cred,
1570 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001571 struct rpc_task_setup task_setup_data = {
1572 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001573 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001574 .callback_ops = &nfs4_open_ops,
1575 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001576 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001577 .flags = RPC_TASK_ASYNC,
1578 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001579 int status;
1580
Trond Myklebust9d12b212012-01-17 22:04:25 -05001581 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001582 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001583 data->rpc_done = 0;
1584 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001585 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001586 if (isrecover)
1587 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001588 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001589 if (IS_ERR(task))
1590 return PTR_ERR(task);
1591 status = nfs4_wait_for_completion_rpc_task(task);
1592 if (status != 0) {
1593 data->cancelled = 1;
1594 smp_wmb();
1595 } else
1596 status = data->rpc_status;
1597 rpc_put_task(task);
1598
1599 return status;
1600}
1601
1602static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1603{
1604 struct inode *dir = data->dir->d_inode;
1605 struct nfs_openres *o_res = &data->o_res;
1606 int status;
1607
1608 status = nfs4_run_open_task(data, 1);
1609 if (status != 0 || !data->rpc_done)
1610 return status;
1611
Trond Myklebust6926afd2012-01-07 13:22:46 -05001612 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1613
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001614 nfs_refresh_inode(dir, o_res->dir_attr);
1615
1616 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1617 status = _nfs4_proc_open_confirm(data);
1618 if (status != 0)
1619 return status;
1620 }
1621
1622 return status;
1623}
1624
1625/*
1626 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1627 */
1628static int _nfs4_proc_open(struct nfs4_opendata *data)
1629{
1630 struct inode *dir = data->dir->d_inode;
1631 struct nfs_server *server = NFS_SERVER(dir);
1632 struct nfs_openargs *o_arg = &data->o_arg;
1633 struct nfs_openres *o_res = &data->o_res;
1634 int status;
1635
1636 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001637 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001638 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001639 if (status != 0) {
1640 if (status == -NFS4ERR_BADNAME &&
1641 !(o_arg->open_flags & O_CREAT))
1642 return -ENOENT;
1643 return status;
1644 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001645
Trond Myklebust6926afd2012-01-07 13:22:46 -05001646 nfs_fattr_map_and_free_names(server, &data->f_attr);
1647
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001648 if (o_arg->open_flags & O_CREAT) {
1649 update_changeattr(dir, &o_res->cinfo);
1650 nfs_post_op_update_inode(dir, o_res->dir_attr);
1651 } else
1652 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001653 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1654 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001656 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001658 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
1660 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001661 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001662 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
Andy Adamsond83217c2011-03-01 01:34:17 +00001665static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001666{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001667 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001668 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001669
Trond Myklebusta78cb572009-08-09 15:06:19 -04001670 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001671 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001672 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001673 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001674 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1675 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001676 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001677 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001678 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001679 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001680 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001681}
1682
Andy Adamsond83217c2011-03-01 01:34:17 +00001683static int nfs4_recover_expired_lease(struct nfs_server *server)
1684{
1685 return nfs4_client_recover_expired_lease(server->nfs_client);
1686}
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688/*
1689 * OPEN_EXPIRED:
1690 * reclaim state on the server after a network partition.
1691 * Assumes caller holds the appropriate lock
1692 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001693static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001695 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001696 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001698 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1699 if (IS_ERR(opendata))
1700 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001701 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001702 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001703 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001704 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001705 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001708static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001709{
Trond Myklebust539cd032007-06-05 11:46:42 -04001710 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001711 struct nfs4_exception exception = { };
1712 int err;
1713
1714 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001715 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001716 switch (err) {
1717 default:
1718 goto out;
1719 case -NFS4ERR_GRACE:
1720 case -NFS4ERR_DELAY:
1721 nfs4_handle_exception(server, err, &exception);
1722 err = 0;
1723 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001724 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001725out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001726 return err;
1727}
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1730{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001732 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Trond Myklebust864472e2006-01-03 09:55:15 +01001734 ctx = nfs4_state_find_open_context(state);
1735 if (IS_ERR(ctx))
1736 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001737 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001738 put_nfs_open_context(ctx);
1739 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740}
1741
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001742#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001743static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001744{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001745 int status = NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001746 struct nfs_server *server = NFS_SERVER(state->inode);
1747
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001748 if (state->flags & flags) {
1749 status = nfs41_test_stateid(server, stateid);
1750 if (status != NFS_OK) {
1751 nfs41_free_stateid(server, stateid);
1752 state->flags &= ~flags;
1753 }
1754 }
1755 return status;
1756}
1757
1758static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1759{
1760 int deleg_status, open_status;
1761 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1762 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1763
1764 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1765 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1766
1767 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1768 return NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001769 return nfs4_open_expired(sp, state);
1770}
1771#endif
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001774 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1775 * fields corresponding to attributes that were used to store the verifier.
1776 * Make sure we clobber those fields in the later setattr call
1777 */
1778static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1779{
1780 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1781 !(sattr->ia_valid & ATTR_ATIME_SET))
1782 sattr->ia_valid |= ATTR_ATIME;
1783
1784 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1785 !(sattr->ia_valid & ATTR_MTIME_SET))
1786 sattr->ia_valid |= ATTR_MTIME;
1787}
1788
1789/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001790 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001792static 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 -07001793{
1794 struct nfs4_state_owner *sp;
1795 struct nfs4_state *state = NULL;
1796 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001797 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001798 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001802 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1803 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1805 goto out_err;
1806 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001807 status = nfs4_recover_expired_lease(server);
1808 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001809 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001810 if (dentry->d_inode != NULL)
1811 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001812 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001813 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001814 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001815 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Al Viro82a2c1b2011-06-22 18:30:55 -04001817 if (dentry->d_inode != NULL)
1818 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001819
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001820 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001822 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001824 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001825 status = PTR_ERR(state);
1826 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001827 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001828 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001829 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001830
1831 if (opendata->o_arg.open_flags & O_EXCL) {
1832 nfs4_exclusive_attrset(opendata, sattr);
1833
1834 nfs_fattr_init(opendata->o_res.f_attr);
1835 status = nfs4_do_setattr(state->inode, cred,
1836 opendata->o_res.f_attr, sattr,
1837 state);
1838 if (status == 0)
1839 nfs_setattr_update_inode(state->inode, sattr);
1840 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1841 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001842 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 *res = state;
1845 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001846err_opendata_put:
1847 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001848err_put_state_owner:
1849 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 *res = NULL;
1852 return status;
1853}
1854
1855
Al Viro82a2c1b2011-06-22 18:30:55 -04001856static 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 -07001857{
1858 struct nfs4_exception exception = { };
1859 struct nfs4_state *res;
1860 int status;
1861
1862 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001863 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (status == 0)
1865 break;
1866 /* NOTE: BAD_SEQID means the server and client disagree about the
1867 * book-keeping w.r.t. state-changing operations
1868 * (OPEN/CLOSE/LOCK/LOCKU...)
1869 * It is actually a sign of a bug on the client or on the server.
1870 *
1871 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001872 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 * have unhashed the old state_owner for us, and that we can
1874 * therefore safely retry using a new one. We should still warn
1875 * the user though...
1876 */
1877 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001878 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001879 " returned a bad sequence-id error!\n",
1880 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 exception.retry = 1;
1882 continue;
1883 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001884 /*
1885 * BAD_STATEID on OPEN means that the server cancelled our
1886 * state before it received the OPEN_CONFIRM.
1887 * Recover by retrying the request as per the discussion
1888 * on Page 181 of RFC3530.
1889 */
1890 if (status == -NFS4ERR_BAD_STATEID) {
1891 exception.retry = 1;
1892 continue;
1893 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001894 if (status == -EAGAIN) {
1895 /* We must have found a delegation */
1896 exception.retry = 1;
1897 continue;
1898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1900 status, &exception));
1901 } while (exception.retry);
1902 return res;
1903}
1904
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001905static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1906 struct nfs_fattr *fattr, struct iattr *sattr,
1907 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001909 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001911 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 .iap = sattr,
1913 .server = server,
1914 .bitmask = server->attr_bitmask,
1915 };
1916 struct nfs_setattrres res = {
1917 .fattr = fattr,
1918 .server = server,
1919 };
1920 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001921 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1922 .rpc_argp = &arg,
1923 .rpc_resp = &res,
1924 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001926 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001927 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Trond Myklebust0e574af2005-10-27 22:12:38 -04001929 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Trond Myklebust4fc87962012-03-08 17:42:01 -05001931 if (state != NULL) {
1932 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1933 current->files, current->tgid);
1934 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1935 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001936 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001937 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05001938 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Bryan Schumaker7c513052011-03-24 17:12:24 +00001940 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001941 if (status == 0 && state != NULL)
1942 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001943 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001946static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1947 struct nfs_fattr *fattr, struct iattr *sattr,
1948 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001950 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001951 struct nfs4_exception exception = {
1952 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05001953 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001954 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 int err;
1956 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04001957 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
1958 switch (err) {
1959 case -NFS4ERR_OPENMODE:
1960 if (state && !(state->state & FMODE_WRITE)) {
1961 err = -EBADF;
1962 if (sattr->ia_valid & ATTR_OPEN)
1963 err = -EACCES;
1964 goto out;
1965 }
1966 }
1967 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04001969out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 return err;
1971}
1972
1973struct nfs4_closedata {
1974 struct inode *inode;
1975 struct nfs4_state *state;
1976 struct nfs_closeargs arg;
1977 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001978 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001979 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001980 bool roc;
1981 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982};
1983
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001984static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001985{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001986 struct nfs4_closedata *calldata = data;
1987 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001988 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001989
Fred Isamanf7e89172011-01-06 11:36:32 +00001990 if (calldata->roc)
1991 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001992 nfs4_put_open_state(calldata->state);
1993 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001994 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001995 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001996 kfree(calldata);
1997}
1998
Trond Myklebust88069f72009-12-08 08:33:16 -05001999static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2000 fmode_t fmode)
2001{
2002 spin_lock(&state->owner->so_lock);
2003 if (!(fmode & FMODE_READ))
2004 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2005 if (!(fmode & FMODE_WRITE))
2006 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2007 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2008 spin_unlock(&state->owner->so_lock);
2009}
2010
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002011static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002013 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 struct nfs_server *server = NFS_SERVER(calldata->inode);
2016
Chuck Levera3ca5652012-03-01 17:00:40 -05002017 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002018 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2019 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 /* hmm. we are done with the inode, and in the process of freeing
2021 * the state_owner. we keep this around to process errors
2022 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 switch (task->tk_status) {
2024 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002025 if (calldata->roc)
2026 pnfs_roc_set_barrier(state->inode,
2027 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002028 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002029 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002030 nfs4_close_clear_stateid_flags(state,
2031 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 break;
2033 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002034 case -NFS4ERR_OLD_STATEID:
2035 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002037 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002038 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002040 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2041 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002043 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002044 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002045 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046}
2047
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002048static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002050 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002051 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002052 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002053
Chuck Levera3ca5652012-03-01 17:00:40 -05002054 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002055 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002056 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002057
Trond Myklebust88069f72009-12-08 08:33:16 -05002058 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2059 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002060 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002061 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002062 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002063 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002064 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2065 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2066 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002067 }
2068 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002069 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2070 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2071 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002072 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002073 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002074 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002075
2076 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002077 /* Note: exit _without_ calling nfs4_close_done */
2078 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002079 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002080 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002081
Fred Isamanf7e89172011-01-06 11:36:32 +00002082 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002083 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002084 if (calldata->roc &&
2085 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2086 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2087 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002088 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002089 }
2090 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002091
Trond Myklebust516a6af2005-10-27 22:12:41 -04002092 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002093 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002094 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002095 &calldata->arg.seq_args,
2096 &calldata->res.seq_res,
2097 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002098 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002099 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002100out:
2101 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102}
2103
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002104static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002105 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002106 .rpc_call_done = nfs4_close_done,
2107 .rpc_release = nfs4_free_closedata,
2108};
2109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110/*
2111 * It is possible for data to be read/written from a mem-mapped file
2112 * after the sys_close call (which hits the vfs layer as a flush).
2113 * This means that we can't safely call nfsv4 close on a file until
2114 * the inode is cleared. This in turn means that we are not good
2115 * NFSv4 citizens - we do not indicate to the server to update the file's
2116 * share state even when we are done with one of the three share
2117 * stateid's in the inode.
2118 *
2119 * NOTE: Caller must be holding the sp->so_owner semaphore!
2120 */
Al Viro643168c2011-06-22 18:20:23 -04002121int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002123 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002125 struct nfs4_state_owner *sp = state->owner;
2126 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002127 struct rpc_message msg = {
2128 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2129 .rpc_cred = state->owner->so_cred,
2130 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002131 struct rpc_task_setup task_setup_data = {
2132 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002133 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002134 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002135 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002136 .flags = RPC_TASK_ASYNC,
2137 };
Trond Myklebust95121352005-10-18 14:20:12 -07002138 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002140 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002142 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002143 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002144 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002146 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002147 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002149 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002150 if (calldata->arg.seqid == NULL)
2151 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002152 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002153 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002154 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002155 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002156 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002157 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002158 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002159
Trond Myklebust1174dd12010-12-21 10:52:24 -05002160 msg.rpc_argp = &calldata->arg;
2161 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002162 task_setup_data.callback_data = calldata;
2163 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002164 if (IS_ERR(task))
2165 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002166 status = 0;
2167 if (wait)
2168 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002169 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002170 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002171out_free_calldata:
2172 kfree(calldata);
2173out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002174 if (roc)
2175 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002176 nfs4_put_open_state(state);
2177 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002178 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
Trond Myklebust2b484292010-09-17 10:56:51 -04002181static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002182nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 struct nfs4_state *state;
2185
Trond Myklebust565277f2007-10-15 18:17:53 -04002186 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002187 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002188 if (IS_ERR(state))
2189 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002190 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002191 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192}
2193
Trond Myklebust1185a552009-12-03 15:54:02 -05002194static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002195{
2196 if (ctx->state == NULL)
2197 return;
2198 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002199 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002200 else
Al Viro643168c2011-06-22 18:20:23 -04002201 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002202}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2205{
Benny Halevy43652ad2009-04-01 09:21:54 -04002206 struct nfs4_server_caps_arg args = {
2207 .fhandle = fhandle,
2208 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 struct nfs4_server_caps_res res = {};
2210 struct rpc_message msg = {
2211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002212 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 .rpc_resp = &res,
2214 };
2215 int status;
2216
Bryan Schumaker7c513052011-03-24 17:12:24 +00002217 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (status == 0) {
2219 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002220 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2221 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2222 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2223 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2224 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2226 server->caps |= NFS_CAP_ACLS;
2227 if (res.has_links != 0)
2228 server->caps |= NFS_CAP_HARDLINKS;
2229 if (res.has_symlinks != 0)
2230 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002231 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2232 server->caps |= NFS_CAP_FILEID;
2233 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2234 server->caps |= NFS_CAP_MODE;
2235 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2236 server->caps |= NFS_CAP_NLINK;
2237 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2238 server->caps |= NFS_CAP_OWNER;
2239 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2240 server->caps |= NFS_CAP_OWNER_GROUP;
2241 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2242 server->caps |= NFS_CAP_ATIME;
2243 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2244 server->caps |= NFS_CAP_CTIME;
2245 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2246 server->caps |= NFS_CAP_MTIME;
2247
Trond Myklebusta65318b2009-03-11 14:10:28 -04002248 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2249 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2250 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002252 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 return status;
2256}
2257
Trond Myklebust55a97592006-06-09 09:34:19 -04002258int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
2260 struct nfs4_exception exception = { };
2261 int err;
2262 do {
2263 err = nfs4_handle_exception(server,
2264 _nfs4_server_capabilities(server, fhandle),
2265 &exception);
2266 } while (exception.retry);
2267 return err;
2268}
2269
2270static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2271 struct nfs_fsinfo *info)
2272{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 struct nfs4_lookup_root_arg args = {
2274 .bitmask = nfs4_fattr_bitmap,
2275 };
2276 struct nfs4_lookup_res res = {
2277 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002278 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 .fh = fhandle,
2280 };
2281 struct rpc_message msg = {
2282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2283 .rpc_argp = &args,
2284 .rpc_resp = &res,
2285 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002286
Trond Myklebust0e574af2005-10-27 22:12:38 -04002287 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002288 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289}
2290
2291static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2292 struct nfs_fsinfo *info)
2293{
2294 struct nfs4_exception exception = { };
2295 int err;
2296 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002297 err = _nfs4_lookup_root(server, fhandle, info);
2298 switch (err) {
2299 case 0:
2300 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002301 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002302 default:
2303 err = nfs4_handle_exception(server, err, &exception);
2304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002306out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return err;
2308}
2309
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002310static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2311 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2312{
2313 struct rpc_auth *auth;
2314 int ret;
2315
2316 auth = rpcauth_create(flavor, server->client);
2317 if (!auth) {
2318 ret = -EIO;
2319 goto out;
2320 }
2321 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002322out:
2323 return ret;
2324}
2325
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002326static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002327 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002329 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002330 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002332 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2333 flav_array[len] = RPC_AUTH_NULL;
2334 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002335
2336 for (i = 0; i < len; i++) {
2337 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002338 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002339 continue;
2340 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002341 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002342 /*
2343 * -EACCESS could mean that the user doesn't have correct permissions
2344 * to access the mount. It could also mean that we tried to mount
2345 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2346 * existing mount programs don't handle -EACCES very well so it should
2347 * be mapped to -EPERM instead.
2348 */
2349 if (status == -EACCES)
2350 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002351 return status;
2352}
2353
2354/*
2355 * get the file handle for the "/" directory on the server
2356 */
2357static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2358 struct nfs_fsinfo *info)
2359{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002360 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002361 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002362 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2363 /*
2364 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2365 * by nfs4_map_errors() as this function exits.
2366 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002367 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if (status == 0)
2369 status = nfs4_server_capabilities(server, fhandle);
2370 if (status == 0)
2371 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002372 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373}
2374
Manoj Naik6b97fd32006-06-09 09:34:29 -04002375/*
2376 * Get locations and (maybe) other attributes of a referral.
2377 * Note that we'll actually follow the referral later when
2378 * we detect fsid mismatch in inode revalidation
2379 */
Andy Adamson533eb462011-06-13 18:25:56 -04002380static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2381 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002382{
2383 int status = -ENOMEM;
2384 struct page *page = NULL;
2385 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002386
2387 page = alloc_page(GFP_KERNEL);
2388 if (page == NULL)
2389 goto out;
2390 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2391 if (locations == NULL)
2392 goto out;
2393
Trond Myklebustc228fd32007-01-13 02:28:11 -05002394 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002395 if (status != 0)
2396 goto out;
2397 /* Make sure server returned a different fsid for the referral */
2398 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002399 dprintk("%s: server did not return a different fsid for"
2400 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002401 status = -EIO;
2402 goto out;
2403 }
Andy Adamson533eb462011-06-13 18:25:56 -04002404 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2405 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002406
Andy Adamson533eb462011-06-13 18:25:56 -04002407 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002408 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002409 memset(fhandle, 0, sizeof(struct nfs_fh));
2410out:
2411 if (page)
2412 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002413 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002414 return status;
2415}
2416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2418{
2419 struct nfs4_getattr_arg args = {
2420 .fh = fhandle,
2421 .bitmask = server->attr_bitmask,
2422 };
2423 struct nfs4_getattr_res res = {
2424 .fattr = fattr,
2425 .server = server,
2426 };
2427 struct rpc_message msg = {
2428 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2429 .rpc_argp = &args,
2430 .rpc_resp = &res,
2431 };
2432
Trond Myklebust0e574af2005-10-27 22:12:38 -04002433 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002434 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435}
2436
2437static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2438{
2439 struct nfs4_exception exception = { };
2440 int err;
2441 do {
2442 err = nfs4_handle_exception(server,
2443 _nfs4_proc_getattr(server, fhandle, fattr),
2444 &exception);
2445 } while (exception.retry);
2446 return err;
2447}
2448
2449/*
2450 * The file is not closed if it is opened due to the a request to change
2451 * the size of the file. The open call will not be needed once the
2452 * VFS layer lookup-intents are implemented.
2453 *
2454 * Close is called when the inode is destroyed.
2455 * If we haven't opened the file for O_WRONLY, we
2456 * need to in the size_change case to obtain a stateid.
2457 *
2458 * Got race?
2459 * Because OPEN is always done by name in nfsv4, it is
2460 * possible that we opened a different file by the same
2461 * name. We can recognize this race condition, but we
2462 * can't do anything about it besides returning an error.
2463 *
2464 * This will be fixed with VFS changes (lookup-intent).
2465 */
2466static int
2467nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2468 struct iattr *sattr)
2469{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002470 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002471 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002472 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 int status;
2474
Benny Halevy8a1636c2010-07-14 15:43:57 -04002475 if (pnfs_ld_layoutret_on_setattr(inode))
2476 pnfs_return_layout(inode);
2477
Trond Myklebust0e574af2005-10-27 22:12:38 -04002478 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Trond Myklebustd5308382005-11-04 15:33:38 -05002480 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002481 if (sattr->ia_valid & ATTR_FILE) {
2482 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002483
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002484 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002485 if (ctx) {
2486 cred = ctx->cred;
2487 state = ctx->state;
2488 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002489 }
2490
Trond Myklebust536e43d2012-01-17 22:04:26 -05002491 /* Deal with open(O_TRUNC) */
2492 if (sattr->ia_valid & ATTR_OPEN)
2493 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2494
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002495 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002496 if (status == 0)
2497 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 return status;
2499}
2500
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002501static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2502 const struct qstr *name, struct nfs_fh *fhandle,
2503 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002504{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002505 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002506 int status;
2507 struct nfs4_lookup_arg args = {
2508 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002509 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002510 .name = name,
2511 };
2512 struct nfs4_lookup_res res = {
2513 .server = server,
2514 .fattr = fattr,
2515 .fh = fhandle,
2516 };
2517 struct rpc_message msg = {
2518 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2519 .rpc_argp = &args,
2520 .rpc_resp = &res,
2521 };
2522
2523 nfs_fattr_init(fattr);
2524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002526 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 dprintk("NFS reply lookup: %d\n", status);
2528 return status;
2529}
2530
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002531void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2532{
2533 memset(fh, 0, sizeof(struct nfs_fh));
2534 fattr->fsid.major = 1;
2535 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2536 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2537 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2538 fattr->nlink = 2;
2539}
2540
Bryan Schumaker7c513052011-03-24 17:12:24 +00002541static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2542 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543{
2544 struct nfs4_exception exception = { };
2545 int err;
2546 do {
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002547 int status;
2548
2549 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2550 switch (status) {
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002551 case -NFS4ERR_BADNAME:
2552 return -ENOENT;
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002553 case -NFS4ERR_MOVED:
Trond Myklebusta6f498a2011-11-07 15:50:32 -05002554 return nfs4_get_referral(dir, name, fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002555 case -NFS4ERR_WRONGSEC:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002556 nfs_fixup_secinfo_attributes(fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002557 }
2558 err = nfs4_handle_exception(NFS_SERVER(dir),
2559 status, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 } while (exception.retry);
2561 return err;
2562}
2563
2564static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2565{
Trond Myklebust76b32992007-08-10 17:45:11 -04002566 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 struct nfs4_accessargs args = {
2568 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002569 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002571 struct nfs4_accessres res = {
2572 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002573 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 struct rpc_message msg = {
2575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2576 .rpc_argp = &args,
2577 .rpc_resp = &res,
2578 .rpc_cred = entry->cred,
2579 };
2580 int mode = entry->mask;
2581 int status;
2582
2583 /*
2584 * Determine which access bits we want to ask for...
2585 */
2586 if (mode & MAY_READ)
2587 args.access |= NFS4_ACCESS_READ;
2588 if (S_ISDIR(inode->i_mode)) {
2589 if (mode & MAY_WRITE)
2590 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2591 if (mode & MAY_EXEC)
2592 args.access |= NFS4_ACCESS_LOOKUP;
2593 } else {
2594 if (mode & MAY_WRITE)
2595 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2596 if (mode & MAY_EXEC)
2597 args.access |= NFS4_ACCESS_EXECUTE;
2598 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002599
2600 res.fattr = nfs_alloc_fattr();
2601 if (res.fattr == NULL)
2602 return -ENOMEM;
2603
Bryan Schumaker7c513052011-03-24 17:12:24 +00002604 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 if (!status) {
2606 entry->mask = 0;
2607 if (res.access & NFS4_ACCESS_READ)
2608 entry->mask |= MAY_READ;
2609 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2610 entry->mask |= MAY_WRITE;
2611 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2612 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002613 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002615 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 return status;
2617}
2618
2619static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2620{
2621 struct nfs4_exception exception = { };
2622 int err;
2623 do {
2624 err = nfs4_handle_exception(NFS_SERVER(inode),
2625 _nfs4_proc_access(inode, entry),
2626 &exception);
2627 } while (exception.retry);
2628 return err;
2629}
2630
2631/*
2632 * TODO: For the time being, we don't try to get any attributes
2633 * along with any of the zero-copy operations READ, READDIR,
2634 * READLINK, WRITE.
2635 *
2636 * In the case of the first three, we want to put the GETATTR
2637 * after the read-type operation -- this is because it is hard
2638 * to predict the length of a GETATTR response in v4, and thus
2639 * align the READ data correctly. This means that the GETATTR
2640 * may end up partially falling into the page cache, and we should
2641 * shift it into the 'tail' of the xdr_buf before processing.
2642 * To do this efficiently, we need to know the total length
2643 * of data received, which doesn't seem to be available outside
2644 * of the RPC layer.
2645 *
2646 * In the case of WRITE, we also want to put the GETATTR after
2647 * the operation -- in this case because we want to make sure
2648 * we get the post-operation mtime and size. This means that
2649 * we can't use xdr_encode_pages() as written: we need a variant
2650 * of it which would leave room in the 'tail' iovec.
2651 *
2652 * Both of these changes to the XDR layer would in fact be quite
2653 * minor, but I decided to leave them for a subsequent patch.
2654 */
2655static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2656 unsigned int pgbase, unsigned int pglen)
2657{
2658 struct nfs4_readlink args = {
2659 .fh = NFS_FH(inode),
2660 .pgbase = pgbase,
2661 .pglen = pglen,
2662 .pages = &page,
2663 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002664 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 struct rpc_message msg = {
2666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2667 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002668 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 };
2670
Bryan Schumaker7c513052011-03-24 17:12:24 +00002671 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 -07002672}
2673
2674static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2675 unsigned int pgbase, unsigned int pglen)
2676{
2677 struct nfs4_exception exception = { };
2678 int err;
2679 do {
2680 err = nfs4_handle_exception(NFS_SERVER(inode),
2681 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2682 &exception);
2683 } while (exception.retry);
2684 return err;
2685}
2686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687/*
2688 * Got race?
2689 * We will need to arrange for the VFS layer to provide an atomic open.
2690 * Until then, this create/open method is prone to inefficiency and race
2691 * conditions due to the lookup, create, and open VFS calls from sys_open()
2692 * placed on the wire.
2693 *
2694 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2695 * The file will be opened again in the subsequent VFS open call
2696 * (nfs4_proc_file_open).
2697 *
2698 * The open for read will just hang around to be used by any process that
2699 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2700 */
2701
2702static int
2703nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002704 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705{
Al Viro82a2c1b2011-06-22 18:30:55 -04002706 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002708 struct rpc_cred *cred = NULL;
2709 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 int status = 0;
2711
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002712 if (ctx != NULL) {
2713 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002714 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002715 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002717 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002718 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002719 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 if (IS_ERR(state)) {
2721 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002722 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002724 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002725 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002726 if (ctx != NULL)
2727 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002728 else
Al Viro643168c2011-06-22 18:20:23 -04002729 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730out:
2731 return status;
2732}
2733
2734static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2735{
Trond Myklebust16e42952005-10-27 22:12:44 -04002736 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002737 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002739 .name.len = name->len,
2740 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002741 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002743 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002744 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002745 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002747 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2748 .rpc_argp = &args,
2749 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002751 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Trond Myklebustd3468902010-04-16 16:22:50 -04002753 res.dir_attr = nfs_alloc_fattr();
2754 if (res.dir_attr == NULL)
2755 goto out;
2756
Bryan Schumaker7c513052011-03-24 17:12:24 +00002757 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002758 if (status == 0) {
2759 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002760 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002761 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002762 nfs_free_fattr(res.dir_attr);
2763out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 return status;
2765}
2766
2767static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2768{
2769 struct nfs4_exception exception = { };
2770 int err;
2771 do {
2772 err = nfs4_handle_exception(NFS_SERVER(dir),
2773 _nfs4_proc_remove(dir, name),
2774 &exception);
2775 } while (exception.retry);
2776 return err;
2777}
2778
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002779static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002781 struct nfs_server *server = NFS_SERVER(dir);
2782 struct nfs_removeargs *args = msg->rpc_argp;
2783 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Trond Myklebusta65318b2009-03-11 14:10:28 -04002785 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002786 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002788 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789}
2790
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002791static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2792{
2793 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2794 &data->args.seq_args,
2795 &data->res.seq_res,
2796 task))
2797 return;
2798 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799}
2800
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002801static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002803 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2804
Trond Myklebust14516c32010-07-31 14:29:06 -04002805 if (!nfs4_sequence_done(task, &res->seq_res))
2806 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002807 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002808 return 0;
2809 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002810 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002811 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812}
2813
Jeff Laytond3d41522010-09-17 17:31:57 -04002814static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2815{
2816 struct nfs_server *server = NFS_SERVER(dir);
2817 struct nfs_renameargs *arg = msg->rpc_argp;
2818 struct nfs_renameres *res = msg->rpc_resp;
2819
2820 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2821 arg->bitmask = server->attr_bitmask;
2822 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002823 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002824}
2825
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002826static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2827{
2828 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2829 &data->args.seq_args,
2830 &data->res.seq_res,
2831 task))
2832 return;
2833 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002834}
2835
2836static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2837 struct inode *new_dir)
2838{
2839 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2840
2841 if (!nfs4_sequence_done(task, &res->seq_res))
2842 return 0;
2843 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2844 return 0;
2845
2846 update_changeattr(old_dir, &res->old_cinfo);
2847 nfs_post_op_update_inode(old_dir, res->old_fattr);
2848 update_changeattr(new_dir, &res->new_cinfo);
2849 nfs_post_op_update_inode(new_dir, res->new_fattr);
2850 return 1;
2851}
2852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2854 struct inode *new_dir, struct qstr *new_name)
2855{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002856 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002857 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 .old_dir = NFS_FH(old_dir),
2859 .new_dir = NFS_FH(new_dir),
2860 .old_name = old_name,
2861 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002862 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002864 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002865 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002866 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 struct rpc_message msg = {
2868 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2869 .rpc_argp = &arg,
2870 .rpc_resp = &res,
2871 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002872 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Trond Myklebust011fff72010-04-16 16:22:49 -04002874 res.old_fattr = nfs_alloc_fattr();
2875 res.new_fattr = nfs_alloc_fattr();
2876 if (res.old_fattr == NULL || res.new_fattr == NULL)
2877 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
Bryan Schumaker7c513052011-03-24 17:12:24 +00002879 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 if (!status) {
2881 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002882 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002884 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002886out:
2887 nfs_free_fattr(res.new_fattr);
2888 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 return status;
2890}
2891
2892static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2893 struct inode *new_dir, struct qstr *new_name)
2894{
2895 struct nfs4_exception exception = { };
2896 int err;
2897 do {
2898 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2899 _nfs4_proc_rename(old_dir, old_name,
2900 new_dir, new_name),
2901 &exception);
2902 } while (exception.retry);
2903 return err;
2904}
2905
2906static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2907{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002908 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 struct nfs4_link_arg arg = {
2910 .fh = NFS_FH(inode),
2911 .dir_fh = NFS_FH(dir),
2912 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002913 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002915 struct nfs4_link_res res = {
2916 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002917 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 struct rpc_message msg = {
2919 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2920 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002921 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002923 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Trond Myklebust136f2622010-04-16 16:22:49 -04002925 res.fattr = nfs_alloc_fattr();
2926 res.dir_attr = nfs_alloc_fattr();
2927 if (res.fattr == NULL || res.dir_attr == NULL)
2928 goto out;
2929
Bryan Schumaker7c513052011-03-24 17:12:24 +00002930 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002931 if (!status) {
2932 update_changeattr(dir, &res.cinfo);
2933 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002934 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002935 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002936out:
2937 nfs_free_fattr(res.dir_attr);
2938 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 return status;
2940}
2941
2942static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2943{
2944 struct nfs4_exception exception = { };
2945 int err;
2946 do {
2947 err = nfs4_handle_exception(NFS_SERVER(inode),
2948 _nfs4_proc_link(inode, dir, name),
2949 &exception);
2950 } while (exception.retry);
2951 return err;
2952}
2953
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002954struct nfs4_createdata {
2955 struct rpc_message msg;
2956 struct nfs4_create_arg arg;
2957 struct nfs4_create_res res;
2958 struct nfs_fh fh;
2959 struct nfs_fattr fattr;
2960 struct nfs_fattr dir_fattr;
2961};
2962
2963static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2964 struct qstr *name, struct iattr *sattr, u32 ftype)
2965{
2966 struct nfs4_createdata *data;
2967
2968 data = kzalloc(sizeof(*data), GFP_KERNEL);
2969 if (data != NULL) {
2970 struct nfs_server *server = NFS_SERVER(dir);
2971
2972 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2973 data->msg.rpc_argp = &data->arg;
2974 data->msg.rpc_resp = &data->res;
2975 data->arg.dir_fh = NFS_FH(dir);
2976 data->arg.server = server;
2977 data->arg.name = name;
2978 data->arg.attrs = sattr;
2979 data->arg.ftype = ftype;
2980 data->arg.bitmask = server->attr_bitmask;
2981 data->res.server = server;
2982 data->res.fh = &data->fh;
2983 data->res.fattr = &data->fattr;
2984 data->res.dir_fattr = &data->dir_fattr;
2985 nfs_fattr_init(data->res.fattr);
2986 nfs_fattr_init(data->res.dir_fattr);
2987 }
2988 return data;
2989}
2990
2991static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2992{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002993 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002994 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002995 if (status == 0) {
2996 update_changeattr(dir, &data->res.dir_cinfo);
2997 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2998 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2999 }
3000 return status;
3001}
3002
3003static void nfs4_free_createdata(struct nfs4_createdata *data)
3004{
3005 kfree(data);
3006}
3007
Chuck Lever4f390c12006-08-22 20:06:22 -04003008static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003009 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003011 struct nfs4_createdata *data;
3012 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
Chuck Lever94a6d752006-08-22 20:06:23 -04003014 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003015 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003016
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003017 status = -ENOMEM;
3018 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3019 if (data == NULL)
3020 goto out;
3021
3022 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3023 data->arg.u.symlink.pages = &page;
3024 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003026 status = nfs4_do_create(dir, dentry, data);
3027
3028 nfs4_free_createdata(data);
3029out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 return status;
3031}
3032
Chuck Lever4f390c12006-08-22 20:06:22 -04003033static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003034 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035{
3036 struct nfs4_exception exception = { };
3037 int err;
3038 do {
3039 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003040 _nfs4_proc_symlink(dir, dentry, page,
3041 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 &exception);
3043 } while (exception.retry);
3044 return err;
3045}
3046
3047static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3048 struct iattr *sattr)
3049{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003050 struct nfs4_createdata *data;
3051 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003053 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3054 if (data == NULL)
3055 goto out;
3056
3057 status = nfs4_do_create(dir, dentry, data);
3058
3059 nfs4_free_createdata(data);
3060out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 return status;
3062}
3063
3064static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3065 struct iattr *sattr)
3066{
3067 struct nfs4_exception exception = { };
3068 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003069
3070 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 do {
3072 err = nfs4_handle_exception(NFS_SERVER(dir),
3073 _nfs4_proc_mkdir(dir, dentry, sattr),
3074 &exception);
3075 } while (exception.retry);
3076 return err;
3077}
3078
3079static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003080 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
3082 struct inode *dir = dentry->d_inode;
3083 struct nfs4_readdir_arg args = {
3084 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003085 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 .pgbase = 0,
3087 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003088 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003089 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 };
3091 struct nfs4_readdir_res res;
3092 struct rpc_message msg = {
3093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3094 .rpc_argp = &args,
3095 .rpc_resp = &res,
3096 .rpc_cred = cred,
3097 };
3098 int status;
3099
Harvey Harrison3110ff82008-05-02 13:42:44 -07003100 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003101 dentry->d_parent->d_name.name,
3102 dentry->d_name.name,
3103 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3105 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003106 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 -05003107 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003109 status += args.pgbase;
3110 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003111
3112 nfs_invalidate_atime(dir);
3113
Harvey Harrison3110ff82008-05-02 13:42:44 -07003114 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 return status;
3116}
3117
3118static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003119 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120{
3121 struct nfs4_exception exception = { };
3122 int err;
3123 do {
3124 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3125 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003126 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 &exception);
3128 } while (exception.retry);
3129 return err;
3130}
3131
3132static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3133 struct iattr *sattr, dev_t rdev)
3134{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003135 struct nfs4_createdata *data;
3136 int mode = sattr->ia_mode;
3137 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
3139 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3140 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003141
3142 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3143 if (data == NULL)
3144 goto out;
3145
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003147 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003149 data->arg.ftype = NF4BLK;
3150 data->arg.u.device.specdata1 = MAJOR(rdev);
3151 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 }
3153 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003154 data->arg.ftype = NF4CHR;
3155 data->arg.u.device.specdata1 = MAJOR(rdev);
3156 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003159 status = nfs4_do_create(dir, dentry, data);
3160
3161 nfs4_free_createdata(data);
3162out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 return status;
3164}
3165
3166static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3167 struct iattr *sattr, dev_t rdev)
3168{
3169 struct nfs4_exception exception = { };
3170 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003171
3172 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 do {
3174 err = nfs4_handle_exception(NFS_SERVER(dir),
3175 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3176 &exception);
3177 } while (exception.retry);
3178 return err;
3179}
3180
3181static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3182 struct nfs_fsstat *fsstat)
3183{
3184 struct nfs4_statfs_arg args = {
3185 .fh = fhandle,
3186 .bitmask = server->attr_bitmask,
3187 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003188 struct nfs4_statfs_res res = {
3189 .fsstat = fsstat,
3190 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 struct rpc_message msg = {
3192 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3193 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003194 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 };
3196
Trond Myklebust0e574af2005-10-27 22:12:38 -04003197 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003198 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199}
3200
3201static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3202{
3203 struct nfs4_exception exception = { };
3204 int err;
3205 do {
3206 err = nfs4_handle_exception(server,
3207 _nfs4_proc_statfs(server, fhandle, fsstat),
3208 &exception);
3209 } while (exception.retry);
3210 return err;
3211}
3212
3213static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3214 struct nfs_fsinfo *fsinfo)
3215{
3216 struct nfs4_fsinfo_arg args = {
3217 .fh = fhandle,
3218 .bitmask = server->attr_bitmask,
3219 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003220 struct nfs4_fsinfo_res res = {
3221 .fsinfo = fsinfo,
3222 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 struct rpc_message msg = {
3224 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3225 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003226 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 };
3228
Bryan Schumaker7c513052011-03-24 17:12:24 +00003229 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230}
3231
3232static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3233{
3234 struct nfs4_exception exception = { };
3235 int err;
3236
3237 do {
3238 err = nfs4_handle_exception(server,
3239 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3240 &exception);
3241 } while (exception.retry);
3242 return err;
3243}
3244
3245static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3246{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003247 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3249}
3250
3251static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3252 struct nfs_pathconf *pathconf)
3253{
3254 struct nfs4_pathconf_arg args = {
3255 .fh = fhandle,
3256 .bitmask = server->attr_bitmask,
3257 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003258 struct nfs4_pathconf_res res = {
3259 .pathconf = pathconf,
3260 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 struct rpc_message msg = {
3262 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3263 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003264 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 };
3266
3267 /* None of the pathconf attributes are mandatory to implement */
3268 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3269 memset(pathconf, 0, sizeof(*pathconf));
3270 return 0;
3271 }
3272
Trond Myklebust0e574af2005-10-27 22:12:38 -04003273 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003274 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275}
3276
3277static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3278 struct nfs_pathconf *pathconf)
3279{
3280 struct nfs4_exception exception = { };
3281 int err;
3282
3283 do {
3284 err = nfs4_handle_exception(server,
3285 _nfs4_proc_pathconf(server, fhandle, pathconf),
3286 &exception);
3287 } while (exception.retry);
3288 return err;
3289}
3290
Benny Halevyd20581a2011-05-22 19:52:03 +03003291void __nfs4_read_done_cb(struct nfs_read_data *data)
3292{
3293 nfs_invalidate_atime(data->inode);
3294}
3295
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003296static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
Trond Myklebustec06c092006-03-20 13:44:27 -05003298 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003300 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003301 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003302 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003304
Benny Halevyd20581a2011-05-22 19:52:03 +03003305 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003307 renew_lease(server, data->timestamp);
3308 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309}
3310
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003311static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3312{
3313
3314 dprintk("--> %s\n", __func__);
3315
3316 if (!nfs4_sequence_done(task, &data->res.seq_res))
3317 return -EAGAIN;
3318
Benny Halevyd20581a2011-05-22 19:52:03 +03003319 return data->read_done_cb ? data->read_done_cb(task, data) :
3320 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003321}
3322
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003323static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003326 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003327 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003328 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329}
3330
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003331static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3332{
3333 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3334 &data->args.seq_args,
3335 &data->res.seq_res,
3336 task))
3337 return;
3338 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339}
3340
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003341/* Reset the the nfs_read_data to send the read to the MDS. */
3342void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3343{
3344 dprintk("%s Reset task for i/o through\n", __func__);
3345 put_lseg(data->lseg);
3346 data->lseg = NULL;
3347 /* offsets will differ in the dense stripe case */
3348 data->args.offset = data->mds_offset;
3349 data->ds_clp = NULL;
3350 data->args.fh = NFS_FH(data->inode);
3351 data->read_done_cb = nfs4_read_done_cb;
3352 task->tk_ops = data->mds_ops;
3353 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3354}
3355EXPORT_SYMBOL_GPL(nfs4_reset_read);
3356
Fred Isamanb029bc92011-03-03 15:13:42 +00003357static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 struct inode *inode = data->inode;
3360
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003361 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003362 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003363 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003365 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003367 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003368 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003369 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370}
3371
Fred Isamanb029bc92011-03-03 15:13:42 +00003372static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3373{
3374 if (!nfs4_sequence_done(task, &data->res.seq_res))
3375 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003376 return data->write_done_cb ? data->write_done_cb(task, data) :
3377 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003378}
3379
Fred Isamana69aef12011-03-03 15:13:47 +00003380/* Reset the the nfs_write_data to send the write to the MDS. */
3381void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3382{
3383 dprintk("%s Reset task for i/o through\n", __func__);
3384 put_lseg(data->lseg);
3385 data->lseg = NULL;
3386 data->ds_clp = NULL;
3387 data->write_done_cb = nfs4_write_done_cb;
3388 data->args.fh = NFS_FH(data->inode);
3389 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3390 data->args.offset = data->mds_offset;
3391 data->res.fattr = &data->fattr;
3392 task->tk_ops = data->mds_ops;
3393 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3394}
3395EXPORT_SYMBOL_GPL(nfs4_reset_write);
3396
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003397static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003399 struct nfs_server *server = NFS_SERVER(data->inode);
3400
Fred Isaman7ffd1062011-03-03 15:13:46 +00003401 if (data->lseg) {
3402 data->args.bitmask = NULL;
3403 data->res.fattr = NULL;
3404 } else
3405 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003406 if (!data->write_done_cb)
3407 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003408 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 data->timestamp = jiffies;
3410
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003411 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003412 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413}
3414
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003415static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3416{
3417 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3418 &data->args.seq_args,
3419 &data->res.seq_res,
3420 task))
3421 return;
3422 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423}
3424
Fred Isaman5f452432011-03-23 13:27:46 +00003425static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003428
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003429 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003430 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003431 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003433 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003434 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435}
3436
Fred Isaman5f452432011-03-23 13:27:46 +00003437static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3438{
3439 if (!nfs4_sequence_done(task, &data->res.seq_res))
3440 return -EAGAIN;
3441 return data->write_done_cb(task, data);
3442}
3443
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003444static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003446 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003447
3448 if (data->lseg) {
3449 data->args.bitmask = NULL;
3450 data->res.fattr = NULL;
3451 } else
3452 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003453 if (!data->write_done_cb)
3454 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003455 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003456 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003457 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458}
3459
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003460struct nfs4_renewdata {
3461 struct nfs_client *client;
3462 unsigned long timestamp;
3463};
3464
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465/*
3466 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3467 * standalone procedure for queueing an asynchronous RENEW.
3468 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003469static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003470{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003471 struct nfs4_renewdata *data = calldata;
3472 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003473
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003474 if (atomic_read(&clp->cl_count) > 1)
3475 nfs4_schedule_state_renewal(clp);
3476 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003477 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003478}
3479
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003480static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003482 struct nfs4_renewdata *data = calldata;
3483 struct nfs_client *clp = data->client;
3484 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
3486 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003487 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003488 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3489 return;
3490 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003491 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003492 return;
3493 }
3494 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003496 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497}
3498
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003499static const struct rpc_call_ops nfs4_renew_ops = {
3500 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003501 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003502};
3503
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003504static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505{
3506 struct rpc_message msg = {
3507 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3508 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003509 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003511 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003513 if (renew_flags == 0)
3514 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003515 if (!atomic_inc_not_zero(&clp->cl_count))
3516 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003517 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003518 if (data == NULL)
3519 return -ENOMEM;
3520 data->client = clp;
3521 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003523 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524}
3525
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003526static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527{
3528 struct rpc_message msg = {
3529 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3530 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003531 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 };
3533 unsigned long now = jiffies;
3534 int status;
3535
3536 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3537 if (status < 0)
3538 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003539 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 return 0;
3541}
3542
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003543static inline int nfs4_server_supports_acls(struct nfs_server *server)
3544{
3545 return (server->caps & NFS_CAP_ACLS)
3546 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3547 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3548}
3549
3550/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3551 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3552 * the stack.
3553 */
3554#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3555
Neil Hormane9e3d722011-03-04 19:26:03 -05003556static int buf_to_pages_noslab(const void *buf, size_t buflen,
3557 struct page **pages, unsigned int *pgbase)
3558{
3559 struct page *newpage, **spages;
3560 int rc = 0;
3561 size_t len;
3562 spages = pages;
3563
3564 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003565 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003566 newpage = alloc_page(GFP_KERNEL);
3567
3568 if (newpage == NULL)
3569 goto unwind;
3570 memcpy(page_address(newpage), buf, len);
3571 buf += len;
3572 buflen -= len;
3573 *pages++ = newpage;
3574 rc++;
3575 } while (buflen != 0);
3576
3577 return rc;
3578
3579unwind:
3580 for(; rc > 0; rc--)
3581 __free_page(spages[rc-1]);
3582 return -ENOMEM;
3583}
3584
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003585struct nfs4_cached_acl {
3586 int cached;
3587 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003588 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003589};
3590
3591static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003592{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003593 struct nfs_inode *nfsi = NFS_I(inode);
3594
3595 spin_lock(&inode->i_lock);
3596 kfree(nfsi->nfs4_acl);
3597 nfsi->nfs4_acl = acl;
3598 spin_unlock(&inode->i_lock);
3599}
3600
3601static void nfs4_zap_acl_attr(struct inode *inode)
3602{
3603 nfs4_set_cached_acl(inode, NULL);
3604}
3605
3606static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3607{
3608 struct nfs_inode *nfsi = NFS_I(inode);
3609 struct nfs4_cached_acl *acl;
3610 int ret = -ENOENT;
3611
3612 spin_lock(&inode->i_lock);
3613 acl = nfsi->nfs4_acl;
3614 if (acl == NULL)
3615 goto out;
3616 if (buf == NULL) /* user is just asking for length */
3617 goto out_len;
3618 if (acl->cached == 0)
3619 goto out;
3620 ret = -ERANGE; /* see getxattr(2) man page */
3621 if (acl->len > buflen)
3622 goto out;
3623 memcpy(buf, acl->data, acl->len);
3624out_len:
3625 ret = acl->len;
3626out:
3627 spin_unlock(&inode->i_lock);
3628 return ret;
3629}
3630
Sachin Prabhu5794d212012-04-17 14:36:40 +01003631static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003632{
3633 struct nfs4_cached_acl *acl;
3634
Sachin Prabhu5794d212012-04-17 14:36:40 +01003635 if (pages && acl_len <= PAGE_SIZE) {
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003636 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3637 if (acl == NULL)
3638 goto out;
3639 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003640 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003641 } else {
3642 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3643 if (acl == NULL)
3644 goto out;
3645 acl->cached = 0;
3646 }
3647 acl->len = acl_len;
3648out:
3649 nfs4_set_cached_acl(inode, acl);
3650}
3651
Andy Adamsonbf118a32011-12-07 11:55:27 -05003652/*
3653 * The getxattr API returns the required buffer length when called with a
3654 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3655 * the required buf. On a NULL buf, we send a page of data to the server
3656 * guessing that the ACL request can be serviced by a page. If so, we cache
3657 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3658 * the cache. If not so, we throw away the page, and cache the required
3659 * length. The next getxattr call will then produce another round trip to
3660 * the server, this time with the input buf of the required size.
3661 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003662static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003663{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003664 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003665 struct nfs_getaclargs args = {
3666 .fh = NFS_FH(inode),
3667 .acl_pages = pages,
3668 .acl_len = buflen,
3669 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003670 struct nfs_getaclres res = {
3671 .acl_len = buflen,
3672 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003673 struct rpc_message msg = {
3674 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3675 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003676 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003677 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003678 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003679
Andy Adamsonbf118a32011-12-07 11:55:27 -05003680 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3681 /* As long as we're doing a round trip to the server anyway,
3682 * let's be prepared for a page of acl data. */
3683 if (npages == 0)
3684 npages = 1;
3685
Sachin Prabhu5a006892012-04-17 14:35:39 +01003686 /* Add an extra page to handle the bitmap returned */
3687 npages++;
3688
Andy Adamsonbf118a32011-12-07 11:55:27 -05003689 for (i = 0; i < npages; i++) {
3690 pages[i] = alloc_page(GFP_KERNEL);
3691 if (!pages[i])
3692 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003693 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003694
3695 /* for decoding across pages */
3696 res.acl_scratch = alloc_page(GFP_KERNEL);
3697 if (!res.acl_scratch)
3698 goto out_free;
3699
Andy Adamsonbf118a32011-12-07 11:55:27 -05003700 args.acl_len = npages * PAGE_SIZE;
3701 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003702
Andy Adamsonbf118a32011-12-07 11:55:27 -05003703 /* Let decode_getfacl know not to fail if the ACL data is larger than
3704 * the page we send as a guess */
3705 if (buf == NULL)
3706 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003707
Peng Taode040be2012-01-10 22:42:47 +08003708 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003709 __func__, buf, buflen, npages, args.acl_len);
3710 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3711 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003712 if (ret)
3713 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003714
3715 acl_len = res.acl_len - res.acl_data_offset;
3716 if (acl_len > args.acl_len)
Sachin Prabhu5794d212012-04-17 14:36:40 +01003717 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003718 else
Sachin Prabhu5794d212012-04-17 14:36:40 +01003719 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
Andy Adamsonbf118a32011-12-07 11:55:27 -05003720 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003721 if (buf) {
3722 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003723 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003724 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003725 _copy_from_pages(buf, pages, res.acl_data_offset,
Sachin Prabhu20e0fa92012-03-22 16:46:28 +00003726 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003727 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003728 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003729out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003730 for (i = 0; i < npages; i++)
3731 if (pages[i])
3732 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003733 if (res.acl_scratch)
3734 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003735 return ret;
3736}
3737
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003738static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3739{
3740 struct nfs4_exception exception = { };
3741 ssize_t ret;
3742 do {
3743 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3744 if (ret >= 0)
3745 break;
3746 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3747 } while (exception.retry);
3748 return ret;
3749}
3750
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003751static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3752{
3753 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003754 int ret;
3755
3756 if (!nfs4_server_supports_acls(server))
3757 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003758 ret = nfs_revalidate_inode(server, inode);
3759 if (ret < 0)
3760 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003761 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3762 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003763 ret = nfs4_read_cached_acl(inode, buf, buflen);
3764 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003765 /* -ENOENT is returned if there is no ACL or if there is an ACL
3766 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003767 return ret;
3768 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003769}
3770
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003771static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003772{
3773 struct nfs_server *server = NFS_SERVER(inode);
3774 struct page *pages[NFS4ACL_MAXPAGES];
3775 struct nfs_setaclargs arg = {
3776 .fh = NFS_FH(inode),
3777 .acl_pages = pages,
3778 .acl_len = buflen,
3779 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003780 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003781 struct rpc_message msg = {
3782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3783 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003784 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003785 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003786 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003787
3788 if (!nfs4_server_supports_acls(server))
3789 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003790 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3791 if (i < 0)
3792 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003793 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003794 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003795
3796 /*
3797 * Free each page after tx, so the only ref left is
3798 * held by the network stack
3799 */
3800 for (; i > 0; i--)
3801 put_page(pages[i-1]);
3802
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003803 /*
3804 * Acl update can result in inode attribute update.
3805 * so mark the attribute cache invalid.
3806 */
3807 spin_lock(&inode->i_lock);
3808 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3809 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003810 nfs_access_zap_cache(inode);
3811 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003812 return ret;
3813}
3814
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003815static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3816{
3817 struct nfs4_exception exception = { };
3818 int err;
3819 do {
3820 err = nfs4_handle_exception(NFS_SERVER(inode),
3821 __nfs4_proc_set_acl(inode, buf, buflen),
3822 &exception);
3823 } while (exception.retry);
3824 return err;
3825}
3826
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003828nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003830 struct nfs_client *clp = server->nfs_client;
3831
3832 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 return 0;
3834 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003835 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003836 case -NFS4ERR_ADMIN_REVOKED:
3837 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003838 if (state == NULL)
3839 break;
3840 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003841 case -NFS4ERR_OPENMODE:
3842 if (state == NULL)
3843 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003844 nfs4_schedule_stateid_recovery(server, state);
3845 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003846 case -NFS4ERR_EXPIRED:
3847 if (state != NULL)
3848 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003850 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003851 nfs4_schedule_lease_recovery(clp);
3852 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003853#if defined(CONFIG_NFS_V4_1)
3854 case -NFS4ERR_BADSESSION:
3855 case -NFS4ERR_BADSLOT:
3856 case -NFS4ERR_BAD_HIGH_SLOT:
3857 case -NFS4ERR_DEADSESSION:
3858 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3859 case -NFS4ERR_SEQ_FALSE_RETRY:
3860 case -NFS4ERR_SEQ_MISORDERED:
3861 dprintk("%s ERROR %d, Reset session\n", __func__,
3862 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003863 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003864 task->tk_status = 0;
3865 return -EAGAIN;
3866#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003868 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003869 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003870 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3872 task->tk_status = 0;
3873 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003874 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 case -NFS4ERR_OLD_STATEID:
3876 task->tk_status = 0;
3877 return -EAGAIN;
3878 }
3879 task->tk_status = nfs4_map_errors(task->tk_status);
3880 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003881wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003882 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003883 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3884 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3885 task->tk_status = 0;
3886 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887}
3888
Chuck Levercd937102012-03-02 17:14:31 -05003889static void nfs4_construct_boot_verifier(struct nfs_client *clp,
3890 nfs4_verifier *bootverf)
3891{
3892 __be32 verf[2];
3893
3894 verf[0] = htonl((u32)clp->cl_boot_time.tv_sec);
3895 verf[1] = htonl((u32)clp->cl_boot_time.tv_nsec);
3896 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3897}
3898
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003899int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3900 unsigned short port, struct rpc_cred *cred,
3901 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902{
3903 nfs4_verifier sc_verifier;
3904 struct nfs4_setclientid setclientid = {
3905 .sc_verifier = &sc_verifier,
3906 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003907 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 };
3909 struct rpc_message msg = {
3910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3911 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003912 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003913 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 int loop = 0;
3916 int status;
3917
Chuck Levercd937102012-03-02 17:14:31 -05003918 nfs4_construct_boot_verifier(clp, &sc_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919
3920 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05003921 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003923 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003924 clp->cl_ipaddr,
3925 rpc_peeraddr2str(clp->cl_rpcclient,
3926 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003927 rpc_peeraddr2str(clp->cl_rpcclient,
3928 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003929 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 clp->cl_id_uniquifier);
3931 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003932 sizeof(setclientid.sc_netid),
3933 rpc_peeraddr2str(clp->cl_rpcclient,
3934 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003936 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05003938 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003940 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 if (status != -NFS4ERR_CLID_INUSE)
3942 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003943 if (loop != 0) {
3944 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003946 }
3947 ++loop;
3948 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 }
3950 return status;
3951}
3952
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003953int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003954 struct nfs4_setclientid_res *arg,
3955 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956{
3957 struct nfs_fsinfo fsinfo;
3958 struct rpc_message msg = {
3959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003960 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003962 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 };
3964 unsigned long now;
3965 int status;
3966
3967 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003968 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 if (status == 0) {
3970 spin_lock(&clp->cl_lock);
3971 clp->cl_lease_time = fsinfo.lease_time * HZ;
3972 clp->cl_last_renewal = now;
3973 spin_unlock(&clp->cl_lock);
3974 }
3975 return status;
3976}
3977
Trond Myklebustfe650402006-01-03 09:55:18 +01003978struct nfs4_delegreturndata {
3979 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003980 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003981 struct nfs_fh fh;
3982 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003983 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003984 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003985 int rpc_status;
3986};
3987
Trond Myklebustfe650402006-01-03 09:55:18 +01003988static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3989{
3990 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003991
Trond Myklebust14516c32010-07-31 14:29:06 -04003992 if (!nfs4_sequence_done(task, &data->res.seq_res))
3993 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003994
Ricardo Labiaga79708862009-12-07 09:23:21 -05003995 switch (task->tk_status) {
3996 case -NFS4ERR_STALE_STATEID:
3997 case -NFS4ERR_EXPIRED:
3998 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003999 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004000 break;
4001 default:
4002 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4003 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004004 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004005 return;
4006 }
4007 }
4008 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004009}
4010
4011static void nfs4_delegreturn_release(void *calldata)
4012{
Trond Myklebustfe650402006-01-03 09:55:18 +01004013 kfree(calldata);
4014}
4015
Andy Adamson938e1012009-04-01 09:22:28 -04004016#if defined(CONFIG_NFS_V4_1)
4017static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4018{
4019 struct nfs4_delegreturndata *d_data;
4020
4021 d_data = (struct nfs4_delegreturndata *)data;
4022
Trond Myklebust035168a2010-06-16 09:52:26 -04004023 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004024 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004025 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004026 return;
4027 rpc_call_start(task);
4028}
4029#endif /* CONFIG_NFS_V4_1 */
4030
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004031static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004032#if defined(CONFIG_NFS_V4_1)
4033 .rpc_call_prepare = nfs4_delegreturn_prepare,
4034#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004035 .rpc_call_done = nfs4_delegreturn_done,
4036 .rpc_release = nfs4_delegreturn_release,
4037};
4038
Trond Myklebuste6f81072008-01-24 18:14:34 -05004039static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004040{
4041 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004042 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004043 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004044 struct rpc_message msg = {
4045 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4046 .rpc_cred = cred,
4047 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004048 struct rpc_task_setup task_setup_data = {
4049 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004050 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004051 .callback_ops = &nfs4_delegreturn_ops,
4052 .flags = RPC_TASK_ASYNC,
4053 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004054 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004055
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004056 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004057 if (data == NULL)
4058 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004059 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004060 data->args.fhandle = &data->fh;
4061 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004062 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004063 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004064 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004065 data->res.fattr = &data->fattr;
4066 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004067 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004068 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004069 data->rpc_status = 0;
4070
Trond Myklebustc970aa82007-07-14 15:39:59 -04004071 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004072 msg.rpc_argp = &data->args;
4073 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004074 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004075 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004076 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004077 if (!issync)
4078 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004079 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004080 if (status != 0)
4081 goto out;
4082 status = data->rpc_status;
4083 if (status != 0)
4084 goto out;
4085 nfs_refresh_inode(inode, &data->fattr);
4086out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004087 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004088 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089}
4090
Trond Myklebuste6f81072008-01-24 18:14:34 -05004091int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092{
4093 struct nfs_server *server = NFS_SERVER(inode);
4094 struct nfs4_exception exception = { };
4095 int err;
4096 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004097 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 switch (err) {
4099 case -NFS4ERR_STALE_STATEID:
4100 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 case 0:
4102 return 0;
4103 }
4104 err = nfs4_handle_exception(server, err, &exception);
4105 } while (exception.retry);
4106 return err;
4107}
4108
4109#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4110#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4111
4112/*
4113 * sleep, with exponential backoff, and retry the LOCK operation.
4114 */
4115static unsigned long
4116nfs4_set_lock_task_retry(unsigned long timeout)
4117{
Jeff Laytond3103102011-12-01 22:44:39 +01004118 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 timeout <<= 1;
4120 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4121 return NFS4_LOCK_MAXTIMEOUT;
4122 return timeout;
4123}
4124
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4126{
4127 struct inode *inode = state->inode;
4128 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004129 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004130 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004132 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004134 struct nfs_lockt_res res = {
4135 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 };
4137 struct rpc_message msg = {
4138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4139 .rpc_argp = &arg,
4140 .rpc_resp = &res,
4141 .rpc_cred = state->owner->so_cred,
4142 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 struct nfs4_lock_state *lsp;
4144 int status;
4145
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004146 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004147 status = nfs4_set_lock_state(state, request);
4148 if (status != 0)
4149 goto out;
4150 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004151 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004152 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004153 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004154 switch (status) {
4155 case 0:
4156 request->fl_type = F_UNLCK;
4157 break;
4158 case -NFS4ERR_DENIED:
4159 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004161 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004162out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 return status;
4164}
4165
4166static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4167{
4168 struct nfs4_exception exception = { };
4169 int err;
4170
4171 do {
4172 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4173 _nfs4_proc_getlk(state, cmd, request),
4174 &exception);
4175 } while (exception.retry);
4176 return err;
4177}
4178
4179static int do_vfs_lock(struct file *file, struct file_lock *fl)
4180{
4181 int res = 0;
4182 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4183 case FL_POSIX:
4184 res = posix_lock_file_wait(file, fl);
4185 break;
4186 case FL_FLOCK:
4187 res = flock_lock_file_wait(file, fl);
4188 break;
4189 default:
4190 BUG();
4191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 return res;
4193}
4194
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004195struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004196 struct nfs_locku_args arg;
4197 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004198 struct nfs4_lock_state *lsp;
4199 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004200 struct file_lock fl;
4201 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004202 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004203};
4204
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004205static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4206 struct nfs_open_context *ctx,
4207 struct nfs4_lock_state *lsp,
4208 struct nfs_seqid *seqid)
4209{
4210 struct nfs4_unlockdata *p;
4211 struct inode *inode = lsp->ls_state->inode;
4212
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004213 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004214 if (p == NULL)
4215 return NULL;
4216 p->arg.fh = NFS_FH(inode);
4217 p->arg.fl = &p->fl;
4218 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004219 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004220 p->arg.stateid = &lsp->ls_stateid;
4221 p->lsp = lsp;
4222 atomic_inc(&lsp->ls_count);
4223 /* Ensure we don't close file until we're done freeing locks! */
4224 p->ctx = get_nfs_open_context(ctx);
4225 memcpy(&p->fl, fl, sizeof(p->fl));
4226 p->server = NFS_SERVER(inode);
4227 return p;
4228}
4229
Trond Myklebust06f814a2006-01-03 09:55:07 +01004230static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004231{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004232 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004233 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004234 nfs4_put_lock_state(calldata->lsp);
4235 put_nfs_open_context(calldata->ctx);
4236 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004237}
4238
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004239static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004240{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004241 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004242
Trond Myklebust14516c32010-07-31 14:29:06 -04004243 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4244 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004245 switch (task->tk_status) {
4246 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004247 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4248 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004249 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004250 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004251 case -NFS4ERR_BAD_STATEID:
4252 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004253 case -NFS4ERR_STALE_STATEID:
4254 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004255 break;
4256 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004257 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004258 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004259 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004260}
4261
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004262static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004263{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004264 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004266 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004267 return;
4268 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004269 /* Note: exit _without_ running nfs4_locku_done */
4270 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004271 return;
4272 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004273 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004274 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004275 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004276 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004277 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004278 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279}
4280
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004281static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004282 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004283 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004284 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004285};
4286
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004287static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4288 struct nfs_open_context *ctx,
4289 struct nfs4_lock_state *lsp,
4290 struct nfs_seqid *seqid)
4291{
4292 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004293 struct rpc_message msg = {
4294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4295 .rpc_cred = ctx->cred,
4296 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004297 struct rpc_task_setup task_setup_data = {
4298 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004299 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004300 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004301 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004302 .flags = RPC_TASK_ASYNC,
4303 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004304
Frank Filz137d6ac2007-07-09 15:32:29 -07004305 /* Ensure this is an unlock - when canceling a lock, the
4306 * canceled lock is passed in, and it won't be an unlock.
4307 */
4308 fl->fl_type = F_UNLCK;
4309
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004310 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4311 if (data == NULL) {
4312 nfs_free_seqid(seqid);
4313 return ERR_PTR(-ENOMEM);
4314 }
4315
Trond Myklebust9d12b212012-01-17 22:04:25 -05004316 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004317 msg.rpc_argp = &data->arg;
4318 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004319 task_setup_data.callback_data = data;
4320 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004321}
4322
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4324{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004325 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004326 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004327 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004328 struct rpc_task *task;
4329 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004330 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331
Trond Myklebust9b073572006-06-29 16:38:34 -04004332 status = nfs4_set_lock_state(state, request);
4333 /* Unlock _before_ we do the RPC call */
4334 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004335 down_read(&nfsi->rwsem);
4336 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4337 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004338 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004339 }
4340 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004341 if (status != 0)
4342 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004343 /* Is this a delegated lock? */
4344 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004345 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004346 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004347 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004348 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004349 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004350 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004351 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004352 status = PTR_ERR(task);
4353 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004354 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004355 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004356 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004357out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004358 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004359 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360}
4361
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004362struct nfs4_lockdata {
4363 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004364 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004365 struct nfs4_lock_state *lsp;
4366 struct nfs_open_context *ctx;
4367 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004368 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004369 int rpc_status;
4370 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004371 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004372};
4373
4374static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004375 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4376 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004377{
4378 struct nfs4_lockdata *p;
4379 struct inode *inode = lsp->ls_state->inode;
4380 struct nfs_server *server = NFS_SERVER(inode);
4381
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004382 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004383 if (p == NULL)
4384 return NULL;
4385
4386 p->arg.fh = NFS_FH(inode);
4387 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004388 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004389 if (p->arg.open_seqid == NULL)
4390 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004391 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004392 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004393 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004394 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004395 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004396 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004397 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004398 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004399 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004400 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004401 atomic_inc(&lsp->ls_count);
4402 p->ctx = get_nfs_open_context(ctx);
4403 memcpy(&p->fl, fl, sizeof(p->fl));
4404 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004405out_free_seqid:
4406 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004407out_free:
4408 kfree(p);
4409 return NULL;
4410}
4411
4412static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4413{
4414 struct nfs4_lockdata *data = calldata;
4415 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
Harvey Harrison3110ff82008-05-02 13:42:44 -07004417 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004418 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4419 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004420 /* Do we need to do an open_to_lock_owner? */
4421 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004422 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4423 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004424 data->arg.open_stateid = &state->stateid;
4425 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004426 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004427 } else
4428 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004429 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004430 if (nfs4_setup_sequence(data->server,
4431 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004432 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004433 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004434 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004435 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004436}
4437
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004438static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4439{
4440 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4441 nfs4_lock_prepare(task, calldata);
4442}
4443
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004444static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4445{
4446 struct nfs4_lockdata *data = calldata;
4447
Harvey Harrison3110ff82008-05-02 13:42:44 -07004448 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004449
Trond Myklebust14516c32010-07-31 14:29:06 -04004450 if (!nfs4_sequence_done(task, &data->res.seq_res))
4451 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004452
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004453 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004454 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004455 if (data->rpc_status == 0)
4456 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4457 else
4458 goto out;
4459 }
4460 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004461 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004462 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004463 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004464 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004465out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004466 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004467}
4468
4469static void nfs4_lock_release(void *calldata)
4470{
4471 struct nfs4_lockdata *data = calldata;
4472
Harvey Harrison3110ff82008-05-02 13:42:44 -07004473 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004474 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004475 if (data->cancelled != 0) {
4476 struct rpc_task *task;
4477 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4478 data->arg.lock_seqid);
4479 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004480 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004481 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004482 } else
4483 nfs_free_seqid(data->arg.lock_seqid);
4484 nfs4_put_lock_state(data->lsp);
4485 put_nfs_open_context(data->ctx);
4486 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004487 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004488}
4489
4490static const struct rpc_call_ops nfs4_lock_ops = {
4491 .rpc_call_prepare = nfs4_lock_prepare,
4492 .rpc_call_done = nfs4_lock_done,
4493 .rpc_release = nfs4_lock_release,
4494};
4495
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004496static const struct rpc_call_ops nfs4_recover_lock_ops = {
4497 .rpc_call_prepare = nfs4_recover_lock_prepare,
4498 .rpc_call_done = nfs4_lock_done,
4499 .rpc_release = nfs4_lock_release,
4500};
4501
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004502static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4503{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004504 switch (error) {
4505 case -NFS4ERR_ADMIN_REVOKED:
4506 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004507 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004508 if (new_lock_owner != 0 ||
4509 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004510 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004511 break;
4512 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004513 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004514 case -NFS4ERR_EXPIRED:
4515 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004516 };
4517}
4518
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004519static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004520{
4521 struct nfs4_lockdata *data;
4522 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004523 struct rpc_message msg = {
4524 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4525 .rpc_cred = state->owner->so_cred,
4526 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004527 struct rpc_task_setup task_setup_data = {
4528 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004529 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004530 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004531 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004532 .flags = RPC_TASK_ASYNC,
4533 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004534 int ret;
4535
Harvey Harrison3110ff82008-05-02 13:42:44 -07004536 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004537 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004538 fl->fl_u.nfs4_fl.owner,
4539 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004540 if (data == NULL)
4541 return -ENOMEM;
4542 if (IS_SETLKW(cmd))
4543 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004544 if (recovery_type > NFS_LOCK_NEW) {
4545 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004546 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004547 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4548 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004549 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004550 msg.rpc_argp = &data->arg;
4551 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004552 task_setup_data.callback_data = data;
4553 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004554 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004555 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004556 ret = nfs4_wait_for_completion_rpc_task(task);
4557 if (ret == 0) {
4558 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004559 if (ret)
4560 nfs4_handle_setlk_error(data->server, data->lsp,
4561 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004562 } else
4563 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004564 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004565 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004566 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567}
4568
4569static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4570{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004571 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004572 struct nfs4_exception exception = {
4573 .inode = state->inode,
4574 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004575 int err;
4576
4577 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004578 /* Cache the lock if possible... */
4579 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4580 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004581 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004582 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004583 break;
4584 nfs4_handle_exception(server, err, &exception);
4585 } while (exception.retry);
4586 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587}
4588
4589static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4590{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004591 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004592 struct nfs4_exception exception = {
4593 .inode = state->inode,
4594 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004595 int err;
4596
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004597 err = nfs4_set_lock_state(state, request);
4598 if (err != 0)
4599 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004600 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004601 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4602 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004603 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004604 switch (err) {
4605 default:
4606 goto out;
4607 case -NFS4ERR_GRACE:
4608 case -NFS4ERR_DELAY:
4609 nfs4_handle_exception(server, err, &exception);
4610 err = 0;
4611 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004612 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004613out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004614 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615}
4616
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004617#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004618static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004619{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004620 int status, ret = NFS_OK;
4621 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004622 struct nfs_server *server = NFS_SERVER(state->inode);
4623
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004624 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4625 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4626 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4627 if (status != NFS_OK) {
4628 nfs41_free_stateid(server, &lsp->ls_stateid);
4629 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4630 ret = status;
4631 }
4632 }
4633 };
4634
4635 return ret;
4636}
4637
4638static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4639{
4640 int status = NFS_OK;
4641
4642 if (test_bit(LK_STATE_IN_USE, &state->flags))
4643 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004644 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004645 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004646 return nfs4_lock_expired(state, request);
4647}
4648#endif
4649
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4651{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004652 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004653 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004654 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004656 if ((fl_flags & FL_POSIX) &&
4657 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4658 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004659 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004660 status = nfs4_set_lock_state(state, request);
4661 if (status != 0)
4662 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004663 request->fl_flags |= FL_ACCESS;
4664 status = do_vfs_lock(request->fl_file, request);
4665 if (status < 0)
4666 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004667 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004668 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004669 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004670 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004671 request->fl_flags = fl_flags & ~FL_SLEEP;
4672 status = do_vfs_lock(request->fl_file, request);
4673 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004674 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004675 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004676 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004677 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004678 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004679 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004680 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004681 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4682 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004683out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004684 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004685out:
4686 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 return status;
4688}
4689
4690static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4691{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004692 struct nfs4_exception exception = {
4693 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004694 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004695 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 int err;
4697
4698 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004699 err = _nfs4_proc_setlk(state, cmd, request);
4700 if (err == -NFS4ERR_DENIED)
4701 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004703 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 } while (exception.retry);
4705 return err;
4706}
4707
4708static int
4709nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4710{
4711 struct nfs_open_context *ctx;
4712 struct nfs4_state *state;
4713 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4714 int status;
4715
4716 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004717 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 state = ctx->state;
4719
4720 if (request->fl_start < 0 || request->fl_end < 0)
4721 return -EINVAL;
4722
Trond Myklebustd9531262009-07-21 19:22:38 -04004723 if (IS_GETLK(cmd)) {
4724 if (state != NULL)
4725 return nfs4_proc_getlk(state, F_GETLK, request);
4726 return 0;
4727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728
4729 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4730 return -EINVAL;
4731
Trond Myklebustd9531262009-07-21 19:22:38 -04004732 if (request->fl_type == F_UNLCK) {
4733 if (state != NULL)
4734 return nfs4_proc_unlck(state, cmd, request);
4735 return 0;
4736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737
Trond Myklebustd9531262009-07-21 19:22:38 -04004738 if (state == NULL)
4739 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004740 /*
4741 * Don't rely on the VFS having checked the file open mode,
4742 * since it won't do this for flock() locks.
4743 */
4744 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4745 case F_RDLCK:
4746 if (!(filp->f_mode & FMODE_READ))
4747 return -EBADF;
4748 break;
4749 case F_WRLCK:
4750 if (!(filp->f_mode & FMODE_WRITE))
4751 return -EBADF;
4752 }
4753
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 do {
4755 status = nfs4_proc_setlk(state, cmd, request);
4756 if ((status != -EAGAIN) || IS_SETLK(cmd))
4757 break;
4758 timeout = nfs4_set_lock_task_retry(timeout);
4759 status = -ERESTARTSYS;
4760 if (signalled())
4761 break;
4762 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 return status;
4764}
4765
Trond Myklebust888e6942005-11-04 15:38:11 -05004766int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4767{
4768 struct nfs_server *server = NFS_SERVER(state->inode);
4769 struct nfs4_exception exception = { };
4770 int err;
4771
4772 err = nfs4_set_lock_state(state, fl);
4773 if (err != 0)
4774 goto out;
4775 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004776 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004777 switch (err) {
4778 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004779 printk(KERN_ERR "NFS: %s: unhandled error "
4780 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004781 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004782 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004783 goto out;
4784 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004785 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004786 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004787 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004788 nfs4_schedule_lease_recovery(server->nfs_client);
4789 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004790 case -NFS4ERR_BADSESSION:
4791 case -NFS4ERR_BADSLOT:
4792 case -NFS4ERR_BAD_HIGH_SLOT:
4793 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4794 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004795 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004796 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004797 case -ERESTARTSYS:
4798 /*
4799 * The show must go on: exit, but mark the
4800 * stateid as needing recovery.
4801 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004802 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004803 case -NFS4ERR_ADMIN_REVOKED:
4804 case -NFS4ERR_BAD_STATEID:
4805 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004806 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004807 err = 0;
4808 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004809 case -EKEYEXPIRED:
4810 /*
4811 * User RPCSEC_GSS context has expired.
4812 * We cannot recover this stateid now, so
4813 * skip it and allow recovery thread to
4814 * proceed.
4815 */
4816 err = 0;
4817 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004818 case -ENOMEM:
4819 case -NFS4ERR_DENIED:
4820 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4821 err = 0;
4822 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004823 case -NFS4ERR_DELAY:
4824 break;
4825 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004826 err = nfs4_handle_exception(server, err, &exception);
4827 } while (exception.retry);
4828out:
4829 return err;
4830}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004831
Trond Myklebustcf470c32012-03-07 13:49:12 -05004832struct nfs_release_lockowner_data {
4833 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004834 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004835 struct nfs_release_lockowner_args args;
4836};
4837
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004838static void nfs4_release_lockowner_release(void *calldata)
4839{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004840 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004841 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004842 kfree(calldata);
4843}
4844
Trond Myklebust17280172012-03-11 13:11:00 -04004845static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004846 .rpc_release = nfs4_release_lockowner_release,
4847};
4848
Trond Myklebustcf470c32012-03-07 13:49:12 -05004849int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004850{
4851 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004852 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004853 struct rpc_message msg = {
4854 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4855 };
4856
4857 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004858 return -EINVAL;
4859 data = kmalloc(sizeof(*data), GFP_NOFS);
4860 if (!data)
4861 return -ENOMEM;
4862 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004863 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004864 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4865 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4866 data->args.lock_owner.s_dev = server->s_dev;
4867 msg.rpc_argp = &data->args;
4868 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4869 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004870}
4871
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004872#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4873
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004874static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4875 const void *buf, size_t buflen,
4876 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004877{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004878 if (strcmp(key, "") != 0)
4879 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004880
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004881 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004882}
4883
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004884static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4885 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004886{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004887 if (strcmp(key, "") != 0)
4888 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004889
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004890 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004891}
4892
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004893static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4894 size_t list_len, const char *name,
4895 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004896{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004897 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004898
J. Bruce Fields096455a2006-03-20 23:23:42 -05004899 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4900 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004901
4902 if (list && len <= list_len)
4903 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004904 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004905}
4906
Andy Adamson533eb462011-06-13 18:25:56 -04004907/*
4908 * nfs_fhget will use either the mounted_on_fileid or the fileid
4909 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004910static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4911{
Andy Adamson533eb462011-06-13 18:25:56 -04004912 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4913 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4914 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05004915 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004916 return;
4917
4918 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05004919 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004920 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4921 fattr->nlink = 2;
4922}
4923
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04004924static int _nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004925 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004926{
4927 struct nfs_server *server = NFS_SERVER(dir);
4928 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004929 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004930 };
4931 struct nfs4_fs_locations_arg args = {
4932 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004933 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004934 .page = page,
4935 .bitmask = bitmask,
4936 };
Benny Halevy22958462009-04-01 09:22:02 -04004937 struct nfs4_fs_locations_res res = {
4938 .fs_locations = fs_locations,
4939 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004940 struct rpc_message msg = {
4941 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4942 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004943 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004944 };
4945 int status;
4946
Harvey Harrison3110ff82008-05-02 13:42:44 -07004947 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004948
4949 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4950 * is not supported */
4951 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4952 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4953 else
4954 bitmask[0] |= FATTR4_WORD0_FILEID;
4955
Trond Myklebustc228fd32007-01-13 02:28:11 -05004956 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004957 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004958 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004959 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004960 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004961 return status;
4962}
4963
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04004964int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4965 struct nfs4_fs_locations *fs_locations, struct page *page)
4966{
4967 struct nfs4_exception exception = { };
4968 int err;
4969 do {
4970 err = nfs4_handle_exception(NFS_SERVER(dir),
4971 _nfs4_proc_fs_locations(dir, name, fs_locations, page),
4972 &exception);
4973 } while (exception.retry);
4974 return err;
4975}
4976
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004977static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4978{
4979 int status;
4980 struct nfs4_secinfo_arg args = {
4981 .dir_fh = NFS_FH(dir),
4982 .name = name,
4983 };
4984 struct nfs4_secinfo_res res = {
4985 .flavors = flavors,
4986 };
4987 struct rpc_message msg = {
4988 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4989 .rpc_argp = &args,
4990 .rpc_resp = &res,
4991 };
4992
4993 dprintk("NFS call secinfo %s\n", name->name);
4994 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4995 dprintk("NFS reply secinfo: %d\n", status);
4996 return status;
4997}
4998
Trond Myklebust17280172012-03-11 13:11:00 -04004999static int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5000 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005001{
5002 struct nfs4_exception exception = { };
5003 int err;
5004 do {
5005 err = nfs4_handle_exception(NFS_SERVER(dir),
5006 _nfs4_proc_secinfo(dir, name, flavors),
5007 &exception);
5008 } while (exception.retry);
5009 return err;
5010}
5011
Andy Adamson557134a2009-04-01 09:21:53 -04005012#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005013/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005014 * Check the exchange flags returned by the server for invalid flags, having
5015 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5016 * DS flags set.
5017 */
5018static int nfs4_check_cl_exchange_flags(u32 flags)
5019{
5020 if (flags & ~EXCHGID4_FLAG_MASK_R)
5021 goto out_inval;
5022 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5023 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5024 goto out_inval;
5025 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5026 goto out_inval;
5027 return NFS_OK;
5028out_inval:
5029 return -NFS4ERR_INVAL;
5030}
5031
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005032static bool
5033nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
5034{
5035 if (a->server_scope_sz == b->server_scope_sz &&
5036 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5037 return true;
5038
5039 return false;
5040}
5041
Andy Adamson357f54d2010-12-14 10:11:57 -05005042/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005043 * nfs4_proc_exchange_id()
5044 *
5045 * Since the clientid has expired, all compounds using sessions
5046 * associated with the stale clientid will be returning
5047 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5048 * be in some phase of session reset.
5049 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005050int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005051{
5052 nfs4_verifier verifier;
5053 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005054 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005055 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005056 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005057 };
5058 struct nfs41_exchange_id_res res = {
5059 .client = clp,
5060 };
5061 int status;
5062 struct rpc_message msg = {
5063 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5064 .rpc_argp = &args,
5065 .rpc_resp = &res,
5066 .rpc_cred = cred,
5067 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005068
5069 dprintk("--> %s\n", __func__);
5070 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04005071
Chuck Levercd937102012-03-02 17:14:31 -05005072 nfs4_construct_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005073
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005074 args.id_len = scnprintf(args.id, sizeof(args.id),
5075 "%s/%s.%s/%u",
5076 clp->cl_ipaddr,
5077 init_utsname()->nodename,
5078 init_utsname()->domainname,
5079 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005080
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005081 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005082 if (unlikely(!res.server_scope)) {
5083 status = -ENOMEM;
5084 goto out;
5085 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005086
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005087 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
5088 if (unlikely(!res.impl_id)) {
5089 status = -ENOMEM;
5090 goto out_server_scope;
5091 }
5092
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005093 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005094 if (!status)
5095 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005096
5097 if (!status) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005098 /* use the most recent implementation id */
5099 kfree(clp->impl_id);
5100 clp->impl_id = res.impl_id;
5101 } else
5102 kfree(res.impl_id);
5103
5104 if (!status) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005105 if (clp->server_scope &&
5106 !nfs41_same_server_scope(clp->server_scope,
5107 res.server_scope)) {
5108 dprintk("%s: server_scope mismatch detected\n",
5109 __func__);
5110 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5111 kfree(clp->server_scope);
5112 clp->server_scope = NULL;
5113 }
5114
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005115 if (!clp->server_scope) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005116 clp->server_scope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005117 goto out;
5118 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005119 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005120
5121out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005122 kfree(res.server_scope);
5123out:
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005124 if (clp->impl_id)
5125 dprintk("%s: Server Implementation ID: "
5126 "domain: %s, name: %s, date: %llu,%u\n",
5127 __func__, clp->impl_id->domain, clp->impl_id->name,
5128 clp->impl_id->date.seconds,
5129 clp->impl_id->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005130 dprintk("<-- %s status= %d\n", __func__, status);
5131 return status;
5132}
5133
Andy Adamson2050f0c2009-04-01 09:22:30 -04005134struct nfs4_get_lease_time_data {
5135 struct nfs4_get_lease_time_args *args;
5136 struct nfs4_get_lease_time_res *res;
5137 struct nfs_client *clp;
5138};
5139
5140static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5141 void *calldata)
5142{
5143 int ret;
5144 struct nfs4_get_lease_time_data *data =
5145 (struct nfs4_get_lease_time_data *)calldata;
5146
5147 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005148 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005149 /* just setup sequence, do not trigger session recovery
5150 since we're invoked within one */
5151 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005152 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005153 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005154
5155 BUG_ON(ret == -EAGAIN);
5156 rpc_call_start(task);
5157 dprintk("<-- %s\n", __func__);
5158}
5159
5160/*
5161 * Called from nfs4_state_manager thread for session setup, so don't recover
5162 * from sequence operation or clientid errors.
5163 */
5164static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5165{
5166 struct nfs4_get_lease_time_data *data =
5167 (struct nfs4_get_lease_time_data *)calldata;
5168
5169 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005170 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5171 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005172 switch (task->tk_status) {
5173 case -NFS4ERR_DELAY:
5174 case -NFS4ERR_GRACE:
5175 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5176 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5177 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005178 /* fall through */
5179 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005180 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005181 return;
5182 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005183 dprintk("<-- %s\n", __func__);
5184}
5185
Trond Myklebust17280172012-03-11 13:11:00 -04005186static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005187 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5188 .rpc_call_done = nfs4_get_lease_time_done,
5189};
5190
5191int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5192{
5193 struct rpc_task *task;
5194 struct nfs4_get_lease_time_args args;
5195 struct nfs4_get_lease_time_res res = {
5196 .lr_fsinfo = fsinfo,
5197 };
5198 struct nfs4_get_lease_time_data data = {
5199 .args = &args,
5200 .res = &res,
5201 .clp = clp,
5202 };
5203 struct rpc_message msg = {
5204 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5205 .rpc_argp = &args,
5206 .rpc_resp = &res,
5207 };
5208 struct rpc_task_setup task_setup = {
5209 .rpc_client = clp->cl_rpcclient,
5210 .rpc_message = &msg,
5211 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005212 .callback_data = &data,
5213 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005214 };
5215 int status;
5216
Trond Myklebust9d12b212012-01-17 22:04:25 -05005217 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005218 dprintk("--> %s\n", __func__);
5219 task = rpc_run_task(&task_setup);
5220
5221 if (IS_ERR(task))
5222 status = PTR_ERR(task);
5223 else {
5224 status = task->tk_status;
5225 rpc_put_task(task);
5226 }
5227 dprintk("<-- %s return %d\n", __func__, status);
5228
5229 return status;
5230}
5231
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005232static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5233{
5234 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5235}
5236
5237static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5238 struct nfs4_slot *new,
5239 u32 max_slots,
5240 u32 ivalue)
5241{
5242 struct nfs4_slot *old = NULL;
5243 u32 i;
5244
5245 spin_lock(&tbl->slot_tbl_lock);
5246 if (new) {
5247 old = tbl->slots;
5248 tbl->slots = new;
5249 tbl->max_slots = max_slots;
5250 }
5251 tbl->highest_used_slotid = -1; /* no slot is currently used */
5252 for (i = 0; i < tbl->max_slots; i++)
5253 tbl->slots[i].seq_nr = ivalue;
5254 spin_unlock(&tbl->slot_tbl_lock);
5255 kfree(old);
5256}
5257
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005258/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005259 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005260 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005261static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5262 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005263{
Andy Adamson104aeba2010-01-14 17:45:10 -05005264 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005265 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005266
Andy Adamson104aeba2010-01-14 17:45:10 -05005267 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5268 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005269
Andy Adamson104aeba2010-01-14 17:45:10 -05005270 /* Does the newly negotiated max_reqs match the existing slot table? */
5271 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005272 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005273 if (!new)
5274 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005275 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005276 ret = 0;
5277
5278 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005279 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5280 tbl, tbl->slots, tbl->max_slots);
5281out:
5282 dprintk("<-- %s: return %d\n", __func__, ret);
5283 return ret;
5284}
5285
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005286/* Destroy the slot table */
5287static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5288{
5289 if (session->fc_slot_table.slots != NULL) {
5290 kfree(session->fc_slot_table.slots);
5291 session->fc_slot_table.slots = NULL;
5292 }
5293 if (session->bc_slot_table.slots != NULL) {
5294 kfree(session->bc_slot_table.slots);
5295 session->bc_slot_table.slots = NULL;
5296 }
5297 return;
5298}
5299
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005300/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005301 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005302 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005303static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005304{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005305 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005306 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005307
Andy Adamsonaacd5532011-11-09 13:58:21 -05005308 dprintk("--> %s\n", __func__);
5309 /* Fore channel */
5310 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005311 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5312 if (status) /* -ENOMEM */
5313 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005314 /* Back channel */
5315 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005316 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5317 if (status && tbl->slots == NULL)
5318 /* Fore and back channel share a connection so get
5319 * both slot tables or neither */
5320 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005321 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005322}
5323
5324struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5325{
5326 struct nfs4_session *session;
5327 struct nfs4_slot_table *tbl;
5328
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005329 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005330 if (!session)
5331 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005332
Andy Adamson557134a2009-04-01 09:21:53 -04005333 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005334 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005335 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005336 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005337 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005338
5339 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005340 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005341 spin_lock_init(&tbl->slot_tbl_lock);
5342 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005343 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005344
Trond Myklebust1055d762010-06-16 09:52:27 -04005345 session->session_state = 1<<NFS4_SESSION_INITING;
5346
Andy Adamson557134a2009-04-01 09:21:53 -04005347 session->clp = clp;
5348 return session;
5349}
5350
5351void nfs4_destroy_session(struct nfs4_session *session)
5352{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005353 struct rpc_xprt *xprt;
5354
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005355 nfs4_proc_destroy_session(session);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005356
5357 rcu_read_lock();
5358 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5359 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005360 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005361 __func__, xprt);
5362 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005363 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005364 kfree(session);
5365}
5366
Andy Adamsonfc931582009-04-01 09:22:31 -04005367/*
5368 * Initialize the values to be used by the client in CREATE_SESSION
5369 * If nfs4_init_session set the fore channel request and response sizes,
5370 * use them.
5371 *
5372 * Set the back channel max_resp_sz_cached to zero to force the client to
5373 * always set csa_cachethis to FALSE because the current implementation
5374 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5375 */
5376static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5377{
5378 struct nfs4_session *session = args->client->cl_session;
5379 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5380 mxresp_sz = session->fc_attrs.max_resp_sz;
5381
5382 if (mxrqst_sz == 0)
5383 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5384 if (mxresp_sz == 0)
5385 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5386 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005387 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5388 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005389 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005390 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005391
5392 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005393 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005394 __func__,
5395 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005396 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005397
5398 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005399 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5400 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5401 args->bc_attrs.max_resp_sz_cached = 0;
5402 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5403 args->bc_attrs.max_reqs = 1;
5404
5405 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5406 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5407 __func__,
5408 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5409 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5410 args->bc_attrs.max_reqs);
5411}
5412
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005413static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005414{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005415 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5416 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5417
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005418 if (rcvd->max_resp_sz > sent->max_resp_sz)
5419 return -EINVAL;
5420 /*
5421 * Our requested max_ops is the minimum we need; we're not
5422 * prepared to break up compounds into smaller pieces than that.
5423 * So, no point even trying to continue if the server won't
5424 * cooperate:
5425 */
5426 if (rcvd->max_ops < sent->max_ops)
5427 return -EINVAL;
5428 if (rcvd->max_reqs == 0)
5429 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005430 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5431 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005432 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005433}
5434
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005435static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5436{
5437 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5438 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005439
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005440 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5441 return -EINVAL;
5442 if (rcvd->max_resp_sz < sent->max_resp_sz)
5443 return -EINVAL;
5444 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5445 return -EINVAL;
5446 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005447 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005448 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005449 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005450 return -EINVAL;
5451 return 0;
5452}
Andy Adamson8d353012009-04-01 09:22:32 -04005453
Andy Adamson8d353012009-04-01 09:22:32 -04005454static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5455 struct nfs4_session *session)
5456{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005457 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005458
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005459 ret = nfs4_verify_fore_channel_attrs(args, session);
5460 if (ret)
5461 return ret;
5462 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005463}
5464
Andy Adamsonfc931582009-04-01 09:22:31 -04005465static int _nfs4_proc_create_session(struct nfs_client *clp)
5466{
5467 struct nfs4_session *session = clp->cl_session;
5468 struct nfs41_create_session_args args = {
5469 .client = clp,
5470 .cb_program = NFS4_CALLBACK,
5471 };
5472 struct nfs41_create_session_res res = {
5473 .client = clp,
5474 };
5475 struct rpc_message msg = {
5476 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5477 .rpc_argp = &args,
5478 .rpc_resp = &res,
5479 };
5480 int status;
5481
5482 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005483 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005484
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005485 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005486
Andy Adamson8d353012009-04-01 09:22:32 -04005487 if (!status)
5488 /* Verify the session's negotiated channel_attrs values */
5489 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005490 if (!status) {
5491 /* Increment the clientid slot sequence id */
5492 clp->cl_seqid++;
5493 }
5494
5495 return status;
5496}
5497
5498/*
5499 * Issues a CREATE_SESSION operation to the server.
5500 * It is the responsibility of the caller to verify the session is
5501 * expired before calling this routine.
5502 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005503int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005504{
5505 int status;
5506 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005507 struct nfs4_session *session = clp->cl_session;
5508
5509 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5510
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005511 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005512 if (status)
5513 goto out;
5514
Andy Adamsonaacd5532011-11-09 13:58:21 -05005515 /* Init or reset the session slot tables */
5516 status = nfs4_setup_session_slot_tables(session);
5517 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005518 if (status)
5519 goto out;
5520
5521 ptr = (unsigned *)&session->sess_id.data[0];
5522 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5523 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005524out:
5525 dprintk("<-- %s\n", __func__);
5526 return status;
5527}
5528
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005529/*
5530 * Issue the over-the-wire RPC DESTROY_SESSION.
5531 * The caller must serialize access to this routine.
5532 */
5533int nfs4_proc_destroy_session(struct nfs4_session *session)
5534{
5535 int status = 0;
5536 struct rpc_message msg;
5537
5538 dprintk("--> nfs4_proc_destroy_session\n");
5539
5540 /* session is still being setup */
5541 if (session->clp->cl_cons_state != NFS_CS_READY)
5542 return status;
5543
5544 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5545 msg.rpc_argp = session;
5546 msg.rpc_resp = NULL;
5547 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005548 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005549
5550 if (status)
5551 printk(KERN_WARNING
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -05005552 "NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005553 "Session has been destroyed regardless...\n", status);
5554
5555 dprintk("<-- nfs4_proc_destroy_session\n");
5556 return status;
5557}
5558
Trond Myklebustfccba802009-07-21 16:48:07 -04005559int nfs4_init_session(struct nfs_server *server)
5560{
5561 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005562 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005563 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005564 int ret;
5565
5566 if (!nfs4_has_session(clp))
5567 return 0;
5568
Trond Myklebust1055d762010-06-16 09:52:27 -04005569 session = clp->cl_session;
5570 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5571 return 0;
5572
Andy Adamson68bf05e2009-12-15 12:55:02 -05005573 rsize = server->rsize;
5574 if (rsize == 0)
5575 rsize = NFS_MAX_FILE_IO_SIZE;
5576 wsize = server->wsize;
5577 if (wsize == 0)
5578 wsize = NFS_MAX_FILE_IO_SIZE;
5579
Andy Adamson68bf05e2009-12-15 12:55:02 -05005580 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5581 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005582
Trond Myklebustfccba802009-07-21 16:48:07 -04005583 ret = nfs4_recover_expired_lease(server);
5584 if (!ret)
5585 ret = nfs4_check_client_ready(clp);
5586 return ret;
5587}
5588
Andy Adamsond83217c2011-03-01 01:34:17 +00005589int nfs4_init_ds_session(struct nfs_client *clp)
5590{
5591 struct nfs4_session *session = clp->cl_session;
5592 int ret;
5593
5594 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5595 return 0;
5596
5597 ret = nfs4_client_recover_expired_lease(clp);
5598 if (!ret)
5599 /* Test for the DS role */
5600 if (!is_ds_client(clp))
5601 ret = -ENODEV;
5602 if (!ret)
5603 ret = nfs4_check_client_ready(clp);
5604 return ret;
5605
5606}
5607EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5608
5609
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005610/*
5611 * Renew the cl_session lease.
5612 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005613struct nfs4_sequence_data {
5614 struct nfs_client *clp;
5615 struct nfs4_sequence_args args;
5616 struct nfs4_sequence_res res;
5617};
5618
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005619static void nfs41_sequence_release(void *data)
5620{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005621 struct nfs4_sequence_data *calldata = data;
5622 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005623
Alexandros Batsakis71358402010-02-05 03:45:05 -08005624 if (atomic_read(&clp->cl_count) > 1)
5625 nfs4_schedule_state_renewal(clp);
5626 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005627 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005628}
5629
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005630static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5631{
5632 switch(task->tk_status) {
5633 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005634 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5635 return -EAGAIN;
5636 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005637 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005638 }
5639 return 0;
5640}
5641
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005642static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005643{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005644 struct nfs4_sequence_data *calldata = data;
5645 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005646
Trond Myklebust14516c32010-07-31 14:29:06 -04005647 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5648 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005649
5650 if (task->tk_status < 0) {
5651 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005652 if (atomic_read(&clp->cl_count) == 1)
5653 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005654
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005655 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5656 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005657 return;
5658 }
5659 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005660 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005661out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005662 dprintk("<-- %s\n", __func__);
5663}
5664
5665static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5666{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005667 struct nfs4_sequence_data *calldata = data;
5668 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005669 struct nfs4_sequence_args *args;
5670 struct nfs4_sequence_res *res;
5671
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005672 args = task->tk_msg.rpc_argp;
5673 res = task->tk_msg.rpc_resp;
5674
Trond Myklebust9d12b212012-01-17 22:04:25 -05005675 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005676 return;
5677 rpc_call_start(task);
5678}
5679
5680static const struct rpc_call_ops nfs41_sequence_ops = {
5681 .rpc_call_done = nfs41_sequence_call_done,
5682 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005683 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005684};
5685
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005686static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005687{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005688 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005689 struct rpc_message msg = {
5690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5691 .rpc_cred = cred,
5692 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005693 struct rpc_task_setup task_setup_data = {
5694 .rpc_client = clp->cl_rpcclient,
5695 .rpc_message = &msg,
5696 .callback_ops = &nfs41_sequence_ops,
5697 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5698 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005699
Alexandros Batsakis71358402010-02-05 03:45:05 -08005700 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005701 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005702 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005703 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005704 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005705 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005706 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005707 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005708 msg.rpc_argp = &calldata->args;
5709 msg.rpc_resp = &calldata->res;
5710 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005711 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005712
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005713 return rpc_run_task(&task_setup_data);
5714}
5715
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005716static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005717{
5718 struct rpc_task *task;
5719 int ret = 0;
5720
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005721 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5722 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005723 task = _nfs41_proc_sequence(clp, cred);
5724 if (IS_ERR(task))
5725 ret = PTR_ERR(task);
5726 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005727 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005728 dprintk("<-- %s status=%d\n", __func__, ret);
5729 return ret;
5730}
5731
5732static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5733{
5734 struct rpc_task *task;
5735 int ret;
5736
5737 task = _nfs41_proc_sequence(clp, cred);
5738 if (IS_ERR(task)) {
5739 ret = PTR_ERR(task);
5740 goto out;
5741 }
5742 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005743 if (!ret) {
5744 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5745
5746 if (task->tk_status == 0)
5747 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005748 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005749 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005750 rpc_put_task(task);
5751out:
5752 dprintk("<-- %s status=%d\n", __func__, ret);
5753 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005754}
5755
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005756struct nfs4_reclaim_complete_data {
5757 struct nfs_client *clp;
5758 struct nfs41_reclaim_complete_args arg;
5759 struct nfs41_reclaim_complete_res res;
5760};
5761
5762static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5763{
5764 struct nfs4_reclaim_complete_data *calldata = data;
5765
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005766 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005767 if (nfs41_setup_sequence(calldata->clp->cl_session,
5768 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005769 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005770 return;
5771
5772 rpc_call_start(task);
5773}
5774
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005775static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5776{
5777 switch(task->tk_status) {
5778 case 0:
5779 case -NFS4ERR_COMPLETE_ALREADY:
5780 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5781 break;
5782 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005783 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005784 /* fall through */
5785 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005786 return -EAGAIN;
5787 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005788 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005789 }
5790 return 0;
5791}
5792
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005793static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5794{
5795 struct nfs4_reclaim_complete_data *calldata = data;
5796 struct nfs_client *clp = calldata->clp;
5797 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5798
5799 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005800 if (!nfs41_sequence_done(task, res))
5801 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005802
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005803 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5804 rpc_restart_call_prepare(task);
5805 return;
5806 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005807 dprintk("<-- %s\n", __func__);
5808}
5809
5810static void nfs4_free_reclaim_complete_data(void *data)
5811{
5812 struct nfs4_reclaim_complete_data *calldata = data;
5813
5814 kfree(calldata);
5815}
5816
5817static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5818 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5819 .rpc_call_done = nfs4_reclaim_complete_done,
5820 .rpc_release = nfs4_free_reclaim_complete_data,
5821};
5822
5823/*
5824 * Issue a global reclaim complete.
5825 */
5826static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5827{
5828 struct nfs4_reclaim_complete_data *calldata;
5829 struct rpc_task *task;
5830 struct rpc_message msg = {
5831 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5832 };
5833 struct rpc_task_setup task_setup_data = {
5834 .rpc_client = clp->cl_rpcclient,
5835 .rpc_message = &msg,
5836 .callback_ops = &nfs4_reclaim_complete_call_ops,
5837 .flags = RPC_TASK_ASYNC,
5838 };
5839 int status = -ENOMEM;
5840
5841 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005842 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005843 if (calldata == NULL)
5844 goto out;
5845 calldata->clp = clp;
5846 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005847
Trond Myklebust9d12b212012-01-17 22:04:25 -05005848 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005849 msg.rpc_argp = &calldata->arg;
5850 msg.rpc_resp = &calldata->res;
5851 task_setup_data.callback_data = calldata;
5852 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005853 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005854 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005855 goto out;
5856 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005857 status = nfs4_wait_for_completion_rpc_task(task);
5858 if (status == 0)
5859 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005860 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005861 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005862out:
5863 dprintk("<-- %s status=%d\n", __func__, status);
5864 return status;
5865}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005866
5867static void
5868nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5869{
5870 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005871 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005872
5873 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005874 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5875 * right now covering the LAYOUTGET we are about to send.
5876 * However, that is not so catastrophic, and there seems
5877 * to be no way to prevent it completely.
5878 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005879 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005880 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005881 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005882 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5883 NFS_I(lgp->args.inode)->layout,
5884 lgp->args.ctx->state)) {
5885 rpc_exit(task, NFS4_OK);
5886 return;
5887 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005888 rpc_call_start(task);
5889}
5890
5891static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5892{
5893 struct nfs4_layoutget *lgp = calldata;
5894 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5895
5896 dprintk("--> %s\n", __func__);
5897
5898 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5899 return;
5900
5901 switch (task->tk_status) {
5902 case 0:
5903 break;
5904 case -NFS4ERR_LAYOUTTRYLATER:
5905 case -NFS4ERR_RECALLCONFLICT:
5906 task->tk_status = -NFS4ERR_DELAY;
5907 /* Fall through */
5908 default:
5909 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5910 rpc_restart_call_prepare(task);
5911 return;
5912 }
5913 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005914 dprintk("<-- %s\n", __func__);
5915}
5916
5917static void nfs4_layoutget_release(void *calldata)
5918{
5919 struct nfs4_layoutget *lgp = calldata;
5920
5921 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005922 put_nfs_open_context(lgp->args.ctx);
5923 kfree(calldata);
5924 dprintk("<-- %s\n", __func__);
5925}
5926
5927static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5928 .rpc_call_prepare = nfs4_layoutget_prepare,
5929 .rpc_call_done = nfs4_layoutget_done,
5930 .rpc_release = nfs4_layoutget_release,
5931};
5932
5933int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5934{
5935 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5936 struct rpc_task *task;
5937 struct rpc_message msg = {
5938 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5939 .rpc_argp = &lgp->args,
5940 .rpc_resp = &lgp->res,
5941 };
5942 struct rpc_task_setup task_setup_data = {
5943 .rpc_client = server->client,
5944 .rpc_message = &msg,
5945 .callback_ops = &nfs4_layoutget_call_ops,
5946 .callback_data = lgp,
5947 .flags = RPC_TASK_ASYNC,
5948 };
5949 int status = 0;
5950
5951 dprintk("--> %s\n", __func__);
5952
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005953 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005954 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05005955 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005956 task = rpc_run_task(&task_setup_data);
5957 if (IS_ERR(task))
5958 return PTR_ERR(task);
5959 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005960 if (status == 0)
5961 status = task->tk_status;
5962 if (status == 0)
5963 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005964 rpc_put_task(task);
5965 dprintk("<-- %s status=%d\n", __func__, status);
5966 return status;
5967}
5968
Benny Halevycbe82602011-05-22 19:52:37 +03005969static void
5970nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5971{
5972 struct nfs4_layoutreturn *lrp = calldata;
5973
5974 dprintk("--> %s\n", __func__);
5975 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005976 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03005977 return;
5978 rpc_call_start(task);
5979}
5980
5981static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5982{
5983 struct nfs4_layoutreturn *lrp = calldata;
5984 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005985 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005986
5987 dprintk("--> %s\n", __func__);
5988
5989 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5990 return;
5991
5992 server = NFS_SERVER(lrp->args.inode);
5993 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005994 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03005995 return;
5996 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005997 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005998 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005999 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03006000 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03006001 } else
6002 BUG_ON(!list_empty(&lo->plh_segs));
6003 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006004 lo->plh_block_lgets--;
6005 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006006 dprintk("<-- %s\n", __func__);
6007}
6008
6009static void nfs4_layoutreturn_release(void *calldata)
6010{
6011 struct nfs4_layoutreturn *lrp = calldata;
6012
6013 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006014 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006015 kfree(calldata);
6016 dprintk("<-- %s\n", __func__);
6017}
6018
6019static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6020 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6021 .rpc_call_done = nfs4_layoutreturn_done,
6022 .rpc_release = nfs4_layoutreturn_release,
6023};
6024
6025int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6026{
6027 struct rpc_task *task;
6028 struct rpc_message msg = {
6029 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6030 .rpc_argp = &lrp->args,
6031 .rpc_resp = &lrp->res,
6032 };
6033 struct rpc_task_setup task_setup_data = {
6034 .rpc_client = lrp->clp->cl_rpcclient,
6035 .rpc_message = &msg,
6036 .callback_ops = &nfs4_layoutreturn_call_ops,
6037 .callback_data = lrp,
6038 };
6039 int status;
6040
6041 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006042 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006043 task = rpc_run_task(&task_setup_data);
6044 if (IS_ERR(task))
6045 return PTR_ERR(task);
6046 status = task->tk_status;
6047 dprintk("<-- %s status=%d\n", __func__, status);
6048 rpc_put_task(task);
6049 return status;
6050}
6051
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006052/*
6053 * Retrieve the list of Data Server devices from the MDS.
6054 */
6055static int _nfs4_getdevicelist(struct nfs_server *server,
6056 const struct nfs_fh *fh,
6057 struct pnfs_devicelist *devlist)
6058{
6059 struct nfs4_getdevicelist_args args = {
6060 .fh = fh,
6061 .layoutclass = server->pnfs_curr_ld->id,
6062 };
6063 struct nfs4_getdevicelist_res res = {
6064 .devlist = devlist,
6065 };
6066 struct rpc_message msg = {
6067 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6068 .rpc_argp = &args,
6069 .rpc_resp = &res,
6070 };
6071 int status;
6072
6073 dprintk("--> %s\n", __func__);
6074 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6075 &res.seq_res, 0);
6076 dprintk("<-- %s status=%d\n", __func__, status);
6077 return status;
6078}
6079
6080int nfs4_proc_getdevicelist(struct nfs_server *server,
6081 const struct nfs_fh *fh,
6082 struct pnfs_devicelist *devlist)
6083{
6084 struct nfs4_exception exception = { };
6085 int err;
6086
6087 do {
6088 err = nfs4_handle_exception(server,
6089 _nfs4_getdevicelist(server, fh, devlist),
6090 &exception);
6091 } while (exception.retry);
6092
6093 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6094 err, devlist->num_devs);
6095
6096 return err;
6097}
6098EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6099
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006100static int
6101_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6102{
6103 struct nfs4_getdeviceinfo_args args = {
6104 .pdev = pdev,
6105 };
6106 struct nfs4_getdeviceinfo_res res = {
6107 .pdev = pdev,
6108 };
6109 struct rpc_message msg = {
6110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6111 .rpc_argp = &args,
6112 .rpc_resp = &res,
6113 };
6114 int status;
6115
6116 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006117 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006118 dprintk("<-- %s status=%d\n", __func__, status);
6119
6120 return status;
6121}
6122
6123int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6124{
6125 struct nfs4_exception exception = { };
6126 int err;
6127
6128 do {
6129 err = nfs4_handle_exception(server,
6130 _nfs4_proc_getdeviceinfo(server, pdev),
6131 &exception);
6132 } while (exception.retry);
6133 return err;
6134}
6135EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6136
Andy Adamson863a3c62011-03-23 13:27:54 +00006137static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6138{
6139 struct nfs4_layoutcommit_data *data = calldata;
6140 struct nfs_server *server = NFS_SERVER(data->args.inode);
6141
6142 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006143 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006144 return;
6145 rpc_call_start(task);
6146}
6147
6148static void
6149nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6150{
6151 struct nfs4_layoutcommit_data *data = calldata;
6152 struct nfs_server *server = NFS_SERVER(data->args.inode);
6153
6154 if (!nfs4_sequence_done(task, &data->res.seq_res))
6155 return;
6156
6157 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006158 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6159 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6160 case -NFS4ERR_BADLAYOUT: /* no layout */
6161 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006162 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006163 break;
6164 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006165 nfs_post_op_update_inode_force_wcc(data->args.inode,
6166 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006167 break;
6168 default:
6169 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6170 rpc_restart_call_prepare(task);
6171 return;
6172 }
6173 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006174}
6175
6176static void nfs4_layoutcommit_release(void *calldata)
6177{
6178 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006179 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006180 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006181
Andy Adamsondb29c082011-07-30 20:52:38 -04006182 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006183 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006184 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6185 list_del_init(&lseg->pls_lc_list);
6186 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6187 &lseg->pls_flags))
6188 put_lseg(lseg);
6189 }
Peng Tao92407e72011-10-23 20:21:17 -07006190
6191 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6192 smp_mb__after_clear_bit();
6193 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6194
Andy Adamson863a3c62011-03-23 13:27:54 +00006195 put_rpccred(data->cred);
6196 kfree(data);
6197}
6198
6199static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6200 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6201 .rpc_call_done = nfs4_layoutcommit_done,
6202 .rpc_release = nfs4_layoutcommit_release,
6203};
6204
6205int
Andy Adamsonef311532011-03-12 02:58:10 -05006206nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006207{
6208 struct rpc_message msg = {
6209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6210 .rpc_argp = &data->args,
6211 .rpc_resp = &data->res,
6212 .rpc_cred = data->cred,
6213 };
6214 struct rpc_task_setup task_setup_data = {
6215 .task = &data->task,
6216 .rpc_client = NFS_CLIENT(data->args.inode),
6217 .rpc_message = &msg,
6218 .callback_ops = &nfs4_layoutcommit_ops,
6219 .callback_data = data,
6220 .flags = RPC_TASK_ASYNC,
6221 };
6222 struct rpc_task *task;
6223 int status = 0;
6224
6225 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6226 "lbw: %llu inode %lu\n",
6227 data->task.tk_pid, sync,
6228 data->args.lastbytewritten,
6229 data->args.inode->i_ino);
6230
Trond Myklebust9d12b212012-01-17 22:04:25 -05006231 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006232 task = rpc_run_task(&task_setup_data);
6233 if (IS_ERR(task))
6234 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006235 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006236 goto out;
6237 status = nfs4_wait_for_completion_rpc_task(task);
6238 if (status != 0)
6239 goto out;
6240 status = task->tk_status;
6241out:
6242 dprintk("%s: status %d\n", __func__, status);
6243 rpc_put_task(task);
6244 return status;
6245}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006246
6247static int
6248_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6249 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6250{
6251 struct nfs41_secinfo_no_name_args args = {
6252 .style = SECINFO_STYLE_CURRENT_FH,
6253 };
6254 struct nfs4_secinfo_res res = {
6255 .flavors = flavors,
6256 };
6257 struct rpc_message msg = {
6258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6259 .rpc_argp = &args,
6260 .rpc_resp = &res,
6261 };
6262 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6263}
6264
6265static int
6266nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6267 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6268{
6269 struct nfs4_exception exception = { };
6270 int err;
6271 do {
6272 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6273 switch (err) {
6274 case 0:
6275 case -NFS4ERR_WRONGSEC:
6276 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006277 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006278 default:
6279 err = nfs4_handle_exception(server, err, &exception);
6280 }
6281 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006282out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006283 return err;
6284}
6285
6286static int
6287nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6288 struct nfs_fsinfo *info)
6289{
6290 int err;
6291 struct page *page;
6292 rpc_authflavor_t flavor;
6293 struct nfs4_secinfo_flavors *flavors;
6294
6295 page = alloc_page(GFP_KERNEL);
6296 if (!page) {
6297 err = -ENOMEM;
6298 goto out;
6299 }
6300
6301 flavors = page_address(page);
6302 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6303
6304 /*
6305 * Fall back on "guess and check" method if
6306 * the server doesn't support SECINFO_NO_NAME
6307 */
6308 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6309 err = nfs4_find_root_sec(server, fhandle, info);
6310 goto out_freepage;
6311 }
6312 if (err)
6313 goto out_freepage;
6314
6315 flavor = nfs_find_best_sec(flavors);
6316 if (err == 0)
6317 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6318
6319out_freepage:
6320 put_page(page);
6321 if (err == -EACCES)
6322 return -EPERM;
6323out:
6324 return err;
6325}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006326
6327static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006328{
6329 int status;
6330 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006331 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006332 };
6333 struct nfs41_test_stateid_res res;
6334 struct rpc_message msg = {
6335 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6336 .rpc_argp = &args,
6337 .rpc_resp = &res,
6338 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006339
Trond Myklebust9d12b212012-01-17 22:04:25 -05006340 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006341 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6342
6343 if (status == NFS_OK)
6344 return res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006345 return status;
6346}
6347
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006348static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006349{
6350 struct nfs4_exception exception = { };
6351 int err;
6352 do {
6353 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006354 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006355 &exception);
6356 } while (exception.retry);
6357 return err;
6358}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006359
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006360static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006361{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006362 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006363 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006364 };
6365 struct nfs41_free_stateid_res res;
6366 struct rpc_message msg = {
6367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6368 .rpc_argp = &args,
6369 .rpc_resp = &res,
6370 };
6371
Trond Myklebust9d12b212012-01-17 22:04:25 -05006372 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6373 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006374}
6375
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006376static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006377{
6378 struct nfs4_exception exception = { };
6379 int err;
6380 do {
6381 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006382 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006383 &exception);
6384 } while (exception.retry);
6385 return err;
6386}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006387
6388static bool nfs41_match_stateid(const nfs4_stateid *s1,
6389 const nfs4_stateid *s2)
6390{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006391 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006392 return false;
6393
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006394 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006395 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006396 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006397 return true;
6398
6399 return false;
6400}
6401
Andy Adamson557134a2009-04-01 09:21:53 -04006402#endif /* CONFIG_NFS_V4_1 */
6403
Trond Myklebust36281ca2012-03-04 18:13:56 -05006404static bool nfs4_match_stateid(const nfs4_stateid *s1,
6405 const nfs4_stateid *s2)
6406{
Trond Myklebustf597c532012-03-04 18:13:56 -05006407 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006408}
6409
6410
Trond Myklebust17280172012-03-11 13:11:00 -04006411static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006412 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006413 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 .recover_open = nfs4_open_reclaim,
6415 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006416 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006417 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418};
6419
Andy Adamson591d71c2009-04-01 09:22:47 -04006420#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006421static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006422 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6423 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6424 .recover_open = nfs4_open_reclaim,
6425 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006426 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006427 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006428 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006429};
6430#endif /* CONFIG_NFS_V4_1 */
6431
Trond Myklebust17280172012-03-11 13:11:00 -04006432static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006433 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006434 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 .recover_open = nfs4_open_expired,
6436 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006437 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006438 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439};
6440
Andy Adamson591d71c2009-04-01 09:22:47 -04006441#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006442static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006443 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6444 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006445 .recover_open = nfs41_open_expired,
6446 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006447 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006448 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006449};
6450#endif /* CONFIG_NFS_V4_1 */
6451
Trond Myklebust17280172012-03-11 13:11:00 -04006452static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006453 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006454 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006455 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006456};
6457
6458#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006459static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006460 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006461 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006462 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006463};
6464#endif
6465
Trond Myklebust97dc1352010-06-16 09:52:26 -04006466static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6467 .minor_version = 0,
6468 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006469 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006470 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006471 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6472 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6473 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006474};
6475
6476#if defined(CONFIG_NFS_V4_1)
6477static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6478 .minor_version = 1,
6479 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006480 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006481 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006482 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6483 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6484 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006485};
6486#endif
6487
Trond Myklebust97dc1352010-06-16 09:52:26 -04006488const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6489 [0] = &nfs_v4_0_minor_ops,
6490#if defined(CONFIG_NFS_V4_1)
6491 [1] = &nfs_v4_1_minor_ops,
6492#endif
6493};
6494
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006495static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006496 .permission = nfs_permission,
6497 .getattr = nfs_getattr,
6498 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006499 .getxattr = generic_getxattr,
6500 .setxattr = generic_setxattr,
6501 .listxattr = generic_listxattr,
6502 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006503};
6504
David Howells509de812006-08-22 20:06:11 -04006505const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506 .version = 4, /* protocol version */
6507 .dentry_ops = &nfs4_dentry_operations,
6508 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006509 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006510 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511 .getroot = nfs4_proc_get_root,
6512 .getattr = nfs4_proc_getattr,
6513 .setattr = nfs4_proc_setattr,
6514 .lookup = nfs4_proc_lookup,
6515 .access = nfs4_proc_access,
6516 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517 .create = nfs4_proc_create,
6518 .remove = nfs4_proc_remove,
6519 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006520 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521 .unlink_done = nfs4_proc_unlink_done,
6522 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006523 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006524 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006525 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526 .link = nfs4_proc_link,
6527 .symlink = nfs4_proc_symlink,
6528 .mkdir = nfs4_proc_mkdir,
6529 .rmdir = nfs4_proc_remove,
6530 .readdir = nfs4_proc_readdir,
6531 .mknod = nfs4_proc_mknod,
6532 .statfs = nfs4_proc_statfs,
6533 .fsinfo = nfs4_proc_fsinfo,
6534 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006535 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536 .decode_dirent = nfs4_decode_dirent,
6537 .read_setup = nfs4_proc_read_setup,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006538 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006539 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540 .write_setup = nfs4_proc_write_setup,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006541 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006542 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006544 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006546 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006547 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006548 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006549 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006550 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551};
6552
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006553static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6554 .prefix = XATTR_NAME_NFSV4_ACL,
6555 .list = nfs4_xattr_list_nfs4_acl,
6556 .get = nfs4_xattr_get_nfs4_acl,
6557 .set = nfs4_xattr_set_nfs4_acl,
6558};
6559
6560const struct xattr_handler *nfs4_xattr_handlers[] = {
6561 &nfs4_xattr_nfs4_acl_handler,
6562 NULL
6563};
6564
Trond Myklebustef159e92012-02-06 19:50:40 -05006565module_param(max_session_slots, ushort, 0644);
6566MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6567 "requests the client will negotiate");
6568
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569/*
6570 * Local variables:
6571 * c-basic-offset: 8
6572 * End:
6573 */