blob: ba837d977a1a0aae979409f8db688bee76992b1a [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 Myklebust48c22eb2012-01-17 22:04:25 -0500841 p->o_arg.id = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400842 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100843 p->o_arg.server = server;
844 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500845 p->o_arg.dir_bitmask = server->cache_consistency_bitmask;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100846 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500847 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500848 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400849
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100850 p->o_arg.u.attrs = &p->attrs;
851 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500852
853 verf[0] = jiffies;
854 verf[1] = current->pid;
855 memcpy(p->o_arg.u.verifier.data, verf,
856 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100857 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100858 p->c_arg.fh = &p->o_res.fh;
859 p->c_arg.stateid = &p->o_res.stateid;
860 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400861 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400862 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100863 return p;
864err_free:
865 kfree(p);
866err:
867 dput(parent);
868 return NULL;
869}
870
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400871static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100872{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400873 struct nfs4_opendata *p = container_of(kref,
874 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400875 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400876
877 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400878 if (p->state != NULL)
879 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400880 nfs4_put_state_owner(p->owner);
881 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400882 dput(p->dentry);
883 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500884 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400885 kfree(p);
886}
887
888static void nfs4_opendata_put(struct nfs4_opendata *p)
889{
890 if (p != NULL)
891 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100892}
893
Trond Myklebust06f814a2006-01-03 09:55:07 +0100894static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
895{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100896 int ret;
897
Trond Myklebust06f814a2006-01-03 09:55:07 +0100898 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100899 return ret;
900}
901
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500902static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400903{
904 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500905
Trond Myklebust536e43d2012-01-17 22:04:26 -0500906 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500907 goto out;
908 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400909 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500910 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
911 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400912 break;
913 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500914 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
915 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400916 break;
917 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500918 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
919 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400920 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500921out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400922 return ret;
923}
924
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400926{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500927 if (delegation == NULL)
928 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500929 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400930 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500931 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400932 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500933 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400934 return 1;
935}
936
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100938{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500939 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100940 case FMODE_WRITE:
941 state->n_wronly++;
942 break;
943 case FMODE_READ:
944 state->n_rdonly++;
945 break;
946 case FMODE_READ|FMODE_WRITE:
947 state->n_rdwr++;
948 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500949 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100950}
951
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500952static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400953{
954 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500955 nfs4_stateid_copy(&state->stateid, stateid);
956 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500957 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400958 case FMODE_READ:
959 set_bit(NFS_O_RDONLY_STATE, &state->flags);
960 break;
961 case FMODE_WRITE:
962 set_bit(NFS_O_WRONLY_STATE, &state->flags);
963 break;
964 case FMODE_READ|FMODE_WRITE:
965 set_bit(NFS_O_RDWR_STATE, &state->flags);
966 }
967}
968
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500969static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400970{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400971 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500972 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400973 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400974}
975
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500976static 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 -0700977{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400978 /*
979 * Protect the call to nfs4_state_set_mode_locked and
980 * serialise the stateid update
981 */
982 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400983 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500984 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400985 set_bit(NFS_DELEGATED_STATE, &state->flags);
986 }
987 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500988 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400989 write_sequnlock(&state->seqlock);
990 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500991 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700992 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500995static 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 -0500996{
997 struct nfs_inode *nfsi = NFS_I(state->inode);
998 struct nfs_delegation *deleg_cur;
999 int ret = 0;
1000
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001001 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001002
1003 rcu_read_lock();
1004 deleg_cur = rcu_dereference(nfsi->delegation);
1005 if (deleg_cur == NULL)
1006 goto no_delegation;
1007
1008 spin_lock(&deleg_cur->lock);
1009 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001010 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001011 goto no_delegation_unlock;
1012
1013 if (delegation == NULL)
1014 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001015 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001016 goto no_delegation_unlock;
1017
Trond Myklebustb7391f42008-12-23 15:21:52 -05001018 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001019 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001020 ret = 1;
1021no_delegation_unlock:
1022 spin_unlock(&deleg_cur->lock);
1023no_delegation:
1024 rcu_read_unlock();
1025
1026 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001027 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001028 ret = 1;
1029 }
1030
1031 return ret;
1032}
1033
1034
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001035static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001036{
1037 struct nfs_delegation *delegation;
1038
1039 rcu_read_lock();
1040 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001041 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001042 rcu_read_unlock();
1043 return;
1044 }
1045 rcu_read_unlock();
1046 nfs_inode_return_delegation(inode);
1047}
1048
Trond Myklebust6ee41262007-07-08 14:11:36 -04001049static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001050{
1051 struct nfs4_state *state = opendata->state;
1052 struct nfs_inode *nfsi = NFS_I(state->inode);
1053 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001054 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001055 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001056 nfs4_stateid stateid;
1057 int ret = -EAGAIN;
1058
Trond Myklebustaac00a82007-07-05 19:02:21 -04001059 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001060 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001061 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001062 if (can_open_cached(state, fmode, open_mode)) {
1063 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001064 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001065 goto out_return_state;
1066 }
1067 spin_unlock(&state->owner->so_lock);
1068 }
Trond Myklebust34310432008-12-23 15:21:38 -05001069 rcu_read_lock();
1070 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001071 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001072 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001073 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001074 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001075 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001076 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001077 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001078 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001079 if (ret != 0)
1080 goto out;
1081 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001082
1083 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001084 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001085 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001086 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001087out:
1088 return ERR_PTR(ret);
1089out_return_state:
1090 atomic_inc(&state->count);
1091 return state;
1092}
1093
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001094static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1095{
1096 struct inode *inode;
1097 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001098 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001099 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001100
Trond Myklebustaac00a82007-07-05 19:02:21 -04001101 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001102 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001103 goto out;
1104 }
1105
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001106 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001107 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001108 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001109 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001110 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001111 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001112 goto err;
1113 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001114 state = nfs4_get_open_state(inode, data->owner);
1115 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001116 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001117 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001118 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001119 int delegation_flags = 0;
1120
Trond Myklebust003707c2007-07-05 18:07:55 -04001121 rcu_read_lock();
1122 delegation = rcu_dereference(NFS_I(inode)->delegation);
1123 if (delegation)
1124 delegation_flags = delegation->flags;
1125 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001126 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1127 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1128 "returning a delegation for "
1129 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001130 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001131 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001132 nfs_inode_set_delegation(state->inode,
1133 data->owner->so_cred,
1134 &data->o_res);
1135 else
1136 nfs_inode_reclaim_delegation(state->inode,
1137 data->owner->so_cred,
1138 &data->o_res);
1139 }
Trond Myklebust34310432008-12-23 15:21:38 -05001140
1141 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001142 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001143 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001144out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001145 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001146err_put_inode:
1147 iput(inode);
1148err:
1149 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001150}
1151
Trond Myklebust864472e2006-01-03 09:55:15 +01001152static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1153{
1154 struct nfs_inode *nfsi = NFS_I(state->inode);
1155 struct nfs_open_context *ctx;
1156
1157 spin_lock(&state->inode->i_lock);
1158 list_for_each_entry(ctx, &nfsi->open_files, list) {
1159 if (ctx->state != state)
1160 continue;
1161 get_nfs_open_context(ctx);
1162 spin_unlock(&state->inode->i_lock);
1163 return ctx;
1164 }
1165 spin_unlock(&state->inode->i_lock);
1166 return ERR_PTR(-ENOENT);
1167}
1168
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001169static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1170{
1171 struct nfs4_opendata *opendata;
1172
Al Viro3d4ff432011-06-22 18:40:12 -04001173 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001174 if (opendata == NULL)
1175 return ERR_PTR(-ENOMEM);
1176 opendata->state = state;
1177 atomic_inc(&state->count);
1178 return opendata;
1179}
1180
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001181static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001182{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001183 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001184 int ret;
1185
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001186 opendata->o_arg.open_flags = 0;
1187 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001188 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1189 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1190 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001191 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001192 if (ret != 0)
1193 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001194 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001195 if (IS_ERR(newstate))
1196 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001197 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001198 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001199 return 0;
1200}
1201
1202static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1203{
Trond Myklebust864472e2006-01-03 09:55:15 +01001204 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001205 int ret;
1206
1207 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001208 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001209 smp_rmb();
1210 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001211 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001212 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001213 if (ret != 0)
1214 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001215 if (newstate != state)
1216 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001217 }
1218 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001219 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001220 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001221 if (ret != 0)
1222 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001223 if (newstate != state)
1224 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001225 }
1226 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001227 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001228 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001229 if (ret != 0)
1230 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001231 if (newstate != state)
1232 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001233 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001234 /*
1235 * We may have performed cached opens for all three recoveries.
1236 * Check if we need to update the current stateid.
1237 */
1238 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001239 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001240 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001241 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001242 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001243 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001244 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001245 return 0;
1246}
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248/*
1249 * OPEN_RECLAIM:
1250 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001252static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001254 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001255 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001256 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 int status;
1258
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001259 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1260 if (IS_ERR(opendata))
1261 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001262 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1263 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001264 rcu_read_lock();
1265 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001266 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001267 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001268 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001269 opendata->o_arg.u.delegation_type = delegation_type;
1270 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001271 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return status;
1273}
1274
Trond Myklebust539cd032007-06-05 11:46:42 -04001275static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 struct nfs_server *server = NFS_SERVER(state->inode);
1278 struct nfs4_exception exception = { };
1279 int err;
1280 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001281 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001282 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001283 break;
1284 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 } while (exception.retry);
1286 return err;
1287}
1288
Trond Myklebust864472e2006-01-03 09:55:15 +01001289static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1290{
1291 struct nfs_open_context *ctx;
1292 int ret;
1293
1294 ctx = nfs4_state_find_open_context(state);
1295 if (IS_ERR(ctx))
1296 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001297 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001298 put_nfs_open_context(ctx);
1299 return ret;
1300}
1301
Trond Myklebust13437e12007-07-06 15:10:43 -04001302static 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 -07001303{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001304 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001305 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001307 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1308 if (IS_ERR(opendata))
1309 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001310 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001311 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001312 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001313 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001314 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
Trond Myklebust13437e12007-07-06 15:10:43 -04001317int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
1319 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001320 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 int err;
1322 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001323 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 switch (err) {
1325 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001326 case -ENOENT:
1327 case -ESTALE:
1328 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001329 case -NFS4ERR_BADSESSION:
1330 case -NFS4ERR_BADSLOT:
1331 case -NFS4ERR_BAD_HIGH_SLOT:
1332 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1333 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001334 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001335 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 case -NFS4ERR_STALE_CLIENTID:
1337 case -NFS4ERR_STALE_STATEID:
1338 case -NFS4ERR_EXPIRED:
1339 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001340 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001341 goto out;
1342 case -ERESTARTSYS:
1343 /*
1344 * The show must go on: exit, but mark the
1345 * stateid as needing recovery.
1346 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001347 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001348 case -NFS4ERR_ADMIN_REVOKED:
1349 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001350 nfs_inode_find_state_and_recover(state->inode,
1351 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001352 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001353 case -EKEYEXPIRED:
1354 /*
1355 * User RPCSEC_GSS context has expired.
1356 * We cannot recover this stateid now, so
1357 * skip it and allow recovery thread to
1358 * proceed.
1359 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001360 case -ENOMEM:
1361 err = 0;
1362 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
1364 err = nfs4_handle_exception(server, err, &exception);
1365 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001366out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return err;
1368}
1369
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001370static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1371{
1372 struct nfs4_opendata *data = calldata;
1373
1374 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001375 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001376 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001377 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001378 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001379 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001380 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001381}
1382
1383static void nfs4_open_confirm_release(void *calldata)
1384{
1385 struct nfs4_opendata *data = calldata;
1386 struct nfs4_state *state = NULL;
1387
1388 /* If this request hasn't been cancelled, do nothing */
1389 if (data->cancelled == 0)
1390 goto out_free;
1391 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001392 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001393 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001394 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001395 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001396 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001397out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001398 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001399}
1400
1401static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001402 .rpc_call_done = nfs4_open_confirm_done,
1403 .rpc_release = nfs4_open_confirm_release,
1404};
1405
1406/*
1407 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1408 */
1409static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1410{
1411 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1412 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001413 struct rpc_message msg = {
1414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1415 .rpc_argp = &data->c_arg,
1416 .rpc_resp = &data->c_res,
1417 .rpc_cred = data->owner->so_cred,
1418 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001419 struct rpc_task_setup task_setup_data = {
1420 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001421 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001422 .callback_ops = &nfs4_open_confirm_ops,
1423 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001424 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001425 .flags = RPC_TASK_ASYNC,
1426 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 int status;
1428
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001429 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001430 data->rpc_done = 0;
1431 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001432 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001433 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001434 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001435 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001436 status = nfs4_wait_for_completion_rpc_task(task);
1437 if (status != 0) {
1438 data->cancelled = 1;
1439 smp_wmb();
1440 } else
1441 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001442 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return status;
1444}
1445
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001446static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001448 struct nfs4_opendata *data = calldata;
1449 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001450
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001451 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1452 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001453 /*
1454 * Check if we still need to send an OPEN call, or if we can use
1455 * a delegation instead.
1456 */
1457 if (data->state != NULL) {
1458 struct nfs_delegation *delegation;
1459
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001460 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001461 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001462 rcu_read_lock();
1463 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001464 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1465 can_open_delegated(delegation, data->o_arg.fmode))
1466 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001467 rcu_read_unlock();
1468 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001469 /* Update sequence id. */
Trond Myklebust48c22eb2012-01-17 22:04:25 -05001470 data->o_arg.id = sp->so_seqid.owner_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 {
1957 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001958 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 &exception);
1960 } while (exception.retry);
1961 return err;
1962}
1963
1964struct nfs4_closedata {
1965 struct inode *inode;
1966 struct nfs4_state *state;
1967 struct nfs_closeargs arg;
1968 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001969 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001970 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001971 bool roc;
1972 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973};
1974
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001975static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001976{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001977 struct nfs4_closedata *calldata = data;
1978 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001979 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001980
Fred Isamanf7e89172011-01-06 11:36:32 +00001981 if (calldata->roc)
1982 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001983 nfs4_put_open_state(calldata->state);
1984 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001985 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001986 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001987 kfree(calldata);
1988}
1989
Trond Myklebust88069f72009-12-08 08:33:16 -05001990static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1991 fmode_t fmode)
1992{
1993 spin_lock(&state->owner->so_lock);
1994 if (!(fmode & FMODE_READ))
1995 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1996 if (!(fmode & FMODE_WRITE))
1997 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1998 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1999 spin_unlock(&state->owner->so_lock);
2000}
2001
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002002static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002004 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 struct nfs_server *server = NFS_SERVER(calldata->inode);
2007
Chuck Levera3ca5652012-03-01 17:00:40 -05002008 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002009 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2010 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 /* hmm. we are done with the inode, and in the process of freeing
2012 * the state_owner. we keep this around to process errors
2013 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 switch (task->tk_status) {
2015 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002016 if (calldata->roc)
2017 pnfs_roc_set_barrier(state->inode,
2018 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002019 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002020 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002021 nfs4_close_clear_stateid_flags(state,
2022 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 break;
2024 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002025 case -NFS4ERR_OLD_STATEID:
2026 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002028 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002029 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002031 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2032 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002034 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002035 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002036 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
2038
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002039static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002041 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002042 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002043 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002044
Chuck Levera3ca5652012-03-01 17:00:40 -05002045 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002046 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002047 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002048
Trond Myklebust88069f72009-12-08 08:33:16 -05002049 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2050 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002051 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002052 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002053 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002054 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002055 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2056 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2057 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002058 }
2059 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002060 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2061 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2062 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002063 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002064 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002065 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002066
2067 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002068 /* Note: exit _without_ calling nfs4_close_done */
2069 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002070 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002071 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002072
Fred Isamanf7e89172011-01-06 11:36:32 +00002073 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002074 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002075 if (calldata->roc &&
2076 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2077 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2078 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002079 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002080 }
2081 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002082
Trond Myklebust516a6af2005-10-27 22:12:41 -04002083 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002084 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002085 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002086 &calldata->arg.seq_args,
2087 &calldata->res.seq_res,
2088 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002089 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002090 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002091out:
2092 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093}
2094
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002095static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002096 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002097 .rpc_call_done = nfs4_close_done,
2098 .rpc_release = nfs4_free_closedata,
2099};
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101/*
2102 * It is possible for data to be read/written from a mem-mapped file
2103 * after the sys_close call (which hits the vfs layer as a flush).
2104 * This means that we can't safely call nfsv4 close on a file until
2105 * the inode is cleared. This in turn means that we are not good
2106 * NFSv4 citizens - we do not indicate to the server to update the file's
2107 * share state even when we are done with one of the three share
2108 * stateid's in the inode.
2109 *
2110 * NOTE: Caller must be holding the sp->so_owner semaphore!
2111 */
Al Viro643168c2011-06-22 18:20:23 -04002112int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002114 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002116 struct nfs4_state_owner *sp = state->owner;
2117 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002118 struct rpc_message msg = {
2119 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2120 .rpc_cred = state->owner->so_cred,
2121 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002122 struct rpc_task_setup task_setup_data = {
2123 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002124 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002125 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002126 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002127 .flags = RPC_TASK_ASYNC,
2128 };
Trond Myklebust95121352005-10-18 14:20:12 -07002129 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002131 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002133 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002134 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002135 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002137 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002138 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002140 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002141 if (calldata->arg.seqid == NULL)
2142 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002143 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002144 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002145 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002146 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002147 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002148 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002149 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002150
Trond Myklebust1174dd12010-12-21 10:52:24 -05002151 msg.rpc_argp = &calldata->arg;
2152 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002153 task_setup_data.callback_data = calldata;
2154 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002155 if (IS_ERR(task))
2156 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002157 status = 0;
2158 if (wait)
2159 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002160 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002161 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002162out_free_calldata:
2163 kfree(calldata);
2164out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002165 if (roc)
2166 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002167 nfs4_put_open_state(state);
2168 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002169 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
Trond Myklebust2b484292010-09-17 10:56:51 -04002172static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002173nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 struct nfs4_state *state;
2176
Trond Myklebust565277f2007-10-15 18:17:53 -04002177 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002178 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002179 if (IS_ERR(state))
2180 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002181 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002182 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
Trond Myklebust1185a552009-12-03 15:54:02 -05002185static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002186{
2187 if (ctx->state == NULL)
2188 return;
2189 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002190 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002191 else
Al Viro643168c2011-06-22 18:20:23 -04002192 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002193}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2196{
Benny Halevy43652ad2009-04-01 09:21:54 -04002197 struct nfs4_server_caps_arg args = {
2198 .fhandle = fhandle,
2199 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 struct nfs4_server_caps_res res = {};
2201 struct rpc_message msg = {
2202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002203 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 .rpc_resp = &res,
2205 };
2206 int status;
2207
Bryan Schumaker7c513052011-03-24 17:12:24 +00002208 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (status == 0) {
2210 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002211 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2212 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2213 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2214 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2215 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2217 server->caps |= NFS_CAP_ACLS;
2218 if (res.has_links != 0)
2219 server->caps |= NFS_CAP_HARDLINKS;
2220 if (res.has_symlinks != 0)
2221 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002222 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2223 server->caps |= NFS_CAP_FILEID;
2224 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2225 server->caps |= NFS_CAP_MODE;
2226 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2227 server->caps |= NFS_CAP_NLINK;
2228 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2229 server->caps |= NFS_CAP_OWNER;
2230 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2231 server->caps |= NFS_CAP_OWNER_GROUP;
2232 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2233 server->caps |= NFS_CAP_ATIME;
2234 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2235 server->caps |= NFS_CAP_CTIME;
2236 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2237 server->caps |= NFS_CAP_MTIME;
2238
Trond Myklebusta65318b2009-03-11 14:10:28 -04002239 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2240 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2241 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002243 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return status;
2247}
2248
Trond Myklebust55a97592006-06-09 09:34:19 -04002249int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
2251 struct nfs4_exception exception = { };
2252 int err;
2253 do {
2254 err = nfs4_handle_exception(server,
2255 _nfs4_server_capabilities(server, fhandle),
2256 &exception);
2257 } while (exception.retry);
2258 return err;
2259}
2260
2261static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2262 struct nfs_fsinfo *info)
2263{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 struct nfs4_lookup_root_arg args = {
2265 .bitmask = nfs4_fattr_bitmap,
2266 };
2267 struct nfs4_lookup_res res = {
2268 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002269 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 .fh = fhandle,
2271 };
2272 struct rpc_message msg = {
2273 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2274 .rpc_argp = &args,
2275 .rpc_resp = &res,
2276 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002277
Trond Myklebust0e574af2005-10-27 22:12:38 -04002278 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002279 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
2281
2282static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2283 struct nfs_fsinfo *info)
2284{
2285 struct nfs4_exception exception = { };
2286 int err;
2287 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002288 err = _nfs4_lookup_root(server, fhandle, info);
2289 switch (err) {
2290 case 0:
2291 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002292 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002293 default:
2294 err = nfs4_handle_exception(server, err, &exception);
2295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002297out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return err;
2299}
2300
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002301static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2302 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2303{
2304 struct rpc_auth *auth;
2305 int ret;
2306
2307 auth = rpcauth_create(flavor, server->client);
2308 if (!auth) {
2309 ret = -EIO;
2310 goto out;
2311 }
2312 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002313out:
2314 return ret;
2315}
2316
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002317static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002318 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002320 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002321 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002323 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2324 flav_array[len] = RPC_AUTH_NULL;
2325 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002326
2327 for (i = 0; i < len; i++) {
2328 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002329 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002330 continue;
2331 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002332 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002333 /*
2334 * -EACCESS could mean that the user doesn't have correct permissions
2335 * to access the mount. It could also mean that we tried to mount
2336 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2337 * existing mount programs don't handle -EACCES very well so it should
2338 * be mapped to -EPERM instead.
2339 */
2340 if (status == -EACCES)
2341 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002342 return status;
2343}
2344
2345/*
2346 * get the file handle for the "/" directory on the server
2347 */
2348static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2349 struct nfs_fsinfo *info)
2350{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002351 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002352 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002353 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2354 /*
2355 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2356 * by nfs4_map_errors() as this function exits.
2357 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002358 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if (status == 0)
2360 status = nfs4_server_capabilities(server, fhandle);
2361 if (status == 0)
2362 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002363 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
Manoj Naik6b97fd32006-06-09 09:34:29 -04002366/*
2367 * Get locations and (maybe) other attributes of a referral.
2368 * Note that we'll actually follow the referral later when
2369 * we detect fsid mismatch in inode revalidation
2370 */
Andy Adamson533eb462011-06-13 18:25:56 -04002371static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2372 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002373{
2374 int status = -ENOMEM;
2375 struct page *page = NULL;
2376 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002377
2378 page = alloc_page(GFP_KERNEL);
2379 if (page == NULL)
2380 goto out;
2381 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2382 if (locations == NULL)
2383 goto out;
2384
Trond Myklebustc228fd32007-01-13 02:28:11 -05002385 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002386 if (status != 0)
2387 goto out;
2388 /* Make sure server returned a different fsid for the referral */
2389 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002390 dprintk("%s: server did not return a different fsid for"
2391 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002392 status = -EIO;
2393 goto out;
2394 }
Andy Adamson533eb462011-06-13 18:25:56 -04002395 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2396 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002397
Andy Adamson533eb462011-06-13 18:25:56 -04002398 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002399 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002400 memset(fhandle, 0, sizeof(struct nfs_fh));
2401out:
2402 if (page)
2403 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002404 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002405 return status;
2406}
2407
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2409{
2410 struct nfs4_getattr_arg args = {
2411 .fh = fhandle,
2412 .bitmask = server->attr_bitmask,
2413 };
2414 struct nfs4_getattr_res res = {
2415 .fattr = fattr,
2416 .server = server,
2417 };
2418 struct rpc_message msg = {
2419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2420 .rpc_argp = &args,
2421 .rpc_resp = &res,
2422 };
2423
Trond Myklebust0e574af2005-10-27 22:12:38 -04002424 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002425 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426}
2427
2428static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2429{
2430 struct nfs4_exception exception = { };
2431 int err;
2432 do {
2433 err = nfs4_handle_exception(server,
2434 _nfs4_proc_getattr(server, fhandle, fattr),
2435 &exception);
2436 } while (exception.retry);
2437 return err;
2438}
2439
2440/*
2441 * The file is not closed if it is opened due to the a request to change
2442 * the size of the file. The open call will not be needed once the
2443 * VFS layer lookup-intents are implemented.
2444 *
2445 * Close is called when the inode is destroyed.
2446 * If we haven't opened the file for O_WRONLY, we
2447 * need to in the size_change case to obtain a stateid.
2448 *
2449 * Got race?
2450 * Because OPEN is always done by name in nfsv4, it is
2451 * possible that we opened a different file by the same
2452 * name. We can recognize this race condition, but we
2453 * can't do anything about it besides returning an error.
2454 *
2455 * This will be fixed with VFS changes (lookup-intent).
2456 */
2457static int
2458nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2459 struct iattr *sattr)
2460{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002461 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002462 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002463 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 int status;
2465
Benny Halevy8a1636c2010-07-14 15:43:57 -04002466 if (pnfs_ld_layoutret_on_setattr(inode))
2467 pnfs_return_layout(inode);
2468
Trond Myklebust0e574af2005-10-27 22:12:38 -04002469 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Trond Myklebustd5308382005-11-04 15:33:38 -05002471 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002472 if (sattr->ia_valid & ATTR_FILE) {
2473 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002474
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002475 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002476 if (ctx) {
2477 cred = ctx->cred;
2478 state = ctx->state;
2479 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002480 }
2481
Trond Myklebust536e43d2012-01-17 22:04:26 -05002482 /* Deal with open(O_TRUNC) */
2483 if (sattr->ia_valid & ATTR_OPEN)
2484 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2485
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002486 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002487 if (status == 0)
2488 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 return status;
2490}
2491
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002492static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2493 const struct qstr *name, struct nfs_fh *fhandle,
2494 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002495{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002496 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002497 int status;
2498 struct nfs4_lookup_arg args = {
2499 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002500 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002501 .name = name,
2502 };
2503 struct nfs4_lookup_res res = {
2504 .server = server,
2505 .fattr = fattr,
2506 .fh = fhandle,
2507 };
2508 struct rpc_message msg = {
2509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2510 .rpc_argp = &args,
2511 .rpc_resp = &res,
2512 };
2513
2514 nfs_fattr_init(fattr);
2515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002517 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 dprintk("NFS reply lookup: %d\n", status);
2519 return status;
2520}
2521
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002522void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2523{
2524 memset(fh, 0, sizeof(struct nfs_fh));
2525 fattr->fsid.major = 1;
2526 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2527 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2528 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2529 fattr->nlink = 2;
2530}
2531
Bryan Schumaker7c513052011-03-24 17:12:24 +00002532static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2533 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
2535 struct nfs4_exception exception = { };
2536 int err;
2537 do {
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002538 int status;
2539
2540 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2541 switch (status) {
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002542 case -NFS4ERR_BADNAME:
2543 return -ENOENT;
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002544 case -NFS4ERR_MOVED:
Trond Myklebusta6f498a2011-11-07 15:50:32 -05002545 return nfs4_get_referral(dir, name, fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002546 case -NFS4ERR_WRONGSEC:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002547 nfs_fixup_secinfo_attributes(fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002548 }
2549 err = nfs4_handle_exception(NFS_SERVER(dir),
2550 status, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 } while (exception.retry);
2552 return err;
2553}
2554
2555static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2556{
Trond Myklebust76b32992007-08-10 17:45:11 -04002557 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 struct nfs4_accessargs args = {
2559 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002560 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002562 struct nfs4_accessres res = {
2563 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002564 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 struct rpc_message msg = {
2566 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2567 .rpc_argp = &args,
2568 .rpc_resp = &res,
2569 .rpc_cred = entry->cred,
2570 };
2571 int mode = entry->mask;
2572 int status;
2573
2574 /*
2575 * Determine which access bits we want to ask for...
2576 */
2577 if (mode & MAY_READ)
2578 args.access |= NFS4_ACCESS_READ;
2579 if (S_ISDIR(inode->i_mode)) {
2580 if (mode & MAY_WRITE)
2581 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2582 if (mode & MAY_EXEC)
2583 args.access |= NFS4_ACCESS_LOOKUP;
2584 } else {
2585 if (mode & MAY_WRITE)
2586 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2587 if (mode & MAY_EXEC)
2588 args.access |= NFS4_ACCESS_EXECUTE;
2589 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002590
2591 res.fattr = nfs_alloc_fattr();
2592 if (res.fattr == NULL)
2593 return -ENOMEM;
2594
Bryan Schumaker7c513052011-03-24 17:12:24 +00002595 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 if (!status) {
2597 entry->mask = 0;
2598 if (res.access & NFS4_ACCESS_READ)
2599 entry->mask |= MAY_READ;
2600 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2601 entry->mask |= MAY_WRITE;
2602 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2603 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002604 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002606 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 return status;
2608}
2609
2610static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2611{
2612 struct nfs4_exception exception = { };
2613 int err;
2614 do {
2615 err = nfs4_handle_exception(NFS_SERVER(inode),
2616 _nfs4_proc_access(inode, entry),
2617 &exception);
2618 } while (exception.retry);
2619 return err;
2620}
2621
2622/*
2623 * TODO: For the time being, we don't try to get any attributes
2624 * along with any of the zero-copy operations READ, READDIR,
2625 * READLINK, WRITE.
2626 *
2627 * In the case of the first three, we want to put the GETATTR
2628 * after the read-type operation -- this is because it is hard
2629 * to predict the length of a GETATTR response in v4, and thus
2630 * align the READ data correctly. This means that the GETATTR
2631 * may end up partially falling into the page cache, and we should
2632 * shift it into the 'tail' of the xdr_buf before processing.
2633 * To do this efficiently, we need to know the total length
2634 * of data received, which doesn't seem to be available outside
2635 * of the RPC layer.
2636 *
2637 * In the case of WRITE, we also want to put the GETATTR after
2638 * the operation -- in this case because we want to make sure
2639 * we get the post-operation mtime and size. This means that
2640 * we can't use xdr_encode_pages() as written: we need a variant
2641 * of it which would leave room in the 'tail' iovec.
2642 *
2643 * Both of these changes to the XDR layer would in fact be quite
2644 * minor, but I decided to leave them for a subsequent patch.
2645 */
2646static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2647 unsigned int pgbase, unsigned int pglen)
2648{
2649 struct nfs4_readlink args = {
2650 .fh = NFS_FH(inode),
2651 .pgbase = pgbase,
2652 .pglen = pglen,
2653 .pages = &page,
2654 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002655 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 struct rpc_message msg = {
2657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2658 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002659 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 };
2661
Bryan Schumaker7c513052011-03-24 17:12:24 +00002662 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 -07002663}
2664
2665static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2666 unsigned int pgbase, unsigned int pglen)
2667{
2668 struct nfs4_exception exception = { };
2669 int err;
2670 do {
2671 err = nfs4_handle_exception(NFS_SERVER(inode),
2672 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2673 &exception);
2674 } while (exception.retry);
2675 return err;
2676}
2677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678/*
2679 * Got race?
2680 * We will need to arrange for the VFS layer to provide an atomic open.
2681 * Until then, this create/open method is prone to inefficiency and race
2682 * conditions due to the lookup, create, and open VFS calls from sys_open()
2683 * placed on the wire.
2684 *
2685 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2686 * The file will be opened again in the subsequent VFS open call
2687 * (nfs4_proc_file_open).
2688 *
2689 * The open for read will just hang around to be used by any process that
2690 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2691 */
2692
2693static int
2694nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002695 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
Al Viro82a2c1b2011-06-22 18:30:55 -04002697 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002699 struct rpc_cred *cred = NULL;
2700 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 int status = 0;
2702
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002703 if (ctx != NULL) {
2704 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002705 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002706 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002708 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002709 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002710 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 if (IS_ERR(state)) {
2712 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002713 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002715 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002716 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002717 if (ctx != NULL)
2718 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002719 else
Al Viro643168c2011-06-22 18:20:23 -04002720 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721out:
2722 return status;
2723}
2724
2725static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2726{
Trond Myklebust16e42952005-10-27 22:12:44 -04002727 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002728 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002730 .name.len = name->len,
2731 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002732 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002734 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002735 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002736 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2739 .rpc_argp = &args,
2740 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002742 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Trond Myklebustd3468902010-04-16 16:22:50 -04002744 res.dir_attr = nfs_alloc_fattr();
2745 if (res.dir_attr == NULL)
2746 goto out;
2747
Bryan Schumaker7c513052011-03-24 17:12:24 +00002748 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002749 if (status == 0) {
2750 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002751 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002752 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002753 nfs_free_fattr(res.dir_attr);
2754out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 return status;
2756}
2757
2758static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2759{
2760 struct nfs4_exception exception = { };
2761 int err;
2762 do {
2763 err = nfs4_handle_exception(NFS_SERVER(dir),
2764 _nfs4_proc_remove(dir, name),
2765 &exception);
2766 } while (exception.retry);
2767 return err;
2768}
2769
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002770static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002772 struct nfs_server *server = NFS_SERVER(dir);
2773 struct nfs_removeargs *args = msg->rpc_argp;
2774 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Trond Myklebusta65318b2009-03-11 14:10:28 -04002776 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002777 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002779 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780}
2781
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002782static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2783{
2784 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2785 &data->args.seq_args,
2786 &data->res.seq_res,
2787 task))
2788 return;
2789 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790}
2791
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002792static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002794 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2795
Trond Myklebust14516c32010-07-31 14:29:06 -04002796 if (!nfs4_sequence_done(task, &res->seq_res))
2797 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002798 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002799 return 0;
2800 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002801 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002802 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803}
2804
Jeff Laytond3d41522010-09-17 17:31:57 -04002805static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2806{
2807 struct nfs_server *server = NFS_SERVER(dir);
2808 struct nfs_renameargs *arg = msg->rpc_argp;
2809 struct nfs_renameres *res = msg->rpc_resp;
2810
2811 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2812 arg->bitmask = server->attr_bitmask;
2813 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002814 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002815}
2816
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002817static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2818{
2819 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2820 &data->args.seq_args,
2821 &data->res.seq_res,
2822 task))
2823 return;
2824 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002825}
2826
2827static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2828 struct inode *new_dir)
2829{
2830 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2831
2832 if (!nfs4_sequence_done(task, &res->seq_res))
2833 return 0;
2834 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2835 return 0;
2836
2837 update_changeattr(old_dir, &res->old_cinfo);
2838 nfs_post_op_update_inode(old_dir, res->old_fattr);
2839 update_changeattr(new_dir, &res->new_cinfo);
2840 nfs_post_op_update_inode(new_dir, res->new_fattr);
2841 return 1;
2842}
2843
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2845 struct inode *new_dir, struct qstr *new_name)
2846{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002847 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002848 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 .old_dir = NFS_FH(old_dir),
2850 .new_dir = NFS_FH(new_dir),
2851 .old_name = old_name,
2852 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002853 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002855 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002856 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002857 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 struct rpc_message msg = {
2859 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2860 .rpc_argp = &arg,
2861 .rpc_resp = &res,
2862 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002863 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Trond Myklebust011fff72010-04-16 16:22:49 -04002865 res.old_fattr = nfs_alloc_fattr();
2866 res.new_fattr = nfs_alloc_fattr();
2867 if (res.old_fattr == NULL || res.new_fattr == NULL)
2868 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Bryan Schumaker7c513052011-03-24 17:12:24 +00002870 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 if (!status) {
2872 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002873 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002875 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002877out:
2878 nfs_free_fattr(res.new_fattr);
2879 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 return status;
2881}
2882
2883static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2884 struct inode *new_dir, struct qstr *new_name)
2885{
2886 struct nfs4_exception exception = { };
2887 int err;
2888 do {
2889 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2890 _nfs4_proc_rename(old_dir, old_name,
2891 new_dir, new_name),
2892 &exception);
2893 } while (exception.retry);
2894 return err;
2895}
2896
2897static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2898{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002899 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 struct nfs4_link_arg arg = {
2901 .fh = NFS_FH(inode),
2902 .dir_fh = NFS_FH(dir),
2903 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002904 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002906 struct nfs4_link_res res = {
2907 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002908 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 struct rpc_message msg = {
2910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2911 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002912 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002914 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Trond Myklebust136f2622010-04-16 16:22:49 -04002916 res.fattr = nfs_alloc_fattr();
2917 res.dir_attr = nfs_alloc_fattr();
2918 if (res.fattr == NULL || res.dir_attr == NULL)
2919 goto out;
2920
Bryan Schumaker7c513052011-03-24 17:12:24 +00002921 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002922 if (!status) {
2923 update_changeattr(dir, &res.cinfo);
2924 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002925 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002926 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002927out:
2928 nfs_free_fattr(res.dir_attr);
2929 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 return status;
2931}
2932
2933static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2934{
2935 struct nfs4_exception exception = { };
2936 int err;
2937 do {
2938 err = nfs4_handle_exception(NFS_SERVER(inode),
2939 _nfs4_proc_link(inode, dir, name),
2940 &exception);
2941 } while (exception.retry);
2942 return err;
2943}
2944
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002945struct nfs4_createdata {
2946 struct rpc_message msg;
2947 struct nfs4_create_arg arg;
2948 struct nfs4_create_res res;
2949 struct nfs_fh fh;
2950 struct nfs_fattr fattr;
2951 struct nfs_fattr dir_fattr;
2952};
2953
2954static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2955 struct qstr *name, struct iattr *sattr, u32 ftype)
2956{
2957 struct nfs4_createdata *data;
2958
2959 data = kzalloc(sizeof(*data), GFP_KERNEL);
2960 if (data != NULL) {
2961 struct nfs_server *server = NFS_SERVER(dir);
2962
2963 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2964 data->msg.rpc_argp = &data->arg;
2965 data->msg.rpc_resp = &data->res;
2966 data->arg.dir_fh = NFS_FH(dir);
2967 data->arg.server = server;
2968 data->arg.name = name;
2969 data->arg.attrs = sattr;
2970 data->arg.ftype = ftype;
2971 data->arg.bitmask = server->attr_bitmask;
2972 data->res.server = server;
2973 data->res.fh = &data->fh;
2974 data->res.fattr = &data->fattr;
2975 data->res.dir_fattr = &data->dir_fattr;
2976 nfs_fattr_init(data->res.fattr);
2977 nfs_fattr_init(data->res.dir_fattr);
2978 }
2979 return data;
2980}
2981
2982static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2983{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002984 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002985 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002986 if (status == 0) {
2987 update_changeattr(dir, &data->res.dir_cinfo);
2988 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2989 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2990 }
2991 return status;
2992}
2993
2994static void nfs4_free_createdata(struct nfs4_createdata *data)
2995{
2996 kfree(data);
2997}
2998
Chuck Lever4f390c12006-08-22 20:06:22 -04002999static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003000 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003002 struct nfs4_createdata *data;
3003 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Chuck Lever94a6d752006-08-22 20:06:23 -04003005 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003006 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003007
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003008 status = -ENOMEM;
3009 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3010 if (data == NULL)
3011 goto out;
3012
3013 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3014 data->arg.u.symlink.pages = &page;
3015 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003017 status = nfs4_do_create(dir, dentry, data);
3018
3019 nfs4_free_createdata(data);
3020out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 return status;
3022}
3023
Chuck Lever4f390c12006-08-22 20:06:22 -04003024static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003025 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026{
3027 struct nfs4_exception exception = { };
3028 int err;
3029 do {
3030 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003031 _nfs4_proc_symlink(dir, dentry, page,
3032 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 &exception);
3034 } while (exception.retry);
3035 return err;
3036}
3037
3038static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3039 struct iattr *sattr)
3040{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003041 struct nfs4_createdata *data;
3042 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003044 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3045 if (data == NULL)
3046 goto out;
3047
3048 status = nfs4_do_create(dir, dentry, data);
3049
3050 nfs4_free_createdata(data);
3051out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 return status;
3053}
3054
3055static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3056 struct iattr *sattr)
3057{
3058 struct nfs4_exception exception = { };
3059 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003060
3061 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 do {
3063 err = nfs4_handle_exception(NFS_SERVER(dir),
3064 _nfs4_proc_mkdir(dir, dentry, sattr),
3065 &exception);
3066 } while (exception.retry);
3067 return err;
3068}
3069
3070static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003071 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
3073 struct inode *dir = dentry->d_inode;
3074 struct nfs4_readdir_arg args = {
3075 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003076 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 .pgbase = 0,
3078 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003079 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003080 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 };
3082 struct nfs4_readdir_res res;
3083 struct rpc_message msg = {
3084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3085 .rpc_argp = &args,
3086 .rpc_resp = &res,
3087 .rpc_cred = cred,
3088 };
3089 int status;
3090
Harvey Harrison3110ff82008-05-02 13:42:44 -07003091 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003092 dentry->d_parent->d_name.name,
3093 dentry->d_name.name,
3094 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3096 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003097 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 -05003098 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003100 status += args.pgbase;
3101 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003102
3103 nfs_invalidate_atime(dir);
3104
Harvey Harrison3110ff82008-05-02 13:42:44 -07003105 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 return status;
3107}
3108
3109static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003110 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
3112 struct nfs4_exception exception = { };
3113 int err;
3114 do {
3115 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3116 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003117 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 &exception);
3119 } while (exception.retry);
3120 return err;
3121}
3122
3123static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3124 struct iattr *sattr, dev_t rdev)
3125{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003126 struct nfs4_createdata *data;
3127 int mode = sattr->ia_mode;
3128 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
3130 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3131 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003132
3133 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3134 if (data == NULL)
3135 goto out;
3136
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003138 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003140 data->arg.ftype = NF4BLK;
3141 data->arg.u.device.specdata1 = MAJOR(rdev);
3142 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 }
3144 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003145 data->arg.ftype = NF4CHR;
3146 data->arg.u.device.specdata1 = MAJOR(rdev);
3147 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003150 status = nfs4_do_create(dir, dentry, data);
3151
3152 nfs4_free_createdata(data);
3153out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 return status;
3155}
3156
3157static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3158 struct iattr *sattr, dev_t rdev)
3159{
3160 struct nfs4_exception exception = { };
3161 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003162
3163 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 do {
3165 err = nfs4_handle_exception(NFS_SERVER(dir),
3166 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3167 &exception);
3168 } while (exception.retry);
3169 return err;
3170}
3171
3172static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3173 struct nfs_fsstat *fsstat)
3174{
3175 struct nfs4_statfs_arg args = {
3176 .fh = fhandle,
3177 .bitmask = server->attr_bitmask,
3178 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003179 struct nfs4_statfs_res res = {
3180 .fsstat = fsstat,
3181 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 struct rpc_message msg = {
3183 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3184 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003185 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 };
3187
Trond Myklebust0e574af2005-10-27 22:12:38 -04003188 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003189 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190}
3191
3192static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3193{
3194 struct nfs4_exception exception = { };
3195 int err;
3196 do {
3197 err = nfs4_handle_exception(server,
3198 _nfs4_proc_statfs(server, fhandle, fsstat),
3199 &exception);
3200 } while (exception.retry);
3201 return err;
3202}
3203
3204static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3205 struct nfs_fsinfo *fsinfo)
3206{
3207 struct nfs4_fsinfo_arg args = {
3208 .fh = fhandle,
3209 .bitmask = server->attr_bitmask,
3210 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003211 struct nfs4_fsinfo_res res = {
3212 .fsinfo = fsinfo,
3213 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 struct rpc_message msg = {
3215 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3216 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003217 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 };
3219
Bryan Schumaker7c513052011-03-24 17:12:24 +00003220 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
3223static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3224{
3225 struct nfs4_exception exception = { };
3226 int err;
3227
3228 do {
3229 err = nfs4_handle_exception(server,
3230 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3231 &exception);
3232 } while (exception.retry);
3233 return err;
3234}
3235
3236static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3237{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003238 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3240}
3241
3242static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3243 struct nfs_pathconf *pathconf)
3244{
3245 struct nfs4_pathconf_arg args = {
3246 .fh = fhandle,
3247 .bitmask = server->attr_bitmask,
3248 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003249 struct nfs4_pathconf_res res = {
3250 .pathconf = pathconf,
3251 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 struct rpc_message msg = {
3253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3254 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003255 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 };
3257
3258 /* None of the pathconf attributes are mandatory to implement */
3259 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3260 memset(pathconf, 0, sizeof(*pathconf));
3261 return 0;
3262 }
3263
Trond Myklebust0e574af2005-10-27 22:12:38 -04003264 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003265 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266}
3267
3268static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3269 struct nfs_pathconf *pathconf)
3270{
3271 struct nfs4_exception exception = { };
3272 int err;
3273
3274 do {
3275 err = nfs4_handle_exception(server,
3276 _nfs4_proc_pathconf(server, fhandle, pathconf),
3277 &exception);
3278 } while (exception.retry);
3279 return err;
3280}
3281
Benny Halevyd20581a2011-05-22 19:52:03 +03003282void __nfs4_read_done_cb(struct nfs_read_data *data)
3283{
3284 nfs_invalidate_atime(data->inode);
3285}
3286
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003287static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288{
Trond Myklebustec06c092006-03-20 13:44:27 -05003289 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003291 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003292 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003293 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003295
Benny Halevyd20581a2011-05-22 19:52:03 +03003296 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003298 renew_lease(server, data->timestamp);
3299 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300}
3301
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003302static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3303{
3304
3305 dprintk("--> %s\n", __func__);
3306
3307 if (!nfs4_sequence_done(task, &data->res.seq_res))
3308 return -EAGAIN;
3309
Benny Halevyd20581a2011-05-22 19:52:03 +03003310 return data->read_done_cb ? data->read_done_cb(task, data) :
3311 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003312}
3313
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003314static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003317 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003318 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003319 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320}
3321
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003322static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3323{
3324 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3325 &data->args.seq_args,
3326 &data->res.seq_res,
3327 task))
3328 return;
3329 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330}
3331
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003332/* Reset the the nfs_read_data to send the read to the MDS. */
3333void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3334{
3335 dprintk("%s Reset task for i/o through\n", __func__);
3336 put_lseg(data->lseg);
3337 data->lseg = NULL;
3338 /* offsets will differ in the dense stripe case */
3339 data->args.offset = data->mds_offset;
3340 data->ds_clp = NULL;
3341 data->args.fh = NFS_FH(data->inode);
3342 data->read_done_cb = nfs4_read_done_cb;
3343 task->tk_ops = data->mds_ops;
3344 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3345}
3346EXPORT_SYMBOL_GPL(nfs4_reset_read);
3347
Fred Isamanb029bc92011-03-03 15:13:42 +00003348static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 struct inode *inode = data->inode;
3351
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003352 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003353 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003354 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003356 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003358 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003359 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003360 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361}
3362
Fred Isamanb029bc92011-03-03 15:13:42 +00003363static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3364{
3365 if (!nfs4_sequence_done(task, &data->res.seq_res))
3366 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003367 return data->write_done_cb ? data->write_done_cb(task, data) :
3368 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003369}
3370
Fred Isamana69aef12011-03-03 15:13:47 +00003371/* Reset the the nfs_write_data to send the write to the MDS. */
3372void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3373{
3374 dprintk("%s Reset task for i/o through\n", __func__);
3375 put_lseg(data->lseg);
3376 data->lseg = NULL;
3377 data->ds_clp = NULL;
3378 data->write_done_cb = nfs4_write_done_cb;
3379 data->args.fh = NFS_FH(data->inode);
3380 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3381 data->args.offset = data->mds_offset;
3382 data->res.fattr = &data->fattr;
3383 task->tk_ops = data->mds_ops;
3384 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3385}
3386EXPORT_SYMBOL_GPL(nfs4_reset_write);
3387
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003388static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003390 struct nfs_server *server = NFS_SERVER(data->inode);
3391
Fred Isaman7ffd1062011-03-03 15:13:46 +00003392 if (data->lseg) {
3393 data->args.bitmask = NULL;
3394 data->res.fattr = NULL;
3395 } else
3396 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003397 if (!data->write_done_cb)
3398 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003399 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 data->timestamp = jiffies;
3401
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003402 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003403 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404}
3405
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003406static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3407{
3408 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3409 &data->args.seq_args,
3410 &data->res.seq_res,
3411 task))
3412 return;
3413 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414}
3415
Fred Isaman5f452432011-03-23 13:27:46 +00003416static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003419
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003420 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003421 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003422 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003424 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426}
3427
Fred Isaman5f452432011-03-23 13:27:46 +00003428static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3429{
3430 if (!nfs4_sequence_done(task, &data->res.seq_res))
3431 return -EAGAIN;
3432 return data->write_done_cb(task, data);
3433}
3434
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003435static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003437 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003438
3439 if (data->lseg) {
3440 data->args.bitmask = NULL;
3441 data->res.fattr = NULL;
3442 } else
3443 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003444 if (!data->write_done_cb)
3445 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003446 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003447 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003448 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449}
3450
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003451struct nfs4_renewdata {
3452 struct nfs_client *client;
3453 unsigned long timestamp;
3454};
3455
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456/*
3457 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3458 * standalone procedure for queueing an asynchronous RENEW.
3459 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003460static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003461{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003462 struct nfs4_renewdata *data = calldata;
3463 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003464
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003465 if (atomic_read(&clp->cl_count) > 1)
3466 nfs4_schedule_state_renewal(clp);
3467 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003468 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003469}
3470
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003471static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003473 struct nfs4_renewdata *data = calldata;
3474 struct nfs_client *clp = data->client;
3475 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
3477 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003478 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003479 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3480 return;
3481 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003482 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003483 return;
3484 }
3485 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003487 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488}
3489
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003490static const struct rpc_call_ops nfs4_renew_ops = {
3491 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003492 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003493};
3494
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003495static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496{
3497 struct rpc_message msg = {
3498 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3499 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003500 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003502 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003504 if (renew_flags == 0)
3505 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003506 if (!atomic_inc_not_zero(&clp->cl_count))
3507 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003508 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003509 if (data == NULL)
3510 return -ENOMEM;
3511 data->client = clp;
3512 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003514 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515}
3516
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003517static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518{
3519 struct rpc_message msg = {
3520 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3521 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003522 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 };
3524 unsigned long now = jiffies;
3525 int status;
3526
3527 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3528 if (status < 0)
3529 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003530 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 return 0;
3532}
3533
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003534static inline int nfs4_server_supports_acls(struct nfs_server *server)
3535{
3536 return (server->caps & NFS_CAP_ACLS)
3537 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3538 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3539}
3540
3541/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3542 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3543 * the stack.
3544 */
3545#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3546
Neil Hormane9e3d722011-03-04 19:26:03 -05003547static int buf_to_pages_noslab(const void *buf, size_t buflen,
3548 struct page **pages, unsigned int *pgbase)
3549{
3550 struct page *newpage, **spages;
3551 int rc = 0;
3552 size_t len;
3553 spages = pages;
3554
3555 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003556 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003557 newpage = alloc_page(GFP_KERNEL);
3558
3559 if (newpage == NULL)
3560 goto unwind;
3561 memcpy(page_address(newpage), buf, len);
3562 buf += len;
3563 buflen -= len;
3564 *pages++ = newpage;
3565 rc++;
3566 } while (buflen != 0);
3567
3568 return rc;
3569
3570unwind:
3571 for(; rc > 0; rc--)
3572 __free_page(spages[rc-1]);
3573 return -ENOMEM;
3574}
3575
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003576struct nfs4_cached_acl {
3577 int cached;
3578 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003579 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003580};
3581
3582static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003583{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003584 struct nfs_inode *nfsi = NFS_I(inode);
3585
3586 spin_lock(&inode->i_lock);
3587 kfree(nfsi->nfs4_acl);
3588 nfsi->nfs4_acl = acl;
3589 spin_unlock(&inode->i_lock);
3590}
3591
3592static void nfs4_zap_acl_attr(struct inode *inode)
3593{
3594 nfs4_set_cached_acl(inode, NULL);
3595}
3596
3597static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3598{
3599 struct nfs_inode *nfsi = NFS_I(inode);
3600 struct nfs4_cached_acl *acl;
3601 int ret = -ENOENT;
3602
3603 spin_lock(&inode->i_lock);
3604 acl = nfsi->nfs4_acl;
3605 if (acl == NULL)
3606 goto out;
3607 if (buf == NULL) /* user is just asking for length */
3608 goto out_len;
3609 if (acl->cached == 0)
3610 goto out;
3611 ret = -ERANGE; /* see getxattr(2) man page */
3612 if (acl->len > buflen)
3613 goto out;
3614 memcpy(buf, acl->data, acl->len);
3615out_len:
3616 ret = acl->len;
3617out:
3618 spin_unlock(&inode->i_lock);
3619 return ret;
3620}
3621
3622static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3623{
3624 struct nfs4_cached_acl *acl;
3625
3626 if (buf && acl_len <= PAGE_SIZE) {
3627 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3628 if (acl == NULL)
3629 goto out;
3630 acl->cached = 1;
3631 memcpy(acl->data, buf, acl_len);
3632 } else {
3633 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3634 if (acl == NULL)
3635 goto out;
3636 acl->cached = 0;
3637 }
3638 acl->len = acl_len;
3639out:
3640 nfs4_set_cached_acl(inode, acl);
3641}
3642
Andy Adamsonbf118a32011-12-07 11:55:27 -05003643/*
3644 * The getxattr API returns the required buffer length when called with a
3645 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3646 * the required buf. On a NULL buf, we send a page of data to the server
3647 * guessing that the ACL request can be serviced by a page. If so, we cache
3648 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3649 * the cache. If not so, we throw away the page, and cache the required
3650 * length. The next getxattr call will then produce another round trip to
3651 * the server, this time with the input buf of the required size.
3652 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003653static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003654{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003655 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003656 struct nfs_getaclargs args = {
3657 .fh = NFS_FH(inode),
3658 .acl_pages = pages,
3659 .acl_len = buflen,
3660 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003661 struct nfs_getaclres res = {
3662 .acl_len = buflen,
3663 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003664 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003665 struct rpc_message msg = {
3666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3667 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003668 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003669 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003670 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003671
Andy Adamsonbf118a32011-12-07 11:55:27 -05003672 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3673 /* As long as we're doing a round trip to the server anyway,
3674 * let's be prepared for a page of acl data. */
3675 if (npages == 0)
3676 npages = 1;
3677
3678 for (i = 0; i < npages; i++) {
3679 pages[i] = alloc_page(GFP_KERNEL);
3680 if (!pages[i])
3681 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003682 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003683 if (npages > 1) {
3684 /* for decoding across pages */
Trond Myklebust331818f2012-02-03 18:30:53 -05003685 res.acl_scratch = alloc_page(GFP_KERNEL);
3686 if (!res.acl_scratch)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003687 goto out_free;
3688 }
3689 args.acl_len = npages * PAGE_SIZE;
3690 args.acl_pgbase = 0;
3691 /* Let decode_getfacl know not to fail if the ACL data is larger than
3692 * the page we send as a guess */
3693 if (buf == NULL)
3694 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3695 resp_buf = page_address(pages[0]);
3696
Peng Taode040be2012-01-10 22:42:47 +08003697 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003698 __func__, buf, buflen, npages, args.acl_len);
3699 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3700 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003701 if (ret)
3702 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003703
3704 acl_len = res.acl_len - res.acl_data_offset;
3705 if (acl_len > args.acl_len)
3706 nfs4_write_cached_acl(inode, NULL, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003707 else
Andy Adamsonbf118a32011-12-07 11:55:27 -05003708 nfs4_write_cached_acl(inode, resp_buf + res.acl_data_offset,
3709 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003710 if (buf) {
3711 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003712 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003713 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003714 _copy_from_pages(buf, pages, res.acl_data_offset,
Sachin Prabhu20e0fa92012-03-22 16:46:28 +00003715 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003716 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003717 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003718out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003719 for (i = 0; i < npages; i++)
3720 if (pages[i])
3721 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003722 if (res.acl_scratch)
3723 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003724 return ret;
3725}
3726
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003727static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3728{
3729 struct nfs4_exception exception = { };
3730 ssize_t ret;
3731 do {
3732 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3733 if (ret >= 0)
3734 break;
3735 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3736 } while (exception.retry);
3737 return ret;
3738}
3739
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003740static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3741{
3742 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003743 int ret;
3744
3745 if (!nfs4_server_supports_acls(server))
3746 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003747 ret = nfs_revalidate_inode(server, inode);
3748 if (ret < 0)
3749 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003750 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3751 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003752 ret = nfs4_read_cached_acl(inode, buf, buflen);
3753 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003754 /* -ENOENT is returned if there is no ACL or if there is an ACL
3755 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003756 return ret;
3757 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003758}
3759
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003760static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003761{
3762 struct nfs_server *server = NFS_SERVER(inode);
3763 struct page *pages[NFS4ACL_MAXPAGES];
3764 struct nfs_setaclargs arg = {
3765 .fh = NFS_FH(inode),
3766 .acl_pages = pages,
3767 .acl_len = buflen,
3768 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003769 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003770 struct rpc_message msg = {
3771 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3772 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003773 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003774 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003775 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003776
3777 if (!nfs4_server_supports_acls(server))
3778 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003779 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3780 if (i < 0)
3781 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003782 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003783 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003784
3785 /*
3786 * Free each page after tx, so the only ref left is
3787 * held by the network stack
3788 */
3789 for (; i > 0; i--)
3790 put_page(pages[i-1]);
3791
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003792 /*
3793 * Acl update can result in inode attribute update.
3794 * so mark the attribute cache invalid.
3795 */
3796 spin_lock(&inode->i_lock);
3797 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3798 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003799 nfs_access_zap_cache(inode);
3800 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003801 return ret;
3802}
3803
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003804static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3805{
3806 struct nfs4_exception exception = { };
3807 int err;
3808 do {
3809 err = nfs4_handle_exception(NFS_SERVER(inode),
3810 __nfs4_proc_set_acl(inode, buf, buflen),
3811 &exception);
3812 } while (exception.retry);
3813 return err;
3814}
3815
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003817nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003819 struct nfs_client *clp = server->nfs_client;
3820
3821 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 return 0;
3823 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003824 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003825 case -NFS4ERR_ADMIN_REVOKED:
3826 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003827 if (state == NULL)
3828 break;
3829 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003830 case -NFS4ERR_OPENMODE:
3831 if (state == NULL)
3832 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003833 nfs4_schedule_stateid_recovery(server, state);
3834 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003835 case -NFS4ERR_EXPIRED:
3836 if (state != NULL)
3837 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003839 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003840 nfs4_schedule_lease_recovery(clp);
3841 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003842#if defined(CONFIG_NFS_V4_1)
3843 case -NFS4ERR_BADSESSION:
3844 case -NFS4ERR_BADSLOT:
3845 case -NFS4ERR_BAD_HIGH_SLOT:
3846 case -NFS4ERR_DEADSESSION:
3847 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3848 case -NFS4ERR_SEQ_FALSE_RETRY:
3849 case -NFS4ERR_SEQ_MISORDERED:
3850 dprintk("%s ERROR %d, Reset session\n", __func__,
3851 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003852 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003853 task->tk_status = 0;
3854 return -EAGAIN;
3855#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003857 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003858 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003859 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3861 task->tk_status = 0;
3862 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003863 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 case -NFS4ERR_OLD_STATEID:
3865 task->tk_status = 0;
3866 return -EAGAIN;
3867 }
3868 task->tk_status = nfs4_map_errors(task->tk_status);
3869 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003870wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003871 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003872 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3873 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3874 task->tk_status = 0;
3875 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876}
3877
Chuck Levercd937102012-03-02 17:14:31 -05003878static void nfs4_construct_boot_verifier(struct nfs_client *clp,
3879 nfs4_verifier *bootverf)
3880{
3881 __be32 verf[2];
3882
3883 verf[0] = htonl((u32)clp->cl_boot_time.tv_sec);
3884 verf[1] = htonl((u32)clp->cl_boot_time.tv_nsec);
3885 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3886}
3887
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003888int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3889 unsigned short port, struct rpc_cred *cred,
3890 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891{
3892 nfs4_verifier sc_verifier;
3893 struct nfs4_setclientid setclientid = {
3894 .sc_verifier = &sc_verifier,
3895 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003896 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 };
3898 struct rpc_message msg = {
3899 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3900 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003901 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003902 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 int loop = 0;
3905 int status;
3906
Chuck Levercd937102012-03-02 17:14:31 -05003907 nfs4_construct_boot_verifier(clp, &sc_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
3909 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05003910 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003912 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003913 clp->cl_ipaddr,
3914 rpc_peeraddr2str(clp->cl_rpcclient,
3915 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003916 rpc_peeraddr2str(clp->cl_rpcclient,
3917 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003918 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 clp->cl_id_uniquifier);
3920 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003921 sizeof(setclientid.sc_netid),
3922 rpc_peeraddr2str(clp->cl_rpcclient,
3923 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003925 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05003927 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003929 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 if (status != -NFS4ERR_CLID_INUSE)
3931 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003932 if (loop != 0) {
3933 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003935 }
3936 ++loop;
3937 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 }
3939 return status;
3940}
3941
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003942int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003943 struct nfs4_setclientid_res *arg,
3944 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945{
3946 struct nfs_fsinfo fsinfo;
3947 struct rpc_message msg = {
3948 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003949 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003951 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 };
3953 unsigned long now;
3954 int status;
3955
3956 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003957 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 if (status == 0) {
3959 spin_lock(&clp->cl_lock);
3960 clp->cl_lease_time = fsinfo.lease_time * HZ;
3961 clp->cl_last_renewal = now;
3962 spin_unlock(&clp->cl_lock);
3963 }
3964 return status;
3965}
3966
Trond Myklebustfe650402006-01-03 09:55:18 +01003967struct nfs4_delegreturndata {
3968 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003969 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003970 struct nfs_fh fh;
3971 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003972 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003973 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003974 int rpc_status;
3975};
3976
Trond Myklebustfe650402006-01-03 09:55:18 +01003977static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3978{
3979 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003980
Trond Myklebust14516c32010-07-31 14:29:06 -04003981 if (!nfs4_sequence_done(task, &data->res.seq_res))
3982 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003983
Ricardo Labiaga79708862009-12-07 09:23:21 -05003984 switch (task->tk_status) {
3985 case -NFS4ERR_STALE_STATEID:
3986 case -NFS4ERR_EXPIRED:
3987 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003988 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003989 break;
3990 default:
3991 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3992 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003993 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003994 return;
3995 }
3996 }
3997 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003998}
3999
4000static void nfs4_delegreturn_release(void *calldata)
4001{
Trond Myklebustfe650402006-01-03 09:55:18 +01004002 kfree(calldata);
4003}
4004
Andy Adamson938e1012009-04-01 09:22:28 -04004005#if defined(CONFIG_NFS_V4_1)
4006static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4007{
4008 struct nfs4_delegreturndata *d_data;
4009
4010 d_data = (struct nfs4_delegreturndata *)data;
4011
Trond Myklebust035168a2010-06-16 09:52:26 -04004012 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004013 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004014 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004015 return;
4016 rpc_call_start(task);
4017}
4018#endif /* CONFIG_NFS_V4_1 */
4019
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004020static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004021#if defined(CONFIG_NFS_V4_1)
4022 .rpc_call_prepare = nfs4_delegreturn_prepare,
4023#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004024 .rpc_call_done = nfs4_delegreturn_done,
4025 .rpc_release = nfs4_delegreturn_release,
4026};
4027
Trond Myklebuste6f81072008-01-24 18:14:34 -05004028static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004029{
4030 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004031 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004032 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004033 struct rpc_message msg = {
4034 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4035 .rpc_cred = cred,
4036 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004037 struct rpc_task_setup task_setup_data = {
4038 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004039 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004040 .callback_ops = &nfs4_delegreturn_ops,
4041 .flags = RPC_TASK_ASYNC,
4042 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004043 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004044
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004045 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004046 if (data == NULL)
4047 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004048 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004049 data->args.fhandle = &data->fh;
4050 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004051 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004052 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004053 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004054 data->res.fattr = &data->fattr;
4055 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004056 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004057 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004058 data->rpc_status = 0;
4059
Trond Myklebustc970aa82007-07-14 15:39:59 -04004060 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004061 msg.rpc_argp = &data->args;
4062 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004063 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004064 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004065 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004066 if (!issync)
4067 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004068 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004069 if (status != 0)
4070 goto out;
4071 status = data->rpc_status;
4072 if (status != 0)
4073 goto out;
4074 nfs_refresh_inode(inode, &data->fattr);
4075out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004076 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004077 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078}
4079
Trond Myklebuste6f81072008-01-24 18:14:34 -05004080int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081{
4082 struct nfs_server *server = NFS_SERVER(inode);
4083 struct nfs4_exception exception = { };
4084 int err;
4085 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004086 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 switch (err) {
4088 case -NFS4ERR_STALE_STATEID:
4089 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 case 0:
4091 return 0;
4092 }
4093 err = nfs4_handle_exception(server, err, &exception);
4094 } while (exception.retry);
4095 return err;
4096}
4097
4098#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4099#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4100
4101/*
4102 * sleep, with exponential backoff, and retry the LOCK operation.
4103 */
4104static unsigned long
4105nfs4_set_lock_task_retry(unsigned long timeout)
4106{
Jeff Laytond3103102011-12-01 22:44:39 +01004107 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 timeout <<= 1;
4109 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4110 return NFS4_LOCK_MAXTIMEOUT;
4111 return timeout;
4112}
4113
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4115{
4116 struct inode *inode = state->inode;
4117 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004118 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004119 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004121 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004123 struct nfs_lockt_res res = {
4124 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 };
4126 struct rpc_message msg = {
4127 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4128 .rpc_argp = &arg,
4129 .rpc_resp = &res,
4130 .rpc_cred = state->owner->so_cred,
4131 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 struct nfs4_lock_state *lsp;
4133 int status;
4134
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004135 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004136 status = nfs4_set_lock_state(state, request);
4137 if (status != 0)
4138 goto out;
4139 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004140 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004141 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004142 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004143 switch (status) {
4144 case 0:
4145 request->fl_type = F_UNLCK;
4146 break;
4147 case -NFS4ERR_DENIED:
4148 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004150 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004151out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 return status;
4153}
4154
4155static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4156{
4157 struct nfs4_exception exception = { };
4158 int err;
4159
4160 do {
4161 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4162 _nfs4_proc_getlk(state, cmd, request),
4163 &exception);
4164 } while (exception.retry);
4165 return err;
4166}
4167
4168static int do_vfs_lock(struct file *file, struct file_lock *fl)
4169{
4170 int res = 0;
4171 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4172 case FL_POSIX:
4173 res = posix_lock_file_wait(file, fl);
4174 break;
4175 case FL_FLOCK:
4176 res = flock_lock_file_wait(file, fl);
4177 break;
4178 default:
4179 BUG();
4180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 return res;
4182}
4183
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004184struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004185 struct nfs_locku_args arg;
4186 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004187 struct nfs4_lock_state *lsp;
4188 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004189 struct file_lock fl;
4190 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004191 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004192};
4193
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004194static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4195 struct nfs_open_context *ctx,
4196 struct nfs4_lock_state *lsp,
4197 struct nfs_seqid *seqid)
4198{
4199 struct nfs4_unlockdata *p;
4200 struct inode *inode = lsp->ls_state->inode;
4201
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004202 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004203 if (p == NULL)
4204 return NULL;
4205 p->arg.fh = NFS_FH(inode);
4206 p->arg.fl = &p->fl;
4207 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004208 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004209 p->arg.stateid = &lsp->ls_stateid;
4210 p->lsp = lsp;
4211 atomic_inc(&lsp->ls_count);
4212 /* Ensure we don't close file until we're done freeing locks! */
4213 p->ctx = get_nfs_open_context(ctx);
4214 memcpy(&p->fl, fl, sizeof(p->fl));
4215 p->server = NFS_SERVER(inode);
4216 return p;
4217}
4218
Trond Myklebust06f814a2006-01-03 09:55:07 +01004219static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004220{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004221 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004222 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004223 nfs4_put_lock_state(calldata->lsp);
4224 put_nfs_open_context(calldata->ctx);
4225 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004226}
4227
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004228static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004229{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004230 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004231
Trond Myklebust14516c32010-07-31 14:29:06 -04004232 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4233 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004234 switch (task->tk_status) {
4235 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004236 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4237 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004238 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004239 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004240 case -NFS4ERR_BAD_STATEID:
4241 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004242 case -NFS4ERR_STALE_STATEID:
4243 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004244 break;
4245 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004246 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004247 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004248 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004249}
4250
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004251static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004252{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004253 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004255 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004256 return;
4257 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004258 /* Note: exit _without_ running nfs4_locku_done */
4259 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004260 return;
4261 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004262 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004263 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004264 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004265 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004266 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004267 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268}
4269
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004270static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004271 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004272 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004273 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004274};
4275
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004276static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4277 struct nfs_open_context *ctx,
4278 struct nfs4_lock_state *lsp,
4279 struct nfs_seqid *seqid)
4280{
4281 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004282 struct rpc_message msg = {
4283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4284 .rpc_cred = ctx->cred,
4285 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004286 struct rpc_task_setup task_setup_data = {
4287 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004288 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004289 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004290 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004291 .flags = RPC_TASK_ASYNC,
4292 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004293
Frank Filz137d6ac2007-07-09 15:32:29 -07004294 /* Ensure this is an unlock - when canceling a lock, the
4295 * canceled lock is passed in, and it won't be an unlock.
4296 */
4297 fl->fl_type = F_UNLCK;
4298
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004299 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4300 if (data == NULL) {
4301 nfs_free_seqid(seqid);
4302 return ERR_PTR(-ENOMEM);
4303 }
4304
Trond Myklebust9d12b212012-01-17 22:04:25 -05004305 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004306 msg.rpc_argp = &data->arg;
4307 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004308 task_setup_data.callback_data = data;
4309 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004310}
4311
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4313{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004314 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004315 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004316 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004317 struct rpc_task *task;
4318 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004319 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320
Trond Myklebust9b073572006-06-29 16:38:34 -04004321 status = nfs4_set_lock_state(state, request);
4322 /* Unlock _before_ we do the RPC call */
4323 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004324 down_read(&nfsi->rwsem);
4325 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4326 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004327 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004328 }
4329 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004330 if (status != 0)
4331 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004332 /* Is this a delegated lock? */
4333 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004334 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004335 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004336 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004337 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004338 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004339 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004340 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004341 status = PTR_ERR(task);
4342 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004343 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004344 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004345 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004346out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004347 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004348 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349}
4350
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004351struct nfs4_lockdata {
4352 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004353 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004354 struct nfs4_lock_state *lsp;
4355 struct nfs_open_context *ctx;
4356 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004357 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004358 int rpc_status;
4359 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004360 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004361};
4362
4363static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004364 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4365 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004366{
4367 struct nfs4_lockdata *p;
4368 struct inode *inode = lsp->ls_state->inode;
4369 struct nfs_server *server = NFS_SERVER(inode);
4370
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004371 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004372 if (p == NULL)
4373 return NULL;
4374
4375 p->arg.fh = NFS_FH(inode);
4376 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004377 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004378 if (p->arg.open_seqid == NULL)
4379 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004380 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004381 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004382 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004383 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004384 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004385 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004386 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004387 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004388 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004389 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004390 atomic_inc(&lsp->ls_count);
4391 p->ctx = get_nfs_open_context(ctx);
4392 memcpy(&p->fl, fl, sizeof(p->fl));
4393 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004394out_free_seqid:
4395 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004396out_free:
4397 kfree(p);
4398 return NULL;
4399}
4400
4401static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4402{
4403 struct nfs4_lockdata *data = calldata;
4404 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405
Harvey Harrison3110ff82008-05-02 13:42:44 -07004406 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004407 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4408 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004409 /* Do we need to do an open_to_lock_owner? */
4410 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004411 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4412 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004413 data->arg.open_stateid = &state->stateid;
4414 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004415 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004416 } else
4417 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004418 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004419 if (nfs4_setup_sequence(data->server,
4420 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004421 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004422 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004423 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004424 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004425}
4426
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004427static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4428{
4429 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4430 nfs4_lock_prepare(task, calldata);
4431}
4432
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004433static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4434{
4435 struct nfs4_lockdata *data = calldata;
4436
Harvey Harrison3110ff82008-05-02 13:42:44 -07004437 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004438
Trond Myklebust14516c32010-07-31 14:29:06 -04004439 if (!nfs4_sequence_done(task, &data->res.seq_res))
4440 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004441
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004442 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004443 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004444 if (data->rpc_status == 0)
4445 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4446 else
4447 goto out;
4448 }
4449 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004450 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004451 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004452 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004453 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004454out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004455 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004456}
4457
4458static void nfs4_lock_release(void *calldata)
4459{
4460 struct nfs4_lockdata *data = calldata;
4461
Harvey Harrison3110ff82008-05-02 13:42:44 -07004462 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004463 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004464 if (data->cancelled != 0) {
4465 struct rpc_task *task;
4466 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4467 data->arg.lock_seqid);
4468 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004469 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004470 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004471 } else
4472 nfs_free_seqid(data->arg.lock_seqid);
4473 nfs4_put_lock_state(data->lsp);
4474 put_nfs_open_context(data->ctx);
4475 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004476 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004477}
4478
4479static const struct rpc_call_ops nfs4_lock_ops = {
4480 .rpc_call_prepare = nfs4_lock_prepare,
4481 .rpc_call_done = nfs4_lock_done,
4482 .rpc_release = nfs4_lock_release,
4483};
4484
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004485static const struct rpc_call_ops nfs4_recover_lock_ops = {
4486 .rpc_call_prepare = nfs4_recover_lock_prepare,
4487 .rpc_call_done = nfs4_lock_done,
4488 .rpc_release = nfs4_lock_release,
4489};
4490
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004491static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4492{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004493 switch (error) {
4494 case -NFS4ERR_ADMIN_REVOKED:
4495 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004496 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004497 if (new_lock_owner != 0 ||
4498 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004499 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004500 break;
4501 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004502 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004503 case -NFS4ERR_EXPIRED:
4504 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004505 };
4506}
4507
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004508static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004509{
4510 struct nfs4_lockdata *data;
4511 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004512 struct rpc_message msg = {
4513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4514 .rpc_cred = state->owner->so_cred,
4515 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004516 struct rpc_task_setup task_setup_data = {
4517 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004518 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004519 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004520 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004521 .flags = RPC_TASK_ASYNC,
4522 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004523 int ret;
4524
Harvey Harrison3110ff82008-05-02 13:42:44 -07004525 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004526 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004527 fl->fl_u.nfs4_fl.owner,
4528 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004529 if (data == NULL)
4530 return -ENOMEM;
4531 if (IS_SETLKW(cmd))
4532 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004533 if (recovery_type > NFS_LOCK_NEW) {
4534 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004535 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004536 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4537 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004538 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004539 msg.rpc_argp = &data->arg;
4540 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004541 task_setup_data.callback_data = data;
4542 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004543 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004544 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004545 ret = nfs4_wait_for_completion_rpc_task(task);
4546 if (ret == 0) {
4547 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004548 if (ret)
4549 nfs4_handle_setlk_error(data->server, data->lsp,
4550 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004551 } else
4552 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004553 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004554 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004555 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556}
4557
4558static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4559{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004560 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004561 struct nfs4_exception exception = {
4562 .inode = state->inode,
4563 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004564 int err;
4565
4566 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004567 /* Cache the lock if possible... */
4568 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4569 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004570 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004571 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004572 break;
4573 nfs4_handle_exception(server, err, &exception);
4574 } while (exception.retry);
4575 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576}
4577
4578static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4579{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004580 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004581 struct nfs4_exception exception = {
4582 .inode = state->inode,
4583 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004584 int err;
4585
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004586 err = nfs4_set_lock_state(state, request);
4587 if (err != 0)
4588 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004589 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004590 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4591 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004592 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004593 switch (err) {
4594 default:
4595 goto out;
4596 case -NFS4ERR_GRACE:
4597 case -NFS4ERR_DELAY:
4598 nfs4_handle_exception(server, err, &exception);
4599 err = 0;
4600 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004601 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004602out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004603 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604}
4605
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004606#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004607static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004608{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004609 int status, ret = NFS_OK;
4610 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004611 struct nfs_server *server = NFS_SERVER(state->inode);
4612
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004613 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4614 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4615 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4616 if (status != NFS_OK) {
4617 nfs41_free_stateid(server, &lsp->ls_stateid);
4618 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4619 ret = status;
4620 }
4621 }
4622 };
4623
4624 return ret;
4625}
4626
4627static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4628{
4629 int status = NFS_OK;
4630
4631 if (test_bit(LK_STATE_IN_USE, &state->flags))
4632 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004633 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004634 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004635 return nfs4_lock_expired(state, request);
4636}
4637#endif
4638
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4640{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004641 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004642 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004643 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004645 if ((fl_flags & FL_POSIX) &&
4646 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4647 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004648 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004649 status = nfs4_set_lock_state(state, request);
4650 if (status != 0)
4651 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004652 request->fl_flags |= FL_ACCESS;
4653 status = do_vfs_lock(request->fl_file, request);
4654 if (status < 0)
4655 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004656 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004657 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004658 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004659 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004660 request->fl_flags = fl_flags & ~FL_SLEEP;
4661 status = do_vfs_lock(request->fl_file, request);
4662 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004663 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004664 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004665 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004666 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004667 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004668 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004669 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004670 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4671 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004672out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004673 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004674out:
4675 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 return status;
4677}
4678
4679static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4680{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004681 struct nfs4_exception exception = {
4682 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004683 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004684 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 int err;
4686
4687 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004688 err = _nfs4_proc_setlk(state, cmd, request);
4689 if (err == -NFS4ERR_DENIED)
4690 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004692 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 } while (exception.retry);
4694 return err;
4695}
4696
4697static int
4698nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4699{
4700 struct nfs_open_context *ctx;
4701 struct nfs4_state *state;
4702 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4703 int status;
4704
4705 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004706 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 state = ctx->state;
4708
4709 if (request->fl_start < 0 || request->fl_end < 0)
4710 return -EINVAL;
4711
Trond Myklebustd9531262009-07-21 19:22:38 -04004712 if (IS_GETLK(cmd)) {
4713 if (state != NULL)
4714 return nfs4_proc_getlk(state, F_GETLK, request);
4715 return 0;
4716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
4718 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4719 return -EINVAL;
4720
Trond Myklebustd9531262009-07-21 19:22:38 -04004721 if (request->fl_type == F_UNLCK) {
4722 if (state != NULL)
4723 return nfs4_proc_unlck(state, cmd, request);
4724 return 0;
4725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726
Trond Myklebustd9531262009-07-21 19:22:38 -04004727 if (state == NULL)
4728 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004729 /*
4730 * Don't rely on the VFS having checked the file open mode,
4731 * since it won't do this for flock() locks.
4732 */
4733 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4734 case F_RDLCK:
4735 if (!(filp->f_mode & FMODE_READ))
4736 return -EBADF;
4737 break;
4738 case F_WRLCK:
4739 if (!(filp->f_mode & FMODE_WRITE))
4740 return -EBADF;
4741 }
4742
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 do {
4744 status = nfs4_proc_setlk(state, cmd, request);
4745 if ((status != -EAGAIN) || IS_SETLK(cmd))
4746 break;
4747 timeout = nfs4_set_lock_task_retry(timeout);
4748 status = -ERESTARTSYS;
4749 if (signalled())
4750 break;
4751 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 return status;
4753}
4754
Trond Myklebust888e6942005-11-04 15:38:11 -05004755int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4756{
4757 struct nfs_server *server = NFS_SERVER(state->inode);
4758 struct nfs4_exception exception = { };
4759 int err;
4760
4761 err = nfs4_set_lock_state(state, fl);
4762 if (err != 0)
4763 goto out;
4764 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004765 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004766 switch (err) {
4767 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004768 printk(KERN_ERR "NFS: %s: unhandled error "
4769 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004770 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004771 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004772 goto out;
4773 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004774 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004775 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004776 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004777 nfs4_schedule_lease_recovery(server->nfs_client);
4778 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004779 case -NFS4ERR_BADSESSION:
4780 case -NFS4ERR_BADSLOT:
4781 case -NFS4ERR_BAD_HIGH_SLOT:
4782 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4783 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004784 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004785 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004786 case -ERESTARTSYS:
4787 /*
4788 * The show must go on: exit, but mark the
4789 * stateid as needing recovery.
4790 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004791 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004792 case -NFS4ERR_ADMIN_REVOKED:
4793 case -NFS4ERR_BAD_STATEID:
4794 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004795 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004796 err = 0;
4797 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004798 case -EKEYEXPIRED:
4799 /*
4800 * User RPCSEC_GSS context has expired.
4801 * We cannot recover this stateid now, so
4802 * skip it and allow recovery thread to
4803 * proceed.
4804 */
4805 err = 0;
4806 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004807 case -ENOMEM:
4808 case -NFS4ERR_DENIED:
4809 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4810 err = 0;
4811 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004812 case -NFS4ERR_DELAY:
4813 break;
4814 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004815 err = nfs4_handle_exception(server, err, &exception);
4816 } while (exception.retry);
4817out:
4818 return err;
4819}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004820
Trond Myklebustcf470c32012-03-07 13:49:12 -05004821struct nfs_release_lockowner_data {
4822 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004823 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004824 struct nfs_release_lockowner_args args;
4825};
4826
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004827static void nfs4_release_lockowner_release(void *calldata)
4828{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004829 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004830 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004831 kfree(calldata);
4832}
4833
Trond Myklebust17280172012-03-11 13:11:00 -04004834static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004835 .rpc_release = nfs4_release_lockowner_release,
4836};
4837
Trond Myklebustcf470c32012-03-07 13:49:12 -05004838int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004839{
4840 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004841 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004842 struct rpc_message msg = {
4843 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4844 };
4845
4846 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004847 return -EINVAL;
4848 data = kmalloc(sizeof(*data), GFP_NOFS);
4849 if (!data)
4850 return -ENOMEM;
4851 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004852 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004853 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4854 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4855 data->args.lock_owner.s_dev = server->s_dev;
4856 msg.rpc_argp = &data->args;
4857 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4858 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004859}
4860
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004861#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4862
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004863static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4864 const void *buf, size_t buflen,
4865 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004866{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004867 if (strcmp(key, "") != 0)
4868 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004869
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004870 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004871}
4872
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004873static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4874 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004875{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004876 if (strcmp(key, "") != 0)
4877 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004878
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004879 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004880}
4881
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004882static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4883 size_t list_len, const char *name,
4884 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004885{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004886 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004887
J. Bruce Fields096455a2006-03-20 23:23:42 -05004888 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4889 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004890
4891 if (list && len <= list_len)
4892 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004893 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004894}
4895
Andy Adamson533eb462011-06-13 18:25:56 -04004896/*
4897 * nfs_fhget will use either the mounted_on_fileid or the fileid
4898 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004899static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4900{
Andy Adamson533eb462011-06-13 18:25:56 -04004901 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4902 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4903 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05004904 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004905 return;
4906
4907 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05004908 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004909 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4910 fattr->nlink = 2;
4911}
4912
Trond Myklebust56659e92007-07-17 21:52:39 -04004913int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004914 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004915{
4916 struct nfs_server *server = NFS_SERVER(dir);
4917 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004918 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004919 };
4920 struct nfs4_fs_locations_arg args = {
4921 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004922 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004923 .page = page,
4924 .bitmask = bitmask,
4925 };
Benny Halevy22958462009-04-01 09:22:02 -04004926 struct nfs4_fs_locations_res res = {
4927 .fs_locations = fs_locations,
4928 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004929 struct rpc_message msg = {
4930 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4931 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004932 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004933 };
4934 int status;
4935
Harvey Harrison3110ff82008-05-02 13:42:44 -07004936 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004937
4938 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4939 * is not supported */
4940 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4941 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4942 else
4943 bitmask[0] |= FATTR4_WORD0_FILEID;
4944
Trond Myklebustc228fd32007-01-13 02:28:11 -05004945 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004946 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004947 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004948 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004949 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004950 return status;
4951}
4952
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004953static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4954{
4955 int status;
4956 struct nfs4_secinfo_arg args = {
4957 .dir_fh = NFS_FH(dir),
4958 .name = name,
4959 };
4960 struct nfs4_secinfo_res res = {
4961 .flavors = flavors,
4962 };
4963 struct rpc_message msg = {
4964 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4965 .rpc_argp = &args,
4966 .rpc_resp = &res,
4967 };
4968
4969 dprintk("NFS call secinfo %s\n", name->name);
4970 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4971 dprintk("NFS reply secinfo: %d\n", status);
4972 return status;
4973}
4974
Trond Myklebust17280172012-03-11 13:11:00 -04004975static int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
4976 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004977{
4978 struct nfs4_exception exception = { };
4979 int err;
4980 do {
4981 err = nfs4_handle_exception(NFS_SERVER(dir),
4982 _nfs4_proc_secinfo(dir, name, flavors),
4983 &exception);
4984 } while (exception.retry);
4985 return err;
4986}
4987
Andy Adamson557134a2009-04-01 09:21:53 -04004988#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004989/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004990 * Check the exchange flags returned by the server for invalid flags, having
4991 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4992 * DS flags set.
4993 */
4994static int nfs4_check_cl_exchange_flags(u32 flags)
4995{
4996 if (flags & ~EXCHGID4_FLAG_MASK_R)
4997 goto out_inval;
4998 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4999 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5000 goto out_inval;
5001 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5002 goto out_inval;
5003 return NFS_OK;
5004out_inval:
5005 return -NFS4ERR_INVAL;
5006}
5007
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005008static bool
5009nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
5010{
5011 if (a->server_scope_sz == b->server_scope_sz &&
5012 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5013 return true;
5014
5015 return false;
5016}
5017
Andy Adamson357f54d2010-12-14 10:11:57 -05005018/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005019 * nfs4_proc_exchange_id()
5020 *
5021 * Since the clientid has expired, all compounds using sessions
5022 * associated with the stale clientid will be returning
5023 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5024 * be in some phase of session reset.
5025 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005026int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005027{
5028 nfs4_verifier verifier;
5029 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005030 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005031 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005032 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005033 };
5034 struct nfs41_exchange_id_res res = {
5035 .client = clp,
5036 };
5037 int status;
5038 struct rpc_message msg = {
5039 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5040 .rpc_argp = &args,
5041 .rpc_resp = &res,
5042 .rpc_cred = cred,
5043 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005044
5045 dprintk("--> %s\n", __func__);
5046 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04005047
Chuck Levercd937102012-03-02 17:14:31 -05005048 nfs4_construct_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005049
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005050 args.id_len = scnprintf(args.id, sizeof(args.id),
5051 "%s/%s.%s/%u",
5052 clp->cl_ipaddr,
5053 init_utsname()->nodename,
5054 init_utsname()->domainname,
5055 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005056
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005057 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005058 if (unlikely(!res.server_scope)) {
5059 status = -ENOMEM;
5060 goto out;
5061 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005062
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005063 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
5064 if (unlikely(!res.impl_id)) {
5065 status = -ENOMEM;
5066 goto out_server_scope;
5067 }
5068
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005069 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005070 if (!status)
5071 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005072
5073 if (!status) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005074 /* use the most recent implementation id */
5075 kfree(clp->impl_id);
5076 clp->impl_id = res.impl_id;
5077 } else
5078 kfree(res.impl_id);
5079
5080 if (!status) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005081 if (clp->server_scope &&
5082 !nfs41_same_server_scope(clp->server_scope,
5083 res.server_scope)) {
5084 dprintk("%s: server_scope mismatch detected\n",
5085 __func__);
5086 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5087 kfree(clp->server_scope);
5088 clp->server_scope = NULL;
5089 }
5090
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005091 if (!clp->server_scope) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005092 clp->server_scope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005093 goto out;
5094 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005095 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005096
5097out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005098 kfree(res.server_scope);
5099out:
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005100 if (clp->impl_id)
5101 dprintk("%s: Server Implementation ID: "
5102 "domain: %s, name: %s, date: %llu,%u\n",
5103 __func__, clp->impl_id->domain, clp->impl_id->name,
5104 clp->impl_id->date.seconds,
5105 clp->impl_id->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005106 dprintk("<-- %s status= %d\n", __func__, status);
5107 return status;
5108}
5109
Andy Adamson2050f0c2009-04-01 09:22:30 -04005110struct nfs4_get_lease_time_data {
5111 struct nfs4_get_lease_time_args *args;
5112 struct nfs4_get_lease_time_res *res;
5113 struct nfs_client *clp;
5114};
5115
5116static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5117 void *calldata)
5118{
5119 int ret;
5120 struct nfs4_get_lease_time_data *data =
5121 (struct nfs4_get_lease_time_data *)calldata;
5122
5123 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005124 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005125 /* just setup sequence, do not trigger session recovery
5126 since we're invoked within one */
5127 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005128 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005129 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005130
5131 BUG_ON(ret == -EAGAIN);
5132 rpc_call_start(task);
5133 dprintk("<-- %s\n", __func__);
5134}
5135
5136/*
5137 * Called from nfs4_state_manager thread for session setup, so don't recover
5138 * from sequence operation or clientid errors.
5139 */
5140static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5141{
5142 struct nfs4_get_lease_time_data *data =
5143 (struct nfs4_get_lease_time_data *)calldata;
5144
5145 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005146 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5147 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005148 switch (task->tk_status) {
5149 case -NFS4ERR_DELAY:
5150 case -NFS4ERR_GRACE:
5151 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5152 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5153 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005154 /* fall through */
5155 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005156 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005157 return;
5158 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005159 dprintk("<-- %s\n", __func__);
5160}
5161
Trond Myklebust17280172012-03-11 13:11:00 -04005162static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005163 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5164 .rpc_call_done = nfs4_get_lease_time_done,
5165};
5166
5167int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5168{
5169 struct rpc_task *task;
5170 struct nfs4_get_lease_time_args args;
5171 struct nfs4_get_lease_time_res res = {
5172 .lr_fsinfo = fsinfo,
5173 };
5174 struct nfs4_get_lease_time_data data = {
5175 .args = &args,
5176 .res = &res,
5177 .clp = clp,
5178 };
5179 struct rpc_message msg = {
5180 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5181 .rpc_argp = &args,
5182 .rpc_resp = &res,
5183 };
5184 struct rpc_task_setup task_setup = {
5185 .rpc_client = clp->cl_rpcclient,
5186 .rpc_message = &msg,
5187 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005188 .callback_data = &data,
5189 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005190 };
5191 int status;
5192
Trond Myklebust9d12b212012-01-17 22:04:25 -05005193 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005194 dprintk("--> %s\n", __func__);
5195 task = rpc_run_task(&task_setup);
5196
5197 if (IS_ERR(task))
5198 status = PTR_ERR(task);
5199 else {
5200 status = task->tk_status;
5201 rpc_put_task(task);
5202 }
5203 dprintk("<-- %s return %d\n", __func__, status);
5204
5205 return status;
5206}
5207
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005208static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5209{
5210 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5211}
5212
5213static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5214 struct nfs4_slot *new,
5215 u32 max_slots,
5216 u32 ivalue)
5217{
5218 struct nfs4_slot *old = NULL;
5219 u32 i;
5220
5221 spin_lock(&tbl->slot_tbl_lock);
5222 if (new) {
5223 old = tbl->slots;
5224 tbl->slots = new;
5225 tbl->max_slots = max_slots;
5226 }
5227 tbl->highest_used_slotid = -1; /* no slot is currently used */
5228 for (i = 0; i < tbl->max_slots; i++)
5229 tbl->slots[i].seq_nr = ivalue;
5230 spin_unlock(&tbl->slot_tbl_lock);
5231 kfree(old);
5232}
5233
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005234/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005235 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005236 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005237static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5238 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005239{
Andy Adamson104aeba2010-01-14 17:45:10 -05005240 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005241 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005242
Andy Adamson104aeba2010-01-14 17:45:10 -05005243 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5244 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005245
Andy Adamson104aeba2010-01-14 17:45:10 -05005246 /* Does the newly negotiated max_reqs match the existing slot table? */
5247 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005248 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005249 if (!new)
5250 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005251 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005252 ret = 0;
5253
5254 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005255 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5256 tbl, tbl->slots, tbl->max_slots);
5257out:
5258 dprintk("<-- %s: return %d\n", __func__, ret);
5259 return ret;
5260}
5261
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005262/* Destroy the slot table */
5263static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5264{
5265 if (session->fc_slot_table.slots != NULL) {
5266 kfree(session->fc_slot_table.slots);
5267 session->fc_slot_table.slots = NULL;
5268 }
5269 if (session->bc_slot_table.slots != NULL) {
5270 kfree(session->bc_slot_table.slots);
5271 session->bc_slot_table.slots = NULL;
5272 }
5273 return;
5274}
5275
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005276/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005277 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005278 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005279static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005280{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005281 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005282 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005283
Andy Adamsonaacd5532011-11-09 13:58:21 -05005284 dprintk("--> %s\n", __func__);
5285 /* Fore channel */
5286 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005287 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5288 if (status) /* -ENOMEM */
5289 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005290 /* Back channel */
5291 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005292 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5293 if (status && tbl->slots == NULL)
5294 /* Fore and back channel share a connection so get
5295 * both slot tables or neither */
5296 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005297 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005298}
5299
5300struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5301{
5302 struct nfs4_session *session;
5303 struct nfs4_slot_table *tbl;
5304
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005305 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005306 if (!session)
5307 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005308
Andy Adamson557134a2009-04-01 09:21:53 -04005309 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005310 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005311 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005312 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005313 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005314
5315 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005316 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005317 spin_lock_init(&tbl->slot_tbl_lock);
5318 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005319 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005320
Trond Myklebust1055d762010-06-16 09:52:27 -04005321 session->session_state = 1<<NFS4_SESSION_INITING;
5322
Andy Adamson557134a2009-04-01 09:21:53 -04005323 session->clp = clp;
5324 return session;
5325}
5326
5327void nfs4_destroy_session(struct nfs4_session *session)
5328{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005329 struct rpc_xprt *xprt;
5330
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005331 nfs4_proc_destroy_session(session);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005332
5333 rcu_read_lock();
5334 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5335 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005336 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005337 __func__, xprt);
5338 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005339 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005340 kfree(session);
5341}
5342
Andy Adamsonfc931582009-04-01 09:22:31 -04005343/*
5344 * Initialize the values to be used by the client in CREATE_SESSION
5345 * If nfs4_init_session set the fore channel request and response sizes,
5346 * use them.
5347 *
5348 * Set the back channel max_resp_sz_cached to zero to force the client to
5349 * always set csa_cachethis to FALSE because the current implementation
5350 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5351 */
5352static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5353{
5354 struct nfs4_session *session = args->client->cl_session;
5355 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5356 mxresp_sz = session->fc_attrs.max_resp_sz;
5357
5358 if (mxrqst_sz == 0)
5359 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5360 if (mxresp_sz == 0)
5361 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5362 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005363 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5364 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005365 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005366 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005367
5368 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005369 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005370 __func__,
5371 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005372 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005373
5374 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005375 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5376 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5377 args->bc_attrs.max_resp_sz_cached = 0;
5378 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5379 args->bc_attrs.max_reqs = 1;
5380
5381 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5382 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5383 __func__,
5384 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5385 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5386 args->bc_attrs.max_reqs);
5387}
5388
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005389static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005390{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005391 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5392 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5393
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005394 if (rcvd->max_resp_sz > sent->max_resp_sz)
5395 return -EINVAL;
5396 /*
5397 * Our requested max_ops is the minimum we need; we're not
5398 * prepared to break up compounds into smaller pieces than that.
5399 * So, no point even trying to continue if the server won't
5400 * cooperate:
5401 */
5402 if (rcvd->max_ops < sent->max_ops)
5403 return -EINVAL;
5404 if (rcvd->max_reqs == 0)
5405 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005406 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5407 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005408 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005409}
5410
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005411static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5412{
5413 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5414 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005415
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005416 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5417 return -EINVAL;
5418 if (rcvd->max_resp_sz < sent->max_resp_sz)
5419 return -EINVAL;
5420 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5421 return -EINVAL;
5422 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005423 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005424 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005425 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005426 return -EINVAL;
5427 return 0;
5428}
Andy Adamson8d353012009-04-01 09:22:32 -04005429
Andy Adamson8d353012009-04-01 09:22:32 -04005430static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5431 struct nfs4_session *session)
5432{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005433 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005434
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005435 ret = nfs4_verify_fore_channel_attrs(args, session);
5436 if (ret)
5437 return ret;
5438 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005439}
5440
Andy Adamsonfc931582009-04-01 09:22:31 -04005441static int _nfs4_proc_create_session(struct nfs_client *clp)
5442{
5443 struct nfs4_session *session = clp->cl_session;
5444 struct nfs41_create_session_args args = {
5445 .client = clp,
5446 .cb_program = NFS4_CALLBACK,
5447 };
5448 struct nfs41_create_session_res res = {
5449 .client = clp,
5450 };
5451 struct rpc_message msg = {
5452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5453 .rpc_argp = &args,
5454 .rpc_resp = &res,
5455 };
5456 int status;
5457
5458 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005459 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005460
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005461 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005462
Andy Adamson8d353012009-04-01 09:22:32 -04005463 if (!status)
5464 /* Verify the session's negotiated channel_attrs values */
5465 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005466 if (!status) {
5467 /* Increment the clientid slot sequence id */
5468 clp->cl_seqid++;
5469 }
5470
5471 return status;
5472}
5473
5474/*
5475 * Issues a CREATE_SESSION operation to the server.
5476 * It is the responsibility of the caller to verify the session is
5477 * expired before calling this routine.
5478 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005479int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005480{
5481 int status;
5482 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005483 struct nfs4_session *session = clp->cl_session;
5484
5485 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5486
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005487 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005488 if (status)
5489 goto out;
5490
Andy Adamsonaacd5532011-11-09 13:58:21 -05005491 /* Init or reset the session slot tables */
5492 status = nfs4_setup_session_slot_tables(session);
5493 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005494 if (status)
5495 goto out;
5496
5497 ptr = (unsigned *)&session->sess_id.data[0];
5498 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5499 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005500out:
5501 dprintk("<-- %s\n", __func__);
5502 return status;
5503}
5504
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005505/*
5506 * Issue the over-the-wire RPC DESTROY_SESSION.
5507 * The caller must serialize access to this routine.
5508 */
5509int nfs4_proc_destroy_session(struct nfs4_session *session)
5510{
5511 int status = 0;
5512 struct rpc_message msg;
5513
5514 dprintk("--> nfs4_proc_destroy_session\n");
5515
5516 /* session is still being setup */
5517 if (session->clp->cl_cons_state != NFS_CS_READY)
5518 return status;
5519
5520 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5521 msg.rpc_argp = session;
5522 msg.rpc_resp = NULL;
5523 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005524 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005525
5526 if (status)
5527 printk(KERN_WARNING
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -05005528 "NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005529 "Session has been destroyed regardless...\n", status);
5530
5531 dprintk("<-- nfs4_proc_destroy_session\n");
5532 return status;
5533}
5534
Trond Myklebustfccba802009-07-21 16:48:07 -04005535int nfs4_init_session(struct nfs_server *server)
5536{
5537 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005538 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005539 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005540 int ret;
5541
5542 if (!nfs4_has_session(clp))
5543 return 0;
5544
Trond Myklebust1055d762010-06-16 09:52:27 -04005545 session = clp->cl_session;
5546 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5547 return 0;
5548
Andy Adamson68bf05e2009-12-15 12:55:02 -05005549 rsize = server->rsize;
5550 if (rsize == 0)
5551 rsize = NFS_MAX_FILE_IO_SIZE;
5552 wsize = server->wsize;
5553 if (wsize == 0)
5554 wsize = NFS_MAX_FILE_IO_SIZE;
5555
Andy Adamson68bf05e2009-12-15 12:55:02 -05005556 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5557 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005558
Trond Myklebustfccba802009-07-21 16:48:07 -04005559 ret = nfs4_recover_expired_lease(server);
5560 if (!ret)
5561 ret = nfs4_check_client_ready(clp);
5562 return ret;
5563}
5564
Andy Adamsond83217c2011-03-01 01:34:17 +00005565int nfs4_init_ds_session(struct nfs_client *clp)
5566{
5567 struct nfs4_session *session = clp->cl_session;
5568 int ret;
5569
5570 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5571 return 0;
5572
5573 ret = nfs4_client_recover_expired_lease(clp);
5574 if (!ret)
5575 /* Test for the DS role */
5576 if (!is_ds_client(clp))
5577 ret = -ENODEV;
5578 if (!ret)
5579 ret = nfs4_check_client_ready(clp);
5580 return ret;
5581
5582}
5583EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5584
5585
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005586/*
5587 * Renew the cl_session lease.
5588 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005589struct nfs4_sequence_data {
5590 struct nfs_client *clp;
5591 struct nfs4_sequence_args args;
5592 struct nfs4_sequence_res res;
5593};
5594
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005595static void nfs41_sequence_release(void *data)
5596{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005597 struct nfs4_sequence_data *calldata = data;
5598 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005599
Alexandros Batsakis71358402010-02-05 03:45:05 -08005600 if (atomic_read(&clp->cl_count) > 1)
5601 nfs4_schedule_state_renewal(clp);
5602 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005603 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005604}
5605
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005606static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5607{
5608 switch(task->tk_status) {
5609 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005610 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5611 return -EAGAIN;
5612 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005613 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005614 }
5615 return 0;
5616}
5617
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005618static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005619{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005620 struct nfs4_sequence_data *calldata = data;
5621 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005622
Trond Myklebust14516c32010-07-31 14:29:06 -04005623 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5624 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005625
5626 if (task->tk_status < 0) {
5627 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005628 if (atomic_read(&clp->cl_count) == 1)
5629 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005630
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005631 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5632 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005633 return;
5634 }
5635 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005636 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005637out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005638 dprintk("<-- %s\n", __func__);
5639}
5640
5641static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5642{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005643 struct nfs4_sequence_data *calldata = data;
5644 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005645 struct nfs4_sequence_args *args;
5646 struct nfs4_sequence_res *res;
5647
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005648 args = task->tk_msg.rpc_argp;
5649 res = task->tk_msg.rpc_resp;
5650
Trond Myklebust9d12b212012-01-17 22:04:25 -05005651 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005652 return;
5653 rpc_call_start(task);
5654}
5655
5656static const struct rpc_call_ops nfs41_sequence_ops = {
5657 .rpc_call_done = nfs41_sequence_call_done,
5658 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005659 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005660};
5661
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005662static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005663{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005664 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005665 struct rpc_message msg = {
5666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5667 .rpc_cred = cred,
5668 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005669 struct rpc_task_setup task_setup_data = {
5670 .rpc_client = clp->cl_rpcclient,
5671 .rpc_message = &msg,
5672 .callback_ops = &nfs41_sequence_ops,
5673 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5674 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005675
Alexandros Batsakis71358402010-02-05 03:45:05 -08005676 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005677 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005678 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005679 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005680 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005681 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005682 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005683 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005684 msg.rpc_argp = &calldata->args;
5685 msg.rpc_resp = &calldata->res;
5686 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005687 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005688
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005689 return rpc_run_task(&task_setup_data);
5690}
5691
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005692static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005693{
5694 struct rpc_task *task;
5695 int ret = 0;
5696
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005697 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5698 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005699 task = _nfs41_proc_sequence(clp, cred);
5700 if (IS_ERR(task))
5701 ret = PTR_ERR(task);
5702 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005703 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005704 dprintk("<-- %s status=%d\n", __func__, ret);
5705 return ret;
5706}
5707
5708static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5709{
5710 struct rpc_task *task;
5711 int ret;
5712
5713 task = _nfs41_proc_sequence(clp, cred);
5714 if (IS_ERR(task)) {
5715 ret = PTR_ERR(task);
5716 goto out;
5717 }
5718 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005719 if (!ret) {
5720 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5721
5722 if (task->tk_status == 0)
5723 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005724 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005725 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005726 rpc_put_task(task);
5727out:
5728 dprintk("<-- %s status=%d\n", __func__, ret);
5729 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005730}
5731
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005732struct nfs4_reclaim_complete_data {
5733 struct nfs_client *clp;
5734 struct nfs41_reclaim_complete_args arg;
5735 struct nfs41_reclaim_complete_res res;
5736};
5737
5738static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5739{
5740 struct nfs4_reclaim_complete_data *calldata = data;
5741
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005742 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005743 if (nfs41_setup_sequence(calldata->clp->cl_session,
5744 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005745 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005746 return;
5747
5748 rpc_call_start(task);
5749}
5750
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005751static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5752{
5753 switch(task->tk_status) {
5754 case 0:
5755 case -NFS4ERR_COMPLETE_ALREADY:
5756 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5757 break;
5758 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005759 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005760 /* fall through */
5761 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005762 return -EAGAIN;
5763 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005764 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005765 }
5766 return 0;
5767}
5768
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005769static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5770{
5771 struct nfs4_reclaim_complete_data *calldata = data;
5772 struct nfs_client *clp = calldata->clp;
5773 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5774
5775 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005776 if (!nfs41_sequence_done(task, res))
5777 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005778
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005779 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5780 rpc_restart_call_prepare(task);
5781 return;
5782 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005783 dprintk("<-- %s\n", __func__);
5784}
5785
5786static void nfs4_free_reclaim_complete_data(void *data)
5787{
5788 struct nfs4_reclaim_complete_data *calldata = data;
5789
5790 kfree(calldata);
5791}
5792
5793static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5794 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5795 .rpc_call_done = nfs4_reclaim_complete_done,
5796 .rpc_release = nfs4_free_reclaim_complete_data,
5797};
5798
5799/*
5800 * Issue a global reclaim complete.
5801 */
5802static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5803{
5804 struct nfs4_reclaim_complete_data *calldata;
5805 struct rpc_task *task;
5806 struct rpc_message msg = {
5807 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5808 };
5809 struct rpc_task_setup task_setup_data = {
5810 .rpc_client = clp->cl_rpcclient,
5811 .rpc_message = &msg,
5812 .callback_ops = &nfs4_reclaim_complete_call_ops,
5813 .flags = RPC_TASK_ASYNC,
5814 };
5815 int status = -ENOMEM;
5816
5817 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005818 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005819 if (calldata == NULL)
5820 goto out;
5821 calldata->clp = clp;
5822 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005823
Trond Myklebust9d12b212012-01-17 22:04:25 -05005824 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005825 msg.rpc_argp = &calldata->arg;
5826 msg.rpc_resp = &calldata->res;
5827 task_setup_data.callback_data = calldata;
5828 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005829 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005830 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005831 goto out;
5832 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005833 status = nfs4_wait_for_completion_rpc_task(task);
5834 if (status == 0)
5835 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005836 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005837 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005838out:
5839 dprintk("<-- %s status=%d\n", __func__, status);
5840 return status;
5841}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005842
5843static void
5844nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5845{
5846 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005847 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005848
5849 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005850 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5851 * right now covering the LAYOUTGET we are about to send.
5852 * However, that is not so catastrophic, and there seems
5853 * to be no way to prevent it completely.
5854 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005855 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005856 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005857 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005858 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5859 NFS_I(lgp->args.inode)->layout,
5860 lgp->args.ctx->state)) {
5861 rpc_exit(task, NFS4_OK);
5862 return;
5863 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005864 rpc_call_start(task);
5865}
5866
5867static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5868{
5869 struct nfs4_layoutget *lgp = calldata;
5870 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5871
5872 dprintk("--> %s\n", __func__);
5873
5874 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5875 return;
5876
5877 switch (task->tk_status) {
5878 case 0:
5879 break;
5880 case -NFS4ERR_LAYOUTTRYLATER:
5881 case -NFS4ERR_RECALLCONFLICT:
5882 task->tk_status = -NFS4ERR_DELAY;
5883 /* Fall through */
5884 default:
5885 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5886 rpc_restart_call_prepare(task);
5887 return;
5888 }
5889 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005890 dprintk("<-- %s\n", __func__);
5891}
5892
5893static void nfs4_layoutget_release(void *calldata)
5894{
5895 struct nfs4_layoutget *lgp = calldata;
5896
5897 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005898 put_nfs_open_context(lgp->args.ctx);
5899 kfree(calldata);
5900 dprintk("<-- %s\n", __func__);
5901}
5902
5903static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5904 .rpc_call_prepare = nfs4_layoutget_prepare,
5905 .rpc_call_done = nfs4_layoutget_done,
5906 .rpc_release = nfs4_layoutget_release,
5907};
5908
5909int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5910{
5911 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5912 struct rpc_task *task;
5913 struct rpc_message msg = {
5914 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5915 .rpc_argp = &lgp->args,
5916 .rpc_resp = &lgp->res,
5917 };
5918 struct rpc_task_setup task_setup_data = {
5919 .rpc_client = server->client,
5920 .rpc_message = &msg,
5921 .callback_ops = &nfs4_layoutget_call_ops,
5922 .callback_data = lgp,
5923 .flags = RPC_TASK_ASYNC,
5924 };
5925 int status = 0;
5926
5927 dprintk("--> %s\n", __func__);
5928
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005929 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005930 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05005931 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005932 task = rpc_run_task(&task_setup_data);
5933 if (IS_ERR(task))
5934 return PTR_ERR(task);
5935 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005936 if (status == 0)
5937 status = task->tk_status;
5938 if (status == 0)
5939 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005940 rpc_put_task(task);
5941 dprintk("<-- %s status=%d\n", __func__, status);
5942 return status;
5943}
5944
Benny Halevycbe82602011-05-22 19:52:37 +03005945static void
5946nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5947{
5948 struct nfs4_layoutreturn *lrp = calldata;
5949
5950 dprintk("--> %s\n", __func__);
5951 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005952 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03005953 return;
5954 rpc_call_start(task);
5955}
5956
5957static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5958{
5959 struct nfs4_layoutreturn *lrp = calldata;
5960 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005961 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005962
5963 dprintk("--> %s\n", __func__);
5964
5965 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5966 return;
5967
5968 server = NFS_SERVER(lrp->args.inode);
5969 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005970 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03005971 return;
5972 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005973 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005974 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005975 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005976 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005977 } else
5978 BUG_ON(!list_empty(&lo->plh_segs));
5979 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005980 lo->plh_block_lgets--;
5981 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005982 dprintk("<-- %s\n", __func__);
5983}
5984
5985static void nfs4_layoutreturn_release(void *calldata)
5986{
5987 struct nfs4_layoutreturn *lrp = calldata;
5988
5989 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005990 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005991 kfree(calldata);
5992 dprintk("<-- %s\n", __func__);
5993}
5994
5995static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5996 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5997 .rpc_call_done = nfs4_layoutreturn_done,
5998 .rpc_release = nfs4_layoutreturn_release,
5999};
6000
6001int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6002{
6003 struct rpc_task *task;
6004 struct rpc_message msg = {
6005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6006 .rpc_argp = &lrp->args,
6007 .rpc_resp = &lrp->res,
6008 };
6009 struct rpc_task_setup task_setup_data = {
6010 .rpc_client = lrp->clp->cl_rpcclient,
6011 .rpc_message = &msg,
6012 .callback_ops = &nfs4_layoutreturn_call_ops,
6013 .callback_data = lrp,
6014 };
6015 int status;
6016
6017 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006018 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006019 task = rpc_run_task(&task_setup_data);
6020 if (IS_ERR(task))
6021 return PTR_ERR(task);
6022 status = task->tk_status;
6023 dprintk("<-- %s status=%d\n", __func__, status);
6024 rpc_put_task(task);
6025 return status;
6026}
6027
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006028/*
6029 * Retrieve the list of Data Server devices from the MDS.
6030 */
6031static int _nfs4_getdevicelist(struct nfs_server *server,
6032 const struct nfs_fh *fh,
6033 struct pnfs_devicelist *devlist)
6034{
6035 struct nfs4_getdevicelist_args args = {
6036 .fh = fh,
6037 .layoutclass = server->pnfs_curr_ld->id,
6038 };
6039 struct nfs4_getdevicelist_res res = {
6040 .devlist = devlist,
6041 };
6042 struct rpc_message msg = {
6043 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6044 .rpc_argp = &args,
6045 .rpc_resp = &res,
6046 };
6047 int status;
6048
6049 dprintk("--> %s\n", __func__);
6050 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6051 &res.seq_res, 0);
6052 dprintk("<-- %s status=%d\n", __func__, status);
6053 return status;
6054}
6055
6056int nfs4_proc_getdevicelist(struct nfs_server *server,
6057 const struct nfs_fh *fh,
6058 struct pnfs_devicelist *devlist)
6059{
6060 struct nfs4_exception exception = { };
6061 int err;
6062
6063 do {
6064 err = nfs4_handle_exception(server,
6065 _nfs4_getdevicelist(server, fh, devlist),
6066 &exception);
6067 } while (exception.retry);
6068
6069 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6070 err, devlist->num_devs);
6071
6072 return err;
6073}
6074EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6075
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006076static int
6077_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6078{
6079 struct nfs4_getdeviceinfo_args args = {
6080 .pdev = pdev,
6081 };
6082 struct nfs4_getdeviceinfo_res res = {
6083 .pdev = pdev,
6084 };
6085 struct rpc_message msg = {
6086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6087 .rpc_argp = &args,
6088 .rpc_resp = &res,
6089 };
6090 int status;
6091
6092 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006093 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006094 dprintk("<-- %s status=%d\n", __func__, status);
6095
6096 return status;
6097}
6098
6099int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6100{
6101 struct nfs4_exception exception = { };
6102 int err;
6103
6104 do {
6105 err = nfs4_handle_exception(server,
6106 _nfs4_proc_getdeviceinfo(server, pdev),
6107 &exception);
6108 } while (exception.retry);
6109 return err;
6110}
6111EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6112
Andy Adamson863a3c62011-03-23 13:27:54 +00006113static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6114{
6115 struct nfs4_layoutcommit_data *data = calldata;
6116 struct nfs_server *server = NFS_SERVER(data->args.inode);
6117
6118 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006119 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006120 return;
6121 rpc_call_start(task);
6122}
6123
6124static void
6125nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6126{
6127 struct nfs4_layoutcommit_data *data = calldata;
6128 struct nfs_server *server = NFS_SERVER(data->args.inode);
6129
6130 if (!nfs4_sequence_done(task, &data->res.seq_res))
6131 return;
6132
6133 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006134 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6135 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6136 case -NFS4ERR_BADLAYOUT: /* no layout */
6137 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006138 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006139 break;
6140 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006141 nfs_post_op_update_inode_force_wcc(data->args.inode,
6142 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006143 break;
6144 default:
6145 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6146 rpc_restart_call_prepare(task);
6147 return;
6148 }
6149 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006150}
6151
6152static void nfs4_layoutcommit_release(void *calldata)
6153{
6154 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006155 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006156 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006157
Andy Adamsondb29c082011-07-30 20:52:38 -04006158 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006159 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006160 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6161 list_del_init(&lseg->pls_lc_list);
6162 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6163 &lseg->pls_flags))
6164 put_lseg(lseg);
6165 }
Peng Tao92407e72011-10-23 20:21:17 -07006166
6167 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6168 smp_mb__after_clear_bit();
6169 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6170
Andy Adamson863a3c62011-03-23 13:27:54 +00006171 put_rpccred(data->cred);
6172 kfree(data);
6173}
6174
6175static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6176 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6177 .rpc_call_done = nfs4_layoutcommit_done,
6178 .rpc_release = nfs4_layoutcommit_release,
6179};
6180
6181int
Andy Adamsonef311532011-03-12 02:58:10 -05006182nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006183{
6184 struct rpc_message msg = {
6185 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6186 .rpc_argp = &data->args,
6187 .rpc_resp = &data->res,
6188 .rpc_cred = data->cred,
6189 };
6190 struct rpc_task_setup task_setup_data = {
6191 .task = &data->task,
6192 .rpc_client = NFS_CLIENT(data->args.inode),
6193 .rpc_message = &msg,
6194 .callback_ops = &nfs4_layoutcommit_ops,
6195 .callback_data = data,
6196 .flags = RPC_TASK_ASYNC,
6197 };
6198 struct rpc_task *task;
6199 int status = 0;
6200
6201 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6202 "lbw: %llu inode %lu\n",
6203 data->task.tk_pid, sync,
6204 data->args.lastbytewritten,
6205 data->args.inode->i_ino);
6206
Trond Myklebust9d12b212012-01-17 22:04:25 -05006207 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006208 task = rpc_run_task(&task_setup_data);
6209 if (IS_ERR(task))
6210 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006211 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006212 goto out;
6213 status = nfs4_wait_for_completion_rpc_task(task);
6214 if (status != 0)
6215 goto out;
6216 status = task->tk_status;
6217out:
6218 dprintk("%s: status %d\n", __func__, status);
6219 rpc_put_task(task);
6220 return status;
6221}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006222
6223static int
6224_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6225 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6226{
6227 struct nfs41_secinfo_no_name_args args = {
6228 .style = SECINFO_STYLE_CURRENT_FH,
6229 };
6230 struct nfs4_secinfo_res res = {
6231 .flavors = flavors,
6232 };
6233 struct rpc_message msg = {
6234 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6235 .rpc_argp = &args,
6236 .rpc_resp = &res,
6237 };
6238 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6239}
6240
6241static int
6242nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6243 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6244{
6245 struct nfs4_exception exception = { };
6246 int err;
6247 do {
6248 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6249 switch (err) {
6250 case 0:
6251 case -NFS4ERR_WRONGSEC:
6252 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006253 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006254 default:
6255 err = nfs4_handle_exception(server, err, &exception);
6256 }
6257 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006258out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006259 return err;
6260}
6261
6262static int
6263nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6264 struct nfs_fsinfo *info)
6265{
6266 int err;
6267 struct page *page;
6268 rpc_authflavor_t flavor;
6269 struct nfs4_secinfo_flavors *flavors;
6270
6271 page = alloc_page(GFP_KERNEL);
6272 if (!page) {
6273 err = -ENOMEM;
6274 goto out;
6275 }
6276
6277 flavors = page_address(page);
6278 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6279
6280 /*
6281 * Fall back on "guess and check" method if
6282 * the server doesn't support SECINFO_NO_NAME
6283 */
6284 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6285 err = nfs4_find_root_sec(server, fhandle, info);
6286 goto out_freepage;
6287 }
6288 if (err)
6289 goto out_freepage;
6290
6291 flavor = nfs_find_best_sec(flavors);
6292 if (err == 0)
6293 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6294
6295out_freepage:
6296 put_page(page);
6297 if (err == -EACCES)
6298 return -EPERM;
6299out:
6300 return err;
6301}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006302
6303static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006304{
6305 int status;
6306 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006307 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006308 };
6309 struct nfs41_test_stateid_res res;
6310 struct rpc_message msg = {
6311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6312 .rpc_argp = &args,
6313 .rpc_resp = &res,
6314 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006315
Trond Myklebust9d12b212012-01-17 22:04:25 -05006316 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006317 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6318
6319 if (status == NFS_OK)
6320 return res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006321 return status;
6322}
6323
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006324static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006325{
6326 struct nfs4_exception exception = { };
6327 int err;
6328 do {
6329 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006330 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006331 &exception);
6332 } while (exception.retry);
6333 return err;
6334}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006335
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006336static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006337{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006338 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006339 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006340 };
6341 struct nfs41_free_stateid_res res;
6342 struct rpc_message msg = {
6343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6344 .rpc_argp = &args,
6345 .rpc_resp = &res,
6346 };
6347
Trond Myklebust9d12b212012-01-17 22:04:25 -05006348 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6349 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006350}
6351
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006352static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006353{
6354 struct nfs4_exception exception = { };
6355 int err;
6356 do {
6357 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006358 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006359 &exception);
6360 } while (exception.retry);
6361 return err;
6362}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006363
6364static bool nfs41_match_stateid(const nfs4_stateid *s1,
6365 const nfs4_stateid *s2)
6366{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006367 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006368 return false;
6369
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006370 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006371 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006372 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006373 return true;
6374
6375 return false;
6376}
6377
Andy Adamson557134a2009-04-01 09:21:53 -04006378#endif /* CONFIG_NFS_V4_1 */
6379
Trond Myklebust36281ca2012-03-04 18:13:56 -05006380static bool nfs4_match_stateid(const nfs4_stateid *s1,
6381 const nfs4_stateid *s2)
6382{
Trond Myklebustf597c532012-03-04 18:13:56 -05006383 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006384}
6385
6386
Trond Myklebust17280172012-03-11 13:11:00 -04006387static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006388 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006389 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390 .recover_open = nfs4_open_reclaim,
6391 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006392 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006393 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394};
6395
Andy Adamson591d71c2009-04-01 09:22:47 -04006396#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006397static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006398 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6399 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6400 .recover_open = nfs4_open_reclaim,
6401 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006402 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006403 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006404 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006405};
6406#endif /* CONFIG_NFS_V4_1 */
6407
Trond Myklebust17280172012-03-11 13:11:00 -04006408static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006409 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006410 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411 .recover_open = nfs4_open_expired,
6412 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006413 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006414 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415};
6416
Andy Adamson591d71c2009-04-01 09:22:47 -04006417#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006418static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006419 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6420 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006421 .recover_open = nfs41_open_expired,
6422 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006423 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006424 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006425};
6426#endif /* CONFIG_NFS_V4_1 */
6427
Trond Myklebust17280172012-03-11 13:11:00 -04006428static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006429 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006430 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006431 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006432};
6433
6434#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006435static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006436 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006437 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006438 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006439};
6440#endif
6441
Trond Myklebust97dc1352010-06-16 09:52:26 -04006442static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6443 .minor_version = 0,
6444 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006445 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006446 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006447 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6448 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6449 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006450};
6451
6452#if defined(CONFIG_NFS_V4_1)
6453static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6454 .minor_version = 1,
6455 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006456 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006457 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006458 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6459 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6460 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006461};
6462#endif
6463
Trond Myklebust97dc1352010-06-16 09:52:26 -04006464const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6465 [0] = &nfs_v4_0_minor_ops,
6466#if defined(CONFIG_NFS_V4_1)
6467 [1] = &nfs_v4_1_minor_ops,
6468#endif
6469};
6470
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006471static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006472 .permission = nfs_permission,
6473 .getattr = nfs_getattr,
6474 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006475 .getxattr = generic_getxattr,
6476 .setxattr = generic_setxattr,
6477 .listxattr = generic_listxattr,
6478 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006479};
6480
David Howells509de812006-08-22 20:06:11 -04006481const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482 .version = 4, /* protocol version */
6483 .dentry_ops = &nfs4_dentry_operations,
6484 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006485 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006486 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487 .getroot = nfs4_proc_get_root,
6488 .getattr = nfs4_proc_getattr,
6489 .setattr = nfs4_proc_setattr,
6490 .lookup = nfs4_proc_lookup,
6491 .access = nfs4_proc_access,
6492 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493 .create = nfs4_proc_create,
6494 .remove = nfs4_proc_remove,
6495 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006496 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497 .unlink_done = nfs4_proc_unlink_done,
6498 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006499 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006500 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006501 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502 .link = nfs4_proc_link,
6503 .symlink = nfs4_proc_symlink,
6504 .mkdir = nfs4_proc_mkdir,
6505 .rmdir = nfs4_proc_remove,
6506 .readdir = nfs4_proc_readdir,
6507 .mknod = nfs4_proc_mknod,
6508 .statfs = nfs4_proc_statfs,
6509 .fsinfo = nfs4_proc_fsinfo,
6510 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006511 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512 .decode_dirent = nfs4_decode_dirent,
6513 .read_setup = nfs4_proc_read_setup,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006514 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006515 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516 .write_setup = nfs4_proc_write_setup,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006517 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006518 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006520 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006522 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006523 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006524 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006525 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006526 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527};
6528
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006529static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6530 .prefix = XATTR_NAME_NFSV4_ACL,
6531 .list = nfs4_xattr_list_nfs4_acl,
6532 .get = nfs4_xattr_get_nfs4_acl,
6533 .set = nfs4_xattr_set_nfs4_acl,
6534};
6535
6536const struct xattr_handler *nfs4_xattr_handlers[] = {
6537 &nfs4_xattr_nfs4_acl_handler,
6538 NULL
6539};
6540
Trond Myklebustef159e92012-02-06 19:50:40 -05006541module_param(max_session_slots, ushort, 0644);
6542MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6543 "requests the client will negotiate");
6544
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545/*
6546 * Local variables:
6547 * c-basic-offset: 8
6548 * End:
6549 */