blob: d121c67f87d0ebf38e89586823ae1323ef2d1ab2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
Bryan Schumaker8f70e952011-03-24 17:12:31 +000046#include <linux/sunrpc/gss_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050055#include <linux/nfs_idmap.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040056#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000057#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050058#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010059#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Trond Myklebust4ce79712005-06-22 17:16:21 +000061#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050063#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050064#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040065#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040066#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define NFSDBG_FACILITY NFSDBG_PROC
69
Trond Myklebust2066fe82006-09-15 08:30:46 -040070#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFS4_POLL_RETRY_MAX (15*HZ)
72
Trond Myklebusta78cb572009-08-09 15:06:19 -040073#define NFS4_MAX_LOOP_ON_RECOVER (10)
74
Trond Myklebustef159e92012-02-06 19:50:40 -050075static unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
76
Trond Myklebustcdd4e682006-01-03 09:55:12 +010077struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010078static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080079static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050081static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050082static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Trond Myklebust99367812007-07-17 21:52:41 -040083static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040084static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
86 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040087#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050088static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
89static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040090#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050092static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Trond Myklebust52567b02009-10-23 14:46:42 -040094 if (err >= -1000)
95 return err;
96 switch (err) {
97 case -NFS4ERR_RESOURCE:
98 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000099 case -NFS4ERR_WRONGSEC:
100 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800101 case -NFS4ERR_BADOWNER:
102 case -NFS4ERR_BADNAME:
103 return -EINVAL;
Trond Myklebust27b0a532012-05-28 11:36:28 -0400104 case -NFS4ERR_SHARE_DENIED:
105 return -EACCES;
Trond Myklebust52567b02009-10-23 14:46:42 -0400106 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700108 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400109 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400111 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114/*
115 * This is our standard bitmap for GETATTR requests.
116 */
117const u32 nfs4_fattr_bitmap[2] = {
118 FATTR4_WORD0_TYPE
119 | FATTR4_WORD0_CHANGE
120 | FATTR4_WORD0_SIZE
121 | FATTR4_WORD0_FSID
122 | FATTR4_WORD0_FILEID,
123 FATTR4_WORD1_MODE
124 | FATTR4_WORD1_NUMLINKS
125 | FATTR4_WORD1_OWNER
126 | FATTR4_WORD1_OWNER_GROUP
127 | FATTR4_WORD1_RAWDEV
128 | FATTR4_WORD1_SPACE_USED
129 | FATTR4_WORD1_TIME_ACCESS
130 | FATTR4_WORD1_TIME_METADATA
131 | FATTR4_WORD1_TIME_MODIFY
132};
133
134const u32 nfs4_statfs_bitmap[2] = {
135 FATTR4_WORD0_FILES_AVAIL
136 | FATTR4_WORD0_FILES_FREE
137 | FATTR4_WORD0_FILES_TOTAL,
138 FATTR4_WORD1_SPACE_AVAIL
139 | FATTR4_WORD1_SPACE_FREE
140 | FATTR4_WORD1_SPACE_TOTAL
141};
142
Trond Myklebust4ce79712005-06-22 17:16:21 +0000143const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 FATTR4_WORD0_MAXLINK
145 | FATTR4_WORD0_MAXNAME,
146 0
147};
148
Fred Isamandae100c2011-07-30 20:52:37 -0400149const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 | FATTR4_WORD0_MAXREAD
151 | FATTR4_WORD0_MAXWRITE
152 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700153 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400154 | FATTR4_WORD1_FS_LAYOUT_TYPES,
155 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
Manoj Naik830b8e32006-06-09 09:34:25 -0400158const u32 nfs4_fs_locations_bitmap[2] = {
159 FATTR4_WORD0_TYPE
160 | FATTR4_WORD0_CHANGE
161 | FATTR4_WORD0_SIZE
162 | FATTR4_WORD0_FSID
163 | FATTR4_WORD0_FILEID
164 | FATTR4_WORD0_FS_LOCATIONS,
165 FATTR4_WORD1_MODE
166 | FATTR4_WORD1_NUMLINKS
167 | FATTR4_WORD1_OWNER
168 | FATTR4_WORD1_OWNER_GROUP
169 | FATTR4_WORD1_RAWDEV
170 | FATTR4_WORD1_SPACE_USED
171 | FATTR4_WORD1_TIME_ACCESS
172 | FATTR4_WORD1_TIME_METADATA
173 | FATTR4_WORD1_TIME_MODIFY
174 | FATTR4_WORD1_MOUNTED_ON_FILEID
175};
176
Al Virobc4785c2006-10-19 23:28:51 -0700177static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 struct nfs4_readdir_arg *readdir)
179{
Al Viro0dbb4c62006-10-19 23:28:49 -0700180 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 BUG_ON(readdir->count < 80);
183 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000184 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
186 return;
187 }
188
189 readdir->cookie = 0;
190 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
191 if (cookie == 2)
192 return;
193
194 /*
195 * NFSv4 servers do not return entries for '.' and '..'
196 * Therefore, we fake these entries here. We let '.'
197 * have cookie 0 and '..' have cookie 1. Note that
198 * when talking to the server, we always send cookie 0
199 * instead of 1 or 2.
200 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800201 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 if (cookie == 0) {
204 *p++ = xdr_one; /* next */
205 *p++ = xdr_zero; /* cookie, first word */
206 *p++ = xdr_one; /* cookie, second word */
207 *p++ = xdr_one; /* entry len */
208 memcpy(p, ".\0\0\0", 4); /* entry */
209 p++;
210 *p++ = xdr_one; /* bitmap length */
211 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
212 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400213 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215
216 *p++ = xdr_one; /* next */
217 *p++ = xdr_zero; /* cookie, first word */
218 *p++ = xdr_two; /* cookie, second word */
219 *p++ = xdr_two; /* entry len */
220 memcpy(p, "..\0\0", 4); /* entry */
221 p++;
222 *p++ = xdr_one; /* bitmap length */
223 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
224 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400225 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 readdir->pgbase = (char *)p - (char *)start;
228 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800229 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Trond Myklebust65de8722008-12-23 15:21:44 -0500232static int nfs4_wait_clnt_recover(struct nfs_client *clp)
233{
234 int res;
235
236 might_sleep();
237
Trond Myklebuste005e802008-12-23 15:21:48 -0500238 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400239 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500240 return res;
241}
242
243static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
244{
245 int res = 0;
246
247 might_sleep();
248
249 if (*timeout <= 0)
250 *timeout = NFS4_POLL_RETRY_MIN;
251 if (*timeout > NFS4_POLL_RETRY_MAX)
252 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100253 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500254 if (fatal_signal_pending(current))
255 res = -ERESTARTSYS;
256 *timeout <<= 1;
257 return res;
258}
259
260/* This is the error handling routine for processes that are allowed
261 * to sleep.
262 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800263static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500264{
265 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500266 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500267 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500268 int ret = errorcode;
269
270 exception->retry = 0;
271 switch(errorcode) {
272 case 0:
273 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500274 case -NFS4ERR_OPENMODE:
Trond Myklebust14977482012-03-27 18:31:25 -0400275 if (inode && nfs_have_delegation(inode, FMODE_READ)) {
Trond Myklebust3114ea72012-03-07 16:39:06 -0500276 nfs_inode_return_delegation(inode);
277 exception->retry = 1;
278 return 0;
279 }
280 if (state == NULL)
281 break;
282 nfs4_schedule_stateid_recovery(server, state);
283 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500284 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500285 case -NFS4ERR_ADMIN_REVOKED:
286 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500287 if (state == NULL)
288 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400289 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500290 nfs4_schedule_stateid_recovery(server, state);
291 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400292 case -NFS4ERR_EXPIRED:
293 if (state != NULL)
294 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500295 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500296 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500297 nfs4_schedule_lease_recovery(clp);
298 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500299#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400300 case -NFS4ERR_BADSESSION:
301 case -NFS4ERR_BADSLOT:
302 case -NFS4ERR_BAD_HIGH_SLOT:
303 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
304 case -NFS4ERR_DEADSESSION:
305 case -NFS4ERR_SEQ_FALSE_RETRY:
306 case -NFS4ERR_SEQ_MISORDERED:
307 dprintk("%s ERROR: %d Reset session\n", __func__,
308 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500309 nfs4_schedule_session_recovery(clp->cl_session);
Bryan Schumaker824904c2012-10-30 16:06:35 -0400310 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500311#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500312 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500313 if (exception->timeout > HZ) {
314 /* We have retried a decent amount, time to
315 * fail
316 */
317 ret = -EBUSY;
318 break;
319 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500320 case -NFS4ERR_GRACE:
321 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500322 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500323 ret = nfs4_delay(server->client, &exception->timeout);
324 if (ret != 0)
325 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400326 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500327 case -NFS4ERR_OLD_STATEID:
328 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800329 break;
330 case -NFS4ERR_BADOWNER:
331 /* The following works around a Linux server bug! */
332 case -NFS4ERR_BADNAME:
333 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
334 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
335 exception->retry = 1;
336 printk(KERN_WARNING "NFS: v4 server %s "
337 "does not accept raw "
338 "uid/gids. "
339 "Reenabling the idmapper.\n",
340 server->nfs_client->cl_hostname);
341 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500342 }
343 /* We failed to handle the error */
344 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500345wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500346 ret = nfs4_wait_clnt_recover(clp);
347 if (ret == 0)
348 exception->retry = 1;
349 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500350}
351
352
Trond Myklebust452e9352010-07-31 14:29:06 -0400353static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 spin_lock(&clp->cl_lock);
356 if (time_before(clp->cl_last_renewal,timestamp))
357 clp->cl_last_renewal = timestamp;
358 spin_unlock(&clp->cl_lock);
359}
360
Trond Myklebust452e9352010-07-31 14:29:06 -0400361static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
362{
363 do_renew_lease(server->nfs_client, timestamp);
364}
365
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400366#if defined(CONFIG_NFS_V4_1)
367
Benny Halevy510b8172009-04-01 09:22:14 -0400368/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400369 * nfs4_free_slot - free a slot and efficiently update slot table.
370 *
371 * freeing a slot is trivially done by clearing its respective bit
372 * in the bitmap.
373 * If the freed slotid equals highest_used_slotid we want to update it
374 * so that the server would be able to size down the slot table if needed,
375 * otherwise we know that the highest_used_slotid is still in use.
376 * When updating highest_used_slotid there may be "holes" in the bitmap
377 * so we need to scan down from highest_used_slotid to 0 looking for the now
378 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500379 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500380 *
381 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400382 */
383static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500384nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400385{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500386 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400387 /* clear used bit in bitmap */
388 __clear_bit(slotid, tbl->used_slots);
389
390 /* update highest_used_slotid when it is freed */
391 if (slotid == tbl->highest_used_slotid) {
392 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500393 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400394 tbl->highest_used_slotid = slotid;
395 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500396 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400397 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500398 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
399 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400400}
401
Trond Myklebust961a8282012-01-17 22:57:37 -0500402bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
403{
404 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
405 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400406}
407
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800408/*
Andy Adamson42acd022011-01-06 02:04:34 +0000409 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800410 */
Andy Adamson42acd022011-01-06 02:04:34 +0000411static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800412{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400413 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500414 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
415 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800416 return;
417 }
418
Trond Myklebust45d43c22012-02-06 19:38:51 -0500419 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800420 return;
421
Andy Adamson42acd022011-01-06 02:04:34 +0000422 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
423 complete(&ses->fc_slot_table.complete);
424}
425
426/*
427 * Signal state manager thread if session back channel is drained
428 */
429void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
430{
431 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500432 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000433 return;
434 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
435 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800436}
437
Trond Myklebustd185a332010-06-16 09:52:25 -0400438static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400439{
440 struct nfs4_slot_table *tbl;
441
Trond Myklebustd185a332010-06-16 09:52:25 -0400442 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f302010-09-24 09:17:01 -0400443 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400444 /* just wake up the next guy waiting since
445 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500446 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500447 return;
Andy Adamson13615872009-04-01 09:22:17 -0400448 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500449
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500450 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500451 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000452 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500453 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f302010-09-24 09:17:01 -0400454 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400455}
456
Trond Myklebust14516c32010-07-31 14:29:06 -0400457static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400458{
459 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400460 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400461
462 /*
463 * sr_status remains 1 if an RPC level error occurred. The server
464 * may or may not have processed the sequence operation..
465 * Proceed as if the server received and processed the sequence
466 * operation.
467 */
468 if (res->sr_status == 1)
469 res->sr_status = NFS_OK;
470
Bryan Schumaker468f8612011-04-18 15:57:32 -0400471 /* don't increment the sequence number if the task wasn't sent */
472 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400473 goto out;
474
Andy Adamson691daf32009-12-04 15:55:39 -0500475 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400476 switch (res->sr_status) {
477 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400478 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f302010-09-24 09:17:01 -0400479 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400480 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400481 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400482 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500483 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500484 if (res->sr_status_flags != 0)
485 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400486 break;
487 case -NFS4ERR_DELAY:
488 /* The server detected a resend of the RPC call and
489 * returned NFS4ERR_DELAY as per Section 2.10.6.2
490 * of RFC5661.
491 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200492 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f302010-09-24 09:17:01 -0400493 __func__,
494 res->sr_slot - res->sr_session->fc_slot_table.slots,
495 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400496 goto out_retry;
497 default:
498 /* Just update the slot sequence no. */
Benny Halevydfb4f302010-09-24 09:17:01 -0400499 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400500 }
501out:
502 /* The session may be reset by one of the error handlers. */
503 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400504 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400505 return 1;
506out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400507 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400508 goto out;
509 rpc_delay(task, NFS4_POLL_RETRY_MAX);
510 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400511}
512
Trond Myklebust14516c32010-07-31 14:29:06 -0400513static int nfs4_sequence_done(struct rpc_task *task,
514 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400515{
Trond Myklebust14516c32010-07-31 14:29:06 -0400516 if (res->sr_session == NULL)
517 return 1;
518 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400519}
520
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400521/*
Benny Halevy510b8172009-04-01 09:22:14 -0400522 * nfs4_find_slot - efficiently look for a free slot
523 *
524 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
525 * If found, we mark the slot as used, update the highest_used_slotid,
526 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500527 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400528 *
529 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400530 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500531static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800532nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400533{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500534 u32 slotid;
535 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400536
Trond Myklebust45d43c22012-02-06 19:38:51 -0500537 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400538 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
539 tbl->max_slots);
540 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
541 if (slotid >= tbl->max_slots)
542 goto out;
543 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500544 if (slotid > tbl->highest_used_slotid ||
545 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400546 tbl->highest_used_slotid = slotid;
547 ret_id = slotid;
548out:
549 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
550 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400551 return ret_id;
552}
553
Trond Myklebust9d12b212012-01-17 22:04:25 -0500554static void nfs41_init_sequence(struct nfs4_sequence_args *args,
555 struct nfs4_sequence_res *res, int cache_reply)
556{
557 args->sa_session = NULL;
558 args->sa_cache_this = 0;
559 if (cache_reply)
560 args->sa_cache_this = 1;
561 res->sr_session = NULL;
562 res->sr_slot = NULL;
563}
564
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000565int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400566 struct nfs4_sequence_args *args,
567 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400568 struct rpc_task *task)
569{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400570 struct nfs4_slot *slot;
571 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500572 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400573
574 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400575 /* slot already allocated? */
Benny Halevydfb4f302010-09-24 09:17:01 -0400576 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400577 return 0;
578
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400579 tbl = &session->fc_slot_table;
580
581 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400582 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800583 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500584 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500585 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400586 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500587 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500588 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400589 }
590
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800591 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
592 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
593 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
594 spin_unlock(&tbl->slot_tbl_lock);
595 dprintk("%s enforce FIFO order\n", __func__);
596 return -EAGAIN;
597 }
598
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800599 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500600 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400601 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
602 spin_unlock(&tbl->slot_tbl_lock);
603 dprintk("<-- %s: no free slots\n", __func__);
604 return -EAGAIN;
605 }
606 spin_unlock(&tbl->slot_tbl_lock);
607
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800608 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400609 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400610 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400611 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400612
613 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
614
Benny Halevy99fe60d2009-04-01 09:22:29 -0400615 res->sr_session = session;
Benny Halevydfb4f302010-09-24 09:17:01 -0400616 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400617 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400618 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400619 /*
620 * sr_status is only set in decode_sequence, and so will remain
621 * set to 1 if an rpc level failure occurs.
622 */
623 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400624 return 0;
625}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000626EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400627
Trond Myklebust035168a2010-06-16 09:52:26 -0400628int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400629 struct nfs4_sequence_args *args,
630 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400631 struct rpc_task *task)
632{
Trond Myklebust035168a2010-06-16 09:52:26 -0400633 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400634 int ret = 0;
635
Trond Myklebust9d12b212012-01-17 22:04:25 -0500636 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400637 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400638
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200639 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f302010-09-24 09:17:01 -0400640 __func__, session->clp, session, res->sr_slot ?
641 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400642
Trond Myklebust9d12b212012-01-17 22:04:25 -0500643 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400644out:
645 dprintk("<-- %s status=%d\n", __func__, ret);
646 return ret;
647}
648
649struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400650 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400651 struct nfs4_sequence_args *seq_args;
652 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400653};
654
655static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
656{
657 struct nfs41_call_sync_data *data = calldata;
658
Trond Myklebust035168a2010-06-16 09:52:26 -0400659 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
660
661 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500662 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400663 return;
664 rpc_call_start(task);
665}
666
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800667static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
668{
669 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
670 nfs41_call_sync_prepare(task, calldata);
671}
672
Andy Adamson69ab40c2009-04-01 09:22:19 -0400673static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
674{
675 struct nfs41_call_sync_data *data = calldata;
676
Trond Myklebust14516c32010-07-31 14:29:06 -0400677 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400678}
679
Trond Myklebust17280172012-03-11 13:11:00 -0400680static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400681 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400682 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400683};
684
Trond Myklebust17280172012-03-11 13:11:00 -0400685static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800686 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
687 .rpc_call_done = nfs41_call_sync_done,
688};
689
Bryan Schumaker7c513052011-03-24 17:12:24 +0000690static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
691 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400692 struct rpc_message *msg,
693 struct nfs4_sequence_args *args,
694 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800695 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400696{
697 int ret;
698 struct rpc_task *task;
699 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400700 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400701 .seq_args = args,
702 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400703 };
704 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000705 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400706 .rpc_message = msg,
707 .callback_ops = &nfs41_call_sync_ops,
708 .callback_data = &data
709 };
710
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800711 if (privileged)
712 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400713 task = rpc_run_task(&task_setup);
714 if (IS_ERR(task))
715 ret = PTR_ERR(task);
716 else {
717 ret = task->tk_status;
718 rpc_put_task(task);
719 }
720 return ret;
721}
722
Bryan Schumaker7c513052011-03-24 17:12:24 +0000723int _nfs4_call_sync_session(struct rpc_clnt *clnt,
724 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400725 struct rpc_message *msg,
726 struct nfs4_sequence_args *args,
727 struct nfs4_sequence_res *res,
728 int cache_reply)
729{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500730 nfs41_init_sequence(args, res, cache_reply);
731 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400732}
733
Trond Myklebustdf896452010-06-16 09:52:26 -0400734#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500735static inline
736void nfs41_init_sequence(struct nfs4_sequence_args *args,
737 struct nfs4_sequence_res *res, int cache_reply)
738{
739}
740
Trond Myklebust14516c32010-07-31 14:29:06 -0400741static int nfs4_sequence_done(struct rpc_task *task,
742 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400743{
Trond Myklebust14516c32010-07-31 14:29:06 -0400744 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400745}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400746#endif /* CONFIG_NFS_V4_1 */
747
Bryan Schumaker7c513052011-03-24 17:12:24 +0000748int _nfs4_call_sync(struct rpc_clnt *clnt,
749 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400750 struct rpc_message *msg,
751 struct nfs4_sequence_args *args,
752 struct nfs4_sequence_res *res,
753 int cache_reply)
754{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500755 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000756 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400757}
758
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000759static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000760int nfs4_call_sync(struct rpc_clnt *clnt,
761 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000762 struct rpc_message *msg,
763 struct nfs4_sequence_args *args,
764 struct nfs4_sequence_res *res,
765 int cache_reply)
766{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000767 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
768 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000769}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400770
Trond Myklebust38478b22006-05-25 01:40:57 -0400771static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Trond Myklebust38478b22006-05-25 01:40:57 -0400773 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Trond Myklebust38478b22006-05-25 01:40:57 -0400775 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400776 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700777 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400778 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700779 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400780 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100783struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400784 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100785 struct nfs_openargs o_arg;
786 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100787 struct nfs_open_confirmargs c_arg;
788 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500789 struct nfs4_string owner_name;
790 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100791 struct nfs_fattr f_attr;
792 struct nfs_fattr dir_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100793 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400794 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100795 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400796 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100797 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100798 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400799 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100800 int rpc_status;
801 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100802};
803
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400804
805static void nfs4_init_opendata_res(struct nfs4_opendata *p)
806{
807 p->o_res.f_attr = &p->f_attr;
808 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400809 p->o_res.seqid = p->o_arg.seqid;
810 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400811 p->o_res.server = p->o_arg.server;
812 nfs_fattr_init(&p->f_attr);
813 nfs_fattr_init(&p->dir_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500814 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400815}
816
Al Viro82a2c1b2011-06-22 18:30:55 -0400817static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500818 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400819 const struct iattr *attrs,
820 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100821{
Al Viro82a2c1b2011-06-22 18:30:55 -0400822 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823 struct inode *dir = parent->d_inode;
824 struct nfs_server *server = NFS_SERVER(dir);
825 struct nfs4_opendata *p;
826
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400827 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828 if (p == NULL)
829 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400830 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100831 if (p->o_arg.seqid == NULL)
832 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400833 nfs_sb_active(dentry->d_sb);
834 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100835 p->dir = parent;
836 p->owner = sp;
837 atomic_inc(&sp->so_count);
838 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500839 p->o_arg.open_flags = flags;
840 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400841 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400842 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
843 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400844 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100845 p->o_arg.server = server;
846 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500847 p->o_arg.dir_bitmask = server->cache_consistency_bitmask;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100848 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500849 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500850 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400851
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100852 p->o_arg.u.attrs = &p->attrs;
853 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500854
855 verf[0] = jiffies;
856 verf[1] = current->pid;
857 memcpy(p->o_arg.u.verifier.data, verf,
858 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100859 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100860 p->c_arg.fh = &p->o_res.fh;
861 p->c_arg.stateid = &p->o_res.stateid;
862 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400863 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400864 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100865 return p;
866err_free:
867 kfree(p);
868err:
869 dput(parent);
870 return NULL;
871}
872
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400873static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100874{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400875 struct nfs4_opendata *p = container_of(kref,
876 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400877 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400878
879 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400880 if (p->state != NULL)
881 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400882 nfs4_put_state_owner(p->owner);
883 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400884 dput(p->dentry);
885 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500886 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400887 kfree(p);
888}
889
890static void nfs4_opendata_put(struct nfs4_opendata *p)
891{
892 if (p != NULL)
893 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100894}
895
Trond Myklebust06f814a2006-01-03 09:55:07 +0100896static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
897{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100898 int ret;
899
Trond Myklebust06f814a2006-01-03 09:55:07 +0100900 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100901 return ret;
902}
903
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500904static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400905{
906 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500907
Trond Myklebust536e43d2012-01-17 22:04:26 -0500908 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500909 goto out;
910 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400911 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500912 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
913 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400914 break;
915 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500916 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
917 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400918 break;
919 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500920 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
921 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400922 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500923out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400924 return ret;
925}
926
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400928{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500929 if (delegation == NULL)
930 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500931 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400932 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500933 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400934 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500935 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400936 return 1;
937}
938
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500939static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100940{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500941 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100942 case FMODE_WRITE:
943 state->n_wronly++;
944 break;
945 case FMODE_READ:
946 state->n_rdonly++;
947 break;
948 case FMODE_READ|FMODE_WRITE:
949 state->n_rdwr++;
950 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500951 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100952}
953
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400955{
956 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500957 nfs4_stateid_copy(&state->stateid, stateid);
958 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400960 case FMODE_READ:
961 set_bit(NFS_O_RDONLY_STATE, &state->flags);
962 break;
963 case FMODE_WRITE:
964 set_bit(NFS_O_WRONLY_STATE, &state->flags);
965 break;
966 case FMODE_READ|FMODE_WRITE:
967 set_bit(NFS_O_RDWR_STATE, &state->flags);
968 }
969}
970
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500971static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400972{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400973 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500974 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400975 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400976}
977
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500978static 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 -0700979{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400980 /*
981 * Protect the call to nfs4_state_set_mode_locked and
982 * serialise the stateid update
983 */
984 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400985 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500986 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400987 set_bit(NFS_DELEGATED_STATE, &state->flags);
988 }
989 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500990 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400991 write_sequnlock(&state->seqlock);
992 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500993 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700994 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500997static 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 -0500998{
999 struct nfs_inode *nfsi = NFS_I(state->inode);
1000 struct nfs_delegation *deleg_cur;
1001 int ret = 0;
1002
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001003 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001004
1005 rcu_read_lock();
1006 deleg_cur = rcu_dereference(nfsi->delegation);
1007 if (deleg_cur == NULL)
1008 goto no_delegation;
1009
1010 spin_lock(&deleg_cur->lock);
1011 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001012 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001013 goto no_delegation_unlock;
1014
1015 if (delegation == NULL)
1016 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001017 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001018 goto no_delegation_unlock;
1019
Trond Myklebustb7391f42008-12-23 15:21:52 -05001020 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001021 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001022 ret = 1;
1023no_delegation_unlock:
1024 spin_unlock(&deleg_cur->lock);
1025no_delegation:
1026 rcu_read_unlock();
1027
1028 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001029 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001030 ret = 1;
1031 }
1032
1033 return ret;
1034}
1035
1036
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001037static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001038{
1039 struct nfs_delegation *delegation;
1040
1041 rcu_read_lock();
1042 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001043 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001044 rcu_read_unlock();
1045 return;
1046 }
1047 rcu_read_unlock();
1048 nfs_inode_return_delegation(inode);
1049}
1050
Trond Myklebust6ee41262007-07-08 14:11:36 -04001051static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001052{
1053 struct nfs4_state *state = opendata->state;
1054 struct nfs_inode *nfsi = NFS_I(state->inode);
1055 struct nfs_delegation *delegation;
Trond Myklebust19091a72013-05-29 15:36:40 -04001056 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001057 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001058 nfs4_stateid stateid;
1059 int ret = -EAGAIN;
1060
Trond Myklebustaac00a82007-07-05 19:02:21 -04001061 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001062 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001063 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001064 if (can_open_cached(state, fmode, open_mode)) {
1065 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001066 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001067 goto out_return_state;
1068 }
1069 spin_unlock(&state->owner->so_lock);
1070 }
Trond Myklebust34310432008-12-23 15:21:38 -05001071 rcu_read_lock();
1072 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001073 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001074 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001075 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001076 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001077 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001078 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001079 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001080 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001081 if (ret != 0)
1082 goto out;
1083 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001084
1085 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001086 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001087 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001088 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001089out:
1090 return ERR_PTR(ret);
1091out_return_state:
1092 atomic_inc(&state->count);
1093 return state;
1094}
1095
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001096static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1097{
1098 struct inode *inode;
1099 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001100 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001101 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001102
Trond Myklebustaac00a82007-07-05 19:02:21 -04001103 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001104 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001105 goto out;
1106 }
1107
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001108 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001109 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001110 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001111 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001112 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001113 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001114 goto err;
1115 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001116 state = nfs4_get_open_state(inode, data->owner);
1117 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001118 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001119 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001120 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001121 int delegation_flags = 0;
1122
Trond Myklebust003707c2007-07-05 18:07:55 -04001123 rcu_read_lock();
1124 delegation = rcu_dereference(NFS_I(inode)->delegation);
1125 if (delegation)
1126 delegation_flags = delegation->flags;
1127 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001128 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1129 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1130 "returning a delegation for "
1131 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001132 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001133 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001134 nfs_inode_set_delegation(state->inode,
1135 data->owner->so_cred,
1136 &data->o_res);
1137 else
1138 nfs_inode_reclaim_delegation(state->inode,
1139 data->owner->so_cred,
1140 &data->o_res);
1141 }
Trond Myklebust34310432008-12-23 15:21:38 -05001142
1143 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001144 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001145 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001146out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001147 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001148err_put_inode:
1149 iput(inode);
1150err:
1151 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001152}
1153
Trond Myklebust864472e2006-01-03 09:55:15 +01001154static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1155{
1156 struct nfs_inode *nfsi = NFS_I(state->inode);
1157 struct nfs_open_context *ctx;
1158
1159 spin_lock(&state->inode->i_lock);
1160 list_for_each_entry(ctx, &nfsi->open_files, list) {
1161 if (ctx->state != state)
1162 continue;
1163 get_nfs_open_context(ctx);
1164 spin_unlock(&state->inode->i_lock);
1165 return ctx;
1166 }
1167 spin_unlock(&state->inode->i_lock);
1168 return ERR_PTR(-ENOENT);
1169}
1170
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001171static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1172{
1173 struct nfs4_opendata *opendata;
1174
Al Viro3d4ff432011-06-22 18:40:12 -04001175 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001176 if (opendata == NULL)
1177 return ERR_PTR(-ENOMEM);
1178 opendata->state = state;
1179 atomic_inc(&state->count);
1180 return opendata;
1181}
1182
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001183static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001184{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001185 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001186 int ret;
1187
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001188 opendata->o_arg.open_flags = 0;
1189 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001190 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1191 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1192 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001193 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001194 if (ret != 0)
1195 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001196 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001197 if (IS_ERR(newstate))
1198 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001199 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001200 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001201 return 0;
1202}
1203
1204static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1205{
Trond Myklebust864472e2006-01-03 09:55:15 +01001206 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001207 int ret;
1208
1209 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001210 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001211 smp_rmb();
1212 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001213 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001214 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001215 if (ret != 0)
1216 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001217 if (newstate != state)
1218 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001219 }
1220 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001221 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001222 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001223 if (ret != 0)
1224 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001225 if (newstate != state)
1226 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001227 }
1228 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001229 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001230 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001231 if (ret != 0)
1232 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001233 if (newstate != state)
1234 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001235 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001236 /*
1237 * We may have performed cached opens for all three recoveries.
1238 * Check if we need to update the current stateid.
1239 */
1240 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001241 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001242 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001243 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001244 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001245 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001246 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001247 return 0;
1248}
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250/*
1251 * OPEN_RECLAIM:
1252 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001254static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001256 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001257 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001258 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 int status;
1260
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001261 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1262 if (IS_ERR(opendata))
1263 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001264 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1265 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001266 rcu_read_lock();
1267 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001268 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001269 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001270 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001271 opendata->o_arg.u.delegation_type = delegation_type;
1272 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001273 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return status;
1275}
1276
Trond Myklebust539cd032007-06-05 11:46:42 -04001277static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
1279 struct nfs_server *server = NFS_SERVER(state->inode);
1280 struct nfs4_exception exception = { };
1281 int err;
1282 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001283 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001284 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001285 break;
1286 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 } while (exception.retry);
1288 return err;
1289}
1290
Trond Myklebust864472e2006-01-03 09:55:15 +01001291static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1292{
1293 struct nfs_open_context *ctx;
1294 int ret;
1295
1296 ctx = nfs4_state_find_open_context(state);
1297 if (IS_ERR(ctx))
1298 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001299 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001300 put_nfs_open_context(ctx);
1301 return ret;
1302}
1303
Trond Myklebust13437e12007-07-06 15:10:43 -04001304static 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 -07001305{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001306 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001307 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001309 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1310 if (IS_ERR(opendata))
1311 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001312 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001313 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001314 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001315 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001316 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
1318
Trond Myklebust13437e12007-07-06 15:10:43 -04001319int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001322 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 int err;
1324 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001325 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 switch (err) {
1327 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001328 case -ENOENT:
1329 case -ESTALE:
1330 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001331 case -NFS4ERR_BADSESSION:
1332 case -NFS4ERR_BADSLOT:
1333 case -NFS4ERR_BAD_HIGH_SLOT:
1334 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1335 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001336 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001337 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 case -NFS4ERR_STALE_CLIENTID:
1339 case -NFS4ERR_STALE_STATEID:
1340 case -NFS4ERR_EXPIRED:
1341 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001342 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001343 goto out;
1344 case -ERESTARTSYS:
1345 /*
1346 * The show must go on: exit, but mark the
1347 * stateid as needing recovery.
1348 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001349 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001350 case -NFS4ERR_ADMIN_REVOKED:
1351 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001352 nfs_inode_find_state_and_recover(state->inode,
1353 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001354 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001355 case -EKEYEXPIRED:
1356 /*
1357 * User RPCSEC_GSS context has expired.
1358 * We cannot recover this stateid now, so
1359 * skip it and allow recovery thread to
1360 * proceed.
1361 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001362 case -ENOMEM:
1363 err = 0;
1364 goto out;
Trond Myklebusted9a34c2013-04-01 15:34:05 -04001365 case -NFS4ERR_DELAY:
1366 case -NFS4ERR_GRACE:
1367 set_bit(NFS_DELEGATED_STATE, &state->flags);
1368 ssleep(1);
1369 err = -EAGAIN;
1370 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372 err = nfs4_handle_exception(server, err, &exception);
1373 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001374out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return err;
1376}
1377
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001378static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1379{
1380 struct nfs4_opendata *data = calldata;
1381
1382 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001383 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001384 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001385 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001386 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001387 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001388 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001389}
1390
1391static void nfs4_open_confirm_release(void *calldata)
1392{
1393 struct nfs4_opendata *data = calldata;
1394 struct nfs4_state *state = NULL;
1395
1396 /* If this request hasn't been cancelled, do nothing */
1397 if (data->cancelled == 0)
1398 goto out_free;
1399 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001400 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001401 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001402 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001403 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001404 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001405out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001406 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001407}
1408
1409static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001410 .rpc_call_done = nfs4_open_confirm_done,
1411 .rpc_release = nfs4_open_confirm_release,
1412};
1413
1414/*
1415 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1416 */
1417static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1418{
1419 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1420 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001421 struct rpc_message msg = {
1422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1423 .rpc_argp = &data->c_arg,
1424 .rpc_resp = &data->c_res,
1425 .rpc_cred = data->owner->so_cred,
1426 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001427 struct rpc_task_setup task_setup_data = {
1428 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001429 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001430 .callback_ops = &nfs4_open_confirm_ops,
1431 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001432 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001433 .flags = RPC_TASK_ASYNC,
1434 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 int status;
1436
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001437 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001438 data->rpc_done = 0;
1439 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001440 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001441 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001442 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001443 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001444 status = nfs4_wait_for_completion_rpc_task(task);
1445 if (status != 0) {
1446 data->cancelled = 1;
1447 smp_wmb();
1448 } else
1449 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001450 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return status;
1452}
1453
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001454static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001456 struct nfs4_opendata *data = calldata;
1457 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001458
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001459 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1460 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001461 /*
1462 * Check if we still need to send an OPEN call, or if we can use
1463 * a delegation instead.
1464 */
1465 if (data->state != NULL) {
1466 struct nfs_delegation *delegation;
1467
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001468 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001469 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001470 rcu_read_lock();
1471 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001472 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1473 can_open_delegated(delegation, data->o_arg.fmode))
1474 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001475 rcu_read_unlock();
1476 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001477 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001478 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001479 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001480 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001481 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1482 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001483 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001484 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001485 &data->o_arg.seq_args,
Trond Myklebust0ada2102012-10-29 18:37:40 -04001486 &data->o_res.seq_res,
1487 task) != 0)
1488 nfs_release_seqid(data->o_arg.seqid);
1489 else
1490 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001491 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001492unlock_no_action:
1493 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001494out_no_action:
1495 task->tk_action = NULL;
1496
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001497}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001499static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1500{
1501 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1502 nfs4_open_prepare(task, calldata);
1503}
1504
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001505static void nfs4_open_done(struct rpc_task *task, void *calldata)
1506{
1507 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001509 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001510
Trond Myklebust14516c32010-07-31 14:29:06 -04001511 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1512 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001513
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001514 if (task->tk_status == 0) {
1515 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001516 case S_IFREG:
1517 break;
1518 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001519 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001520 break;
1521 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001522 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001523 break;
1524 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001526 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001527 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001528 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1529 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001530 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001531 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001532}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001533
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001534static void nfs4_open_release(void *calldata)
1535{
1536 struct nfs4_opendata *data = calldata;
1537 struct nfs4_state *state = NULL;
1538
1539 /* If this request hasn't been cancelled, do nothing */
1540 if (data->cancelled == 0)
1541 goto out_free;
1542 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001543 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001544 goto out_free;
1545 /* In case we need an open_confirm, no cleanup! */
1546 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1547 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001548 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001549 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001550 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001551out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001552 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001553}
1554
1555static const struct rpc_call_ops nfs4_open_ops = {
1556 .rpc_call_prepare = nfs4_open_prepare,
1557 .rpc_call_done = nfs4_open_done,
1558 .rpc_release = nfs4_open_release,
1559};
1560
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001561static const struct rpc_call_ops nfs4_recover_open_ops = {
1562 .rpc_call_prepare = nfs4_recover_open_prepare,
1563 .rpc_call_done = nfs4_open_done,
1564 .rpc_release = nfs4_open_release,
1565};
1566
1567static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001568{
1569 struct inode *dir = data->dir->d_inode;
1570 struct nfs_server *server = NFS_SERVER(dir);
1571 struct nfs_openargs *o_arg = &data->o_arg;
1572 struct nfs_openres *o_res = &data->o_res;
1573 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001574 struct rpc_message msg = {
1575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1576 .rpc_argp = o_arg,
1577 .rpc_resp = o_res,
1578 .rpc_cred = data->owner->so_cred,
1579 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001580 struct rpc_task_setup task_setup_data = {
1581 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001582 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001583 .callback_ops = &nfs4_open_ops,
1584 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001585 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001586 .flags = RPC_TASK_ASYNC,
1587 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001588 int status;
1589
Trond Myklebust9d12b212012-01-17 22:04:25 -05001590 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001591 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001592 data->rpc_done = 0;
1593 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001594 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001595 if (isrecover)
1596 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001597 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001598 if (IS_ERR(task))
1599 return PTR_ERR(task);
1600 status = nfs4_wait_for_completion_rpc_task(task);
1601 if (status != 0) {
1602 data->cancelled = 1;
1603 smp_wmb();
1604 } else
1605 status = data->rpc_status;
1606 rpc_put_task(task);
1607
1608 return status;
1609}
1610
1611static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1612{
1613 struct inode *dir = data->dir->d_inode;
1614 struct nfs_openres *o_res = &data->o_res;
1615 int status;
1616
1617 status = nfs4_run_open_task(data, 1);
1618 if (status != 0 || !data->rpc_done)
1619 return status;
1620
Trond Myklebust6926afd2012-01-07 13:22:46 -05001621 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1622
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001623 nfs_refresh_inode(dir, o_res->dir_attr);
1624
1625 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1626 status = _nfs4_proc_open_confirm(data);
1627 if (status != 0)
1628 return status;
1629 }
1630
1631 return status;
1632}
1633
1634/*
1635 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1636 */
1637static int _nfs4_proc_open(struct nfs4_opendata *data)
1638{
1639 struct inode *dir = data->dir->d_inode;
1640 struct nfs_server *server = NFS_SERVER(dir);
1641 struct nfs_openargs *o_arg = &data->o_arg;
1642 struct nfs_openres *o_res = &data->o_res;
1643 int status;
1644
1645 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001646 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001647 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001648 if (status != 0) {
1649 if (status == -NFS4ERR_BADNAME &&
1650 !(o_arg->open_flags & O_CREAT))
1651 return -ENOENT;
1652 return status;
1653 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001654
Trond Myklebust6926afd2012-01-07 13:22:46 -05001655 nfs_fattr_map_and_free_names(server, &data->f_attr);
1656
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001657 if (o_arg->open_flags & O_CREAT) {
1658 update_changeattr(dir, &o_res->cinfo);
1659 nfs_post_op_update_inode(dir, o_res->dir_attr);
1660 } else
1661 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001662 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1663 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001665 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001667 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
1669 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001670 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001671 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
Andy Adamsond83217c2011-03-01 01:34:17 +00001674static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001675{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001676 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001677 int ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001678
Trond Myklebusta78cb572009-08-09 15:06:19 -04001679 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001680 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001681 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001682 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001683 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1684 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001685 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001686 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001687 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001688 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001689 return ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001690}
1691
Andy Adamsond83217c2011-03-01 01:34:17 +00001692static int nfs4_recover_expired_lease(struct nfs_server *server)
1693{
1694 return nfs4_client_recover_expired_lease(server->nfs_client);
1695}
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697/*
1698 * OPEN_EXPIRED:
1699 * reclaim state on the server after a network partition.
1700 * Assumes caller holds the appropriate lock
1701 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001702static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001704 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001705 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001707 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1708 if (IS_ERR(opendata))
1709 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001710 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001711 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001712 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001713 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001714 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
1716
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001717static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001718{
Trond Myklebust539cd032007-06-05 11:46:42 -04001719 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001720 struct nfs4_exception exception = { };
1721 int err;
1722
1723 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001724 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001725 switch (err) {
1726 default:
1727 goto out;
1728 case -NFS4ERR_GRACE:
1729 case -NFS4ERR_DELAY:
1730 nfs4_handle_exception(server, err, &exception);
1731 err = 0;
1732 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001733 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001734out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001735 return err;
1736}
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1739{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001741 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Trond Myklebust864472e2006-01-03 09:55:15 +01001743 ctx = nfs4_state_find_open_context(state);
1744 if (IS_ERR(ctx))
1745 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001746 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001747 put_nfs_open_context(ctx);
1748 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749}
1750
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001751#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001752static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001753{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001754 int status = NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001755 struct nfs_server *server = NFS_SERVER(state->inode);
1756
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001757 if (state->flags & flags) {
1758 status = nfs41_test_stateid(server, stateid);
1759 if (status != NFS_OK) {
1760 nfs41_free_stateid(server, stateid);
1761 state->flags &= ~flags;
1762 }
1763 }
1764 return status;
1765}
1766
1767static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1768{
1769 int deleg_status, open_status;
1770 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1771 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1772
1773 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1774 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1775
1776 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1777 return NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001778 return nfs4_open_expired(sp, state);
1779}
1780#endif
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001783 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1784 * fields corresponding to attributes that were used to store the verifier.
1785 * Make sure we clobber those fields in the later setattr call
1786 */
1787static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1788{
1789 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1790 !(sattr->ia_valid & ATTR_ATIME_SET))
1791 sattr->ia_valid |= ATTR_ATIME;
1792
1793 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1794 !(sattr->ia_valid & ATTR_MTIME_SET))
1795 sattr->ia_valid |= ATTR_MTIME;
1796}
1797
1798/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001799 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001801static 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 -07001802{
1803 struct nfs4_state_owner *sp;
1804 struct nfs4_state *state = NULL;
1805 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001806 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001807 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001811 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1812 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1814 goto out_err;
1815 }
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001816 status = nfs4_recover_expired_lease(server);
1817 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001818 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001819 if (dentry->d_inode != NULL)
1820 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001821 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001822 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001823 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001824 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Al Viro82a2c1b2011-06-22 18:30:55 -04001826 if (dentry->d_inode != NULL)
1827 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001828
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001829 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001831 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001833 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001834 status = PTR_ERR(state);
1835 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001836 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001837 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001838 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001839
1840 if (opendata->o_arg.open_flags & O_EXCL) {
1841 nfs4_exclusive_attrset(opendata, sattr);
1842
1843 nfs_fattr_init(opendata->o_res.f_attr);
1844 status = nfs4_do_setattr(state->inode, cred,
1845 opendata->o_res.f_attr, sattr,
1846 state);
1847 if (status == 0)
1848 nfs_setattr_update_inode(state->inode, sattr);
1849 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1850 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001851 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 *res = state;
1854 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001855err_opendata_put:
1856 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001857err_put_state_owner:
1858 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 *res = NULL;
1861 return status;
1862}
1863
1864
Al Viro82a2c1b2011-06-22 18:30:55 -04001865static 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 -07001866{
1867 struct nfs4_exception exception = { };
1868 struct nfs4_state *res;
1869 int status;
1870
Trond Myklebust5e74cfe2012-06-08 10:58:09 -04001871 fmode &= FMODE_READ|FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001873 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (status == 0)
1875 break;
1876 /* NOTE: BAD_SEQID means the server and client disagree about the
1877 * book-keeping w.r.t. state-changing operations
1878 * (OPEN/CLOSE/LOCK/LOCKU...)
1879 * It is actually a sign of a bug on the client or on the server.
1880 *
1881 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001882 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 * have unhashed the old state_owner for us, and that we can
1884 * therefore safely retry using a new one. We should still warn
1885 * the user though...
1886 */
1887 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001888 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001889 " returned a bad sequence-id error!\n",
1890 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 exception.retry = 1;
1892 continue;
1893 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001894 /*
1895 * BAD_STATEID on OPEN means that the server cancelled our
1896 * state before it received the OPEN_CONFIRM.
1897 * Recover by retrying the request as per the discussion
1898 * on Page 181 of RFC3530.
1899 */
1900 if (status == -NFS4ERR_BAD_STATEID) {
1901 exception.retry = 1;
1902 continue;
1903 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001904 if (status == -EAGAIN) {
1905 /* We must have found a delegation */
1906 exception.retry = 1;
1907 continue;
1908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1910 status, &exception));
1911 } while (exception.retry);
1912 return res;
1913}
1914
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001915static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1916 struct nfs_fattr *fattr, struct iattr *sattr,
1917 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001919 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001921 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 .iap = sattr,
1923 .server = server,
1924 .bitmask = server->attr_bitmask,
1925 };
1926 struct nfs_setattrres res = {
1927 .fattr = fattr,
1928 .server = server,
1929 };
1930 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001931 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1932 .rpc_argp = &arg,
1933 .rpc_resp = &res,
1934 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001936 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001937 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Trond Myklebust0e574af2005-10-27 22:12:38 -04001939 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Trond Myklebust4fc87962012-03-08 17:42:01 -05001941 if (state != NULL) {
1942 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1943 current->files, current->tgid);
1944 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1945 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001946 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001947 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05001948 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Bryan Schumaker7c513052011-03-24 17:12:24 +00001950 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001951 if (status == 0 && state != NULL)
1952 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001953 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001956static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1957 struct nfs_fattr *fattr, struct iattr *sattr,
1958 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001960 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001961 struct nfs4_exception exception = {
1962 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05001963 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001964 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 int err;
1966 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04001967 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
1968 switch (err) {
1969 case -NFS4ERR_OPENMODE:
1970 if (state && !(state->state & FMODE_WRITE)) {
1971 err = -EBADF;
1972 if (sattr->ia_valid & ATTR_OPEN)
1973 err = -EACCES;
1974 goto out;
1975 }
1976 }
1977 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04001979out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 return err;
1981}
1982
1983struct nfs4_closedata {
1984 struct inode *inode;
1985 struct nfs4_state *state;
1986 struct nfs_closeargs arg;
1987 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001988 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001989 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001990 bool roc;
1991 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992};
1993
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001994static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001995{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001996 struct nfs4_closedata *calldata = data;
1997 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001998 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001999
Fred Isamanf7e89172011-01-06 11:36:32 +00002000 if (calldata->roc)
2001 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002002 nfs4_put_open_state(calldata->state);
2003 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002004 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002005 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002006 kfree(calldata);
2007}
2008
Trond Myklebust88069f72009-12-08 08:33:16 -05002009static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2010 fmode_t fmode)
2011{
2012 spin_lock(&state->owner->so_lock);
2013 if (!(fmode & FMODE_READ))
2014 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2015 if (!(fmode & FMODE_WRITE))
2016 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2017 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2018 spin_unlock(&state->owner->so_lock);
2019}
2020
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002021static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002023 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 struct nfs_server *server = NFS_SERVER(calldata->inode);
2026
Chuck Levera3ca5652012-03-01 17:00:40 -05002027 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002028 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2029 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 /* hmm. we are done with the inode, and in the process of freeing
2031 * the state_owner. we keep this around to process errors
2032 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 switch (task->tk_status) {
2034 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002035 if (calldata->roc)
2036 pnfs_roc_set_barrier(state->inode,
2037 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002038 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002039 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002040 nfs4_close_clear_stateid_flags(state,
2041 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 break;
2043 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002044 case -NFS4ERR_OLD_STATEID:
2045 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002047 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002048 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002050 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2051 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002053 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002054 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002055 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056}
2057
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002058static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002060 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002061 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002062 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002063
Chuck Levera3ca5652012-03-01 17:00:40 -05002064 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002065 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002066 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002067
Trond Myklebust88069f72009-12-08 08:33:16 -05002068 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2069 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002070 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002071 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002072 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002073 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002074 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2075 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2076 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002077 }
2078 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002079 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2080 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2081 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002082 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002083 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002084 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002085
2086 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002087 /* Note: exit _without_ calling nfs4_close_done */
2088 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002089 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002090 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002091
Fred Isamanf7e89172011-01-06 11:36:32 +00002092 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002093 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002094 if (calldata->roc &&
2095 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2096 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2097 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002098 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002099 }
2100 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002101
Trond Myklebust516a6af2005-10-27 22:12:41 -04002102 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002103 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002104 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002105 &calldata->arg.seq_args,
2106 &calldata->res.seq_res,
Trond Myklebust0ada2102012-10-29 18:37:40 -04002107 task) != 0)
2108 nfs_release_seqid(calldata->arg.seqid);
2109 else
2110 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002111out:
2112 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002115static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002116 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002117 .rpc_call_done = nfs4_close_done,
2118 .rpc_release = nfs4_free_closedata,
2119};
2120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121/*
2122 * It is possible for data to be read/written from a mem-mapped file
2123 * after the sys_close call (which hits the vfs layer as a flush).
2124 * This means that we can't safely call nfsv4 close on a file until
2125 * the inode is cleared. This in turn means that we are not good
2126 * NFSv4 citizens - we do not indicate to the server to update the file's
2127 * share state even when we are done with one of the three share
2128 * stateid's in the inode.
2129 *
2130 * NOTE: Caller must be holding the sp->so_owner semaphore!
2131 */
Al Viro643168c2011-06-22 18:20:23 -04002132int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002134 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002136 struct nfs4_state_owner *sp = state->owner;
2137 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002138 struct rpc_message msg = {
2139 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2140 .rpc_cred = state->owner->so_cred,
2141 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002142 struct rpc_task_setup task_setup_data = {
2143 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002144 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002145 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002146 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002147 .flags = RPC_TASK_ASYNC,
2148 };
Trond Myklebust95121352005-10-18 14:20:12 -07002149 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002151 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002153 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002154 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002155 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002157 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002158 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002160 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002161 if (calldata->arg.seqid == NULL)
2162 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002163 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002164 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002165 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002166 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002167 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002168 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002169 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002170
Trond Myklebust1174dd12010-12-21 10:52:24 -05002171 msg.rpc_argp = &calldata->arg;
2172 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002173 task_setup_data.callback_data = calldata;
2174 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002175 if (IS_ERR(task))
2176 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002177 status = 0;
2178 if (wait)
2179 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002180 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002181 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002182out_free_calldata:
2183 kfree(calldata);
2184out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002185 if (roc)
2186 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002187 nfs4_put_open_state(state);
2188 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002189 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
2191
Trond Myklebust2b484292010-09-17 10:56:51 -04002192static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002193nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 struct nfs4_state *state;
2196
Trond Myklebust565277f2007-10-15 18:17:53 -04002197 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002198 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002199 if (IS_ERR(state))
2200 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002201 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002202 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203}
2204
Trond Myklebust1185a552009-12-03 15:54:02 -05002205static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002206{
2207 if (ctx->state == NULL)
2208 return;
2209 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002210 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002211 else
Al Viro643168c2011-06-22 18:20:23 -04002212 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002213}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
2215static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2216{
Benny Halevy43652ad2009-04-01 09:21:54 -04002217 struct nfs4_server_caps_arg args = {
2218 .fhandle = fhandle,
2219 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 struct nfs4_server_caps_res res = {};
2221 struct rpc_message msg = {
2222 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002223 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 .rpc_resp = &res,
2225 };
2226 int status;
2227
Bryan Schumaker7c513052011-03-24 17:12:24 +00002228 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if (status == 0) {
2230 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002231 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2232 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2233 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2234 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2235 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2237 server->caps |= NFS_CAP_ACLS;
2238 if (res.has_links != 0)
2239 server->caps |= NFS_CAP_HARDLINKS;
2240 if (res.has_symlinks != 0)
2241 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002242 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2243 server->caps |= NFS_CAP_FILEID;
2244 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2245 server->caps |= NFS_CAP_MODE;
2246 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2247 server->caps |= NFS_CAP_NLINK;
2248 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2249 server->caps |= NFS_CAP_OWNER;
2250 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2251 server->caps |= NFS_CAP_OWNER_GROUP;
2252 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2253 server->caps |= NFS_CAP_ATIME;
2254 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2255 server->caps |= NFS_CAP_CTIME;
2256 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2257 server->caps |= NFS_CAP_MTIME;
2258
Trond Myklebusta65318b2009-03-11 14:10:28 -04002259 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2260 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2261 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002263 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 return status;
2267}
2268
Trond Myklebust55a97592006-06-09 09:34:19 -04002269int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
2271 struct nfs4_exception exception = { };
2272 int err;
2273 do {
2274 err = nfs4_handle_exception(server,
2275 _nfs4_server_capabilities(server, fhandle),
2276 &exception);
2277 } while (exception.retry);
2278 return err;
2279}
2280
2281static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2282 struct nfs_fsinfo *info)
2283{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 struct nfs4_lookup_root_arg args = {
2285 .bitmask = nfs4_fattr_bitmap,
2286 };
2287 struct nfs4_lookup_res res = {
2288 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002289 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 .fh = fhandle,
2291 };
2292 struct rpc_message msg = {
2293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2294 .rpc_argp = &args,
2295 .rpc_resp = &res,
2296 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002297
Trond Myklebust0e574af2005-10-27 22:12:38 -04002298 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002299 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300}
2301
2302static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2303 struct nfs_fsinfo *info)
2304{
2305 struct nfs4_exception exception = { };
2306 int err;
2307 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002308 err = _nfs4_lookup_root(server, fhandle, info);
2309 switch (err) {
2310 case 0:
2311 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002312 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002313 default:
2314 err = nfs4_handle_exception(server, err, &exception);
2315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002317out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 return err;
2319}
2320
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002321static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2322 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2323{
2324 struct rpc_auth *auth;
2325 int ret;
2326
2327 auth = rpcauth_create(flavor, server->client);
2328 if (!auth) {
2329 ret = -EIO;
2330 goto out;
2331 }
2332 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002333out:
2334 return ret;
2335}
2336
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002337static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002338 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002340 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002341 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002343 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2344 flav_array[len] = RPC_AUTH_NULL;
2345 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002346
2347 for (i = 0; i < len; i++) {
2348 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002349 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002350 continue;
2351 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002352 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002353 /*
2354 * -EACCESS could mean that the user doesn't have correct permissions
2355 * to access the mount. It could also mean that we tried to mount
2356 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2357 * existing mount programs don't handle -EACCES very well so it should
2358 * be mapped to -EPERM instead.
2359 */
2360 if (status == -EACCES)
2361 status = -EPERM;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002362 return status;
2363}
2364
2365/*
2366 * get the file handle for the "/" directory on the server
2367 */
2368static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2369 struct nfs_fsinfo *info)
2370{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002371 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002372 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002373 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2374 /*
2375 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2376 * by nfs4_map_errors() as this function exits.
2377 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002378 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 if (status == 0)
2380 status = nfs4_server_capabilities(server, fhandle);
2381 if (status == 0)
2382 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002383 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384}
2385
Manoj Naik6b97fd32006-06-09 09:34:29 -04002386/*
2387 * Get locations and (maybe) other attributes of a referral.
2388 * Note that we'll actually follow the referral later when
2389 * we detect fsid mismatch in inode revalidation
2390 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002391static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2392 const struct qstr *name, struct nfs_fattr *fattr,
2393 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002394{
2395 int status = -ENOMEM;
2396 struct page *page = NULL;
2397 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002398
2399 page = alloc_page(GFP_KERNEL);
2400 if (page == NULL)
2401 goto out;
2402 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2403 if (locations == NULL)
2404 goto out;
2405
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002406 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002407 if (status != 0)
2408 goto out;
2409 /* Make sure server returned a different fsid for the referral */
2410 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002411 dprintk("%s: server did not return a different fsid for"
2412 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002413 status = -EIO;
2414 goto out;
2415 }
Andy Adamson533eb462011-06-13 18:25:56 -04002416 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2417 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002418
Andy Adamson533eb462011-06-13 18:25:56 -04002419 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002420 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002421 memset(fhandle, 0, sizeof(struct nfs_fh));
2422out:
2423 if (page)
2424 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002425 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002426 return status;
2427}
2428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2430{
2431 struct nfs4_getattr_arg args = {
2432 .fh = fhandle,
2433 .bitmask = server->attr_bitmask,
2434 };
2435 struct nfs4_getattr_res res = {
2436 .fattr = fattr,
2437 .server = server,
2438 };
2439 struct rpc_message msg = {
2440 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2441 .rpc_argp = &args,
2442 .rpc_resp = &res,
2443 };
2444
Trond Myklebust0e574af2005-10-27 22:12:38 -04002445 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002446 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447}
2448
2449static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2450{
2451 struct nfs4_exception exception = { };
2452 int err;
2453 do {
2454 err = nfs4_handle_exception(server,
2455 _nfs4_proc_getattr(server, fhandle, fattr),
2456 &exception);
2457 } while (exception.retry);
2458 return err;
2459}
2460
2461/*
2462 * The file is not closed if it is opened due to the a request to change
2463 * the size of the file. The open call will not be needed once the
2464 * VFS layer lookup-intents are implemented.
2465 *
2466 * Close is called when the inode is destroyed.
2467 * If we haven't opened the file for O_WRONLY, we
2468 * need to in the size_change case to obtain a stateid.
2469 *
2470 * Got race?
2471 * Because OPEN is always done by name in nfsv4, it is
2472 * possible that we opened a different file by the same
2473 * name. We can recognize this race condition, but we
2474 * can't do anything about it besides returning an error.
2475 *
2476 * This will be fixed with VFS changes (lookup-intent).
2477 */
2478static int
2479nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2480 struct iattr *sattr)
2481{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002482 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002483 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002484 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 int status;
2486
Benny Halevy8a1636c2010-07-14 15:43:57 -04002487 if (pnfs_ld_layoutret_on_setattr(inode))
2488 pnfs_return_layout(inode);
2489
Trond Myklebust0e574af2005-10-27 22:12:38 -04002490 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Trond Myklebustd5308382005-11-04 15:33:38 -05002492 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002493 if (sattr->ia_valid & ATTR_FILE) {
2494 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002495
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002496 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002497 if (ctx) {
2498 cred = ctx->cred;
2499 state = ctx->state;
2500 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002501 }
2502
Trond Myklebust536e43d2012-01-17 22:04:26 -05002503 /* Deal with open(O_TRUNC) */
2504 if (sattr->ia_valid & ATTR_OPEN)
2505 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2506
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002507 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002508 if (status == 0)
2509 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 return status;
2511}
2512
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002513static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2514 const struct qstr *name, struct nfs_fh *fhandle,
2515 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002516{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002517 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002518 int status;
2519 struct nfs4_lookup_arg args = {
2520 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002521 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002522 .name = name,
2523 };
2524 struct nfs4_lookup_res res = {
2525 .server = server,
2526 .fattr = fattr,
2527 .fh = fhandle,
2528 };
2529 struct rpc_message msg = {
2530 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2531 .rpc_argp = &args,
2532 .rpc_resp = &res,
2533 };
2534
2535 nfs_fattr_init(fattr);
2536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002538 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 dprintk("NFS reply lookup: %d\n", status);
2540 return status;
2541}
2542
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002543static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002544{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002545 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002546 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002547 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2548 fattr->nlink = 2;
2549}
2550
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002551static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2552 struct qstr *name, struct nfs_fh *fhandle,
2553 struct nfs_fattr *fattr)
2554{
2555 struct nfs4_exception exception = { };
2556 struct rpc_clnt *client = *clnt;
2557 int err;
2558 do {
2559 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2560 switch (err) {
2561 case -NFS4ERR_BADNAME:
2562 err = -ENOENT;
2563 goto out;
2564 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002565 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002566 goto out;
2567 case -NFS4ERR_WRONGSEC:
2568 err = -EPERM;
2569 if (client != *clnt)
2570 goto out;
2571
2572 client = nfs4_create_sec_client(client, dir, name);
2573 if (IS_ERR(client))
2574 return PTR_ERR(client);
2575
2576 exception.retry = 1;
2577 break;
2578 default:
2579 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2580 }
2581 } while (exception.retry);
2582
2583out:
2584 if (err == 0)
2585 *clnt = client;
2586 else if (client != *clnt)
2587 rpc_shutdown_client(client);
2588
2589 return err;
2590}
2591
Bryan Schumaker7c513052011-03-24 17:12:24 +00002592static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2593 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002595 int status;
2596 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002597
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002598 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2599 if (client != NFS_CLIENT(dir)) {
2600 rpc_shutdown_client(client);
2601 nfs_fixup_secinfo_attributes(fattr);
2602 }
2603 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604}
2605
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002606struct rpc_clnt *
2607nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2608 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2609{
2610 int status;
2611 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2612
2613 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2614 if (status < 0) {
2615 rpc_shutdown_client(client);
2616 return ERR_PTR(status);
2617 }
2618 return client;
2619}
2620
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2622{
Trond Myklebust76b32992007-08-10 17:45:11 -04002623 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 struct nfs4_accessargs args = {
2625 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002626 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002628 struct nfs4_accessres res = {
2629 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002630 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 struct rpc_message msg = {
2632 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2633 .rpc_argp = &args,
2634 .rpc_resp = &res,
2635 .rpc_cred = entry->cred,
2636 };
2637 int mode = entry->mask;
2638 int status;
2639
2640 /*
2641 * Determine which access bits we want to ask for...
2642 */
2643 if (mode & MAY_READ)
2644 args.access |= NFS4_ACCESS_READ;
2645 if (S_ISDIR(inode->i_mode)) {
2646 if (mode & MAY_WRITE)
2647 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2648 if (mode & MAY_EXEC)
2649 args.access |= NFS4_ACCESS_LOOKUP;
2650 } else {
2651 if (mode & MAY_WRITE)
2652 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2653 if (mode & MAY_EXEC)
2654 args.access |= NFS4_ACCESS_EXECUTE;
2655 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002656
2657 res.fattr = nfs_alloc_fattr();
2658 if (res.fattr == NULL)
2659 return -ENOMEM;
2660
Bryan Schumaker7c513052011-03-24 17:12:24 +00002661 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 if (!status) {
2663 entry->mask = 0;
2664 if (res.access & NFS4_ACCESS_READ)
2665 entry->mask |= MAY_READ;
2666 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2667 entry->mask |= MAY_WRITE;
2668 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2669 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002670 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002672 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 return status;
2674}
2675
2676static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2677{
2678 struct nfs4_exception exception = { };
2679 int err;
2680 do {
2681 err = nfs4_handle_exception(NFS_SERVER(inode),
2682 _nfs4_proc_access(inode, entry),
2683 &exception);
2684 } while (exception.retry);
2685 return err;
2686}
2687
2688/*
2689 * TODO: For the time being, we don't try to get any attributes
2690 * along with any of the zero-copy operations READ, READDIR,
2691 * READLINK, WRITE.
2692 *
2693 * In the case of the first three, we want to put the GETATTR
2694 * after the read-type operation -- this is because it is hard
2695 * to predict the length of a GETATTR response in v4, and thus
2696 * align the READ data correctly. This means that the GETATTR
2697 * may end up partially falling into the page cache, and we should
2698 * shift it into the 'tail' of the xdr_buf before processing.
2699 * To do this efficiently, we need to know the total length
2700 * of data received, which doesn't seem to be available outside
2701 * of the RPC layer.
2702 *
2703 * In the case of WRITE, we also want to put the GETATTR after
2704 * the operation -- in this case because we want to make sure
2705 * we get the post-operation mtime and size. This means that
2706 * we can't use xdr_encode_pages() as written: we need a variant
2707 * of it which would leave room in the 'tail' iovec.
2708 *
2709 * Both of these changes to the XDR layer would in fact be quite
2710 * minor, but I decided to leave them for a subsequent patch.
2711 */
2712static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2713 unsigned int pgbase, unsigned int pglen)
2714{
2715 struct nfs4_readlink args = {
2716 .fh = NFS_FH(inode),
2717 .pgbase = pgbase,
2718 .pglen = pglen,
2719 .pages = &page,
2720 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002721 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 struct rpc_message msg = {
2723 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2724 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002725 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 };
2727
Bryan Schumaker7c513052011-03-24 17:12:24 +00002728 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 -07002729}
2730
2731static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2732 unsigned int pgbase, unsigned int pglen)
2733{
2734 struct nfs4_exception exception = { };
2735 int err;
2736 do {
2737 err = nfs4_handle_exception(NFS_SERVER(inode),
2738 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2739 &exception);
2740 } while (exception.retry);
2741 return err;
2742}
2743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744/*
2745 * Got race?
2746 * We will need to arrange for the VFS layer to provide an atomic open.
2747 * Until then, this create/open method is prone to inefficiency and race
2748 * conditions due to the lookup, create, and open VFS calls from sys_open()
2749 * placed on the wire.
2750 *
2751 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2752 * The file will be opened again in the subsequent VFS open call
2753 * (nfs4_proc_file_open).
2754 *
2755 * The open for read will just hang around to be used by any process that
2756 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2757 */
2758
2759static int
2760nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002761 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762{
Al Viro82a2c1b2011-06-22 18:30:55 -04002763 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002765 struct rpc_cred *cred = NULL;
2766 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 int status = 0;
2768
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002769 if (ctx != NULL) {
2770 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002771 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002772 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002774 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002775 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002776 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 if (IS_ERR(state)) {
2778 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002779 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002781 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002782 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002783 if (ctx != NULL)
2784 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002785 else
Al Viro643168c2011-06-22 18:20:23 -04002786 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787out:
2788 return status;
2789}
2790
2791static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2792{
Trond Myklebust16e42952005-10-27 22:12:44 -04002793 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002794 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002796 .name.len = name->len,
2797 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002798 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002800 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002801 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002802 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2805 .rpc_argp = &args,
2806 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002808 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Trond Myklebustd3468902010-04-16 16:22:50 -04002810 res.dir_attr = nfs_alloc_fattr();
2811 if (res.dir_attr == NULL)
2812 goto out;
2813
Bryan Schumaker7c513052011-03-24 17:12:24 +00002814 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002815 if (status == 0) {
2816 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002817 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002818 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002819 nfs_free_fattr(res.dir_attr);
2820out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 return status;
2822}
2823
2824static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2825{
2826 struct nfs4_exception exception = { };
2827 int err;
2828 do {
2829 err = nfs4_handle_exception(NFS_SERVER(dir),
2830 _nfs4_proc_remove(dir, name),
2831 &exception);
2832 } while (exception.retry);
2833 return err;
2834}
2835
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002836static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002838 struct nfs_server *server = NFS_SERVER(dir);
2839 struct nfs_removeargs *args = msg->rpc_argp;
2840 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
Trond Myklebusta65318b2009-03-11 14:10:28 -04002842 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002843 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002845 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846}
2847
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002848static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2849{
2850 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2851 &data->args.seq_args,
2852 &data->res.seq_res,
2853 task))
2854 return;
2855 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856}
2857
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002858static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002860 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2861
Trond Myklebust14516c32010-07-31 14:29:06 -04002862 if (!nfs4_sequence_done(task, &res->seq_res))
2863 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002864 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002865 return 0;
2866 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002867 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002868 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869}
2870
Jeff Laytond3d41522010-09-17 17:31:57 -04002871static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2872{
2873 struct nfs_server *server = NFS_SERVER(dir);
2874 struct nfs_renameargs *arg = msg->rpc_argp;
2875 struct nfs_renameres *res = msg->rpc_resp;
2876
2877 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2878 arg->bitmask = server->attr_bitmask;
2879 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002880 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002881}
2882
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002883static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2884{
2885 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2886 &data->args.seq_args,
2887 &data->res.seq_res,
2888 task))
2889 return;
2890 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002891}
2892
2893static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2894 struct inode *new_dir)
2895{
2896 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2897
2898 if (!nfs4_sequence_done(task, &res->seq_res))
2899 return 0;
2900 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2901 return 0;
2902
2903 update_changeattr(old_dir, &res->old_cinfo);
2904 nfs_post_op_update_inode(old_dir, res->old_fattr);
2905 update_changeattr(new_dir, &res->new_cinfo);
2906 nfs_post_op_update_inode(new_dir, res->new_fattr);
2907 return 1;
2908}
2909
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2911 struct inode *new_dir, struct qstr *new_name)
2912{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002913 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002914 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 .old_dir = NFS_FH(old_dir),
2916 .new_dir = NFS_FH(new_dir),
2917 .old_name = old_name,
2918 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002919 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002921 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002922 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002923 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 struct rpc_message msg = {
2925 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2926 .rpc_argp = &arg,
2927 .rpc_resp = &res,
2928 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002929 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Trond Myklebust011fff72010-04-16 16:22:49 -04002931 res.old_fattr = nfs_alloc_fattr();
2932 res.new_fattr = nfs_alloc_fattr();
2933 if (res.old_fattr == NULL || res.new_fattr == NULL)
2934 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
Bryan Schumaker7c513052011-03-24 17:12:24 +00002936 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 if (!status) {
2938 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002939 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002941 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002943out:
2944 nfs_free_fattr(res.new_fattr);
2945 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 return status;
2947}
2948
2949static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2950 struct inode *new_dir, struct qstr *new_name)
2951{
2952 struct nfs4_exception exception = { };
2953 int err;
2954 do {
2955 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2956 _nfs4_proc_rename(old_dir, old_name,
2957 new_dir, new_name),
2958 &exception);
2959 } while (exception.retry);
2960 return err;
2961}
2962
2963static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2964{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002965 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 struct nfs4_link_arg arg = {
2967 .fh = NFS_FH(inode),
2968 .dir_fh = NFS_FH(dir),
2969 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002970 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002972 struct nfs4_link_res res = {
2973 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002974 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 struct rpc_message msg = {
2976 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2977 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002978 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002980 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Trond Myklebust136f2622010-04-16 16:22:49 -04002982 res.fattr = nfs_alloc_fattr();
2983 res.dir_attr = nfs_alloc_fattr();
2984 if (res.fattr == NULL || res.dir_attr == NULL)
2985 goto out;
2986
Bryan Schumaker7c513052011-03-24 17:12:24 +00002987 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002988 if (!status) {
2989 update_changeattr(dir, &res.cinfo);
2990 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002991 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002992 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002993out:
2994 nfs_free_fattr(res.dir_attr);
2995 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 return status;
2997}
2998
2999static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3000{
3001 struct nfs4_exception exception = { };
3002 int err;
3003 do {
3004 err = nfs4_handle_exception(NFS_SERVER(inode),
3005 _nfs4_proc_link(inode, dir, name),
3006 &exception);
3007 } while (exception.retry);
3008 return err;
3009}
3010
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003011struct nfs4_createdata {
3012 struct rpc_message msg;
3013 struct nfs4_create_arg arg;
3014 struct nfs4_create_res res;
3015 struct nfs_fh fh;
3016 struct nfs_fattr fattr;
3017 struct nfs_fattr dir_fattr;
3018};
3019
3020static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3021 struct qstr *name, struct iattr *sattr, u32 ftype)
3022{
3023 struct nfs4_createdata *data;
3024
3025 data = kzalloc(sizeof(*data), GFP_KERNEL);
3026 if (data != NULL) {
3027 struct nfs_server *server = NFS_SERVER(dir);
3028
3029 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3030 data->msg.rpc_argp = &data->arg;
3031 data->msg.rpc_resp = &data->res;
3032 data->arg.dir_fh = NFS_FH(dir);
3033 data->arg.server = server;
3034 data->arg.name = name;
3035 data->arg.attrs = sattr;
3036 data->arg.ftype = ftype;
3037 data->arg.bitmask = server->attr_bitmask;
3038 data->res.server = server;
3039 data->res.fh = &data->fh;
3040 data->res.fattr = &data->fattr;
3041 data->res.dir_fattr = &data->dir_fattr;
3042 nfs_fattr_init(data->res.fattr);
3043 nfs_fattr_init(data->res.dir_fattr);
3044 }
3045 return data;
3046}
3047
3048static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3049{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003050 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003051 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003052 if (status == 0) {
3053 update_changeattr(dir, &data->res.dir_cinfo);
3054 nfs_post_op_update_inode(dir, data->res.dir_fattr);
3055 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3056 }
3057 return status;
3058}
3059
3060static void nfs4_free_createdata(struct nfs4_createdata *data)
3061{
3062 kfree(data);
3063}
3064
Chuck Lever4f390c12006-08-22 20:06:22 -04003065static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003066 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003068 struct nfs4_createdata *data;
3069 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
Chuck Lever94a6d752006-08-22 20:06:23 -04003071 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003072 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003073
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003074 status = -ENOMEM;
3075 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3076 if (data == NULL)
3077 goto out;
3078
3079 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3080 data->arg.u.symlink.pages = &page;
3081 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003083 status = nfs4_do_create(dir, dentry, data);
3084
3085 nfs4_free_createdata(data);
3086out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 return status;
3088}
3089
Chuck Lever4f390c12006-08-22 20:06:22 -04003090static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003091 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092{
3093 struct nfs4_exception exception = { };
3094 int err;
3095 do {
3096 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003097 _nfs4_proc_symlink(dir, dentry, page,
3098 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 &exception);
3100 } while (exception.retry);
3101 return err;
3102}
3103
3104static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3105 struct iattr *sattr)
3106{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003107 struct nfs4_createdata *data;
3108 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003110 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3111 if (data == NULL)
3112 goto out;
3113
3114 status = nfs4_do_create(dir, dentry, data);
3115
3116 nfs4_free_createdata(data);
3117out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 return status;
3119}
3120
3121static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3122 struct iattr *sattr)
3123{
3124 struct nfs4_exception exception = { };
3125 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003126
3127 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 do {
3129 err = nfs4_handle_exception(NFS_SERVER(dir),
3130 _nfs4_proc_mkdir(dir, dentry, sattr),
3131 &exception);
3132 } while (exception.retry);
3133 return err;
3134}
3135
3136static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003137 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138{
3139 struct inode *dir = dentry->d_inode;
3140 struct nfs4_readdir_arg args = {
3141 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003142 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 .pgbase = 0,
3144 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003145 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003146 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 };
3148 struct nfs4_readdir_res res;
3149 struct rpc_message msg = {
3150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3151 .rpc_argp = &args,
3152 .rpc_resp = &res,
3153 .rpc_cred = cred,
3154 };
3155 int status;
3156
Harvey Harrison3110ff82008-05-02 13:42:44 -07003157 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003158 dentry->d_parent->d_name.name,
3159 dentry->d_name.name,
3160 (unsigned long long)cookie);
Trond Myklebustf15e7242012-09-03 14:56:02 -04003161 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003163 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 -05003164 if (status >= 0) {
Trond Myklebustf15e7242012-09-03 14:56:02 -04003165 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003166 status += args.pgbase;
3167 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003168
3169 nfs_invalidate_atime(dir);
3170
Harvey Harrison3110ff82008-05-02 13:42:44 -07003171 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 return status;
3173}
3174
3175static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003176 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177{
3178 struct nfs4_exception exception = { };
3179 int err;
3180 do {
3181 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3182 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003183 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 &exception);
3185 } while (exception.retry);
3186 return err;
3187}
3188
3189static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3190 struct iattr *sattr, dev_t rdev)
3191{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003192 struct nfs4_createdata *data;
3193 int mode = sattr->ia_mode;
3194 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
3196 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3197 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003198
3199 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3200 if (data == NULL)
3201 goto out;
3202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003204 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003206 data->arg.ftype = NF4BLK;
3207 data->arg.u.device.specdata1 = MAJOR(rdev);
3208 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 }
3210 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003211 data->arg.ftype = NF4CHR;
3212 data->arg.u.device.specdata1 = MAJOR(rdev);
3213 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003216 status = nfs4_do_create(dir, dentry, data);
3217
3218 nfs4_free_createdata(data);
3219out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 return status;
3221}
3222
3223static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3224 struct iattr *sattr, dev_t rdev)
3225{
3226 struct nfs4_exception exception = { };
3227 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003228
3229 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 do {
3231 err = nfs4_handle_exception(NFS_SERVER(dir),
3232 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3233 &exception);
3234 } while (exception.retry);
3235 return err;
3236}
3237
3238static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3239 struct nfs_fsstat *fsstat)
3240{
3241 struct nfs4_statfs_arg args = {
3242 .fh = fhandle,
3243 .bitmask = server->attr_bitmask,
3244 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003245 struct nfs4_statfs_res res = {
3246 .fsstat = fsstat,
3247 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 struct rpc_message msg = {
3249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3250 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003251 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 };
3253
Trond Myklebust0e574af2005-10-27 22:12:38 -04003254 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003255 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256}
3257
3258static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3259{
3260 struct nfs4_exception exception = { };
3261 int err;
3262 do {
3263 err = nfs4_handle_exception(server,
3264 _nfs4_proc_statfs(server, fhandle, fsstat),
3265 &exception);
3266 } while (exception.retry);
3267 return err;
3268}
3269
3270static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3271 struct nfs_fsinfo *fsinfo)
3272{
3273 struct nfs4_fsinfo_arg args = {
3274 .fh = fhandle,
3275 .bitmask = server->attr_bitmask,
3276 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003277 struct nfs4_fsinfo_res res = {
3278 .fsinfo = fsinfo,
3279 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 struct rpc_message msg = {
3281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3282 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003283 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 };
3285
Bryan Schumaker7c513052011-03-24 17:12:24 +00003286 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287}
3288
3289static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3290{
3291 struct nfs4_exception exception = { };
3292 int err;
3293
3294 do {
3295 err = nfs4_handle_exception(server,
3296 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3297 &exception);
3298 } while (exception.retry);
3299 return err;
3300}
3301
3302static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3303{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003304 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3306}
3307
3308static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3309 struct nfs_pathconf *pathconf)
3310{
3311 struct nfs4_pathconf_arg args = {
3312 .fh = fhandle,
3313 .bitmask = server->attr_bitmask,
3314 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003315 struct nfs4_pathconf_res res = {
3316 .pathconf = pathconf,
3317 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 struct rpc_message msg = {
3319 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3320 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003321 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 };
3323
3324 /* None of the pathconf attributes are mandatory to implement */
3325 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3326 memset(pathconf, 0, sizeof(*pathconf));
3327 return 0;
3328 }
3329
Trond Myklebust0e574af2005-10-27 22:12:38 -04003330 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003331 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332}
3333
3334static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3335 struct nfs_pathconf *pathconf)
3336{
3337 struct nfs4_exception exception = { };
3338 int err;
3339
3340 do {
3341 err = nfs4_handle_exception(server,
3342 _nfs4_proc_pathconf(server, fhandle, pathconf),
3343 &exception);
3344 } while (exception.retry);
3345 return err;
3346}
3347
Benny Halevyd20581a2011-05-22 19:52:03 +03003348void __nfs4_read_done_cb(struct nfs_read_data *data)
3349{
3350 nfs_invalidate_atime(data->inode);
3351}
3352
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003353static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354{
Trond Myklebustec06c092006-03-20 13:44:27 -05003355 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003357 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003358 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003359 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003361
Benny Halevyd20581a2011-05-22 19:52:03 +03003362 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003364 renew_lease(server, data->timestamp);
3365 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366}
3367
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003368static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3369{
3370
3371 dprintk("--> %s\n", __func__);
3372
3373 if (!nfs4_sequence_done(task, &data->res.seq_res))
3374 return -EAGAIN;
3375
Benny Halevyd20581a2011-05-22 19:52:03 +03003376 return data->read_done_cb ? data->read_done_cb(task, data) :
3377 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003378}
3379
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003380static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003383 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003384 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003385 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386}
3387
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003388static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3389{
3390 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3391 &data->args.seq_args,
3392 &data->res.seq_res,
3393 task))
3394 return;
3395 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396}
3397
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003398/* Reset the the nfs_read_data to send the read to the MDS. */
3399void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3400{
3401 dprintk("%s Reset task for i/o through\n", __func__);
3402 put_lseg(data->lseg);
3403 data->lseg = NULL;
3404 /* offsets will differ in the dense stripe case */
3405 data->args.offset = data->mds_offset;
3406 data->ds_clp = NULL;
3407 data->args.fh = NFS_FH(data->inode);
3408 data->read_done_cb = nfs4_read_done_cb;
3409 task->tk_ops = data->mds_ops;
3410 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3411}
3412EXPORT_SYMBOL_GPL(nfs4_reset_read);
3413
Fred Isamanb029bc92011-03-03 15:13:42 +00003414static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 struct inode *inode = data->inode;
3417
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003418 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003419 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003420 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003422 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003424 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003425 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003426 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427}
3428
Fred Isamanb029bc92011-03-03 15:13:42 +00003429static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3430{
3431 if (!nfs4_sequence_done(task, &data->res.seq_res))
3432 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003433 return data->write_done_cb ? data->write_done_cb(task, data) :
3434 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003435}
3436
Fred Isamana69aef12011-03-03 15:13:47 +00003437/* Reset the the nfs_write_data to send the write to the MDS. */
3438void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3439{
3440 dprintk("%s Reset task for i/o through\n", __func__);
3441 put_lseg(data->lseg);
3442 data->lseg = NULL;
3443 data->ds_clp = NULL;
3444 data->write_done_cb = nfs4_write_done_cb;
3445 data->args.fh = NFS_FH(data->inode);
3446 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3447 data->args.offset = data->mds_offset;
3448 data->res.fattr = &data->fattr;
3449 task->tk_ops = data->mds_ops;
3450 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3451}
3452EXPORT_SYMBOL_GPL(nfs4_reset_write);
3453
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003454static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003456 struct nfs_server *server = NFS_SERVER(data->inode);
3457
Fred Isaman7ffd1062011-03-03 15:13:46 +00003458 if (data->lseg) {
3459 data->args.bitmask = NULL;
3460 data->res.fattr = NULL;
3461 } else
3462 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003463 if (!data->write_done_cb)
3464 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003465 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 data->timestamp = jiffies;
3467
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003468 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003469 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470}
3471
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003472static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3473{
3474 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3475 &data->args.seq_args,
3476 &data->res.seq_res,
3477 task))
3478 return;
3479 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480}
3481
Fred Isaman5f452432011-03-23 13:27:46 +00003482static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003485
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003486 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003487 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003488 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003490 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003491 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492}
3493
Fred Isaman5f452432011-03-23 13:27:46 +00003494static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3495{
3496 if (!nfs4_sequence_done(task, &data->res.seq_res))
3497 return -EAGAIN;
3498 return data->write_done_cb(task, data);
3499}
3500
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003501static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003503 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003504
3505 if (data->lseg) {
3506 data->args.bitmask = NULL;
3507 data->res.fattr = NULL;
3508 } else
3509 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003510 if (!data->write_done_cb)
3511 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003512 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003513 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003514 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515}
3516
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003517struct nfs4_renewdata {
3518 struct nfs_client *client;
3519 unsigned long timestamp;
3520};
3521
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522/*
3523 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3524 * standalone procedure for queueing an asynchronous RENEW.
3525 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003526static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003527{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003528 struct nfs4_renewdata *data = calldata;
3529 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003530
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003531 if (atomic_read(&clp->cl_count) > 1)
3532 nfs4_schedule_state_renewal(clp);
3533 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003534 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003535}
3536
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003537static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003539 struct nfs4_renewdata *data = calldata;
3540 struct nfs_client *clp = data->client;
3541 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
3543 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003544 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003545 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3546 return;
3547 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003548 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003549 return;
3550 }
3551 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003553 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554}
3555
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003556static const struct rpc_call_ops nfs4_renew_ops = {
3557 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003558 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003559};
3560
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003561static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562{
3563 struct rpc_message msg = {
3564 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3565 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003566 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003568 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003570 if (renew_flags == 0)
3571 return 0;
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003572 if (!atomic_inc_not_zero(&clp->cl_count))
3573 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003574 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003575 if (data == NULL)
3576 return -ENOMEM;
3577 data->client = clp;
3578 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003580 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581}
3582
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003583static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584{
3585 struct rpc_message msg = {
3586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3587 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003588 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 };
3590 unsigned long now = jiffies;
3591 int status;
3592
3593 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3594 if (status < 0)
3595 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003596 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 return 0;
3598}
3599
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003600static inline int nfs4_server_supports_acls(struct nfs_server *server)
3601{
3602 return (server->caps & NFS_CAP_ACLS)
3603 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3604 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3605}
3606
3607/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3608 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3609 * the stack.
3610 */
3611#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3612
Neil Hormane9e3d722011-03-04 19:26:03 -05003613static int buf_to_pages_noslab(const void *buf, size_t buflen,
3614 struct page **pages, unsigned int *pgbase)
3615{
3616 struct page *newpage, **spages;
3617 int rc = 0;
3618 size_t len;
3619 spages = pages;
3620
3621 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003622 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003623 newpage = alloc_page(GFP_KERNEL);
3624
3625 if (newpage == NULL)
3626 goto unwind;
3627 memcpy(page_address(newpage), buf, len);
3628 buf += len;
3629 buflen -= len;
3630 *pages++ = newpage;
3631 rc++;
3632 } while (buflen != 0);
3633
3634 return rc;
3635
3636unwind:
3637 for(; rc > 0; rc--)
3638 __free_page(spages[rc-1]);
3639 return -ENOMEM;
3640}
3641
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003642struct nfs4_cached_acl {
3643 int cached;
3644 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003645 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003646};
3647
3648static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003649{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003650 struct nfs_inode *nfsi = NFS_I(inode);
3651
3652 spin_lock(&inode->i_lock);
3653 kfree(nfsi->nfs4_acl);
3654 nfsi->nfs4_acl = acl;
3655 spin_unlock(&inode->i_lock);
3656}
3657
3658static void nfs4_zap_acl_attr(struct inode *inode)
3659{
3660 nfs4_set_cached_acl(inode, NULL);
3661}
3662
3663static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3664{
3665 struct nfs_inode *nfsi = NFS_I(inode);
3666 struct nfs4_cached_acl *acl;
3667 int ret = -ENOENT;
3668
3669 spin_lock(&inode->i_lock);
3670 acl = nfsi->nfs4_acl;
3671 if (acl == NULL)
3672 goto out;
3673 if (buf == NULL) /* user is just asking for length */
3674 goto out_len;
3675 if (acl->cached == 0)
3676 goto out;
3677 ret = -ERANGE; /* see getxattr(2) man page */
3678 if (acl->len > buflen)
3679 goto out;
3680 memcpy(buf, acl->data, acl->len);
3681out_len:
3682 ret = acl->len;
3683out:
3684 spin_unlock(&inode->i_lock);
3685 return ret;
3686}
3687
Sachin Prabhu5794d212012-04-17 14:36:40 +01003688static 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 +00003689{
3690 struct nfs4_cached_acl *acl;
3691
Sachin Prabhu5794d212012-04-17 14:36:40 +01003692 if (pages && acl_len <= PAGE_SIZE) {
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003693 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3694 if (acl == NULL)
3695 goto out;
3696 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003697 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003698 } else {
3699 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3700 if (acl == NULL)
3701 goto out;
3702 acl->cached = 0;
3703 }
3704 acl->len = acl_len;
3705out:
3706 nfs4_set_cached_acl(inode, acl);
3707}
3708
Andy Adamsonbf118a32011-12-07 11:55:27 -05003709/*
3710 * The getxattr API returns the required buffer length when called with a
3711 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3712 * the required buf. On a NULL buf, we send a page of data to the server
3713 * guessing that the ACL request can be serviced by a page. If so, we cache
3714 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3715 * the cache. If not so, we throw away the page, and cache the required
3716 * length. The next getxattr call will then produce another round trip to
3717 * the server, this time with the input buf of the required size.
3718 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003719static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003720{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003721 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003722 struct nfs_getaclargs args = {
3723 .fh = NFS_FH(inode),
3724 .acl_pages = pages,
3725 .acl_len = buflen,
3726 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003727 struct nfs_getaclres res = {
3728 .acl_len = buflen,
3729 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003730 struct rpc_message msg = {
3731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3732 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003733 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003734 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003735 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003736
Andy Adamsonbf118a32011-12-07 11:55:27 -05003737 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3738 /* As long as we're doing a round trip to the server anyway,
3739 * let's be prepared for a page of acl data. */
3740 if (npages == 0)
3741 npages = 1;
3742
Sachin Prabhu5a006892012-04-17 14:35:39 +01003743 /* Add an extra page to handle the bitmap returned */
3744 npages++;
3745
Andy Adamsonbf118a32011-12-07 11:55:27 -05003746 for (i = 0; i < npages; i++) {
3747 pages[i] = alloc_page(GFP_KERNEL);
3748 if (!pages[i])
3749 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003750 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003751
3752 /* for decoding across pages */
3753 res.acl_scratch = alloc_page(GFP_KERNEL);
3754 if (!res.acl_scratch)
3755 goto out_free;
3756
Andy Adamsonbf118a32011-12-07 11:55:27 -05003757 args.acl_len = npages * PAGE_SIZE;
3758 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003759
Andy Adamsonbf118a32011-12-07 11:55:27 -05003760 /* Let decode_getfacl know not to fail if the ACL data is larger than
3761 * the page we send as a guess */
3762 if (buf == NULL)
3763 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003764
Peng Taode040be2012-01-10 22:42:47 +08003765 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003766 __func__, buf, buflen, npages, args.acl_len);
3767 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3768 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003769 if (ret)
3770 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003771
3772 acl_len = res.acl_len - res.acl_data_offset;
3773 if (acl_len > args.acl_len)
Sachin Prabhu5794d212012-04-17 14:36:40 +01003774 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003775 else
Sachin Prabhu5794d212012-04-17 14:36:40 +01003776 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
Andy Adamsonbf118a32011-12-07 11:55:27 -05003777 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003778 if (buf) {
3779 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003780 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003781 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003782 _copy_from_pages(buf, pages, res.acl_data_offset,
Sachin Prabhu20e0fa92012-03-22 16:46:28 +00003783 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003784 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003785 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003786out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003787 for (i = 0; i < npages; i++)
3788 if (pages[i])
3789 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003790 if (res.acl_scratch)
3791 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003792 return ret;
3793}
3794
Trond Myklebust16b42892006-08-24 12:27:15 -04003795static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3796{
3797 struct nfs4_exception exception = { };
3798 ssize_t ret;
3799 do {
3800 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3801 if (ret >= 0)
3802 break;
3803 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3804 } while (exception.retry);
3805 return ret;
3806}
3807
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003808static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3809{
3810 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003811 int ret;
3812
3813 if (!nfs4_server_supports_acls(server))
3814 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003815 ret = nfs_revalidate_inode(server, inode);
3816 if (ret < 0)
3817 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003818 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3819 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003820 ret = nfs4_read_cached_acl(inode, buf, buflen);
3821 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003822 /* -ENOENT is returned if there is no ACL or if there is an ACL
3823 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003824 return ret;
3825 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003826}
3827
Trond Myklebust16b42892006-08-24 12:27:15 -04003828static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003829{
3830 struct nfs_server *server = NFS_SERVER(inode);
3831 struct page *pages[NFS4ACL_MAXPAGES];
3832 struct nfs_setaclargs arg = {
3833 .fh = NFS_FH(inode),
3834 .acl_pages = pages,
3835 .acl_len = buflen,
3836 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003837 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003838 struct rpc_message msg = {
3839 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3840 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003841 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003842 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003843 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003844
3845 if (!nfs4_server_supports_acls(server))
3846 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003847 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3848 if (i < 0)
3849 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003850 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003851 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003852
3853 /*
3854 * Free each page after tx, so the only ref left is
3855 * held by the network stack
3856 */
3857 for (; i > 0; i--)
3858 put_page(pages[i-1]);
3859
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003860 /*
3861 * Acl update can result in inode attribute update.
3862 * so mark the attribute cache invalid.
3863 */
3864 spin_lock(&inode->i_lock);
3865 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3866 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003867 nfs_access_zap_cache(inode);
3868 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003869 return ret;
3870}
3871
Trond Myklebust16b42892006-08-24 12:27:15 -04003872static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3873{
3874 struct nfs4_exception exception = { };
3875 int err;
3876 do {
3877 err = nfs4_handle_exception(NFS_SERVER(inode),
3878 __nfs4_proc_set_acl(inode, buf, buflen),
3879 &exception);
3880 } while (exception.retry);
3881 return err;
3882}
3883
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003885nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003887 struct nfs_client *clp = server->nfs_client;
3888
3889 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 return 0;
3891 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003892 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003893 case -NFS4ERR_ADMIN_REVOKED:
3894 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003895 if (state == NULL)
3896 break;
3897 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003898 case -NFS4ERR_OPENMODE:
3899 if (state == NULL)
3900 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003901 nfs4_schedule_stateid_recovery(server, state);
3902 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003903 case -NFS4ERR_EXPIRED:
3904 if (state != NULL)
3905 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003907 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003908 nfs4_schedule_lease_recovery(clp);
3909 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003910#if defined(CONFIG_NFS_V4_1)
3911 case -NFS4ERR_BADSESSION:
3912 case -NFS4ERR_BADSLOT:
3913 case -NFS4ERR_BAD_HIGH_SLOT:
3914 case -NFS4ERR_DEADSESSION:
3915 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3916 case -NFS4ERR_SEQ_FALSE_RETRY:
3917 case -NFS4ERR_SEQ_MISORDERED:
3918 dprintk("%s ERROR %d, Reset session\n", __func__,
3919 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003920 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003921 task->tk_status = 0;
3922 return -EAGAIN;
3923#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003925 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003926 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003927 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3929 task->tk_status = 0;
3930 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003931 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 case -NFS4ERR_OLD_STATEID:
3933 task->tk_status = 0;
3934 return -EAGAIN;
3935 }
3936 task->tk_status = nfs4_map_errors(task->tk_status);
3937 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003938wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003939 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003940 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3941 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3942 task->tk_status = 0;
3943 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944}
3945
Chuck Levercd937102012-03-02 17:14:31 -05003946static void nfs4_construct_boot_verifier(struct nfs_client *clp,
3947 nfs4_verifier *bootverf)
3948{
3949 __be32 verf[2];
3950
3951 verf[0] = htonl((u32)clp->cl_boot_time.tv_sec);
3952 verf[1] = htonl((u32)clp->cl_boot_time.tv_nsec);
3953 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3954}
3955
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003956int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3957 unsigned short port, struct rpc_cred *cred,
3958 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959{
3960 nfs4_verifier sc_verifier;
3961 struct nfs4_setclientid setclientid = {
3962 .sc_verifier = &sc_verifier,
3963 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003964 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 };
3966 struct rpc_message msg = {
3967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3968 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003969 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003970 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 int loop = 0;
3973 int status;
3974
Chuck Levercd937102012-03-02 17:14:31 -05003975 nfs4_construct_boot_verifier(clp, &sc_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976
3977 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05003978 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003980 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003981 clp->cl_ipaddr,
3982 rpc_peeraddr2str(clp->cl_rpcclient,
3983 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003984 rpc_peeraddr2str(clp->cl_rpcclient,
3985 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003986 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 clp->cl_id_uniquifier);
3988 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003989 sizeof(setclientid.sc_netid),
3990 rpc_peeraddr2str(clp->cl_rpcclient,
3991 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003993 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05003995 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003997 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 if (status != -NFS4ERR_CLID_INUSE)
3999 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004000 if (loop != 0) {
4001 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004003 }
4004 ++loop;
4005 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 }
4007 return status;
4008}
4009
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004010int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004011 struct nfs4_setclientid_res *arg,
4012 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013{
4014 struct nfs_fsinfo fsinfo;
4015 struct rpc_message msg = {
4016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004017 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004019 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 };
4021 unsigned long now;
4022 int status;
4023
4024 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004025 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 if (status == 0) {
4027 spin_lock(&clp->cl_lock);
4028 clp->cl_lease_time = fsinfo.lease_time * HZ;
4029 clp->cl_last_renewal = now;
4030 spin_unlock(&clp->cl_lock);
4031 }
4032 return status;
4033}
4034
Trond Myklebustfe650402006-01-03 09:55:18 +01004035struct nfs4_delegreturndata {
4036 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004037 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004038 struct nfs_fh fh;
4039 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004040 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004041 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004042 int rpc_status;
4043};
4044
Trond Myklebustfe650402006-01-03 09:55:18 +01004045static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4046{
4047 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004048
Trond Myklebust14516c32010-07-31 14:29:06 -04004049 if (!nfs4_sequence_done(task, &data->res.seq_res))
4050 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004051
Ricardo Labiaga79708862009-12-07 09:23:21 -05004052 switch (task->tk_status) {
4053 case -NFS4ERR_STALE_STATEID:
4054 case -NFS4ERR_EXPIRED:
4055 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;
4058 default:
4059 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4060 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004061 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004062 return;
4063 }
4064 }
4065 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004066}
4067
4068static void nfs4_delegreturn_release(void *calldata)
4069{
Trond Myklebustfe650402006-01-03 09:55:18 +01004070 kfree(calldata);
4071}
4072
Andy Adamson938e1012009-04-01 09:22:28 -04004073#if defined(CONFIG_NFS_V4_1)
4074static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4075{
4076 struct nfs4_delegreturndata *d_data;
4077
4078 d_data = (struct nfs4_delegreturndata *)data;
4079
Trond Myklebust035168a2010-06-16 09:52:26 -04004080 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004081 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004082 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004083 return;
4084 rpc_call_start(task);
4085}
4086#endif /* CONFIG_NFS_V4_1 */
4087
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004088static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004089#if defined(CONFIG_NFS_V4_1)
4090 .rpc_call_prepare = nfs4_delegreturn_prepare,
4091#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004092 .rpc_call_done = nfs4_delegreturn_done,
4093 .rpc_release = nfs4_delegreturn_release,
4094};
4095
Trond Myklebuste6f81072008-01-24 18:14:34 -05004096static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004097{
4098 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004099 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004100 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004101 struct rpc_message msg = {
4102 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4103 .rpc_cred = cred,
4104 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004105 struct rpc_task_setup task_setup_data = {
4106 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004107 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004108 .callback_ops = &nfs4_delegreturn_ops,
4109 .flags = RPC_TASK_ASYNC,
4110 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004111 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004112
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004113 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004114 if (data == NULL)
4115 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004116 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004117 data->args.fhandle = &data->fh;
4118 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004119 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004120 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004121 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004122 data->res.fattr = &data->fattr;
4123 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004124 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004125 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004126 data->rpc_status = 0;
4127
Trond Myklebustc970aa82007-07-14 15:39:59 -04004128 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004129 msg.rpc_argp = &data->args;
4130 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004131 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004132 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004133 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004134 if (!issync)
4135 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004136 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004137 if (status != 0)
4138 goto out;
4139 status = data->rpc_status;
4140 if (status != 0)
4141 goto out;
4142 nfs_refresh_inode(inode, &data->fattr);
4143out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004144 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004145 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146}
4147
Trond Myklebuste6f81072008-01-24 18:14:34 -05004148int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149{
4150 struct nfs_server *server = NFS_SERVER(inode);
4151 struct nfs4_exception exception = { };
4152 int err;
4153 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004154 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 switch (err) {
4156 case -NFS4ERR_STALE_STATEID:
4157 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 case 0:
4159 return 0;
4160 }
4161 err = nfs4_handle_exception(server, err, &exception);
4162 } while (exception.retry);
4163 return err;
4164}
4165
4166#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4167#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4168
4169/*
4170 * sleep, with exponential backoff, and retry the LOCK operation.
4171 */
4172static unsigned long
4173nfs4_set_lock_task_retry(unsigned long timeout)
4174{
Jeff Laytond3103102011-12-01 22:44:39 +01004175 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 timeout <<= 1;
4177 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4178 return NFS4_LOCK_MAXTIMEOUT;
4179 return timeout;
4180}
4181
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4183{
4184 struct inode *inode = state->inode;
4185 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004186 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004187 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004189 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004191 struct nfs_lockt_res res = {
4192 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 };
4194 struct rpc_message msg = {
4195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4196 .rpc_argp = &arg,
4197 .rpc_resp = &res,
4198 .rpc_cred = state->owner->so_cred,
4199 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 struct nfs4_lock_state *lsp;
4201 int status;
4202
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004203 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004204 status = nfs4_set_lock_state(state, request);
4205 if (status != 0)
4206 goto out;
4207 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004208 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004209 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004210 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004211 switch (status) {
4212 case 0:
4213 request->fl_type = F_UNLCK;
4214 break;
4215 case -NFS4ERR_DENIED:
4216 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004218 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004219out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 return status;
4221}
4222
4223static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4224{
4225 struct nfs4_exception exception = { };
4226 int err;
4227
4228 do {
4229 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4230 _nfs4_proc_getlk(state, cmd, request),
4231 &exception);
4232 } while (exception.retry);
4233 return err;
4234}
4235
4236static int do_vfs_lock(struct file *file, struct file_lock *fl)
4237{
4238 int res = 0;
4239 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4240 case FL_POSIX:
4241 res = posix_lock_file_wait(file, fl);
4242 break;
4243 case FL_FLOCK:
4244 res = flock_lock_file_wait(file, fl);
4245 break;
4246 default:
4247 BUG();
4248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 return res;
4250}
4251
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004252struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004253 struct nfs_locku_args arg;
4254 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004255 struct nfs4_lock_state *lsp;
4256 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004257 struct file_lock fl;
4258 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004259 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004260};
4261
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004262static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4263 struct nfs_open_context *ctx,
4264 struct nfs4_lock_state *lsp,
4265 struct nfs_seqid *seqid)
4266{
4267 struct nfs4_unlockdata *p;
4268 struct inode *inode = lsp->ls_state->inode;
4269
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004270 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004271 if (p == NULL)
4272 return NULL;
4273 p->arg.fh = NFS_FH(inode);
4274 p->arg.fl = &p->fl;
4275 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004276 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004277 p->arg.stateid = &lsp->ls_stateid;
4278 p->lsp = lsp;
4279 atomic_inc(&lsp->ls_count);
4280 /* Ensure we don't close file until we're done freeing locks! */
4281 p->ctx = get_nfs_open_context(ctx);
4282 memcpy(&p->fl, fl, sizeof(p->fl));
4283 p->server = NFS_SERVER(inode);
4284 return p;
4285}
4286
Trond Myklebust06f814a2006-01-03 09:55:07 +01004287static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004288{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004289 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004290 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004291 nfs4_put_lock_state(calldata->lsp);
4292 put_nfs_open_context(calldata->ctx);
4293 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004294}
4295
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004296static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004297{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004298 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004299
Trond Myklebust14516c32010-07-31 14:29:06 -04004300 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4301 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004302 switch (task->tk_status) {
4303 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004304 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4305 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004306 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004307 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004308 case -NFS4ERR_BAD_STATEID:
4309 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004310 case -NFS4ERR_STALE_STATEID:
4311 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004312 break;
4313 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004314 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004315 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004316 }
Trond Myklebust8827f312012-10-29 18:53:23 -04004317 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004318}
4319
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004320static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004321{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004322 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004324 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004325 return;
4326 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004327 /* Note: exit _without_ running nfs4_locku_done */
4328 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004329 return;
4330 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004331 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004332 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004333 &calldata->arg.seq_args,
Trond Myklebust0ada2102012-10-29 18:37:40 -04004334 &calldata->res.seq_res,
4335 task) != 0)
4336 nfs_release_seqid(calldata->arg.seqid);
4337 else
4338 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339}
4340
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004341static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004342 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004343 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004344 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004345};
4346
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004347static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4348 struct nfs_open_context *ctx,
4349 struct nfs4_lock_state *lsp,
4350 struct nfs_seqid *seqid)
4351{
4352 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004353 struct rpc_message msg = {
4354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4355 .rpc_cred = ctx->cred,
4356 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004357 struct rpc_task_setup task_setup_data = {
4358 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004359 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004360 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004361 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004362 .flags = RPC_TASK_ASYNC,
4363 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004364
Frank Filz137d6ac2007-07-09 15:32:29 -07004365 /* Ensure this is an unlock - when canceling a lock, the
4366 * canceled lock is passed in, and it won't be an unlock.
4367 */
4368 fl->fl_type = F_UNLCK;
4369
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004370 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4371 if (data == NULL) {
4372 nfs_free_seqid(seqid);
4373 return ERR_PTR(-ENOMEM);
4374 }
4375
Trond Myklebust9d12b212012-01-17 22:04:25 -05004376 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004377 msg.rpc_argp = &data->arg;
4378 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004379 task_setup_data.callback_data = data;
4380 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004381}
4382
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4384{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004385 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004386 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004387 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004388 struct rpc_task *task;
4389 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004390 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391
Trond Myklebust9b073572006-06-29 16:38:34 -04004392 status = nfs4_set_lock_state(state, request);
4393 /* Unlock _before_ we do the RPC call */
4394 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004395 down_read(&nfsi->rwsem);
4396 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4397 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004398 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004399 }
4400 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004401 if (status != 0)
4402 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004403 /* Is this a delegated lock? */
4404 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004405 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004406 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004407 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004408 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004409 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004410 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004411 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004412 status = PTR_ERR(task);
4413 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004414 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004415 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004416 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004417out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004418 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004419 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420}
4421
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004422struct nfs4_lockdata {
4423 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004424 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004425 struct nfs4_lock_state *lsp;
4426 struct nfs_open_context *ctx;
4427 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004428 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004429 int rpc_status;
4430 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004431 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004432};
4433
4434static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004435 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4436 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004437{
4438 struct nfs4_lockdata *p;
4439 struct inode *inode = lsp->ls_state->inode;
4440 struct nfs_server *server = NFS_SERVER(inode);
4441
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004442 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004443 if (p == NULL)
4444 return NULL;
4445
4446 p->arg.fh = NFS_FH(inode);
4447 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004448 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004449 if (p->arg.open_seqid == NULL)
4450 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004451 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004452 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004453 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004454 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004455 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004456 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004457 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004458 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004459 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004460 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004461 atomic_inc(&lsp->ls_count);
4462 p->ctx = get_nfs_open_context(ctx);
4463 memcpy(&p->fl, fl, sizeof(p->fl));
4464 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004465out_free_seqid:
4466 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004467out_free:
4468 kfree(p);
4469 return NULL;
4470}
4471
4472static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4473{
4474 struct nfs4_lockdata *data = calldata;
4475 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476
Harvey Harrison3110ff82008-05-02 13:42:44 -07004477 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004478 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4479 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004480 /* Do we need to do an open_to_lock_owner? */
4481 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004482 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust0ada2102012-10-29 18:37:40 -04004483 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004484 data->arg.open_stateid = &state->stateid;
4485 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004486 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004487 } else
4488 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004489 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004490 if (nfs4_setup_sequence(data->server,
4491 &data->arg.seq_args,
Trond Myklebust0ada2102012-10-29 18:37:40 -04004492 &data->res.seq_res,
4493 task) == 0) {
4494 rpc_call_start(task);
Andy Adamson66179ef2009-04-01 09:22:22 -04004495 return;
Trond Myklebust0ada2102012-10-29 18:37:40 -04004496 }
4497 nfs_release_seqid(data->arg.open_seqid);
4498out_release_lock_seqid:
4499 nfs_release_seqid(data->arg.lock_seqid);
4500 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004501}
4502
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004503static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4504{
4505 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4506 nfs4_lock_prepare(task, calldata);
4507}
4508
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004509static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4510{
4511 struct nfs4_lockdata *data = calldata;
4512
Harvey Harrison3110ff82008-05-02 13:42:44 -07004513 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004514
Trond Myklebust14516c32010-07-31 14:29:06 -04004515 if (!nfs4_sequence_done(task, &data->res.seq_res))
4516 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004517
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004518 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004519 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004520 if (data->rpc_status == 0)
4521 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4522 else
4523 goto out;
4524 }
4525 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004526 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004527 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004528 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004529 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004530out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004531 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004532}
4533
4534static void nfs4_lock_release(void *calldata)
4535{
4536 struct nfs4_lockdata *data = calldata;
4537
Harvey Harrison3110ff82008-05-02 13:42:44 -07004538 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004539 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004540 if (data->cancelled != 0) {
4541 struct rpc_task *task;
4542 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4543 data->arg.lock_seqid);
4544 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004545 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004546 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004547 } else
4548 nfs_free_seqid(data->arg.lock_seqid);
4549 nfs4_put_lock_state(data->lsp);
4550 put_nfs_open_context(data->ctx);
4551 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004552 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004553}
4554
4555static const struct rpc_call_ops nfs4_lock_ops = {
4556 .rpc_call_prepare = nfs4_lock_prepare,
4557 .rpc_call_done = nfs4_lock_done,
4558 .rpc_release = nfs4_lock_release,
4559};
4560
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004561static const struct rpc_call_ops nfs4_recover_lock_ops = {
4562 .rpc_call_prepare = nfs4_recover_lock_prepare,
4563 .rpc_call_done = nfs4_lock_done,
4564 .rpc_release = nfs4_lock_release,
4565};
4566
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004567static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4568{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004569 switch (error) {
4570 case -NFS4ERR_ADMIN_REVOKED:
4571 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004572 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004573 if (new_lock_owner != 0 ||
4574 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004575 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004576 break;
4577 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004578 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004579 case -NFS4ERR_EXPIRED:
4580 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004581 };
4582}
4583
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004584static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004585{
4586 struct nfs4_lockdata *data;
4587 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004588 struct rpc_message msg = {
4589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4590 .rpc_cred = state->owner->so_cred,
4591 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004592 struct rpc_task_setup task_setup_data = {
4593 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004594 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004595 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004596 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004597 .flags = RPC_TASK_ASYNC,
4598 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004599 int ret;
4600
Harvey Harrison3110ff82008-05-02 13:42:44 -07004601 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004602 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004603 fl->fl_u.nfs4_fl.owner,
4604 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004605 if (data == NULL)
4606 return -ENOMEM;
4607 if (IS_SETLKW(cmd))
4608 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004609 if (recovery_type > NFS_LOCK_NEW) {
4610 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004611 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004612 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4613 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004614 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004615 msg.rpc_argp = &data->arg;
4616 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004617 task_setup_data.callback_data = data;
4618 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004619 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004620 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004621 ret = nfs4_wait_for_completion_rpc_task(task);
4622 if (ret == 0) {
4623 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004624 if (ret)
4625 nfs4_handle_setlk_error(data->server, data->lsp,
4626 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004627 } else
4628 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004629 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004630 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004631 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632}
4633
4634static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4635{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004636 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004637 struct nfs4_exception exception = {
4638 .inode = state->inode,
4639 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004640 int err;
4641
4642 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004643 /* Cache the lock if possible... */
4644 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4645 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004646 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004647 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004648 break;
4649 nfs4_handle_exception(server, err, &exception);
4650 } while (exception.retry);
4651 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652}
4653
4654static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4655{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004656 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004657 struct nfs4_exception exception = {
4658 .inode = state->inode,
4659 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004660 int err;
4661
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004662 err = nfs4_set_lock_state(state, request);
4663 if (err != 0)
4664 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004665 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004666 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4667 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004668 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004669 switch (err) {
4670 default:
4671 goto out;
4672 case -NFS4ERR_GRACE:
4673 case -NFS4ERR_DELAY:
4674 nfs4_handle_exception(server, err, &exception);
4675 err = 0;
4676 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004677 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004678out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004679 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680}
4681
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004682#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004683static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004684{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004685 int status, ret = NFS_OK;
4686 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004687 struct nfs_server *server = NFS_SERVER(state->inode);
4688
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004689 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4690 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4691 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4692 if (status != NFS_OK) {
4693 nfs41_free_stateid(server, &lsp->ls_stateid);
4694 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4695 ret = status;
4696 }
4697 }
4698 };
4699
4700 return ret;
4701}
4702
4703static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4704{
4705 int status = NFS_OK;
4706
4707 if (test_bit(LK_STATE_IN_USE, &state->flags))
4708 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004709 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004710 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004711 return nfs4_lock_expired(state, request);
4712}
4713#endif
4714
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4716{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004717 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004718 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004719 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004721 if ((fl_flags & FL_POSIX) &&
4722 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4723 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004724 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004725 status = nfs4_set_lock_state(state, request);
4726 if (status != 0)
4727 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004728 request->fl_flags |= FL_ACCESS;
4729 status = do_vfs_lock(request->fl_file, request);
4730 if (status < 0)
4731 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004732 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004733 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004734 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004735 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004736 request->fl_flags = fl_flags & ~FL_SLEEP;
4737 status = do_vfs_lock(request->fl_file, request);
4738 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004739 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004740 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004741 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004742 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004743 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004744 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004745 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004746 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4747 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004748out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004749 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004750out:
4751 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 return status;
4753}
4754
4755static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4756{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004757 struct nfs4_exception exception = {
4758 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004759 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004760 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 int err;
4762
4763 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004764 err = _nfs4_proc_setlk(state, cmd, request);
4765 if (err == -NFS4ERR_DENIED)
4766 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004768 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 } while (exception.retry);
4770 return err;
4771}
4772
4773static int
4774nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4775{
4776 struct nfs_open_context *ctx;
4777 struct nfs4_state *state;
4778 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4779 int status;
4780
4781 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004782 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 state = ctx->state;
4784
4785 if (request->fl_start < 0 || request->fl_end < 0)
4786 return -EINVAL;
4787
Trond Myklebustd9531262009-07-21 19:22:38 -04004788 if (IS_GETLK(cmd)) {
4789 if (state != NULL)
4790 return nfs4_proc_getlk(state, F_GETLK, request);
4791 return 0;
4792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
4794 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4795 return -EINVAL;
4796
Trond Myklebustd9531262009-07-21 19:22:38 -04004797 if (request->fl_type == F_UNLCK) {
4798 if (state != NULL)
4799 return nfs4_proc_unlck(state, cmd, request);
4800 return 0;
4801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
Trond Myklebustd9531262009-07-21 19:22:38 -04004803 if (state == NULL)
4804 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004805 /*
4806 * Don't rely on the VFS having checked the file open mode,
4807 * since it won't do this for flock() locks.
4808 */
4809 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4810 case F_RDLCK:
4811 if (!(filp->f_mode & FMODE_READ))
4812 return -EBADF;
4813 break;
4814 case F_WRLCK:
4815 if (!(filp->f_mode & FMODE_WRITE))
4816 return -EBADF;
4817 }
4818
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 do {
4820 status = nfs4_proc_setlk(state, cmd, request);
4821 if ((status != -EAGAIN) || IS_SETLK(cmd))
4822 break;
4823 timeout = nfs4_set_lock_task_retry(timeout);
4824 status = -ERESTARTSYS;
4825 if (signalled())
4826 break;
4827 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 return status;
4829}
4830
Trond Myklebust888e6942005-11-04 15:38:11 -05004831int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4832{
4833 struct nfs_server *server = NFS_SERVER(state->inode);
4834 struct nfs4_exception exception = { };
4835 int err;
4836
4837 err = nfs4_set_lock_state(state, fl);
4838 if (err != 0)
4839 goto out;
4840 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004841 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004842 switch (err) {
4843 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004844 printk(KERN_ERR "NFS: %s: unhandled error "
4845 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004846 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004847 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004848 goto out;
4849 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004850 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004851 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004852 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004853 nfs4_schedule_lease_recovery(server->nfs_client);
4854 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004855 case -NFS4ERR_BADSESSION:
4856 case -NFS4ERR_BADSLOT:
4857 case -NFS4ERR_BAD_HIGH_SLOT:
4858 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4859 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004860 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004861 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004862 case -ERESTARTSYS:
4863 /*
4864 * The show must go on: exit, but mark the
4865 * stateid as needing recovery.
4866 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004867 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004868 case -NFS4ERR_ADMIN_REVOKED:
4869 case -NFS4ERR_BAD_STATEID:
4870 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004871 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004872 err = 0;
4873 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004874 case -EKEYEXPIRED:
4875 /*
4876 * User RPCSEC_GSS context has expired.
4877 * We cannot recover this stateid now, so
4878 * skip it and allow recovery thread to
4879 * proceed.
4880 */
4881 err = 0;
4882 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004883 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 Schumakerd1967aa2012-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 Schumakerd1967aa2012-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 Schumakerd1967aa2012-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)
5803 return 0;
Bryan Schumakerd1967aa2012-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 Schumakerd1967aa2012-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 Kedarc2bf3222012-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 Kedarc2bf3222012-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 Kedarc2bf3222012-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 Kedarc2bf3222012-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 Kedarc2bf3222012-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 Kedarc2bf3222012-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;
6098 if (status == 0)
6099 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006100 rpc_put_task(task);
6101 dprintk("<-- %s status=%d\n", __func__, status);
6102 return status;
6103}
6104
Benny Halevycbe82602011-05-22 19:52:37 +03006105static void
6106nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6107{
6108 struct nfs4_layoutreturn *lrp = calldata;
6109
6110 dprintk("--> %s\n", __func__);
6111 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006112 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006113 return;
6114 rpc_call_start(task);
6115}
6116
6117static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6118{
6119 struct nfs4_layoutreturn *lrp = calldata;
6120 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006121 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006122
6123 dprintk("--> %s\n", __func__);
6124
6125 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6126 return;
6127
6128 server = NFS_SERVER(lrp->args.inode);
6129 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006130 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006131 return;
6132 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006133 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebustf401b102012-08-08 16:03:13 -04006134 if (task->tk_status == 0 && lrp->res.lrs_present)
6135 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006136 lo->plh_block_lgets--;
6137 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006138 dprintk("<-- %s\n", __func__);
6139}
6140
6141static void nfs4_layoutreturn_release(void *calldata)
6142{
6143 struct nfs4_layoutreturn *lrp = calldata;
6144
6145 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006146 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006147 kfree(calldata);
6148 dprintk("<-- %s\n", __func__);
6149}
6150
6151static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6152 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6153 .rpc_call_done = nfs4_layoutreturn_done,
6154 .rpc_release = nfs4_layoutreturn_release,
6155};
6156
6157int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6158{
6159 struct rpc_task *task;
6160 struct rpc_message msg = {
6161 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6162 .rpc_argp = &lrp->args,
6163 .rpc_resp = &lrp->res,
6164 };
6165 struct rpc_task_setup task_setup_data = {
6166 .rpc_client = lrp->clp->cl_rpcclient,
6167 .rpc_message = &msg,
6168 .callback_ops = &nfs4_layoutreturn_call_ops,
6169 .callback_data = lrp,
6170 };
6171 int status;
6172
6173 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006174 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006175 task = rpc_run_task(&task_setup_data);
6176 if (IS_ERR(task))
6177 return PTR_ERR(task);
6178 status = task->tk_status;
6179 dprintk("<-- %s status=%d\n", __func__, status);
6180 rpc_put_task(task);
6181 return status;
6182}
6183
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006184/*
6185 * Retrieve the list of Data Server devices from the MDS.
6186 */
6187static int _nfs4_getdevicelist(struct nfs_server *server,
6188 const struct nfs_fh *fh,
6189 struct pnfs_devicelist *devlist)
6190{
6191 struct nfs4_getdevicelist_args args = {
6192 .fh = fh,
6193 .layoutclass = server->pnfs_curr_ld->id,
6194 };
6195 struct nfs4_getdevicelist_res res = {
6196 .devlist = devlist,
6197 };
6198 struct rpc_message msg = {
6199 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6200 .rpc_argp = &args,
6201 .rpc_resp = &res,
6202 };
6203 int status;
6204
6205 dprintk("--> %s\n", __func__);
6206 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6207 &res.seq_res, 0);
6208 dprintk("<-- %s status=%d\n", __func__, status);
6209 return status;
6210}
6211
6212int nfs4_proc_getdevicelist(struct nfs_server *server,
6213 const struct nfs_fh *fh,
6214 struct pnfs_devicelist *devlist)
6215{
6216 struct nfs4_exception exception = { };
6217 int err;
6218
6219 do {
6220 err = nfs4_handle_exception(server,
6221 _nfs4_getdevicelist(server, fh, devlist),
6222 &exception);
6223 } while (exception.retry);
6224
6225 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6226 err, devlist->num_devs);
6227
6228 return err;
6229}
6230EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6231
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006232static int
6233_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6234{
6235 struct nfs4_getdeviceinfo_args args = {
6236 .pdev = pdev,
6237 };
6238 struct nfs4_getdeviceinfo_res res = {
6239 .pdev = pdev,
6240 };
6241 struct rpc_message msg = {
6242 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6243 .rpc_argp = &args,
6244 .rpc_resp = &res,
6245 };
6246 int status;
6247
6248 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006249 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006250 dprintk("<-- %s status=%d\n", __func__, status);
6251
6252 return status;
6253}
6254
6255int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6256{
6257 struct nfs4_exception exception = { };
6258 int err;
6259
6260 do {
6261 err = nfs4_handle_exception(server,
6262 _nfs4_proc_getdeviceinfo(server, pdev),
6263 &exception);
6264 } while (exception.retry);
6265 return err;
6266}
6267EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6268
Andy Adamson863a3c62011-03-23 13:27:54 +00006269static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6270{
6271 struct nfs4_layoutcommit_data *data = calldata;
6272 struct nfs_server *server = NFS_SERVER(data->args.inode);
6273
6274 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006275 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006276 return;
6277 rpc_call_start(task);
6278}
6279
6280static void
6281nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6282{
6283 struct nfs4_layoutcommit_data *data = calldata;
6284 struct nfs_server *server = NFS_SERVER(data->args.inode);
6285
6286 if (!nfs4_sequence_done(task, &data->res.seq_res))
6287 return;
6288
6289 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006290 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6291 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6292 case -NFS4ERR_BADLAYOUT: /* no layout */
6293 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006294 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006295 break;
6296 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006297 nfs_post_op_update_inode_force_wcc(data->args.inode,
6298 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006299 break;
6300 default:
6301 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6302 rpc_restart_call_prepare(task);
6303 return;
6304 }
6305 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006306}
6307
6308static void nfs4_layoutcommit_release(void *calldata)
6309{
6310 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006311 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006312 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006313
Andy Adamsondb29c082011-07-30 20:52:38 -04006314 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006315 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006316 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6317 list_del_init(&lseg->pls_lc_list);
6318 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6319 &lseg->pls_flags))
6320 put_lseg(lseg);
6321 }
Peng Tao92407e72011-10-23 20:21:17 -07006322
6323 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6324 smp_mb__after_clear_bit();
6325 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6326
Andy Adamson863a3c62011-03-23 13:27:54 +00006327 put_rpccred(data->cred);
6328 kfree(data);
6329}
6330
6331static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6332 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6333 .rpc_call_done = nfs4_layoutcommit_done,
6334 .rpc_release = nfs4_layoutcommit_release,
6335};
6336
6337int
Andy Adamsonef311532011-03-12 02:58:10 -05006338nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006339{
6340 struct rpc_message msg = {
6341 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6342 .rpc_argp = &data->args,
6343 .rpc_resp = &data->res,
6344 .rpc_cred = data->cred,
6345 };
6346 struct rpc_task_setup task_setup_data = {
6347 .task = &data->task,
6348 .rpc_client = NFS_CLIENT(data->args.inode),
6349 .rpc_message = &msg,
6350 .callback_ops = &nfs4_layoutcommit_ops,
6351 .callback_data = data,
6352 .flags = RPC_TASK_ASYNC,
6353 };
6354 struct rpc_task *task;
6355 int status = 0;
6356
6357 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6358 "lbw: %llu inode %lu\n",
6359 data->task.tk_pid, sync,
6360 data->args.lastbytewritten,
6361 data->args.inode->i_ino);
6362
Trond Myklebust9d12b212012-01-17 22:04:25 -05006363 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006364 task = rpc_run_task(&task_setup_data);
6365 if (IS_ERR(task))
6366 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006367 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006368 goto out;
6369 status = nfs4_wait_for_completion_rpc_task(task);
6370 if (status != 0)
6371 goto out;
6372 status = task->tk_status;
6373out:
6374 dprintk("%s: status %d\n", __func__, status);
6375 rpc_put_task(task);
6376 return status;
6377}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006378
6379static int
6380_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6381 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6382{
6383 struct nfs41_secinfo_no_name_args args = {
6384 .style = SECINFO_STYLE_CURRENT_FH,
6385 };
6386 struct nfs4_secinfo_res res = {
6387 .flavors = flavors,
6388 };
6389 struct rpc_message msg = {
6390 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6391 .rpc_argp = &args,
6392 .rpc_resp = &res,
6393 };
6394 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6395}
6396
6397static int
6398nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6399 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6400{
6401 struct nfs4_exception exception = { };
6402 int err;
6403 do {
6404 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6405 switch (err) {
6406 case 0:
6407 case -NFS4ERR_WRONGSEC:
6408 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006409 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006410 default:
6411 err = nfs4_handle_exception(server, err, &exception);
6412 }
6413 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006414out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006415 return err;
6416}
6417
6418static int
6419nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6420 struct nfs_fsinfo *info)
6421{
6422 int err;
6423 struct page *page;
6424 rpc_authflavor_t flavor;
6425 struct nfs4_secinfo_flavors *flavors;
6426
6427 page = alloc_page(GFP_KERNEL);
6428 if (!page) {
6429 err = -ENOMEM;
6430 goto out;
6431 }
6432
6433 flavors = page_address(page);
6434 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6435
6436 /*
6437 * Fall back on "guess and check" method if
6438 * the server doesn't support SECINFO_NO_NAME
6439 */
6440 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6441 err = nfs4_find_root_sec(server, fhandle, info);
6442 goto out_freepage;
6443 }
6444 if (err)
6445 goto out_freepage;
6446
6447 flavor = nfs_find_best_sec(flavors);
6448 if (err == 0)
6449 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6450
6451out_freepage:
6452 put_page(page);
6453 if (err == -EACCES)
6454 return -EPERM;
6455out:
6456 return err;
6457}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006458
6459static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006460{
6461 int status;
6462 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006463 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006464 };
6465 struct nfs41_test_stateid_res res;
6466 struct rpc_message msg = {
6467 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6468 .rpc_argp = &args,
6469 .rpc_resp = &res,
6470 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006471
Trond Myklebust9d12b212012-01-17 22:04:25 -05006472 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006473 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6474
6475 if (status == NFS_OK)
6476 return res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006477 return status;
6478}
6479
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006480static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006481{
6482 struct nfs4_exception exception = { };
6483 int err;
6484 do {
6485 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006486 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006487 &exception);
6488 } while (exception.retry);
6489 return err;
6490}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006491
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006492static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006493{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006494 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006495 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006496 };
6497 struct nfs41_free_stateid_res res;
6498 struct rpc_message msg = {
6499 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6500 .rpc_argp = &args,
6501 .rpc_resp = &res,
6502 };
6503
Trond Myklebust9d12b212012-01-17 22:04:25 -05006504 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6505 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006506}
6507
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006508static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006509{
6510 struct nfs4_exception exception = { };
6511 int err;
6512 do {
6513 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006514 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006515 &exception);
6516 } while (exception.retry);
6517 return err;
6518}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006519
6520static bool nfs41_match_stateid(const nfs4_stateid *s1,
6521 const nfs4_stateid *s2)
6522{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006523 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006524 return false;
6525
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006526 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006527 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006528 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006529 return true;
6530
6531 return false;
6532}
6533
Andy Adamson557134a2009-04-01 09:21:53 -04006534#endif /* CONFIG_NFS_V4_1 */
6535
Trond Myklebust36281ca2012-03-04 18:13:56 -05006536static bool nfs4_match_stateid(const nfs4_stateid *s1,
6537 const nfs4_stateid *s2)
6538{
Trond Myklebustf597c532012-03-04 18:13:56 -05006539 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006540}
6541
6542
Trond Myklebust17280172012-03-11 13:11:00 -04006543static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006544 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006545 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546 .recover_open = nfs4_open_reclaim,
6547 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006548 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006549 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550};
6551
Andy Adamson591d71c2009-04-01 09:22:47 -04006552#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006553static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006554 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6555 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6556 .recover_open = nfs4_open_reclaim,
6557 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006558 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006559 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006560 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006561};
6562#endif /* CONFIG_NFS_V4_1 */
6563
Trond Myklebust17280172012-03-11 13:11:00 -04006564static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006565 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006566 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567 .recover_open = nfs4_open_expired,
6568 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006569 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006570 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571};
6572
Andy Adamson591d71c2009-04-01 09:22:47 -04006573#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006574static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006575 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6576 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006577 .recover_open = nfs41_open_expired,
6578 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006579 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006580 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006581};
6582#endif /* CONFIG_NFS_V4_1 */
6583
Trond Myklebust17280172012-03-11 13:11:00 -04006584static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006585 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006586 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006587 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006588};
6589
6590#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006591static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006592 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006593 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006594 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006595};
6596#endif
6597
Trond Myklebust97dc1352010-06-16 09:52:26 -04006598static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6599 .minor_version = 0,
6600 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006601 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006602 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006603 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6604 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6605 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006606};
6607
6608#if defined(CONFIG_NFS_V4_1)
6609static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6610 .minor_version = 1,
6611 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006612 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006613 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006614 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6615 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6616 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006617};
6618#endif
6619
Trond Myklebust97dc1352010-06-16 09:52:26 -04006620const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6621 [0] = &nfs_v4_0_minor_ops,
6622#if defined(CONFIG_NFS_V4_1)
6623 [1] = &nfs_v4_1_minor_ops,
6624#endif
6625};
6626
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006627static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006628 .permission = nfs_permission,
6629 .getattr = nfs_getattr,
6630 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006631 .getxattr = generic_getxattr,
6632 .setxattr = generic_setxattr,
6633 .listxattr = generic_listxattr,
6634 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006635};
6636
David Howells509de812006-08-22 20:06:11 -04006637const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638 .version = 4, /* protocol version */
6639 .dentry_ops = &nfs4_dentry_operations,
6640 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006641 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006642 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 .getroot = nfs4_proc_get_root,
6644 .getattr = nfs4_proc_getattr,
6645 .setattr = nfs4_proc_setattr,
6646 .lookup = nfs4_proc_lookup,
6647 .access = nfs4_proc_access,
6648 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006649 .create = nfs4_proc_create,
6650 .remove = nfs4_proc_remove,
6651 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006652 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653 .unlink_done = nfs4_proc_unlink_done,
6654 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006655 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006656 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006657 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658 .link = nfs4_proc_link,
6659 .symlink = nfs4_proc_symlink,
6660 .mkdir = nfs4_proc_mkdir,
6661 .rmdir = nfs4_proc_remove,
6662 .readdir = nfs4_proc_readdir,
6663 .mknod = nfs4_proc_mknod,
6664 .statfs = nfs4_proc_statfs,
6665 .fsinfo = nfs4_proc_fsinfo,
6666 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006667 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668 .decode_dirent = nfs4_decode_dirent,
6669 .read_setup = nfs4_proc_read_setup,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006670 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006671 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 .write_setup = nfs4_proc_write_setup,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006673 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006674 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006676 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006678 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006679 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006680 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006681 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006682 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683};
6684
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006685static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6686 .prefix = XATTR_NAME_NFSV4_ACL,
6687 .list = nfs4_xattr_list_nfs4_acl,
6688 .get = nfs4_xattr_get_nfs4_acl,
6689 .set = nfs4_xattr_set_nfs4_acl,
6690};
6691
6692const struct xattr_handler *nfs4_xattr_handlers[] = {
6693 &nfs4_xattr_nfs4_acl_handler,
6694 NULL
6695};
6696
Trond Myklebustef159e92012-02-06 19:50:40 -05006697module_param(max_session_slots, ushort, 0644);
6698MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6699 "requests the client will negotiate");
6700
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701/*
6702 * Local variables:
6703 * c-basic-offset: 8
6704 * End:
6705 */