blob: 237f3dccbfa38fa22671421693d7b37ca41375db [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 Myklebust5d8dd812012-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 Cross6072cdb2013-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 Schumaker90f85c32012-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 Myklebust5ca6b682013-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 Myklebustda549162013-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 Myklebustb8480fc2012-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
Trond Myklebusta6831dc2014-10-17 15:10:25 +03001743static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
1744{
1745 nfs_remove_bad_delegation(state->inode);
1746 write_seqlock(&state->seqlock);
1747 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1748 write_sequnlock(&state->seqlock);
1749 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1750}
1751
1752static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
1753{
1754 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
1755 nfs_finish_clear_delegation_stateid(state);
1756}
1757
1758static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1759{
1760 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
1761 nfs40_clear_delegation_stateid(state);
1762 return nfs4_open_expired(sp, state);
1763}
1764
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001765#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001766static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001767{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001768 int status = NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001769 struct nfs_server *server = NFS_SERVER(state->inode);
1770
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001771 if (state->flags & flags) {
1772 status = nfs41_test_stateid(server, stateid);
1773 if (status != NFS_OK) {
1774 nfs41_free_stateid(server, stateid);
1775 state->flags &= ~flags;
1776 }
1777 }
1778 return status;
1779}
1780
1781static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1782{
1783 int deleg_status, open_status;
1784 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1785 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1786
1787 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1788 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1789
1790 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1791 return NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001792 return nfs4_open_expired(sp, state);
1793}
1794#endif
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001797 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1798 * fields corresponding to attributes that were used to store the verifier.
1799 * Make sure we clobber those fields in the later setattr call
1800 */
1801static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1802{
1803 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1804 !(sattr->ia_valid & ATTR_ATIME_SET))
1805 sattr->ia_valid |= ATTR_ATIME;
1806
1807 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1808 !(sattr->ia_valid & ATTR_MTIME_SET))
1809 sattr->ia_valid |= ATTR_MTIME;
1810}
1811
1812/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001813 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001815static 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 -07001816{
1817 struct nfs4_state_owner *sp;
1818 struct nfs4_state *state = NULL;
1819 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001820 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001821 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
1823 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001825 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1826 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1828 goto out_err;
1829 }
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001830 status = nfs4_recover_expired_lease(server);
1831 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001832 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001833 if (dentry->d_inode != NULL)
1834 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001835 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001836 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001837 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001838 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Al Viro82a2c1b2011-06-22 18:30:55 -04001840 if (dentry->d_inode != NULL)
1841 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001842
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001843 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001845 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001847 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001848 status = PTR_ERR(state);
1849 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001850 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001851 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001852 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001853
NeilBrown783a0bf2015-07-30 13:00:56 +10001854 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001855 nfs4_exclusive_attrset(opendata, sattr);
1856
1857 nfs_fattr_init(opendata->o_res.f_attr);
1858 status = nfs4_do_setattr(state->inode, cred,
1859 opendata->o_res.f_attr, sattr,
1860 state);
1861 if (status == 0)
1862 nfs_setattr_update_inode(state->inode, sattr);
1863 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1864 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001865 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 *res = state;
1868 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001869err_opendata_put:
1870 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001871err_put_state_owner:
1872 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 *res = NULL;
1875 return status;
1876}
1877
1878
Al Viro82a2c1b2011-06-22 18:30:55 -04001879static 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 -07001880{
1881 struct nfs4_exception exception = { };
1882 struct nfs4_state *res;
1883 int status;
1884
Trond Myklebust3820a6c2012-06-08 10:58:09 -04001885 fmode &= FMODE_READ|FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001887 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (status == 0)
1889 break;
1890 /* NOTE: BAD_SEQID means the server and client disagree about the
1891 * book-keeping w.r.t. state-changing operations
1892 * (OPEN/CLOSE/LOCK/LOCKU...)
1893 * It is actually a sign of a bug on the client or on the server.
1894 *
1895 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001896 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 * have unhashed the old state_owner for us, and that we can
1898 * therefore safely retry using a new one. We should still warn
1899 * the user though...
1900 */
1901 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001902 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001903 " returned a bad sequence-id error!\n",
1904 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 exception.retry = 1;
1906 continue;
1907 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001908 /*
1909 * BAD_STATEID on OPEN means that the server cancelled our
1910 * state before it received the OPEN_CONFIRM.
1911 * Recover by retrying the request as per the discussion
1912 * on Page 181 of RFC3530.
1913 */
1914 if (status == -NFS4ERR_BAD_STATEID) {
1915 exception.retry = 1;
1916 continue;
1917 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001918 if (status == -EAGAIN) {
1919 /* We must have found a delegation */
1920 exception.retry = 1;
1921 continue;
1922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1924 status, &exception));
1925 } while (exception.retry);
1926 return res;
1927}
1928
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001929static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1930 struct nfs_fattr *fattr, struct iattr *sattr,
1931 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001933 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001935 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 .iap = sattr,
1937 .server = server,
1938 .bitmask = server->attr_bitmask,
1939 };
1940 struct nfs_setattrres res = {
1941 .fattr = fattr,
1942 .server = server,
1943 };
1944 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001945 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1946 .rpc_argp = &arg,
1947 .rpc_resp = &res,
1948 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001950 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001951 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Trond Myklebust0e574af2005-10-27 22:12:38 -04001953 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Trond Myklebust4fc87962012-03-08 17:42:01 -05001955 if (state != NULL) {
1956 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1957 current->files, current->tgid);
1958 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1959 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001960 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001961 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05001962 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Bryan Schumaker7c513052011-03-24 17:12:24 +00001964 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001965 if (status == 0 && state != NULL)
1966 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001967 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968}
1969
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001970static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1971 struct nfs_fattr *fattr, struct iattr *sattr,
1972 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001974 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001975 struct nfs4_exception exception = {
1976 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05001977 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001978 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 int err;
1980 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04001981 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
1982 switch (err) {
1983 case -NFS4ERR_OPENMODE:
1984 if (state && !(state->state & FMODE_WRITE)) {
1985 err = -EBADF;
1986 if (sattr->ia_valid & ATTR_OPEN)
1987 err = -EACCES;
1988 goto out;
1989 }
1990 }
1991 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04001993out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 return err;
1995}
1996
1997struct nfs4_closedata {
1998 struct inode *inode;
1999 struct nfs4_state *state;
2000 struct nfs_closeargs arg;
2001 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002002 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002003 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002004 bool roc;
2005 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006};
2007
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002008static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002009{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002010 struct nfs4_closedata *calldata = data;
2011 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002012 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002013
Fred Isamanf7e89172011-01-06 11:36:32 +00002014 if (calldata->roc)
2015 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002016 nfs4_put_open_state(calldata->state);
2017 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002018 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002019 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002020 kfree(calldata);
2021}
2022
Trond Myklebust88069f72009-12-08 08:33:16 -05002023static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2024 fmode_t fmode)
2025{
2026 spin_lock(&state->owner->so_lock);
2027 if (!(fmode & FMODE_READ))
2028 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2029 if (!(fmode & FMODE_WRITE))
2030 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2031 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2032 spin_unlock(&state->owner->so_lock);
2033}
2034
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002035static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002037 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 struct nfs_server *server = NFS_SERVER(calldata->inode);
2040
Chuck Levera3ca5652012-03-01 17:00:40 -05002041 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002042 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2043 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 /* hmm. we are done with the inode, and in the process of freeing
2045 * the state_owner. we keep this around to process errors
2046 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 switch (task->tk_status) {
2048 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002049 if (calldata->roc)
2050 pnfs_roc_set_barrier(state->inode,
2051 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002052 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002053 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002054 nfs4_close_clear_stateid_flags(state,
2055 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 break;
2057 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002058 case -NFS4ERR_OLD_STATEID:
2059 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002061 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002062 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002064 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2065 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002067 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002068 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002069 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
2071
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002072static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002074 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002075 struct nfs4_state *state = calldata->state;
Trond Myklebust8d31c5d2014-08-25 22:33:12 -04002076 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002077 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002078
Chuck Levera3ca5652012-03-01 17:00:40 -05002079 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002080 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002081 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002082
Trond Myklebust88069f72009-12-08 08:33:16 -05002083 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002084 spin_lock(&state->owner->so_lock);
Trond Myklebust8d31c5d2014-08-25 22:33:12 -04002085 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2086 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2087 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04002088 /* Calculate the change in open mode */
Trond Myklebust1f9ad5d2014-09-18 11:51:32 -04002089 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002090 if (state->n_rdwr == 0) {
Trond Myklebust1f9ad5d2014-09-18 11:51:32 -04002091 if (state->n_rdonly == 0)
2092 call_close |= is_rdonly;
2093 else if (is_rdonly)
2094 calldata->arg.fmode |= FMODE_READ;
2095 if (state->n_wronly == 0)
2096 call_close |= is_wronly;
2097 else if (is_wronly)
2098 calldata->arg.fmode |= FMODE_WRITE;
2099 } else if (is_rdwr)
2100 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2101
2102 if (calldata->arg.fmode == 0)
2103 call_close |= is_rdwr;
2104
Trond Myklebust4cecb762005-11-04 15:32:58 -05002105 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002106
2107 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002108 /* Note: exit _without_ calling nfs4_close_done */
2109 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002110 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002111 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002112
Fred Isamanf7e89172011-01-06 11:36:32 +00002113 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002114 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002115 if (calldata->roc &&
2116 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2117 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2118 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002119 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002120 }
2121 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002122
Trond Myklebust516a6af2005-10-27 22:12:41 -04002123 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002124 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002125 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002126 &calldata->arg.seq_args,
2127 &calldata->res.seq_res,
Trond Myklebustb8480fc2012-10-29 18:37:40 -04002128 task) != 0)
2129 nfs_release_seqid(calldata->arg.seqid);
2130 else
2131 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002132out:
2133 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002136static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002137 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002138 .rpc_call_done = nfs4_close_done,
2139 .rpc_release = nfs4_free_closedata,
2140};
2141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142/*
2143 * It is possible for data to be read/written from a mem-mapped file
2144 * after the sys_close call (which hits the vfs layer as a flush).
2145 * This means that we can't safely call nfsv4 close on a file until
2146 * the inode is cleared. This in turn means that we are not good
2147 * NFSv4 citizens - we do not indicate to the server to update the file's
2148 * share state even when we are done with one of the three share
2149 * stateid's in the inode.
2150 *
2151 * NOTE: Caller must be holding the sp->so_owner semaphore!
2152 */
Al Viro643168c2011-06-22 18:20:23 -04002153int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002155 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002157 struct nfs4_state_owner *sp = state->owner;
2158 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002159 struct rpc_message msg = {
2160 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2161 .rpc_cred = state->owner->so_cred,
2162 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002163 struct rpc_task_setup task_setup_data = {
2164 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002165 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002166 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002167 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002168 .flags = RPC_TASK_ASYNC,
2169 };
Trond Myklebust95121352005-10-18 14:20:12 -07002170 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002172 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002174 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002175 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002176 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002178 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002179 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002181 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002182 if (calldata->arg.seqid == NULL)
2183 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002184 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002185 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002186 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002187 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002188 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002189 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002190 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002191
Trond Myklebust1174dd12010-12-21 10:52:24 -05002192 msg.rpc_argp = &calldata->arg;
2193 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002194 task_setup_data.callback_data = calldata;
2195 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002196 if (IS_ERR(task))
2197 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002198 status = 0;
2199 if (wait)
2200 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002201 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002202 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002203out_free_calldata:
2204 kfree(calldata);
2205out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002206 if (roc)
2207 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002208 nfs4_put_open_state(state);
2209 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002210 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211}
2212
Trond Myklebust2b484292010-09-17 10:56:51 -04002213static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002214nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 struct nfs4_state *state;
2217
Trond Myklebust565277f2007-10-15 18:17:53 -04002218 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002219 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002220 if (IS_ERR(state))
2221 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002222 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002223 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224}
2225
Trond Myklebust1185a552009-12-03 15:54:02 -05002226static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002227{
2228 if (ctx->state == NULL)
2229 return;
2230 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002231 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002232 else
Al Viro643168c2011-06-22 18:20:23 -04002233 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002234}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2237{
Benny Halevy43652ad2009-04-01 09:21:54 -04002238 struct nfs4_server_caps_arg args = {
2239 .fhandle = fhandle,
2240 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 struct nfs4_server_caps_res res = {};
2242 struct rpc_message msg = {
2243 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002244 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 .rpc_resp = &res,
2246 };
2247 int status;
2248
Bryan Schumaker7c513052011-03-24 17:12:24 +00002249 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 if (status == 0) {
2251 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002252 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2253 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2254 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2255 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2256 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2258 server->caps |= NFS_CAP_ACLS;
2259 if (res.has_links != 0)
2260 server->caps |= NFS_CAP_HARDLINKS;
2261 if (res.has_symlinks != 0)
2262 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002263 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2264 server->caps |= NFS_CAP_FILEID;
2265 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2266 server->caps |= NFS_CAP_MODE;
2267 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2268 server->caps |= NFS_CAP_NLINK;
2269 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2270 server->caps |= NFS_CAP_OWNER;
2271 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2272 server->caps |= NFS_CAP_OWNER_GROUP;
2273 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2274 server->caps |= NFS_CAP_ATIME;
2275 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2276 server->caps |= NFS_CAP_CTIME;
2277 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2278 server->caps |= NFS_CAP_MTIME;
2279
Trond Myklebusta65318b2009-03-11 14:10:28 -04002280 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2281 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2282 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002284 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return status;
2288}
2289
Trond Myklebust55a97592006-06-09 09:34:19 -04002290int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291{
2292 struct nfs4_exception exception = { };
2293 int err;
2294 do {
2295 err = nfs4_handle_exception(server,
2296 _nfs4_server_capabilities(server, fhandle),
2297 &exception);
2298 } while (exception.retry);
2299 return err;
2300}
2301
2302static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2303 struct nfs_fsinfo *info)
2304{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 struct nfs4_lookup_root_arg args = {
2306 .bitmask = nfs4_fattr_bitmap,
2307 };
2308 struct nfs4_lookup_res res = {
2309 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002310 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 .fh = fhandle,
2312 };
2313 struct rpc_message msg = {
2314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2315 .rpc_argp = &args,
2316 .rpc_resp = &res,
2317 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002318
Trond Myklebust0e574af2005-10-27 22:12:38 -04002319 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002320 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321}
2322
2323static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2324 struct nfs_fsinfo *info)
2325{
2326 struct nfs4_exception exception = { };
2327 int err;
2328 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002329 err = _nfs4_lookup_root(server, fhandle, info);
2330 switch (err) {
2331 case 0:
2332 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002333 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002334 default:
2335 err = nfs4_handle_exception(server, err, &exception);
2336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002338out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 return err;
2340}
2341
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002342static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2343 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2344{
2345 struct rpc_auth *auth;
2346 int ret;
2347
2348 auth = rpcauth_create(flavor, server->client);
2349 if (!auth) {
2350 ret = -EIO;
2351 goto out;
2352 }
2353 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002354out:
2355 return ret;
2356}
2357
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002358static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002359 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002361 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002362 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002364 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2365 flav_array[len] = RPC_AUTH_NULL;
2366 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002367
2368 for (i = 0; i < len; i++) {
2369 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002370 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002371 continue;
2372 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002373 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002374 /*
2375 * -EACCESS could mean that the user doesn't have correct permissions
2376 * to access the mount. It could also mean that we tried to mount
2377 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2378 * existing mount programs don't handle -EACCES very well so it should
2379 * be mapped to -EPERM instead.
2380 */
2381 if (status == -EACCES)
2382 status = -EPERM;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002383 return status;
2384}
2385
2386/*
2387 * get the file handle for the "/" directory on the server
2388 */
2389static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2390 struct nfs_fsinfo *info)
2391{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002392 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002393 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002394 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2395 /*
2396 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2397 * by nfs4_map_errors() as this function exits.
2398 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002399 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 if (status == 0)
2401 status = nfs4_server_capabilities(server, fhandle);
2402 if (status == 0)
2403 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002404 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405}
2406
Manoj Naik6b97fd32006-06-09 09:34:29 -04002407/*
2408 * Get locations and (maybe) other attributes of a referral.
2409 * Note that we'll actually follow the referral later when
2410 * we detect fsid mismatch in inode revalidation
2411 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002412static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2413 const struct qstr *name, struct nfs_fattr *fattr,
2414 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002415{
2416 int status = -ENOMEM;
2417 struct page *page = NULL;
2418 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002419
2420 page = alloc_page(GFP_KERNEL);
2421 if (page == NULL)
2422 goto out;
2423 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2424 if (locations == NULL)
2425 goto out;
2426
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002427 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002428 if (status != 0)
2429 goto out;
2430 /* Make sure server returned a different fsid for the referral */
2431 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002432 dprintk("%s: server did not return a different fsid for"
2433 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002434 status = -EIO;
2435 goto out;
2436 }
Andy Adamson533eb462011-06-13 18:25:56 -04002437 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2438 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002439
Andy Adamson533eb462011-06-13 18:25:56 -04002440 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002441 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002442 memset(fhandle, 0, sizeof(struct nfs_fh));
2443out:
2444 if (page)
2445 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002446 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002447 return status;
2448}
2449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2451{
2452 struct nfs4_getattr_arg args = {
2453 .fh = fhandle,
2454 .bitmask = server->attr_bitmask,
2455 };
2456 struct nfs4_getattr_res res = {
2457 .fattr = fattr,
2458 .server = server,
2459 };
2460 struct rpc_message msg = {
2461 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2462 .rpc_argp = &args,
2463 .rpc_resp = &res,
2464 };
2465
Trond Myklebust0e574af2005-10-27 22:12:38 -04002466 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002467 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468}
2469
2470static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2471{
2472 struct nfs4_exception exception = { };
2473 int err;
2474 do {
2475 err = nfs4_handle_exception(server,
2476 _nfs4_proc_getattr(server, fhandle, fattr),
2477 &exception);
2478 } while (exception.retry);
2479 return err;
2480}
2481
2482/*
2483 * The file is not closed if it is opened due to the a request to change
2484 * the size of the file. The open call will not be needed once the
2485 * VFS layer lookup-intents are implemented.
2486 *
2487 * Close is called when the inode is destroyed.
2488 * If we haven't opened the file for O_WRONLY, we
2489 * need to in the size_change case to obtain a stateid.
2490 *
2491 * Got race?
2492 * Because OPEN is always done by name in nfsv4, it is
2493 * possible that we opened a different file by the same
2494 * name. We can recognize this race condition, but we
2495 * can't do anything about it besides returning an error.
2496 *
2497 * This will be fixed with VFS changes (lookup-intent).
2498 */
2499static int
2500nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2501 struct iattr *sattr)
2502{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002503 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002504 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002505 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 int status;
2507
Benny Halevy8a1636c2010-07-14 15:43:57 -04002508 if (pnfs_ld_layoutret_on_setattr(inode))
2509 pnfs_return_layout(inode);
2510
Trond Myklebust0e574af2005-10-27 22:12:38 -04002511 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Trond Myklebustd5308382005-11-04 15:33:38 -05002513 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002514 if (sattr->ia_valid & ATTR_FILE) {
2515 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002516
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002517 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002518 if (ctx) {
2519 cred = ctx->cred;
2520 state = ctx->state;
2521 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002522 }
2523
Trond Myklebust536e43d2012-01-17 22:04:26 -05002524 /* Deal with open(O_TRUNC) */
2525 if (sattr->ia_valid & ATTR_OPEN)
2526 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2527
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002528 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002529 if (status == 0)
2530 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 return status;
2532}
2533
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002534static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2535 const struct qstr *name, struct nfs_fh *fhandle,
2536 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002537{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002538 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002539 int status;
2540 struct nfs4_lookup_arg args = {
2541 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002542 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002543 .name = name,
2544 };
2545 struct nfs4_lookup_res res = {
2546 .server = server,
2547 .fattr = fattr,
2548 .fh = fhandle,
2549 };
2550 struct rpc_message msg = {
2551 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2552 .rpc_argp = &args,
2553 .rpc_resp = &res,
2554 };
2555
2556 nfs_fattr_init(fattr);
2557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002559 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 dprintk("NFS reply lookup: %d\n", status);
2561 return status;
2562}
2563
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002564static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002565{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002566 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002567 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002568 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2569 fattr->nlink = 2;
2570}
2571
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002572static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2573 struct qstr *name, struct nfs_fh *fhandle,
2574 struct nfs_fattr *fattr)
2575{
2576 struct nfs4_exception exception = { };
2577 struct rpc_clnt *client = *clnt;
2578 int err;
2579 do {
2580 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2581 switch (err) {
2582 case -NFS4ERR_BADNAME:
2583 err = -ENOENT;
2584 goto out;
2585 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002586 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002587 goto out;
2588 case -NFS4ERR_WRONGSEC:
2589 err = -EPERM;
2590 if (client != *clnt)
2591 goto out;
2592
2593 client = nfs4_create_sec_client(client, dir, name);
2594 if (IS_ERR(client))
2595 return PTR_ERR(client);
2596
2597 exception.retry = 1;
2598 break;
2599 default:
2600 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2601 }
2602 } while (exception.retry);
2603
2604out:
2605 if (err == 0)
2606 *clnt = client;
2607 else if (client != *clnt)
2608 rpc_shutdown_client(client);
2609
2610 return err;
2611}
2612
Bryan Schumaker7c513052011-03-24 17:12:24 +00002613static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2614 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002616 int status;
2617 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002618
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002619 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2620 if (client != NFS_CLIENT(dir)) {
2621 rpc_shutdown_client(client);
2622 nfs_fixup_secinfo_attributes(fattr);
2623 }
2624 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625}
2626
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002627struct rpc_clnt *
2628nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2629 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2630{
2631 int status;
2632 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2633
2634 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2635 if (status < 0) {
2636 rpc_shutdown_client(client);
2637 return ERR_PTR(status);
2638 }
2639 return client;
2640}
2641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2643{
Trond Myklebust76b32992007-08-10 17:45:11 -04002644 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 struct nfs4_accessargs args = {
2646 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002647 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002649 struct nfs4_accessres res = {
2650 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002651 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 struct rpc_message msg = {
2653 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2654 .rpc_argp = &args,
2655 .rpc_resp = &res,
2656 .rpc_cred = entry->cred,
2657 };
2658 int mode = entry->mask;
2659 int status;
2660
2661 /*
2662 * Determine which access bits we want to ask for...
2663 */
2664 if (mode & MAY_READ)
2665 args.access |= NFS4_ACCESS_READ;
2666 if (S_ISDIR(inode->i_mode)) {
2667 if (mode & MAY_WRITE)
2668 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2669 if (mode & MAY_EXEC)
2670 args.access |= NFS4_ACCESS_LOOKUP;
2671 } else {
2672 if (mode & MAY_WRITE)
2673 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2674 if (mode & MAY_EXEC)
2675 args.access |= NFS4_ACCESS_EXECUTE;
2676 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002677
2678 res.fattr = nfs_alloc_fattr();
2679 if (res.fattr == NULL)
2680 return -ENOMEM;
2681
Bryan Schumaker7c513052011-03-24 17:12:24 +00002682 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 if (!status) {
2684 entry->mask = 0;
2685 if (res.access & NFS4_ACCESS_READ)
2686 entry->mask |= MAY_READ;
2687 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2688 entry->mask |= MAY_WRITE;
2689 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2690 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002691 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002693 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 return status;
2695}
2696
2697static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2698{
2699 struct nfs4_exception exception = { };
2700 int err;
2701 do {
2702 err = nfs4_handle_exception(NFS_SERVER(inode),
2703 _nfs4_proc_access(inode, entry),
2704 &exception);
2705 } while (exception.retry);
2706 return err;
2707}
2708
2709/*
2710 * TODO: For the time being, we don't try to get any attributes
2711 * along with any of the zero-copy operations READ, READDIR,
2712 * READLINK, WRITE.
2713 *
2714 * In the case of the first three, we want to put the GETATTR
2715 * after the read-type operation -- this is because it is hard
2716 * to predict the length of a GETATTR response in v4, and thus
2717 * align the READ data correctly. This means that the GETATTR
2718 * may end up partially falling into the page cache, and we should
2719 * shift it into the 'tail' of the xdr_buf before processing.
2720 * To do this efficiently, we need to know the total length
2721 * of data received, which doesn't seem to be available outside
2722 * of the RPC layer.
2723 *
2724 * In the case of WRITE, we also want to put the GETATTR after
2725 * the operation -- in this case because we want to make sure
2726 * we get the post-operation mtime and size. This means that
2727 * we can't use xdr_encode_pages() as written: we need a variant
2728 * of it which would leave room in the 'tail' iovec.
2729 *
2730 * Both of these changes to the XDR layer would in fact be quite
2731 * minor, but I decided to leave them for a subsequent patch.
2732 */
2733static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2734 unsigned int pgbase, unsigned int pglen)
2735{
2736 struct nfs4_readlink args = {
2737 .fh = NFS_FH(inode),
2738 .pgbase = pgbase,
2739 .pglen = pglen,
2740 .pages = &page,
2741 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002742 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 struct rpc_message msg = {
2744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2745 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002746 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 };
2748
Bryan Schumaker7c513052011-03-24 17:12:24 +00002749 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 -07002750}
2751
2752static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2753 unsigned int pgbase, unsigned int pglen)
2754{
2755 struct nfs4_exception exception = { };
2756 int err;
2757 do {
2758 err = nfs4_handle_exception(NFS_SERVER(inode),
2759 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2760 &exception);
2761 } while (exception.retry);
2762 return err;
2763}
2764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765/*
Miklos Szeredibfa09372012-06-05 15:10:19 +02002766 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768static int
2769nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredibfa09372012-06-05 15:10:19 +02002770 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771{
Miklos Szeredibfa09372012-06-05 15:10:19 +02002772 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 int status = 0;
2775
Miklos Szeredibfa09372012-06-05 15:10:19 +02002776 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2777 if (IS_ERR(ctx))
2778 return PTR_ERR(ctx);
2779
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002780 sattr->ia_mode &= ~current_umask();
Miklos Szeredibfa09372012-06-05 15:10:19 +02002781 state = nfs4_do_open(dir, dentry, ctx->mode, flags, sattr, ctx->cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002782 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 if (IS_ERR(state)) {
2784 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002785 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002787 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002788 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Miklos Szeredibfa09372012-06-05 15:10:19 +02002789 ctx->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790out:
Miklos Szeredibfa09372012-06-05 15:10:19 +02002791 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 return status;
2793}
2794
2795static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2796{
Trond Myklebust16e42952005-10-27 22:12:44 -04002797 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002798 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 .fh = NFS_FH(dir),
Linus Torvalds6cd1e702012-05-10 13:14:12 -07002800 .name = *name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002801 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002803 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002804 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002805 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002807 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2808 .rpc_argp = &args,
2809 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002811 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
Trond Myklebustd3468902010-04-16 16:22:50 -04002813 res.dir_attr = nfs_alloc_fattr();
2814 if (res.dir_attr == NULL)
2815 goto out;
2816
Bryan Schumaker7c513052011-03-24 17:12:24 +00002817 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002818 if (status == 0) {
2819 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002820 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002821 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002822 nfs_free_fattr(res.dir_attr);
2823out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 return status;
2825}
2826
2827static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2828{
2829 struct nfs4_exception exception = { };
2830 int err;
2831 do {
2832 err = nfs4_handle_exception(NFS_SERVER(dir),
2833 _nfs4_proc_remove(dir, name),
2834 &exception);
2835 } while (exception.retry);
2836 return err;
2837}
2838
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002839static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002841 struct nfs_server *server = NFS_SERVER(dir);
2842 struct nfs_removeargs *args = msg->rpc_argp;
2843 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
Trond Myklebusta65318b2009-03-11 14:10:28 -04002845 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002846 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002848 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849}
2850
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002851static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2852{
2853 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2854 &data->args.seq_args,
2855 &data->res.seq_res,
2856 task))
2857 return;
2858 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859}
2860
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002861static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002863 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2864
Trond Myklebust14516c32010-07-31 14:29:06 -04002865 if (!nfs4_sequence_done(task, &res->seq_res))
2866 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002867 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002868 return 0;
2869 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002870 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002871 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872}
2873
Jeff Laytond3d41522010-09-17 17:31:57 -04002874static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2875{
2876 struct nfs_server *server = NFS_SERVER(dir);
2877 struct nfs_renameargs *arg = msg->rpc_argp;
2878 struct nfs_renameres *res = msg->rpc_resp;
2879
2880 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2881 arg->bitmask = server->attr_bitmask;
2882 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002883 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002884}
2885
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002886static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2887{
2888 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2889 &data->args.seq_args,
2890 &data->res.seq_res,
2891 task))
2892 return;
2893 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002894}
2895
2896static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2897 struct inode *new_dir)
2898{
2899 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2900
2901 if (!nfs4_sequence_done(task, &res->seq_res))
2902 return 0;
2903 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2904 return 0;
2905
2906 update_changeattr(old_dir, &res->old_cinfo);
2907 nfs_post_op_update_inode(old_dir, res->old_fattr);
2908 update_changeattr(new_dir, &res->new_cinfo);
2909 nfs_post_op_update_inode(new_dir, res->new_fattr);
2910 return 1;
2911}
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2914 struct inode *new_dir, struct qstr *new_name)
2915{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002916 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002917 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 .old_dir = NFS_FH(old_dir),
2919 .new_dir = NFS_FH(new_dir),
2920 .old_name = old_name,
2921 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002922 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002924 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002925 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002926 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 struct rpc_message msg = {
2928 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2929 .rpc_argp = &arg,
2930 .rpc_resp = &res,
2931 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002932 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Trond Myklebust011fff72010-04-16 16:22:49 -04002934 res.old_fattr = nfs_alloc_fattr();
2935 res.new_fattr = nfs_alloc_fattr();
2936 if (res.old_fattr == NULL || res.new_fattr == NULL)
2937 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
Bryan Schumaker7c513052011-03-24 17:12:24 +00002939 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 if (!status) {
2941 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002942 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002944 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002946out:
2947 nfs_free_fattr(res.new_fattr);
2948 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 return status;
2950}
2951
2952static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2953 struct inode *new_dir, struct qstr *new_name)
2954{
2955 struct nfs4_exception exception = { };
2956 int err;
2957 do {
2958 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2959 _nfs4_proc_rename(old_dir, old_name,
2960 new_dir, new_name),
2961 &exception);
2962 } while (exception.retry);
2963 return err;
2964}
2965
2966static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2967{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002968 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 struct nfs4_link_arg arg = {
2970 .fh = NFS_FH(inode),
2971 .dir_fh = NFS_FH(dir),
2972 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002973 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002975 struct nfs4_link_res res = {
2976 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002977 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 struct rpc_message msg = {
2979 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2980 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002981 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002983 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Trond Myklebust136f2622010-04-16 16:22:49 -04002985 res.fattr = nfs_alloc_fattr();
2986 res.dir_attr = nfs_alloc_fattr();
2987 if (res.fattr == NULL || res.dir_attr == NULL)
2988 goto out;
2989
Bryan Schumaker7c513052011-03-24 17:12:24 +00002990 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002991 if (!status) {
2992 update_changeattr(dir, &res.cinfo);
2993 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002994 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002995 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002996out:
2997 nfs_free_fattr(res.dir_attr);
2998 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 return status;
3000}
3001
3002static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3003{
3004 struct nfs4_exception exception = { };
3005 int err;
3006 do {
3007 err = nfs4_handle_exception(NFS_SERVER(inode),
3008 _nfs4_proc_link(inode, dir, name),
3009 &exception);
3010 } while (exception.retry);
3011 return err;
3012}
3013
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003014struct nfs4_createdata {
3015 struct rpc_message msg;
3016 struct nfs4_create_arg arg;
3017 struct nfs4_create_res res;
3018 struct nfs_fh fh;
3019 struct nfs_fattr fattr;
3020 struct nfs_fattr dir_fattr;
3021};
3022
3023static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3024 struct qstr *name, struct iattr *sattr, u32 ftype)
3025{
3026 struct nfs4_createdata *data;
3027
3028 data = kzalloc(sizeof(*data), GFP_KERNEL);
3029 if (data != NULL) {
3030 struct nfs_server *server = NFS_SERVER(dir);
3031
3032 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3033 data->msg.rpc_argp = &data->arg;
3034 data->msg.rpc_resp = &data->res;
3035 data->arg.dir_fh = NFS_FH(dir);
3036 data->arg.server = server;
3037 data->arg.name = name;
3038 data->arg.attrs = sattr;
3039 data->arg.ftype = ftype;
3040 data->arg.bitmask = server->attr_bitmask;
3041 data->res.server = server;
3042 data->res.fh = &data->fh;
3043 data->res.fattr = &data->fattr;
3044 data->res.dir_fattr = &data->dir_fattr;
3045 nfs_fattr_init(data->res.fattr);
3046 nfs_fattr_init(data->res.dir_fattr);
3047 }
3048 return data;
3049}
3050
3051static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3052{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003053 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003054 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003055 if (status == 0) {
3056 update_changeattr(dir, &data->res.dir_cinfo);
3057 nfs_post_op_update_inode(dir, data->res.dir_fattr);
3058 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3059 }
3060 return status;
3061}
3062
3063static void nfs4_free_createdata(struct nfs4_createdata *data)
3064{
3065 kfree(data);
3066}
3067
Chuck Lever4f390c12006-08-22 20:06:22 -04003068static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003069 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003071 struct nfs4_createdata *data;
3072 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
Chuck Lever94a6d752006-08-22 20:06:23 -04003074 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003075 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003076
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003077 status = -ENOMEM;
3078 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3079 if (data == NULL)
3080 goto out;
3081
3082 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3083 data->arg.u.symlink.pages = &page;
3084 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003086 status = nfs4_do_create(dir, dentry, data);
3087
3088 nfs4_free_createdata(data);
3089out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 return status;
3091}
3092
Chuck Lever4f390c12006-08-22 20:06:22 -04003093static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003094 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095{
3096 struct nfs4_exception exception = { };
3097 int err;
3098 do {
3099 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003100 _nfs4_proc_symlink(dir, dentry, page,
3101 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 &exception);
3103 } while (exception.retry);
3104 return err;
3105}
3106
3107static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3108 struct iattr *sattr)
3109{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003110 struct nfs4_createdata *data;
3111 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003113 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3114 if (data == NULL)
3115 goto out;
3116
3117 status = nfs4_do_create(dir, dentry, data);
3118
3119 nfs4_free_createdata(data);
3120out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 return status;
3122}
3123
3124static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3125 struct iattr *sattr)
3126{
3127 struct nfs4_exception exception = { };
3128 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003129
3130 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 do {
3132 err = nfs4_handle_exception(NFS_SERVER(dir),
3133 _nfs4_proc_mkdir(dir, dentry, sattr),
3134 &exception);
3135 } while (exception.retry);
3136 return err;
3137}
3138
3139static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003140 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141{
3142 struct inode *dir = dentry->d_inode;
3143 struct nfs4_readdir_arg args = {
3144 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003145 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 .pgbase = 0,
3147 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003148 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003149 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 };
3151 struct nfs4_readdir_res res;
3152 struct rpc_message msg = {
3153 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3154 .rpc_argp = &args,
3155 .rpc_resp = &res,
3156 .rpc_cred = cred,
3157 };
3158 int status;
3159
Harvey Harrison3110ff82008-05-02 13:42:44 -07003160 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003161 dentry->d_parent->d_name.name,
3162 dentry->d_name.name,
3163 (unsigned long long)cookie);
Trond Myklebust91addc02012-09-03 14:56:02 -04003164 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003166 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 -05003167 if (status >= 0) {
Trond Myklebust91addc02012-09-03 14:56:02 -04003168 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003169 status += args.pgbase;
3170 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003171
3172 nfs_invalidate_atime(dir);
3173
Harvey Harrison3110ff82008-05-02 13:42:44 -07003174 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 return status;
3176}
3177
3178static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003179 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180{
3181 struct nfs4_exception exception = { };
3182 int err;
3183 do {
3184 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3185 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003186 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 &exception);
3188 } while (exception.retry);
3189 return err;
3190}
3191
3192static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3193 struct iattr *sattr, dev_t rdev)
3194{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003195 struct nfs4_createdata *data;
3196 int mode = sattr->ia_mode;
3197 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
3199 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3200 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003201
3202 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3203 if (data == NULL)
3204 goto out;
3205
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003207 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003209 data->arg.ftype = NF4BLK;
3210 data->arg.u.device.specdata1 = MAJOR(rdev);
3211 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 }
3213 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003214 data->arg.ftype = NF4CHR;
3215 data->arg.u.device.specdata1 = MAJOR(rdev);
3216 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003219 status = nfs4_do_create(dir, dentry, data);
3220
3221 nfs4_free_createdata(data);
3222out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 return status;
3224}
3225
3226static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3227 struct iattr *sattr, dev_t rdev)
3228{
3229 struct nfs4_exception exception = { };
3230 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003231
3232 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 do {
3234 err = nfs4_handle_exception(NFS_SERVER(dir),
3235 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3236 &exception);
3237 } while (exception.retry);
3238 return err;
3239}
3240
3241static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3242 struct nfs_fsstat *fsstat)
3243{
3244 struct nfs4_statfs_arg args = {
3245 .fh = fhandle,
3246 .bitmask = server->attr_bitmask,
3247 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003248 struct nfs4_statfs_res res = {
3249 .fsstat = fsstat,
3250 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 struct rpc_message msg = {
3252 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3253 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003254 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 };
3256
Trond Myklebust0e574af2005-10-27 22:12:38 -04003257 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003258 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259}
3260
3261static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3262{
3263 struct nfs4_exception exception = { };
3264 int err;
3265 do {
3266 err = nfs4_handle_exception(server,
3267 _nfs4_proc_statfs(server, fhandle, fsstat),
3268 &exception);
3269 } while (exception.retry);
3270 return err;
3271}
3272
3273static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3274 struct nfs_fsinfo *fsinfo)
3275{
3276 struct nfs4_fsinfo_arg args = {
3277 .fh = fhandle,
3278 .bitmask = server->attr_bitmask,
3279 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003280 struct nfs4_fsinfo_res res = {
3281 .fsinfo = fsinfo,
3282 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 struct rpc_message msg = {
3284 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3285 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003286 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 };
3288
Bryan Schumaker7c513052011-03-24 17:12:24 +00003289 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290}
3291
3292static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3293{
3294 struct nfs4_exception exception = { };
3295 int err;
3296
3297 do {
3298 err = nfs4_handle_exception(server,
3299 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3300 &exception);
3301 } while (exception.retry);
3302 return err;
3303}
3304
3305static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3306{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003307 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3309}
3310
3311static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3312 struct nfs_pathconf *pathconf)
3313{
3314 struct nfs4_pathconf_arg args = {
3315 .fh = fhandle,
3316 .bitmask = server->attr_bitmask,
3317 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003318 struct nfs4_pathconf_res res = {
3319 .pathconf = pathconf,
3320 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 struct rpc_message msg = {
3322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3323 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003324 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 };
3326
3327 /* None of the pathconf attributes are mandatory to implement */
3328 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3329 memset(pathconf, 0, sizeof(*pathconf));
3330 return 0;
3331 }
3332
Trond Myklebust0e574af2005-10-27 22:12:38 -04003333 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003334 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335}
3336
3337static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3338 struct nfs_pathconf *pathconf)
3339{
3340 struct nfs4_exception exception = { };
3341 int err;
3342
3343 do {
3344 err = nfs4_handle_exception(server,
3345 _nfs4_proc_pathconf(server, fhandle, pathconf),
3346 &exception);
3347 } while (exception.retry);
3348 return err;
3349}
3350
Benny Halevyd20581a2011-05-22 19:52:03 +03003351void __nfs4_read_done_cb(struct nfs_read_data *data)
3352{
3353 nfs_invalidate_atime(data->inode);
3354}
3355
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003356static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357{
Trond Myklebustec06c092006-03-20 13:44:27 -05003358 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003360 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003361 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003362 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003364
Benny Halevyd20581a2011-05-22 19:52:03 +03003365 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003367 renew_lease(server, data->timestamp);
3368 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369}
3370
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003371static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3372{
3373
3374 dprintk("--> %s\n", __func__);
3375
3376 if (!nfs4_sequence_done(task, &data->res.seq_res))
3377 return -EAGAIN;
3378
Benny Halevyd20581a2011-05-22 19:52:03 +03003379 return data->read_done_cb ? data->read_done_cb(task, data) :
3380 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003381}
3382
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003383static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003386 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003387 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003388 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389}
3390
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003391static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3392{
3393 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3394 &data->args.seq_args,
3395 &data->res.seq_res,
3396 task))
3397 return;
3398 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399}
3400
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003401/* Reset the the nfs_read_data to send the read to the MDS. */
3402void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3403{
3404 dprintk("%s Reset task for i/o through\n", __func__);
3405 put_lseg(data->lseg);
3406 data->lseg = NULL;
3407 /* offsets will differ in the dense stripe case */
3408 data->args.offset = data->mds_offset;
3409 data->ds_clp = NULL;
3410 data->args.fh = NFS_FH(data->inode);
3411 data->read_done_cb = nfs4_read_done_cb;
3412 task->tk_ops = data->mds_ops;
3413 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3414}
3415EXPORT_SYMBOL_GPL(nfs4_reset_read);
3416
Fred Isamanb029bc92011-03-03 15:13:42 +00003417static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 struct inode *inode = data->inode;
3420
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003421 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003422 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003423 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003425 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003427 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003428 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430}
3431
Fred Isamanb029bc92011-03-03 15:13:42 +00003432static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3433{
3434 if (!nfs4_sequence_done(task, &data->res.seq_res))
3435 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003436 return data->write_done_cb ? data->write_done_cb(task, data) :
3437 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003438}
3439
Fred Isamana69aef12011-03-03 15:13:47 +00003440/* Reset the the nfs_write_data to send the write to the MDS. */
3441void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3442{
3443 dprintk("%s Reset task for i/o through\n", __func__);
3444 put_lseg(data->lseg);
3445 data->lseg = NULL;
3446 data->ds_clp = NULL;
3447 data->write_done_cb = nfs4_write_done_cb;
3448 data->args.fh = NFS_FH(data->inode);
3449 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3450 data->args.offset = data->mds_offset;
3451 data->res.fattr = &data->fattr;
3452 task->tk_ops = data->mds_ops;
3453 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3454}
3455EXPORT_SYMBOL_GPL(nfs4_reset_write);
3456
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003457static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003459 struct nfs_server *server = NFS_SERVER(data->inode);
3460
Fred Isaman7ffd1062011-03-03 15:13:46 +00003461 if (data->lseg) {
3462 data->args.bitmask = NULL;
3463 data->res.fattr = NULL;
3464 } else
3465 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003466 if (!data->write_done_cb)
3467 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003468 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 data->timestamp = jiffies;
3470
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003471 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003472 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473}
3474
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003475static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3476{
3477 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3478 &data->args.seq_args,
3479 &data->res.seq_res,
3480 task))
3481 return;
3482 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483}
3484
Fred Isaman5f452432011-03-23 13:27:46 +00003485static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003488
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003489 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003490 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003491 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003493 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003494 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495}
3496
Fred Isaman5f452432011-03-23 13:27:46 +00003497static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3498{
3499 if (!nfs4_sequence_done(task, &data->res.seq_res))
3500 return -EAGAIN;
3501 return data->write_done_cb(task, data);
3502}
3503
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003504static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003506 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003507
3508 if (data->lseg) {
3509 data->args.bitmask = NULL;
3510 data->res.fattr = NULL;
3511 } else
3512 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003513 if (!data->write_done_cb)
3514 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003515 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003516 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003517 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518}
3519
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003520struct nfs4_renewdata {
3521 struct nfs_client *client;
3522 unsigned long timestamp;
3523};
3524
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525/*
3526 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3527 * standalone procedure for queueing an asynchronous RENEW.
3528 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003529static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003530{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003531 struct nfs4_renewdata *data = calldata;
3532 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003533
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003534 if (atomic_read(&clp->cl_count) > 1)
3535 nfs4_schedule_state_renewal(clp);
3536 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003537 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003538}
3539
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003540static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003542 struct nfs4_renewdata *data = calldata;
3543 struct nfs_client *clp = data->client;
3544 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
3546 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003547 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003548 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3549 return;
3550 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003551 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003552 return;
3553 }
3554 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003556 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557}
3558
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003559static const struct rpc_call_ops nfs4_renew_ops = {
3560 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003561 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003562};
3563
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003564static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565{
3566 struct rpc_message msg = {
3567 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3568 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003569 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003571 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003573 if (renew_flags == 0)
3574 return 0;
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003575 if (!atomic_inc_not_zero(&clp->cl_count))
3576 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003577 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003578 if (data == NULL)
3579 return -ENOMEM;
3580 data->client = clp;
3581 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003583 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584}
3585
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003586static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
3588 struct rpc_message msg = {
3589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3590 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003591 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 };
3593 unsigned long now = jiffies;
3594 int status;
3595
3596 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3597 if (status < 0)
3598 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003599 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 return 0;
3601}
3602
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003603static inline int nfs4_server_supports_acls(struct nfs_server *server)
3604{
3605 return (server->caps & NFS_CAP_ACLS)
3606 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3607 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3608}
3609
3610/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3611 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3612 * the stack.
3613 */
3614#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3615
Neil Hormane9e3d722011-03-04 19:26:03 -05003616static int buf_to_pages_noslab(const void *buf, size_t buflen,
3617 struct page **pages, unsigned int *pgbase)
3618{
3619 struct page *newpage, **spages;
3620 int rc = 0;
3621 size_t len;
3622 spages = pages;
3623
3624 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003625 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003626 newpage = alloc_page(GFP_KERNEL);
3627
3628 if (newpage == NULL)
3629 goto unwind;
3630 memcpy(page_address(newpage), buf, len);
3631 buf += len;
3632 buflen -= len;
3633 *pages++ = newpage;
3634 rc++;
3635 } while (buflen != 0);
3636
3637 return rc;
3638
3639unwind:
3640 for(; rc > 0; rc--)
3641 __free_page(spages[rc-1]);
3642 return -ENOMEM;
3643}
3644
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003645struct nfs4_cached_acl {
3646 int cached;
3647 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003648 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003649};
3650
3651static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003652{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003653 struct nfs_inode *nfsi = NFS_I(inode);
3654
3655 spin_lock(&inode->i_lock);
3656 kfree(nfsi->nfs4_acl);
3657 nfsi->nfs4_acl = acl;
3658 spin_unlock(&inode->i_lock);
3659}
3660
3661static void nfs4_zap_acl_attr(struct inode *inode)
3662{
3663 nfs4_set_cached_acl(inode, NULL);
3664}
3665
3666static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3667{
3668 struct nfs_inode *nfsi = NFS_I(inode);
3669 struct nfs4_cached_acl *acl;
3670 int ret = -ENOENT;
3671
3672 spin_lock(&inode->i_lock);
3673 acl = nfsi->nfs4_acl;
3674 if (acl == NULL)
3675 goto out;
3676 if (buf == NULL) /* user is just asking for length */
3677 goto out_len;
3678 if (acl->cached == 0)
3679 goto out;
3680 ret = -ERANGE; /* see getxattr(2) man page */
3681 if (acl->len > buflen)
3682 goto out;
3683 memcpy(buf, acl->data, acl->len);
3684out_len:
3685 ret = acl->len;
3686out:
3687 spin_unlock(&inode->i_lock);
3688 return ret;
3689}
3690
Sachin Prabhu5794d212012-04-17 14:36:40 +01003691static 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 +00003692{
3693 struct nfs4_cached_acl *acl;
3694
Sachin Prabhu5794d212012-04-17 14:36:40 +01003695 if (pages && acl_len <= PAGE_SIZE) {
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003696 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3697 if (acl == NULL)
3698 goto out;
3699 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003700 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003701 } else {
3702 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3703 if (acl == NULL)
3704 goto out;
3705 acl->cached = 0;
3706 }
3707 acl->len = acl_len;
3708out:
3709 nfs4_set_cached_acl(inode, acl);
3710}
3711
Andy Adamsonbf118a32011-12-07 11:55:27 -05003712/*
3713 * The getxattr API returns the required buffer length when called with a
3714 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3715 * the required buf. On a NULL buf, we send a page of data to the server
3716 * guessing that the ACL request can be serviced by a page. If so, we cache
3717 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3718 * the cache. If not so, we throw away the page, and cache the required
3719 * length. The next getxattr call will then produce another round trip to
3720 * the server, this time with the input buf of the required size.
3721 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003722static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003723{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003724 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003725 struct nfs_getaclargs args = {
3726 .fh = NFS_FH(inode),
3727 .acl_pages = pages,
3728 .acl_len = buflen,
3729 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003730 struct nfs_getaclres res = {
3731 .acl_len = buflen,
3732 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003733 struct rpc_message msg = {
3734 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3735 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003736 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003737 };
Chuck Lever682ed4b2012-07-11 16:30:32 -04003738 int ret = -ENOMEM, npages, i;
3739 size_t acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003740
Andy Adamsonbf118a32011-12-07 11:55:27 -05003741 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3742 /* As long as we're doing a round trip to the server anyway,
3743 * let's be prepared for a page of acl data. */
3744 if (npages == 0)
3745 npages = 1;
3746
Sachin Prabhu5a006892012-04-17 14:35:39 +01003747 /* Add an extra page to handle the bitmap returned */
3748 npages++;
3749
Andy Adamsonbf118a32011-12-07 11:55:27 -05003750 for (i = 0; i < npages; i++) {
3751 pages[i] = alloc_page(GFP_KERNEL);
3752 if (!pages[i])
3753 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003754 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003755
3756 /* for decoding across pages */
3757 res.acl_scratch = alloc_page(GFP_KERNEL);
3758 if (!res.acl_scratch)
3759 goto out_free;
3760
Andy Adamsonbf118a32011-12-07 11:55:27 -05003761 args.acl_len = npages * PAGE_SIZE;
3762 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003763
Andy Adamsonbf118a32011-12-07 11:55:27 -05003764 /* Let decode_getfacl know not to fail if the ACL data is larger than
3765 * the page we send as a guess */
3766 if (buf == NULL)
3767 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003768
Peng Taode040be2012-01-10 22:42:47 +08003769 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003770 __func__, buf, buflen, npages, args.acl_len);
3771 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3772 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003773 if (ret)
3774 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003775
3776 acl_len = res.acl_len - res.acl_data_offset;
3777 if (acl_len > args.acl_len)
Sachin Prabhu5794d212012-04-17 14:36:40 +01003778 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003779 else
Sachin Prabhu5794d212012-04-17 14:36:40 +01003780 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
Andy Adamsonbf118a32011-12-07 11:55:27 -05003781 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003782 if (buf) {
3783 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003784 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003785 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003786 _copy_from_pages(buf, pages, res.acl_data_offset,
Sachin Prabhu20e0fa92012-03-22 16:46:28 +00003787 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003788 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003789 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003790out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003791 for (i = 0; i < npages; i++)
3792 if (pages[i])
3793 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003794 if (res.acl_scratch)
3795 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003796 return ret;
3797}
3798
Trond Myklebust16b42892006-08-24 12:27:15 -04003799static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3800{
3801 struct nfs4_exception exception = { };
3802 ssize_t ret;
3803 do {
3804 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3805 if (ret >= 0)
3806 break;
3807 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3808 } while (exception.retry);
3809 return ret;
3810}
3811
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003812static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3813{
3814 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003815 int ret;
3816
3817 if (!nfs4_server_supports_acls(server))
3818 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003819 ret = nfs_revalidate_inode(server, inode);
3820 if (ret < 0)
3821 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003822 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3823 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003824 ret = nfs4_read_cached_acl(inode, buf, buflen);
3825 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003826 /* -ENOENT is returned if there is no ACL or if there is an ACL
3827 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003828 return ret;
3829 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003830}
3831
Trond Myklebust16b42892006-08-24 12:27:15 -04003832static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003833{
3834 struct nfs_server *server = NFS_SERVER(inode);
3835 struct page *pages[NFS4ACL_MAXPAGES];
3836 struct nfs_setaclargs arg = {
3837 .fh = NFS_FH(inode),
3838 .acl_pages = pages,
3839 .acl_len = buflen,
3840 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003841 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003842 struct rpc_message msg = {
3843 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3844 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003845 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003846 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003847 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003848
3849 if (!nfs4_server_supports_acls(server))
3850 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003851 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3852 if (i < 0)
3853 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003854 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003855 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003856
3857 /*
3858 * Free each page after tx, so the only ref left is
3859 * held by the network stack
3860 */
3861 for (; i > 0; i--)
3862 put_page(pages[i-1]);
3863
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003864 /*
3865 * Acl update can result in inode attribute update.
3866 * so mark the attribute cache invalid.
3867 */
3868 spin_lock(&inode->i_lock);
3869 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3870 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003871 nfs_access_zap_cache(inode);
3872 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003873 return ret;
3874}
3875
Trond Myklebust16b42892006-08-24 12:27:15 -04003876static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3877{
3878 struct nfs4_exception exception = { };
3879 int err;
3880 do {
3881 err = nfs4_handle_exception(NFS_SERVER(inode),
3882 __nfs4_proc_set_acl(inode, buf, buflen),
3883 &exception);
3884 } while (exception.retry);
3885 return err;
3886}
3887
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003889nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003891 struct nfs_client *clp = server->nfs_client;
3892
3893 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 return 0;
3895 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003896 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003897 case -NFS4ERR_ADMIN_REVOKED:
3898 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003899 if (state == NULL)
3900 break;
3901 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003902 case -NFS4ERR_OPENMODE:
3903 if (state == NULL)
3904 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003905 nfs4_schedule_stateid_recovery(server, state);
3906 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003907 case -NFS4ERR_EXPIRED:
3908 if (state != NULL)
3909 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003911 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003912 nfs4_schedule_lease_recovery(clp);
3913 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003914#if defined(CONFIG_NFS_V4_1)
3915 case -NFS4ERR_BADSESSION:
3916 case -NFS4ERR_BADSLOT:
3917 case -NFS4ERR_BAD_HIGH_SLOT:
3918 case -NFS4ERR_DEADSESSION:
3919 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3920 case -NFS4ERR_SEQ_FALSE_RETRY:
3921 case -NFS4ERR_SEQ_MISORDERED:
3922 dprintk("%s ERROR %d, Reset session\n", __func__,
3923 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003924 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson7aa17922013-11-15 16:36:16 -05003925 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003926#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003928 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003929 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3931 task->tk_status = 0;
3932 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003933 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 case -NFS4ERR_OLD_STATEID:
3935 task->tk_status = 0;
3936 return -EAGAIN;
3937 }
3938 task->tk_status = nfs4_map_errors(task->tk_status);
3939 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003940wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003941 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003942 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3943 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3944 task->tk_status = 0;
3945 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946}
3947
Chuck Levercd937102012-03-02 17:14:31 -05003948static void nfs4_construct_boot_verifier(struct nfs_client *clp,
3949 nfs4_verifier *bootverf)
3950{
3951 __be32 verf[2];
3952
3953 verf[0] = htonl((u32)clp->cl_boot_time.tv_sec);
3954 verf[1] = htonl((u32)clp->cl_boot_time.tv_nsec);
3955 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3956}
3957
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003958int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3959 unsigned short port, struct rpc_cred *cred,
3960 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961{
3962 nfs4_verifier sc_verifier;
3963 struct nfs4_setclientid setclientid = {
3964 .sc_verifier = &sc_verifier,
3965 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003966 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 };
3968 struct rpc_message msg = {
3969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3970 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003971 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003972 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 int loop = 0;
3975 int status;
3976
Chuck Levercd937102012-03-02 17:14:31 -05003977 nfs4_construct_boot_verifier(clp, &sc_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978
3979 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05003980 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003982 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003983 clp->cl_ipaddr,
3984 rpc_peeraddr2str(clp->cl_rpcclient,
3985 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003986 rpc_peeraddr2str(clp->cl_rpcclient,
3987 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003988 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 clp->cl_id_uniquifier);
3990 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003991 sizeof(setclientid.sc_netid),
3992 rpc_peeraddr2str(clp->cl_rpcclient,
3993 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003995 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05003997 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003999 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 if (status != -NFS4ERR_CLID_INUSE)
4001 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004002 if (loop != 0) {
4003 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004005 }
4006 ++loop;
4007 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 }
4009 return status;
4010}
4011
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004012int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004013 struct nfs4_setclientid_res *arg,
4014 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015{
4016 struct nfs_fsinfo fsinfo;
4017 struct rpc_message msg = {
4018 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004019 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004021 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 };
4023 unsigned long now;
4024 int status;
4025
4026 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004027 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 if (status == 0) {
4029 spin_lock(&clp->cl_lock);
4030 clp->cl_lease_time = fsinfo.lease_time * HZ;
4031 clp->cl_last_renewal = now;
4032 spin_unlock(&clp->cl_lock);
4033 }
4034 return status;
4035}
4036
Trond Myklebustfe650402006-01-03 09:55:18 +01004037struct nfs4_delegreturndata {
4038 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004039 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004040 struct nfs_fh fh;
4041 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004042 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004043 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004044 int rpc_status;
4045};
4046
Trond Myklebustfe650402006-01-03 09:55:18 +01004047static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4048{
4049 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004050
Trond Myklebust14516c32010-07-31 14:29:06 -04004051 if (!nfs4_sequence_done(task, &data->res.seq_res))
4052 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004053
Ricardo Labiaga79708862009-12-07 09:23:21 -05004054 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05004055 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004056 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004057 break;
Trond Myklebustea36a932013-11-19 16:34:14 -05004058 case -NFS4ERR_ADMIN_REVOKED:
4059 case -NFS4ERR_DELEG_REVOKED:
4060 case -NFS4ERR_BAD_STATEID:
4061 case -NFS4ERR_OLD_STATEID:
4062 case -NFS4ERR_STALE_STATEID:
4063 case -NFS4ERR_EXPIRED:
4064 task->tk_status = 0;
4065 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05004066 default:
4067 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4068 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004069 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004070 return;
4071 }
4072 }
4073 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004074}
4075
4076static void nfs4_delegreturn_release(void *calldata)
4077{
Trond Myklebustfe650402006-01-03 09:55:18 +01004078 kfree(calldata);
4079}
4080
Andy Adamson938e1012009-04-01 09:22:28 -04004081#if defined(CONFIG_NFS_V4_1)
4082static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4083{
4084 struct nfs4_delegreturndata *d_data;
4085
4086 d_data = (struct nfs4_delegreturndata *)data;
4087
Trond Myklebust035168a2010-06-16 09:52:26 -04004088 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004089 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004090 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004091 return;
4092 rpc_call_start(task);
4093}
4094#endif /* CONFIG_NFS_V4_1 */
4095
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004096static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004097#if defined(CONFIG_NFS_V4_1)
4098 .rpc_call_prepare = nfs4_delegreturn_prepare,
4099#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004100 .rpc_call_done = nfs4_delegreturn_done,
4101 .rpc_release = nfs4_delegreturn_release,
4102};
4103
Trond Myklebuste6f81072008-01-24 18:14:34 -05004104static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004105{
4106 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004107 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004108 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004109 struct rpc_message msg = {
4110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4111 .rpc_cred = cred,
4112 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004113 struct rpc_task_setup task_setup_data = {
4114 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004115 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004116 .callback_ops = &nfs4_delegreturn_ops,
4117 .flags = RPC_TASK_ASYNC,
4118 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004119 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004120
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004121 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004122 if (data == NULL)
4123 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004124 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004125 data->args.fhandle = &data->fh;
4126 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004127 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004128 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004129 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004130 data->res.fattr = &data->fattr;
4131 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004132 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004133 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004134 data->rpc_status = 0;
4135
Trond Myklebustc970aa82007-07-14 15:39:59 -04004136 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004137 msg.rpc_argp = &data->args;
4138 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004139 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004140 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004141 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004142 if (!issync)
4143 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004144 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004145 if (status != 0)
4146 goto out;
4147 status = data->rpc_status;
4148 if (status != 0)
4149 goto out;
4150 nfs_refresh_inode(inode, &data->fattr);
4151out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004152 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004153 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154}
4155
Trond Myklebuste6f81072008-01-24 18:14:34 -05004156int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157{
4158 struct nfs_server *server = NFS_SERVER(inode);
4159 struct nfs4_exception exception = { };
4160 int err;
4161 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004162 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 switch (err) {
4164 case -NFS4ERR_STALE_STATEID:
4165 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 case 0:
4167 return 0;
4168 }
4169 err = nfs4_handle_exception(server, err, &exception);
4170 } while (exception.retry);
4171 return err;
4172}
4173
4174#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4175#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4176
4177/*
4178 * sleep, with exponential backoff, and retry the LOCK operation.
4179 */
4180static unsigned long
4181nfs4_set_lock_task_retry(unsigned long timeout)
4182{
Colin Cross6072cdb2013-05-06 23:50:06 +00004183 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 timeout <<= 1;
4185 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4186 return NFS4_LOCK_MAXTIMEOUT;
4187 return timeout;
4188}
4189
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4191{
4192 struct inode *inode = state->inode;
4193 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004194 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004195 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004197 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004199 struct nfs_lockt_res res = {
4200 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 };
4202 struct rpc_message msg = {
4203 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4204 .rpc_argp = &arg,
4205 .rpc_resp = &res,
4206 .rpc_cred = state->owner->so_cred,
4207 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 struct nfs4_lock_state *lsp;
4209 int status;
4210
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004211 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004212 status = nfs4_set_lock_state(state, request);
4213 if (status != 0)
4214 goto out;
4215 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004216 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004217 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004218 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004219 switch (status) {
4220 case 0:
4221 request->fl_type = F_UNLCK;
4222 break;
4223 case -NFS4ERR_DENIED:
4224 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004226 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0581d2013-10-01 14:24:58 -04004227 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004228out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 return status;
4230}
4231
4232static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4233{
4234 struct nfs4_exception exception = { };
4235 int err;
4236
4237 do {
4238 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4239 _nfs4_proc_getlk(state, cmd, request),
4240 &exception);
4241 } while (exception.retry);
4242 return err;
4243}
4244
4245static int do_vfs_lock(struct file *file, struct file_lock *fl)
4246{
4247 int res = 0;
4248 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4249 case FL_POSIX:
4250 res = posix_lock_file_wait(file, fl);
4251 break;
4252 case FL_FLOCK:
4253 res = flock_lock_file_wait(file, fl);
4254 break;
4255 default:
4256 BUG();
4257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 return res;
4259}
4260
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004261struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004262 struct nfs_locku_args arg;
4263 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004264 struct nfs4_lock_state *lsp;
4265 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004266 struct file_lock fl;
4267 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004268 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004269};
4270
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004271static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4272 struct nfs_open_context *ctx,
4273 struct nfs4_lock_state *lsp,
4274 struct nfs_seqid *seqid)
4275{
4276 struct nfs4_unlockdata *p;
4277 struct inode *inode = lsp->ls_state->inode;
4278
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004279 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004280 if (p == NULL)
4281 return NULL;
4282 p->arg.fh = NFS_FH(inode);
4283 p->arg.fl = &p->fl;
4284 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004285 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004286 p->arg.stateid = &lsp->ls_stateid;
4287 p->lsp = lsp;
4288 atomic_inc(&lsp->ls_count);
4289 /* Ensure we don't close file until we're done freeing locks! */
4290 p->ctx = get_nfs_open_context(ctx);
4291 memcpy(&p->fl, fl, sizeof(p->fl));
4292 p->server = NFS_SERVER(inode);
4293 return p;
4294}
4295
Trond Myklebust06f814a2006-01-03 09:55:07 +01004296static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004297{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004298 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004299 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004300 nfs4_put_lock_state(calldata->lsp);
4301 put_nfs_open_context(calldata->ctx);
4302 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004303}
4304
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004305static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004306{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004307 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004308
Trond Myklebust14516c32010-07-31 14:29:06 -04004309 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4310 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004311 switch (task->tk_status) {
4312 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004313 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4314 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004315 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004316 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004317 case -NFS4ERR_BAD_STATEID:
4318 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004319 case -NFS4ERR_STALE_STATEID:
4320 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004321 break;
4322 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004323 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004324 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004325 }
Trond Myklebust94e018a2012-10-29 18:53:23 -04004326 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004327}
4328
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004329static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004330{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004331 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004333 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004334 return;
4335 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004336 /* Note: exit _without_ running nfs4_locku_done */
4337 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004338 return;
4339 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004340 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004341 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004342 &calldata->arg.seq_args,
Trond Myklebustb8480fc2012-10-29 18:37:40 -04004343 &calldata->res.seq_res,
4344 task) != 0)
4345 nfs_release_seqid(calldata->arg.seqid);
4346 else
4347 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348}
4349
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004350static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004351 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004352 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004353 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004354};
4355
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004356static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4357 struct nfs_open_context *ctx,
4358 struct nfs4_lock_state *lsp,
4359 struct nfs_seqid *seqid)
4360{
4361 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004362 struct rpc_message msg = {
4363 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4364 .rpc_cred = ctx->cred,
4365 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004366 struct rpc_task_setup task_setup_data = {
4367 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004368 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004369 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004370 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004371 .flags = RPC_TASK_ASYNC,
4372 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004373
Frank Filz137d6ac2007-07-09 15:32:29 -07004374 /* Ensure this is an unlock - when canceling a lock, the
4375 * canceled lock is passed in, and it won't be an unlock.
4376 */
4377 fl->fl_type = F_UNLCK;
4378
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004379 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4380 if (data == NULL) {
4381 nfs_free_seqid(seqid);
4382 return ERR_PTR(-ENOMEM);
4383 }
4384
Trond Myklebust9d12b212012-01-17 22:04:25 -05004385 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004386 msg.rpc_argp = &data->arg;
4387 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004388 task_setup_data.callback_data = data;
4389 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004390}
4391
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4393{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004394 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004395 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004396 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004397 struct rpc_task *task;
4398 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004399 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
Trond Myklebust9b073572006-06-29 16:38:34 -04004401 status = nfs4_set_lock_state(state, request);
4402 /* Unlock _before_ we do the RPC call */
4403 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004404 down_read(&nfsi->rwsem);
4405 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4406 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004407 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004408 }
4409 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004410 if (status != 0)
4411 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004412 /* Is this a delegated lock? */
4413 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004414 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004415 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004416 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004417 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004418 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004419 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004420 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004421 status = PTR_ERR(task);
4422 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004423 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004424 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004425 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004426out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004427 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004428 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429}
4430
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004431struct nfs4_lockdata {
4432 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004433 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004434 struct nfs4_lock_state *lsp;
4435 struct nfs_open_context *ctx;
4436 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004437 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004438 int rpc_status;
4439 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004440 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004441};
4442
4443static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004444 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4445 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004446{
4447 struct nfs4_lockdata *p;
4448 struct inode *inode = lsp->ls_state->inode;
4449 struct nfs_server *server = NFS_SERVER(inode);
4450
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004451 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004452 if (p == NULL)
4453 return NULL;
4454
4455 p->arg.fh = NFS_FH(inode);
4456 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004457 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004458 if (p->arg.open_seqid == NULL)
4459 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004460 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004461 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004462 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004463 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004464 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004465 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004466 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004467 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004468 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004469 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004470 atomic_inc(&lsp->ls_count);
4471 p->ctx = get_nfs_open_context(ctx);
4472 memcpy(&p->fl, fl, sizeof(p->fl));
4473 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004474out_free_seqid:
4475 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004476out_free:
4477 kfree(p);
4478 return NULL;
4479}
4480
4481static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4482{
4483 struct nfs4_lockdata *data = calldata;
4484 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485
Harvey Harrison3110ff82008-05-02 13:42:44 -07004486 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004487 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4488 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004489 /* Do we need to do an open_to_lock_owner? */
4490 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004491 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebustb8480fc2012-10-29 18:37:40 -04004492 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004493 data->arg.open_stateid = &state->stateid;
4494 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004495 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004496 } else
4497 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004498 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004499 if (nfs4_setup_sequence(data->server,
4500 &data->arg.seq_args,
Trond Myklebustb8480fc2012-10-29 18:37:40 -04004501 &data->res.seq_res,
4502 task) == 0) {
4503 rpc_call_start(task);
Andy Adamson66179ef2009-04-01 09:22:22 -04004504 return;
Trond Myklebustb8480fc2012-10-29 18:37:40 -04004505 }
4506 nfs_release_seqid(data->arg.open_seqid);
4507out_release_lock_seqid:
4508 nfs_release_seqid(data->arg.lock_seqid);
4509 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004510}
4511
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004512static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4513{
4514 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4515 nfs4_lock_prepare(task, calldata);
4516}
4517
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004518static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4519{
4520 struct nfs4_lockdata *data = calldata;
4521
Harvey Harrison3110ff82008-05-02 13:42:44 -07004522 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004523
Trond Myklebust14516c32010-07-31 14:29:06 -04004524 if (!nfs4_sequence_done(task, &data->res.seq_res))
4525 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004526
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004527 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004528 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004529 if (data->rpc_status == 0)
4530 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4531 else
4532 goto out;
4533 }
4534 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004535 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004536 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004537 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004538 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004539out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004540 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004541}
4542
4543static void nfs4_lock_release(void *calldata)
4544{
4545 struct nfs4_lockdata *data = calldata;
4546
Harvey Harrison3110ff82008-05-02 13:42:44 -07004547 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004548 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004549 if (data->cancelled != 0) {
4550 struct rpc_task *task;
4551 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4552 data->arg.lock_seqid);
4553 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004554 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004555 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004556 } else
4557 nfs_free_seqid(data->arg.lock_seqid);
4558 nfs4_put_lock_state(data->lsp);
4559 put_nfs_open_context(data->ctx);
4560 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004561 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004562}
4563
4564static const struct rpc_call_ops nfs4_lock_ops = {
4565 .rpc_call_prepare = nfs4_lock_prepare,
4566 .rpc_call_done = nfs4_lock_done,
4567 .rpc_release = nfs4_lock_release,
4568};
4569
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004570static const struct rpc_call_ops nfs4_recover_lock_ops = {
4571 .rpc_call_prepare = nfs4_recover_lock_prepare,
4572 .rpc_call_done = nfs4_lock_done,
4573 .rpc_release = nfs4_lock_release,
4574};
4575
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004576static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4577{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004578 switch (error) {
4579 case -NFS4ERR_ADMIN_REVOKED:
4580 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004581 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004582 if (new_lock_owner != 0 ||
4583 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004584 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004585 break;
4586 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004587 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004588 case -NFS4ERR_EXPIRED:
4589 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004590 };
4591}
4592
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004593static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004594{
4595 struct nfs4_lockdata *data;
4596 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004597 struct rpc_message msg = {
4598 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4599 .rpc_cred = state->owner->so_cred,
4600 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004601 struct rpc_task_setup task_setup_data = {
4602 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004603 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004604 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004605 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004606 .flags = RPC_TASK_ASYNC,
4607 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004608 int ret;
4609
Harvey Harrison3110ff82008-05-02 13:42:44 -07004610 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004611 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004612 fl->fl_u.nfs4_fl.owner,
4613 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004614 if (data == NULL)
4615 return -ENOMEM;
4616 if (IS_SETLKW(cmd))
4617 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004618 if (recovery_type > NFS_LOCK_NEW) {
4619 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004620 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004621 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4622 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004623 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004624 msg.rpc_argp = &data->arg;
4625 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004626 task_setup_data.callback_data = data;
4627 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004628 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004629 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004630 ret = nfs4_wait_for_completion_rpc_task(task);
4631 if (ret == 0) {
4632 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004633 if (ret)
4634 nfs4_handle_setlk_error(data->server, data->lsp,
4635 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004636 } else
4637 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004638 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004639 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004640 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641}
4642
4643static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4644{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004645 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004646 struct nfs4_exception exception = {
4647 .inode = state->inode,
4648 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004649 int err;
4650
4651 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004652 /* Cache the lock if possible... */
4653 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4654 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004655 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004656 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004657 break;
4658 nfs4_handle_exception(server, err, &exception);
4659 } while (exception.retry);
4660 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661}
4662
4663static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4664{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004665 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004666 struct nfs4_exception exception = {
4667 .inode = state->inode,
4668 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004669 int err;
4670
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004671 err = nfs4_set_lock_state(state, request);
4672 if (err != 0)
4673 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004674 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004675 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4676 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004677 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004678 switch (err) {
4679 default:
4680 goto out;
4681 case -NFS4ERR_GRACE:
4682 case -NFS4ERR_DELAY:
4683 nfs4_handle_exception(server, err, &exception);
4684 err = 0;
4685 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004686 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004687out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004688 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689}
4690
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004691#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004692static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004693{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004694 int status, ret = NFS_OK;
4695 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004696 struct nfs_server *server = NFS_SERVER(state->inode);
4697
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004698 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4699 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4700 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4701 if (status != NFS_OK) {
4702 nfs41_free_stateid(server, &lsp->ls_stateid);
4703 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4704 ret = status;
4705 }
4706 }
4707 };
4708
4709 return ret;
4710}
4711
4712static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4713{
4714 int status = NFS_OK;
4715
4716 if (test_bit(LK_STATE_IN_USE, &state->flags))
4717 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004718 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004719 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004720 return nfs4_lock_expired(state, request);
4721}
4722#endif
4723
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4725{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004726 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004727 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004728 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004730 if ((fl_flags & FL_POSIX) &&
4731 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4732 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004733 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004734 status = nfs4_set_lock_state(state, request);
4735 if (status != 0)
4736 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004737 request->fl_flags |= FL_ACCESS;
4738 status = do_vfs_lock(request->fl_file, request);
4739 if (status < 0)
4740 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004741 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004742 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004743 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004744 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004745 request->fl_flags = fl_flags & ~FL_SLEEP;
4746 status = do_vfs_lock(request->fl_file, request);
4747 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004748 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004749 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004750 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004751 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004752 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004753 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004754 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004755 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4756 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004757out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004758 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004759out:
4760 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 return status;
4762}
4763
4764static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4765{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004766 struct nfs4_exception exception = {
4767 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004768 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004769 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 int err;
4771
4772 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004773 err = _nfs4_proc_setlk(state, cmd, request);
4774 if (err == -NFS4ERR_DENIED)
4775 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004777 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 } while (exception.retry);
4779 return err;
4780}
4781
4782static int
4783nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4784{
4785 struct nfs_open_context *ctx;
4786 struct nfs4_state *state;
4787 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4788 int status;
4789
4790 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004791 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 state = ctx->state;
4793
4794 if (request->fl_start < 0 || request->fl_end < 0)
4795 return -EINVAL;
4796
Trond Myklebustd9531262009-07-21 19:22:38 -04004797 if (IS_GETLK(cmd)) {
4798 if (state != NULL)
4799 return nfs4_proc_getlk(state, F_GETLK, request);
4800 return 0;
4801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
4803 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4804 return -EINVAL;
4805
Trond Myklebustd9531262009-07-21 19:22:38 -04004806 if (request->fl_type == F_UNLCK) {
4807 if (state != NULL)
4808 return nfs4_proc_unlck(state, cmd, request);
4809 return 0;
4810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811
Trond Myklebustd9531262009-07-21 19:22:38 -04004812 if (state == NULL)
4813 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004814 /*
4815 * Don't rely on the VFS having checked the file open mode,
4816 * since it won't do this for flock() locks.
4817 */
4818 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4819 case F_RDLCK:
4820 if (!(filp->f_mode & FMODE_READ))
4821 return -EBADF;
4822 break;
4823 case F_WRLCK:
4824 if (!(filp->f_mode & FMODE_WRITE))
4825 return -EBADF;
4826 }
4827
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 do {
4829 status = nfs4_proc_setlk(state, cmd, request);
4830 if ((status != -EAGAIN) || IS_SETLK(cmd))
4831 break;
4832 timeout = nfs4_set_lock_task_retry(timeout);
4833 status = -ERESTARTSYS;
4834 if (signalled())
4835 break;
4836 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 return status;
4838}
4839
Trond Myklebust888e6942005-11-04 15:38:11 -05004840int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4841{
4842 struct nfs_server *server = NFS_SERVER(state->inode);
4843 struct nfs4_exception exception = { };
4844 int err;
4845
4846 err = nfs4_set_lock_state(state, fl);
4847 if (err != 0)
4848 goto out;
4849 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004850 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004851 switch (err) {
4852 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004853 printk(KERN_ERR "NFS: %s: unhandled error "
4854 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004855 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004856 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004857 goto out;
4858 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004859 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004860 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004861 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004862 nfs4_schedule_lease_recovery(server->nfs_client);
4863 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004864 case -NFS4ERR_BADSESSION:
4865 case -NFS4ERR_BADSLOT:
4866 case -NFS4ERR_BAD_HIGH_SLOT:
4867 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4868 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004869 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004870 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004871 case -ERESTARTSYS:
4872 /*
4873 * The show must go on: exit, but mark the
4874 * stateid as needing recovery.
4875 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004876 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004877 case -NFS4ERR_ADMIN_REVOKED:
4878 case -NFS4ERR_BAD_STATEID:
4879 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004880 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004881 err = 0;
4882 goto out;
4883 case -ENOMEM:
4884 case -NFS4ERR_DENIED:
4885 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4886 err = 0;
4887 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004888 case -NFS4ERR_DELAY:
4889 break;
4890 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004891 err = nfs4_handle_exception(server, err, &exception);
4892 } while (exception.retry);
4893out:
4894 return err;
4895}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004896
Trond Myklebustcf470c32012-03-07 13:49:12 -05004897struct nfs_release_lockowner_data {
4898 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004899 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004900 struct nfs_release_lockowner_args args;
4901};
4902
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004903static void nfs4_release_lockowner_release(void *calldata)
4904{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004905 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004906 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004907 kfree(calldata);
4908}
4909
Trond Myklebust17280172012-03-11 13:11:00 -04004910static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004911 .rpc_release = nfs4_release_lockowner_release,
4912};
4913
Trond Myklebustcf470c32012-03-07 13:49:12 -05004914int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004915{
4916 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004917 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004918 struct rpc_message msg = {
4919 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4920 };
4921
4922 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004923 return -EINVAL;
4924 data = kmalloc(sizeof(*data), GFP_NOFS);
4925 if (!data)
4926 return -ENOMEM;
4927 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004928 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004929 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4930 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4931 data->args.lock_owner.s_dev = server->s_dev;
4932 msg.rpc_argp = &data->args;
4933 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4934 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004935}
4936
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004937#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4938
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004939static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4940 const void *buf, size_t buflen,
4941 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004942{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004943 if (strcmp(key, "") != 0)
4944 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004945
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004946 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004947}
4948
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004949static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4950 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004951{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004952 if (strcmp(key, "") != 0)
4953 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004954
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004955 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004956}
4957
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004958static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4959 size_t list_len, const char *name,
4960 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004961{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004962 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004963
J. Bruce Fields096455a2006-03-20 23:23:42 -05004964 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4965 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004966
4967 if (list && len <= list_len)
4968 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004969 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004970}
4971
Andy Adamson533eb462011-06-13 18:25:56 -04004972/*
4973 * nfs_fhget will use either the mounted_on_fileid or the fileid
4974 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004975static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4976{
Andy Adamson533eb462011-06-13 18:25:56 -04004977 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4978 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4979 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05004980 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004981 return;
4982
4983 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05004984 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004985 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4986 fattr->nlink = 2;
4987}
4988
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004989static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
4990 const struct qstr *name,
4991 struct nfs4_fs_locations *fs_locations,
4992 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004993{
4994 struct nfs_server *server = NFS_SERVER(dir);
4995 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004996 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004997 };
4998 struct nfs4_fs_locations_arg args = {
4999 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005000 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005001 .page = page,
5002 .bitmask = bitmask,
5003 };
Benny Halevy22958462009-04-01 09:22:02 -04005004 struct nfs4_fs_locations_res res = {
5005 .fs_locations = fs_locations,
5006 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005007 struct rpc_message msg = {
5008 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5009 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005010 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005011 };
5012 int status;
5013
Harvey Harrison3110ff82008-05-02 13:42:44 -07005014 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005015
5016 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5017 * is not supported */
5018 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5019 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5020 else
5021 bitmask[0] |= FATTR4_WORD0_FILEID;
5022
Trond Myklebustc228fd32007-01-13 02:28:11 -05005023 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005024 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005025 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005026 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005027 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005028 return status;
5029}
5030
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005031int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5032 const struct qstr *name,
5033 struct nfs4_fs_locations *fs_locations,
5034 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005035{
5036 struct nfs4_exception exception = { };
5037 int err;
5038 do {
5039 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005040 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005041 &exception);
5042 } while (exception.retry);
5043 return err;
5044}
5045
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005046static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5047{
5048 int status;
5049 struct nfs4_secinfo_arg args = {
5050 .dir_fh = NFS_FH(dir),
5051 .name = name,
5052 };
5053 struct nfs4_secinfo_res res = {
5054 .flavors = flavors,
5055 };
5056 struct rpc_message msg = {
5057 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5058 .rpc_argp = &args,
5059 .rpc_resp = &res,
5060 };
5061
5062 dprintk("NFS call secinfo %s\n", name->name);
5063 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5064 dprintk("NFS reply secinfo: %d\n", status);
5065 return status;
5066}
5067
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005068int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5069 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005070{
5071 struct nfs4_exception exception = { };
5072 int err;
5073 do {
5074 err = nfs4_handle_exception(NFS_SERVER(dir),
5075 _nfs4_proc_secinfo(dir, name, flavors),
5076 &exception);
5077 } while (exception.retry);
5078 return err;
5079}
5080
Andy Adamson557134a2009-04-01 09:21:53 -04005081#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005082/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005083 * Check the exchange flags returned by the server for invalid flags, having
5084 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5085 * DS flags set.
5086 */
5087static int nfs4_check_cl_exchange_flags(u32 flags)
5088{
5089 if (flags & ~EXCHGID4_FLAG_MASK_R)
5090 goto out_inval;
5091 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5092 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5093 goto out_inval;
5094 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5095 goto out_inval;
5096 return NFS_OK;
5097out_inval:
5098 return -NFS4ERR_INVAL;
5099}
5100
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005101static bool
5102nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
5103{
5104 if (a->server_scope_sz == b->server_scope_sz &&
5105 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5106 return true;
5107
5108 return false;
5109}
5110
Andy Adamson357f54d2010-12-14 10:11:57 -05005111/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005112 * nfs4_proc_exchange_id()
5113 *
5114 * Since the clientid has expired, all compounds using sessions
5115 * associated with the stale clientid will be returning
5116 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5117 * be in some phase of session reset.
5118 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005119int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005120{
5121 nfs4_verifier verifier;
5122 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005123 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005124 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005125 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005126 };
5127 struct nfs41_exchange_id_res res = {
5128 .client = clp,
5129 };
5130 int status;
5131 struct rpc_message msg = {
5132 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5133 .rpc_argp = &args,
5134 .rpc_resp = &res,
5135 .rpc_cred = cred,
5136 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005137
5138 dprintk("--> %s\n", __func__);
5139 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04005140
Chuck Levercd937102012-03-02 17:14:31 -05005141 nfs4_construct_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005142
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005143 args.id_len = scnprintf(args.id, sizeof(args.id),
Trond Myklebust3617e502012-04-30 12:04:58 -04005144 "%s/%s/%u",
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005145 clp->cl_ipaddr,
Trond Myklebust3617e502012-04-30 12:04:58 -04005146 clp->cl_rpcclient->cl_nodename,
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005147 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005148
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005149 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005150 if (unlikely(!res.server_scope)) {
5151 status = -ENOMEM;
5152 goto out;
5153 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005154
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005155 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
5156 if (unlikely(!res.impl_id)) {
5157 status = -ENOMEM;
5158 goto out_server_scope;
5159 }
5160
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005161 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005162 if (!status)
5163 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005164
5165 if (!status) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005166 /* use the most recent implementation id */
5167 kfree(clp->impl_id);
5168 clp->impl_id = res.impl_id;
5169 } else
5170 kfree(res.impl_id);
5171
5172 if (!status) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005173 if (clp->server_scope &&
5174 !nfs41_same_server_scope(clp->server_scope,
5175 res.server_scope)) {
5176 dprintk("%s: server_scope mismatch detected\n",
5177 __func__);
5178 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5179 kfree(clp->server_scope);
5180 clp->server_scope = NULL;
5181 }
5182
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005183 if (!clp->server_scope) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005184 clp->server_scope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005185 goto out;
5186 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005187 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005188
5189out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005190 kfree(res.server_scope);
5191out:
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005192 if (clp->impl_id)
5193 dprintk("%s: Server Implementation ID: "
5194 "domain: %s, name: %s, date: %llu,%u\n",
5195 __func__, clp->impl_id->domain, clp->impl_id->name,
5196 clp->impl_id->date.seconds,
5197 clp->impl_id->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005198 dprintk("<-- %s status= %d\n", __func__, status);
5199 return status;
5200}
5201
Andy Adamson2050f0c2009-04-01 09:22:30 -04005202struct nfs4_get_lease_time_data {
5203 struct nfs4_get_lease_time_args *args;
5204 struct nfs4_get_lease_time_res *res;
5205 struct nfs_client *clp;
5206};
5207
5208static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5209 void *calldata)
5210{
5211 int ret;
5212 struct nfs4_get_lease_time_data *data =
5213 (struct nfs4_get_lease_time_data *)calldata;
5214
5215 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005216 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005217 /* just setup sequence, do not trigger session recovery
5218 since we're invoked within one */
5219 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005220 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005221 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005222
5223 BUG_ON(ret == -EAGAIN);
5224 rpc_call_start(task);
5225 dprintk("<-- %s\n", __func__);
5226}
5227
5228/*
5229 * Called from nfs4_state_manager thread for session setup, so don't recover
5230 * from sequence operation or clientid errors.
5231 */
5232static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5233{
5234 struct nfs4_get_lease_time_data *data =
5235 (struct nfs4_get_lease_time_data *)calldata;
5236
5237 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005238 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5239 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005240 switch (task->tk_status) {
5241 case -NFS4ERR_DELAY:
5242 case -NFS4ERR_GRACE:
5243 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5244 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5245 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005246 /* fall through */
5247 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005248 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005249 return;
5250 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005251 dprintk("<-- %s\n", __func__);
5252}
5253
Trond Myklebust17280172012-03-11 13:11:00 -04005254static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005255 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5256 .rpc_call_done = nfs4_get_lease_time_done,
5257};
5258
5259int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5260{
5261 struct rpc_task *task;
5262 struct nfs4_get_lease_time_args args;
5263 struct nfs4_get_lease_time_res res = {
5264 .lr_fsinfo = fsinfo,
5265 };
5266 struct nfs4_get_lease_time_data data = {
5267 .args = &args,
5268 .res = &res,
5269 .clp = clp,
5270 };
5271 struct rpc_message msg = {
5272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5273 .rpc_argp = &args,
5274 .rpc_resp = &res,
5275 };
5276 struct rpc_task_setup task_setup = {
5277 .rpc_client = clp->cl_rpcclient,
5278 .rpc_message = &msg,
5279 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005280 .callback_data = &data,
5281 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005282 };
5283 int status;
5284
Trond Myklebust9d12b212012-01-17 22:04:25 -05005285 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005286 dprintk("--> %s\n", __func__);
5287 task = rpc_run_task(&task_setup);
5288
5289 if (IS_ERR(task))
5290 status = PTR_ERR(task);
5291 else {
5292 status = task->tk_status;
5293 rpc_put_task(task);
5294 }
5295 dprintk("<-- %s return %d\n", __func__, status);
5296
5297 return status;
5298}
5299
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005300static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5301{
5302 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5303}
5304
5305static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5306 struct nfs4_slot *new,
5307 u32 max_slots,
5308 u32 ivalue)
5309{
5310 struct nfs4_slot *old = NULL;
5311 u32 i;
5312
5313 spin_lock(&tbl->slot_tbl_lock);
5314 if (new) {
5315 old = tbl->slots;
5316 tbl->slots = new;
5317 tbl->max_slots = max_slots;
5318 }
5319 tbl->highest_used_slotid = -1; /* no slot is currently used */
5320 for (i = 0; i < tbl->max_slots; i++)
5321 tbl->slots[i].seq_nr = ivalue;
5322 spin_unlock(&tbl->slot_tbl_lock);
5323 kfree(old);
5324}
5325
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005326/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005327 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005328 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005329static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5330 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005331{
Andy Adamson104aeba2010-01-14 17:45:10 -05005332 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005333 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005334
Andy Adamson104aeba2010-01-14 17:45:10 -05005335 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5336 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005337
Andy Adamson104aeba2010-01-14 17:45:10 -05005338 /* Does the newly negotiated max_reqs match the existing slot table? */
5339 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005340 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005341 if (!new)
5342 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005343 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005344 ret = 0;
5345
5346 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005347 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5348 tbl, tbl->slots, tbl->max_slots);
5349out:
5350 dprintk("<-- %s: return %d\n", __func__, ret);
5351 return ret;
5352}
5353
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005354/* Destroy the slot table */
5355static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5356{
5357 if (session->fc_slot_table.slots != NULL) {
5358 kfree(session->fc_slot_table.slots);
5359 session->fc_slot_table.slots = NULL;
5360 }
5361 if (session->bc_slot_table.slots != NULL) {
5362 kfree(session->bc_slot_table.slots);
5363 session->bc_slot_table.slots = NULL;
5364 }
5365 return;
5366}
5367
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005368/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005369 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005370 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005371static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005372{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005373 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005374 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005375
Andy Adamsonaacd5532011-11-09 13:58:21 -05005376 dprintk("--> %s\n", __func__);
5377 /* Fore channel */
5378 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005379 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5380 if (status) /* -ENOMEM */
5381 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005382 /* Back channel */
5383 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005384 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5385 if (status && tbl->slots == NULL)
5386 /* Fore and back channel share a connection so get
5387 * both slot tables or neither */
5388 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005389 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005390}
5391
5392struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5393{
5394 struct nfs4_session *session;
5395 struct nfs4_slot_table *tbl;
5396
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005397 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005398 if (!session)
5399 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005400
Andy Adamson557134a2009-04-01 09:21:53 -04005401 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005402 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005403 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005404 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005405 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005406
5407 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005408 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005409 spin_lock_init(&tbl->slot_tbl_lock);
5410 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005411 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005412
Trond Myklebust1055d762010-06-16 09:52:27 -04005413 session->session_state = 1<<NFS4_SESSION_INITING;
5414
Andy Adamson557134a2009-04-01 09:21:53 -04005415 session->clp = clp;
5416 return session;
5417}
5418
5419void nfs4_destroy_session(struct nfs4_session *session)
5420{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005421 struct rpc_xprt *xprt;
5422
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005423 nfs4_proc_destroy_session(session);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005424
5425 rcu_read_lock();
5426 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5427 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005428 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005429 __func__, xprt);
5430 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005431 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005432 kfree(session);
5433}
5434
Andy Adamsonfc931582009-04-01 09:22:31 -04005435/*
5436 * Initialize the values to be used by the client in CREATE_SESSION
5437 * If nfs4_init_session set the fore channel request and response sizes,
5438 * use them.
5439 *
5440 * Set the back channel max_resp_sz_cached to zero to force the client to
5441 * always set csa_cachethis to FALSE because the current implementation
5442 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5443 */
5444static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5445{
5446 struct nfs4_session *session = args->client->cl_session;
5447 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5448 mxresp_sz = session->fc_attrs.max_resp_sz;
5449
5450 if (mxrqst_sz == 0)
5451 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5452 if (mxresp_sz == 0)
5453 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5454 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005455 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5456 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005457 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005458 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005459
5460 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005461 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005462 __func__,
5463 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005464 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005465
5466 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005467 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5468 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5469 args->bc_attrs.max_resp_sz_cached = 0;
5470 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5471 args->bc_attrs.max_reqs = 1;
5472
5473 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5474 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5475 __func__,
5476 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5477 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5478 args->bc_attrs.max_reqs);
5479}
5480
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005481static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005482{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005483 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5484 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5485
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005486 if (rcvd->max_resp_sz > sent->max_resp_sz)
5487 return -EINVAL;
5488 /*
5489 * Our requested max_ops is the minimum we need; we're not
5490 * prepared to break up compounds into smaller pieces than that.
5491 * So, no point even trying to continue if the server won't
5492 * cooperate:
5493 */
5494 if (rcvd->max_ops < sent->max_ops)
5495 return -EINVAL;
5496 if (rcvd->max_reqs == 0)
5497 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005498 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5499 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005500 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005501}
5502
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005503static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5504{
5505 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5506 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005507
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005508 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5509 return -EINVAL;
5510 if (rcvd->max_resp_sz < sent->max_resp_sz)
5511 return -EINVAL;
5512 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5513 return -EINVAL;
5514 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005515 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005516 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005517 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005518 return -EINVAL;
5519 return 0;
5520}
Andy Adamson8d353012009-04-01 09:22:32 -04005521
Andy Adamson8d353012009-04-01 09:22:32 -04005522static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5523 struct nfs4_session *session)
5524{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005525 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005526
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005527 ret = nfs4_verify_fore_channel_attrs(args, session);
5528 if (ret)
5529 return ret;
5530 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005531}
5532
Andy Adamsonfc931582009-04-01 09:22:31 -04005533static int _nfs4_proc_create_session(struct nfs_client *clp)
5534{
5535 struct nfs4_session *session = clp->cl_session;
5536 struct nfs41_create_session_args args = {
5537 .client = clp,
5538 .cb_program = NFS4_CALLBACK,
5539 };
5540 struct nfs41_create_session_res res = {
5541 .client = clp,
5542 };
5543 struct rpc_message msg = {
5544 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5545 .rpc_argp = &args,
5546 .rpc_resp = &res,
5547 };
5548 int status;
5549
5550 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005551 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005552
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005553 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005554
Andy Adamson8d353012009-04-01 09:22:32 -04005555 if (!status)
5556 /* Verify the session's negotiated channel_attrs values */
5557 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005558 if (!status) {
5559 /* Increment the clientid slot sequence id */
5560 clp->cl_seqid++;
5561 }
5562
5563 return status;
5564}
5565
5566/*
5567 * Issues a CREATE_SESSION operation to the server.
5568 * It is the responsibility of the caller to verify the session is
5569 * expired before calling this routine.
5570 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005571int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005572{
5573 int status;
5574 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005575 struct nfs4_session *session = clp->cl_session;
5576
5577 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5578
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005579 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005580 if (status)
5581 goto out;
5582
Andy Adamsonaacd5532011-11-09 13:58:21 -05005583 /* Init or reset the session slot tables */
5584 status = nfs4_setup_session_slot_tables(session);
5585 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005586 if (status)
5587 goto out;
5588
5589 ptr = (unsigned *)&session->sess_id.data[0];
5590 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5591 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005592out:
5593 dprintk("<-- %s\n", __func__);
5594 return status;
5595}
5596
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005597/*
5598 * Issue the over-the-wire RPC DESTROY_SESSION.
5599 * The caller must serialize access to this routine.
5600 */
5601int nfs4_proc_destroy_session(struct nfs4_session *session)
5602{
5603 int status = 0;
5604 struct rpc_message msg;
5605
5606 dprintk("--> nfs4_proc_destroy_session\n");
5607
5608 /* session is still being setup */
5609 if (session->clp->cl_cons_state != NFS_CS_READY)
5610 return status;
5611
5612 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5613 msg.rpc_argp = session;
5614 msg.rpc_resp = NULL;
5615 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005616 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005617
5618 if (status)
5619 printk(KERN_WARNING
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -05005620 "NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005621 "Session has been destroyed regardless...\n", status);
5622
5623 dprintk("<-- nfs4_proc_destroy_session\n");
5624 return status;
5625}
5626
Trond Myklebustfccba802009-07-21 16:48:07 -04005627int nfs4_init_session(struct nfs_server *server)
5628{
5629 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005630 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005631 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005632 int ret;
5633
5634 if (!nfs4_has_session(clp))
5635 return 0;
5636
Trond Myklebust1055d762010-06-16 09:52:27 -04005637 session = clp->cl_session;
5638 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5639 return 0;
5640
Andy Adamson68bf05e2009-12-15 12:55:02 -05005641 rsize = server->rsize;
5642 if (rsize == 0)
5643 rsize = NFS_MAX_FILE_IO_SIZE;
5644 wsize = server->wsize;
5645 if (wsize == 0)
5646 wsize = NFS_MAX_FILE_IO_SIZE;
5647
Andy Adamson68bf05e2009-12-15 12:55:02 -05005648 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5649 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005650
Trond Myklebustfccba802009-07-21 16:48:07 -04005651 ret = nfs4_recover_expired_lease(server);
5652 if (!ret)
5653 ret = nfs4_check_client_ready(clp);
5654 return ret;
5655}
5656
Andy Adamsond83217c2011-03-01 01:34:17 +00005657int nfs4_init_ds_session(struct nfs_client *clp)
5658{
5659 struct nfs4_session *session = clp->cl_session;
5660 int ret;
5661
5662 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5663 return 0;
5664
5665 ret = nfs4_client_recover_expired_lease(clp);
5666 if (!ret)
5667 /* Test for the DS role */
5668 if (!is_ds_client(clp))
5669 ret = -ENODEV;
5670 if (!ret)
5671 ret = nfs4_check_client_ready(clp);
5672 return ret;
5673
5674}
5675EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5676
5677
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005678/*
5679 * Renew the cl_session lease.
5680 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005681struct nfs4_sequence_data {
5682 struct nfs_client *clp;
5683 struct nfs4_sequence_args args;
5684 struct nfs4_sequence_res res;
5685};
5686
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005687static void nfs41_sequence_release(void *data)
5688{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005689 struct nfs4_sequence_data *calldata = data;
5690 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005691
Alexandros Batsakis71358402010-02-05 03:45:05 -08005692 if (atomic_read(&clp->cl_count) > 1)
5693 nfs4_schedule_state_renewal(clp);
5694 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005695 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005696}
5697
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005698static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5699{
5700 switch(task->tk_status) {
5701 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005702 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5703 return -EAGAIN;
5704 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005705 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005706 }
5707 return 0;
5708}
5709
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005710static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005711{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005712 struct nfs4_sequence_data *calldata = data;
5713 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005714
Trond Myklebust14516c32010-07-31 14:29:06 -04005715 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5716 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005717
5718 if (task->tk_status < 0) {
5719 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005720 if (atomic_read(&clp->cl_count) == 1)
5721 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005722
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005723 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5724 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005725 return;
5726 }
5727 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005728 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005729out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005730 dprintk("<-- %s\n", __func__);
5731}
5732
5733static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5734{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005735 struct nfs4_sequence_data *calldata = data;
5736 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005737 struct nfs4_sequence_args *args;
5738 struct nfs4_sequence_res *res;
5739
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005740 args = task->tk_msg.rpc_argp;
5741 res = task->tk_msg.rpc_resp;
5742
Trond Myklebust9d12b212012-01-17 22:04:25 -05005743 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005744 return;
5745 rpc_call_start(task);
5746}
5747
Bryan Schumaker968618d2012-11-12 16:55:38 -05005748static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
5749{
5750 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5751 nfs41_sequence_prepare(task, data);
5752}
5753
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005754static const struct rpc_call_ops nfs41_sequence_ops = {
5755 .rpc_call_done = nfs41_sequence_call_done,
5756 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005757 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005758};
5759
Bryan Schumaker968618d2012-11-12 16:55:38 -05005760static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
5761 .rpc_call_done = nfs41_sequence_call_done,
5762 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
5763 .rpc_release = nfs41_sequence_release,
5764};
5765
5766static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
5767 const struct rpc_call_ops *seq_ops)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005768{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005769 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005770 struct rpc_message msg = {
5771 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5772 .rpc_cred = cred,
5773 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005774 struct rpc_task_setup task_setup_data = {
5775 .rpc_client = clp->cl_rpcclient,
5776 .rpc_message = &msg,
Bryan Schumaker968618d2012-11-12 16:55:38 -05005777 .callback_ops = seq_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005778 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5779 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005780
Alexandros Batsakis71358402010-02-05 03:45:05 -08005781 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005782 return ERR_PTR(-EIO);
Benny Halevydfb4f302010-09-24 09:17:01 -04005783 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005784 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005785 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005786 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005787 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005788 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005789 msg.rpc_argp = &calldata->args;
5790 msg.rpc_resp = &calldata->res;
5791 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005792 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005793
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005794 return rpc_run_task(&task_setup_data);
5795}
5796
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005797static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005798{
5799 struct rpc_task *task;
5800 int ret = 0;
5801
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005802 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamson7c6003e2014-09-29 12:31:57 -04005803 return -EAGAIN;
Bryan Schumaker968618d2012-11-12 16:55:38 -05005804 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005805 if (IS_ERR(task))
5806 ret = PTR_ERR(task);
5807 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005808 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005809 dprintk("<-- %s status=%d\n", __func__, ret);
5810 return ret;
5811}
5812
5813static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5814{
5815 struct rpc_task *task;
5816 int ret;
5817
Bryan Schumaker968618d2012-11-12 16:55:38 -05005818 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005819 if (IS_ERR(task)) {
5820 ret = PTR_ERR(task);
5821 goto out;
5822 }
5823 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005824 if (!ret) {
5825 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5826
5827 if (task->tk_status == 0)
5828 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005829 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005830 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005831 rpc_put_task(task);
5832out:
5833 dprintk("<-- %s status=%d\n", __func__, ret);
5834 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005835}
5836
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005837struct nfs4_reclaim_complete_data {
5838 struct nfs_client *clp;
5839 struct nfs41_reclaim_complete_args arg;
5840 struct nfs41_reclaim_complete_res res;
5841};
5842
5843static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5844{
5845 struct nfs4_reclaim_complete_data *calldata = data;
5846
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005847 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005848 if (nfs41_setup_sequence(calldata->clp->cl_session,
5849 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005850 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005851 return;
5852
5853 rpc_call_start(task);
5854}
5855
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005856static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5857{
5858 switch(task->tk_status) {
5859 case 0:
5860 case -NFS4ERR_COMPLETE_ALREADY:
5861 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5862 break;
5863 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005864 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005865 /* fall through */
5866 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005867 return -EAGAIN;
5868 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005869 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005870 }
5871 return 0;
5872}
5873
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005874static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5875{
5876 struct nfs4_reclaim_complete_data *calldata = data;
5877 struct nfs_client *clp = calldata->clp;
5878 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5879
5880 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005881 if (!nfs41_sequence_done(task, res))
5882 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005883
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005884 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5885 rpc_restart_call_prepare(task);
5886 return;
5887 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005888 dprintk("<-- %s\n", __func__);
5889}
5890
5891static void nfs4_free_reclaim_complete_data(void *data)
5892{
5893 struct nfs4_reclaim_complete_data *calldata = data;
5894
5895 kfree(calldata);
5896}
5897
5898static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5899 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5900 .rpc_call_done = nfs4_reclaim_complete_done,
5901 .rpc_release = nfs4_free_reclaim_complete_data,
5902};
5903
5904/*
5905 * Issue a global reclaim complete.
5906 */
5907static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5908{
5909 struct nfs4_reclaim_complete_data *calldata;
5910 struct rpc_task *task;
5911 struct rpc_message msg = {
5912 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5913 };
5914 struct rpc_task_setup task_setup_data = {
5915 .rpc_client = clp->cl_rpcclient,
5916 .rpc_message = &msg,
5917 .callback_ops = &nfs4_reclaim_complete_call_ops,
5918 .flags = RPC_TASK_ASYNC,
5919 };
5920 int status = -ENOMEM;
5921
5922 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005923 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005924 if (calldata == NULL)
5925 goto out;
5926 calldata->clp = clp;
5927 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005928
Trond Myklebust9d12b212012-01-17 22:04:25 -05005929 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005930 msg.rpc_argp = &calldata->arg;
5931 msg.rpc_resp = &calldata->res;
5932 task_setup_data.callback_data = calldata;
5933 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005934 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005935 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005936 goto out;
5937 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005938 status = nfs4_wait_for_completion_rpc_task(task);
5939 if (status == 0)
5940 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005941 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005942 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005943out:
5944 dprintk("<-- %s status=%d\n", __func__, status);
5945 return status;
5946}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005947
5948static void
5949nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5950{
5951 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005952 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005953
5954 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005955 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5956 * right now covering the LAYOUTGET we are about to send.
5957 * However, that is not so catastrophic, and there seems
5958 * to be no way to prevent it completely.
5959 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005960 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005961 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005962 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005963 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5964 NFS_I(lgp->args.inode)->layout,
5965 lgp->args.ctx->state)) {
5966 rpc_exit(task, NFS4_OK);
5967 return;
5968 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005969 rpc_call_start(task);
5970}
5971
5972static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5973{
5974 struct nfs4_layoutget *lgp = calldata;
5975 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5976
5977 dprintk("--> %s\n", __func__);
5978
5979 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5980 return;
5981
5982 switch (task->tk_status) {
5983 case 0:
5984 break;
5985 case -NFS4ERR_LAYOUTTRYLATER:
5986 case -NFS4ERR_RECALLCONFLICT:
5987 task->tk_status = -NFS4ERR_DELAY;
5988 /* Fall through */
5989 default:
5990 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5991 rpc_restart_call_prepare(task);
5992 return;
5993 }
5994 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005995 dprintk("<-- %s\n", __func__);
5996}
5997
Idan Kedar93085842012-08-02 11:47:10 +03005998static size_t max_response_pages(struct nfs_server *server)
5999{
6000 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6001 return nfs_page_array_len(0, max_resp_sz);
6002}
6003
6004static void nfs4_free_pages(struct page **pages, size_t size)
6005{
6006 int i;
6007
6008 if (!pages)
6009 return;
6010
6011 for (i = 0; i < size; i++) {
6012 if (!pages[i])
6013 break;
6014 __free_page(pages[i]);
6015 }
6016 kfree(pages);
6017}
6018
6019static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6020{
6021 struct page **pages;
6022 int i;
6023
6024 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6025 if (!pages) {
6026 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6027 return NULL;
6028 }
6029
6030 for (i = 0; i < size; i++) {
6031 pages[i] = alloc_page(gfp_flags);
6032 if (!pages[i]) {
6033 dprintk("%s: failed to allocate page\n", __func__);
6034 nfs4_free_pages(pages, size);
6035 return NULL;
6036 }
6037 }
6038
6039 return pages;
6040}
6041
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006042static void nfs4_layoutget_release(void *calldata)
6043{
6044 struct nfs4_layoutget *lgp = calldata;
Idan Kedar93085842012-08-02 11:47:10 +03006045 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6046 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006047
6048 dprintk("--> %s\n", __func__);
Idan Kedar93085842012-08-02 11:47:10 +03006049 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006050 put_nfs_open_context(lgp->args.ctx);
6051 kfree(calldata);
6052 dprintk("<-- %s\n", __func__);
6053}
6054
6055static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6056 .rpc_call_prepare = nfs4_layoutget_prepare,
6057 .rpc_call_done = nfs4_layoutget_done,
6058 .rpc_release = nfs4_layoutget_release,
6059};
6060
Idan Kedar93085842012-08-02 11:47:10 +03006061int nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006062{
6063 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedar93085842012-08-02 11:47:10 +03006064 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006065 struct rpc_task *task;
6066 struct rpc_message msg = {
6067 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6068 .rpc_argp = &lgp->args,
6069 .rpc_resp = &lgp->res,
6070 };
6071 struct rpc_task_setup task_setup_data = {
6072 .rpc_client = server->client,
6073 .rpc_message = &msg,
6074 .callback_ops = &nfs4_layoutget_call_ops,
6075 .callback_data = lgp,
6076 .flags = RPC_TASK_ASYNC,
6077 };
6078 int status = 0;
6079
6080 dprintk("--> %s\n", __func__);
6081
Idan Kedar93085842012-08-02 11:47:10 +03006082 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6083 if (!lgp->args.layout.pages) {
6084 nfs4_layoutget_release(lgp);
6085 return -ENOMEM;
6086 }
6087 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6088
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006089 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006090 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006091 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006092 task = rpc_run_task(&task_setup_data);
6093 if (IS_ERR(task))
6094 return PTR_ERR(task);
6095 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006096 if (status == 0)
6097 status = task->tk_status;
Weston Andros Adamson644a9ff2013-02-15 16:03:46 -05006098 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6099 if (status == 0 && lgp->res.layoutp->len)
Fred Isamanc31663d2011-01-06 11:36:24 +00006100 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006101 rpc_put_task(task);
6102 dprintk("<-- %s status=%d\n", __func__, status);
6103 return status;
6104}
6105
Benny Halevycbe82602011-05-22 19:52:37 +03006106static void
6107nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6108{
6109 struct nfs4_layoutreturn *lrp = calldata;
6110
6111 dprintk("--> %s\n", __func__);
6112 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006113 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006114 return;
6115 rpc_call_start(task);
6116}
6117
6118static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6119{
6120 struct nfs4_layoutreturn *lrp = calldata;
6121 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006122 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006123
6124 dprintk("--> %s\n", __func__);
6125
6126 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6127 return;
6128
6129 server = NFS_SERVER(lrp->args.inode);
6130 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006131 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006132 return;
6133 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006134 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust27e16c72012-08-08 16:03:13 -04006135 if (task->tk_status == 0 && lrp->res.lrs_present)
6136 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006137 lo->plh_block_lgets--;
6138 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006139 dprintk("<-- %s\n", __func__);
6140}
6141
6142static void nfs4_layoutreturn_release(void *calldata)
6143{
6144 struct nfs4_layoutreturn *lrp = calldata;
6145
6146 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006147 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006148 kfree(calldata);
6149 dprintk("<-- %s\n", __func__);
6150}
6151
6152static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6153 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6154 .rpc_call_done = nfs4_layoutreturn_done,
6155 .rpc_release = nfs4_layoutreturn_release,
6156};
6157
6158int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6159{
6160 struct rpc_task *task;
6161 struct rpc_message msg = {
6162 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6163 .rpc_argp = &lrp->args,
6164 .rpc_resp = &lrp->res,
6165 };
6166 struct rpc_task_setup task_setup_data = {
6167 .rpc_client = lrp->clp->cl_rpcclient,
6168 .rpc_message = &msg,
6169 .callback_ops = &nfs4_layoutreturn_call_ops,
6170 .callback_data = lrp,
6171 };
6172 int status;
6173
6174 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006175 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006176 task = rpc_run_task(&task_setup_data);
6177 if (IS_ERR(task))
6178 return PTR_ERR(task);
6179 status = task->tk_status;
6180 dprintk("<-- %s status=%d\n", __func__, status);
6181 rpc_put_task(task);
6182 return status;
6183}
6184
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006185/*
6186 * Retrieve the list of Data Server devices from the MDS.
6187 */
6188static int _nfs4_getdevicelist(struct nfs_server *server,
6189 const struct nfs_fh *fh,
6190 struct pnfs_devicelist *devlist)
6191{
6192 struct nfs4_getdevicelist_args args = {
6193 .fh = fh,
6194 .layoutclass = server->pnfs_curr_ld->id,
6195 };
6196 struct nfs4_getdevicelist_res res = {
6197 .devlist = devlist,
6198 };
6199 struct rpc_message msg = {
6200 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6201 .rpc_argp = &args,
6202 .rpc_resp = &res,
6203 };
6204 int status;
6205
6206 dprintk("--> %s\n", __func__);
6207 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6208 &res.seq_res, 0);
6209 dprintk("<-- %s status=%d\n", __func__, status);
6210 return status;
6211}
6212
6213int nfs4_proc_getdevicelist(struct nfs_server *server,
6214 const struct nfs_fh *fh,
6215 struct pnfs_devicelist *devlist)
6216{
6217 struct nfs4_exception exception = { };
6218 int err;
6219
6220 do {
6221 err = nfs4_handle_exception(server,
6222 _nfs4_getdevicelist(server, fh, devlist),
6223 &exception);
6224 } while (exception.retry);
6225
6226 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6227 err, devlist->num_devs);
6228
6229 return err;
6230}
6231EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6232
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006233static int
6234_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6235{
6236 struct nfs4_getdeviceinfo_args args = {
6237 .pdev = pdev,
6238 };
6239 struct nfs4_getdeviceinfo_res res = {
6240 .pdev = pdev,
6241 };
6242 struct rpc_message msg = {
6243 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6244 .rpc_argp = &args,
6245 .rpc_resp = &res,
6246 };
6247 int status;
6248
6249 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006250 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006251 dprintk("<-- %s status=%d\n", __func__, status);
6252
6253 return status;
6254}
6255
6256int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6257{
6258 struct nfs4_exception exception = { };
6259 int err;
6260
6261 do {
6262 err = nfs4_handle_exception(server,
6263 _nfs4_proc_getdeviceinfo(server, pdev),
6264 &exception);
6265 } while (exception.retry);
6266 return err;
6267}
6268EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6269
Andy Adamson863a3c62011-03-23 13:27:54 +00006270static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6271{
6272 struct nfs4_layoutcommit_data *data = calldata;
6273 struct nfs_server *server = NFS_SERVER(data->args.inode);
6274
6275 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006276 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006277 return;
6278 rpc_call_start(task);
6279}
6280
6281static void
6282nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6283{
6284 struct nfs4_layoutcommit_data *data = calldata;
6285 struct nfs_server *server = NFS_SERVER(data->args.inode);
6286
6287 if (!nfs4_sequence_done(task, &data->res.seq_res))
6288 return;
6289
6290 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006291 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6292 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6293 case -NFS4ERR_BADLAYOUT: /* no layout */
6294 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006295 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006296 break;
6297 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006298 nfs_post_op_update_inode_force_wcc(data->args.inode,
6299 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006300 break;
6301 default:
6302 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6303 rpc_restart_call_prepare(task);
6304 return;
6305 }
6306 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006307}
6308
6309static void nfs4_layoutcommit_release(void *calldata)
6310{
6311 struct nfs4_layoutcommit_data *data = calldata;
6312
Andy Adamsondb29c082011-07-30 20:52:38 -04006313 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006314 put_rpccred(data->cred);
6315 kfree(data);
6316}
6317
6318static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6319 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6320 .rpc_call_done = nfs4_layoutcommit_done,
6321 .rpc_release = nfs4_layoutcommit_release,
6322};
6323
6324int
Andy Adamsonef311532011-03-12 02:58:10 -05006325nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006326{
6327 struct rpc_message msg = {
6328 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6329 .rpc_argp = &data->args,
6330 .rpc_resp = &data->res,
6331 .rpc_cred = data->cred,
6332 };
6333 struct rpc_task_setup task_setup_data = {
6334 .task = &data->task,
6335 .rpc_client = NFS_CLIENT(data->args.inode),
6336 .rpc_message = &msg,
6337 .callback_ops = &nfs4_layoutcommit_ops,
6338 .callback_data = data,
6339 .flags = RPC_TASK_ASYNC,
6340 };
6341 struct rpc_task *task;
6342 int status = 0;
6343
6344 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6345 "lbw: %llu inode %lu\n",
6346 data->task.tk_pid, sync,
6347 data->args.lastbytewritten,
6348 data->args.inode->i_ino);
6349
Trond Myklebust9d12b212012-01-17 22:04:25 -05006350 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006351 task = rpc_run_task(&task_setup_data);
6352 if (IS_ERR(task))
6353 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006354 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006355 goto out;
6356 status = nfs4_wait_for_completion_rpc_task(task);
6357 if (status != 0)
6358 goto out;
6359 status = task->tk_status;
6360out:
6361 dprintk("%s: status %d\n", __func__, status);
6362 rpc_put_task(task);
6363 return status;
6364}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006365
6366static int
6367_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6368 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6369{
6370 struct nfs41_secinfo_no_name_args args = {
6371 .style = SECINFO_STYLE_CURRENT_FH,
6372 };
6373 struct nfs4_secinfo_res res = {
6374 .flavors = flavors,
6375 };
6376 struct rpc_message msg = {
6377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6378 .rpc_argp = &args,
6379 .rpc_resp = &res,
6380 };
6381 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6382}
6383
6384static int
6385nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6386 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6387{
6388 struct nfs4_exception exception = { };
6389 int err;
6390 do {
6391 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6392 switch (err) {
6393 case 0:
6394 case -NFS4ERR_WRONGSEC:
Weston Andros Adamsonb8dd2d62014-01-13 16:54:45 -05006395 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006396 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006397 default:
6398 err = nfs4_handle_exception(server, err, &exception);
6399 }
6400 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006401out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006402 return err;
6403}
6404
6405static int
6406nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6407 struct nfs_fsinfo *info)
6408{
6409 int err;
6410 struct page *page;
6411 rpc_authflavor_t flavor;
6412 struct nfs4_secinfo_flavors *flavors;
6413
6414 page = alloc_page(GFP_KERNEL);
6415 if (!page) {
6416 err = -ENOMEM;
6417 goto out;
6418 }
6419
6420 flavors = page_address(page);
6421 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6422
6423 /*
6424 * Fall back on "guess and check" method if
6425 * the server doesn't support SECINFO_NO_NAME
6426 */
Weston Andros Adamsonb8dd2d62014-01-13 16:54:45 -05006427 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006428 err = nfs4_find_root_sec(server, fhandle, info);
6429 goto out_freepage;
6430 }
6431 if (err)
6432 goto out_freepage;
6433
6434 flavor = nfs_find_best_sec(flavors);
6435 if (err == 0)
6436 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6437
6438out_freepage:
6439 put_page(page);
6440 if (err == -EACCES)
6441 return -EPERM;
6442out:
6443 return err;
6444}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006445
6446static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006447{
6448 int status;
6449 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006450 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006451 };
6452 struct nfs41_test_stateid_res res;
6453 struct rpc_message msg = {
6454 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6455 .rpc_argp = &args,
6456 .rpc_resp = &res,
6457 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006458
Trond Myklebust9d12b212012-01-17 22:04:25 -05006459 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006460 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6461
6462 if (status == NFS_OK)
6463 return res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006464 return status;
6465}
6466
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006467static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006468{
6469 struct nfs4_exception exception = { };
6470 int err;
6471 do {
6472 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006473 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006474 &exception);
6475 } while (exception.retry);
6476 return err;
6477}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006478
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006479static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006480{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006481 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006482 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006483 };
6484 struct nfs41_free_stateid_res res;
6485 struct rpc_message msg = {
6486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6487 .rpc_argp = &args,
6488 .rpc_resp = &res,
6489 };
6490
Trond Myklebust9d12b212012-01-17 22:04:25 -05006491 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6492 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006493}
6494
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006495static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006496{
6497 struct nfs4_exception exception = { };
6498 int err;
6499 do {
6500 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006501 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006502 &exception);
6503 } while (exception.retry);
6504 return err;
6505}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006506
6507static bool nfs41_match_stateid(const nfs4_stateid *s1,
6508 const nfs4_stateid *s2)
6509{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006510 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006511 return false;
6512
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006513 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006514 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006515 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006516 return true;
6517
6518 return false;
6519}
6520
Andy Adamson557134a2009-04-01 09:21:53 -04006521#endif /* CONFIG_NFS_V4_1 */
6522
Trond Myklebust36281ca2012-03-04 18:13:56 -05006523static bool nfs4_match_stateid(const nfs4_stateid *s1,
6524 const nfs4_stateid *s2)
6525{
Trond Myklebustf597c532012-03-04 18:13:56 -05006526 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006527}
6528
6529
Trond Myklebust17280172012-03-11 13:11:00 -04006530static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006531 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006532 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533 .recover_open = nfs4_open_reclaim,
6534 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006535 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006536 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537};
6538
Andy Adamson591d71c2009-04-01 09:22:47 -04006539#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006540static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006541 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6542 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6543 .recover_open = nfs4_open_reclaim,
6544 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006545 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006546 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006547 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006548};
6549#endif /* CONFIG_NFS_V4_1 */
6550
Trond Myklebust17280172012-03-11 13:11:00 -04006551static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006552 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006553 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebusta6831dc2014-10-17 15:10:25 +03006554 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006556 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006557 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558};
6559
Andy Adamson591d71c2009-04-01 09:22:47 -04006560#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006561static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006562 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6563 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006564 .recover_open = nfs41_open_expired,
6565 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006566 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006567 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006568};
6569#endif /* CONFIG_NFS_V4_1 */
6570
Trond Myklebust17280172012-03-11 13:11:00 -04006571static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006572 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006573 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006574 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006575};
6576
6577#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006578static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006579 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006580 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006581 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006582};
6583#endif
6584
Trond Myklebust97dc1352010-06-16 09:52:26 -04006585static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6586 .minor_version = 0,
6587 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006588 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006589 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006590 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6591 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6592 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006593};
6594
6595#if defined(CONFIG_NFS_V4_1)
6596static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6597 .minor_version = 1,
6598 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006599 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006600 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006601 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6602 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6603 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006604};
6605#endif
6606
Trond Myklebust97dc1352010-06-16 09:52:26 -04006607const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6608 [0] = &nfs_v4_0_minor_ops,
6609#if defined(CONFIG_NFS_V4_1)
6610 [1] = &nfs_v4_1_minor_ops,
6611#endif
6612};
6613
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006614static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006615 .permission = nfs_permission,
6616 .getattr = nfs_getattr,
6617 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006618 .getxattr = generic_getxattr,
6619 .setxattr = generic_setxattr,
6620 .listxattr = generic_listxattr,
6621 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006622};
6623
David Howells509de812006-08-22 20:06:11 -04006624const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625 .version = 4, /* protocol version */
6626 .dentry_ops = &nfs4_dentry_operations,
6627 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006628 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006629 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630 .getroot = nfs4_proc_get_root,
6631 .getattr = nfs4_proc_getattr,
6632 .setattr = nfs4_proc_setattr,
6633 .lookup = nfs4_proc_lookup,
6634 .access = nfs4_proc_access,
6635 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 .create = nfs4_proc_create,
6637 .remove = nfs4_proc_remove,
6638 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006639 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640 .unlink_done = nfs4_proc_unlink_done,
6641 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006642 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006643 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006644 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645 .link = nfs4_proc_link,
6646 .symlink = nfs4_proc_symlink,
6647 .mkdir = nfs4_proc_mkdir,
6648 .rmdir = nfs4_proc_remove,
6649 .readdir = nfs4_proc_readdir,
6650 .mknod = nfs4_proc_mknod,
6651 .statfs = nfs4_proc_statfs,
6652 .fsinfo = nfs4_proc_fsinfo,
6653 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006654 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655 .decode_dirent = nfs4_decode_dirent,
6656 .read_setup = nfs4_proc_read_setup,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006657 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006658 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 .write_setup = nfs4_proc_write_setup,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006660 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006661 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006663 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006665 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006666 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006667 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006668 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006669 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670};
6671
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006672static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6673 .prefix = XATTR_NAME_NFSV4_ACL,
6674 .list = nfs4_xattr_list_nfs4_acl,
6675 .get = nfs4_xattr_get_nfs4_acl,
6676 .set = nfs4_xattr_set_nfs4_acl,
6677};
6678
6679const struct xattr_handler *nfs4_xattr_handlers[] = {
6680 &nfs4_xattr_nfs4_acl_handler,
6681 NULL
6682};
6683
Trond Myklebustef159e92012-02-06 19:50:40 -05006684module_param(max_session_slots, ushort, 0644);
6685MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6686 "requests the client will negotiate");
6687
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688/*
6689 * Local variables:
6690 * c-basic-offset: 8
6691 * End:
6692 */