blob: f01c3d1b54b7b196a0c47360d6c220f408c075b1 [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;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100791 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400792 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100793 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400794 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100795 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100796 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400797 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100798 int rpc_status;
799 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100800};
801
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400802
803static void nfs4_init_opendata_res(struct nfs4_opendata *p)
804{
805 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400806 p->o_res.seqid = p->o_arg.seqid;
807 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400808 p->o_res.server = p->o_arg.server;
809 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500810 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400811}
812
Al Viro82a2c1b2011-06-22 18:30:55 -0400813static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500814 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400815 const struct iattr *attrs,
816 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100817{
Al Viro82a2c1b2011-06-22 18:30:55 -0400818 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100819 struct inode *dir = parent->d_inode;
820 struct nfs_server *server = NFS_SERVER(dir);
821 struct nfs4_opendata *p;
822
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400823 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100824 if (p == NULL)
825 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400826 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100827 if (p->o_arg.seqid == NULL)
828 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400829 nfs_sb_active(dentry->d_sb);
830 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100831 p->dir = parent;
832 p->owner = sp;
833 atomic_inc(&sp->so_count);
834 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500835 p->o_arg.open_flags = flags;
836 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400837 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400838 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
839 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400840 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100841 p->o_arg.server = server;
842 p->o_arg.bitmask = server->attr_bitmask;
843 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500844 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500845 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400846
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100847 p->o_arg.u.attrs = &p->attrs;
848 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500849
850 verf[0] = jiffies;
851 verf[1] = current->pid;
852 memcpy(p->o_arg.u.verifier.data, verf,
853 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100854 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100855 p->c_arg.fh = &p->o_res.fh;
856 p->c_arg.stateid = &p->o_res.stateid;
857 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400858 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400859 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100860 return p;
861err_free:
862 kfree(p);
863err:
864 dput(parent);
865 return NULL;
866}
867
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400868static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100869{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400870 struct nfs4_opendata *p = container_of(kref,
871 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400872 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400873
874 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400875 if (p->state != NULL)
876 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400877 nfs4_put_state_owner(p->owner);
878 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400879 dput(p->dentry);
880 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500881 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400882 kfree(p);
883}
884
885static void nfs4_opendata_put(struct nfs4_opendata *p)
886{
887 if (p != NULL)
888 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100889}
890
Trond Myklebust06f814a2006-01-03 09:55:07 +0100891static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
892{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100893 int ret;
894
Trond Myklebust06f814a2006-01-03 09:55:07 +0100895 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100896 return ret;
897}
898
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500899static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400900{
901 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500902
Trond Myklebust536e43d2012-01-17 22:04:26 -0500903 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500904 goto out;
905 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400906 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500907 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
908 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400909 break;
910 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500911 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
912 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400913 break;
914 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500915 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
916 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400917 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500918out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400919 return ret;
920}
921
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400923{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500924 if (delegation == NULL)
925 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500926 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400927 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500928 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400929 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500930 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400931 return 1;
932}
933
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500934static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100935{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500936 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100937 case FMODE_WRITE:
938 state->n_wronly++;
939 break;
940 case FMODE_READ:
941 state->n_rdonly++;
942 break;
943 case FMODE_READ|FMODE_WRITE:
944 state->n_rdwr++;
945 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100947}
948
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500949static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400950{
951 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500952 nfs4_stateid_copy(&state->stateid, stateid);
953 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400955 case FMODE_READ:
956 set_bit(NFS_O_RDONLY_STATE, &state->flags);
957 break;
958 case FMODE_WRITE:
959 set_bit(NFS_O_WRONLY_STATE, &state->flags);
960 break;
961 case FMODE_READ|FMODE_WRITE:
962 set_bit(NFS_O_RDWR_STATE, &state->flags);
963 }
964}
965
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500966static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400967{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400968 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500969 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400970 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400971}
972
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973static 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 -0700974{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400975 /*
976 * Protect the call to nfs4_state_set_mode_locked and
977 * serialise the stateid update
978 */
979 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400980 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500981 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400982 set_bit(NFS_DELEGATED_STATE, &state->flags);
983 }
984 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500985 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400986 write_sequnlock(&state->seqlock);
987 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500988 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700989 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992static 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 -0500993{
994 struct nfs_inode *nfsi = NFS_I(state->inode);
995 struct nfs_delegation *deleg_cur;
996 int ret = 0;
997
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500999
1000 rcu_read_lock();
1001 deleg_cur = rcu_dereference(nfsi->delegation);
1002 if (deleg_cur == NULL)
1003 goto no_delegation;
1004
1005 spin_lock(&deleg_cur->lock);
1006 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001007 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001008 goto no_delegation_unlock;
1009
1010 if (delegation == NULL)
1011 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001012 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001013 goto no_delegation_unlock;
1014
Trond Myklebustb7391f42008-12-23 15:21:52 -05001015 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001016 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001017 ret = 1;
1018no_delegation_unlock:
1019 spin_unlock(&deleg_cur->lock);
1020no_delegation:
1021 rcu_read_unlock();
1022
1023 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001024 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001025 ret = 1;
1026 }
1027
1028 return ret;
1029}
1030
1031
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001032static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001033{
1034 struct nfs_delegation *delegation;
1035
1036 rcu_read_lock();
1037 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001038 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001039 rcu_read_unlock();
1040 return;
1041 }
1042 rcu_read_unlock();
1043 nfs_inode_return_delegation(inode);
1044}
1045
Trond Myklebust6ee41262007-07-08 14:11:36 -04001046static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001047{
1048 struct nfs4_state *state = opendata->state;
1049 struct nfs_inode *nfsi = NFS_I(state->inode);
1050 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001051 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001052 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001053 nfs4_stateid stateid;
1054 int ret = -EAGAIN;
1055
Trond Myklebustaac00a82007-07-05 19:02:21 -04001056 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001057 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001058 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001059 if (can_open_cached(state, fmode, open_mode)) {
1060 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001061 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001062 goto out_return_state;
1063 }
1064 spin_unlock(&state->owner->so_lock);
1065 }
Trond Myklebust34310432008-12-23 15:21:38 -05001066 rcu_read_lock();
1067 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001068 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001069 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001070 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001071 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001072 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001073 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001074 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001075 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001076 if (ret != 0)
1077 goto out;
1078 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001079
1080 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001081 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001082 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001083 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001084out:
1085 return ERR_PTR(ret);
1086out_return_state:
1087 atomic_inc(&state->count);
1088 return state;
1089}
1090
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001091static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1092{
1093 struct inode *inode;
1094 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001095 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001096 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001097
Trond Myklebustaac00a82007-07-05 19:02:21 -04001098 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001099 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001100 goto out;
1101 }
1102
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001103 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001104 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001105 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001106 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001107 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001108 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001109 goto err;
1110 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001111 state = nfs4_get_open_state(inode, data->owner);
1112 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001113 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001114 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001115 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001116 int delegation_flags = 0;
1117
Trond Myklebust003707c2007-07-05 18:07:55 -04001118 rcu_read_lock();
1119 delegation = rcu_dereference(NFS_I(inode)->delegation);
1120 if (delegation)
1121 delegation_flags = delegation->flags;
1122 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001123 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1124 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1125 "returning a delegation for "
1126 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001127 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001128 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001129 nfs_inode_set_delegation(state->inode,
1130 data->owner->so_cred,
1131 &data->o_res);
1132 else
1133 nfs_inode_reclaim_delegation(state->inode,
1134 data->owner->so_cred,
1135 &data->o_res);
1136 }
Trond Myklebust34310432008-12-23 15:21:38 -05001137
1138 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001139 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001140 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001141out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001142 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001143err_put_inode:
1144 iput(inode);
1145err:
1146 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001147}
1148
Trond Myklebust864472e2006-01-03 09:55:15 +01001149static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1150{
1151 struct nfs_inode *nfsi = NFS_I(state->inode);
1152 struct nfs_open_context *ctx;
1153
1154 spin_lock(&state->inode->i_lock);
1155 list_for_each_entry(ctx, &nfsi->open_files, list) {
1156 if (ctx->state != state)
1157 continue;
1158 get_nfs_open_context(ctx);
1159 spin_unlock(&state->inode->i_lock);
1160 return ctx;
1161 }
1162 spin_unlock(&state->inode->i_lock);
1163 return ERR_PTR(-ENOENT);
1164}
1165
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001166static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1167{
1168 struct nfs4_opendata *opendata;
1169
Al Viro3d4ff432011-06-22 18:40:12 -04001170 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001171 if (opendata == NULL)
1172 return ERR_PTR(-ENOMEM);
1173 opendata->state = state;
1174 atomic_inc(&state->count);
1175 return opendata;
1176}
1177
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001178static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001179{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001180 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001181 int ret;
1182
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001183 opendata->o_arg.open_flags = 0;
1184 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001185 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1186 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1187 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001188 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001189 if (ret != 0)
1190 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001191 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001192 if (IS_ERR(newstate))
1193 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001194 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001195 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001196 return 0;
1197}
1198
1199static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1200{
Trond Myklebust864472e2006-01-03 09:55:15 +01001201 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001202 int ret;
1203
1204 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001205 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001206 smp_rmb();
1207 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001208 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001209 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001210 if (ret != 0)
1211 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001212 if (newstate != state)
1213 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001214 }
1215 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001216 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001217 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001218 if (ret != 0)
1219 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001220 if (newstate != state)
1221 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001222 }
1223 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001224 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001225 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001226 if (ret != 0)
1227 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001228 if (newstate != state)
1229 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001230 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001231 /*
1232 * We may have performed cached opens for all three recoveries.
1233 * Check if we need to update the current stateid.
1234 */
1235 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001236 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001237 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001238 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001239 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001240 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001241 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001242 return 0;
1243}
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245/*
1246 * OPEN_RECLAIM:
1247 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001249static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001251 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001252 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001253 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 int status;
1255
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001256 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1257 if (IS_ERR(opendata))
1258 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001259 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1260 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001261 rcu_read_lock();
1262 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001263 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001264 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001265 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001266 opendata->o_arg.u.delegation_type = delegation_type;
1267 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001268 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return status;
1270}
1271
Trond Myklebust539cd032007-06-05 11:46:42 -04001272static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274 struct nfs_server *server = NFS_SERVER(state->inode);
1275 struct nfs4_exception exception = { };
1276 int err;
1277 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001278 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001279 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001280 break;
1281 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 } while (exception.retry);
1283 return err;
1284}
1285
Trond Myklebust864472e2006-01-03 09:55:15 +01001286static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1287{
1288 struct nfs_open_context *ctx;
1289 int ret;
1290
1291 ctx = nfs4_state_find_open_context(state);
1292 if (IS_ERR(ctx))
1293 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001294 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001295 put_nfs_open_context(ctx);
1296 return ret;
1297}
1298
Trond Myklebust13437e12007-07-06 15:10:43 -04001299static 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 -07001300{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001301 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001302 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001304 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1305 if (IS_ERR(opendata))
1306 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001307 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001308 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001309 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001310 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001311 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
Trond Myklebust13437e12007-07-06 15:10:43 -04001314int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
1316 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001317 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 int err;
1319 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001320 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 switch (err) {
1322 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001323 case -ENOENT:
1324 case -ESTALE:
1325 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001326 case -NFS4ERR_BADSESSION:
1327 case -NFS4ERR_BADSLOT:
1328 case -NFS4ERR_BAD_HIGH_SLOT:
1329 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1330 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001331 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001332 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 case -NFS4ERR_STALE_CLIENTID:
1334 case -NFS4ERR_STALE_STATEID:
1335 case -NFS4ERR_EXPIRED:
1336 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001337 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001338 goto out;
1339 case -ERESTARTSYS:
1340 /*
1341 * The show must go on: exit, but mark the
1342 * stateid as needing recovery.
1343 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001344 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001345 case -NFS4ERR_ADMIN_REVOKED:
1346 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001347 nfs_inode_find_state_and_recover(state->inode,
1348 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001349 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001350 case -EKEYEXPIRED:
1351 /*
1352 * User RPCSEC_GSS context has expired.
1353 * We cannot recover this stateid now, so
1354 * skip it and allow recovery thread to
1355 * proceed.
1356 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001357 case -ENOMEM:
1358 err = 0;
1359 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
1361 err = nfs4_handle_exception(server, err, &exception);
1362 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001363out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return err;
1365}
1366
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001367static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1368{
1369 struct nfs4_opendata *data = calldata;
1370
1371 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001372 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001373 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001374 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001375 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001376 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001377 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001378}
1379
1380static void nfs4_open_confirm_release(void *calldata)
1381{
1382 struct nfs4_opendata *data = calldata;
1383 struct nfs4_state *state = NULL;
1384
1385 /* If this request hasn't been cancelled, do nothing */
1386 if (data->cancelled == 0)
1387 goto out_free;
1388 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001389 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001390 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001391 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001392 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001393 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001394out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001395 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001396}
1397
1398static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001399 .rpc_call_done = nfs4_open_confirm_done,
1400 .rpc_release = nfs4_open_confirm_release,
1401};
1402
1403/*
1404 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1405 */
1406static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1407{
1408 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1409 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001410 struct rpc_message msg = {
1411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1412 .rpc_argp = &data->c_arg,
1413 .rpc_resp = &data->c_res,
1414 .rpc_cred = data->owner->so_cred,
1415 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001416 struct rpc_task_setup task_setup_data = {
1417 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001418 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001419 .callback_ops = &nfs4_open_confirm_ops,
1420 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001421 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001422 .flags = RPC_TASK_ASYNC,
1423 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 int status;
1425
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001426 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001427 data->rpc_done = 0;
1428 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001429 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001430 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001431 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001432 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001433 status = nfs4_wait_for_completion_rpc_task(task);
1434 if (status != 0) {
1435 data->cancelled = 1;
1436 smp_wmb();
1437 } else
1438 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001439 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return status;
1441}
1442
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001443static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001445 struct nfs4_opendata *data = calldata;
1446 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001447
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001448 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1449 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001450 /*
1451 * Check if we still need to send an OPEN call, or if we can use
1452 * a delegation instead.
1453 */
1454 if (data->state != NULL) {
1455 struct nfs_delegation *delegation;
1456
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001457 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001458 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001459 rcu_read_lock();
1460 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001461 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1462 can_open_delegated(delegation, data->o_arg.fmode))
1463 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001464 rcu_read_unlock();
1465 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001466 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001467 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001468 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001469 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001470 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1471 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001472 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001473 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001474 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001475 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001476 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001477 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001478 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001479unlock_no_action:
1480 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001481out_no_action:
1482 task->tk_action = NULL;
1483
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001484}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001486static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1487{
1488 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1489 nfs4_open_prepare(task, calldata);
1490}
1491
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001492static void nfs4_open_done(struct rpc_task *task, void *calldata)
1493{
1494 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001496 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001497
Trond Myklebust14516c32010-07-31 14:29:06 -04001498 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1499 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001500
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001501 if (task->tk_status == 0) {
1502 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001503 case S_IFREG:
1504 break;
1505 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001506 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001507 break;
1508 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001509 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001510 break;
1511 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001512 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001513 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001514 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001515 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1516 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001517 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001518 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001519}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001520
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001521static void nfs4_open_release(void *calldata)
1522{
1523 struct nfs4_opendata *data = calldata;
1524 struct nfs4_state *state = NULL;
1525
1526 /* If this request hasn't been cancelled, do nothing */
1527 if (data->cancelled == 0)
1528 goto out_free;
1529 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001530 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001531 goto out_free;
1532 /* In case we need an open_confirm, no cleanup! */
1533 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1534 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001535 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001536 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001537 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001538out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001539 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001540}
1541
1542static const struct rpc_call_ops nfs4_open_ops = {
1543 .rpc_call_prepare = nfs4_open_prepare,
1544 .rpc_call_done = nfs4_open_done,
1545 .rpc_release = nfs4_open_release,
1546};
1547
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001548static const struct rpc_call_ops nfs4_recover_open_ops = {
1549 .rpc_call_prepare = nfs4_recover_open_prepare,
1550 .rpc_call_done = nfs4_open_done,
1551 .rpc_release = nfs4_open_release,
1552};
1553
1554static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001555{
1556 struct inode *dir = data->dir->d_inode;
1557 struct nfs_server *server = NFS_SERVER(dir);
1558 struct nfs_openargs *o_arg = &data->o_arg;
1559 struct nfs_openres *o_res = &data->o_res;
1560 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001561 struct rpc_message msg = {
1562 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1563 .rpc_argp = o_arg,
1564 .rpc_resp = o_res,
1565 .rpc_cred = data->owner->so_cred,
1566 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001567 struct rpc_task_setup task_setup_data = {
1568 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001569 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001570 .callback_ops = &nfs4_open_ops,
1571 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001572 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001573 .flags = RPC_TASK_ASYNC,
1574 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001575 int status;
1576
Trond Myklebust9d12b212012-01-17 22:04:25 -05001577 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001578 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001579 data->rpc_done = 0;
1580 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001581 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001582 if (isrecover)
1583 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001584 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001585 if (IS_ERR(task))
1586 return PTR_ERR(task);
1587 status = nfs4_wait_for_completion_rpc_task(task);
1588 if (status != 0) {
1589 data->cancelled = 1;
1590 smp_wmb();
1591 } else
1592 status = data->rpc_status;
1593 rpc_put_task(task);
1594
1595 return status;
1596}
1597
1598static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1599{
1600 struct inode *dir = data->dir->d_inode;
1601 struct nfs_openres *o_res = &data->o_res;
1602 int status;
1603
1604 status = nfs4_run_open_task(data, 1);
1605 if (status != 0 || !data->rpc_done)
1606 return status;
1607
Trond Myklebust6926afd2012-01-07 13:22:46 -05001608 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1609
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001610 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1611 status = _nfs4_proc_open_confirm(data);
1612 if (status != 0)
1613 return status;
1614 }
1615
1616 return status;
1617}
1618
1619/*
1620 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1621 */
1622static int _nfs4_proc_open(struct nfs4_opendata *data)
1623{
1624 struct inode *dir = data->dir->d_inode;
1625 struct nfs_server *server = NFS_SERVER(dir);
1626 struct nfs_openargs *o_arg = &data->o_arg;
1627 struct nfs_openres *o_res = &data->o_res;
1628 int status;
1629
1630 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001631 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001632 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001633 if (status != 0) {
1634 if (status == -NFS4ERR_BADNAME &&
1635 !(o_arg->open_flags & O_CREAT))
1636 return -ENOENT;
1637 return status;
1638 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001639
Trond Myklebust6926afd2012-01-07 13:22:46 -05001640 nfs_fattr_map_and_free_names(server, &data->f_attr);
1641
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001642 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001643 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001644 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1645 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001647 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001649 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
1651 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001652 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001653 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
Andy Adamsond83217c2011-03-01 01:34:17 +00001656static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001657{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001658 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001659 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001660
Trond Myklebusta78cb572009-08-09 15:06:19 -04001661 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001662 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001663 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001664 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001665 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1666 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001667 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001668 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001669 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001670 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001671 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001672}
1673
Andy Adamsond83217c2011-03-01 01:34:17 +00001674static int nfs4_recover_expired_lease(struct nfs_server *server)
1675{
1676 return nfs4_client_recover_expired_lease(server->nfs_client);
1677}
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679/*
1680 * OPEN_EXPIRED:
1681 * reclaim state on the server after a network partition.
1682 * Assumes caller holds the appropriate lock
1683 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001684static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001686 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001687 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001689 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1690 if (IS_ERR(opendata))
1691 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001692 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001693 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001694 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001695 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001696 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001699static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001700{
Trond Myklebust539cd032007-06-05 11:46:42 -04001701 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001702 struct nfs4_exception exception = { };
1703 int err;
1704
1705 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001706 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001707 switch (err) {
1708 default:
1709 goto out;
1710 case -NFS4ERR_GRACE:
1711 case -NFS4ERR_DELAY:
1712 nfs4_handle_exception(server, err, &exception);
1713 err = 0;
1714 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001715 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001716out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001717 return err;
1718}
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1721{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001723 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Trond Myklebust864472e2006-01-03 09:55:15 +01001725 ctx = nfs4_state_find_open_context(state);
1726 if (IS_ERR(ctx))
1727 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001728 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001729 put_nfs_open_context(ctx);
1730 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001733#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001734static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001735{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001736 int status = NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001737 struct nfs_server *server = NFS_SERVER(state->inode);
1738
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001739 if (state->flags & flags) {
1740 status = nfs41_test_stateid(server, stateid);
1741 if (status != NFS_OK) {
1742 nfs41_free_stateid(server, stateid);
1743 state->flags &= ~flags;
1744 }
1745 }
1746 return status;
1747}
1748
1749static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1750{
1751 int deleg_status, open_status;
1752 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1753 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1754
1755 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1756 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1757
1758 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1759 return NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001760 return nfs4_open_expired(sp, state);
1761}
1762#endif
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001765 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1766 * fields corresponding to attributes that were used to store the verifier.
1767 * Make sure we clobber those fields in the later setattr call
1768 */
1769static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1770{
1771 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1772 !(sattr->ia_valid & ATTR_ATIME_SET))
1773 sattr->ia_valid |= ATTR_ATIME;
1774
1775 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1776 !(sattr->ia_valid & ATTR_MTIME_SET))
1777 sattr->ia_valid |= ATTR_MTIME;
1778}
1779
1780/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001781 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001783static 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 -07001784{
1785 struct nfs4_state_owner *sp;
1786 struct nfs4_state *state = NULL;
1787 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001788 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001789 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001793 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1794 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1796 goto out_err;
1797 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001798 status = nfs4_recover_expired_lease(server);
1799 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001800 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001801 if (dentry->d_inode != NULL)
1802 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001803 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001804 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001805 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001806 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Al Viro82a2c1b2011-06-22 18:30:55 -04001808 if (dentry->d_inode != NULL)
1809 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001810
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001811 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001813 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001815 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001816 status = PTR_ERR(state);
1817 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001818 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001819 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001820 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001821
1822 if (opendata->o_arg.open_flags & O_EXCL) {
1823 nfs4_exclusive_attrset(opendata, sattr);
1824
1825 nfs_fattr_init(opendata->o_res.f_attr);
1826 status = nfs4_do_setattr(state->inode, cred,
1827 opendata->o_res.f_attr, sattr,
1828 state);
1829 if (status == 0)
1830 nfs_setattr_update_inode(state->inode, sattr);
1831 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1832 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001833 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 *res = state;
1836 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001837err_opendata_put:
1838 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001839err_put_state_owner:
1840 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 *res = NULL;
1843 return status;
1844}
1845
1846
Al Viro82a2c1b2011-06-22 18:30:55 -04001847static 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 -07001848{
1849 struct nfs4_exception exception = { };
1850 struct nfs4_state *res;
1851 int status;
1852
1853 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001854 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (status == 0)
1856 break;
1857 /* NOTE: BAD_SEQID means the server and client disagree about the
1858 * book-keeping w.r.t. state-changing operations
1859 * (OPEN/CLOSE/LOCK/LOCKU...)
1860 * It is actually a sign of a bug on the client or on the server.
1861 *
1862 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001863 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 * have unhashed the old state_owner for us, and that we can
1865 * therefore safely retry using a new one. We should still warn
1866 * the user though...
1867 */
1868 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001869 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001870 " returned a bad sequence-id error!\n",
1871 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 exception.retry = 1;
1873 continue;
1874 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001875 /*
1876 * BAD_STATEID on OPEN means that the server cancelled our
1877 * state before it received the OPEN_CONFIRM.
1878 * Recover by retrying the request as per the discussion
1879 * on Page 181 of RFC3530.
1880 */
1881 if (status == -NFS4ERR_BAD_STATEID) {
1882 exception.retry = 1;
1883 continue;
1884 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001885 if (status == -EAGAIN) {
1886 /* We must have found a delegation */
1887 exception.retry = 1;
1888 continue;
1889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1891 status, &exception));
1892 } while (exception.retry);
1893 return res;
1894}
1895
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001896static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1897 struct nfs_fattr *fattr, struct iattr *sattr,
1898 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001900 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001902 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 .iap = sattr,
1904 .server = server,
1905 .bitmask = server->attr_bitmask,
1906 };
1907 struct nfs_setattrres res = {
1908 .fattr = fattr,
1909 .server = server,
1910 };
1911 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001912 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1913 .rpc_argp = &arg,
1914 .rpc_resp = &res,
1915 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001917 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001918 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Trond Myklebust0e574af2005-10-27 22:12:38 -04001920 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Trond Myklebust4fc87962012-03-08 17:42:01 -05001922 if (state != NULL) {
1923 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1924 current->files, current->tgid);
1925 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1926 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001927 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001928 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05001929 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Bryan Schumaker7c513052011-03-24 17:12:24 +00001931 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001932 if (status == 0 && state != NULL)
1933 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001934 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935}
1936
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001937static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1938 struct nfs_fattr *fattr, struct iattr *sattr,
1939 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001941 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001942 struct nfs4_exception exception = {
1943 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05001944 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001945 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 int err;
1947 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04001948 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
1949 switch (err) {
1950 case -NFS4ERR_OPENMODE:
1951 if (state && !(state->state & FMODE_WRITE)) {
1952 err = -EBADF;
1953 if (sattr->ia_valid & ATTR_OPEN)
1954 err = -EACCES;
1955 goto out;
1956 }
1957 }
1958 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04001960out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 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 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002371static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2372 const struct qstr *name, struct nfs_fattr *fattr,
2373 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002374{
2375 int status = -ENOMEM;
2376 struct page *page = NULL;
2377 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002378
2379 page = alloc_page(GFP_KERNEL);
2380 if (page == NULL)
2381 goto out;
2382 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2383 if (locations == NULL)
2384 goto out;
2385
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002386 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002387 if (status != 0)
2388 goto out;
2389 /* Make sure server returned a different fsid for the referral */
2390 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002391 dprintk("%s: server did not return a different fsid for"
2392 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002393 status = -EIO;
2394 goto out;
2395 }
Andy Adamson533eb462011-06-13 18:25:56 -04002396 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2397 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002398
Andy Adamson533eb462011-06-13 18:25:56 -04002399 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002400 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002401 memset(fhandle, 0, sizeof(struct nfs_fh));
2402out:
2403 if (page)
2404 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002405 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002406 return status;
2407}
2408
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2410{
2411 struct nfs4_getattr_arg args = {
2412 .fh = fhandle,
2413 .bitmask = server->attr_bitmask,
2414 };
2415 struct nfs4_getattr_res res = {
2416 .fattr = fattr,
2417 .server = server,
2418 };
2419 struct rpc_message msg = {
2420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2421 .rpc_argp = &args,
2422 .rpc_resp = &res,
2423 };
2424
Trond Myklebust0e574af2005-10-27 22:12:38 -04002425 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002426 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427}
2428
2429static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2430{
2431 struct nfs4_exception exception = { };
2432 int err;
2433 do {
2434 err = nfs4_handle_exception(server,
2435 _nfs4_proc_getattr(server, fhandle, fattr),
2436 &exception);
2437 } while (exception.retry);
2438 return err;
2439}
2440
2441/*
2442 * The file is not closed if it is opened due to the a request to change
2443 * the size of the file. The open call will not be needed once the
2444 * VFS layer lookup-intents are implemented.
2445 *
2446 * Close is called when the inode is destroyed.
2447 * If we haven't opened the file for O_WRONLY, we
2448 * need to in the size_change case to obtain a stateid.
2449 *
2450 * Got race?
2451 * Because OPEN is always done by name in nfsv4, it is
2452 * possible that we opened a different file by the same
2453 * name. We can recognize this race condition, but we
2454 * can't do anything about it besides returning an error.
2455 *
2456 * This will be fixed with VFS changes (lookup-intent).
2457 */
2458static int
2459nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2460 struct iattr *sattr)
2461{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002462 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002463 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002464 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 int status;
2466
Benny Halevy8a1636c2010-07-14 15:43:57 -04002467 if (pnfs_ld_layoutret_on_setattr(inode))
2468 pnfs_return_layout(inode);
2469
Trond Myklebust0e574af2005-10-27 22:12:38 -04002470 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Trond Myklebustd5308382005-11-04 15:33:38 -05002472 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002473 if (sattr->ia_valid & ATTR_FILE) {
2474 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002475
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002476 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002477 if (ctx) {
2478 cred = ctx->cred;
2479 state = ctx->state;
2480 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002481 }
2482
Trond Myklebust536e43d2012-01-17 22:04:26 -05002483 /* Deal with open(O_TRUNC) */
2484 if (sattr->ia_valid & ATTR_OPEN)
2485 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2486
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002487 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002488 if (status == 0)
2489 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 return status;
2491}
2492
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002493static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2494 const struct qstr *name, struct nfs_fh *fhandle,
2495 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002496{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002497 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002498 int status;
2499 struct nfs4_lookup_arg args = {
2500 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002501 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002502 .name = name,
2503 };
2504 struct nfs4_lookup_res res = {
2505 .server = server,
2506 .fattr = fattr,
2507 .fh = fhandle,
2508 };
2509 struct rpc_message msg = {
2510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2511 .rpc_argp = &args,
2512 .rpc_resp = &res,
2513 };
2514
2515 nfs_fattr_init(fattr);
2516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002518 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 dprintk("NFS reply lookup: %d\n", status);
2520 return status;
2521}
2522
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002523static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002524{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002525 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002526 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002527 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2528 fattr->nlink = 2;
2529}
2530
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002531static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2532 struct qstr *name, struct nfs_fh *fhandle,
2533 struct nfs_fattr *fattr)
2534{
2535 struct nfs4_exception exception = { };
2536 struct rpc_clnt *client = *clnt;
2537 int err;
2538 do {
2539 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2540 switch (err) {
2541 case -NFS4ERR_BADNAME:
2542 err = -ENOENT;
2543 goto out;
2544 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002545 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002546 goto out;
2547 case -NFS4ERR_WRONGSEC:
2548 err = -EPERM;
2549 if (client != *clnt)
2550 goto out;
2551
2552 client = nfs4_create_sec_client(client, dir, name);
2553 if (IS_ERR(client))
2554 return PTR_ERR(client);
2555
2556 exception.retry = 1;
2557 break;
2558 default:
2559 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2560 }
2561 } while (exception.retry);
2562
2563out:
2564 if (err == 0)
2565 *clnt = client;
2566 else if (client != *clnt)
2567 rpc_shutdown_client(client);
2568
2569 return err;
2570}
2571
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002572static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002573 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002575 int status;
2576 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002577
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002578 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2579 if (client != NFS_CLIENT(dir)) {
2580 rpc_shutdown_client(client);
2581 nfs_fixup_secinfo_attributes(fattr);
2582 }
2583 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002586struct rpc_clnt *
2587nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2588 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2589{
2590 int status;
2591 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2592
2593 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2594 if (status < 0) {
2595 rpc_shutdown_client(client);
2596 return ERR_PTR(status);
2597 }
2598 return client;
2599}
2600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2602{
Trond Myklebust76b32992007-08-10 17:45:11 -04002603 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 struct nfs4_accessargs args = {
2605 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002606 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002608 struct nfs4_accessres res = {
2609 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002610 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 struct rpc_message msg = {
2612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2613 .rpc_argp = &args,
2614 .rpc_resp = &res,
2615 .rpc_cred = entry->cred,
2616 };
2617 int mode = entry->mask;
2618 int status;
2619
2620 /*
2621 * Determine which access bits we want to ask for...
2622 */
2623 if (mode & MAY_READ)
2624 args.access |= NFS4_ACCESS_READ;
2625 if (S_ISDIR(inode->i_mode)) {
2626 if (mode & MAY_WRITE)
2627 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2628 if (mode & MAY_EXEC)
2629 args.access |= NFS4_ACCESS_LOOKUP;
2630 } else {
2631 if (mode & MAY_WRITE)
2632 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2633 if (mode & MAY_EXEC)
2634 args.access |= NFS4_ACCESS_EXECUTE;
2635 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002636
2637 res.fattr = nfs_alloc_fattr();
2638 if (res.fattr == NULL)
2639 return -ENOMEM;
2640
Bryan Schumaker7c513052011-03-24 17:12:24 +00002641 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 if (!status) {
2643 entry->mask = 0;
2644 if (res.access & NFS4_ACCESS_READ)
2645 entry->mask |= MAY_READ;
2646 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2647 entry->mask |= MAY_WRITE;
2648 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2649 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002650 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002652 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 return status;
2654}
2655
2656static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2657{
2658 struct nfs4_exception exception = { };
2659 int err;
2660 do {
2661 err = nfs4_handle_exception(NFS_SERVER(inode),
2662 _nfs4_proc_access(inode, entry),
2663 &exception);
2664 } while (exception.retry);
2665 return err;
2666}
2667
2668/*
2669 * TODO: For the time being, we don't try to get any attributes
2670 * along with any of the zero-copy operations READ, READDIR,
2671 * READLINK, WRITE.
2672 *
2673 * In the case of the first three, we want to put the GETATTR
2674 * after the read-type operation -- this is because it is hard
2675 * to predict the length of a GETATTR response in v4, and thus
2676 * align the READ data correctly. This means that the GETATTR
2677 * may end up partially falling into the page cache, and we should
2678 * shift it into the 'tail' of the xdr_buf before processing.
2679 * To do this efficiently, we need to know the total length
2680 * of data received, which doesn't seem to be available outside
2681 * of the RPC layer.
2682 *
2683 * In the case of WRITE, we also want to put the GETATTR after
2684 * the operation -- in this case because we want to make sure
2685 * we get the post-operation mtime and size. This means that
2686 * we can't use xdr_encode_pages() as written: we need a variant
2687 * of it which would leave room in the 'tail' iovec.
2688 *
2689 * Both of these changes to the XDR layer would in fact be quite
2690 * minor, but I decided to leave them for a subsequent patch.
2691 */
2692static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2693 unsigned int pgbase, unsigned int pglen)
2694{
2695 struct nfs4_readlink args = {
2696 .fh = NFS_FH(inode),
2697 .pgbase = pgbase,
2698 .pglen = pglen,
2699 .pages = &page,
2700 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002701 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 struct rpc_message msg = {
2703 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2704 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002705 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 };
2707
Bryan Schumaker7c513052011-03-24 17:12:24 +00002708 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 -07002709}
2710
2711static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2712 unsigned int pgbase, unsigned int pglen)
2713{
2714 struct nfs4_exception exception = { };
2715 int err;
2716 do {
2717 err = nfs4_handle_exception(NFS_SERVER(inode),
2718 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2719 &exception);
2720 } while (exception.retry);
2721 return err;
2722}
2723
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724/*
2725 * Got race?
2726 * We will need to arrange for the VFS layer to provide an atomic open.
2727 * Until then, this create/open method is prone to inefficiency and race
2728 * conditions due to the lookup, create, and open VFS calls from sys_open()
2729 * placed on the wire.
2730 *
2731 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2732 * The file will be opened again in the subsequent VFS open call
2733 * (nfs4_proc_file_open).
2734 *
2735 * The open for read will just hang around to be used by any process that
2736 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2737 */
2738
2739static int
2740nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002741 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742{
Al Viro82a2c1b2011-06-22 18:30:55 -04002743 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002745 struct rpc_cred *cred = NULL;
2746 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 int status = 0;
2748
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002749 if (ctx != NULL) {
2750 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002751 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002752 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002754 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002755 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002756 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 if (IS_ERR(state)) {
2758 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002759 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002761 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002762 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002763 if (ctx != NULL)
2764 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002765 else
Al Viro643168c2011-06-22 18:20:23 -04002766 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767out:
2768 return status;
2769}
2770
2771static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2772{
Trond Myklebust16e42952005-10-27 22:12:44 -04002773 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002774 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002776 .name.len = name->len,
2777 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002778 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002780 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002781 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002782 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2785 .rpc_argp = &args,
2786 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002788 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Trond Myklebustd3468902010-04-16 16:22:50 -04002790 res.dir_attr = nfs_alloc_fattr();
2791 if (res.dir_attr == NULL)
2792 goto out;
2793
Bryan Schumaker7c513052011-03-24 17:12:24 +00002794 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002795 if (status == 0) {
2796 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002797 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002798 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002799 nfs_free_fattr(res.dir_attr);
2800out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 return status;
2802}
2803
2804static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2805{
2806 struct nfs4_exception exception = { };
2807 int err;
2808 do {
2809 err = nfs4_handle_exception(NFS_SERVER(dir),
2810 _nfs4_proc_remove(dir, name),
2811 &exception);
2812 } while (exception.retry);
2813 return err;
2814}
2815
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002816static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002818 struct nfs_server *server = NFS_SERVER(dir);
2819 struct nfs_removeargs *args = msg->rpc_argp;
2820 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Trond Myklebusta65318b2009-03-11 14:10:28 -04002822 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002823 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002825 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826}
2827
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002828static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2829{
2830 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2831 &data->args.seq_args,
2832 &data->res.seq_res,
2833 task))
2834 return;
2835 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
2837
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002838static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002840 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2841
Trond Myklebust14516c32010-07-31 14:29:06 -04002842 if (!nfs4_sequence_done(task, &res->seq_res))
2843 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002844 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002845 return 0;
2846 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002847 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002848 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849}
2850
Jeff Laytond3d41522010-09-17 17:31:57 -04002851static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2852{
2853 struct nfs_server *server = NFS_SERVER(dir);
2854 struct nfs_renameargs *arg = msg->rpc_argp;
2855 struct nfs_renameres *res = msg->rpc_resp;
2856
2857 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2858 arg->bitmask = server->attr_bitmask;
2859 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002860 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002861}
2862
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002863static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2864{
2865 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2866 &data->args.seq_args,
2867 &data->res.seq_res,
2868 task))
2869 return;
2870 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002871}
2872
2873static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2874 struct inode *new_dir)
2875{
2876 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2877
2878 if (!nfs4_sequence_done(task, &res->seq_res))
2879 return 0;
2880 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2881 return 0;
2882
2883 update_changeattr(old_dir, &res->old_cinfo);
2884 nfs_post_op_update_inode(old_dir, res->old_fattr);
2885 update_changeattr(new_dir, &res->new_cinfo);
2886 nfs_post_op_update_inode(new_dir, res->new_fattr);
2887 return 1;
2888}
2889
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2891 struct inode *new_dir, struct qstr *new_name)
2892{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002893 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002894 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 .old_dir = NFS_FH(old_dir),
2896 .new_dir = NFS_FH(new_dir),
2897 .old_name = old_name,
2898 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002899 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002901 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002902 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002903 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 struct rpc_message msg = {
2905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2906 .rpc_argp = &arg,
2907 .rpc_resp = &res,
2908 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002909 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Trond Myklebust011fff72010-04-16 16:22:49 -04002911 res.old_fattr = nfs_alloc_fattr();
2912 res.new_fattr = nfs_alloc_fattr();
2913 if (res.old_fattr == NULL || res.new_fattr == NULL)
2914 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Bryan Schumaker7c513052011-03-24 17:12:24 +00002916 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 if (!status) {
2918 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002919 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002921 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002923out:
2924 nfs_free_fattr(res.new_fattr);
2925 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 return status;
2927}
2928
2929static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2930 struct inode *new_dir, struct qstr *new_name)
2931{
2932 struct nfs4_exception exception = { };
2933 int err;
2934 do {
2935 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2936 _nfs4_proc_rename(old_dir, old_name,
2937 new_dir, new_name),
2938 &exception);
2939 } while (exception.retry);
2940 return err;
2941}
2942
2943static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2944{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002945 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 struct nfs4_link_arg arg = {
2947 .fh = NFS_FH(inode),
2948 .dir_fh = NFS_FH(dir),
2949 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002950 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002952 struct nfs4_link_res res = {
2953 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002954 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 struct rpc_message msg = {
2956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2957 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002958 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002960 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Trond Myklebust136f2622010-04-16 16:22:49 -04002962 res.fattr = nfs_alloc_fattr();
2963 res.dir_attr = nfs_alloc_fattr();
2964 if (res.fattr == NULL || res.dir_attr == NULL)
2965 goto out;
2966
Bryan Schumaker7c513052011-03-24 17:12:24 +00002967 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002968 if (!status) {
2969 update_changeattr(dir, &res.cinfo);
2970 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002971 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002972 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002973out:
2974 nfs_free_fattr(res.dir_attr);
2975 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 return status;
2977}
2978
2979static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2980{
2981 struct nfs4_exception exception = { };
2982 int err;
2983 do {
2984 err = nfs4_handle_exception(NFS_SERVER(inode),
2985 _nfs4_proc_link(inode, dir, name),
2986 &exception);
2987 } while (exception.retry);
2988 return err;
2989}
2990
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002991struct nfs4_createdata {
2992 struct rpc_message msg;
2993 struct nfs4_create_arg arg;
2994 struct nfs4_create_res res;
2995 struct nfs_fh fh;
2996 struct nfs_fattr fattr;
2997 struct nfs_fattr dir_fattr;
2998};
2999
3000static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3001 struct qstr *name, struct iattr *sattr, u32 ftype)
3002{
3003 struct nfs4_createdata *data;
3004
3005 data = kzalloc(sizeof(*data), GFP_KERNEL);
3006 if (data != NULL) {
3007 struct nfs_server *server = NFS_SERVER(dir);
3008
3009 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3010 data->msg.rpc_argp = &data->arg;
3011 data->msg.rpc_resp = &data->res;
3012 data->arg.dir_fh = NFS_FH(dir);
3013 data->arg.server = server;
3014 data->arg.name = name;
3015 data->arg.attrs = sattr;
3016 data->arg.ftype = ftype;
3017 data->arg.bitmask = server->attr_bitmask;
3018 data->res.server = server;
3019 data->res.fh = &data->fh;
3020 data->res.fattr = &data->fattr;
3021 data->res.dir_fattr = &data->dir_fattr;
3022 nfs_fattr_init(data->res.fattr);
3023 nfs_fattr_init(data->res.dir_fattr);
3024 }
3025 return data;
3026}
3027
3028static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3029{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003030 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003031 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003032 if (status == 0) {
3033 update_changeattr(dir, &data->res.dir_cinfo);
3034 nfs_post_op_update_inode(dir, data->res.dir_fattr);
3035 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3036 }
3037 return status;
3038}
3039
3040static void nfs4_free_createdata(struct nfs4_createdata *data)
3041{
3042 kfree(data);
3043}
3044
Chuck Lever4f390c12006-08-22 20:06:22 -04003045static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003046 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003048 struct nfs4_createdata *data;
3049 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Chuck Lever94a6d752006-08-22 20:06:23 -04003051 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003052 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003053
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003054 status = -ENOMEM;
3055 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3056 if (data == NULL)
3057 goto out;
3058
3059 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3060 data->arg.u.symlink.pages = &page;
3061 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003063 status = nfs4_do_create(dir, dentry, data);
3064
3065 nfs4_free_createdata(data);
3066out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 return status;
3068}
3069
Chuck Lever4f390c12006-08-22 20:06:22 -04003070static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003071 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
3073 struct nfs4_exception exception = { };
3074 int err;
3075 do {
3076 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003077 _nfs4_proc_symlink(dir, dentry, page,
3078 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 &exception);
3080 } while (exception.retry);
3081 return err;
3082}
3083
3084static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3085 struct iattr *sattr)
3086{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003087 struct nfs4_createdata *data;
3088 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003090 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3091 if (data == NULL)
3092 goto out;
3093
3094 status = nfs4_do_create(dir, dentry, data);
3095
3096 nfs4_free_createdata(data);
3097out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 return status;
3099}
3100
3101static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3102 struct iattr *sattr)
3103{
3104 struct nfs4_exception exception = { };
3105 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003106
3107 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 do {
3109 err = nfs4_handle_exception(NFS_SERVER(dir),
3110 _nfs4_proc_mkdir(dir, dentry, sattr),
3111 &exception);
3112 } while (exception.retry);
3113 return err;
3114}
3115
3116static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003117 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118{
3119 struct inode *dir = dentry->d_inode;
3120 struct nfs4_readdir_arg args = {
3121 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003122 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 .pgbase = 0,
3124 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003125 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003126 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 };
3128 struct nfs4_readdir_res res;
3129 struct rpc_message msg = {
3130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3131 .rpc_argp = &args,
3132 .rpc_resp = &res,
3133 .rpc_cred = cred,
3134 };
3135 int status;
3136
Harvey Harrison3110ff82008-05-02 13:42:44 -07003137 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003138 dentry->d_parent->d_name.name,
3139 dentry->d_name.name,
3140 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3142 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003143 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 -05003144 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003146 status += args.pgbase;
3147 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003148
3149 nfs_invalidate_atime(dir);
3150
Harvey Harrison3110ff82008-05-02 13:42:44 -07003151 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 return status;
3153}
3154
3155static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003156 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157{
3158 struct nfs4_exception exception = { };
3159 int err;
3160 do {
3161 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3162 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003163 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 &exception);
3165 } while (exception.retry);
3166 return err;
3167}
3168
3169static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3170 struct iattr *sattr, dev_t rdev)
3171{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003172 struct nfs4_createdata *data;
3173 int mode = sattr->ia_mode;
3174 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
3176 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3177 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003178
3179 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3180 if (data == NULL)
3181 goto out;
3182
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003184 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003186 data->arg.ftype = NF4BLK;
3187 data->arg.u.device.specdata1 = MAJOR(rdev);
3188 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 }
3190 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003191 data->arg.ftype = NF4CHR;
3192 data->arg.u.device.specdata1 = MAJOR(rdev);
3193 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003196 status = nfs4_do_create(dir, dentry, data);
3197
3198 nfs4_free_createdata(data);
3199out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 return status;
3201}
3202
3203static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3204 struct iattr *sattr, dev_t rdev)
3205{
3206 struct nfs4_exception exception = { };
3207 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003208
3209 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 do {
3211 err = nfs4_handle_exception(NFS_SERVER(dir),
3212 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3213 &exception);
3214 } while (exception.retry);
3215 return err;
3216}
3217
3218static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3219 struct nfs_fsstat *fsstat)
3220{
3221 struct nfs4_statfs_arg args = {
3222 .fh = fhandle,
3223 .bitmask = server->attr_bitmask,
3224 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003225 struct nfs4_statfs_res res = {
3226 .fsstat = fsstat,
3227 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 struct rpc_message msg = {
3229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3230 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003231 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 };
3233
Trond Myklebust0e574af2005-10-27 22:12:38 -04003234 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003235 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236}
3237
3238static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3239{
3240 struct nfs4_exception exception = { };
3241 int err;
3242 do {
3243 err = nfs4_handle_exception(server,
3244 _nfs4_proc_statfs(server, fhandle, fsstat),
3245 &exception);
3246 } while (exception.retry);
3247 return err;
3248}
3249
3250static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3251 struct nfs_fsinfo *fsinfo)
3252{
3253 struct nfs4_fsinfo_arg args = {
3254 .fh = fhandle,
3255 .bitmask = server->attr_bitmask,
3256 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003257 struct nfs4_fsinfo_res res = {
3258 .fsinfo = fsinfo,
3259 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 struct rpc_message msg = {
3261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3262 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003263 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 };
3265
Bryan Schumaker7c513052011-03-24 17:12:24 +00003266 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267}
3268
3269static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3270{
3271 struct nfs4_exception exception = { };
3272 int err;
3273
3274 do {
3275 err = nfs4_handle_exception(server,
3276 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3277 &exception);
3278 } while (exception.retry);
3279 return err;
3280}
3281
3282static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3283{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003284 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3286}
3287
3288static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3289 struct nfs_pathconf *pathconf)
3290{
3291 struct nfs4_pathconf_arg args = {
3292 .fh = fhandle,
3293 .bitmask = server->attr_bitmask,
3294 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003295 struct nfs4_pathconf_res res = {
3296 .pathconf = pathconf,
3297 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 struct rpc_message msg = {
3299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3300 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003301 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 };
3303
3304 /* None of the pathconf attributes are mandatory to implement */
3305 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3306 memset(pathconf, 0, sizeof(*pathconf));
3307 return 0;
3308 }
3309
Trond Myklebust0e574af2005-10-27 22:12:38 -04003310 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003311 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312}
3313
3314static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3315 struct nfs_pathconf *pathconf)
3316{
3317 struct nfs4_exception exception = { };
3318 int err;
3319
3320 do {
3321 err = nfs4_handle_exception(server,
3322 _nfs4_proc_pathconf(server, fhandle, pathconf),
3323 &exception);
3324 } while (exception.retry);
3325 return err;
3326}
3327
Benny Halevyd20581a2011-05-22 19:52:03 +03003328void __nfs4_read_done_cb(struct nfs_read_data *data)
3329{
Fred Isamancd841602012-04-20 14:47:44 -04003330 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003331}
3332
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003333static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
Fred Isamancd841602012-04-20 14:47:44 -04003335 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003337 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003338 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003339 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003341
Benny Halevyd20581a2011-05-22 19:52:03 +03003342 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003344 renew_lease(server, data->timestamp);
3345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346}
3347
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003348static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3349{
3350
3351 dprintk("--> %s\n", __func__);
3352
3353 if (!nfs4_sequence_done(task, &data->res.seq_res))
3354 return -EAGAIN;
3355
Benny Halevyd20581a2011-05-22 19:52:03 +03003356 return data->read_done_cb ? data->read_done_cb(task, data) :
3357 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003358}
3359
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003360static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003363 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003364 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003365 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366}
3367
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003368static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3369{
Fred Isamancd841602012-04-20 14:47:44 -04003370 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003371 &data->args.seq_args,
3372 &data->res.seq_res,
3373 task))
3374 return;
3375 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376}
3377
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003378/* Reset the the nfs_read_data to send the read to the MDS. */
3379void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3380{
Fred Isamancd841602012-04-20 14:47:44 -04003381 struct nfs_pgio_header *hdr = data->header;
3382 struct inode *inode = hdr->inode;
3383
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003384 dprintk("%s Reset task for i/o through\n", __func__);
Fred Isamancd841602012-04-20 14:47:44 -04003385 data->ds_clp = NULL;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003386 /* offsets will differ in the dense stripe case */
3387 data->args.offset = data->mds_offset;
Fred Isamancd841602012-04-20 14:47:44 -04003388 data->args.fh = NFS_FH(inode);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003389 data->read_done_cb = nfs4_read_done_cb;
Fred Isamancd841602012-04-20 14:47:44 -04003390 task->tk_ops = hdr->mds_ops;
3391 rpc_task_reset_client(task, NFS_CLIENT(inode));
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003392}
3393EXPORT_SYMBOL_GPL(nfs4_reset_read);
3394
Fred Isamanb029bc92011-03-03 15:13:42 +00003395static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396{
Fred Isamancd841602012-04-20 14:47:44 -04003397 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003399 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003400 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003401 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003403 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003405 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003406 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003407 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408}
3409
Fred Isamanb029bc92011-03-03 15:13:42 +00003410static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3411{
3412 if (!nfs4_sequence_done(task, &data->res.seq_res))
3413 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003414 return data->write_done_cb ? data->write_done_cb(task, data) :
3415 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003416}
3417
Fred Isamana69aef12011-03-03 15:13:47 +00003418/* Reset the the nfs_write_data to send the write to the MDS. */
3419void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3420{
Fred Isamancd841602012-04-20 14:47:44 -04003421 struct nfs_pgio_header *hdr = data->header;
3422 struct inode *inode = hdr->inode;
3423
Fred Isamana69aef12011-03-03 15:13:47 +00003424 dprintk("%s Reset task for i/o through\n", __func__);
Fred Isamancd841602012-04-20 14:47:44 -04003425 data->ds_clp = NULL;
Fred Isamana69aef12011-03-03 15:13:47 +00003426 data->write_done_cb = nfs4_write_done_cb;
Fred Isamancd841602012-04-20 14:47:44 -04003427 data->args.fh = NFS_FH(inode);
Fred Isamana69aef12011-03-03 15:13:47 +00003428 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3429 data->args.offset = data->mds_offset;
3430 data->res.fattr = &data->fattr;
Fred Isamancd841602012-04-20 14:47:44 -04003431 task->tk_ops = hdr->mds_ops;
3432 rpc_task_reset_client(task, NFS_CLIENT(inode));
Fred Isamana69aef12011-03-03 15:13:47 +00003433}
3434EXPORT_SYMBOL_GPL(nfs4_reset_write);
3435
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003436static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437{
Fred Isamancd841602012-04-20 14:47:44 -04003438 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003439
Fred Isaman6c75dc02012-04-20 14:47:47 -04003440 if (data->ds_clp) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003441 data->args.bitmask = NULL;
3442 data->res.fattr = NULL;
3443 } else
3444 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003445 if (!data->write_done_cb)
3446 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003447 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 data->timestamp = jiffies;
3449
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003450 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003451 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452}
3453
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003454static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3455{
Fred Isamancd841602012-04-20 14:47:44 -04003456 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003457 &data->args.seq_args,
3458 &data->res.seq_res,
3459 task))
3460 return;
3461 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462}
3463
Fred Isaman0b7c0152012-04-20 14:47:39 -04003464static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3465{
3466 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3467 &data->args.seq_args,
3468 &data->res.seq_res,
3469 task))
3470 return;
3471 rpc_call_start(task);
3472}
3473
3474static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003477
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003478 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003479 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003480 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003482 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003483 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484}
3485
Fred Isaman0b7c0152012-04-20 14:47:39 -04003486static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003487{
3488 if (!nfs4_sequence_done(task, &data->res.seq_res))
3489 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003490 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003491}
3492
Fred Isaman0b7c0152012-04-20 14:47:39 -04003493static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003495 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003496
3497 if (data->lseg) {
3498 data->args.bitmask = NULL;
3499 data->res.fattr = NULL;
3500 } else
3501 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003502 if (data->commit_done_cb == NULL)
3503 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003504 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003505 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003506 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507}
3508
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003509struct nfs4_renewdata {
3510 struct nfs_client *client;
3511 unsigned long timestamp;
3512};
3513
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514/*
3515 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3516 * standalone procedure for queueing an asynchronous RENEW.
3517 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003518static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003519{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003520 struct nfs4_renewdata *data = calldata;
3521 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003522
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003523 if (atomic_read(&clp->cl_count) > 1)
3524 nfs4_schedule_state_renewal(clp);
3525 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003526 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003527}
3528
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003529static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003531 struct nfs4_renewdata *data = calldata;
3532 struct nfs_client *clp = data->client;
3533 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
3535 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003536 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003537 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3538 return;
3539 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003540 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003541 return;
3542 }
3543 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003545 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546}
3547
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003548static const struct rpc_call_ops nfs4_renew_ops = {
3549 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003550 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003551};
3552
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003553static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554{
3555 struct rpc_message msg = {
3556 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3557 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003558 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003560 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003562 if (renew_flags == 0)
3563 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003564 if (!atomic_inc_not_zero(&clp->cl_count))
3565 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003566 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003567 if (data == NULL)
3568 return -ENOMEM;
3569 data->client = clp;
3570 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003572 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573}
3574
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003575static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576{
3577 struct rpc_message msg = {
3578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3579 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003580 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 };
3582 unsigned long now = jiffies;
3583 int status;
3584
3585 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3586 if (status < 0)
3587 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003588 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 return 0;
3590}
3591
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003592static inline int nfs4_server_supports_acls(struct nfs_server *server)
3593{
3594 return (server->caps & NFS_CAP_ACLS)
3595 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3596 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3597}
3598
3599/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3600 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3601 * the stack.
3602 */
3603#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3604
Neil Hormane9e3d722011-03-04 19:26:03 -05003605static int buf_to_pages_noslab(const void *buf, size_t buflen,
3606 struct page **pages, unsigned int *pgbase)
3607{
3608 struct page *newpage, **spages;
3609 int rc = 0;
3610 size_t len;
3611 spages = pages;
3612
3613 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003614 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003615 newpage = alloc_page(GFP_KERNEL);
3616
3617 if (newpage == NULL)
3618 goto unwind;
3619 memcpy(page_address(newpage), buf, len);
3620 buf += len;
3621 buflen -= len;
3622 *pages++ = newpage;
3623 rc++;
3624 } while (buflen != 0);
3625
3626 return rc;
3627
3628unwind:
3629 for(; rc > 0; rc--)
3630 __free_page(spages[rc-1]);
3631 return -ENOMEM;
3632}
3633
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003634struct nfs4_cached_acl {
3635 int cached;
3636 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003637 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003638};
3639
3640static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003641{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003642 struct nfs_inode *nfsi = NFS_I(inode);
3643
3644 spin_lock(&inode->i_lock);
3645 kfree(nfsi->nfs4_acl);
3646 nfsi->nfs4_acl = acl;
3647 spin_unlock(&inode->i_lock);
3648}
3649
3650static void nfs4_zap_acl_attr(struct inode *inode)
3651{
3652 nfs4_set_cached_acl(inode, NULL);
3653}
3654
3655static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3656{
3657 struct nfs_inode *nfsi = NFS_I(inode);
3658 struct nfs4_cached_acl *acl;
3659 int ret = -ENOENT;
3660
3661 spin_lock(&inode->i_lock);
3662 acl = nfsi->nfs4_acl;
3663 if (acl == NULL)
3664 goto out;
3665 if (buf == NULL) /* user is just asking for length */
3666 goto out_len;
3667 if (acl->cached == 0)
3668 goto out;
3669 ret = -ERANGE; /* see getxattr(2) man page */
3670 if (acl->len > buflen)
3671 goto out;
3672 memcpy(buf, acl->data, acl->len);
3673out_len:
3674 ret = acl->len;
3675out:
3676 spin_unlock(&inode->i_lock);
3677 return ret;
3678}
3679
Sachin Prabhu5794d212012-04-17 14:36:40 +01003680static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003681{
3682 struct nfs4_cached_acl *acl;
3683
Sachin Prabhu5794d212012-04-17 14:36:40 +01003684 if (pages && acl_len <= PAGE_SIZE) {
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003685 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3686 if (acl == NULL)
3687 goto out;
3688 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003689 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003690 } else {
3691 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3692 if (acl == NULL)
3693 goto out;
3694 acl->cached = 0;
3695 }
3696 acl->len = acl_len;
3697out:
3698 nfs4_set_cached_acl(inode, acl);
3699}
3700
Andy Adamsonbf118a32011-12-07 11:55:27 -05003701/*
3702 * The getxattr API returns the required buffer length when called with a
3703 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3704 * the required buf. On a NULL buf, we send a page of data to the server
3705 * guessing that the ACL request can be serviced by a page. If so, we cache
3706 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3707 * the cache. If not so, we throw away the page, and cache the required
3708 * length. The next getxattr call will then produce another round trip to
3709 * the server, this time with the input buf of the required size.
3710 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003711static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003712{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003713 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003714 struct nfs_getaclargs args = {
3715 .fh = NFS_FH(inode),
3716 .acl_pages = pages,
3717 .acl_len = buflen,
3718 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003719 struct nfs_getaclres res = {
3720 .acl_len = buflen,
3721 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003722 struct rpc_message msg = {
3723 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3724 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003725 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003726 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003727 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003728
Andy Adamsonbf118a32011-12-07 11:55:27 -05003729 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3730 /* As long as we're doing a round trip to the server anyway,
3731 * let's be prepared for a page of acl data. */
3732 if (npages == 0)
3733 npages = 1;
3734
Sachin Prabhu5a006892012-04-17 14:35:39 +01003735 /* Add an extra page to handle the bitmap returned */
3736 npages++;
3737
Andy Adamsonbf118a32011-12-07 11:55:27 -05003738 for (i = 0; i < npages; i++) {
3739 pages[i] = alloc_page(GFP_KERNEL);
3740 if (!pages[i])
3741 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003742 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003743
3744 /* for decoding across pages */
3745 res.acl_scratch = alloc_page(GFP_KERNEL);
3746 if (!res.acl_scratch)
3747 goto out_free;
3748
Andy Adamsonbf118a32011-12-07 11:55:27 -05003749 args.acl_len = npages * PAGE_SIZE;
3750 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003751
Andy Adamsonbf118a32011-12-07 11:55:27 -05003752 /* Let decode_getfacl know not to fail if the ACL data is larger than
3753 * the page we send as a guess */
3754 if (buf == NULL)
3755 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003756
Peng Taode040be2012-01-10 22:42:47 +08003757 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003758 __func__, buf, buflen, npages, args.acl_len);
3759 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3760 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003761 if (ret)
3762 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003763
3764 acl_len = res.acl_len - res.acl_data_offset;
3765 if (acl_len > args.acl_len)
Sachin Prabhu5794d212012-04-17 14:36:40 +01003766 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003767 else
Sachin Prabhu5794d212012-04-17 14:36:40 +01003768 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
Andy Adamsonbf118a32011-12-07 11:55:27 -05003769 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003770 if (buf) {
3771 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003772 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003773 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003774 _copy_from_pages(buf, pages, res.acl_data_offset,
Sachin Prabhu20e0fa92012-03-22 16:46:28 +00003775 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003776 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003777 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003778out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003779 for (i = 0; i < npages; i++)
3780 if (pages[i])
3781 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003782 if (res.acl_scratch)
3783 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003784 return ret;
3785}
3786
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003787static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3788{
3789 struct nfs4_exception exception = { };
3790 ssize_t ret;
3791 do {
3792 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3793 if (ret >= 0)
3794 break;
3795 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3796 } while (exception.retry);
3797 return ret;
3798}
3799
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003800static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3801{
3802 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003803 int ret;
3804
3805 if (!nfs4_server_supports_acls(server))
3806 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003807 ret = nfs_revalidate_inode(server, inode);
3808 if (ret < 0)
3809 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003810 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3811 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003812 ret = nfs4_read_cached_acl(inode, buf, buflen);
3813 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003814 /* -ENOENT is returned if there is no ACL or if there is an ACL
3815 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003816 return ret;
3817 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003818}
3819
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003820static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003821{
3822 struct nfs_server *server = NFS_SERVER(inode);
3823 struct page *pages[NFS4ACL_MAXPAGES];
3824 struct nfs_setaclargs arg = {
3825 .fh = NFS_FH(inode),
3826 .acl_pages = pages,
3827 .acl_len = buflen,
3828 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003829 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003830 struct rpc_message msg = {
3831 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3832 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003833 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003834 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003835 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003836
3837 if (!nfs4_server_supports_acls(server))
3838 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003839 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3840 if (i < 0)
3841 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003842 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003843 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003844
3845 /*
3846 * Free each page after tx, so the only ref left is
3847 * held by the network stack
3848 */
3849 for (; i > 0; i--)
3850 put_page(pages[i-1]);
3851
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003852 /*
3853 * Acl update can result in inode attribute update.
3854 * so mark the attribute cache invalid.
3855 */
3856 spin_lock(&inode->i_lock);
3857 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3858 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003859 nfs_access_zap_cache(inode);
3860 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003861 return ret;
3862}
3863
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003864static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3865{
3866 struct nfs4_exception exception = { };
3867 int err;
3868 do {
3869 err = nfs4_handle_exception(NFS_SERVER(inode),
3870 __nfs4_proc_set_acl(inode, buf, buflen),
3871 &exception);
3872 } while (exception.retry);
3873 return err;
3874}
3875
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003877nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003879 struct nfs_client *clp = server->nfs_client;
3880
3881 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 return 0;
3883 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003884 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003885 case -NFS4ERR_ADMIN_REVOKED:
3886 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003887 if (state == NULL)
3888 break;
3889 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003890 case -NFS4ERR_OPENMODE:
3891 if (state == NULL)
3892 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003893 nfs4_schedule_stateid_recovery(server, state);
3894 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003895 case -NFS4ERR_EXPIRED:
3896 if (state != NULL)
3897 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003899 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003900 nfs4_schedule_lease_recovery(clp);
3901 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003902#if defined(CONFIG_NFS_V4_1)
3903 case -NFS4ERR_BADSESSION:
3904 case -NFS4ERR_BADSLOT:
3905 case -NFS4ERR_BAD_HIGH_SLOT:
3906 case -NFS4ERR_DEADSESSION:
3907 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3908 case -NFS4ERR_SEQ_FALSE_RETRY:
3909 case -NFS4ERR_SEQ_MISORDERED:
3910 dprintk("%s ERROR %d, Reset session\n", __func__,
3911 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003912 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003913 task->tk_status = 0;
3914 return -EAGAIN;
3915#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003917 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003918 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003919 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3921 task->tk_status = 0;
3922 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003923 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 case -NFS4ERR_OLD_STATEID:
3925 task->tk_status = 0;
3926 return -EAGAIN;
3927 }
3928 task->tk_status = nfs4_map_errors(task->tk_status);
3929 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003930wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003931 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003932 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3933 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3934 task->tk_status = 0;
3935 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936}
3937
Chuck Levercd937102012-03-02 17:14:31 -05003938static void nfs4_construct_boot_verifier(struct nfs_client *clp,
3939 nfs4_verifier *bootverf)
3940{
3941 __be32 verf[2];
3942
3943 verf[0] = htonl((u32)clp->cl_boot_time.tv_sec);
3944 verf[1] = htonl((u32)clp->cl_boot_time.tv_nsec);
3945 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3946}
3947
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003948int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3949 unsigned short port, struct rpc_cred *cred,
3950 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951{
3952 nfs4_verifier sc_verifier;
3953 struct nfs4_setclientid setclientid = {
3954 .sc_verifier = &sc_verifier,
3955 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003956 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 };
3958 struct rpc_message msg = {
3959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3960 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003961 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003962 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 int loop = 0;
3965 int status;
3966
Chuck Levercd937102012-03-02 17:14:31 -05003967 nfs4_construct_boot_verifier(clp, &sc_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968
3969 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05003970 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003972 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003973 clp->cl_ipaddr,
3974 rpc_peeraddr2str(clp->cl_rpcclient,
3975 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003976 rpc_peeraddr2str(clp->cl_rpcclient,
3977 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003978 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 clp->cl_id_uniquifier);
3980 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003981 sizeof(setclientid.sc_netid),
3982 rpc_peeraddr2str(clp->cl_rpcclient,
3983 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003985 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05003987 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003989 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 if (status != -NFS4ERR_CLID_INUSE)
3991 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003992 if (loop != 0) {
3993 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003995 }
3996 ++loop;
3997 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 }
3999 return status;
4000}
4001
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004002int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004003 struct nfs4_setclientid_res *arg,
4004 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005{
4006 struct nfs_fsinfo fsinfo;
4007 struct rpc_message msg = {
4008 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004009 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004011 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 };
4013 unsigned long now;
4014 int status;
4015
4016 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004017 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 if (status == 0) {
4019 spin_lock(&clp->cl_lock);
4020 clp->cl_lease_time = fsinfo.lease_time * HZ;
4021 clp->cl_last_renewal = now;
4022 spin_unlock(&clp->cl_lock);
4023 }
4024 return status;
4025}
4026
Trond Myklebustfe650402006-01-03 09:55:18 +01004027struct nfs4_delegreturndata {
4028 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004029 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004030 struct nfs_fh fh;
4031 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004032 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004033 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004034 int rpc_status;
4035};
4036
Trond Myklebustfe650402006-01-03 09:55:18 +01004037static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4038{
4039 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004040
Trond Myklebust14516c32010-07-31 14:29:06 -04004041 if (!nfs4_sequence_done(task, &data->res.seq_res))
4042 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004043
Ricardo Labiaga79708862009-12-07 09:23:21 -05004044 switch (task->tk_status) {
4045 case -NFS4ERR_STALE_STATEID:
4046 case -NFS4ERR_EXPIRED:
4047 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004048 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004049 break;
4050 default:
4051 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4052 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004053 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004054 return;
4055 }
4056 }
4057 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004058}
4059
4060static void nfs4_delegreturn_release(void *calldata)
4061{
Trond Myklebustfe650402006-01-03 09:55:18 +01004062 kfree(calldata);
4063}
4064
Andy Adamson938e1012009-04-01 09:22:28 -04004065#if defined(CONFIG_NFS_V4_1)
4066static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4067{
4068 struct nfs4_delegreturndata *d_data;
4069
4070 d_data = (struct nfs4_delegreturndata *)data;
4071
Trond Myklebust035168a2010-06-16 09:52:26 -04004072 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004073 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004074 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004075 return;
4076 rpc_call_start(task);
4077}
4078#endif /* CONFIG_NFS_V4_1 */
4079
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004080static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004081#if defined(CONFIG_NFS_V4_1)
4082 .rpc_call_prepare = nfs4_delegreturn_prepare,
4083#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004084 .rpc_call_done = nfs4_delegreturn_done,
4085 .rpc_release = nfs4_delegreturn_release,
4086};
4087
Trond Myklebuste6f81072008-01-24 18:14:34 -05004088static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004089{
4090 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004091 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004092 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004093 struct rpc_message msg = {
4094 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4095 .rpc_cred = cred,
4096 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004097 struct rpc_task_setup task_setup_data = {
4098 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004099 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004100 .callback_ops = &nfs4_delegreturn_ops,
4101 .flags = RPC_TASK_ASYNC,
4102 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004103 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004104
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004105 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004106 if (data == NULL)
4107 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004108 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004109 data->args.fhandle = &data->fh;
4110 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004111 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004112 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004113 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004114 data->res.fattr = &data->fattr;
4115 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004116 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004117 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004118 data->rpc_status = 0;
4119
Trond Myklebustc970aa82007-07-14 15:39:59 -04004120 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004121 msg.rpc_argp = &data->args;
4122 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004123 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004124 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004125 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004126 if (!issync)
4127 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004128 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004129 if (status != 0)
4130 goto out;
4131 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004132 if (status == 0)
4133 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4134 else
4135 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004136out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004137 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004138 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139}
4140
Trond Myklebuste6f81072008-01-24 18:14:34 -05004141int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142{
4143 struct nfs_server *server = NFS_SERVER(inode);
4144 struct nfs4_exception exception = { };
4145 int err;
4146 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004147 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 switch (err) {
4149 case -NFS4ERR_STALE_STATEID:
4150 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 case 0:
4152 return 0;
4153 }
4154 err = nfs4_handle_exception(server, err, &exception);
4155 } while (exception.retry);
4156 return err;
4157}
4158
4159#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4160#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4161
4162/*
4163 * sleep, with exponential backoff, and retry the LOCK operation.
4164 */
4165static unsigned long
4166nfs4_set_lock_task_retry(unsigned long timeout)
4167{
Jeff Laytond3103102011-12-01 22:44:39 +01004168 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 timeout <<= 1;
4170 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4171 return NFS4_LOCK_MAXTIMEOUT;
4172 return timeout;
4173}
4174
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4176{
4177 struct inode *inode = state->inode;
4178 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004179 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004180 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004182 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004184 struct nfs_lockt_res res = {
4185 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 };
4187 struct rpc_message msg = {
4188 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4189 .rpc_argp = &arg,
4190 .rpc_resp = &res,
4191 .rpc_cred = state->owner->so_cred,
4192 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 struct nfs4_lock_state *lsp;
4194 int status;
4195
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004196 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004197 status = nfs4_set_lock_state(state, request);
4198 if (status != 0)
4199 goto out;
4200 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004201 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004202 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004203 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004204 switch (status) {
4205 case 0:
4206 request->fl_type = F_UNLCK;
4207 break;
4208 case -NFS4ERR_DENIED:
4209 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004211 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004212out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 return status;
4214}
4215
4216static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4217{
4218 struct nfs4_exception exception = { };
4219 int err;
4220
4221 do {
4222 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4223 _nfs4_proc_getlk(state, cmd, request),
4224 &exception);
4225 } while (exception.retry);
4226 return err;
4227}
4228
4229static int do_vfs_lock(struct file *file, struct file_lock *fl)
4230{
4231 int res = 0;
4232 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4233 case FL_POSIX:
4234 res = posix_lock_file_wait(file, fl);
4235 break;
4236 case FL_FLOCK:
4237 res = flock_lock_file_wait(file, fl);
4238 break;
4239 default:
4240 BUG();
4241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 return res;
4243}
4244
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004245struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004246 struct nfs_locku_args arg;
4247 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004248 struct nfs4_lock_state *lsp;
4249 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004250 struct file_lock fl;
4251 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004252 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004253};
4254
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004255static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4256 struct nfs_open_context *ctx,
4257 struct nfs4_lock_state *lsp,
4258 struct nfs_seqid *seqid)
4259{
4260 struct nfs4_unlockdata *p;
4261 struct inode *inode = lsp->ls_state->inode;
4262
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004263 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004264 if (p == NULL)
4265 return NULL;
4266 p->arg.fh = NFS_FH(inode);
4267 p->arg.fl = &p->fl;
4268 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004269 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004270 p->arg.stateid = &lsp->ls_stateid;
4271 p->lsp = lsp;
4272 atomic_inc(&lsp->ls_count);
4273 /* Ensure we don't close file until we're done freeing locks! */
4274 p->ctx = get_nfs_open_context(ctx);
4275 memcpy(&p->fl, fl, sizeof(p->fl));
4276 p->server = NFS_SERVER(inode);
4277 return p;
4278}
4279
Trond Myklebust06f814a2006-01-03 09:55:07 +01004280static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004281{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004282 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004283 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004284 nfs4_put_lock_state(calldata->lsp);
4285 put_nfs_open_context(calldata->ctx);
4286 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004287}
4288
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004289static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004290{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004291 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004292
Trond Myklebust14516c32010-07-31 14:29:06 -04004293 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4294 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004295 switch (task->tk_status) {
4296 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004297 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4298 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004299 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004300 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004301 case -NFS4ERR_BAD_STATEID:
4302 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004303 case -NFS4ERR_STALE_STATEID:
4304 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004305 break;
4306 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004307 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004308 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004309 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004310}
4311
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004312static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004313{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004314 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004316 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004317 return;
4318 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004319 /* Note: exit _without_ running nfs4_locku_done */
4320 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004321 return;
4322 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004323 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004324 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004325 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004326 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004327 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004328 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329}
4330
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004331static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004332 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004333 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004334 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004335};
4336
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004337static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4338 struct nfs_open_context *ctx,
4339 struct nfs4_lock_state *lsp,
4340 struct nfs_seqid *seqid)
4341{
4342 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004343 struct rpc_message msg = {
4344 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4345 .rpc_cred = ctx->cred,
4346 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004347 struct rpc_task_setup task_setup_data = {
4348 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004349 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004350 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004351 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004352 .flags = RPC_TASK_ASYNC,
4353 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004354
Frank Filz137d6ac2007-07-09 15:32:29 -07004355 /* Ensure this is an unlock - when canceling a lock, the
4356 * canceled lock is passed in, and it won't be an unlock.
4357 */
4358 fl->fl_type = F_UNLCK;
4359
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004360 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4361 if (data == NULL) {
4362 nfs_free_seqid(seqid);
4363 return ERR_PTR(-ENOMEM);
4364 }
4365
Trond Myklebust9d12b212012-01-17 22:04:25 -05004366 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004367 msg.rpc_argp = &data->arg;
4368 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004369 task_setup_data.callback_data = data;
4370 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004371}
4372
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4374{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004375 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004376 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004377 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004378 struct rpc_task *task;
4379 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004380 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381
Trond Myklebust9b073572006-06-29 16:38:34 -04004382 status = nfs4_set_lock_state(state, request);
4383 /* Unlock _before_ we do the RPC call */
4384 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004385 down_read(&nfsi->rwsem);
4386 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4387 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004388 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004389 }
4390 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004391 if (status != 0)
4392 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004393 /* Is this a delegated lock? */
4394 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004395 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004396 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004397 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004398 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004399 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004400 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004401 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004402 status = PTR_ERR(task);
4403 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004404 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004405 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004406 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004407out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004408 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004409 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410}
4411
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004412struct nfs4_lockdata {
4413 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004414 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004415 struct nfs4_lock_state *lsp;
4416 struct nfs_open_context *ctx;
4417 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004418 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004419 int rpc_status;
4420 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004421 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004422};
4423
4424static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004425 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4426 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004427{
4428 struct nfs4_lockdata *p;
4429 struct inode *inode = lsp->ls_state->inode;
4430 struct nfs_server *server = NFS_SERVER(inode);
4431
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004432 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004433 if (p == NULL)
4434 return NULL;
4435
4436 p->arg.fh = NFS_FH(inode);
4437 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004438 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004439 if (p->arg.open_seqid == NULL)
4440 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004441 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004442 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004443 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004444 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004445 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004446 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004447 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004448 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004449 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004450 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004451 atomic_inc(&lsp->ls_count);
4452 p->ctx = get_nfs_open_context(ctx);
4453 memcpy(&p->fl, fl, sizeof(p->fl));
4454 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004455out_free_seqid:
4456 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004457out_free:
4458 kfree(p);
4459 return NULL;
4460}
4461
4462static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4463{
4464 struct nfs4_lockdata *data = calldata;
4465 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
Harvey Harrison3110ff82008-05-02 13:42:44 -07004467 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004468 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4469 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004470 /* Do we need to do an open_to_lock_owner? */
4471 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004472 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4473 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004474 data->arg.open_stateid = &state->stateid;
4475 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004476 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004477 } else
4478 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004479 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004480 if (nfs4_setup_sequence(data->server,
4481 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004482 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004483 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004484 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004485 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004486}
4487
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004488static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4489{
4490 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4491 nfs4_lock_prepare(task, calldata);
4492}
4493
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004494static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4495{
4496 struct nfs4_lockdata *data = calldata;
4497
Harvey Harrison3110ff82008-05-02 13:42:44 -07004498 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004499
Trond Myklebust14516c32010-07-31 14:29:06 -04004500 if (!nfs4_sequence_done(task, &data->res.seq_res))
4501 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004502
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004503 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004504 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004505 if (data->rpc_status == 0)
4506 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4507 else
4508 goto out;
4509 }
4510 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004511 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004512 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004513 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004514 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004515out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004516 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004517}
4518
4519static void nfs4_lock_release(void *calldata)
4520{
4521 struct nfs4_lockdata *data = calldata;
4522
Harvey Harrison3110ff82008-05-02 13:42:44 -07004523 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004524 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004525 if (data->cancelled != 0) {
4526 struct rpc_task *task;
4527 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4528 data->arg.lock_seqid);
4529 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004530 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004531 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004532 } else
4533 nfs_free_seqid(data->arg.lock_seqid);
4534 nfs4_put_lock_state(data->lsp);
4535 put_nfs_open_context(data->ctx);
4536 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004537 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004538}
4539
4540static const struct rpc_call_ops nfs4_lock_ops = {
4541 .rpc_call_prepare = nfs4_lock_prepare,
4542 .rpc_call_done = nfs4_lock_done,
4543 .rpc_release = nfs4_lock_release,
4544};
4545
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004546static const struct rpc_call_ops nfs4_recover_lock_ops = {
4547 .rpc_call_prepare = nfs4_recover_lock_prepare,
4548 .rpc_call_done = nfs4_lock_done,
4549 .rpc_release = nfs4_lock_release,
4550};
4551
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004552static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4553{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004554 switch (error) {
4555 case -NFS4ERR_ADMIN_REVOKED:
4556 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004557 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004558 if (new_lock_owner != 0 ||
4559 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004560 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004561 break;
4562 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004563 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004564 case -NFS4ERR_EXPIRED:
4565 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004566 };
4567}
4568
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004569static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004570{
4571 struct nfs4_lockdata *data;
4572 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004573 struct rpc_message msg = {
4574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4575 .rpc_cred = state->owner->so_cred,
4576 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004577 struct rpc_task_setup task_setup_data = {
4578 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004579 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004580 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004581 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004582 .flags = RPC_TASK_ASYNC,
4583 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004584 int ret;
4585
Harvey Harrison3110ff82008-05-02 13:42:44 -07004586 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004587 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004588 fl->fl_u.nfs4_fl.owner,
4589 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004590 if (data == NULL)
4591 return -ENOMEM;
4592 if (IS_SETLKW(cmd))
4593 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004594 if (recovery_type > NFS_LOCK_NEW) {
4595 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004596 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004597 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4598 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004599 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004600 msg.rpc_argp = &data->arg;
4601 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004602 task_setup_data.callback_data = data;
4603 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004604 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004605 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004606 ret = nfs4_wait_for_completion_rpc_task(task);
4607 if (ret == 0) {
4608 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004609 if (ret)
4610 nfs4_handle_setlk_error(data->server, data->lsp,
4611 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004612 } else
4613 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004614 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004615 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004616 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617}
4618
4619static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4620{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004621 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004622 struct nfs4_exception exception = {
4623 .inode = state->inode,
4624 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004625 int err;
4626
4627 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004628 /* Cache the lock if possible... */
4629 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4630 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004631 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004632 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004633 break;
4634 nfs4_handle_exception(server, err, &exception);
4635 } while (exception.retry);
4636 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637}
4638
4639static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4640{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004641 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004642 struct nfs4_exception exception = {
4643 .inode = state->inode,
4644 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004645 int err;
4646
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004647 err = nfs4_set_lock_state(state, request);
4648 if (err != 0)
4649 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004650 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004651 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4652 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004653 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004654 switch (err) {
4655 default:
4656 goto out;
4657 case -NFS4ERR_GRACE:
4658 case -NFS4ERR_DELAY:
4659 nfs4_handle_exception(server, err, &exception);
4660 err = 0;
4661 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004662 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004663out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004664 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665}
4666
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004667#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004668static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004669{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004670 int status, ret = NFS_OK;
4671 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004672 struct nfs_server *server = NFS_SERVER(state->inode);
4673
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004674 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4675 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4676 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4677 if (status != NFS_OK) {
4678 nfs41_free_stateid(server, &lsp->ls_stateid);
4679 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4680 ret = status;
4681 }
4682 }
4683 };
4684
4685 return ret;
4686}
4687
4688static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4689{
4690 int status = NFS_OK;
4691
4692 if (test_bit(LK_STATE_IN_USE, &state->flags))
4693 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004694 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004695 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004696 return nfs4_lock_expired(state, request);
4697}
4698#endif
4699
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4701{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004702 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004703 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004704 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004706 if ((fl_flags & FL_POSIX) &&
4707 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4708 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004709 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004710 status = nfs4_set_lock_state(state, request);
4711 if (status != 0)
4712 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004713 request->fl_flags |= FL_ACCESS;
4714 status = do_vfs_lock(request->fl_file, request);
4715 if (status < 0)
4716 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004717 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004718 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004719 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004720 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004721 request->fl_flags = fl_flags & ~FL_SLEEP;
4722 status = do_vfs_lock(request->fl_file, request);
4723 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004724 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004725 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004726 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004727 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004728 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004729 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004730 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004731 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4732 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004733out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004734 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004735out:
4736 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 return status;
4738}
4739
4740static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4741{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004742 struct nfs4_exception exception = {
4743 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004744 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004745 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 int err;
4747
4748 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004749 err = _nfs4_proc_setlk(state, cmd, request);
4750 if (err == -NFS4ERR_DENIED)
4751 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004753 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 } while (exception.retry);
4755 return err;
4756}
4757
4758static int
4759nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4760{
4761 struct nfs_open_context *ctx;
4762 struct nfs4_state *state;
4763 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4764 int status;
4765
4766 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004767 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 state = ctx->state;
4769
4770 if (request->fl_start < 0 || request->fl_end < 0)
4771 return -EINVAL;
4772
Trond Myklebustd9531262009-07-21 19:22:38 -04004773 if (IS_GETLK(cmd)) {
4774 if (state != NULL)
4775 return nfs4_proc_getlk(state, F_GETLK, request);
4776 return 0;
4777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778
4779 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4780 return -EINVAL;
4781
Trond Myklebustd9531262009-07-21 19:22:38 -04004782 if (request->fl_type == F_UNLCK) {
4783 if (state != NULL)
4784 return nfs4_proc_unlck(state, cmd, request);
4785 return 0;
4786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
Trond Myklebustd9531262009-07-21 19:22:38 -04004788 if (state == NULL)
4789 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004790 /*
4791 * Don't rely on the VFS having checked the file open mode,
4792 * since it won't do this for flock() locks.
4793 */
4794 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4795 case F_RDLCK:
4796 if (!(filp->f_mode & FMODE_READ))
4797 return -EBADF;
4798 break;
4799 case F_WRLCK:
4800 if (!(filp->f_mode & FMODE_WRITE))
4801 return -EBADF;
4802 }
4803
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 do {
4805 status = nfs4_proc_setlk(state, cmd, request);
4806 if ((status != -EAGAIN) || IS_SETLK(cmd))
4807 break;
4808 timeout = nfs4_set_lock_task_retry(timeout);
4809 status = -ERESTARTSYS;
4810 if (signalled())
4811 break;
4812 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 return status;
4814}
4815
Trond Myklebust888e6942005-11-04 15:38:11 -05004816int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4817{
4818 struct nfs_server *server = NFS_SERVER(state->inode);
4819 struct nfs4_exception exception = { };
4820 int err;
4821
4822 err = nfs4_set_lock_state(state, fl);
4823 if (err != 0)
4824 goto out;
4825 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004826 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004827 switch (err) {
4828 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004829 printk(KERN_ERR "NFS: %s: unhandled error "
4830 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004831 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004832 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004833 goto out;
4834 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004835 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004836 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004837 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004838 nfs4_schedule_lease_recovery(server->nfs_client);
4839 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004840 case -NFS4ERR_BADSESSION:
4841 case -NFS4ERR_BADSLOT:
4842 case -NFS4ERR_BAD_HIGH_SLOT:
4843 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4844 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004845 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004846 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004847 case -ERESTARTSYS:
4848 /*
4849 * The show must go on: exit, but mark the
4850 * stateid as needing recovery.
4851 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004852 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004853 case -NFS4ERR_ADMIN_REVOKED:
4854 case -NFS4ERR_BAD_STATEID:
4855 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004856 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004857 err = 0;
4858 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004859 case -EKEYEXPIRED:
4860 /*
4861 * User RPCSEC_GSS context has expired.
4862 * We cannot recover this stateid now, so
4863 * skip it and allow recovery thread to
4864 * proceed.
4865 */
4866 err = 0;
4867 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004868 case -ENOMEM:
4869 case -NFS4ERR_DENIED:
4870 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4871 err = 0;
4872 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004873 case -NFS4ERR_DELAY:
4874 break;
4875 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004876 err = nfs4_handle_exception(server, err, &exception);
4877 } while (exception.retry);
4878out:
4879 return err;
4880}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004881
Trond Myklebustcf470c32012-03-07 13:49:12 -05004882struct nfs_release_lockowner_data {
4883 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004884 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004885 struct nfs_release_lockowner_args args;
4886};
4887
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004888static void nfs4_release_lockowner_release(void *calldata)
4889{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004890 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004891 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004892 kfree(calldata);
4893}
4894
Trond Myklebust17280172012-03-11 13:11:00 -04004895static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004896 .rpc_release = nfs4_release_lockowner_release,
4897};
4898
Trond Myklebustcf470c32012-03-07 13:49:12 -05004899int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004900{
4901 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004902 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004903 struct rpc_message msg = {
4904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4905 };
4906
4907 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004908 return -EINVAL;
4909 data = kmalloc(sizeof(*data), GFP_NOFS);
4910 if (!data)
4911 return -ENOMEM;
4912 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004913 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004914 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4915 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4916 data->args.lock_owner.s_dev = server->s_dev;
4917 msg.rpc_argp = &data->args;
4918 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4919 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004920}
4921
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004922#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4923
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004924static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4925 const void *buf, size_t buflen,
4926 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004927{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004928 if (strcmp(key, "") != 0)
4929 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004930
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004931 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004932}
4933
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004934static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4935 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004936{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004937 if (strcmp(key, "") != 0)
4938 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004939
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004940 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004941}
4942
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004943static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4944 size_t list_len, const char *name,
4945 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004946{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004947 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004948
J. Bruce Fields096455a2006-03-20 23:23:42 -05004949 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4950 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004951
4952 if (list && len <= list_len)
4953 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004954 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004955}
4956
Andy Adamson533eb462011-06-13 18:25:56 -04004957/*
4958 * nfs_fhget will use either the mounted_on_fileid or the fileid
4959 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004960static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4961{
Andy Adamson533eb462011-06-13 18:25:56 -04004962 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4963 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4964 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05004965 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004966 return;
4967
4968 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05004969 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004970 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4971 fattr->nlink = 2;
4972}
4973
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004974static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
4975 const struct qstr *name,
4976 struct nfs4_fs_locations *fs_locations,
4977 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004978{
4979 struct nfs_server *server = NFS_SERVER(dir);
4980 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004981 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004982 };
4983 struct nfs4_fs_locations_arg args = {
4984 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004985 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004986 .page = page,
4987 .bitmask = bitmask,
4988 };
Benny Halevy22958462009-04-01 09:22:02 -04004989 struct nfs4_fs_locations_res res = {
4990 .fs_locations = fs_locations,
4991 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004992 struct rpc_message msg = {
4993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4994 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004995 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004996 };
4997 int status;
4998
Harvey Harrison3110ff82008-05-02 13:42:44 -07004999 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005000
5001 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5002 * is not supported */
5003 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5004 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5005 else
5006 bitmask[0] |= FATTR4_WORD0_FILEID;
5007
Trond Myklebustc228fd32007-01-13 02:28:11 -05005008 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005009 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005010 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005011 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005012 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005013 return status;
5014}
5015
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005016int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5017 const struct qstr *name,
5018 struct nfs4_fs_locations *fs_locations,
5019 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005020{
5021 struct nfs4_exception exception = { };
5022 int err;
5023 do {
5024 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005025 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005026 &exception);
5027 } while (exception.retry);
5028 return err;
5029}
5030
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005031static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5032{
5033 int status;
5034 struct nfs4_secinfo_arg args = {
5035 .dir_fh = NFS_FH(dir),
5036 .name = name,
5037 };
5038 struct nfs4_secinfo_res res = {
5039 .flavors = flavors,
5040 };
5041 struct rpc_message msg = {
5042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5043 .rpc_argp = &args,
5044 .rpc_resp = &res,
5045 };
5046
5047 dprintk("NFS call secinfo %s\n", name->name);
5048 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5049 dprintk("NFS reply secinfo: %d\n", status);
5050 return status;
5051}
5052
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005053int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5054 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005055{
5056 struct nfs4_exception exception = { };
5057 int err;
5058 do {
5059 err = nfs4_handle_exception(NFS_SERVER(dir),
5060 _nfs4_proc_secinfo(dir, name, flavors),
5061 &exception);
5062 } while (exception.retry);
5063 return err;
5064}
5065
Andy Adamson557134a2009-04-01 09:21:53 -04005066#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005067/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005068 * Check the exchange flags returned by the server for invalid flags, having
5069 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5070 * DS flags set.
5071 */
5072static int nfs4_check_cl_exchange_flags(u32 flags)
5073{
5074 if (flags & ~EXCHGID4_FLAG_MASK_R)
5075 goto out_inval;
5076 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5077 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5078 goto out_inval;
5079 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5080 goto out_inval;
5081 return NFS_OK;
5082out_inval:
5083 return -NFS4ERR_INVAL;
5084}
5085
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005086static bool
5087nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
5088{
5089 if (a->server_scope_sz == b->server_scope_sz &&
5090 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5091 return true;
5092
5093 return false;
5094}
5095
Andy Adamson357f54d2010-12-14 10:11:57 -05005096/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005097 * nfs4_proc_exchange_id()
5098 *
5099 * Since the clientid has expired, all compounds using sessions
5100 * associated with the stale clientid will be returning
5101 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5102 * be in some phase of session reset.
5103 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005104int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005105{
5106 nfs4_verifier verifier;
5107 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005108 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005109 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005110 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005111 };
5112 struct nfs41_exchange_id_res res = {
5113 .client = clp,
5114 };
5115 int status;
5116 struct rpc_message msg = {
5117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5118 .rpc_argp = &args,
5119 .rpc_resp = &res,
5120 .rpc_cred = cred,
5121 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005122
5123 dprintk("--> %s\n", __func__);
5124 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04005125
Chuck Levercd937102012-03-02 17:14:31 -05005126 nfs4_construct_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005127
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005128 args.id_len = scnprintf(args.id, sizeof(args.id),
5129 "%s/%s.%s/%u",
5130 clp->cl_ipaddr,
5131 init_utsname()->nodename,
5132 init_utsname()->domainname,
5133 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005134
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005135 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005136 if (unlikely(!res.server_scope)) {
5137 status = -ENOMEM;
5138 goto out;
5139 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005140
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005141 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
5142 if (unlikely(!res.impl_id)) {
5143 status = -ENOMEM;
5144 goto out_server_scope;
5145 }
5146
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005147 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005148 if (!status)
5149 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005150
5151 if (!status) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005152 /* use the most recent implementation id */
5153 kfree(clp->impl_id);
5154 clp->impl_id = res.impl_id;
5155 } else
5156 kfree(res.impl_id);
5157
5158 if (!status) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005159 if (clp->server_scope &&
5160 !nfs41_same_server_scope(clp->server_scope,
5161 res.server_scope)) {
5162 dprintk("%s: server_scope mismatch detected\n",
5163 __func__);
5164 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5165 kfree(clp->server_scope);
5166 clp->server_scope = NULL;
5167 }
5168
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005169 if (!clp->server_scope) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005170 clp->server_scope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005171 goto out;
5172 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005173 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005174
5175out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005176 kfree(res.server_scope);
5177out:
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005178 if (clp->impl_id)
5179 dprintk("%s: Server Implementation ID: "
5180 "domain: %s, name: %s, date: %llu,%u\n",
5181 __func__, clp->impl_id->domain, clp->impl_id->name,
5182 clp->impl_id->date.seconds,
5183 clp->impl_id->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005184 dprintk("<-- %s status= %d\n", __func__, status);
5185 return status;
5186}
5187
Andy Adamson2050f0c2009-04-01 09:22:30 -04005188struct nfs4_get_lease_time_data {
5189 struct nfs4_get_lease_time_args *args;
5190 struct nfs4_get_lease_time_res *res;
5191 struct nfs_client *clp;
5192};
5193
5194static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5195 void *calldata)
5196{
5197 int ret;
5198 struct nfs4_get_lease_time_data *data =
5199 (struct nfs4_get_lease_time_data *)calldata;
5200
5201 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005202 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005203 /* just setup sequence, do not trigger session recovery
5204 since we're invoked within one */
5205 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005206 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005207 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005208
5209 BUG_ON(ret == -EAGAIN);
5210 rpc_call_start(task);
5211 dprintk("<-- %s\n", __func__);
5212}
5213
5214/*
5215 * Called from nfs4_state_manager thread for session setup, so don't recover
5216 * from sequence operation or clientid errors.
5217 */
5218static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5219{
5220 struct nfs4_get_lease_time_data *data =
5221 (struct nfs4_get_lease_time_data *)calldata;
5222
5223 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005224 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5225 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005226 switch (task->tk_status) {
5227 case -NFS4ERR_DELAY:
5228 case -NFS4ERR_GRACE:
5229 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5230 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5231 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005232 /* fall through */
5233 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005234 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005235 return;
5236 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005237 dprintk("<-- %s\n", __func__);
5238}
5239
Trond Myklebust17280172012-03-11 13:11:00 -04005240static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005241 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5242 .rpc_call_done = nfs4_get_lease_time_done,
5243};
5244
5245int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5246{
5247 struct rpc_task *task;
5248 struct nfs4_get_lease_time_args args;
5249 struct nfs4_get_lease_time_res res = {
5250 .lr_fsinfo = fsinfo,
5251 };
5252 struct nfs4_get_lease_time_data data = {
5253 .args = &args,
5254 .res = &res,
5255 .clp = clp,
5256 };
5257 struct rpc_message msg = {
5258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5259 .rpc_argp = &args,
5260 .rpc_resp = &res,
5261 };
5262 struct rpc_task_setup task_setup = {
5263 .rpc_client = clp->cl_rpcclient,
5264 .rpc_message = &msg,
5265 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005266 .callback_data = &data,
5267 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005268 };
5269 int status;
5270
Trond Myklebust9d12b212012-01-17 22:04:25 -05005271 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005272 dprintk("--> %s\n", __func__);
5273 task = rpc_run_task(&task_setup);
5274
5275 if (IS_ERR(task))
5276 status = PTR_ERR(task);
5277 else {
5278 status = task->tk_status;
5279 rpc_put_task(task);
5280 }
5281 dprintk("<-- %s return %d\n", __func__, status);
5282
5283 return status;
5284}
5285
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005286static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5287{
5288 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5289}
5290
5291static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5292 struct nfs4_slot *new,
5293 u32 max_slots,
5294 u32 ivalue)
5295{
5296 struct nfs4_slot *old = NULL;
5297 u32 i;
5298
5299 spin_lock(&tbl->slot_tbl_lock);
5300 if (new) {
5301 old = tbl->slots;
5302 tbl->slots = new;
5303 tbl->max_slots = max_slots;
5304 }
5305 tbl->highest_used_slotid = -1; /* no slot is currently used */
5306 for (i = 0; i < tbl->max_slots; i++)
5307 tbl->slots[i].seq_nr = ivalue;
5308 spin_unlock(&tbl->slot_tbl_lock);
5309 kfree(old);
5310}
5311
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005312/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005313 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005314 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005315static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5316 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005317{
Andy Adamson104aeba2010-01-14 17:45:10 -05005318 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005319 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005320
Andy Adamson104aeba2010-01-14 17:45:10 -05005321 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5322 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005323
Andy Adamson104aeba2010-01-14 17:45:10 -05005324 /* Does the newly negotiated max_reqs match the existing slot table? */
5325 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005326 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005327 if (!new)
5328 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005329 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005330 ret = 0;
5331
5332 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005333 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5334 tbl, tbl->slots, tbl->max_slots);
5335out:
5336 dprintk("<-- %s: return %d\n", __func__, ret);
5337 return ret;
5338}
5339
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005340/* Destroy the slot table */
5341static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5342{
5343 if (session->fc_slot_table.slots != NULL) {
5344 kfree(session->fc_slot_table.slots);
5345 session->fc_slot_table.slots = NULL;
5346 }
5347 if (session->bc_slot_table.slots != NULL) {
5348 kfree(session->bc_slot_table.slots);
5349 session->bc_slot_table.slots = NULL;
5350 }
5351 return;
5352}
5353
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005354/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005355 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005356 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005357static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005358{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005359 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005360 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005361
Andy Adamsonaacd5532011-11-09 13:58:21 -05005362 dprintk("--> %s\n", __func__);
5363 /* Fore channel */
5364 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005365 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5366 if (status) /* -ENOMEM */
5367 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005368 /* Back channel */
5369 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005370 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5371 if (status && tbl->slots == NULL)
5372 /* Fore and back channel share a connection so get
5373 * both slot tables or neither */
5374 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005375 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005376}
5377
5378struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5379{
5380 struct nfs4_session *session;
5381 struct nfs4_slot_table *tbl;
5382
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005383 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005384 if (!session)
5385 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005386
Andy Adamson557134a2009-04-01 09:21:53 -04005387 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005388 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005389 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005390 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005391 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005392
5393 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005394 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005395 spin_lock_init(&tbl->slot_tbl_lock);
5396 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005397 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005398
Trond Myklebust1055d762010-06-16 09:52:27 -04005399 session->session_state = 1<<NFS4_SESSION_INITING;
5400
Andy Adamson557134a2009-04-01 09:21:53 -04005401 session->clp = clp;
5402 return session;
5403}
5404
5405void nfs4_destroy_session(struct nfs4_session *session)
5406{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005407 struct rpc_xprt *xprt;
5408
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005409 nfs4_proc_destroy_session(session);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005410
5411 rcu_read_lock();
5412 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5413 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005414 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005415 __func__, xprt);
5416 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005417 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005418 kfree(session);
5419}
5420
Andy Adamsonfc931582009-04-01 09:22:31 -04005421/*
5422 * Initialize the values to be used by the client in CREATE_SESSION
5423 * If nfs4_init_session set the fore channel request and response sizes,
5424 * use them.
5425 *
5426 * Set the back channel max_resp_sz_cached to zero to force the client to
5427 * always set csa_cachethis to FALSE because the current implementation
5428 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5429 */
5430static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5431{
5432 struct nfs4_session *session = args->client->cl_session;
5433 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5434 mxresp_sz = session->fc_attrs.max_resp_sz;
5435
5436 if (mxrqst_sz == 0)
5437 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5438 if (mxresp_sz == 0)
5439 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5440 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005441 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5442 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005443 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005444 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005445
5446 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005447 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005448 __func__,
5449 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005450 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005451
5452 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005453 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5454 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5455 args->bc_attrs.max_resp_sz_cached = 0;
5456 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5457 args->bc_attrs.max_reqs = 1;
5458
5459 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5460 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5461 __func__,
5462 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5463 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5464 args->bc_attrs.max_reqs);
5465}
5466
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005467static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005468{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005469 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5470 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5471
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005472 if (rcvd->max_resp_sz > sent->max_resp_sz)
5473 return -EINVAL;
5474 /*
5475 * Our requested max_ops is the minimum we need; we're not
5476 * prepared to break up compounds into smaller pieces than that.
5477 * So, no point even trying to continue if the server won't
5478 * cooperate:
5479 */
5480 if (rcvd->max_ops < sent->max_ops)
5481 return -EINVAL;
5482 if (rcvd->max_reqs == 0)
5483 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005484 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5485 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005486 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005487}
5488
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005489static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5490{
5491 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5492 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005493
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005494 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5495 return -EINVAL;
5496 if (rcvd->max_resp_sz < sent->max_resp_sz)
5497 return -EINVAL;
5498 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5499 return -EINVAL;
5500 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005501 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005502 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005503 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005504 return -EINVAL;
5505 return 0;
5506}
Andy Adamson8d353012009-04-01 09:22:32 -04005507
Andy Adamson8d353012009-04-01 09:22:32 -04005508static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5509 struct nfs4_session *session)
5510{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005511 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005512
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005513 ret = nfs4_verify_fore_channel_attrs(args, session);
5514 if (ret)
5515 return ret;
5516 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005517}
5518
Andy Adamsonfc931582009-04-01 09:22:31 -04005519static int _nfs4_proc_create_session(struct nfs_client *clp)
5520{
5521 struct nfs4_session *session = clp->cl_session;
5522 struct nfs41_create_session_args args = {
5523 .client = clp,
5524 .cb_program = NFS4_CALLBACK,
5525 };
5526 struct nfs41_create_session_res res = {
5527 .client = clp,
5528 };
5529 struct rpc_message msg = {
5530 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5531 .rpc_argp = &args,
5532 .rpc_resp = &res,
5533 };
5534 int status;
5535
5536 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005537 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005538
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005539 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005540
Andy Adamson8d353012009-04-01 09:22:32 -04005541 if (!status)
5542 /* Verify the session's negotiated channel_attrs values */
5543 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005544 if (!status) {
5545 /* Increment the clientid slot sequence id */
5546 clp->cl_seqid++;
5547 }
5548
5549 return status;
5550}
5551
5552/*
5553 * Issues a CREATE_SESSION operation to the server.
5554 * It is the responsibility of the caller to verify the session is
5555 * expired before calling this routine.
5556 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005557int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005558{
5559 int status;
5560 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005561 struct nfs4_session *session = clp->cl_session;
5562
5563 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5564
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005565 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005566 if (status)
5567 goto out;
5568
Andy Adamsonaacd5532011-11-09 13:58:21 -05005569 /* Init or reset the session slot tables */
5570 status = nfs4_setup_session_slot_tables(session);
5571 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005572 if (status)
5573 goto out;
5574
5575 ptr = (unsigned *)&session->sess_id.data[0];
5576 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5577 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005578out:
5579 dprintk("<-- %s\n", __func__);
5580 return status;
5581}
5582
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005583/*
5584 * Issue the over-the-wire RPC DESTROY_SESSION.
5585 * The caller must serialize access to this routine.
5586 */
5587int nfs4_proc_destroy_session(struct nfs4_session *session)
5588{
5589 int status = 0;
5590 struct rpc_message msg;
5591
5592 dprintk("--> nfs4_proc_destroy_session\n");
5593
5594 /* session is still being setup */
5595 if (session->clp->cl_cons_state != NFS_CS_READY)
5596 return status;
5597
5598 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5599 msg.rpc_argp = session;
5600 msg.rpc_resp = NULL;
5601 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005602 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005603
5604 if (status)
5605 printk(KERN_WARNING
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -05005606 "NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005607 "Session has been destroyed regardless...\n", status);
5608
5609 dprintk("<-- nfs4_proc_destroy_session\n");
5610 return status;
5611}
5612
Trond Myklebustfccba802009-07-21 16:48:07 -04005613int nfs4_init_session(struct nfs_server *server)
5614{
5615 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005616 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005617 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005618 int ret;
5619
5620 if (!nfs4_has_session(clp))
5621 return 0;
5622
Trond Myklebust1055d762010-06-16 09:52:27 -04005623 session = clp->cl_session;
5624 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5625 return 0;
5626
Andy Adamson68bf05e2009-12-15 12:55:02 -05005627 rsize = server->rsize;
5628 if (rsize == 0)
5629 rsize = NFS_MAX_FILE_IO_SIZE;
5630 wsize = server->wsize;
5631 if (wsize == 0)
5632 wsize = NFS_MAX_FILE_IO_SIZE;
5633
Andy Adamson68bf05e2009-12-15 12:55:02 -05005634 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5635 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005636
Trond Myklebustfccba802009-07-21 16:48:07 -04005637 ret = nfs4_recover_expired_lease(server);
5638 if (!ret)
5639 ret = nfs4_check_client_ready(clp);
5640 return ret;
5641}
5642
Andy Adamsond83217c2011-03-01 01:34:17 +00005643int nfs4_init_ds_session(struct nfs_client *clp)
5644{
5645 struct nfs4_session *session = clp->cl_session;
5646 int ret;
5647
5648 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5649 return 0;
5650
5651 ret = nfs4_client_recover_expired_lease(clp);
5652 if (!ret)
5653 /* Test for the DS role */
5654 if (!is_ds_client(clp))
5655 ret = -ENODEV;
5656 if (!ret)
5657 ret = nfs4_check_client_ready(clp);
5658 return ret;
5659
5660}
5661EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5662
5663
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005664/*
5665 * Renew the cl_session lease.
5666 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005667struct nfs4_sequence_data {
5668 struct nfs_client *clp;
5669 struct nfs4_sequence_args args;
5670 struct nfs4_sequence_res res;
5671};
5672
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005673static void nfs41_sequence_release(void *data)
5674{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005675 struct nfs4_sequence_data *calldata = data;
5676 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005677
Alexandros Batsakis71358402010-02-05 03:45:05 -08005678 if (atomic_read(&clp->cl_count) > 1)
5679 nfs4_schedule_state_renewal(clp);
5680 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005681 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005682}
5683
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005684static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5685{
5686 switch(task->tk_status) {
5687 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005688 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5689 return -EAGAIN;
5690 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005691 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005692 }
5693 return 0;
5694}
5695
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005696static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005697{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005698 struct nfs4_sequence_data *calldata = data;
5699 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005700
Trond Myklebust14516c32010-07-31 14:29:06 -04005701 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5702 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005703
5704 if (task->tk_status < 0) {
5705 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005706 if (atomic_read(&clp->cl_count) == 1)
5707 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005708
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005709 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5710 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005711 return;
5712 }
5713 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005714 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005715out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005716 dprintk("<-- %s\n", __func__);
5717}
5718
5719static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5720{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005721 struct nfs4_sequence_data *calldata = data;
5722 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005723 struct nfs4_sequence_args *args;
5724 struct nfs4_sequence_res *res;
5725
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005726 args = task->tk_msg.rpc_argp;
5727 res = task->tk_msg.rpc_resp;
5728
Trond Myklebust9d12b212012-01-17 22:04:25 -05005729 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005730 return;
5731 rpc_call_start(task);
5732}
5733
5734static const struct rpc_call_ops nfs41_sequence_ops = {
5735 .rpc_call_done = nfs41_sequence_call_done,
5736 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005737 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005738};
5739
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005740static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005741{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005742 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005743 struct rpc_message msg = {
5744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5745 .rpc_cred = cred,
5746 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005747 struct rpc_task_setup task_setup_data = {
5748 .rpc_client = clp->cl_rpcclient,
5749 .rpc_message = &msg,
5750 .callback_ops = &nfs41_sequence_ops,
5751 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5752 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005753
Alexandros Batsakis71358402010-02-05 03:45:05 -08005754 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005755 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005756 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005757 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005758 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005759 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005760 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005761 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005762 msg.rpc_argp = &calldata->args;
5763 msg.rpc_resp = &calldata->res;
5764 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005765 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005766
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005767 return rpc_run_task(&task_setup_data);
5768}
5769
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005770static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005771{
5772 struct rpc_task *task;
5773 int ret = 0;
5774
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005775 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5776 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005777 task = _nfs41_proc_sequence(clp, cred);
5778 if (IS_ERR(task))
5779 ret = PTR_ERR(task);
5780 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005781 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005782 dprintk("<-- %s status=%d\n", __func__, ret);
5783 return ret;
5784}
5785
5786static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5787{
5788 struct rpc_task *task;
5789 int ret;
5790
5791 task = _nfs41_proc_sequence(clp, cred);
5792 if (IS_ERR(task)) {
5793 ret = PTR_ERR(task);
5794 goto out;
5795 }
5796 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005797 if (!ret) {
5798 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5799
5800 if (task->tk_status == 0)
5801 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005802 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005803 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005804 rpc_put_task(task);
5805out:
5806 dprintk("<-- %s status=%d\n", __func__, ret);
5807 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005808}
5809
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005810struct nfs4_reclaim_complete_data {
5811 struct nfs_client *clp;
5812 struct nfs41_reclaim_complete_args arg;
5813 struct nfs41_reclaim_complete_res res;
5814};
5815
5816static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5817{
5818 struct nfs4_reclaim_complete_data *calldata = data;
5819
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005820 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005821 if (nfs41_setup_sequence(calldata->clp->cl_session,
5822 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005823 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005824 return;
5825
5826 rpc_call_start(task);
5827}
5828
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005829static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5830{
5831 switch(task->tk_status) {
5832 case 0:
5833 case -NFS4ERR_COMPLETE_ALREADY:
5834 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5835 break;
5836 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005837 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005838 /* fall through */
5839 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005840 return -EAGAIN;
5841 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005842 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005843 }
5844 return 0;
5845}
5846
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005847static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5848{
5849 struct nfs4_reclaim_complete_data *calldata = data;
5850 struct nfs_client *clp = calldata->clp;
5851 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5852
5853 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005854 if (!nfs41_sequence_done(task, res))
5855 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005856
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005857 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5858 rpc_restart_call_prepare(task);
5859 return;
5860 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005861 dprintk("<-- %s\n", __func__);
5862}
5863
5864static void nfs4_free_reclaim_complete_data(void *data)
5865{
5866 struct nfs4_reclaim_complete_data *calldata = data;
5867
5868 kfree(calldata);
5869}
5870
5871static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5872 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5873 .rpc_call_done = nfs4_reclaim_complete_done,
5874 .rpc_release = nfs4_free_reclaim_complete_data,
5875};
5876
5877/*
5878 * Issue a global reclaim complete.
5879 */
5880static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5881{
5882 struct nfs4_reclaim_complete_data *calldata;
5883 struct rpc_task *task;
5884 struct rpc_message msg = {
5885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5886 };
5887 struct rpc_task_setup task_setup_data = {
5888 .rpc_client = clp->cl_rpcclient,
5889 .rpc_message = &msg,
5890 .callback_ops = &nfs4_reclaim_complete_call_ops,
5891 .flags = RPC_TASK_ASYNC,
5892 };
5893 int status = -ENOMEM;
5894
5895 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005896 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005897 if (calldata == NULL)
5898 goto out;
5899 calldata->clp = clp;
5900 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005901
Trond Myklebust9d12b212012-01-17 22:04:25 -05005902 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005903 msg.rpc_argp = &calldata->arg;
5904 msg.rpc_resp = &calldata->res;
5905 task_setup_data.callback_data = calldata;
5906 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005907 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005908 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005909 goto out;
5910 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005911 status = nfs4_wait_for_completion_rpc_task(task);
5912 if (status == 0)
5913 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005914 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005915 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005916out:
5917 dprintk("<-- %s status=%d\n", __func__, status);
5918 return status;
5919}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005920
5921static void
5922nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5923{
5924 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005925 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005926
5927 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005928 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5929 * right now covering the LAYOUTGET we are about to send.
5930 * However, that is not so catastrophic, and there seems
5931 * to be no way to prevent it completely.
5932 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005933 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005934 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005935 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005936 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5937 NFS_I(lgp->args.inode)->layout,
5938 lgp->args.ctx->state)) {
5939 rpc_exit(task, NFS4_OK);
5940 return;
5941 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005942 rpc_call_start(task);
5943}
5944
5945static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5946{
5947 struct nfs4_layoutget *lgp = calldata;
5948 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5949
5950 dprintk("--> %s\n", __func__);
5951
5952 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5953 return;
5954
5955 switch (task->tk_status) {
5956 case 0:
5957 break;
5958 case -NFS4ERR_LAYOUTTRYLATER:
5959 case -NFS4ERR_RECALLCONFLICT:
5960 task->tk_status = -NFS4ERR_DELAY;
5961 /* Fall through */
5962 default:
5963 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5964 rpc_restart_call_prepare(task);
5965 return;
5966 }
5967 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005968 dprintk("<-- %s\n", __func__);
5969}
5970
5971static void nfs4_layoutget_release(void *calldata)
5972{
5973 struct nfs4_layoutget *lgp = calldata;
5974
5975 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005976 put_nfs_open_context(lgp->args.ctx);
5977 kfree(calldata);
5978 dprintk("<-- %s\n", __func__);
5979}
5980
5981static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5982 .rpc_call_prepare = nfs4_layoutget_prepare,
5983 .rpc_call_done = nfs4_layoutget_done,
5984 .rpc_release = nfs4_layoutget_release,
5985};
5986
5987int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5988{
5989 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5990 struct rpc_task *task;
5991 struct rpc_message msg = {
5992 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5993 .rpc_argp = &lgp->args,
5994 .rpc_resp = &lgp->res,
5995 };
5996 struct rpc_task_setup task_setup_data = {
5997 .rpc_client = server->client,
5998 .rpc_message = &msg,
5999 .callback_ops = &nfs4_layoutget_call_ops,
6000 .callback_data = lgp,
6001 .flags = RPC_TASK_ASYNC,
6002 };
6003 int status = 0;
6004
6005 dprintk("--> %s\n", __func__);
6006
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006007 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006008 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006009 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006010 task = rpc_run_task(&task_setup_data);
6011 if (IS_ERR(task))
6012 return PTR_ERR(task);
6013 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006014 if (status == 0)
6015 status = task->tk_status;
6016 if (status == 0)
6017 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006018 rpc_put_task(task);
6019 dprintk("<-- %s status=%d\n", __func__, status);
6020 return status;
6021}
6022
Benny Halevycbe82602011-05-22 19:52:37 +03006023static void
6024nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6025{
6026 struct nfs4_layoutreturn *lrp = calldata;
6027
6028 dprintk("--> %s\n", __func__);
6029 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006030 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006031 return;
6032 rpc_call_start(task);
6033}
6034
6035static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6036{
6037 struct nfs4_layoutreturn *lrp = calldata;
6038 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006039 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006040
6041 dprintk("--> %s\n", __func__);
6042
6043 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6044 return;
6045
6046 server = NFS_SERVER(lrp->args.inode);
6047 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006048 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006049 return;
6050 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006051 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006052 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03006053 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03006054 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03006055 } else
6056 BUG_ON(!list_empty(&lo->plh_segs));
6057 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006058 lo->plh_block_lgets--;
6059 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006060 dprintk("<-- %s\n", __func__);
6061}
6062
6063static void nfs4_layoutreturn_release(void *calldata)
6064{
6065 struct nfs4_layoutreturn *lrp = calldata;
6066
6067 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006068 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006069 kfree(calldata);
6070 dprintk("<-- %s\n", __func__);
6071}
6072
6073static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6074 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6075 .rpc_call_done = nfs4_layoutreturn_done,
6076 .rpc_release = nfs4_layoutreturn_release,
6077};
6078
6079int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6080{
6081 struct rpc_task *task;
6082 struct rpc_message msg = {
6083 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6084 .rpc_argp = &lrp->args,
6085 .rpc_resp = &lrp->res,
6086 };
6087 struct rpc_task_setup task_setup_data = {
6088 .rpc_client = lrp->clp->cl_rpcclient,
6089 .rpc_message = &msg,
6090 .callback_ops = &nfs4_layoutreturn_call_ops,
6091 .callback_data = lrp,
6092 };
6093 int status;
6094
6095 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006096 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006097 task = rpc_run_task(&task_setup_data);
6098 if (IS_ERR(task))
6099 return PTR_ERR(task);
6100 status = task->tk_status;
6101 dprintk("<-- %s status=%d\n", __func__, status);
6102 rpc_put_task(task);
6103 return status;
6104}
6105
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006106/*
6107 * Retrieve the list of Data Server devices from the MDS.
6108 */
6109static int _nfs4_getdevicelist(struct nfs_server *server,
6110 const struct nfs_fh *fh,
6111 struct pnfs_devicelist *devlist)
6112{
6113 struct nfs4_getdevicelist_args args = {
6114 .fh = fh,
6115 .layoutclass = server->pnfs_curr_ld->id,
6116 };
6117 struct nfs4_getdevicelist_res res = {
6118 .devlist = devlist,
6119 };
6120 struct rpc_message msg = {
6121 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6122 .rpc_argp = &args,
6123 .rpc_resp = &res,
6124 };
6125 int status;
6126
6127 dprintk("--> %s\n", __func__);
6128 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6129 &res.seq_res, 0);
6130 dprintk("<-- %s status=%d\n", __func__, status);
6131 return status;
6132}
6133
6134int nfs4_proc_getdevicelist(struct nfs_server *server,
6135 const struct nfs_fh *fh,
6136 struct pnfs_devicelist *devlist)
6137{
6138 struct nfs4_exception exception = { };
6139 int err;
6140
6141 do {
6142 err = nfs4_handle_exception(server,
6143 _nfs4_getdevicelist(server, fh, devlist),
6144 &exception);
6145 } while (exception.retry);
6146
6147 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6148 err, devlist->num_devs);
6149
6150 return err;
6151}
6152EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6153
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006154static int
6155_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6156{
6157 struct nfs4_getdeviceinfo_args args = {
6158 .pdev = pdev,
6159 };
6160 struct nfs4_getdeviceinfo_res res = {
6161 .pdev = pdev,
6162 };
6163 struct rpc_message msg = {
6164 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6165 .rpc_argp = &args,
6166 .rpc_resp = &res,
6167 };
6168 int status;
6169
6170 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006171 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006172 dprintk("<-- %s status=%d\n", __func__, status);
6173
6174 return status;
6175}
6176
6177int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6178{
6179 struct nfs4_exception exception = { };
6180 int err;
6181
6182 do {
6183 err = nfs4_handle_exception(server,
6184 _nfs4_proc_getdeviceinfo(server, pdev),
6185 &exception);
6186 } while (exception.retry);
6187 return err;
6188}
6189EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6190
Andy Adamson863a3c62011-03-23 13:27:54 +00006191static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6192{
6193 struct nfs4_layoutcommit_data *data = calldata;
6194 struct nfs_server *server = NFS_SERVER(data->args.inode);
6195
6196 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006197 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006198 return;
6199 rpc_call_start(task);
6200}
6201
6202static void
6203nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6204{
6205 struct nfs4_layoutcommit_data *data = calldata;
6206 struct nfs_server *server = NFS_SERVER(data->args.inode);
6207
6208 if (!nfs4_sequence_done(task, &data->res.seq_res))
6209 return;
6210
6211 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006212 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6213 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6214 case -NFS4ERR_BADLAYOUT: /* no layout */
6215 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006216 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006217 break;
6218 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006219 nfs_post_op_update_inode_force_wcc(data->args.inode,
6220 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006221 break;
6222 default:
6223 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6224 rpc_restart_call_prepare(task);
6225 return;
6226 }
6227 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006228}
6229
6230static void nfs4_layoutcommit_release(void *calldata)
6231{
6232 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006233 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006234 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006235
Andy Adamsondb29c082011-07-30 20:52:38 -04006236 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006237 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006238 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6239 list_del_init(&lseg->pls_lc_list);
6240 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6241 &lseg->pls_flags))
6242 put_lseg(lseg);
6243 }
Peng Tao92407e72011-10-23 20:21:17 -07006244
6245 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6246 smp_mb__after_clear_bit();
6247 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6248
Andy Adamson863a3c62011-03-23 13:27:54 +00006249 put_rpccred(data->cred);
6250 kfree(data);
6251}
6252
6253static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6254 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6255 .rpc_call_done = nfs4_layoutcommit_done,
6256 .rpc_release = nfs4_layoutcommit_release,
6257};
6258
6259int
Andy Adamsonef311532011-03-12 02:58:10 -05006260nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006261{
6262 struct rpc_message msg = {
6263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6264 .rpc_argp = &data->args,
6265 .rpc_resp = &data->res,
6266 .rpc_cred = data->cred,
6267 };
6268 struct rpc_task_setup task_setup_data = {
6269 .task = &data->task,
6270 .rpc_client = NFS_CLIENT(data->args.inode),
6271 .rpc_message = &msg,
6272 .callback_ops = &nfs4_layoutcommit_ops,
6273 .callback_data = data,
6274 .flags = RPC_TASK_ASYNC,
6275 };
6276 struct rpc_task *task;
6277 int status = 0;
6278
6279 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6280 "lbw: %llu inode %lu\n",
6281 data->task.tk_pid, sync,
6282 data->args.lastbytewritten,
6283 data->args.inode->i_ino);
6284
Trond Myklebust9d12b212012-01-17 22:04:25 -05006285 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006286 task = rpc_run_task(&task_setup_data);
6287 if (IS_ERR(task))
6288 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006289 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006290 goto out;
6291 status = nfs4_wait_for_completion_rpc_task(task);
6292 if (status != 0)
6293 goto out;
6294 status = task->tk_status;
6295out:
6296 dprintk("%s: status %d\n", __func__, status);
6297 rpc_put_task(task);
6298 return status;
6299}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006300
6301static int
6302_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6303 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6304{
6305 struct nfs41_secinfo_no_name_args args = {
6306 .style = SECINFO_STYLE_CURRENT_FH,
6307 };
6308 struct nfs4_secinfo_res res = {
6309 .flavors = flavors,
6310 };
6311 struct rpc_message msg = {
6312 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6313 .rpc_argp = &args,
6314 .rpc_resp = &res,
6315 };
6316 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6317}
6318
6319static int
6320nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6321 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6322{
6323 struct nfs4_exception exception = { };
6324 int err;
6325 do {
6326 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6327 switch (err) {
6328 case 0:
6329 case -NFS4ERR_WRONGSEC:
6330 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006331 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006332 default:
6333 err = nfs4_handle_exception(server, err, &exception);
6334 }
6335 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006336out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006337 return err;
6338}
6339
6340static int
6341nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6342 struct nfs_fsinfo *info)
6343{
6344 int err;
6345 struct page *page;
6346 rpc_authflavor_t flavor;
6347 struct nfs4_secinfo_flavors *flavors;
6348
6349 page = alloc_page(GFP_KERNEL);
6350 if (!page) {
6351 err = -ENOMEM;
6352 goto out;
6353 }
6354
6355 flavors = page_address(page);
6356 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6357
6358 /*
6359 * Fall back on "guess and check" method if
6360 * the server doesn't support SECINFO_NO_NAME
6361 */
6362 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6363 err = nfs4_find_root_sec(server, fhandle, info);
6364 goto out_freepage;
6365 }
6366 if (err)
6367 goto out_freepage;
6368
6369 flavor = nfs_find_best_sec(flavors);
6370 if (err == 0)
6371 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6372
6373out_freepage:
6374 put_page(page);
6375 if (err == -EACCES)
6376 return -EPERM;
6377out:
6378 return err;
6379}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006380
6381static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006382{
6383 int status;
6384 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006385 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006386 };
6387 struct nfs41_test_stateid_res res;
6388 struct rpc_message msg = {
6389 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6390 .rpc_argp = &args,
6391 .rpc_resp = &res,
6392 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006393
Trond Myklebust9d12b212012-01-17 22:04:25 -05006394 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006395 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6396
6397 if (status == NFS_OK)
6398 return res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006399 return status;
6400}
6401
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006402static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006403{
6404 struct nfs4_exception exception = { };
6405 int err;
6406 do {
6407 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006408 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006409 &exception);
6410 } while (exception.retry);
6411 return err;
6412}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006413
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006414static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006415{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006416 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006417 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006418 };
6419 struct nfs41_free_stateid_res res;
6420 struct rpc_message msg = {
6421 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6422 .rpc_argp = &args,
6423 .rpc_resp = &res,
6424 };
6425
Trond Myklebust9d12b212012-01-17 22:04:25 -05006426 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6427 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006428}
6429
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006430static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006431{
6432 struct nfs4_exception exception = { };
6433 int err;
6434 do {
6435 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006436 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006437 &exception);
6438 } while (exception.retry);
6439 return err;
6440}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006441
6442static bool nfs41_match_stateid(const nfs4_stateid *s1,
6443 const nfs4_stateid *s2)
6444{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006445 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006446 return false;
6447
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006448 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006449 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006450 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006451 return true;
6452
6453 return false;
6454}
6455
Andy Adamson557134a2009-04-01 09:21:53 -04006456#endif /* CONFIG_NFS_V4_1 */
6457
Trond Myklebust36281ca2012-03-04 18:13:56 -05006458static bool nfs4_match_stateid(const nfs4_stateid *s1,
6459 const nfs4_stateid *s2)
6460{
Trond Myklebustf597c532012-03-04 18:13:56 -05006461 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006462}
6463
6464
Trond Myklebust17280172012-03-11 13:11:00 -04006465static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006466 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006467 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468 .recover_open = nfs4_open_reclaim,
6469 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006470 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006471 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472};
6473
Andy Adamson591d71c2009-04-01 09:22:47 -04006474#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006475static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006476 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6477 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6478 .recover_open = nfs4_open_reclaim,
6479 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006480 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006481 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006482 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006483};
6484#endif /* CONFIG_NFS_V4_1 */
6485
Trond Myklebust17280172012-03-11 13:11:00 -04006486static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006487 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006488 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489 .recover_open = nfs4_open_expired,
6490 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006491 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006492 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493};
6494
Andy Adamson591d71c2009-04-01 09:22:47 -04006495#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006496static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006497 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6498 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006499 .recover_open = nfs41_open_expired,
6500 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006501 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006502 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006503};
6504#endif /* CONFIG_NFS_V4_1 */
6505
Trond Myklebust17280172012-03-11 13:11:00 -04006506static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006507 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006508 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006509 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006510};
6511
6512#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006513static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006514 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006515 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006516 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006517};
6518#endif
6519
Trond Myklebust97dc1352010-06-16 09:52:26 -04006520static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6521 .minor_version = 0,
6522 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006523 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006524 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006525 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6526 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6527 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006528};
6529
6530#if defined(CONFIG_NFS_V4_1)
6531static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6532 .minor_version = 1,
6533 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006534 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006535 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006536 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6537 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6538 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006539};
6540#endif
6541
Trond Myklebust97dc1352010-06-16 09:52:26 -04006542const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6543 [0] = &nfs_v4_0_minor_ops,
6544#if defined(CONFIG_NFS_V4_1)
6545 [1] = &nfs_v4_1_minor_ops,
6546#endif
6547};
6548
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006549static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006550 .permission = nfs_permission,
6551 .getattr = nfs_getattr,
6552 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006553 .getxattr = generic_getxattr,
6554 .setxattr = generic_setxattr,
6555 .listxattr = generic_listxattr,
6556 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006557};
6558
David Howells509de812006-08-22 20:06:11 -04006559const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 .version = 4, /* protocol version */
6561 .dentry_ops = &nfs4_dentry_operations,
6562 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006563 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006564 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006566 .submount = nfs4_submount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567 .getattr = nfs4_proc_getattr,
6568 .setattr = nfs4_proc_setattr,
6569 .lookup = nfs4_proc_lookup,
6570 .access = nfs4_proc_access,
6571 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 .create = nfs4_proc_create,
6573 .remove = nfs4_proc_remove,
6574 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006575 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576 .unlink_done = nfs4_proc_unlink_done,
6577 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006578 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006579 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006580 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 .link = nfs4_proc_link,
6582 .symlink = nfs4_proc_symlink,
6583 .mkdir = nfs4_proc_mkdir,
6584 .rmdir = nfs4_proc_remove,
6585 .readdir = nfs4_proc_readdir,
6586 .mknod = nfs4_proc_mknod,
6587 .statfs = nfs4_proc_statfs,
6588 .fsinfo = nfs4_proc_fsinfo,
6589 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006590 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591 .decode_dirent = nfs4_decode_dirent,
6592 .read_setup = nfs4_proc_read_setup,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006593 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006594 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595 .write_setup = nfs4_proc_write_setup,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006596 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006597 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006599 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006600 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006602 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006603 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006604 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006605 .init_client = nfs4_init_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606};
6607
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006608static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6609 .prefix = XATTR_NAME_NFSV4_ACL,
6610 .list = nfs4_xattr_list_nfs4_acl,
6611 .get = nfs4_xattr_get_nfs4_acl,
6612 .set = nfs4_xattr_set_nfs4_acl,
6613};
6614
6615const struct xattr_handler *nfs4_xattr_handlers[] = {
6616 &nfs4_xattr_nfs4_acl_handler,
6617 NULL
6618};
6619
Trond Myklebustef159e92012-02-06 19:50:40 -05006620module_param(max_session_slots, ushort, 0644);
6621MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6622 "requests the client will negotiate");
6623
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624/*
6625 * Local variables:
6626 * c-basic-offset: 8
6627 * End:
6628 */