blob: 720fa0b2fa4820b3658ac28b827069272da33027 [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 Myklebust27b0a532012-05-28 11:36:28 -0400104 case -NFS4ERR_SHARE_DENIED:
105 return -EACCES;
Trond Myklebust52567b02009-10-23 14:46:42 -0400106 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700108 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400109 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400111 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114/*
115 * This is our standard bitmap for GETATTR requests.
116 */
117const u32 nfs4_fattr_bitmap[2] = {
118 FATTR4_WORD0_TYPE
119 | FATTR4_WORD0_CHANGE
120 | FATTR4_WORD0_SIZE
121 | FATTR4_WORD0_FSID
122 | FATTR4_WORD0_FILEID,
123 FATTR4_WORD1_MODE
124 | FATTR4_WORD1_NUMLINKS
125 | FATTR4_WORD1_OWNER
126 | FATTR4_WORD1_OWNER_GROUP
127 | FATTR4_WORD1_RAWDEV
128 | FATTR4_WORD1_SPACE_USED
129 | FATTR4_WORD1_TIME_ACCESS
130 | FATTR4_WORD1_TIME_METADATA
131 | FATTR4_WORD1_TIME_MODIFY
132};
133
134const u32 nfs4_statfs_bitmap[2] = {
135 FATTR4_WORD0_FILES_AVAIL
136 | FATTR4_WORD0_FILES_FREE
137 | FATTR4_WORD0_FILES_TOTAL,
138 FATTR4_WORD1_SPACE_AVAIL
139 | FATTR4_WORD1_SPACE_FREE
140 | FATTR4_WORD1_SPACE_TOTAL
141};
142
Trond Myklebust4ce79712005-06-22 17:16:21 +0000143const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 FATTR4_WORD0_MAXLINK
145 | FATTR4_WORD0_MAXNAME,
146 0
147};
148
Fred Isamandae100c2011-07-30 20:52:37 -0400149const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 | FATTR4_WORD0_MAXREAD
151 | FATTR4_WORD0_MAXWRITE
152 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700153 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400154 | FATTR4_WORD1_FS_LAYOUT_TYPES,
155 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
Manoj Naik830b8e32006-06-09 09:34:25 -0400158const u32 nfs4_fs_locations_bitmap[2] = {
159 FATTR4_WORD0_TYPE
160 | FATTR4_WORD0_CHANGE
161 | FATTR4_WORD0_SIZE
162 | FATTR4_WORD0_FSID
163 | FATTR4_WORD0_FILEID
164 | FATTR4_WORD0_FS_LOCATIONS,
165 FATTR4_WORD1_MODE
166 | FATTR4_WORD1_NUMLINKS
167 | FATTR4_WORD1_OWNER
168 | FATTR4_WORD1_OWNER_GROUP
169 | FATTR4_WORD1_RAWDEV
170 | FATTR4_WORD1_SPACE_USED
171 | FATTR4_WORD1_TIME_ACCESS
172 | FATTR4_WORD1_TIME_METADATA
173 | FATTR4_WORD1_TIME_MODIFY
174 | FATTR4_WORD1_MOUNTED_ON_FILEID
175};
176
Al Virobc4785c2006-10-19 23:28:51 -0700177static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 struct nfs4_readdir_arg *readdir)
179{
Al Viro0dbb4c62006-10-19 23:28:49 -0700180 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 BUG_ON(readdir->count < 80);
183 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000184 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
186 return;
187 }
188
189 readdir->cookie = 0;
190 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
191 if (cookie == 2)
192 return;
193
194 /*
195 * NFSv4 servers do not return entries for '.' and '..'
196 * Therefore, we fake these entries here. We let '.'
197 * have cookie 0 and '..' have cookie 1. Note that
198 * when talking to the server, we always send cookie 0
199 * instead of 1 or 2.
200 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800201 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 if (cookie == 0) {
204 *p++ = xdr_one; /* next */
205 *p++ = xdr_zero; /* cookie, first word */
206 *p++ = xdr_one; /* cookie, second word */
207 *p++ = xdr_one; /* entry len */
208 memcpy(p, ".\0\0\0", 4); /* entry */
209 p++;
210 *p++ = xdr_one; /* bitmap length */
211 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
212 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400213 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215
216 *p++ = xdr_one; /* next */
217 *p++ = xdr_zero; /* cookie, first word */
218 *p++ = xdr_two; /* cookie, second word */
219 *p++ = xdr_two; /* entry len */
220 memcpy(p, "..\0\0", 4); /* entry */
221 p++;
222 *p++ = xdr_one; /* bitmap length */
223 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
224 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400225 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 readdir->pgbase = (char *)p - (char *)start;
228 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800229 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Trond Myklebust65de8722008-12-23 15:21:44 -0500232static int nfs4_wait_clnt_recover(struct nfs_client *clp)
233{
234 int res;
235
236 might_sleep();
237
Trond Myklebuste005e802008-12-23 15:21:48 -0500238 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400239 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500240 return res;
241}
242
243static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
244{
245 int res = 0;
246
247 might_sleep();
248
249 if (*timeout <= 0)
250 *timeout = NFS4_POLL_RETRY_MIN;
251 if (*timeout > NFS4_POLL_RETRY_MAX)
252 *timeout = NFS4_POLL_RETRY_MAX;
Colin Cross3be91672013-05-06 23:50:06 +0000253 freezable_schedule_timeout_killable_unsafe(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500254 if (fatal_signal_pending(current))
255 res = -ERESTARTSYS;
256 *timeout <<= 1;
257 return res;
258}
259
260/* This is the error handling routine for processes that are allowed
261 * to sleep.
262 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800263static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500264{
265 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500266 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500267 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500268 int ret = errorcode;
269
270 exception->retry = 0;
271 switch(errorcode) {
272 case 0:
273 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500274 case -NFS4ERR_OPENMODE:
Trond Myklebust14977482012-03-27 18:31:25 -0400275 if (inode && nfs_have_delegation(inode, FMODE_READ)) {
Trond Myklebust3114ea72012-03-07 16:39:06 -0500276 nfs_inode_return_delegation(inode);
277 exception->retry = 1;
278 return 0;
279 }
280 if (state == NULL)
281 break;
282 nfs4_schedule_stateid_recovery(server, state);
283 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500284 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500285 case -NFS4ERR_ADMIN_REVOKED:
286 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500287 if (state == NULL)
288 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400289 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500290 nfs4_schedule_stateid_recovery(server, state);
291 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400292 case -NFS4ERR_EXPIRED:
293 if (state != NULL)
294 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500295 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500296 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500297 nfs4_schedule_lease_recovery(clp);
298 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500299#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400300 case -NFS4ERR_BADSESSION:
301 case -NFS4ERR_BADSLOT:
302 case -NFS4ERR_BAD_HIGH_SLOT:
303 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
304 case -NFS4ERR_DEADSESSION:
305 case -NFS4ERR_SEQ_FALSE_RETRY:
306 case -NFS4ERR_SEQ_MISORDERED:
307 dprintk("%s ERROR: %d Reset session\n", __func__,
308 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500309 nfs4_schedule_session_recovery(clp->cl_session);
Bryan Schumaker824904c2012-10-30 16:06:35 -0400310 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500311#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500312 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500313 if (exception->timeout > HZ) {
314 /* We have retried a decent amount, time to
315 * fail
316 */
317 ret = -EBUSY;
318 break;
319 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500320 case -NFS4ERR_GRACE:
321 case -NFS4ERR_DELAY:
322 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 Halevydfb4f302010-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 Halevydfb4f302010-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 Halevydfb4f302010-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 Halevydfb4f302010-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 Halevydfb4f302010-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 Halevydfb4f302010-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 Halevydfb4f302010-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 Halevydfb4f302010-09-24 09:17:01 -0400639 __func__, session->clp, session, res->sr_slot ?
640 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400641
Trond Myklebust9d12b212012-01-17 22:04:25 -0500642 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400643out:
644 dprintk("<-- %s status=%d\n", __func__, ret);
645 return ret;
646}
647
648struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400649 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400650 struct nfs4_sequence_args *seq_args;
651 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400652};
653
654static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
655{
656 struct nfs41_call_sync_data *data = calldata;
657
Trond Myklebust035168a2010-06-16 09:52:26 -0400658 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
659
660 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500661 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400662 return;
663 rpc_call_start(task);
664}
665
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800666static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
667{
668 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
669 nfs41_call_sync_prepare(task, calldata);
670}
671
Andy Adamson69ab40c2009-04-01 09:22:19 -0400672static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
673{
674 struct nfs41_call_sync_data *data = calldata;
675
Trond Myklebust14516c32010-07-31 14:29:06 -0400676 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400677}
678
Trond Myklebust17280172012-03-11 13:11:00 -0400679static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400680 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400681 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400682};
683
Trond Myklebust17280172012-03-11 13:11:00 -0400684static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800685 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
686 .rpc_call_done = nfs41_call_sync_done,
687};
688
Bryan Schumaker7c513052011-03-24 17:12:24 +0000689static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
690 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400691 struct rpc_message *msg,
692 struct nfs4_sequence_args *args,
693 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800694 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400695{
696 int ret;
697 struct rpc_task *task;
698 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400699 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400700 .seq_args = args,
701 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400702 };
703 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000704 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400705 .rpc_message = msg,
706 .callback_ops = &nfs41_call_sync_ops,
707 .callback_data = &data
708 };
709
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800710 if (privileged)
711 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400712 task = rpc_run_task(&task_setup);
713 if (IS_ERR(task))
714 ret = PTR_ERR(task);
715 else {
716 ret = task->tk_status;
717 rpc_put_task(task);
718 }
719 return ret;
720}
721
Bryan Schumaker7c513052011-03-24 17:12:24 +0000722int _nfs4_call_sync_session(struct rpc_clnt *clnt,
723 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400724 struct rpc_message *msg,
725 struct nfs4_sequence_args *args,
726 struct nfs4_sequence_res *res,
727 int cache_reply)
728{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500729 nfs41_init_sequence(args, res, cache_reply);
730 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400731}
732
Trond Myklebustdf896452010-06-16 09:52:26 -0400733#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500734static inline
735void nfs41_init_sequence(struct nfs4_sequence_args *args,
736 struct nfs4_sequence_res *res, int cache_reply)
737{
738}
739
Trond Myklebust14516c32010-07-31 14:29:06 -0400740static int nfs4_sequence_done(struct rpc_task *task,
741 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400742{
Trond Myklebust14516c32010-07-31 14:29:06 -0400743 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400744}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400745#endif /* CONFIG_NFS_V4_1 */
746
Bryan Schumaker7c513052011-03-24 17:12:24 +0000747int _nfs4_call_sync(struct rpc_clnt *clnt,
748 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400749 struct rpc_message *msg,
750 struct nfs4_sequence_args *args,
751 struct nfs4_sequence_res *res,
752 int cache_reply)
753{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500754 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000755 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400756}
757
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000758static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000759int nfs4_call_sync(struct rpc_clnt *clnt,
760 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000761 struct rpc_message *msg,
762 struct nfs4_sequence_args *args,
763 struct nfs4_sequence_res *res,
764 int cache_reply)
765{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000766 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
767 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000768}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400769
Trond Myklebust38478b22006-05-25 01:40:57 -0400770static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Trond Myklebust38478b22006-05-25 01:40:57 -0400772 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Trond Myklebust38478b22006-05-25 01:40:57 -0400774 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400775 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700776 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400777 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700778 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400779 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100782struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400783 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100784 struct nfs_openargs o_arg;
785 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100786 struct nfs_open_confirmargs c_arg;
787 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500788 struct nfs4_string owner_name;
789 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100790 struct nfs_fattr f_attr;
791 struct nfs_fattr dir_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100792 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400793 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100794 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400795 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100796 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100797 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400798 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100799 int rpc_status;
800 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100801};
802
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400803
804static void nfs4_init_opendata_res(struct nfs4_opendata *p)
805{
806 p->o_res.f_attr = &p->f_attr;
807 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400808 p->o_res.seqid = p->o_arg.seqid;
809 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400810 p->o_res.server = p->o_arg.server;
811 nfs_fattr_init(&p->f_attr);
812 nfs_fattr_init(&p->dir_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500813 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400814}
815
Al Viro82a2c1b2011-06-22 18:30:55 -0400816static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500817 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400818 const struct iattr *attrs,
819 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100820{
Al Viro82a2c1b2011-06-22 18:30:55 -0400821 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100822 struct inode *dir = parent->d_inode;
823 struct nfs_server *server = NFS_SERVER(dir);
824 struct nfs4_opendata *p;
825
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400826 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100827 if (p == NULL)
828 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400829 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100830 if (p->o_arg.seqid == NULL)
831 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400832 nfs_sb_active(dentry->d_sb);
833 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100834 p->dir = parent;
835 p->owner = sp;
836 atomic_inc(&sp->so_count);
837 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500838 p->o_arg.open_flags = flags;
839 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400840 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400841 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
842 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400843 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100844 p->o_arg.server = server;
845 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500846 p->o_arg.dir_bitmask = server->cache_consistency_bitmask;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100847 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500848 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500849 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400850
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100851 p->o_arg.u.attrs = &p->attrs;
852 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500853
854 verf[0] = jiffies;
855 verf[1] = current->pid;
856 memcpy(p->o_arg.u.verifier.data, verf,
857 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100858 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100859 p->c_arg.fh = &p->o_res.fh;
860 p->c_arg.stateid = &p->o_res.stateid;
861 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400862 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400863 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100864 return p;
865err_free:
866 kfree(p);
867err:
868 dput(parent);
869 return NULL;
870}
871
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400872static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100873{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400874 struct nfs4_opendata *p = container_of(kref,
875 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400876 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400877
878 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400879 if (p->state != NULL)
880 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400881 nfs4_put_state_owner(p->owner);
882 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400883 dput(p->dentry);
884 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500885 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400886 kfree(p);
887}
888
889static void nfs4_opendata_put(struct nfs4_opendata *p)
890{
891 if (p != NULL)
892 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100893}
894
Trond Myklebust06f814a2006-01-03 09:55:07 +0100895static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
896{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100897 int ret;
898
Trond Myklebust06f814a2006-01-03 09:55:07 +0100899 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100900 return ret;
901}
902
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400904{
905 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500906
Trond Myklebust536e43d2012-01-17 22:04:26 -0500907 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908 goto out;
909 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400910 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500911 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
912 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400913 break;
914 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500915 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
916 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400917 break;
918 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500919 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
920 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400921 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400923 return ret;
924}
925
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500926static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400927{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500928 if (delegation == NULL)
929 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500930 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400931 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500932 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400933 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500934 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400935 return 1;
936}
937
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500938static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100939{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100941 case FMODE_WRITE:
942 state->n_wronly++;
943 break;
944 case FMODE_READ:
945 state->n_rdonly++;
946 break;
947 case FMODE_READ|FMODE_WRITE:
948 state->n_rdwr++;
949 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500950 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100951}
952
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500953static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400954{
955 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500956 nfs4_stateid_copy(&state->stateid, stateid);
957 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500958 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400959 case FMODE_READ:
960 set_bit(NFS_O_RDONLY_STATE, &state->flags);
961 break;
962 case FMODE_WRITE:
963 set_bit(NFS_O_WRONLY_STATE, &state->flags);
964 break;
965 case FMODE_READ|FMODE_WRITE:
966 set_bit(NFS_O_RDWR_STATE, &state->flags);
967 }
968}
969
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500970static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400971{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400972 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400974 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400975}
976
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400979 /*
980 * Protect the call to nfs4_state_set_mode_locked and
981 * serialise the stateid update
982 */
983 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400984 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500985 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400986 set_bit(NFS_DELEGATED_STATE, &state->flags);
987 }
988 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400990 write_sequnlock(&state->seqlock);
991 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700993 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500997{
998 struct nfs_inode *nfsi = NFS_I(state->inode);
999 struct nfs_delegation *deleg_cur;
1000 int ret = 0;
1001
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001002 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001003
1004 rcu_read_lock();
1005 deleg_cur = rcu_dereference(nfsi->delegation);
1006 if (deleg_cur == NULL)
1007 goto no_delegation;
1008
1009 spin_lock(&deleg_cur->lock);
1010 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001012 goto no_delegation_unlock;
1013
1014 if (delegation == NULL)
1015 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001016 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001017 goto no_delegation_unlock;
1018
Trond Myklebustb7391f42008-12-23 15:21:52 -05001019 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001020 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001021 ret = 1;
1022no_delegation_unlock:
1023 spin_unlock(&deleg_cur->lock);
1024no_delegation:
1025 rcu_read_unlock();
1026
1027 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001028 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001029 ret = 1;
1030 }
1031
1032 return ret;
1033}
1034
1035
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001036static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001037{
1038 struct nfs_delegation *delegation;
1039
1040 rcu_read_lock();
1041 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001042 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001043 rcu_read_unlock();
1044 return;
1045 }
1046 rcu_read_unlock();
1047 nfs_inode_return_delegation(inode);
1048}
1049
Trond Myklebust6ee41262007-07-08 14:11:36 -04001050static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001051{
1052 struct nfs4_state *state = opendata->state;
1053 struct nfs_inode *nfsi = NFS_I(state->inode);
1054 struct nfs_delegation *delegation;
Trond Myklebust19091a72013-05-29 15:36:40 -04001055 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001056 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001057 nfs4_stateid stateid;
1058 int ret = -EAGAIN;
1059
Trond Myklebustaac00a82007-07-05 19:02:21 -04001060 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001061 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001062 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001063 if (can_open_cached(state, fmode, open_mode)) {
1064 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001065 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001066 goto out_return_state;
1067 }
1068 spin_unlock(&state->owner->so_lock);
1069 }
Trond Myklebust34310432008-12-23 15:21:38 -05001070 rcu_read_lock();
1071 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001072 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001073 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001074 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001075 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001076 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001077 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001078 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001079 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001080 if (ret != 0)
1081 goto out;
1082 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001083
1084 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001085 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001086 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001087 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001088out:
1089 return ERR_PTR(ret);
1090out_return_state:
1091 atomic_inc(&state->count);
1092 return state;
1093}
1094
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001095static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1096{
1097 struct inode *inode;
1098 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001099 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001100 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001101
Trond Myklebustaac00a82007-07-05 19:02:21 -04001102 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001103 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001104 goto out;
1105 }
1106
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001107 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001108 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001109 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001110 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001111 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001112 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001113 goto err;
1114 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001115 state = nfs4_get_open_state(inode, data->owner);
1116 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001117 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001118 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001119 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001120 int delegation_flags = 0;
1121
Trond Myklebust003707c2007-07-05 18:07:55 -04001122 rcu_read_lock();
1123 delegation = rcu_dereference(NFS_I(inode)->delegation);
1124 if (delegation)
1125 delegation_flags = delegation->flags;
1126 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001127 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1128 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1129 "returning a delegation for "
1130 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001131 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001132 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001133 nfs_inode_set_delegation(state->inode,
1134 data->owner->so_cred,
1135 &data->o_res);
1136 else
1137 nfs_inode_reclaim_delegation(state->inode,
1138 data->owner->so_cred,
1139 &data->o_res);
1140 }
Trond Myklebust34310432008-12-23 15:21:38 -05001141
1142 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001143 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001144 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001145out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001146 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001147err_put_inode:
1148 iput(inode);
1149err:
1150 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001151}
1152
Trond Myklebust864472e2006-01-03 09:55:15 +01001153static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1154{
1155 struct nfs_inode *nfsi = NFS_I(state->inode);
1156 struct nfs_open_context *ctx;
1157
1158 spin_lock(&state->inode->i_lock);
1159 list_for_each_entry(ctx, &nfsi->open_files, list) {
1160 if (ctx->state != state)
1161 continue;
1162 get_nfs_open_context(ctx);
1163 spin_unlock(&state->inode->i_lock);
1164 return ctx;
1165 }
1166 spin_unlock(&state->inode->i_lock);
1167 return ERR_PTR(-ENOENT);
1168}
1169
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001170static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1171{
1172 struct nfs4_opendata *opendata;
1173
Al Viro3d4ff432011-06-22 18:40:12 -04001174 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001175 if (opendata == NULL)
1176 return ERR_PTR(-ENOMEM);
1177 opendata->state = state;
1178 atomic_inc(&state->count);
1179 return opendata;
1180}
1181
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001182static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001183{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001184 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001185 int ret;
1186
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001187 opendata->o_arg.open_flags = 0;
1188 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001189 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1190 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1191 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001192 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001193 if (ret != 0)
1194 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001195 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001196 if (IS_ERR(newstate))
1197 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001198 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001199 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001200 return 0;
1201}
1202
1203static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1204{
Trond Myklebust864472e2006-01-03 09:55:15 +01001205 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001206 int ret;
1207
1208 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001209 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001210 smp_rmb();
1211 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001212 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001213 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001214 if (ret != 0)
1215 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001216 if (newstate != state)
1217 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001218 }
1219 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001220 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001221 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001222 if (ret != 0)
1223 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001224 if (newstate != state)
1225 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001226 }
1227 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001228 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001229 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001230 if (ret != 0)
1231 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001232 if (newstate != state)
1233 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001234 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001235 /*
1236 * We may have performed cached opens for all three recoveries.
1237 * Check if we need to update the current stateid.
1238 */
1239 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001240 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001241 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001242 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001243 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001244 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001245 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001246 return 0;
1247}
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249/*
1250 * OPEN_RECLAIM:
1251 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001253static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001255 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001256 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001257 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 int status;
1259
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001260 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1261 if (IS_ERR(opendata))
1262 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001263 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1264 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001265 rcu_read_lock();
1266 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001267 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001268 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001269 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001270 opendata->o_arg.u.delegation_type = delegation_type;
1271 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001272 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return status;
1274}
1275
Trond Myklebust539cd032007-06-05 11:46:42 -04001276static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 struct nfs_server *server = NFS_SERVER(state->inode);
1279 struct nfs4_exception exception = { };
1280 int err;
1281 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001282 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001283 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001284 break;
1285 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 } while (exception.retry);
1287 return err;
1288}
1289
Trond Myklebust864472e2006-01-03 09:55:15 +01001290static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1291{
1292 struct nfs_open_context *ctx;
1293 int ret;
1294
1295 ctx = nfs4_state_find_open_context(state);
1296 if (IS_ERR(ctx))
1297 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001298 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001299 put_nfs_open_context(ctx);
1300 return ret;
1301}
1302
Trond Myklebust13437e12007-07-06 15:10:43 -04001303static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001305 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001306 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001308 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1309 if (IS_ERR(opendata))
1310 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001311 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001312 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001313 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001314 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001315 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
Trond Myklebust13437e12007-07-06 15:10:43 -04001318int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001321 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 int err;
1323 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001324 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 switch (err) {
1326 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001327 case -ENOENT:
1328 case -ESTALE:
1329 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001330 case -NFS4ERR_BADSESSION:
1331 case -NFS4ERR_BADSLOT:
1332 case -NFS4ERR_BAD_HIGH_SLOT:
1333 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1334 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001335 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001336 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 case -NFS4ERR_STALE_CLIENTID:
1338 case -NFS4ERR_STALE_STATEID:
1339 case -NFS4ERR_EXPIRED:
1340 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001341 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001342 goto out;
1343 case -ERESTARTSYS:
1344 /*
1345 * The show must go on: exit, but mark the
1346 * stateid as needing recovery.
1347 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001348 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001349 case -NFS4ERR_ADMIN_REVOKED:
1350 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001351 nfs_inode_find_state_and_recover(state->inode,
1352 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001353 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001354 case -ENOMEM:
1355 err = 0;
1356 goto out;
Trond Myklebusted9a34c2013-04-01 15:34:05 -04001357 case -NFS4ERR_DELAY:
1358 case -NFS4ERR_GRACE:
1359 set_bit(NFS_DELEGATED_STATE, &state->flags);
1360 ssleep(1);
1361 err = -EAGAIN;
1362 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
1364 err = nfs4_handle_exception(server, err, &exception);
1365 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001366out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return err;
1368}
1369
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001370static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1371{
1372 struct nfs4_opendata *data = calldata;
1373
1374 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001375 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001376 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001377 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001378 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001379 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001380 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001381}
1382
1383static void nfs4_open_confirm_release(void *calldata)
1384{
1385 struct nfs4_opendata *data = calldata;
1386 struct nfs4_state *state = NULL;
1387
1388 /* If this request hasn't been cancelled, do nothing */
1389 if (data->cancelled == 0)
1390 goto out_free;
1391 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001392 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001393 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001394 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001395 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001396 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001397out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001398 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001399}
1400
1401static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001402 .rpc_call_done = nfs4_open_confirm_done,
1403 .rpc_release = nfs4_open_confirm_release,
1404};
1405
1406/*
1407 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1408 */
1409static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1410{
1411 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1412 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001413 struct rpc_message msg = {
1414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1415 .rpc_argp = &data->c_arg,
1416 .rpc_resp = &data->c_res,
1417 .rpc_cred = data->owner->so_cred,
1418 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001419 struct rpc_task_setup task_setup_data = {
1420 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001421 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001422 .callback_ops = &nfs4_open_confirm_ops,
1423 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001424 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001425 .flags = RPC_TASK_ASYNC,
1426 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 int status;
1428
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001429 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001430 data->rpc_done = 0;
1431 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001432 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001433 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001434 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001435 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001436 status = nfs4_wait_for_completion_rpc_task(task);
1437 if (status != 0) {
1438 data->cancelled = 1;
1439 smp_wmb();
1440 } else
1441 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001442 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return status;
1444}
1445
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001446static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001448 struct nfs4_opendata *data = calldata;
1449 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001450
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001451 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1452 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001453 /*
1454 * Check if we still need to send an OPEN call, or if we can use
1455 * a delegation instead.
1456 */
1457 if (data->state != NULL) {
1458 struct nfs_delegation *delegation;
1459
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001460 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001461 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001462 rcu_read_lock();
1463 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001464 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1465 can_open_delegated(delegation, data->o_arg.fmode))
1466 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001467 rcu_read_unlock();
1468 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001469 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001470 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001471 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001472 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001473 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1474 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001475 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001476 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001477 &data->o_arg.seq_args,
Trond Myklebust0ada2102012-10-29 18:37:40 -04001478 &data->o_res.seq_res,
1479 task) != 0)
1480 nfs_release_seqid(data->o_arg.seqid);
1481 else
1482 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001483 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001484unlock_no_action:
1485 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001486out_no_action:
1487 task->tk_action = NULL;
1488
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001489}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001491static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1492{
1493 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1494 nfs4_open_prepare(task, calldata);
1495}
1496
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001497static void nfs4_open_done(struct rpc_task *task, void *calldata)
1498{
1499 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001501 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001502
Trond Myklebust14516c32010-07-31 14:29:06 -04001503 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1504 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001505
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001506 if (task->tk_status == 0) {
1507 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001508 case S_IFREG:
1509 break;
1510 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001511 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001512 break;
1513 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001514 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001515 break;
1516 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001517 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001518 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001519 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001520 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1521 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001522 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001523 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001524}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001525
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001526static void nfs4_open_release(void *calldata)
1527{
1528 struct nfs4_opendata *data = calldata;
1529 struct nfs4_state *state = NULL;
1530
1531 /* If this request hasn't been cancelled, do nothing */
1532 if (data->cancelled == 0)
1533 goto out_free;
1534 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001535 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001536 goto out_free;
1537 /* In case we need an open_confirm, no cleanup! */
1538 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1539 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001540 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001541 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001542 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001543out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001544 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001545}
1546
1547static const struct rpc_call_ops nfs4_open_ops = {
1548 .rpc_call_prepare = nfs4_open_prepare,
1549 .rpc_call_done = nfs4_open_done,
1550 .rpc_release = nfs4_open_release,
1551};
1552
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001553static const struct rpc_call_ops nfs4_recover_open_ops = {
1554 .rpc_call_prepare = nfs4_recover_open_prepare,
1555 .rpc_call_done = nfs4_open_done,
1556 .rpc_release = nfs4_open_release,
1557};
1558
1559static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001560{
1561 struct inode *dir = data->dir->d_inode;
1562 struct nfs_server *server = NFS_SERVER(dir);
1563 struct nfs_openargs *o_arg = &data->o_arg;
1564 struct nfs_openres *o_res = &data->o_res;
1565 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001566 struct rpc_message msg = {
1567 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1568 .rpc_argp = o_arg,
1569 .rpc_resp = o_res,
1570 .rpc_cred = data->owner->so_cred,
1571 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001572 struct rpc_task_setup task_setup_data = {
1573 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001574 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001575 .callback_ops = &nfs4_open_ops,
1576 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001577 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001578 .flags = RPC_TASK_ASYNC,
1579 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001580 int status;
1581
Trond Myklebust9d12b212012-01-17 22:04:25 -05001582 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001583 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001584 data->rpc_done = 0;
1585 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001586 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001587 if (isrecover)
1588 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001589 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001590 if (IS_ERR(task))
1591 return PTR_ERR(task);
1592 status = nfs4_wait_for_completion_rpc_task(task);
1593 if (status != 0) {
1594 data->cancelled = 1;
1595 smp_wmb();
1596 } else
1597 status = data->rpc_status;
1598 rpc_put_task(task);
1599
1600 return status;
1601}
1602
1603static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1604{
1605 struct inode *dir = data->dir->d_inode;
1606 struct nfs_openres *o_res = &data->o_res;
1607 int status;
1608
1609 status = nfs4_run_open_task(data, 1);
1610 if (status != 0 || !data->rpc_done)
1611 return status;
1612
Trond Myklebust6926afd2012-01-07 13:22:46 -05001613 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1614
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001615 nfs_refresh_inode(dir, o_res->dir_attr);
1616
1617 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1618 status = _nfs4_proc_open_confirm(data);
1619 if (status != 0)
1620 return status;
1621 }
1622
1623 return status;
1624}
1625
1626/*
1627 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1628 */
1629static int _nfs4_proc_open(struct nfs4_opendata *data)
1630{
1631 struct inode *dir = data->dir->d_inode;
1632 struct nfs_server *server = NFS_SERVER(dir);
1633 struct nfs_openargs *o_arg = &data->o_arg;
1634 struct nfs_openres *o_res = &data->o_res;
1635 int status;
1636
1637 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001638 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001639 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001640 if (status != 0) {
1641 if (status == -NFS4ERR_BADNAME &&
1642 !(o_arg->open_flags & O_CREAT))
1643 return -ENOENT;
1644 return status;
1645 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001646
Trond Myklebust6926afd2012-01-07 13:22:46 -05001647 nfs_fattr_map_and_free_names(server, &data->f_attr);
1648
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001649 if (o_arg->open_flags & O_CREAT) {
1650 update_changeattr(dir, &o_res->cinfo);
1651 nfs_post_op_update_inode(dir, o_res->dir_attr);
1652 } else
1653 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001654 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1655 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001657 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001659 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
1661 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001662 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001663 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
Andy Adamsond83217c2011-03-01 01:34:17 +00001666static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001667{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001668 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001669 int ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001670
Trond Myklebusta78cb572009-08-09 15:06:19 -04001671 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001672 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001673 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001674 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001675 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1676 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001677 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001678 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001679 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001680 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001681 return ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001682}
1683
Andy Adamsond83217c2011-03-01 01:34:17 +00001684static int nfs4_recover_expired_lease(struct nfs_server *server)
1685{
1686 return nfs4_client_recover_expired_lease(server->nfs_client);
1687}
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689/*
1690 * OPEN_EXPIRED:
1691 * reclaim state on the server after a network partition.
1692 * Assumes caller holds the appropriate lock
1693 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001694static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001696 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001697 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001699 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1700 if (IS_ERR(opendata))
1701 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001702 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001703 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001704 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001705 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001706 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001709static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001710{
Trond Myklebust539cd032007-06-05 11:46:42 -04001711 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001712 struct nfs4_exception exception = { };
1713 int err;
1714
1715 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001716 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001717 switch (err) {
1718 default:
1719 goto out;
1720 case -NFS4ERR_GRACE:
1721 case -NFS4ERR_DELAY:
1722 nfs4_handle_exception(server, err, &exception);
1723 err = 0;
1724 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001725 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001726out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001727 return err;
1728}
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1731{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001733 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Trond Myklebust864472e2006-01-03 09:55:15 +01001735 ctx = nfs4_state_find_open_context(state);
1736 if (IS_ERR(ctx))
1737 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001738 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001739 put_nfs_open_context(ctx);
1740 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
1742
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001743#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001744static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001745{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001746 int status = NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001747 struct nfs_server *server = NFS_SERVER(state->inode);
1748
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001749 if (state->flags & flags) {
1750 status = nfs41_test_stateid(server, stateid);
1751 if (status != NFS_OK) {
1752 nfs41_free_stateid(server, stateid);
1753 state->flags &= ~flags;
1754 }
1755 }
1756 return status;
1757}
1758
1759static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1760{
1761 int deleg_status, open_status;
1762 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1763 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1764
1765 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1766 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1767
1768 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1769 return NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001770 return nfs4_open_expired(sp, state);
1771}
1772#endif
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001775 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1776 * fields corresponding to attributes that were used to store the verifier.
1777 * Make sure we clobber those fields in the later setattr call
1778 */
1779static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1780{
1781 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1782 !(sattr->ia_valid & ATTR_ATIME_SET))
1783 sattr->ia_valid |= ATTR_ATIME;
1784
1785 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1786 !(sattr->ia_valid & ATTR_MTIME_SET))
1787 sattr->ia_valid |= ATTR_MTIME;
1788}
1789
1790/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001791 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001793static 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 -07001794{
1795 struct nfs4_state_owner *sp;
1796 struct nfs4_state *state = NULL;
1797 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001798 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001799 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001803 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1804 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1806 goto out_err;
1807 }
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001808 status = nfs4_recover_expired_lease(server);
1809 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001810 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001811 if (dentry->d_inode != NULL)
1812 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001813 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001814 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001815 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001816 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Al Viro82a2c1b2011-06-22 18:30:55 -04001818 if (dentry->d_inode != NULL)
1819 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001820
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001821 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001823 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001825 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001826 status = PTR_ERR(state);
1827 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001828 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001829 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001830 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001831
1832 if (opendata->o_arg.open_flags & O_EXCL) {
1833 nfs4_exclusive_attrset(opendata, sattr);
1834
1835 nfs_fattr_init(opendata->o_res.f_attr);
1836 status = nfs4_do_setattr(state->inode, cred,
1837 opendata->o_res.f_attr, sattr,
1838 state);
1839 if (status == 0)
1840 nfs_setattr_update_inode(state->inode, sattr);
1841 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1842 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001843 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 *res = state;
1846 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001847err_opendata_put:
1848 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001849err_put_state_owner:
1850 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 *res = NULL;
1853 return status;
1854}
1855
1856
Al Viro82a2c1b2011-06-22 18:30:55 -04001857static 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 -07001858{
1859 struct nfs4_exception exception = { };
1860 struct nfs4_state *res;
1861 int status;
1862
Trond Myklebust5e74cfe2012-06-08 10:58:09 -04001863 fmode &= FMODE_READ|FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001865 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 if (status == 0)
1867 break;
1868 /* NOTE: BAD_SEQID means the server and client disagree about the
1869 * book-keeping w.r.t. state-changing operations
1870 * (OPEN/CLOSE/LOCK/LOCKU...)
1871 * It is actually a sign of a bug on the client or on the server.
1872 *
1873 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001874 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 * have unhashed the old state_owner for us, and that we can
1876 * therefore safely retry using a new one. We should still warn
1877 * the user though...
1878 */
1879 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001880 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001881 " returned a bad sequence-id error!\n",
1882 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 exception.retry = 1;
1884 continue;
1885 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001886 /*
1887 * BAD_STATEID on OPEN means that the server cancelled our
1888 * state before it received the OPEN_CONFIRM.
1889 * Recover by retrying the request as per the discussion
1890 * on Page 181 of RFC3530.
1891 */
1892 if (status == -NFS4ERR_BAD_STATEID) {
1893 exception.retry = 1;
1894 continue;
1895 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001896 if (status == -EAGAIN) {
1897 /* We must have found a delegation */
1898 exception.retry = 1;
1899 continue;
1900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1902 status, &exception));
1903 } while (exception.retry);
1904 return res;
1905}
1906
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001907static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1908 struct nfs_fattr *fattr, struct iattr *sattr,
1909 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001911 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001913 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 .iap = sattr,
1915 .server = server,
1916 .bitmask = server->attr_bitmask,
1917 };
1918 struct nfs_setattrres res = {
1919 .fattr = fattr,
1920 .server = server,
1921 };
1922 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001923 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1924 .rpc_argp = &arg,
1925 .rpc_resp = &res,
1926 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001928 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001929 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Trond Myklebust0e574af2005-10-27 22:12:38 -04001931 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Trond Myklebust4fc87962012-03-08 17:42:01 -05001933 if (state != NULL) {
1934 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1935 current->files, current->tgid);
1936 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1937 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001938 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001939 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05001940 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Bryan Schumaker7c513052011-03-24 17:12:24 +00001942 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001943 if (status == 0 && state != NULL)
1944 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001945 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
1947
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001948static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1949 struct nfs_fattr *fattr, struct iattr *sattr,
1950 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001952 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001953 struct nfs4_exception exception = {
1954 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05001955 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001956 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 int err;
1958 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04001959 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
1960 switch (err) {
1961 case -NFS4ERR_OPENMODE:
1962 if (state && !(state->state & FMODE_WRITE)) {
1963 err = -EBADF;
1964 if (sattr->ia_valid & ATTR_OPEN)
1965 err = -EACCES;
1966 goto out;
1967 }
1968 }
1969 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04001971out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return err;
1973}
1974
1975struct nfs4_closedata {
1976 struct inode *inode;
1977 struct nfs4_state *state;
1978 struct nfs_closeargs arg;
1979 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001980 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001981 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001982 bool roc;
1983 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984};
1985
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001986static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001987{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001988 struct nfs4_closedata *calldata = data;
1989 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001990 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001991
Fred Isamanf7e89172011-01-06 11:36:32 +00001992 if (calldata->roc)
1993 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001994 nfs4_put_open_state(calldata->state);
1995 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001996 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001997 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001998 kfree(calldata);
1999}
2000
Trond Myklebust88069f72009-12-08 08:33:16 -05002001static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2002 fmode_t fmode)
2003{
2004 spin_lock(&state->owner->so_lock);
2005 if (!(fmode & FMODE_READ))
2006 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2007 if (!(fmode & FMODE_WRITE))
2008 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2009 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2010 spin_unlock(&state->owner->so_lock);
2011}
2012
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002013static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002015 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 struct nfs_server *server = NFS_SERVER(calldata->inode);
2018
Chuck Levera3ca5652012-03-01 17:00:40 -05002019 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002020 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2021 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 /* hmm. we are done with the inode, and in the process of freeing
2023 * the state_owner. we keep this around to process errors
2024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 switch (task->tk_status) {
2026 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002027 if (calldata->roc)
2028 pnfs_roc_set_barrier(state->inode,
2029 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002030 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002031 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002032 nfs4_close_clear_stateid_flags(state,
2033 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 break;
2035 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002036 case -NFS4ERR_OLD_STATEID:
2037 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002039 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002040 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002042 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2043 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002045 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002046 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002047 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002050static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002052 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002053 struct nfs4_state *state = calldata->state;
Trond Myklebust0bfb0002014-08-25 22:33:12 -04002054 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002055 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002056
Chuck Levera3ca5652012-03-01 17:00:40 -05002057 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002058 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002059 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002060
Trond Myklebust88069f72009-12-08 08:33:16 -05002061 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002062 spin_lock(&state->owner->so_lock);
Trond Myklebust0bfb0002014-08-25 22:33:12 -04002063 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2064 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2065 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04002066 /* Calculate the change in open mode */
Trond Myklebustb6cab212014-09-18 11:51:32 -04002067 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002068 if (state->n_rdwr == 0) {
Trond Myklebustb6cab212014-09-18 11:51:32 -04002069 if (state->n_rdonly == 0)
2070 call_close |= is_rdonly;
2071 else if (is_rdonly)
2072 calldata->arg.fmode |= FMODE_READ;
2073 if (state->n_wronly == 0)
2074 call_close |= is_wronly;
2075 else if (is_wronly)
2076 calldata->arg.fmode |= FMODE_WRITE;
2077 } else if (is_rdwr)
2078 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2079
2080 if (calldata->arg.fmode == 0)
2081 call_close |= is_rdwr;
2082
Trond Myklebust4cecb762005-11-04 15:32:58 -05002083 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002084
2085 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002086 /* Note: exit _without_ calling nfs4_close_done */
2087 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002088 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002089 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002090
Fred Isamanf7e89172011-01-06 11:36:32 +00002091 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002092 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002093 if (calldata->roc &&
2094 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2095 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2096 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002097 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002098 }
2099 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002100
Trond Myklebust516a6af2005-10-27 22:12:41 -04002101 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002102 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002103 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002104 &calldata->arg.seq_args,
2105 &calldata->res.seq_res,
Trond Myklebust0ada2102012-10-29 18:37:40 -04002106 task) != 0)
2107 nfs_release_seqid(calldata->arg.seqid);
2108 else
2109 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002110out:
2111 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002114static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002115 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002116 .rpc_call_done = nfs4_close_done,
2117 .rpc_release = nfs4_free_closedata,
2118};
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120/*
2121 * It is possible for data to be read/written from a mem-mapped file
2122 * after the sys_close call (which hits the vfs layer as a flush).
2123 * This means that we can't safely call nfsv4 close on a file until
2124 * the inode is cleared. This in turn means that we are not good
2125 * NFSv4 citizens - we do not indicate to the server to update the file's
2126 * share state even when we are done with one of the three share
2127 * stateid's in the inode.
2128 *
2129 * NOTE: Caller must be holding the sp->so_owner semaphore!
2130 */
Al Viro643168c2011-06-22 18:20:23 -04002131int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002133 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002135 struct nfs4_state_owner *sp = state->owner;
2136 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002137 struct rpc_message msg = {
2138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2139 .rpc_cred = state->owner->so_cred,
2140 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002141 struct rpc_task_setup task_setup_data = {
2142 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002143 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002144 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002145 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002146 .flags = RPC_TASK_ASYNC,
2147 };
Trond Myklebust95121352005-10-18 14:20:12 -07002148 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002150 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002152 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002153 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002154 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002156 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002157 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002159 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002160 if (calldata->arg.seqid == NULL)
2161 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002162 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002163 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002164 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002165 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002166 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002167 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002168 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002169
Trond Myklebust1174dd12010-12-21 10:52:24 -05002170 msg.rpc_argp = &calldata->arg;
2171 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002172 task_setup_data.callback_data = calldata;
2173 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002174 if (IS_ERR(task))
2175 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002176 status = 0;
2177 if (wait)
2178 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002179 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002180 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002181out_free_calldata:
2182 kfree(calldata);
2183out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002184 if (roc)
2185 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002186 nfs4_put_open_state(state);
2187 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002188 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189}
2190
Trond Myklebust2b484292010-09-17 10:56:51 -04002191static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002192nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 struct nfs4_state *state;
2195
Trond Myklebust565277f2007-10-15 18:17:53 -04002196 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002197 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002198 if (IS_ERR(state))
2199 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002200 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002201 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202}
2203
Trond Myklebust1185a552009-12-03 15:54:02 -05002204static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002205{
2206 if (ctx->state == NULL)
2207 return;
2208 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002209 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002210 else
Al Viro643168c2011-06-22 18:20:23 -04002211 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002212}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2215{
Benny Halevy43652ad2009-04-01 09:21:54 -04002216 struct nfs4_server_caps_arg args = {
2217 .fhandle = fhandle,
2218 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 struct nfs4_server_caps_res res = {};
2220 struct rpc_message msg = {
2221 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002222 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 .rpc_resp = &res,
2224 };
2225 int status;
2226
Bryan Schumaker7c513052011-03-24 17:12:24 +00002227 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 if (status == 0) {
2229 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002230 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2231 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2232 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2233 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2234 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2236 server->caps |= NFS_CAP_ACLS;
2237 if (res.has_links != 0)
2238 server->caps |= NFS_CAP_HARDLINKS;
2239 if (res.has_symlinks != 0)
2240 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002241 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2242 server->caps |= NFS_CAP_FILEID;
2243 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2244 server->caps |= NFS_CAP_MODE;
2245 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2246 server->caps |= NFS_CAP_NLINK;
2247 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2248 server->caps |= NFS_CAP_OWNER;
2249 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2250 server->caps |= NFS_CAP_OWNER_GROUP;
2251 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2252 server->caps |= NFS_CAP_ATIME;
2253 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2254 server->caps |= NFS_CAP_CTIME;
2255 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2256 server->caps |= NFS_CAP_MTIME;
2257
Trond Myklebusta65318b2009-03-11 14:10:28 -04002258 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2259 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2260 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002262 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return status;
2266}
2267
Trond Myklebust55a97592006-06-09 09:34:19 -04002268int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
2270 struct nfs4_exception exception = { };
2271 int err;
2272 do {
2273 err = nfs4_handle_exception(server,
2274 _nfs4_server_capabilities(server, fhandle),
2275 &exception);
2276 } while (exception.retry);
2277 return err;
2278}
2279
2280static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2281 struct nfs_fsinfo *info)
2282{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 struct nfs4_lookup_root_arg args = {
2284 .bitmask = nfs4_fattr_bitmap,
2285 };
2286 struct nfs4_lookup_res res = {
2287 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002288 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 .fh = fhandle,
2290 };
2291 struct rpc_message msg = {
2292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2293 .rpc_argp = &args,
2294 .rpc_resp = &res,
2295 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002296
Trond Myklebust0e574af2005-10-27 22:12:38 -04002297 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002298 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299}
2300
2301static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2302 struct nfs_fsinfo *info)
2303{
2304 struct nfs4_exception exception = { };
2305 int err;
2306 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002307 err = _nfs4_lookup_root(server, fhandle, info);
2308 switch (err) {
2309 case 0:
2310 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002311 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002312 default:
2313 err = nfs4_handle_exception(server, err, &exception);
2314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002316out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 return err;
2318}
2319
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002320static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2321 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2322{
2323 struct rpc_auth *auth;
2324 int ret;
2325
2326 auth = rpcauth_create(flavor, server->client);
2327 if (!auth) {
2328 ret = -EIO;
2329 goto out;
2330 }
2331 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002332out:
2333 return ret;
2334}
2335
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002336static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002337 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002339 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002340 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002342 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2343 flav_array[len] = RPC_AUTH_NULL;
2344 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002345
2346 for (i = 0; i < len; i++) {
2347 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002348 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002349 continue;
2350 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002351 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002352 /*
2353 * -EACCESS could mean that the user doesn't have correct permissions
2354 * to access the mount. It could also mean that we tried to mount
2355 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2356 * existing mount programs don't handle -EACCES very well so it should
2357 * be mapped to -EPERM instead.
2358 */
2359 if (status == -EACCES)
2360 status = -EPERM;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002361 return status;
2362}
2363
2364/*
2365 * get the file handle for the "/" directory on the server
2366 */
2367static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2368 struct nfs_fsinfo *info)
2369{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002370 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002371 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002372 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2373 /*
2374 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2375 * by nfs4_map_errors() as this function exits.
2376 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002377 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if (status == 0)
2379 status = nfs4_server_capabilities(server, fhandle);
2380 if (status == 0)
2381 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002382 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
2384
Manoj Naik6b97fd32006-06-09 09:34:29 -04002385/*
2386 * Get locations and (maybe) other attributes of a referral.
2387 * Note that we'll actually follow the referral later when
2388 * we detect fsid mismatch in inode revalidation
2389 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002390static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2391 const struct qstr *name, struct nfs_fattr *fattr,
2392 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002393{
2394 int status = -ENOMEM;
2395 struct page *page = NULL;
2396 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002397
2398 page = alloc_page(GFP_KERNEL);
2399 if (page == NULL)
2400 goto out;
2401 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2402 if (locations == NULL)
2403 goto out;
2404
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002405 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002406 if (status != 0)
2407 goto out;
2408 /* Make sure server returned a different fsid for the referral */
2409 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002410 dprintk("%s: server did not return a different fsid for"
2411 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002412 status = -EIO;
2413 goto out;
2414 }
Andy Adamson533eb462011-06-13 18:25:56 -04002415 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2416 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002417
Andy Adamson533eb462011-06-13 18:25:56 -04002418 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002419 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002420 memset(fhandle, 0, sizeof(struct nfs_fh));
2421out:
2422 if (page)
2423 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002424 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002425 return status;
2426}
2427
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2429{
2430 struct nfs4_getattr_arg args = {
2431 .fh = fhandle,
2432 .bitmask = server->attr_bitmask,
2433 };
2434 struct nfs4_getattr_res res = {
2435 .fattr = fattr,
2436 .server = server,
2437 };
2438 struct rpc_message msg = {
2439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2440 .rpc_argp = &args,
2441 .rpc_resp = &res,
2442 };
2443
Trond Myklebust0e574af2005-10-27 22:12:38 -04002444 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002445 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446}
2447
2448static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2449{
2450 struct nfs4_exception exception = { };
2451 int err;
2452 do {
2453 err = nfs4_handle_exception(server,
2454 _nfs4_proc_getattr(server, fhandle, fattr),
2455 &exception);
2456 } while (exception.retry);
2457 return err;
2458}
2459
2460/*
2461 * The file is not closed if it is opened due to the a request to change
2462 * the size of the file. The open call will not be needed once the
2463 * VFS layer lookup-intents are implemented.
2464 *
2465 * Close is called when the inode is destroyed.
2466 * If we haven't opened the file for O_WRONLY, we
2467 * need to in the size_change case to obtain a stateid.
2468 *
2469 * Got race?
2470 * Because OPEN is always done by name in nfsv4, it is
2471 * possible that we opened a different file by the same
2472 * name. We can recognize this race condition, but we
2473 * can't do anything about it besides returning an error.
2474 *
2475 * This will be fixed with VFS changes (lookup-intent).
2476 */
2477static int
2478nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2479 struct iattr *sattr)
2480{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002481 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002482 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002483 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 int status;
2485
Benny Halevy8a1636c2010-07-14 15:43:57 -04002486 if (pnfs_ld_layoutret_on_setattr(inode))
2487 pnfs_return_layout(inode);
2488
Trond Myklebust0e574af2005-10-27 22:12:38 -04002489 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Trond Myklebustd5308382005-11-04 15:33:38 -05002491 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002492 if (sattr->ia_valid & ATTR_FILE) {
2493 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002494
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002495 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002496 if (ctx) {
2497 cred = ctx->cred;
2498 state = ctx->state;
2499 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002500 }
2501
Trond Myklebust536e43d2012-01-17 22:04:26 -05002502 /* Deal with open(O_TRUNC) */
2503 if (sattr->ia_valid & ATTR_OPEN)
2504 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2505
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002506 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002507 if (status == 0)
2508 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 return status;
2510}
2511
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002512static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2513 const struct qstr *name, struct nfs_fh *fhandle,
2514 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002515{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002516 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002517 int status;
2518 struct nfs4_lookup_arg args = {
2519 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002520 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002521 .name = name,
2522 };
2523 struct nfs4_lookup_res res = {
2524 .server = server,
2525 .fattr = fattr,
2526 .fh = fhandle,
2527 };
2528 struct rpc_message msg = {
2529 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2530 .rpc_argp = &args,
2531 .rpc_resp = &res,
2532 };
2533
2534 nfs_fattr_init(fattr);
2535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002537 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 dprintk("NFS reply lookup: %d\n", status);
2539 return status;
2540}
2541
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002542static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002543{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002544 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002545 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002546 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2547 fattr->nlink = 2;
2548}
2549
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002550static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2551 struct qstr *name, struct nfs_fh *fhandle,
2552 struct nfs_fattr *fattr)
2553{
2554 struct nfs4_exception exception = { };
2555 struct rpc_clnt *client = *clnt;
2556 int err;
2557 do {
2558 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2559 switch (err) {
2560 case -NFS4ERR_BADNAME:
2561 err = -ENOENT;
2562 goto out;
2563 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002564 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002565 goto out;
2566 case -NFS4ERR_WRONGSEC:
2567 err = -EPERM;
2568 if (client != *clnt)
2569 goto out;
2570
2571 client = nfs4_create_sec_client(client, dir, name);
2572 if (IS_ERR(client))
2573 return PTR_ERR(client);
2574
2575 exception.retry = 1;
2576 break;
2577 default:
2578 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2579 }
2580 } while (exception.retry);
2581
2582out:
2583 if (err == 0)
2584 *clnt = client;
2585 else if (client != *clnt)
2586 rpc_shutdown_client(client);
2587
2588 return err;
2589}
2590
Bryan Schumaker7c513052011-03-24 17:12:24 +00002591static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2592 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002594 int status;
2595 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002596
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002597 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2598 if (client != NFS_CLIENT(dir)) {
2599 rpc_shutdown_client(client);
2600 nfs_fixup_secinfo_attributes(fattr);
2601 }
2602 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603}
2604
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002605struct rpc_clnt *
2606nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2607 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2608{
2609 int status;
2610 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2611
2612 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2613 if (status < 0) {
2614 rpc_shutdown_client(client);
2615 return ERR_PTR(status);
2616 }
2617 return client;
2618}
2619
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2621{
Trond Myklebust76b32992007-08-10 17:45:11 -04002622 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 struct nfs4_accessargs args = {
2624 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002625 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002627 struct nfs4_accessres res = {
2628 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002629 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 struct rpc_message msg = {
2631 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2632 .rpc_argp = &args,
2633 .rpc_resp = &res,
2634 .rpc_cred = entry->cred,
2635 };
2636 int mode = entry->mask;
2637 int status;
2638
2639 /*
2640 * Determine which access bits we want to ask for...
2641 */
2642 if (mode & MAY_READ)
2643 args.access |= NFS4_ACCESS_READ;
2644 if (S_ISDIR(inode->i_mode)) {
2645 if (mode & MAY_WRITE)
2646 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2647 if (mode & MAY_EXEC)
2648 args.access |= NFS4_ACCESS_LOOKUP;
2649 } else {
2650 if (mode & MAY_WRITE)
2651 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2652 if (mode & MAY_EXEC)
2653 args.access |= NFS4_ACCESS_EXECUTE;
2654 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002655
2656 res.fattr = nfs_alloc_fattr();
2657 if (res.fattr == NULL)
2658 return -ENOMEM;
2659
Bryan Schumaker7c513052011-03-24 17:12:24 +00002660 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 if (!status) {
2662 entry->mask = 0;
2663 if (res.access & NFS4_ACCESS_READ)
2664 entry->mask |= MAY_READ;
2665 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2666 entry->mask |= MAY_WRITE;
2667 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2668 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002669 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002671 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 return status;
2673}
2674
2675static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2676{
2677 struct nfs4_exception exception = { };
2678 int err;
2679 do {
2680 err = nfs4_handle_exception(NFS_SERVER(inode),
2681 _nfs4_proc_access(inode, entry),
2682 &exception);
2683 } while (exception.retry);
2684 return err;
2685}
2686
2687/*
2688 * TODO: For the time being, we don't try to get any attributes
2689 * along with any of the zero-copy operations READ, READDIR,
2690 * READLINK, WRITE.
2691 *
2692 * In the case of the first three, we want to put the GETATTR
2693 * after the read-type operation -- this is because it is hard
2694 * to predict the length of a GETATTR response in v4, and thus
2695 * align the READ data correctly. This means that the GETATTR
2696 * may end up partially falling into the page cache, and we should
2697 * shift it into the 'tail' of the xdr_buf before processing.
2698 * To do this efficiently, we need to know the total length
2699 * of data received, which doesn't seem to be available outside
2700 * of the RPC layer.
2701 *
2702 * In the case of WRITE, we also want to put the GETATTR after
2703 * the operation -- in this case because we want to make sure
2704 * we get the post-operation mtime and size. This means that
2705 * we can't use xdr_encode_pages() as written: we need a variant
2706 * of it which would leave room in the 'tail' iovec.
2707 *
2708 * Both of these changes to the XDR layer would in fact be quite
2709 * minor, but I decided to leave them for a subsequent patch.
2710 */
2711static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2712 unsigned int pgbase, unsigned int pglen)
2713{
2714 struct nfs4_readlink args = {
2715 .fh = NFS_FH(inode),
2716 .pgbase = pgbase,
2717 .pglen = pglen,
2718 .pages = &page,
2719 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002720 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 struct rpc_message msg = {
2722 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2723 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002724 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 };
2726
Bryan Schumaker7c513052011-03-24 17:12:24 +00002727 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 -07002728}
2729
2730static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2731 unsigned int pgbase, unsigned int pglen)
2732{
2733 struct nfs4_exception exception = { };
2734 int err;
2735 do {
2736 err = nfs4_handle_exception(NFS_SERVER(inode),
2737 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2738 &exception);
2739 } while (exception.retry);
2740 return err;
2741}
2742
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743/*
2744 * Got race?
2745 * We will need to arrange for the VFS layer to provide an atomic open.
2746 * Until then, this create/open method is prone to inefficiency and race
2747 * conditions due to the lookup, create, and open VFS calls from sys_open()
2748 * placed on the wire.
2749 *
2750 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2751 * The file will be opened again in the subsequent VFS open call
2752 * (nfs4_proc_file_open).
2753 *
2754 * The open for read will just hang around to be used by any process that
2755 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2756 */
2757
2758static int
2759nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002760 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761{
Al Viro82a2c1b2011-06-22 18:30:55 -04002762 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002764 struct rpc_cred *cred = NULL;
2765 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 int status = 0;
2767
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002768 if (ctx != NULL) {
2769 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002770 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002771 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002773 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002774 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002775 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 if (IS_ERR(state)) {
2777 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002778 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002780 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002781 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002782 if (ctx != NULL)
2783 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002784 else
Al Viro643168c2011-06-22 18:20:23 -04002785 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786out:
2787 return status;
2788}
2789
2790static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2791{
Trond Myklebust16e42952005-10-27 22:12:44 -04002792 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002793 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002795 .name.len = name->len,
2796 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002797 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002799 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002800 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002801 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002803 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2804 .rpc_argp = &args,
2805 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002807 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Trond Myklebustd3468902010-04-16 16:22:50 -04002809 res.dir_attr = nfs_alloc_fattr();
2810 if (res.dir_attr == NULL)
2811 goto out;
2812
Bryan Schumaker7c513052011-03-24 17:12:24 +00002813 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002814 if (status == 0) {
2815 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002816 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002817 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002818 nfs_free_fattr(res.dir_attr);
2819out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 return status;
2821}
2822
2823static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2824{
2825 struct nfs4_exception exception = { };
2826 int err;
2827 do {
2828 err = nfs4_handle_exception(NFS_SERVER(dir),
2829 _nfs4_proc_remove(dir, name),
2830 &exception);
2831 } while (exception.retry);
2832 return err;
2833}
2834
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002835static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002837 struct nfs_server *server = NFS_SERVER(dir);
2838 struct nfs_removeargs *args = msg->rpc_argp;
2839 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Trond Myklebusta65318b2009-03-11 14:10:28 -04002841 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002842 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002844 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845}
2846
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002847static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2848{
2849 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2850 &data->args.seq_args,
2851 &data->res.seq_res,
2852 task))
2853 return;
2854 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855}
2856
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002857static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002859 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2860
Trond Myklebust14516c32010-07-31 14:29:06 -04002861 if (!nfs4_sequence_done(task, &res->seq_res))
2862 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002863 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002864 return 0;
2865 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002866 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002867 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868}
2869
Jeff Laytond3d41522010-09-17 17:31:57 -04002870static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2871{
2872 struct nfs_server *server = NFS_SERVER(dir);
2873 struct nfs_renameargs *arg = msg->rpc_argp;
2874 struct nfs_renameres *res = msg->rpc_resp;
2875
2876 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2877 arg->bitmask = server->attr_bitmask;
2878 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002879 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002880}
2881
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002882static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2883{
2884 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2885 &data->args.seq_args,
2886 &data->res.seq_res,
2887 task))
2888 return;
2889 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002890}
2891
2892static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2893 struct inode *new_dir)
2894{
2895 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2896
2897 if (!nfs4_sequence_done(task, &res->seq_res))
2898 return 0;
2899 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2900 return 0;
2901
2902 update_changeattr(old_dir, &res->old_cinfo);
2903 nfs_post_op_update_inode(old_dir, res->old_fattr);
2904 update_changeattr(new_dir, &res->new_cinfo);
2905 nfs_post_op_update_inode(new_dir, res->new_fattr);
2906 return 1;
2907}
2908
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2910 struct inode *new_dir, struct qstr *new_name)
2911{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002912 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002913 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 .old_dir = NFS_FH(old_dir),
2915 .new_dir = NFS_FH(new_dir),
2916 .old_name = old_name,
2917 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002918 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002920 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002921 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002922 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 struct rpc_message msg = {
2924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2925 .rpc_argp = &arg,
2926 .rpc_resp = &res,
2927 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002928 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Trond Myklebust011fff72010-04-16 16:22:49 -04002930 res.old_fattr = nfs_alloc_fattr();
2931 res.new_fattr = nfs_alloc_fattr();
2932 if (res.old_fattr == NULL || res.new_fattr == NULL)
2933 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Bryan Schumaker7c513052011-03-24 17:12:24 +00002935 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (!status) {
2937 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002938 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002940 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002942out:
2943 nfs_free_fattr(res.new_fattr);
2944 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 return status;
2946}
2947
2948static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2949 struct inode *new_dir, struct qstr *new_name)
2950{
2951 struct nfs4_exception exception = { };
2952 int err;
2953 do {
2954 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2955 _nfs4_proc_rename(old_dir, old_name,
2956 new_dir, new_name),
2957 &exception);
2958 } while (exception.retry);
2959 return err;
2960}
2961
2962static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2963{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002964 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 struct nfs4_link_arg arg = {
2966 .fh = NFS_FH(inode),
2967 .dir_fh = NFS_FH(dir),
2968 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002969 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002971 struct nfs4_link_res res = {
2972 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002973 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 struct rpc_message msg = {
2975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2976 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002977 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002979 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Trond Myklebust136f2622010-04-16 16:22:49 -04002981 res.fattr = nfs_alloc_fattr();
2982 res.dir_attr = nfs_alloc_fattr();
2983 if (res.fattr == NULL || res.dir_attr == NULL)
2984 goto out;
2985
Bryan Schumaker7c513052011-03-24 17:12:24 +00002986 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002987 if (!status) {
2988 update_changeattr(dir, &res.cinfo);
2989 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002990 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002991 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002992out:
2993 nfs_free_fattr(res.dir_attr);
2994 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 return status;
2996}
2997
2998static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2999{
3000 struct nfs4_exception exception = { };
3001 int err;
3002 do {
3003 err = nfs4_handle_exception(NFS_SERVER(inode),
3004 _nfs4_proc_link(inode, dir, name),
3005 &exception);
3006 } while (exception.retry);
3007 return err;
3008}
3009
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003010struct nfs4_createdata {
3011 struct rpc_message msg;
3012 struct nfs4_create_arg arg;
3013 struct nfs4_create_res res;
3014 struct nfs_fh fh;
3015 struct nfs_fattr fattr;
3016 struct nfs_fattr dir_fattr;
3017};
3018
3019static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3020 struct qstr *name, struct iattr *sattr, u32 ftype)
3021{
3022 struct nfs4_createdata *data;
3023
3024 data = kzalloc(sizeof(*data), GFP_KERNEL);
3025 if (data != NULL) {
3026 struct nfs_server *server = NFS_SERVER(dir);
3027
3028 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3029 data->msg.rpc_argp = &data->arg;
3030 data->msg.rpc_resp = &data->res;
3031 data->arg.dir_fh = NFS_FH(dir);
3032 data->arg.server = server;
3033 data->arg.name = name;
3034 data->arg.attrs = sattr;
3035 data->arg.ftype = ftype;
3036 data->arg.bitmask = server->attr_bitmask;
3037 data->res.server = server;
3038 data->res.fh = &data->fh;
3039 data->res.fattr = &data->fattr;
3040 data->res.dir_fattr = &data->dir_fattr;
3041 nfs_fattr_init(data->res.fattr);
3042 nfs_fattr_init(data->res.dir_fattr);
3043 }
3044 return data;
3045}
3046
3047static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3048{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003049 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003050 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003051 if (status == 0) {
3052 update_changeattr(dir, &data->res.dir_cinfo);
3053 nfs_post_op_update_inode(dir, data->res.dir_fattr);
3054 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3055 }
3056 return status;
3057}
3058
3059static void nfs4_free_createdata(struct nfs4_createdata *data)
3060{
3061 kfree(data);
3062}
3063
Chuck Lever4f390c12006-08-22 20:06:22 -04003064static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003065 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003067 struct nfs4_createdata *data;
3068 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
Chuck Lever94a6d752006-08-22 20:06:23 -04003070 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003071 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003072
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003073 status = -ENOMEM;
3074 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3075 if (data == NULL)
3076 goto out;
3077
3078 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3079 data->arg.u.symlink.pages = &page;
3080 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003082 status = nfs4_do_create(dir, dentry, data);
3083
3084 nfs4_free_createdata(data);
3085out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 return status;
3087}
3088
Chuck Lever4f390c12006-08-22 20:06:22 -04003089static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003090 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091{
3092 struct nfs4_exception exception = { };
3093 int err;
3094 do {
3095 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003096 _nfs4_proc_symlink(dir, dentry, page,
3097 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 &exception);
3099 } while (exception.retry);
3100 return err;
3101}
3102
3103static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3104 struct iattr *sattr)
3105{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003106 struct nfs4_createdata *data;
3107 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003109 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3110 if (data == NULL)
3111 goto out;
3112
3113 status = nfs4_do_create(dir, dentry, data);
3114
3115 nfs4_free_createdata(data);
3116out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 return status;
3118}
3119
3120static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3121 struct iattr *sattr)
3122{
3123 struct nfs4_exception exception = { };
3124 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003125
3126 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 do {
3128 err = nfs4_handle_exception(NFS_SERVER(dir),
3129 _nfs4_proc_mkdir(dir, dentry, sattr),
3130 &exception);
3131 } while (exception.retry);
3132 return err;
3133}
3134
3135static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003136 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137{
3138 struct inode *dir = dentry->d_inode;
3139 struct nfs4_readdir_arg args = {
3140 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003141 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 .pgbase = 0,
3143 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003144 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003145 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 };
3147 struct nfs4_readdir_res res;
3148 struct rpc_message msg = {
3149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3150 .rpc_argp = &args,
3151 .rpc_resp = &res,
3152 .rpc_cred = cred,
3153 };
3154 int status;
3155
Harvey Harrison3110ff82008-05-02 13:42:44 -07003156 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003157 dentry->d_parent->d_name.name,
3158 dentry->d_name.name,
3159 (unsigned long long)cookie);
Trond Myklebustf15e7242012-09-03 14:56:02 -04003160 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003162 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 -05003163 if (status >= 0) {
Trond Myklebustf15e7242012-09-03 14:56:02 -04003164 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003165 status += args.pgbase;
3166 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003167
3168 nfs_invalidate_atime(dir);
3169
Harvey Harrison3110ff82008-05-02 13:42:44 -07003170 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 return status;
3172}
3173
3174static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003175 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176{
3177 struct nfs4_exception exception = { };
3178 int err;
3179 do {
3180 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3181 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003182 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 &exception);
3184 } while (exception.retry);
3185 return err;
3186}
3187
3188static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3189 struct iattr *sattr, dev_t rdev)
3190{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003191 struct nfs4_createdata *data;
3192 int mode = sattr->ia_mode;
3193 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
3195 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3196 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003197
3198 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3199 if (data == NULL)
3200 goto out;
3201
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003203 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003205 data->arg.ftype = NF4BLK;
3206 data->arg.u.device.specdata1 = MAJOR(rdev);
3207 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 }
3209 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003210 data->arg.ftype = NF4CHR;
3211 data->arg.u.device.specdata1 = MAJOR(rdev);
3212 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003215 status = nfs4_do_create(dir, dentry, data);
3216
3217 nfs4_free_createdata(data);
3218out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 return status;
3220}
3221
3222static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3223 struct iattr *sattr, dev_t rdev)
3224{
3225 struct nfs4_exception exception = { };
3226 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003227
3228 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 do {
3230 err = nfs4_handle_exception(NFS_SERVER(dir),
3231 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3232 &exception);
3233 } while (exception.retry);
3234 return err;
3235}
3236
3237static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3238 struct nfs_fsstat *fsstat)
3239{
3240 struct nfs4_statfs_arg args = {
3241 .fh = fhandle,
3242 .bitmask = server->attr_bitmask,
3243 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003244 struct nfs4_statfs_res res = {
3245 .fsstat = fsstat,
3246 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 struct rpc_message msg = {
3248 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3249 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003250 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 };
3252
Trond Myklebust0e574af2005-10-27 22:12:38 -04003253 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003254 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255}
3256
3257static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3258{
3259 struct nfs4_exception exception = { };
3260 int err;
3261 do {
3262 err = nfs4_handle_exception(server,
3263 _nfs4_proc_statfs(server, fhandle, fsstat),
3264 &exception);
3265 } while (exception.retry);
3266 return err;
3267}
3268
3269static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3270 struct nfs_fsinfo *fsinfo)
3271{
3272 struct nfs4_fsinfo_arg args = {
3273 .fh = fhandle,
3274 .bitmask = server->attr_bitmask,
3275 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003276 struct nfs4_fsinfo_res res = {
3277 .fsinfo = fsinfo,
3278 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 struct rpc_message msg = {
3280 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3281 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003282 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 };
3284
Bryan Schumaker7c513052011-03-24 17:12:24 +00003285 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286}
3287
3288static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3289{
3290 struct nfs4_exception exception = { };
3291 int err;
3292
3293 do {
3294 err = nfs4_handle_exception(server,
3295 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3296 &exception);
3297 } while (exception.retry);
3298 return err;
3299}
3300
3301static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3302{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003303 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3305}
3306
3307static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3308 struct nfs_pathconf *pathconf)
3309{
3310 struct nfs4_pathconf_arg args = {
3311 .fh = fhandle,
3312 .bitmask = server->attr_bitmask,
3313 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003314 struct nfs4_pathconf_res res = {
3315 .pathconf = pathconf,
3316 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 struct rpc_message msg = {
3318 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3319 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003320 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 };
3322
3323 /* None of the pathconf attributes are mandatory to implement */
3324 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3325 memset(pathconf, 0, sizeof(*pathconf));
3326 return 0;
3327 }
3328
Trond Myklebust0e574af2005-10-27 22:12:38 -04003329 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003330 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331}
3332
3333static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3334 struct nfs_pathconf *pathconf)
3335{
3336 struct nfs4_exception exception = { };
3337 int err;
3338
3339 do {
3340 err = nfs4_handle_exception(server,
3341 _nfs4_proc_pathconf(server, fhandle, pathconf),
3342 &exception);
3343 } while (exception.retry);
3344 return err;
3345}
3346
Benny Halevyd20581a2011-05-22 19:52:03 +03003347void __nfs4_read_done_cb(struct nfs_read_data *data)
3348{
3349 nfs_invalidate_atime(data->inode);
3350}
3351
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003352static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353{
Trond Myklebustec06c092006-03-20 13:44:27 -05003354 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003356 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003357 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003358 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003360
Benny Halevyd20581a2011-05-22 19:52:03 +03003361 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003363 renew_lease(server, data->timestamp);
3364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365}
3366
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003367static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3368{
3369
3370 dprintk("--> %s\n", __func__);
3371
3372 if (!nfs4_sequence_done(task, &data->res.seq_res))
3373 return -EAGAIN;
3374
Benny Halevyd20581a2011-05-22 19:52:03 +03003375 return data->read_done_cb ? data->read_done_cb(task, data) :
3376 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003377}
3378
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003379static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003382 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003383 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003384 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385}
3386
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003387static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3388{
3389 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3390 &data->args.seq_args,
3391 &data->res.seq_res,
3392 task))
3393 return;
3394 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395}
3396
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003397/* Reset the the nfs_read_data to send the read to the MDS. */
3398void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3399{
3400 dprintk("%s Reset task for i/o through\n", __func__);
3401 put_lseg(data->lseg);
3402 data->lseg = NULL;
3403 /* offsets will differ in the dense stripe case */
3404 data->args.offset = data->mds_offset;
3405 data->ds_clp = NULL;
3406 data->args.fh = NFS_FH(data->inode);
3407 data->read_done_cb = nfs4_read_done_cb;
3408 task->tk_ops = data->mds_ops;
3409 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3410}
3411EXPORT_SYMBOL_GPL(nfs4_reset_read);
3412
Fred Isamanb029bc92011-03-03 15:13:42 +00003413static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 struct inode *inode = data->inode;
3416
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003417 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003418 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003419 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003421 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003423 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003424 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426}
3427
Fred Isamanb029bc92011-03-03 15:13:42 +00003428static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3429{
3430 if (!nfs4_sequence_done(task, &data->res.seq_res))
3431 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003432 return data->write_done_cb ? data->write_done_cb(task, data) :
3433 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003434}
3435
Fred Isamana69aef12011-03-03 15:13:47 +00003436/* Reset the the nfs_write_data to send the write to the MDS. */
3437void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3438{
3439 dprintk("%s Reset task for i/o through\n", __func__);
3440 put_lseg(data->lseg);
3441 data->lseg = NULL;
3442 data->ds_clp = NULL;
3443 data->write_done_cb = nfs4_write_done_cb;
3444 data->args.fh = NFS_FH(data->inode);
3445 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3446 data->args.offset = data->mds_offset;
3447 data->res.fattr = &data->fattr;
3448 task->tk_ops = data->mds_ops;
3449 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3450}
3451EXPORT_SYMBOL_GPL(nfs4_reset_write);
3452
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003453static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003455 struct nfs_server *server = NFS_SERVER(data->inode);
3456
Fred Isaman7ffd1062011-03-03 15:13:46 +00003457 if (data->lseg) {
3458 data->args.bitmask = NULL;
3459 data->res.fattr = NULL;
3460 } else
3461 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003462 if (!data->write_done_cb)
3463 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003464 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 data->timestamp = jiffies;
3466
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003467 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003468 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469}
3470
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003471static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3472{
3473 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3474 &data->args.seq_args,
3475 &data->res.seq_res,
3476 task))
3477 return;
3478 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479}
3480
Fred Isaman5f452432011-03-23 13:27:46 +00003481static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003484
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003485 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003486 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003487 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003489 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003490 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491}
3492
Fred Isaman5f452432011-03-23 13:27:46 +00003493static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3494{
3495 if (!nfs4_sequence_done(task, &data->res.seq_res))
3496 return -EAGAIN;
3497 return data->write_done_cb(task, data);
3498}
3499
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003500static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003502 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003503
3504 if (data->lseg) {
3505 data->args.bitmask = NULL;
3506 data->res.fattr = NULL;
3507 } else
3508 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003509 if (!data->write_done_cb)
3510 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003511 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003512 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003513 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514}
3515
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003516struct nfs4_renewdata {
3517 struct nfs_client *client;
3518 unsigned long timestamp;
3519};
3520
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521/*
3522 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3523 * standalone procedure for queueing an asynchronous RENEW.
3524 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003525static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003526{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003527 struct nfs4_renewdata *data = calldata;
3528 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003529
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003530 if (atomic_read(&clp->cl_count) > 1)
3531 nfs4_schedule_state_renewal(clp);
3532 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003533 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003534}
3535
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003536static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003538 struct nfs4_renewdata *data = calldata;
3539 struct nfs_client *clp = data->client;
3540 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
3542 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003543 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003544 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3545 return;
3546 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003547 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003548 return;
3549 }
3550 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003552 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553}
3554
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003555static const struct rpc_call_ops nfs4_renew_ops = {
3556 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003557 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003558};
3559
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003560static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561{
3562 struct rpc_message msg = {
3563 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3564 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003565 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003567 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003569 if (renew_flags == 0)
3570 return 0;
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003571 if (!atomic_inc_not_zero(&clp->cl_count))
3572 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003573 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003574 if (data == NULL)
3575 return -ENOMEM;
3576 data->client = clp;
3577 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003579 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580}
3581
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003582static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583{
3584 struct rpc_message msg = {
3585 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3586 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003587 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 };
3589 unsigned long now = jiffies;
3590 int status;
3591
3592 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3593 if (status < 0)
3594 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003595 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 return 0;
3597}
3598
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003599static inline int nfs4_server_supports_acls(struct nfs_server *server)
3600{
3601 return (server->caps & NFS_CAP_ACLS)
3602 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3603 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3604}
3605
3606/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3607 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3608 * the stack.
3609 */
3610#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3611
Neil Hormane9e3d722011-03-04 19:26:03 -05003612static int buf_to_pages_noslab(const void *buf, size_t buflen,
3613 struct page **pages, unsigned int *pgbase)
3614{
3615 struct page *newpage, **spages;
3616 int rc = 0;
3617 size_t len;
3618 spages = pages;
3619
3620 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003621 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003622 newpage = alloc_page(GFP_KERNEL);
3623
3624 if (newpage == NULL)
3625 goto unwind;
3626 memcpy(page_address(newpage), buf, len);
3627 buf += len;
3628 buflen -= len;
3629 *pages++ = newpage;
3630 rc++;
3631 } while (buflen != 0);
3632
3633 return rc;
3634
3635unwind:
3636 for(; rc > 0; rc--)
3637 __free_page(spages[rc-1]);
3638 return -ENOMEM;
3639}
3640
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003641struct nfs4_cached_acl {
3642 int cached;
3643 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003644 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003645};
3646
3647static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003648{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003649 struct nfs_inode *nfsi = NFS_I(inode);
3650
3651 spin_lock(&inode->i_lock);
3652 kfree(nfsi->nfs4_acl);
3653 nfsi->nfs4_acl = acl;
3654 spin_unlock(&inode->i_lock);
3655}
3656
3657static void nfs4_zap_acl_attr(struct inode *inode)
3658{
3659 nfs4_set_cached_acl(inode, NULL);
3660}
3661
3662static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3663{
3664 struct nfs_inode *nfsi = NFS_I(inode);
3665 struct nfs4_cached_acl *acl;
3666 int ret = -ENOENT;
3667
3668 spin_lock(&inode->i_lock);
3669 acl = nfsi->nfs4_acl;
3670 if (acl == NULL)
3671 goto out;
3672 if (buf == NULL) /* user is just asking for length */
3673 goto out_len;
3674 if (acl->cached == 0)
3675 goto out;
3676 ret = -ERANGE; /* see getxattr(2) man page */
3677 if (acl->len > buflen)
3678 goto out;
3679 memcpy(buf, acl->data, acl->len);
3680out_len:
3681 ret = acl->len;
3682out:
3683 spin_unlock(&inode->i_lock);
3684 return ret;
3685}
3686
Sachin Prabhu5794d212012-04-17 14:36:40 +01003687static 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 +00003688{
3689 struct nfs4_cached_acl *acl;
3690
Sachin Prabhu5794d212012-04-17 14:36:40 +01003691 if (pages && acl_len <= PAGE_SIZE) {
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003692 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3693 if (acl == NULL)
3694 goto out;
3695 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003696 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003697 } else {
3698 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3699 if (acl == NULL)
3700 goto out;
3701 acl->cached = 0;
3702 }
3703 acl->len = acl_len;
3704out:
3705 nfs4_set_cached_acl(inode, acl);
3706}
3707
Andy Adamsonbf118a32011-12-07 11:55:27 -05003708/*
3709 * The getxattr API returns the required buffer length when called with a
3710 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3711 * the required buf. On a NULL buf, we send a page of data to the server
3712 * guessing that the ACL request can be serviced by a page. If so, we cache
3713 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3714 * the cache. If not so, we throw away the page, and cache the required
3715 * length. The next getxattr call will then produce another round trip to
3716 * the server, this time with the input buf of the required size.
3717 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003718static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003719{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003720 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003721 struct nfs_getaclargs args = {
3722 .fh = NFS_FH(inode),
3723 .acl_pages = pages,
3724 .acl_len = buflen,
3725 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003726 struct nfs_getaclres res = {
3727 .acl_len = buflen,
3728 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003729 struct rpc_message msg = {
3730 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3731 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003732 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003733 };
Chuck Leverf40661f2012-07-11 16:30:32 -04003734 int ret = -ENOMEM, npages, i;
3735 size_t acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003736
Andy Adamsonbf118a32011-12-07 11:55:27 -05003737 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3738 /* As long as we're doing a round trip to the server anyway,
3739 * let's be prepared for a page of acl data. */
3740 if (npages == 0)
3741 npages = 1;
3742
Sachin Prabhu5a006892012-04-17 14:35:39 +01003743 /* Add an extra page to handle the bitmap returned */
3744 npages++;
3745
Andy Adamsonbf118a32011-12-07 11:55:27 -05003746 for (i = 0; i < npages; i++) {
3747 pages[i] = alloc_page(GFP_KERNEL);
3748 if (!pages[i])
3749 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003750 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003751
3752 /* for decoding across pages */
3753 res.acl_scratch = alloc_page(GFP_KERNEL);
3754 if (!res.acl_scratch)
3755 goto out_free;
3756
Andy Adamsonbf118a32011-12-07 11:55:27 -05003757 args.acl_len = npages * PAGE_SIZE;
3758 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003759
Andy Adamsonbf118a32011-12-07 11:55:27 -05003760 /* Let decode_getfacl know not to fail if the ACL data is larger than
3761 * the page we send as a guess */
3762 if (buf == NULL)
3763 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003764
Peng Taode040be2012-01-10 22:42:47 +08003765 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003766 __func__, buf, buflen, npages, args.acl_len);
3767 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3768 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003769 if (ret)
3770 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003771
3772 acl_len = res.acl_len - res.acl_data_offset;
3773 if (acl_len > args.acl_len)
Sachin Prabhu5794d212012-04-17 14:36:40 +01003774 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003775 else
Sachin Prabhu5794d212012-04-17 14:36:40 +01003776 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
Andy Adamsonbf118a32011-12-07 11:55:27 -05003777 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003778 if (buf) {
3779 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003780 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003781 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003782 _copy_from_pages(buf, pages, res.acl_data_offset,
Sachin Prabhu20e0fa92012-03-22 16:46:28 +00003783 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003784 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003785 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003786out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003787 for (i = 0; i < npages; i++)
3788 if (pages[i])
3789 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003790 if (res.acl_scratch)
3791 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003792 return ret;
3793}
3794
Trond Myklebust16b42892006-08-24 12:27:15 -04003795static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3796{
3797 struct nfs4_exception exception = { };
3798 ssize_t ret;
3799 do {
3800 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3801 if (ret >= 0)
3802 break;
3803 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3804 } while (exception.retry);
3805 return ret;
3806}
3807
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003808static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3809{
3810 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003811 int ret;
3812
3813 if (!nfs4_server_supports_acls(server))
3814 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003815 ret = nfs_revalidate_inode(server, inode);
3816 if (ret < 0)
3817 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003818 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3819 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003820 ret = nfs4_read_cached_acl(inode, buf, buflen);
3821 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003822 /* -ENOENT is returned if there is no ACL or if there is an ACL
3823 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003824 return ret;
3825 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003826}
3827
Trond Myklebust16b42892006-08-24 12:27:15 -04003828static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003829{
3830 struct nfs_server *server = NFS_SERVER(inode);
3831 struct page *pages[NFS4ACL_MAXPAGES];
3832 struct nfs_setaclargs arg = {
3833 .fh = NFS_FH(inode),
3834 .acl_pages = pages,
3835 .acl_len = buflen,
3836 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003837 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003838 struct rpc_message msg = {
3839 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3840 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003841 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003842 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003843 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003844
3845 if (!nfs4_server_supports_acls(server))
3846 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003847 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3848 if (i < 0)
3849 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003850 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003851 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003852
3853 /*
3854 * Free each page after tx, so the only ref left is
3855 * held by the network stack
3856 */
3857 for (; i > 0; i--)
3858 put_page(pages[i-1]);
3859
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003860 /*
3861 * Acl update can result in inode attribute update.
3862 * so mark the attribute cache invalid.
3863 */
3864 spin_lock(&inode->i_lock);
3865 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3866 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003867 nfs_access_zap_cache(inode);
3868 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003869 return ret;
3870}
3871
Trond Myklebust16b42892006-08-24 12:27:15 -04003872static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3873{
3874 struct nfs4_exception exception = { };
3875 int err;
3876 do {
3877 err = nfs4_handle_exception(NFS_SERVER(inode),
3878 __nfs4_proc_set_acl(inode, buf, buflen),
3879 &exception);
3880 } while (exception.retry);
3881 return err;
3882}
3883
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003885nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003887 struct nfs_client *clp = server->nfs_client;
3888
3889 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 return 0;
3891 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003892 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003893 case -NFS4ERR_ADMIN_REVOKED:
3894 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003895 if (state == NULL)
3896 break;
3897 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003898 case -NFS4ERR_OPENMODE:
3899 if (state == NULL)
3900 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003901 nfs4_schedule_stateid_recovery(server, state);
3902 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003903 case -NFS4ERR_EXPIRED:
3904 if (state != NULL)
3905 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003907 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003908 nfs4_schedule_lease_recovery(clp);
3909 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003910#if defined(CONFIG_NFS_V4_1)
3911 case -NFS4ERR_BADSESSION:
3912 case -NFS4ERR_BADSLOT:
3913 case -NFS4ERR_BAD_HIGH_SLOT:
3914 case -NFS4ERR_DEADSESSION:
3915 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3916 case -NFS4ERR_SEQ_FALSE_RETRY:
3917 case -NFS4ERR_SEQ_MISORDERED:
3918 dprintk("%s ERROR %d, Reset session\n", __func__,
3919 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003920 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamsone9d735e2013-11-15 16:36:16 -05003921 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003922#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003924 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003925 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3927 task->tk_status = 0;
3928 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003929 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 case -NFS4ERR_OLD_STATEID:
3931 task->tk_status = 0;
3932 return -EAGAIN;
3933 }
3934 task->tk_status = nfs4_map_errors(task->tk_status);
3935 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003936wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003937 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003938 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3939 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3940 task->tk_status = 0;
3941 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942}
3943
Chuck Levercd937102012-03-02 17:14:31 -05003944static void nfs4_construct_boot_verifier(struct nfs_client *clp,
3945 nfs4_verifier *bootverf)
3946{
3947 __be32 verf[2];
3948
3949 verf[0] = htonl((u32)clp->cl_boot_time.tv_sec);
3950 verf[1] = htonl((u32)clp->cl_boot_time.tv_nsec);
3951 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3952}
3953
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003954int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3955 unsigned short port, struct rpc_cred *cred,
3956 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957{
3958 nfs4_verifier sc_verifier;
3959 struct nfs4_setclientid setclientid = {
3960 .sc_verifier = &sc_verifier,
3961 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003962 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 };
3964 struct rpc_message msg = {
3965 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3966 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003967 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003968 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 int loop = 0;
3971 int status;
3972
Chuck Levercd937102012-03-02 17:14:31 -05003973 nfs4_construct_boot_verifier(clp, &sc_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974
3975 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05003976 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003978 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003979 clp->cl_ipaddr,
3980 rpc_peeraddr2str(clp->cl_rpcclient,
3981 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003982 rpc_peeraddr2str(clp->cl_rpcclient,
3983 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003984 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 clp->cl_id_uniquifier);
3986 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003987 sizeof(setclientid.sc_netid),
3988 rpc_peeraddr2str(clp->cl_rpcclient,
3989 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003991 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05003993 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003995 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 if (status != -NFS4ERR_CLID_INUSE)
3997 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003998 if (loop != 0) {
3999 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004001 }
4002 ++loop;
4003 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 }
4005 return status;
4006}
4007
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004008int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004009 struct nfs4_setclientid_res *arg,
4010 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011{
4012 struct nfs_fsinfo fsinfo;
4013 struct rpc_message msg = {
4014 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004015 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004017 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 };
4019 unsigned long now;
4020 int status;
4021
4022 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004023 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 if (status == 0) {
4025 spin_lock(&clp->cl_lock);
4026 clp->cl_lease_time = fsinfo.lease_time * HZ;
4027 clp->cl_last_renewal = now;
4028 spin_unlock(&clp->cl_lock);
4029 }
4030 return status;
4031}
4032
Trond Myklebustfe650402006-01-03 09:55:18 +01004033struct nfs4_delegreturndata {
4034 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004035 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004036 struct nfs_fh fh;
4037 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004038 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004039 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004040 int rpc_status;
4041};
4042
Trond Myklebustfe650402006-01-03 09:55:18 +01004043static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4044{
4045 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004046
Trond Myklebust14516c32010-07-31 14:29:06 -04004047 if (!nfs4_sequence_done(task, &data->res.seq_res))
4048 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004049
Ricardo Labiaga79708862009-12-07 09:23:21 -05004050 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05004051 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004052 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004053 break;
Trond Myklebusta1a84b62013-11-19 16:34:14 -05004054 case -NFS4ERR_ADMIN_REVOKED:
4055 case -NFS4ERR_DELEG_REVOKED:
4056 case -NFS4ERR_BAD_STATEID:
4057 case -NFS4ERR_OLD_STATEID:
4058 case -NFS4ERR_STALE_STATEID:
4059 case -NFS4ERR_EXPIRED:
4060 task->tk_status = 0;
4061 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05004062 default:
4063 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4064 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004065 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004066 return;
4067 }
4068 }
4069 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004070}
4071
4072static void nfs4_delegreturn_release(void *calldata)
4073{
Trond Myklebustfe650402006-01-03 09:55:18 +01004074 kfree(calldata);
4075}
4076
Andy Adamson938e1012009-04-01 09:22:28 -04004077#if defined(CONFIG_NFS_V4_1)
4078static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4079{
4080 struct nfs4_delegreturndata *d_data;
4081
4082 d_data = (struct nfs4_delegreturndata *)data;
4083
Trond Myklebust035168a2010-06-16 09:52:26 -04004084 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004085 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004086 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004087 return;
4088 rpc_call_start(task);
4089}
4090#endif /* CONFIG_NFS_V4_1 */
4091
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004092static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004093#if defined(CONFIG_NFS_V4_1)
4094 .rpc_call_prepare = nfs4_delegreturn_prepare,
4095#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004096 .rpc_call_done = nfs4_delegreturn_done,
4097 .rpc_release = nfs4_delegreturn_release,
4098};
4099
Trond Myklebuste6f81072008-01-24 18:14:34 -05004100static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004101{
4102 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004103 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004104 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004105 struct rpc_message msg = {
4106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4107 .rpc_cred = cred,
4108 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004109 struct rpc_task_setup task_setup_data = {
4110 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004111 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004112 .callback_ops = &nfs4_delegreturn_ops,
4113 .flags = RPC_TASK_ASYNC,
4114 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004115 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004116
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004117 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004118 if (data == NULL)
4119 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004120 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004121 data->args.fhandle = &data->fh;
4122 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004123 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004124 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004125 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004126 data->res.fattr = &data->fattr;
4127 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004128 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004129 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004130 data->rpc_status = 0;
4131
Trond Myklebustc970aa82007-07-14 15:39:59 -04004132 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004133 msg.rpc_argp = &data->args;
4134 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004135 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004136 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004137 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004138 if (!issync)
4139 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004140 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004141 if (status != 0)
4142 goto out;
4143 status = data->rpc_status;
4144 if (status != 0)
4145 goto out;
4146 nfs_refresh_inode(inode, &data->fattr);
4147out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004148 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004149 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150}
4151
Trond Myklebuste6f81072008-01-24 18:14:34 -05004152int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153{
4154 struct nfs_server *server = NFS_SERVER(inode);
4155 struct nfs4_exception exception = { };
4156 int err;
4157 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004158 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 switch (err) {
4160 case -NFS4ERR_STALE_STATEID:
4161 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 case 0:
4163 return 0;
4164 }
4165 err = nfs4_handle_exception(server, err, &exception);
4166 } while (exception.retry);
4167 return err;
4168}
4169
4170#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4171#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4172
4173/*
4174 * sleep, with exponential backoff, and retry the LOCK operation.
4175 */
4176static unsigned long
4177nfs4_set_lock_task_retry(unsigned long timeout)
4178{
Colin Cross3be91672013-05-06 23:50:06 +00004179 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 timeout <<= 1;
4181 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4182 return NFS4_LOCK_MAXTIMEOUT;
4183 return timeout;
4184}
4185
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4187{
4188 struct inode *inode = state->inode;
4189 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004190 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004191 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004193 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004195 struct nfs_lockt_res res = {
4196 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 };
4198 struct rpc_message msg = {
4199 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4200 .rpc_argp = &arg,
4201 .rpc_resp = &res,
4202 .rpc_cred = state->owner->so_cred,
4203 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 struct nfs4_lock_state *lsp;
4205 int status;
4206
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004207 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004208 status = nfs4_set_lock_state(state, request);
4209 if (status != 0)
4210 goto out;
4211 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004212 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004213 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004214 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004215 switch (status) {
4216 case 0:
4217 request->fl_type = F_UNLCK;
4218 break;
4219 case -NFS4ERR_DENIED:
4220 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004222 request->fl_ops->fl_release_private(request);
Trond Myklebust1ac50b92013-10-01 14:24:58 -04004223 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004224out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 return status;
4226}
4227
4228static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4229{
4230 struct nfs4_exception exception = { };
4231 int err;
4232
4233 do {
4234 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4235 _nfs4_proc_getlk(state, cmd, request),
4236 &exception);
4237 } while (exception.retry);
4238 return err;
4239}
4240
4241static int do_vfs_lock(struct file *file, struct file_lock *fl)
4242{
4243 int res = 0;
4244 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4245 case FL_POSIX:
4246 res = posix_lock_file_wait(file, fl);
4247 break;
4248 case FL_FLOCK:
4249 res = flock_lock_file_wait(file, fl);
4250 break;
4251 default:
4252 BUG();
4253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 return res;
4255}
4256
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004257struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004258 struct nfs_locku_args arg;
4259 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004260 struct nfs4_lock_state *lsp;
4261 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004262 struct file_lock fl;
4263 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004264 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004265};
4266
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004267static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4268 struct nfs_open_context *ctx,
4269 struct nfs4_lock_state *lsp,
4270 struct nfs_seqid *seqid)
4271{
4272 struct nfs4_unlockdata *p;
4273 struct inode *inode = lsp->ls_state->inode;
4274
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004275 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004276 if (p == NULL)
4277 return NULL;
4278 p->arg.fh = NFS_FH(inode);
4279 p->arg.fl = &p->fl;
4280 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004281 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004282 p->arg.stateid = &lsp->ls_stateid;
4283 p->lsp = lsp;
4284 atomic_inc(&lsp->ls_count);
4285 /* Ensure we don't close file until we're done freeing locks! */
4286 p->ctx = get_nfs_open_context(ctx);
4287 memcpy(&p->fl, fl, sizeof(p->fl));
4288 p->server = NFS_SERVER(inode);
4289 return p;
4290}
4291
Trond Myklebust06f814a2006-01-03 09:55:07 +01004292static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004293{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004294 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004295 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004296 nfs4_put_lock_state(calldata->lsp);
4297 put_nfs_open_context(calldata->ctx);
4298 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004299}
4300
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004301static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004302{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004303 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004304
Trond Myklebust14516c32010-07-31 14:29:06 -04004305 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4306 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004307 switch (task->tk_status) {
4308 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004309 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4310 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004311 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004312 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004313 case -NFS4ERR_BAD_STATEID:
4314 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004315 case -NFS4ERR_STALE_STATEID:
4316 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004317 break;
4318 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004319 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004320 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004321 }
Trond Myklebust8827f312012-10-29 18:53:23 -04004322 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004323}
4324
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004325static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004326{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004327 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004329 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004330 return;
4331 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004332 /* Note: exit _without_ running nfs4_locku_done */
4333 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004334 return;
4335 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004336 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004337 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004338 &calldata->arg.seq_args,
Trond Myklebust0ada2102012-10-29 18:37:40 -04004339 &calldata->res.seq_res,
4340 task) != 0)
4341 nfs_release_seqid(calldata->arg.seqid);
4342 else
4343 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344}
4345
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004346static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004347 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004348 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004349 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004350};
4351
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004352static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4353 struct nfs_open_context *ctx,
4354 struct nfs4_lock_state *lsp,
4355 struct nfs_seqid *seqid)
4356{
4357 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004358 struct rpc_message msg = {
4359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4360 .rpc_cred = ctx->cred,
4361 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004362 struct rpc_task_setup task_setup_data = {
4363 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004364 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004365 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004366 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004367 .flags = RPC_TASK_ASYNC,
4368 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004369
Frank Filz137d6ac2007-07-09 15:32:29 -07004370 /* Ensure this is an unlock - when canceling a lock, the
4371 * canceled lock is passed in, and it won't be an unlock.
4372 */
4373 fl->fl_type = F_UNLCK;
4374
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004375 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4376 if (data == NULL) {
4377 nfs_free_seqid(seqid);
4378 return ERR_PTR(-ENOMEM);
4379 }
4380
Trond Myklebust9d12b212012-01-17 22:04:25 -05004381 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004382 msg.rpc_argp = &data->arg;
4383 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004384 task_setup_data.callback_data = data;
4385 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004386}
4387
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4389{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004390 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004391 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004392 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004393 struct rpc_task *task;
4394 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004395 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396
Trond Myklebust9b073572006-06-29 16:38:34 -04004397 status = nfs4_set_lock_state(state, request);
4398 /* Unlock _before_ we do the RPC call */
4399 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004400 down_read(&nfsi->rwsem);
4401 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4402 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004403 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004404 }
4405 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004406 if (status != 0)
4407 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004408 /* Is this a delegated lock? */
4409 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004410 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004411 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004412 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004413 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004414 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004415 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004416 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004417 status = PTR_ERR(task);
4418 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004419 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004420 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004421 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004422out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004423 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004424 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425}
4426
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004427struct nfs4_lockdata {
4428 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004429 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004430 struct nfs4_lock_state *lsp;
4431 struct nfs_open_context *ctx;
4432 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004433 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004434 int rpc_status;
4435 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004436 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004437};
4438
4439static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004440 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4441 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004442{
4443 struct nfs4_lockdata *p;
4444 struct inode *inode = lsp->ls_state->inode;
4445 struct nfs_server *server = NFS_SERVER(inode);
4446
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004447 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004448 if (p == NULL)
4449 return NULL;
4450
4451 p->arg.fh = NFS_FH(inode);
4452 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004453 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004454 if (p->arg.open_seqid == NULL)
4455 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004456 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004457 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004458 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004459 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004460 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004461 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004462 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004463 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004464 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004465 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004466 atomic_inc(&lsp->ls_count);
4467 p->ctx = get_nfs_open_context(ctx);
4468 memcpy(&p->fl, fl, sizeof(p->fl));
4469 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004470out_free_seqid:
4471 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004472out_free:
4473 kfree(p);
4474 return NULL;
4475}
4476
4477static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4478{
4479 struct nfs4_lockdata *data = calldata;
4480 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
Harvey Harrison3110ff82008-05-02 13:42:44 -07004482 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004483 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4484 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004485 /* Do we need to do an open_to_lock_owner? */
4486 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004487 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust0ada2102012-10-29 18:37:40 -04004488 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004489 data->arg.open_stateid = &state->stateid;
4490 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004491 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004492 } else
4493 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004494 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004495 if (nfs4_setup_sequence(data->server,
4496 &data->arg.seq_args,
Trond Myklebust0ada2102012-10-29 18:37:40 -04004497 &data->res.seq_res,
4498 task) == 0) {
4499 rpc_call_start(task);
Andy Adamson66179ef2009-04-01 09:22:22 -04004500 return;
Trond Myklebust0ada2102012-10-29 18:37:40 -04004501 }
4502 nfs_release_seqid(data->arg.open_seqid);
4503out_release_lock_seqid:
4504 nfs_release_seqid(data->arg.lock_seqid);
4505 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004506}
4507
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004508static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4509{
4510 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4511 nfs4_lock_prepare(task, calldata);
4512}
4513
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004514static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4515{
4516 struct nfs4_lockdata *data = calldata;
4517
Harvey Harrison3110ff82008-05-02 13:42:44 -07004518 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004519
Trond Myklebust14516c32010-07-31 14:29:06 -04004520 if (!nfs4_sequence_done(task, &data->res.seq_res))
4521 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004522
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004523 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004524 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004525 if (data->rpc_status == 0)
4526 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4527 else
4528 goto out;
4529 }
4530 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004531 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004532 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004533 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004534 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004535out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004536 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004537}
4538
4539static void nfs4_lock_release(void *calldata)
4540{
4541 struct nfs4_lockdata *data = calldata;
4542
Harvey Harrison3110ff82008-05-02 13:42:44 -07004543 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004544 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004545 if (data->cancelled != 0) {
4546 struct rpc_task *task;
4547 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4548 data->arg.lock_seqid);
4549 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004550 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004551 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004552 } else
4553 nfs_free_seqid(data->arg.lock_seqid);
4554 nfs4_put_lock_state(data->lsp);
4555 put_nfs_open_context(data->ctx);
4556 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004557 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004558}
4559
4560static const struct rpc_call_ops nfs4_lock_ops = {
4561 .rpc_call_prepare = nfs4_lock_prepare,
4562 .rpc_call_done = nfs4_lock_done,
4563 .rpc_release = nfs4_lock_release,
4564};
4565
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004566static const struct rpc_call_ops nfs4_recover_lock_ops = {
4567 .rpc_call_prepare = nfs4_recover_lock_prepare,
4568 .rpc_call_done = nfs4_lock_done,
4569 .rpc_release = nfs4_lock_release,
4570};
4571
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004572static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4573{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004574 switch (error) {
4575 case -NFS4ERR_ADMIN_REVOKED:
4576 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004577 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004578 if (new_lock_owner != 0 ||
4579 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004580 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004581 break;
4582 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004583 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004584 case -NFS4ERR_EXPIRED:
4585 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004586 };
4587}
4588
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004589static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004590{
4591 struct nfs4_lockdata *data;
4592 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004593 struct rpc_message msg = {
4594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4595 .rpc_cred = state->owner->so_cred,
4596 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004597 struct rpc_task_setup task_setup_data = {
4598 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004599 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004600 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004601 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004602 .flags = RPC_TASK_ASYNC,
4603 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004604 int ret;
4605
Harvey Harrison3110ff82008-05-02 13:42:44 -07004606 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004607 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004608 fl->fl_u.nfs4_fl.owner,
4609 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004610 if (data == NULL)
4611 return -ENOMEM;
4612 if (IS_SETLKW(cmd))
4613 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004614 if (recovery_type > NFS_LOCK_NEW) {
4615 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004616 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004617 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4618 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004619 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004620 msg.rpc_argp = &data->arg;
4621 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004622 task_setup_data.callback_data = data;
4623 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004624 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004625 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004626 ret = nfs4_wait_for_completion_rpc_task(task);
4627 if (ret == 0) {
4628 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004629 if (ret)
4630 nfs4_handle_setlk_error(data->server, data->lsp,
4631 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004632 } else
4633 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004634 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004635 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004636 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637}
4638
4639static int nfs4_lock_reclaim(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
4647 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004648 /* Cache the lock if possible... */
4649 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4650 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004651 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004652 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004653 break;
4654 nfs4_handle_exception(server, err, &exception);
4655 } while (exception.retry);
4656 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657}
4658
4659static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4660{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004661 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004662 struct nfs4_exception exception = {
4663 .inode = state->inode,
4664 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004665 int err;
4666
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004667 err = nfs4_set_lock_state(state, request);
4668 if (err != 0)
4669 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004670 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004671 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4672 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004673 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004674 switch (err) {
4675 default:
4676 goto out;
4677 case -NFS4ERR_GRACE:
4678 case -NFS4ERR_DELAY:
4679 nfs4_handle_exception(server, err, &exception);
4680 err = 0;
4681 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004682 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004683out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004684 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685}
4686
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004687#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004688static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004689{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004690 int status, ret = NFS_OK;
4691 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004692 struct nfs_server *server = NFS_SERVER(state->inode);
4693
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004694 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4695 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4696 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4697 if (status != NFS_OK) {
4698 nfs41_free_stateid(server, &lsp->ls_stateid);
4699 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4700 ret = status;
4701 }
4702 }
4703 };
4704
4705 return ret;
4706}
4707
4708static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4709{
4710 int status = NFS_OK;
4711
4712 if (test_bit(LK_STATE_IN_USE, &state->flags))
4713 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004714 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004715 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004716 return nfs4_lock_expired(state, request);
4717}
4718#endif
4719
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4721{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004722 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004723 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004724 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004726 if ((fl_flags & FL_POSIX) &&
4727 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4728 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004729 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004730 status = nfs4_set_lock_state(state, request);
4731 if (status != 0)
4732 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004733 request->fl_flags |= FL_ACCESS;
4734 status = do_vfs_lock(request->fl_file, request);
4735 if (status < 0)
4736 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004737 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004738 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004739 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004740 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004741 request->fl_flags = fl_flags & ~FL_SLEEP;
4742 status = do_vfs_lock(request->fl_file, request);
4743 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004744 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004745 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004746 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004747 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004748 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004749 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004750 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004751 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4752 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004753out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004754 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004755out:
4756 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 return status;
4758}
4759
4760static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4761{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004762 struct nfs4_exception exception = {
4763 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004764 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004765 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 int err;
4767
4768 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004769 err = _nfs4_proc_setlk(state, cmd, request);
4770 if (err == -NFS4ERR_DENIED)
4771 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004773 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 } while (exception.retry);
4775 return err;
4776}
4777
4778static int
4779nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4780{
4781 struct nfs_open_context *ctx;
4782 struct nfs4_state *state;
4783 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4784 int status;
4785
4786 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004787 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 state = ctx->state;
4789
4790 if (request->fl_start < 0 || request->fl_end < 0)
4791 return -EINVAL;
4792
Trond Myklebustd9531262009-07-21 19:22:38 -04004793 if (IS_GETLK(cmd)) {
4794 if (state != NULL)
4795 return nfs4_proc_getlk(state, F_GETLK, request);
4796 return 0;
4797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
4799 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4800 return -EINVAL;
4801
Trond Myklebustd9531262009-07-21 19:22:38 -04004802 if (request->fl_type == F_UNLCK) {
4803 if (state != NULL)
4804 return nfs4_proc_unlck(state, cmd, request);
4805 return 0;
4806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
Trond Myklebustd9531262009-07-21 19:22:38 -04004808 if (state == NULL)
4809 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004810 /*
4811 * Don't rely on the VFS having checked the file open mode,
4812 * since it won't do this for flock() locks.
4813 */
4814 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4815 case F_RDLCK:
4816 if (!(filp->f_mode & FMODE_READ))
4817 return -EBADF;
4818 break;
4819 case F_WRLCK:
4820 if (!(filp->f_mode & FMODE_WRITE))
4821 return -EBADF;
4822 }
4823
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 do {
4825 status = nfs4_proc_setlk(state, cmd, request);
4826 if ((status != -EAGAIN) || IS_SETLK(cmd))
4827 break;
4828 timeout = nfs4_set_lock_task_retry(timeout);
4829 status = -ERESTARTSYS;
4830 if (signalled())
4831 break;
4832 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 return status;
4834}
4835
Trond Myklebust888e6942005-11-04 15:38:11 -05004836int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4837{
4838 struct nfs_server *server = NFS_SERVER(state->inode);
4839 struct nfs4_exception exception = { };
4840 int err;
4841
4842 err = nfs4_set_lock_state(state, fl);
4843 if (err != 0)
4844 goto out;
4845 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004846 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004847 switch (err) {
4848 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004849 printk(KERN_ERR "NFS: %s: unhandled error "
4850 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004851 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004852 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004853 goto out;
4854 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004855 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004856 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004857 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004858 nfs4_schedule_lease_recovery(server->nfs_client);
4859 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004860 case -NFS4ERR_BADSESSION:
4861 case -NFS4ERR_BADSLOT:
4862 case -NFS4ERR_BAD_HIGH_SLOT:
4863 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4864 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004865 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004866 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004867 case -ERESTARTSYS:
4868 /*
4869 * The show must go on: exit, but mark the
4870 * stateid as needing recovery.
4871 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004872 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004873 case -NFS4ERR_ADMIN_REVOKED:
4874 case -NFS4ERR_BAD_STATEID:
4875 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004876 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004877 err = 0;
4878 goto out;
4879 case -ENOMEM:
4880 case -NFS4ERR_DENIED:
4881 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4882 err = 0;
4883 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004884 case -NFS4ERR_DELAY:
4885 break;
4886 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004887 err = nfs4_handle_exception(server, err, &exception);
4888 } while (exception.retry);
4889out:
4890 return err;
4891}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004892
Trond Myklebustcf470c32012-03-07 13:49:12 -05004893struct nfs_release_lockowner_data {
4894 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004895 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004896 struct nfs_release_lockowner_args args;
4897};
4898
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004899static void nfs4_release_lockowner_release(void *calldata)
4900{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004901 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004902 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004903 kfree(calldata);
4904}
4905
Trond Myklebust17280172012-03-11 13:11:00 -04004906static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004907 .rpc_release = nfs4_release_lockowner_release,
4908};
4909
Trond Myklebustcf470c32012-03-07 13:49:12 -05004910int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004911{
4912 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004913 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004914 struct rpc_message msg = {
4915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4916 };
4917
4918 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004919 return -EINVAL;
4920 data = kmalloc(sizeof(*data), GFP_NOFS);
4921 if (!data)
4922 return -ENOMEM;
4923 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004924 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004925 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4926 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4927 data->args.lock_owner.s_dev = server->s_dev;
4928 msg.rpc_argp = &data->args;
4929 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4930 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004931}
4932
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004933#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4934
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004935static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4936 const void *buf, size_t buflen,
4937 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004938{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004939 if (strcmp(key, "") != 0)
4940 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004941
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004942 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004943}
4944
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004945static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4946 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004947{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004948 if (strcmp(key, "") != 0)
4949 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004950
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004951 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004952}
4953
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004954static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4955 size_t list_len, const char *name,
4956 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004957{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004958 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004959
J. Bruce Fields096455a2006-03-20 23:23:42 -05004960 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4961 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004962
4963 if (list && len <= list_len)
4964 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004965 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004966}
4967
Andy Adamson533eb462011-06-13 18:25:56 -04004968/*
4969 * nfs_fhget will use either the mounted_on_fileid or the fileid
4970 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004971static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4972{
Andy Adamson533eb462011-06-13 18:25:56 -04004973 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4974 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4975 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05004976 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004977 return;
4978
4979 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05004980 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004981 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4982 fattr->nlink = 2;
4983}
4984
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004985static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
4986 const struct qstr *name,
4987 struct nfs4_fs_locations *fs_locations,
4988 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004989{
4990 struct nfs_server *server = NFS_SERVER(dir);
4991 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004992 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004993 };
4994 struct nfs4_fs_locations_arg args = {
4995 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004996 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004997 .page = page,
4998 .bitmask = bitmask,
4999 };
Benny Halevy22958462009-04-01 09:22:02 -04005000 struct nfs4_fs_locations_res res = {
5001 .fs_locations = fs_locations,
5002 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005003 struct rpc_message msg = {
5004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5005 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005006 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005007 };
5008 int status;
5009
Harvey Harrison3110ff82008-05-02 13:42:44 -07005010 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005011
5012 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5013 * is not supported */
5014 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5015 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5016 else
5017 bitmask[0] |= FATTR4_WORD0_FILEID;
5018
Trond Myklebustc228fd32007-01-13 02:28:11 -05005019 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005020 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005021 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005022 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005023 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005024 return status;
5025}
5026
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005027int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5028 const struct qstr *name,
5029 struct nfs4_fs_locations *fs_locations,
5030 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005031{
5032 struct nfs4_exception exception = { };
5033 int err;
5034 do {
5035 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005036 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005037 &exception);
5038 } while (exception.retry);
5039 return err;
5040}
5041
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005042static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5043{
5044 int status;
5045 struct nfs4_secinfo_arg args = {
5046 .dir_fh = NFS_FH(dir),
5047 .name = name,
5048 };
5049 struct nfs4_secinfo_res res = {
5050 .flavors = flavors,
5051 };
5052 struct rpc_message msg = {
5053 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5054 .rpc_argp = &args,
5055 .rpc_resp = &res,
5056 };
5057
5058 dprintk("NFS call secinfo %s\n", name->name);
5059 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5060 dprintk("NFS reply secinfo: %d\n", status);
5061 return status;
5062}
5063
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005064int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5065 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005066{
5067 struct nfs4_exception exception = { };
5068 int err;
5069 do {
5070 err = nfs4_handle_exception(NFS_SERVER(dir),
5071 _nfs4_proc_secinfo(dir, name, flavors),
5072 &exception);
5073 } while (exception.retry);
5074 return err;
5075}
5076
Andy Adamson557134a2009-04-01 09:21:53 -04005077#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005078/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005079 * Check the exchange flags returned by the server for invalid flags, having
5080 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5081 * DS flags set.
5082 */
5083static int nfs4_check_cl_exchange_flags(u32 flags)
5084{
5085 if (flags & ~EXCHGID4_FLAG_MASK_R)
5086 goto out_inval;
5087 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5088 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5089 goto out_inval;
5090 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5091 goto out_inval;
5092 return NFS_OK;
5093out_inval:
5094 return -NFS4ERR_INVAL;
5095}
5096
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005097static bool
5098nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
5099{
5100 if (a->server_scope_sz == b->server_scope_sz &&
5101 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5102 return true;
5103
5104 return false;
5105}
5106
Andy Adamson357f54d2010-12-14 10:11:57 -05005107/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005108 * nfs4_proc_exchange_id()
5109 *
5110 * Since the clientid has expired, all compounds using sessions
5111 * associated with the stale clientid will be returning
5112 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5113 * be in some phase of session reset.
5114 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005115int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005116{
5117 nfs4_verifier verifier;
5118 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005119 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005120 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005121 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005122 };
5123 struct nfs41_exchange_id_res res = {
5124 .client = clp,
5125 };
5126 int status;
5127 struct rpc_message msg = {
5128 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5129 .rpc_argp = &args,
5130 .rpc_resp = &res,
5131 .rpc_cred = cred,
5132 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005133
5134 dprintk("--> %s\n", __func__);
5135 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04005136
Chuck Levercd937102012-03-02 17:14:31 -05005137 nfs4_construct_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005138
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005139 args.id_len = scnprintf(args.id, sizeof(args.id),
Trond Myklebust3617e502012-04-30 12:04:58 -04005140 "%s/%s/%u",
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005141 clp->cl_ipaddr,
Trond Myklebust3617e502012-04-30 12:04:58 -04005142 clp->cl_rpcclient->cl_nodename,
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005143 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005144
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005145 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005146 if (unlikely(!res.server_scope)) {
5147 status = -ENOMEM;
5148 goto out;
5149 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005150
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005151 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
5152 if (unlikely(!res.impl_id)) {
5153 status = -ENOMEM;
5154 goto out_server_scope;
5155 }
5156
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005157 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005158 if (!status)
5159 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005160
5161 if (!status) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005162 /* use the most recent implementation id */
5163 kfree(clp->impl_id);
5164 clp->impl_id = res.impl_id;
5165 } else
5166 kfree(res.impl_id);
5167
5168 if (!status) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005169 if (clp->server_scope &&
5170 !nfs41_same_server_scope(clp->server_scope,
5171 res.server_scope)) {
5172 dprintk("%s: server_scope mismatch detected\n",
5173 __func__);
5174 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5175 kfree(clp->server_scope);
5176 clp->server_scope = NULL;
5177 }
5178
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005179 if (!clp->server_scope) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005180 clp->server_scope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005181 goto out;
5182 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005183 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005184
5185out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005186 kfree(res.server_scope);
5187out:
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005188 if (clp->impl_id)
5189 dprintk("%s: Server Implementation ID: "
5190 "domain: %s, name: %s, date: %llu,%u\n",
5191 __func__, clp->impl_id->domain, clp->impl_id->name,
5192 clp->impl_id->date.seconds,
5193 clp->impl_id->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005194 dprintk("<-- %s status= %d\n", __func__, status);
5195 return status;
5196}
5197
Andy Adamson2050f0c2009-04-01 09:22:30 -04005198struct nfs4_get_lease_time_data {
5199 struct nfs4_get_lease_time_args *args;
5200 struct nfs4_get_lease_time_res *res;
5201 struct nfs_client *clp;
5202};
5203
5204static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5205 void *calldata)
5206{
5207 int ret;
5208 struct nfs4_get_lease_time_data *data =
5209 (struct nfs4_get_lease_time_data *)calldata;
5210
5211 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005212 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005213 /* just setup sequence, do not trigger session recovery
5214 since we're invoked within one */
5215 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005216 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005217 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005218
5219 BUG_ON(ret == -EAGAIN);
5220 rpc_call_start(task);
5221 dprintk("<-- %s\n", __func__);
5222}
5223
5224/*
5225 * Called from nfs4_state_manager thread for session setup, so don't recover
5226 * from sequence operation or clientid errors.
5227 */
5228static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5229{
5230 struct nfs4_get_lease_time_data *data =
5231 (struct nfs4_get_lease_time_data *)calldata;
5232
5233 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005234 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5235 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005236 switch (task->tk_status) {
5237 case -NFS4ERR_DELAY:
5238 case -NFS4ERR_GRACE:
5239 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5240 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5241 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005242 /* fall through */
5243 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005244 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005245 return;
5246 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005247 dprintk("<-- %s\n", __func__);
5248}
5249
Trond Myklebust17280172012-03-11 13:11:00 -04005250static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005251 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5252 .rpc_call_done = nfs4_get_lease_time_done,
5253};
5254
5255int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5256{
5257 struct rpc_task *task;
5258 struct nfs4_get_lease_time_args args;
5259 struct nfs4_get_lease_time_res res = {
5260 .lr_fsinfo = fsinfo,
5261 };
5262 struct nfs4_get_lease_time_data data = {
5263 .args = &args,
5264 .res = &res,
5265 .clp = clp,
5266 };
5267 struct rpc_message msg = {
5268 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5269 .rpc_argp = &args,
5270 .rpc_resp = &res,
5271 };
5272 struct rpc_task_setup task_setup = {
5273 .rpc_client = clp->cl_rpcclient,
5274 .rpc_message = &msg,
5275 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005276 .callback_data = &data,
5277 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005278 };
5279 int status;
5280
Trond Myklebust9d12b212012-01-17 22:04:25 -05005281 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005282 dprintk("--> %s\n", __func__);
5283 task = rpc_run_task(&task_setup);
5284
5285 if (IS_ERR(task))
5286 status = PTR_ERR(task);
5287 else {
5288 status = task->tk_status;
5289 rpc_put_task(task);
5290 }
5291 dprintk("<-- %s return %d\n", __func__, status);
5292
5293 return status;
5294}
5295
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005296static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5297{
5298 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5299}
5300
5301static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5302 struct nfs4_slot *new,
5303 u32 max_slots,
5304 u32 ivalue)
5305{
5306 struct nfs4_slot *old = NULL;
5307 u32 i;
5308
5309 spin_lock(&tbl->slot_tbl_lock);
5310 if (new) {
5311 old = tbl->slots;
5312 tbl->slots = new;
5313 tbl->max_slots = max_slots;
5314 }
5315 tbl->highest_used_slotid = -1; /* no slot is currently used */
5316 for (i = 0; i < tbl->max_slots; i++)
5317 tbl->slots[i].seq_nr = ivalue;
5318 spin_unlock(&tbl->slot_tbl_lock);
5319 kfree(old);
5320}
5321
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005322/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005323 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005324 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005325static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5326 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005327{
Andy Adamson104aeba2010-01-14 17:45:10 -05005328 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005329 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005330
Andy Adamson104aeba2010-01-14 17:45:10 -05005331 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5332 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005333
Andy Adamson104aeba2010-01-14 17:45:10 -05005334 /* Does the newly negotiated max_reqs match the existing slot table? */
5335 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005336 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005337 if (!new)
5338 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005339 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005340 ret = 0;
5341
5342 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005343 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5344 tbl, tbl->slots, tbl->max_slots);
5345out:
5346 dprintk("<-- %s: return %d\n", __func__, ret);
5347 return ret;
5348}
5349
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005350/* Destroy the slot table */
5351static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5352{
5353 if (session->fc_slot_table.slots != NULL) {
5354 kfree(session->fc_slot_table.slots);
5355 session->fc_slot_table.slots = NULL;
5356 }
5357 if (session->bc_slot_table.slots != NULL) {
5358 kfree(session->bc_slot_table.slots);
5359 session->bc_slot_table.slots = NULL;
5360 }
5361 return;
5362}
5363
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005364/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005365 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005366 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005367static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005368{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005369 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005370 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005371
Andy Adamsonaacd5532011-11-09 13:58:21 -05005372 dprintk("--> %s\n", __func__);
5373 /* Fore channel */
5374 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005375 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5376 if (status) /* -ENOMEM */
5377 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005378 /* Back channel */
5379 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005380 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5381 if (status && tbl->slots == NULL)
5382 /* Fore and back channel share a connection so get
5383 * both slot tables or neither */
5384 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005385 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005386}
5387
5388struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5389{
5390 struct nfs4_session *session;
5391 struct nfs4_slot_table *tbl;
5392
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005393 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005394 if (!session)
5395 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005396
Andy Adamson557134a2009-04-01 09:21:53 -04005397 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005398 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005399 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005400 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005401 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005402
5403 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005404 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005405 spin_lock_init(&tbl->slot_tbl_lock);
5406 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005407 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005408
Trond Myklebust1055d762010-06-16 09:52:27 -04005409 session->session_state = 1<<NFS4_SESSION_INITING;
5410
Andy Adamson557134a2009-04-01 09:21:53 -04005411 session->clp = clp;
5412 return session;
5413}
5414
5415void nfs4_destroy_session(struct nfs4_session *session)
5416{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005417 struct rpc_xprt *xprt;
5418
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005419 nfs4_proc_destroy_session(session);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005420
5421 rcu_read_lock();
5422 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5423 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005424 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005425 __func__, xprt);
5426 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005427 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005428 kfree(session);
5429}
5430
Andy Adamsonfc931582009-04-01 09:22:31 -04005431/*
5432 * Initialize the values to be used by the client in CREATE_SESSION
5433 * If nfs4_init_session set the fore channel request and response sizes,
5434 * use them.
5435 *
5436 * Set the back channel max_resp_sz_cached to zero to force the client to
5437 * always set csa_cachethis to FALSE because the current implementation
5438 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5439 */
5440static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5441{
5442 struct nfs4_session *session = args->client->cl_session;
5443 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5444 mxresp_sz = session->fc_attrs.max_resp_sz;
5445
5446 if (mxrqst_sz == 0)
5447 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5448 if (mxresp_sz == 0)
5449 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5450 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005451 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5452 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005453 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005454 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005455
5456 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005457 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005458 __func__,
5459 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005460 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005461
5462 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005463 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5464 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5465 args->bc_attrs.max_resp_sz_cached = 0;
5466 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5467 args->bc_attrs.max_reqs = 1;
5468
5469 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5470 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5471 __func__,
5472 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5473 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5474 args->bc_attrs.max_reqs);
5475}
5476
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005477static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005478{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005479 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5480 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5481
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005482 if (rcvd->max_resp_sz > sent->max_resp_sz)
5483 return -EINVAL;
5484 /*
5485 * Our requested max_ops is the minimum we need; we're not
5486 * prepared to break up compounds into smaller pieces than that.
5487 * So, no point even trying to continue if the server won't
5488 * cooperate:
5489 */
5490 if (rcvd->max_ops < sent->max_ops)
5491 return -EINVAL;
5492 if (rcvd->max_reqs == 0)
5493 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005494 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5495 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005496 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005497}
5498
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005499static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5500{
5501 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5502 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005503
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005504 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5505 return -EINVAL;
5506 if (rcvd->max_resp_sz < sent->max_resp_sz)
5507 return -EINVAL;
5508 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5509 return -EINVAL;
5510 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005511 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005512 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005513 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005514 return -EINVAL;
5515 return 0;
5516}
Andy Adamson8d353012009-04-01 09:22:32 -04005517
Andy Adamson8d353012009-04-01 09:22:32 -04005518static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5519 struct nfs4_session *session)
5520{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005521 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005522
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005523 ret = nfs4_verify_fore_channel_attrs(args, session);
5524 if (ret)
5525 return ret;
5526 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005527}
5528
Andy Adamsonfc931582009-04-01 09:22:31 -04005529static int _nfs4_proc_create_session(struct nfs_client *clp)
5530{
5531 struct nfs4_session *session = clp->cl_session;
5532 struct nfs41_create_session_args args = {
5533 .client = clp,
5534 .cb_program = NFS4_CALLBACK,
5535 };
5536 struct nfs41_create_session_res res = {
5537 .client = clp,
5538 };
5539 struct rpc_message msg = {
5540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5541 .rpc_argp = &args,
5542 .rpc_resp = &res,
5543 };
5544 int status;
5545
5546 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005547 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005548
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005549 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005550
Andy Adamson8d353012009-04-01 09:22:32 -04005551 if (!status)
5552 /* Verify the session's negotiated channel_attrs values */
5553 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005554 if (!status) {
5555 /* Increment the clientid slot sequence id */
5556 clp->cl_seqid++;
5557 }
5558
5559 return status;
5560}
5561
5562/*
5563 * Issues a CREATE_SESSION operation to the server.
5564 * It is the responsibility of the caller to verify the session is
5565 * expired before calling this routine.
5566 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005567int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005568{
5569 int status;
5570 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005571 struct nfs4_session *session = clp->cl_session;
5572
5573 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5574
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005575 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005576 if (status)
5577 goto out;
5578
Andy Adamsonaacd5532011-11-09 13:58:21 -05005579 /* Init or reset the session slot tables */
5580 status = nfs4_setup_session_slot_tables(session);
5581 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005582 if (status)
5583 goto out;
5584
5585 ptr = (unsigned *)&session->sess_id.data[0];
5586 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5587 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005588out:
5589 dprintk("<-- %s\n", __func__);
5590 return status;
5591}
5592
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005593/*
5594 * Issue the over-the-wire RPC DESTROY_SESSION.
5595 * The caller must serialize access to this routine.
5596 */
5597int nfs4_proc_destroy_session(struct nfs4_session *session)
5598{
5599 int status = 0;
5600 struct rpc_message msg;
5601
5602 dprintk("--> nfs4_proc_destroy_session\n");
5603
5604 /* session is still being setup */
5605 if (session->clp->cl_cons_state != NFS_CS_READY)
5606 return status;
5607
5608 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5609 msg.rpc_argp = session;
5610 msg.rpc_resp = NULL;
5611 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005612 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005613
5614 if (status)
5615 printk(KERN_WARNING
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -05005616 "NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005617 "Session has been destroyed regardless...\n", status);
5618
5619 dprintk("<-- nfs4_proc_destroy_session\n");
5620 return status;
5621}
5622
Trond Myklebustfccba802009-07-21 16:48:07 -04005623int nfs4_init_session(struct nfs_server *server)
5624{
5625 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005626 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005627 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005628 int ret;
5629
5630 if (!nfs4_has_session(clp))
5631 return 0;
5632
Trond Myklebust1055d762010-06-16 09:52:27 -04005633 session = clp->cl_session;
5634 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5635 return 0;
5636
Andy Adamson68bf05e2009-12-15 12:55:02 -05005637 rsize = server->rsize;
5638 if (rsize == 0)
5639 rsize = NFS_MAX_FILE_IO_SIZE;
5640 wsize = server->wsize;
5641 if (wsize == 0)
5642 wsize = NFS_MAX_FILE_IO_SIZE;
5643
Andy Adamson68bf05e2009-12-15 12:55:02 -05005644 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5645 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005646
Trond Myklebustfccba802009-07-21 16:48:07 -04005647 ret = nfs4_recover_expired_lease(server);
5648 if (!ret)
5649 ret = nfs4_check_client_ready(clp);
5650 return ret;
5651}
5652
Andy Adamsond83217c2011-03-01 01:34:17 +00005653int nfs4_init_ds_session(struct nfs_client *clp)
5654{
5655 struct nfs4_session *session = clp->cl_session;
5656 int ret;
5657
5658 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5659 return 0;
5660
5661 ret = nfs4_client_recover_expired_lease(clp);
5662 if (!ret)
5663 /* Test for the DS role */
5664 if (!is_ds_client(clp))
5665 ret = -ENODEV;
5666 if (!ret)
5667 ret = nfs4_check_client_ready(clp);
5668 return ret;
5669
5670}
5671EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5672
5673
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005674/*
5675 * Renew the cl_session lease.
5676 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005677struct nfs4_sequence_data {
5678 struct nfs_client *clp;
5679 struct nfs4_sequence_args args;
5680 struct nfs4_sequence_res res;
5681};
5682
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005683static void nfs41_sequence_release(void *data)
5684{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005685 struct nfs4_sequence_data *calldata = data;
5686 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005687
Alexandros Batsakis71358402010-02-05 03:45:05 -08005688 if (atomic_read(&clp->cl_count) > 1)
5689 nfs4_schedule_state_renewal(clp);
5690 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005691 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005692}
5693
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005694static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5695{
5696 switch(task->tk_status) {
5697 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005698 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5699 return -EAGAIN;
5700 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005701 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005702 }
5703 return 0;
5704}
5705
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005706static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005707{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005708 struct nfs4_sequence_data *calldata = data;
5709 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005710
Trond Myklebust14516c32010-07-31 14:29:06 -04005711 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5712 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005713
5714 if (task->tk_status < 0) {
5715 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005716 if (atomic_read(&clp->cl_count) == 1)
5717 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005718
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005719 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5720 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005721 return;
5722 }
5723 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005724 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005725out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005726 dprintk("<-- %s\n", __func__);
5727}
5728
5729static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5730{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005731 struct nfs4_sequence_data *calldata = data;
5732 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005733 struct nfs4_sequence_args *args;
5734 struct nfs4_sequence_res *res;
5735
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005736 args = task->tk_msg.rpc_argp;
5737 res = task->tk_msg.rpc_resp;
5738
Trond Myklebust9d12b212012-01-17 22:04:25 -05005739 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005740 return;
5741 rpc_call_start(task);
5742}
5743
Bryan Schumakerd1967aa2012-11-12 16:55:38 -05005744static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
5745{
5746 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5747 nfs41_sequence_prepare(task, data);
5748}
5749
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005750static const struct rpc_call_ops nfs41_sequence_ops = {
5751 .rpc_call_done = nfs41_sequence_call_done,
5752 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005753 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005754};
5755
Bryan Schumakerd1967aa2012-11-12 16:55:38 -05005756static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
5757 .rpc_call_done = nfs41_sequence_call_done,
5758 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
5759 .rpc_release = nfs41_sequence_release,
5760};
5761
5762static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
5763 const struct rpc_call_ops *seq_ops)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005764{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005765 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005766 struct rpc_message msg = {
5767 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5768 .rpc_cred = cred,
5769 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005770 struct rpc_task_setup task_setup_data = {
5771 .rpc_client = clp->cl_rpcclient,
5772 .rpc_message = &msg,
Bryan Schumakerd1967aa2012-11-12 16:55:38 -05005773 .callback_ops = seq_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005774 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5775 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005776
Alexandros Batsakis71358402010-02-05 03:45:05 -08005777 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005778 return ERR_PTR(-EIO);
Benny Halevydfb4f302010-09-24 09:17:01 -04005779 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005780 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005781 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005782 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005783 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005784 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005785 msg.rpc_argp = &calldata->args;
5786 msg.rpc_resp = &calldata->res;
5787 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005788 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005789
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005790 return rpc_run_task(&task_setup_data);
5791}
5792
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005793static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005794{
5795 struct rpc_task *task;
5796 int ret = 0;
5797
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005798 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5799 return 0;
Bryan Schumakerd1967aa2012-11-12 16:55:38 -05005800 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005801 if (IS_ERR(task))
5802 ret = PTR_ERR(task);
5803 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005804 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005805 dprintk("<-- %s status=%d\n", __func__, ret);
5806 return ret;
5807}
5808
5809static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5810{
5811 struct rpc_task *task;
5812 int ret;
5813
Bryan Schumakerd1967aa2012-11-12 16:55:38 -05005814 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005815 if (IS_ERR(task)) {
5816 ret = PTR_ERR(task);
5817 goto out;
5818 }
5819 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005820 if (!ret) {
5821 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5822
5823 if (task->tk_status == 0)
5824 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005825 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005826 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005827 rpc_put_task(task);
5828out:
5829 dprintk("<-- %s status=%d\n", __func__, ret);
5830 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005831}
5832
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005833struct nfs4_reclaim_complete_data {
5834 struct nfs_client *clp;
5835 struct nfs41_reclaim_complete_args arg;
5836 struct nfs41_reclaim_complete_res res;
5837};
5838
5839static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5840{
5841 struct nfs4_reclaim_complete_data *calldata = data;
5842
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005843 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005844 if (nfs41_setup_sequence(calldata->clp->cl_session,
5845 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005846 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005847 return;
5848
5849 rpc_call_start(task);
5850}
5851
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005852static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5853{
5854 switch(task->tk_status) {
5855 case 0:
5856 case -NFS4ERR_COMPLETE_ALREADY:
5857 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5858 break;
5859 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005860 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005861 /* fall through */
5862 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005863 return -EAGAIN;
5864 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005865 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005866 }
5867 return 0;
5868}
5869
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005870static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5871{
5872 struct nfs4_reclaim_complete_data *calldata = data;
5873 struct nfs_client *clp = calldata->clp;
5874 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5875
5876 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005877 if (!nfs41_sequence_done(task, res))
5878 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005879
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005880 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5881 rpc_restart_call_prepare(task);
5882 return;
5883 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005884 dprintk("<-- %s\n", __func__);
5885}
5886
5887static void nfs4_free_reclaim_complete_data(void *data)
5888{
5889 struct nfs4_reclaim_complete_data *calldata = data;
5890
5891 kfree(calldata);
5892}
5893
5894static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5895 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5896 .rpc_call_done = nfs4_reclaim_complete_done,
5897 .rpc_release = nfs4_free_reclaim_complete_data,
5898};
5899
5900/*
5901 * Issue a global reclaim complete.
5902 */
5903static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5904{
5905 struct nfs4_reclaim_complete_data *calldata;
5906 struct rpc_task *task;
5907 struct rpc_message msg = {
5908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5909 };
5910 struct rpc_task_setup task_setup_data = {
5911 .rpc_client = clp->cl_rpcclient,
5912 .rpc_message = &msg,
5913 .callback_ops = &nfs4_reclaim_complete_call_ops,
5914 .flags = RPC_TASK_ASYNC,
5915 };
5916 int status = -ENOMEM;
5917
5918 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005919 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005920 if (calldata == NULL)
5921 goto out;
5922 calldata->clp = clp;
5923 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005924
Trond Myklebust9d12b212012-01-17 22:04:25 -05005925 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005926 msg.rpc_argp = &calldata->arg;
5927 msg.rpc_resp = &calldata->res;
5928 task_setup_data.callback_data = calldata;
5929 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005930 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005931 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005932 goto out;
5933 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005934 status = nfs4_wait_for_completion_rpc_task(task);
5935 if (status == 0)
5936 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005937 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005938 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005939out:
5940 dprintk("<-- %s status=%d\n", __func__, status);
5941 return status;
5942}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005943
5944static void
5945nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5946{
5947 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005948 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005949
5950 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005951 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5952 * right now covering the LAYOUTGET we are about to send.
5953 * However, that is not so catastrophic, and there seems
5954 * to be no way to prevent it completely.
5955 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005956 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005957 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005958 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005959 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5960 NFS_I(lgp->args.inode)->layout,
5961 lgp->args.ctx->state)) {
5962 rpc_exit(task, NFS4_OK);
5963 return;
5964 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005965 rpc_call_start(task);
5966}
5967
5968static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5969{
5970 struct nfs4_layoutget *lgp = calldata;
5971 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5972
5973 dprintk("--> %s\n", __func__);
5974
5975 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5976 return;
5977
5978 switch (task->tk_status) {
5979 case 0:
5980 break;
5981 case -NFS4ERR_LAYOUTTRYLATER:
5982 case -NFS4ERR_RECALLCONFLICT:
5983 task->tk_status = -NFS4ERR_DELAY;
5984 /* Fall through */
5985 default:
5986 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5987 rpc_restart_call_prepare(task);
5988 return;
5989 }
5990 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005991 dprintk("<-- %s\n", __func__);
5992}
5993
Idan Kedarc2bf3222012-08-02 11:47:10 +03005994static size_t max_response_pages(struct nfs_server *server)
5995{
5996 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
5997 return nfs_page_array_len(0, max_resp_sz);
5998}
5999
6000static void nfs4_free_pages(struct page **pages, size_t size)
6001{
6002 int i;
6003
6004 if (!pages)
6005 return;
6006
6007 for (i = 0; i < size; i++) {
6008 if (!pages[i])
6009 break;
6010 __free_page(pages[i]);
6011 }
6012 kfree(pages);
6013}
6014
6015static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6016{
6017 struct page **pages;
6018 int i;
6019
6020 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6021 if (!pages) {
6022 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6023 return NULL;
6024 }
6025
6026 for (i = 0; i < size; i++) {
6027 pages[i] = alloc_page(gfp_flags);
6028 if (!pages[i]) {
6029 dprintk("%s: failed to allocate page\n", __func__);
6030 nfs4_free_pages(pages, size);
6031 return NULL;
6032 }
6033 }
6034
6035 return pages;
6036}
6037
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006038static void nfs4_layoutget_release(void *calldata)
6039{
6040 struct nfs4_layoutget *lgp = calldata;
Idan Kedarc2bf3222012-08-02 11:47:10 +03006041 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6042 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006043
6044 dprintk("--> %s\n", __func__);
Idan Kedarc2bf3222012-08-02 11:47:10 +03006045 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006046 put_nfs_open_context(lgp->args.ctx);
6047 kfree(calldata);
6048 dprintk("<-- %s\n", __func__);
6049}
6050
6051static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6052 .rpc_call_prepare = nfs4_layoutget_prepare,
6053 .rpc_call_done = nfs4_layoutget_done,
6054 .rpc_release = nfs4_layoutget_release,
6055};
6056
Idan Kedarc2bf3222012-08-02 11:47:10 +03006057int nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006058{
6059 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedarc2bf3222012-08-02 11:47:10 +03006060 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006061 struct rpc_task *task;
6062 struct rpc_message msg = {
6063 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6064 .rpc_argp = &lgp->args,
6065 .rpc_resp = &lgp->res,
6066 };
6067 struct rpc_task_setup task_setup_data = {
6068 .rpc_client = server->client,
6069 .rpc_message = &msg,
6070 .callback_ops = &nfs4_layoutget_call_ops,
6071 .callback_data = lgp,
6072 .flags = RPC_TASK_ASYNC,
6073 };
6074 int status = 0;
6075
6076 dprintk("--> %s\n", __func__);
6077
Idan Kedarc2bf3222012-08-02 11:47:10 +03006078 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6079 if (!lgp->args.layout.pages) {
6080 nfs4_layoutget_release(lgp);
6081 return -ENOMEM;
6082 }
6083 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6084
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006085 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006086 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006087 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006088 task = rpc_run_task(&task_setup_data);
6089 if (IS_ERR(task))
6090 return PTR_ERR(task);
6091 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006092 if (status == 0)
6093 status = task->tk_status;
Weston Andros Adamson0c5fa162013-02-15 16:03:46 -05006094 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6095 if (status == 0 && lgp->res.layoutp->len)
Fred Isamanc31663d2011-01-06 11:36:24 +00006096 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006097 rpc_put_task(task);
6098 dprintk("<-- %s status=%d\n", __func__, status);
6099 return status;
6100}
6101
Benny Halevycbe82602011-05-22 19:52:37 +03006102static void
6103nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6104{
6105 struct nfs4_layoutreturn *lrp = calldata;
6106
6107 dprintk("--> %s\n", __func__);
6108 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006109 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006110 return;
6111 rpc_call_start(task);
6112}
6113
6114static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6115{
6116 struct nfs4_layoutreturn *lrp = calldata;
6117 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006118 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006119
6120 dprintk("--> %s\n", __func__);
6121
6122 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6123 return;
6124
6125 server = NFS_SERVER(lrp->args.inode);
6126 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006127 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006128 return;
6129 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006130 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebustf401b102012-08-08 16:03:13 -04006131 if (task->tk_status == 0 && lrp->res.lrs_present)
6132 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006133 lo->plh_block_lgets--;
6134 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006135 dprintk("<-- %s\n", __func__);
6136}
6137
6138static void nfs4_layoutreturn_release(void *calldata)
6139{
6140 struct nfs4_layoutreturn *lrp = calldata;
6141
6142 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006143 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006144 kfree(calldata);
6145 dprintk("<-- %s\n", __func__);
6146}
6147
6148static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6149 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6150 .rpc_call_done = nfs4_layoutreturn_done,
6151 .rpc_release = nfs4_layoutreturn_release,
6152};
6153
6154int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6155{
6156 struct rpc_task *task;
6157 struct rpc_message msg = {
6158 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6159 .rpc_argp = &lrp->args,
6160 .rpc_resp = &lrp->res,
6161 };
6162 struct rpc_task_setup task_setup_data = {
6163 .rpc_client = lrp->clp->cl_rpcclient,
6164 .rpc_message = &msg,
6165 .callback_ops = &nfs4_layoutreturn_call_ops,
6166 .callback_data = lrp,
6167 };
6168 int status;
6169
6170 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006171 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006172 task = rpc_run_task(&task_setup_data);
6173 if (IS_ERR(task))
6174 return PTR_ERR(task);
6175 status = task->tk_status;
6176 dprintk("<-- %s status=%d\n", __func__, status);
6177 rpc_put_task(task);
6178 return status;
6179}
6180
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006181/*
6182 * Retrieve the list of Data Server devices from the MDS.
6183 */
6184static int _nfs4_getdevicelist(struct nfs_server *server,
6185 const struct nfs_fh *fh,
6186 struct pnfs_devicelist *devlist)
6187{
6188 struct nfs4_getdevicelist_args args = {
6189 .fh = fh,
6190 .layoutclass = server->pnfs_curr_ld->id,
6191 };
6192 struct nfs4_getdevicelist_res res = {
6193 .devlist = devlist,
6194 };
6195 struct rpc_message msg = {
6196 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6197 .rpc_argp = &args,
6198 .rpc_resp = &res,
6199 };
6200 int status;
6201
6202 dprintk("--> %s\n", __func__);
6203 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6204 &res.seq_res, 0);
6205 dprintk("<-- %s status=%d\n", __func__, status);
6206 return status;
6207}
6208
6209int nfs4_proc_getdevicelist(struct nfs_server *server,
6210 const struct nfs_fh *fh,
6211 struct pnfs_devicelist *devlist)
6212{
6213 struct nfs4_exception exception = { };
6214 int err;
6215
6216 do {
6217 err = nfs4_handle_exception(server,
6218 _nfs4_getdevicelist(server, fh, devlist),
6219 &exception);
6220 } while (exception.retry);
6221
6222 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6223 err, devlist->num_devs);
6224
6225 return err;
6226}
6227EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6228
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006229static int
6230_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6231{
6232 struct nfs4_getdeviceinfo_args args = {
6233 .pdev = pdev,
6234 };
6235 struct nfs4_getdeviceinfo_res res = {
6236 .pdev = pdev,
6237 };
6238 struct rpc_message msg = {
6239 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6240 .rpc_argp = &args,
6241 .rpc_resp = &res,
6242 };
6243 int status;
6244
6245 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006246 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006247 dprintk("<-- %s status=%d\n", __func__, status);
6248
6249 return status;
6250}
6251
6252int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6253{
6254 struct nfs4_exception exception = { };
6255 int err;
6256
6257 do {
6258 err = nfs4_handle_exception(server,
6259 _nfs4_proc_getdeviceinfo(server, pdev),
6260 &exception);
6261 } while (exception.retry);
6262 return err;
6263}
6264EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6265
Andy Adamson863a3c62011-03-23 13:27:54 +00006266static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6267{
6268 struct nfs4_layoutcommit_data *data = calldata;
6269 struct nfs_server *server = NFS_SERVER(data->args.inode);
6270
6271 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006272 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006273 return;
6274 rpc_call_start(task);
6275}
6276
6277static void
6278nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6279{
6280 struct nfs4_layoutcommit_data *data = calldata;
6281 struct nfs_server *server = NFS_SERVER(data->args.inode);
6282
6283 if (!nfs4_sequence_done(task, &data->res.seq_res))
6284 return;
6285
6286 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006287 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6288 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6289 case -NFS4ERR_BADLAYOUT: /* no layout */
6290 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006291 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006292 break;
6293 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006294 nfs_post_op_update_inode_force_wcc(data->args.inode,
6295 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006296 break;
6297 default:
6298 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6299 rpc_restart_call_prepare(task);
6300 return;
6301 }
6302 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006303}
6304
6305static void nfs4_layoutcommit_release(void *calldata)
6306{
6307 struct nfs4_layoutcommit_data *data = calldata;
6308
Andy Adamsondb29c082011-07-30 20:52:38 -04006309 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006310 put_rpccred(data->cred);
6311 kfree(data);
6312}
6313
6314static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6315 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6316 .rpc_call_done = nfs4_layoutcommit_done,
6317 .rpc_release = nfs4_layoutcommit_release,
6318};
6319
6320int
Andy Adamsonef311532011-03-12 02:58:10 -05006321nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006322{
6323 struct rpc_message msg = {
6324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6325 .rpc_argp = &data->args,
6326 .rpc_resp = &data->res,
6327 .rpc_cred = data->cred,
6328 };
6329 struct rpc_task_setup task_setup_data = {
6330 .task = &data->task,
6331 .rpc_client = NFS_CLIENT(data->args.inode),
6332 .rpc_message = &msg,
6333 .callback_ops = &nfs4_layoutcommit_ops,
6334 .callback_data = data,
6335 .flags = RPC_TASK_ASYNC,
6336 };
6337 struct rpc_task *task;
6338 int status = 0;
6339
6340 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6341 "lbw: %llu inode %lu\n",
6342 data->task.tk_pid, sync,
6343 data->args.lastbytewritten,
6344 data->args.inode->i_ino);
6345
Trond Myklebust9d12b212012-01-17 22:04:25 -05006346 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006347 task = rpc_run_task(&task_setup_data);
6348 if (IS_ERR(task))
6349 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006350 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006351 goto out;
6352 status = nfs4_wait_for_completion_rpc_task(task);
6353 if (status != 0)
6354 goto out;
6355 status = task->tk_status;
6356out:
6357 dprintk("%s: status %d\n", __func__, status);
6358 rpc_put_task(task);
6359 return status;
6360}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006361
6362static int
6363_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6364 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6365{
6366 struct nfs41_secinfo_no_name_args args = {
6367 .style = SECINFO_STYLE_CURRENT_FH,
6368 };
6369 struct nfs4_secinfo_res res = {
6370 .flavors = flavors,
6371 };
6372 struct rpc_message msg = {
6373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6374 .rpc_argp = &args,
6375 .rpc_resp = &res,
6376 };
6377 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6378}
6379
6380static int
6381nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6382 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6383{
6384 struct nfs4_exception exception = { };
6385 int err;
6386 do {
6387 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6388 switch (err) {
6389 case 0:
6390 case -NFS4ERR_WRONGSEC:
Weston Andros Adamsonb5f06082014-01-13 16:54:45 -05006391 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006392 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006393 default:
6394 err = nfs4_handle_exception(server, err, &exception);
6395 }
6396 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006397out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006398 return err;
6399}
6400
6401static int
6402nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6403 struct nfs_fsinfo *info)
6404{
6405 int err;
6406 struct page *page;
6407 rpc_authflavor_t flavor;
6408 struct nfs4_secinfo_flavors *flavors;
6409
6410 page = alloc_page(GFP_KERNEL);
6411 if (!page) {
6412 err = -ENOMEM;
6413 goto out;
6414 }
6415
6416 flavors = page_address(page);
6417 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6418
6419 /*
6420 * Fall back on "guess and check" method if
6421 * the server doesn't support SECINFO_NO_NAME
6422 */
Weston Andros Adamsonb5f06082014-01-13 16:54:45 -05006423 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006424 err = nfs4_find_root_sec(server, fhandle, info);
6425 goto out_freepage;
6426 }
6427 if (err)
6428 goto out_freepage;
6429
6430 flavor = nfs_find_best_sec(flavors);
6431 if (err == 0)
6432 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6433
6434out_freepage:
6435 put_page(page);
6436 if (err == -EACCES)
6437 return -EPERM;
6438out:
6439 return err;
6440}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006441
6442static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006443{
6444 int status;
6445 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006446 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006447 };
6448 struct nfs41_test_stateid_res res;
6449 struct rpc_message msg = {
6450 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6451 .rpc_argp = &args,
6452 .rpc_resp = &res,
6453 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006454
Trond Myklebust9d12b212012-01-17 22:04:25 -05006455 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006456 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6457
6458 if (status == NFS_OK)
6459 return res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006460 return status;
6461}
6462
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006463static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006464{
6465 struct nfs4_exception exception = { };
6466 int err;
6467 do {
6468 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006469 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006470 &exception);
6471 } while (exception.retry);
6472 return err;
6473}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006474
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006475static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006476{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006477 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006478 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006479 };
6480 struct nfs41_free_stateid_res res;
6481 struct rpc_message msg = {
6482 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6483 .rpc_argp = &args,
6484 .rpc_resp = &res,
6485 };
6486
Trond Myklebust9d12b212012-01-17 22:04:25 -05006487 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6488 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006489}
6490
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006491static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006492{
6493 struct nfs4_exception exception = { };
6494 int err;
6495 do {
6496 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006497 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006498 &exception);
6499 } while (exception.retry);
6500 return err;
6501}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006502
6503static bool nfs41_match_stateid(const nfs4_stateid *s1,
6504 const nfs4_stateid *s2)
6505{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006506 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006507 return false;
6508
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006509 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006510 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006511 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006512 return true;
6513
6514 return false;
6515}
6516
Andy Adamson557134a2009-04-01 09:21:53 -04006517#endif /* CONFIG_NFS_V4_1 */
6518
Trond Myklebust36281ca2012-03-04 18:13:56 -05006519static bool nfs4_match_stateid(const nfs4_stateid *s1,
6520 const nfs4_stateid *s2)
6521{
Trond Myklebustf597c532012-03-04 18:13:56 -05006522 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006523}
6524
6525
Trond Myklebust17280172012-03-11 13:11:00 -04006526static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006527 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006528 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529 .recover_open = nfs4_open_reclaim,
6530 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006531 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006532 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533};
6534
Andy Adamson591d71c2009-04-01 09:22:47 -04006535#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006536static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006537 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6538 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6539 .recover_open = nfs4_open_reclaim,
6540 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006541 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006542 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006543 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006544};
6545#endif /* CONFIG_NFS_V4_1 */
6546
Trond Myklebust17280172012-03-11 13:11:00 -04006547static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006548 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006549 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550 .recover_open = nfs4_open_expired,
6551 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006552 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006553 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554};
6555
Andy Adamson591d71c2009-04-01 09:22:47 -04006556#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006557static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006558 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6559 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006560 .recover_open = nfs41_open_expired,
6561 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006562 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006563 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006564};
6565#endif /* CONFIG_NFS_V4_1 */
6566
Trond Myklebust17280172012-03-11 13:11:00 -04006567static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006568 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006569 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006570 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006571};
6572
6573#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006574static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006575 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006576 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006577 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006578};
6579#endif
6580
Trond Myklebust97dc1352010-06-16 09:52:26 -04006581static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6582 .minor_version = 0,
6583 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006584 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006585 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006586 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6587 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6588 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006589};
6590
6591#if defined(CONFIG_NFS_V4_1)
6592static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6593 .minor_version = 1,
6594 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006595 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006596 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006597 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6598 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6599 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006600};
6601#endif
6602
Trond Myklebust97dc1352010-06-16 09:52:26 -04006603const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6604 [0] = &nfs_v4_0_minor_ops,
6605#if defined(CONFIG_NFS_V4_1)
6606 [1] = &nfs_v4_1_minor_ops,
6607#endif
6608};
6609
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006610static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006611 .permission = nfs_permission,
6612 .getattr = nfs_getattr,
6613 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006614 .getxattr = generic_getxattr,
6615 .setxattr = generic_setxattr,
6616 .listxattr = generic_listxattr,
6617 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006618};
6619
David Howells509de812006-08-22 20:06:11 -04006620const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621 .version = 4, /* protocol version */
6622 .dentry_ops = &nfs4_dentry_operations,
6623 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006624 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006625 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 .getroot = nfs4_proc_get_root,
6627 .getattr = nfs4_proc_getattr,
6628 .setattr = nfs4_proc_setattr,
6629 .lookup = nfs4_proc_lookup,
6630 .access = nfs4_proc_access,
6631 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632 .create = nfs4_proc_create,
6633 .remove = nfs4_proc_remove,
6634 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006635 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 .unlink_done = nfs4_proc_unlink_done,
6637 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006638 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006639 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006640 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641 .link = nfs4_proc_link,
6642 .symlink = nfs4_proc_symlink,
6643 .mkdir = nfs4_proc_mkdir,
6644 .rmdir = nfs4_proc_remove,
6645 .readdir = nfs4_proc_readdir,
6646 .mknod = nfs4_proc_mknod,
6647 .statfs = nfs4_proc_statfs,
6648 .fsinfo = nfs4_proc_fsinfo,
6649 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006650 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 .decode_dirent = nfs4_decode_dirent,
6652 .read_setup = nfs4_proc_read_setup,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006653 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006654 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655 .write_setup = nfs4_proc_write_setup,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006656 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006657 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006659 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006661 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006662 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006663 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006664 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006665 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666};
6667
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006668static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6669 .prefix = XATTR_NAME_NFSV4_ACL,
6670 .list = nfs4_xattr_list_nfs4_acl,
6671 .get = nfs4_xattr_get_nfs4_acl,
6672 .set = nfs4_xattr_set_nfs4_acl,
6673};
6674
6675const struct xattr_handler *nfs4_xattr_handlers[] = {
6676 &nfs4_xattr_nfs4_acl_handler,
6677 NULL
6678};
6679
Trond Myklebustef159e92012-02-06 19:50:40 -05006680module_param(max_session_slots, ushort, 0644);
6681MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6682 "requests the client will negotiate");
6683
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684/*
6685 * Local variables:
6686 * c-basic-offset: 8
6687 * End:
6688 */