blob: 1ec05222ccbc65324236165d70e7870e38f4c874 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
Bryan Schumaker8f70e952011-03-24 17:12:31 +000046#include <linux/sunrpc/gss_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050055#include <linux/nfs_idmap.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040056#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000057#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050058#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010059#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Trond Myklebust4ce79712005-06-22 17:16:21 +000061#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050063#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050064#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040065#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040066#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define NFSDBG_FACILITY NFSDBG_PROC
69
Trond Myklebust2066fe82006-09-15 08:30:46 -040070#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFS4_POLL_RETRY_MAX (15*HZ)
72
Trond Myklebusta78cb572009-08-09 15:06:19 -040073#define NFS4_MAX_LOOP_ON_RECOVER (10)
74
Trond Myklebustef159e92012-02-06 19:50:40 -050075static unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
76
Trond Myklebustcdd4e682006-01-03 09:55:12 +010077struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010078static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080079static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050081static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050082static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Trond Myklebust99367812007-07-17 21:52:41 -040083static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040084static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
86 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040087#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050088static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
89static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040090#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050092static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Trond Myklebust52567b02009-10-23 14:46:42 -040094 if (err >= -1000)
95 return err;
96 switch (err) {
97 case -NFS4ERR_RESOURCE:
98 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000099 case -NFS4ERR_WRONGSEC:
100 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800101 case -NFS4ERR_BADOWNER:
102 case -NFS4ERR_BADNAME:
103 return -EINVAL;
Trond Myklebust52567b02009-10-23 14:46:42 -0400104 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700106 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400107 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400109 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112/*
113 * This is our standard bitmap for GETATTR requests.
114 */
115const u32 nfs4_fattr_bitmap[2] = {
116 FATTR4_WORD0_TYPE
117 | FATTR4_WORD0_CHANGE
118 | FATTR4_WORD0_SIZE
119 | FATTR4_WORD0_FSID
120 | FATTR4_WORD0_FILEID,
121 FATTR4_WORD1_MODE
122 | FATTR4_WORD1_NUMLINKS
123 | FATTR4_WORD1_OWNER
124 | FATTR4_WORD1_OWNER_GROUP
125 | FATTR4_WORD1_RAWDEV
126 | FATTR4_WORD1_SPACE_USED
127 | FATTR4_WORD1_TIME_ACCESS
128 | FATTR4_WORD1_TIME_METADATA
129 | FATTR4_WORD1_TIME_MODIFY
130};
131
132const u32 nfs4_statfs_bitmap[2] = {
133 FATTR4_WORD0_FILES_AVAIL
134 | FATTR4_WORD0_FILES_FREE
135 | FATTR4_WORD0_FILES_TOTAL,
136 FATTR4_WORD1_SPACE_AVAIL
137 | FATTR4_WORD1_SPACE_FREE
138 | FATTR4_WORD1_SPACE_TOTAL
139};
140
Trond Myklebust4ce79712005-06-22 17:16:21 +0000141const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 FATTR4_WORD0_MAXLINK
143 | FATTR4_WORD0_MAXNAME,
144 0
145};
146
Fred Isamandae100c2011-07-30 20:52:37 -0400147const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 | FATTR4_WORD0_MAXREAD
149 | FATTR4_WORD0_MAXWRITE
150 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700151 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400152 | FATTR4_WORD1_FS_LAYOUT_TYPES,
153 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154};
155
Manoj Naik830b8e32006-06-09 09:34:25 -0400156const u32 nfs4_fs_locations_bitmap[2] = {
157 FATTR4_WORD0_TYPE
158 | FATTR4_WORD0_CHANGE
159 | FATTR4_WORD0_SIZE
160 | FATTR4_WORD0_FSID
161 | FATTR4_WORD0_FILEID
162 | FATTR4_WORD0_FS_LOCATIONS,
163 FATTR4_WORD1_MODE
164 | FATTR4_WORD1_NUMLINKS
165 | FATTR4_WORD1_OWNER
166 | FATTR4_WORD1_OWNER_GROUP
167 | FATTR4_WORD1_RAWDEV
168 | FATTR4_WORD1_SPACE_USED
169 | FATTR4_WORD1_TIME_ACCESS
170 | FATTR4_WORD1_TIME_METADATA
171 | FATTR4_WORD1_TIME_MODIFY
172 | FATTR4_WORD1_MOUNTED_ON_FILEID
173};
174
Al Virobc4785c2006-10-19 23:28:51 -0700175static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 struct nfs4_readdir_arg *readdir)
177{
Al Viro0dbb4c62006-10-19 23:28:49 -0700178 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 BUG_ON(readdir->count < 80);
181 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000182 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
184 return;
185 }
186
187 readdir->cookie = 0;
188 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
189 if (cookie == 2)
190 return;
191
192 /*
193 * NFSv4 servers do not return entries for '.' and '..'
194 * Therefore, we fake these entries here. We let '.'
195 * have cookie 0 and '..' have cookie 1. Note that
196 * when talking to the server, we always send cookie 0
197 * instead of 1 or 2.
198 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700199 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 if (cookie == 0) {
202 *p++ = xdr_one; /* next */
203 *p++ = xdr_zero; /* cookie, first word */
204 *p++ = xdr_one; /* cookie, second word */
205 *p++ = xdr_one; /* entry len */
206 memcpy(p, ".\0\0\0", 4); /* entry */
207 p++;
208 *p++ = xdr_one; /* bitmap length */
209 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
210 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400211 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213
214 *p++ = xdr_one; /* next */
215 *p++ = xdr_zero; /* cookie, first word */
216 *p++ = xdr_two; /* cookie, second word */
217 *p++ = xdr_two; /* entry len */
218 memcpy(p, "..\0\0", 4); /* entry */
219 p++;
220 *p++ = xdr_one; /* bitmap length */
221 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
222 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400223 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 readdir->pgbase = (char *)p - (char *)start;
226 readdir->count -= readdir->pgbase;
227 kunmap_atomic(start, KM_USER0);
228}
229
Trond Myklebust65de8722008-12-23 15:21:44 -0500230static int nfs4_wait_clnt_recover(struct nfs_client *clp)
231{
232 int res;
233
234 might_sleep();
235
Trond Myklebuste005e802008-12-23 15:21:48 -0500236 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400237 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500238 return res;
239}
240
241static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
242{
243 int res = 0;
244
245 might_sleep();
246
247 if (*timeout <= 0)
248 *timeout = NFS4_POLL_RETRY_MIN;
249 if (*timeout > NFS4_POLL_RETRY_MAX)
250 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100251 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500252 if (fatal_signal_pending(current))
253 res = -ERESTARTSYS;
254 *timeout <<= 1;
255 return res;
256}
257
258/* This is the error handling routine for processes that are allowed
259 * to sleep.
260 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800261static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500262{
263 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500264 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500265 int ret = errorcode;
266
267 exception->retry = 0;
268 switch(errorcode) {
269 case 0:
270 return 0;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500271 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500272 case -NFS4ERR_ADMIN_REVOKED:
273 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500274 if (state != NULL)
275 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500276 case -NFS4ERR_OPENMODE:
277 if (state == NULL)
278 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500279 nfs4_schedule_stateid_recovery(server, state);
280 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400281 case -NFS4ERR_EXPIRED:
282 if (state != NULL)
283 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500284 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500285 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500286 nfs4_schedule_lease_recovery(clp);
287 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500288#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400289 case -NFS4ERR_BADSESSION:
290 case -NFS4ERR_BADSLOT:
291 case -NFS4ERR_BAD_HIGH_SLOT:
292 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
293 case -NFS4ERR_DEADSESSION:
294 case -NFS4ERR_SEQ_FALSE_RETRY:
295 case -NFS4ERR_SEQ_MISORDERED:
296 dprintk("%s ERROR: %d Reset session\n", __func__,
297 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500298 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400299 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500300 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500301#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500302 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500303 if (exception->timeout > HZ) {
304 /* We have retried a decent amount, time to
305 * fail
306 */
307 ret = -EBUSY;
308 break;
309 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500310 case -NFS4ERR_GRACE:
311 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500312 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500313 ret = nfs4_delay(server->client, &exception->timeout);
314 if (ret != 0)
315 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400316 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500317 case -NFS4ERR_OLD_STATEID:
318 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800319 break;
320 case -NFS4ERR_BADOWNER:
321 /* The following works around a Linux server bug! */
322 case -NFS4ERR_BADNAME:
323 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
324 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
325 exception->retry = 1;
326 printk(KERN_WARNING "NFS: v4 server %s "
327 "does not accept raw "
328 "uid/gids. "
329 "Reenabling the idmapper.\n",
330 server->nfs_client->cl_hostname);
331 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500332 }
333 /* We failed to handle the error */
334 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500335wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500336 ret = nfs4_wait_clnt_recover(clp);
337 if (ret == 0)
338 exception->retry = 1;
339 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500340}
341
342
Trond Myklebust452e9352010-07-31 14:29:06 -0400343static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 spin_lock(&clp->cl_lock);
346 if (time_before(clp->cl_last_renewal,timestamp))
347 clp->cl_last_renewal = timestamp;
348 spin_unlock(&clp->cl_lock);
349}
350
Trond Myklebust452e9352010-07-31 14:29:06 -0400351static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
352{
353 do_renew_lease(server->nfs_client, timestamp);
354}
355
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400356#if defined(CONFIG_NFS_V4_1)
357
Benny Halevy510b8172009-04-01 09:22:14 -0400358/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400359 * nfs4_free_slot - free a slot and efficiently update slot table.
360 *
361 * freeing a slot is trivially done by clearing its respective bit
362 * in the bitmap.
363 * If the freed slotid equals highest_used_slotid we want to update it
364 * so that the server would be able to size down the slot table if needed,
365 * otherwise we know that the highest_used_slotid is still in use.
366 * When updating highest_used_slotid there may be "holes" in the bitmap
367 * so we need to scan down from highest_used_slotid to 0 looking for the now
368 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500369 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500370 *
371 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400372 */
373static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500374nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400375{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500376 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400377 /* clear used bit in bitmap */
378 __clear_bit(slotid, tbl->used_slots);
379
380 /* update highest_used_slotid when it is freed */
381 if (slotid == tbl->highest_used_slotid) {
382 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500383 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400384 tbl->highest_used_slotid = slotid;
385 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500386 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400387 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500388 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
389 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400390}
391
Trond Myklebust961a8282012-01-17 22:57:37 -0500392bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
393{
394 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
395 return true;
396}
397
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800398/*
Andy Adamson42acd022011-01-06 02:04:34 +0000399 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800400 */
Andy Adamson42acd022011-01-06 02:04:34 +0000401static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800402{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400403 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500404 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
405 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800406 return;
407 }
408
Trond Myklebust45d43c22012-02-06 19:38:51 -0500409 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800410 return;
411
Andy Adamson42acd022011-01-06 02:04:34 +0000412 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
413 complete(&ses->fc_slot_table.complete);
414}
415
416/*
417 * Signal state manager thread if session back channel is drained
418 */
419void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
420{
421 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500422 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000423 return;
424 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
425 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800426}
427
Trond Myklebustd185a332010-06-16 09:52:25 -0400428static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400429{
430 struct nfs4_slot_table *tbl;
431
Trond Myklebustd185a332010-06-16 09:52:25 -0400432 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400433 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400434 /* just wake up the next guy waiting since
435 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500436 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500437 return;
Andy Adamson13615872009-04-01 09:22:17 -0400438 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500439
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500440 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500441 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000442 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500443 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400444 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400445}
446
Trond Myklebust14516c32010-07-31 14:29:06 -0400447static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400448{
449 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400450 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400451
452 /*
453 * sr_status remains 1 if an RPC level error occurred. The server
454 * may or may not have processed the sequence operation..
455 * Proceed as if the server received and processed the sequence
456 * operation.
457 */
458 if (res->sr_status == 1)
459 res->sr_status = NFS_OK;
460
Bryan Schumaker468f8612011-04-18 15:57:32 -0400461 /* don't increment the sequence number if the task wasn't sent */
462 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400463 goto out;
464
Andy Adamson691daf32009-12-04 15:55:39 -0500465 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400466 switch (res->sr_status) {
467 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400468 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400469 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400470 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400471 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400472 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500473 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500474 if (res->sr_status_flags != 0)
475 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400476 break;
477 case -NFS4ERR_DELAY:
478 /* The server detected a resend of the RPC call and
479 * returned NFS4ERR_DELAY as per Section 2.10.6.2
480 * of RFC5661.
481 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200482 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400483 __func__,
484 res->sr_slot - res->sr_session->fc_slot_table.slots,
485 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400486 goto out_retry;
487 default:
488 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400489 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400490 }
491out:
492 /* The session may be reset by one of the error handlers. */
493 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400494 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400495 return 1;
496out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400497 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400498 goto out;
499 rpc_delay(task, NFS4_POLL_RETRY_MAX);
500 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400501}
502
Trond Myklebust14516c32010-07-31 14:29:06 -0400503static int nfs4_sequence_done(struct rpc_task *task,
504 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400505{
Trond Myklebust14516c32010-07-31 14:29:06 -0400506 if (res->sr_session == NULL)
507 return 1;
508 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400509}
510
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400511/*
Benny Halevy510b8172009-04-01 09:22:14 -0400512 * nfs4_find_slot - efficiently look for a free slot
513 *
514 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
515 * If found, we mark the slot as used, update the highest_used_slotid,
516 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500517 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400518 *
519 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400520 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500521static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800522nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400523{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500524 u32 slotid;
525 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400526
Trond Myklebust45d43c22012-02-06 19:38:51 -0500527 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400528 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
529 tbl->max_slots);
530 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
531 if (slotid >= tbl->max_slots)
532 goto out;
533 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500534 if (slotid > tbl->highest_used_slotid ||
535 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400536 tbl->highest_used_slotid = slotid;
537 ret_id = slotid;
538out:
539 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
540 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400541 return ret_id;
542}
543
Trond Myklebust9d12b212012-01-17 22:04:25 -0500544static void nfs41_init_sequence(struct nfs4_sequence_args *args,
545 struct nfs4_sequence_res *res, int cache_reply)
546{
547 args->sa_session = NULL;
548 args->sa_cache_this = 0;
549 if (cache_reply)
550 args->sa_cache_this = 1;
551 res->sr_session = NULL;
552 res->sr_slot = NULL;
553}
554
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000555int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400556 struct nfs4_sequence_args *args,
557 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400558 struct rpc_task *task)
559{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400560 struct nfs4_slot *slot;
561 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500562 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400563
564 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400565 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400566 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400567 return 0;
568
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400569 tbl = &session->fc_slot_table;
570
571 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400572 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800573 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500574 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500575 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400576 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500577 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500578 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400579 }
580
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800581 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
582 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
583 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
584 spin_unlock(&tbl->slot_tbl_lock);
585 dprintk("%s enforce FIFO order\n", __func__);
586 return -EAGAIN;
587 }
588
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800589 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500590 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400591 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
592 spin_unlock(&tbl->slot_tbl_lock);
593 dprintk("<-- %s: no free slots\n", __func__);
594 return -EAGAIN;
595 }
596 spin_unlock(&tbl->slot_tbl_lock);
597
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800598 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400599 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400600 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400601 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400602
603 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
604
Benny Halevy99fe60d2009-04-01 09:22:29 -0400605 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400606 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400607 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400608 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400609 /*
610 * sr_status is only set in decode_sequence, and so will remain
611 * set to 1 if an rpc level failure occurs.
612 */
613 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400614 return 0;
615}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000616EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400617
Trond Myklebust035168ab2010-06-16 09:52:26 -0400618int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400619 struct nfs4_sequence_args *args,
620 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400621 struct rpc_task *task)
622{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400623 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400624 int ret = 0;
625
Trond Myklebust9d12b212012-01-17 22:04:25 -0500626 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400627 goto out;
Trond Myklebust035168ab2010-06-16 09:52:26 -0400628
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200629 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400630 __func__, session->clp, session, res->sr_slot ?
631 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400632
Trond Myklebust9d12b212012-01-17 22:04:25 -0500633 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400634out:
635 dprintk("<-- %s status=%d\n", __func__, ret);
636 return ret;
637}
638
639struct nfs41_call_sync_data {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400640 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400641 struct nfs4_sequence_args *seq_args;
642 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400643};
644
645static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
646{
647 struct nfs41_call_sync_data *data = calldata;
648
Trond Myklebust035168ab2010-06-16 09:52:26 -0400649 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
650
651 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500652 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400653 return;
654 rpc_call_start(task);
655}
656
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800657static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
658{
659 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
660 nfs41_call_sync_prepare(task, calldata);
661}
662
Andy Adamson69ab40c2009-04-01 09:22:19 -0400663static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
664{
665 struct nfs41_call_sync_data *data = calldata;
666
Trond Myklebust14516c32010-07-31 14:29:06 -0400667 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400668}
669
Andy Adamsonce5039c2009-04-01 09:22:13 -0400670struct rpc_call_ops nfs41_call_sync_ops = {
671 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400672 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400673};
674
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800675struct rpc_call_ops nfs41_call_priv_sync_ops = {
676 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
677 .rpc_call_done = nfs41_call_sync_done,
678};
679
Bryan Schumaker7c513052011-03-24 17:12:24 +0000680static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
681 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400682 struct rpc_message *msg,
683 struct nfs4_sequence_args *args,
684 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800685 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400686{
687 int ret;
688 struct rpc_task *task;
689 struct nfs41_call_sync_data data = {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400690 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400691 .seq_args = args,
692 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400693 };
694 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000695 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400696 .rpc_message = msg,
697 .callback_ops = &nfs41_call_sync_ops,
698 .callback_data = &data
699 };
700
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800701 if (privileged)
702 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400703 task = rpc_run_task(&task_setup);
704 if (IS_ERR(task))
705 ret = PTR_ERR(task);
706 else {
707 ret = task->tk_status;
708 rpc_put_task(task);
709 }
710 return ret;
711}
712
Bryan Schumaker7c513052011-03-24 17:12:24 +0000713int _nfs4_call_sync_session(struct rpc_clnt *clnt,
714 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400715 struct rpc_message *msg,
716 struct nfs4_sequence_args *args,
717 struct nfs4_sequence_res *res,
718 int cache_reply)
719{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500720 nfs41_init_sequence(args, res, cache_reply);
721 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400722}
723
Trond Myklebustdf896452010-06-16 09:52:26 -0400724#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500725static inline
726void nfs41_init_sequence(struct nfs4_sequence_args *args,
727 struct nfs4_sequence_res *res, int cache_reply)
728{
729}
730
Trond Myklebust14516c32010-07-31 14:29:06 -0400731static int nfs4_sequence_done(struct rpc_task *task,
732 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400733{
Trond Myklebust14516c32010-07-31 14:29:06 -0400734 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400735}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400736#endif /* CONFIG_NFS_V4_1 */
737
Bryan Schumaker7c513052011-03-24 17:12:24 +0000738int _nfs4_call_sync(struct rpc_clnt *clnt,
739 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400740 struct rpc_message *msg,
741 struct nfs4_sequence_args *args,
742 struct nfs4_sequence_res *res,
743 int cache_reply)
744{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500745 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000746 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400747}
748
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000749static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000750int nfs4_call_sync(struct rpc_clnt *clnt,
751 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000752 struct rpc_message *msg,
753 struct nfs4_sequence_args *args,
754 struct nfs4_sequence_res *res,
755 int cache_reply)
756{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000757 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
758 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000759}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400760
Trond Myklebust38478b22006-05-25 01:40:57 -0400761static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Trond Myklebust38478b22006-05-25 01:40:57 -0400763 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Trond Myklebust38478b22006-05-25 01:40:57 -0400765 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400766 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700767 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400768 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700769 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400770 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100773struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400774 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100775 struct nfs_openargs o_arg;
776 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100777 struct nfs_open_confirmargs c_arg;
778 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500779 struct nfs4_string owner_name;
780 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100781 struct nfs_fattr f_attr;
782 struct nfs_fattr dir_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100783 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400784 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100785 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400786 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100787 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100788 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400789 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100790 int rpc_status;
791 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100792};
793
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400794
795static void nfs4_init_opendata_res(struct nfs4_opendata *p)
796{
797 p->o_res.f_attr = &p->f_attr;
798 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400799 p->o_res.seqid = p->o_arg.seqid;
800 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400801 p->o_res.server = p->o_arg.server;
802 nfs_fattr_init(&p->f_attr);
803 nfs_fattr_init(&p->dir_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500804 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400805}
806
Al Viro82a2c1b2011-06-22 18:30:55 -0400807static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500808 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400809 const struct iattr *attrs,
810 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100811{
Al Viro82a2c1b2011-06-22 18:30:55 -0400812 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100813 struct inode *dir = parent->d_inode;
814 struct nfs_server *server = NFS_SERVER(dir);
815 struct nfs4_opendata *p;
816
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400817 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818 if (p == NULL)
819 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400820 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100821 if (p->o_arg.seqid == NULL)
822 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400823 nfs_sb_active(dentry->d_sb);
824 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100825 p->dir = parent;
826 p->owner = sp;
827 atomic_inc(&sp->so_count);
828 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500829 p->o_arg.open_flags = flags;
830 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400831 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -0500832 p->o_arg.id = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400833 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100834 p->o_arg.server = server;
835 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500836 p->o_arg.dir_bitmask = server->cache_consistency_bitmask;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100837 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500838 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500839 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400840
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100841 p->o_arg.u.attrs = &p->attrs;
842 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500843
844 verf[0] = jiffies;
845 verf[1] = current->pid;
846 memcpy(p->o_arg.u.verifier.data, verf,
847 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100848 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100849 p->c_arg.fh = &p->o_res.fh;
850 p->c_arg.stateid = &p->o_res.stateid;
851 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400852 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400853 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100854 return p;
855err_free:
856 kfree(p);
857err:
858 dput(parent);
859 return NULL;
860}
861
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400862static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100863{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400864 struct nfs4_opendata *p = container_of(kref,
865 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400866 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400867
868 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400869 if (p->state != NULL)
870 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400871 nfs4_put_state_owner(p->owner);
872 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400873 dput(p->dentry);
874 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500875 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400876 kfree(p);
877}
878
879static void nfs4_opendata_put(struct nfs4_opendata *p)
880{
881 if (p != NULL)
882 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100883}
884
Trond Myklebust06f814a2006-01-03 09:55:07 +0100885static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
886{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100887 int ret;
888
Trond Myklebust06f814a2006-01-03 09:55:07 +0100889 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100890 return ret;
891}
892
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500893static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400894{
895 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500896
Trond Myklebust536e43d2012-01-17 22:04:26 -0500897 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500898 goto out;
899 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400900 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500901 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
902 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400903 break;
904 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500905 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
906 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400907 break;
908 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500909 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
910 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400911 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500912out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400913 return ret;
914}
915
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500916static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400917{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500918 if (delegation == NULL)
919 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400921 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500922 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400923 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500924 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400925 return 1;
926}
927
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100929{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500930 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100931 case FMODE_WRITE:
932 state->n_wronly++;
933 break;
934 case FMODE_READ:
935 state->n_rdonly++;
936 break;
937 case FMODE_READ|FMODE_WRITE:
938 state->n_rdwr++;
939 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100941}
942
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500943static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400944{
945 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500946 nfs4_stateid_copy(&state->stateid, stateid);
947 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500948 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400949 case FMODE_READ:
950 set_bit(NFS_O_RDONLY_STATE, &state->flags);
951 break;
952 case FMODE_WRITE:
953 set_bit(NFS_O_WRONLY_STATE, &state->flags);
954 break;
955 case FMODE_READ|FMODE_WRITE:
956 set_bit(NFS_O_RDWR_STATE, &state->flags);
957 }
958}
959
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500960static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400961{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400962 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400964 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400965}
966
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500967static 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 -0700968{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400969 /*
970 * Protect the call to nfs4_state_set_mode_locked and
971 * serialise the stateid update
972 */
973 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400974 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500975 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400976 set_bit(NFS_DELEGATED_STATE, &state->flags);
977 }
978 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500979 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400980 write_sequnlock(&state->seqlock);
981 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500982 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700983 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500986static 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 -0500987{
988 struct nfs_inode *nfsi = NFS_I(state->inode);
989 struct nfs_delegation *deleg_cur;
990 int ret = 0;
991
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500993
994 rcu_read_lock();
995 deleg_cur = rcu_dereference(nfsi->delegation);
996 if (deleg_cur == NULL)
997 goto no_delegation;
998
999 spin_lock(&deleg_cur->lock);
1000 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001001 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001002 goto no_delegation_unlock;
1003
1004 if (delegation == NULL)
1005 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001006 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001007 goto no_delegation_unlock;
1008
Trond Myklebustb7391f42008-12-23 15:21:52 -05001009 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001010 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001011 ret = 1;
1012no_delegation_unlock:
1013 spin_unlock(&deleg_cur->lock);
1014no_delegation:
1015 rcu_read_unlock();
1016
1017 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001018 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001019 ret = 1;
1020 }
1021
1022 return ret;
1023}
1024
1025
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001026static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001027{
1028 struct nfs_delegation *delegation;
1029
1030 rcu_read_lock();
1031 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001032 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001033 rcu_read_unlock();
1034 return;
1035 }
1036 rcu_read_unlock();
1037 nfs_inode_return_delegation(inode);
1038}
1039
Trond Myklebust6ee41262007-07-08 14:11:36 -04001040static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001041{
1042 struct nfs4_state *state = opendata->state;
1043 struct nfs_inode *nfsi = NFS_I(state->inode);
1044 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001045 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001046 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001047 nfs4_stateid stateid;
1048 int ret = -EAGAIN;
1049
Trond Myklebustaac00a82007-07-05 19:02:21 -04001050 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001051 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001052 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001053 if (can_open_cached(state, fmode, open_mode)) {
1054 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001055 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001056 goto out_return_state;
1057 }
1058 spin_unlock(&state->owner->so_lock);
1059 }
Trond Myklebust34310432008-12-23 15:21:38 -05001060 rcu_read_lock();
1061 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001062 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001063 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001064 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001065 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001066 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001067 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001068 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001069 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001070 if (ret != 0)
1071 goto out;
1072 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001073
1074 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001075 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001076 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001077 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001078out:
1079 return ERR_PTR(ret);
1080out_return_state:
1081 atomic_inc(&state->count);
1082 return state;
1083}
1084
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001085static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1086{
1087 struct inode *inode;
1088 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001089 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001090 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001091
Trond Myklebustaac00a82007-07-05 19:02:21 -04001092 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001093 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001094 goto out;
1095 }
1096
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001097 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001098 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001099 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001100 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001101 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001102 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001103 goto err;
1104 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001105 state = nfs4_get_open_state(inode, data->owner);
1106 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001107 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001108 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001109 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001110 int delegation_flags = 0;
1111
Trond Myklebust003707c2007-07-05 18:07:55 -04001112 rcu_read_lock();
1113 delegation = rcu_dereference(NFS_I(inode)->delegation);
1114 if (delegation)
1115 delegation_flags = delegation->flags;
1116 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001117 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1118 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1119 "returning a delegation for "
1120 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001121 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001122 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001123 nfs_inode_set_delegation(state->inode,
1124 data->owner->so_cred,
1125 &data->o_res);
1126 else
1127 nfs_inode_reclaim_delegation(state->inode,
1128 data->owner->so_cred,
1129 &data->o_res);
1130 }
Trond Myklebust34310432008-12-23 15:21:38 -05001131
1132 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001133 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001134 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001135out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001136 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001137err_put_inode:
1138 iput(inode);
1139err:
1140 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001141}
1142
Trond Myklebust864472e2006-01-03 09:55:15 +01001143static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1144{
1145 struct nfs_inode *nfsi = NFS_I(state->inode);
1146 struct nfs_open_context *ctx;
1147
1148 spin_lock(&state->inode->i_lock);
1149 list_for_each_entry(ctx, &nfsi->open_files, list) {
1150 if (ctx->state != state)
1151 continue;
1152 get_nfs_open_context(ctx);
1153 spin_unlock(&state->inode->i_lock);
1154 return ctx;
1155 }
1156 spin_unlock(&state->inode->i_lock);
1157 return ERR_PTR(-ENOENT);
1158}
1159
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001160static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1161{
1162 struct nfs4_opendata *opendata;
1163
Al Viro3d4ff432011-06-22 18:40:12 -04001164 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001165 if (opendata == NULL)
1166 return ERR_PTR(-ENOMEM);
1167 opendata->state = state;
1168 atomic_inc(&state->count);
1169 return opendata;
1170}
1171
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001172static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001173{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001174 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001175 int ret;
1176
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001177 opendata->o_arg.open_flags = 0;
1178 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001179 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1180 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1181 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001182 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001183 if (ret != 0)
1184 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001185 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001186 if (IS_ERR(newstate))
1187 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001188 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001189 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001190 return 0;
1191}
1192
1193static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1194{
Trond Myklebust864472e2006-01-03 09:55:15 +01001195 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001196 int ret;
1197
1198 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001199 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001200 smp_rmb();
1201 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001202 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001203 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001204 if (ret != 0)
1205 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001206 if (newstate != state)
1207 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001208 }
1209 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001210 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001211 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001212 if (ret != 0)
1213 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001214 if (newstate != state)
1215 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001216 }
1217 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001218 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001219 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001220 if (ret != 0)
1221 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001222 if (newstate != state)
1223 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001224 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001225 /*
1226 * We may have performed cached opens for all three recoveries.
1227 * Check if we need to update the current stateid.
1228 */
1229 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001230 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001231 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001232 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001233 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001234 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001235 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001236 return 0;
1237}
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239/*
1240 * OPEN_RECLAIM:
1241 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001243static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001245 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001246 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001247 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 int status;
1249
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001250 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1251 if (IS_ERR(opendata))
1252 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001253 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1254 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001255 rcu_read_lock();
1256 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001257 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001258 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001259 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001260 opendata->o_arg.u.delegation_type = delegation_type;
1261 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001262 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return status;
1264}
1265
Trond Myklebust539cd032007-06-05 11:46:42 -04001266static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 struct nfs_server *server = NFS_SERVER(state->inode);
1269 struct nfs4_exception exception = { };
1270 int err;
1271 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001272 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001273 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001274 break;
1275 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 } while (exception.retry);
1277 return err;
1278}
1279
Trond Myklebust864472e2006-01-03 09:55:15 +01001280static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1281{
1282 struct nfs_open_context *ctx;
1283 int ret;
1284
1285 ctx = nfs4_state_find_open_context(state);
1286 if (IS_ERR(ctx))
1287 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001288 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001289 put_nfs_open_context(ctx);
1290 return ret;
1291}
1292
Trond Myklebust13437e12007-07-06 15:10:43 -04001293static 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 -07001294{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001295 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001296 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001298 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1299 if (IS_ERR(opendata))
1300 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001301 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001302 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001303 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001304 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001305 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
Trond Myklebust13437e12007-07-06 15:10:43 -04001308int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
1310 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001311 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 int err;
1313 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001314 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 switch (err) {
1316 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001317 case -ENOENT:
1318 case -ESTALE:
1319 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001320 case -NFS4ERR_BADSESSION:
1321 case -NFS4ERR_BADSLOT:
1322 case -NFS4ERR_BAD_HIGH_SLOT:
1323 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1324 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001325 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001326 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 case -NFS4ERR_STALE_CLIENTID:
1328 case -NFS4ERR_STALE_STATEID:
1329 case -NFS4ERR_EXPIRED:
1330 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001331 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001332 goto out;
1333 case -ERESTARTSYS:
1334 /*
1335 * The show must go on: exit, but mark the
1336 * stateid as needing recovery.
1337 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001338 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001339 case -NFS4ERR_ADMIN_REVOKED:
1340 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001341 nfs_inode_find_state_and_recover(state->inode,
1342 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001343 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001344 case -EKEYEXPIRED:
1345 /*
1346 * User RPCSEC_GSS context has expired.
1347 * We cannot recover this stateid now, so
1348 * skip it and allow recovery thread to
1349 * proceed.
1350 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001351 case -ENOMEM:
1352 err = 0;
1353 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355 err = nfs4_handle_exception(server, err, &exception);
1356 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001357out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return err;
1359}
1360
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001361static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1362{
1363 struct nfs4_opendata *data = calldata;
1364
1365 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001366 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001367 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001368 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001369 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001370 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001371 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001372}
1373
1374static void nfs4_open_confirm_release(void *calldata)
1375{
1376 struct nfs4_opendata *data = calldata;
1377 struct nfs4_state *state = NULL;
1378
1379 /* If this request hasn't been cancelled, do nothing */
1380 if (data->cancelled == 0)
1381 goto out_free;
1382 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001383 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001384 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001385 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001386 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001387 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001388out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001389 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001390}
1391
1392static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001393 .rpc_call_done = nfs4_open_confirm_done,
1394 .rpc_release = nfs4_open_confirm_release,
1395};
1396
1397/*
1398 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1399 */
1400static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1401{
1402 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1403 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001404 struct rpc_message msg = {
1405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1406 .rpc_argp = &data->c_arg,
1407 .rpc_resp = &data->c_res,
1408 .rpc_cred = data->owner->so_cred,
1409 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001410 struct rpc_task_setup task_setup_data = {
1411 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001412 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001413 .callback_ops = &nfs4_open_confirm_ops,
1414 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001415 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001416 .flags = RPC_TASK_ASYNC,
1417 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 int status;
1419
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001420 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001421 data->rpc_done = 0;
1422 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001423 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001424 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001425 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001426 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001427 status = nfs4_wait_for_completion_rpc_task(task);
1428 if (status != 0) {
1429 data->cancelled = 1;
1430 smp_wmb();
1431 } else
1432 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001433 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return status;
1435}
1436
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001437static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001439 struct nfs4_opendata *data = calldata;
1440 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001441
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001442 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1443 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001444 /*
1445 * Check if we still need to send an OPEN call, or if we can use
1446 * a delegation instead.
1447 */
1448 if (data->state != NULL) {
1449 struct nfs_delegation *delegation;
1450
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001451 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001452 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001453 rcu_read_lock();
1454 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001455 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1456 can_open_delegated(delegation, data->o_arg.fmode))
1457 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001458 rcu_read_unlock();
1459 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001460 /* Update sequence id. */
Trond Myklebust48c22eb2012-01-17 22:04:25 -05001461 data->o_arg.id = sp->so_seqid.owner_id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001462 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001463 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001464 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001465 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1466 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001467 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001468 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001469 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001470 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001471 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001472 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001473 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001474unlock_no_action:
1475 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001476out_no_action:
1477 task->tk_action = NULL;
1478
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001479}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001481static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1482{
1483 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1484 nfs4_open_prepare(task, calldata);
1485}
1486
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001487static void nfs4_open_done(struct rpc_task *task, void *calldata)
1488{
1489 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001491 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001492
Trond Myklebust14516c32010-07-31 14:29:06 -04001493 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1494 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001495
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001496 if (task->tk_status == 0) {
1497 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001498 case S_IFREG:
1499 break;
1500 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001501 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001502 break;
1503 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001504 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001505 break;
1506 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001507 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001508 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001509 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001510 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1511 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001512 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001513 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001514}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001515
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001516static void nfs4_open_release(void *calldata)
1517{
1518 struct nfs4_opendata *data = calldata;
1519 struct nfs4_state *state = NULL;
1520
1521 /* If this request hasn't been cancelled, do nothing */
1522 if (data->cancelled == 0)
1523 goto out_free;
1524 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001525 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001526 goto out_free;
1527 /* In case we need an open_confirm, no cleanup! */
1528 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1529 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001530 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001531 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001532 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001533out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001534 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001535}
1536
1537static const struct rpc_call_ops nfs4_open_ops = {
1538 .rpc_call_prepare = nfs4_open_prepare,
1539 .rpc_call_done = nfs4_open_done,
1540 .rpc_release = nfs4_open_release,
1541};
1542
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001543static const struct rpc_call_ops nfs4_recover_open_ops = {
1544 .rpc_call_prepare = nfs4_recover_open_prepare,
1545 .rpc_call_done = nfs4_open_done,
1546 .rpc_release = nfs4_open_release,
1547};
1548
1549static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001550{
1551 struct inode *dir = data->dir->d_inode;
1552 struct nfs_server *server = NFS_SERVER(dir);
1553 struct nfs_openargs *o_arg = &data->o_arg;
1554 struct nfs_openres *o_res = &data->o_res;
1555 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001556 struct rpc_message msg = {
1557 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1558 .rpc_argp = o_arg,
1559 .rpc_resp = o_res,
1560 .rpc_cred = data->owner->so_cred,
1561 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001562 struct rpc_task_setup task_setup_data = {
1563 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001564 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001565 .callback_ops = &nfs4_open_ops,
1566 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001567 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001568 .flags = RPC_TASK_ASYNC,
1569 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001570 int status;
1571
Trond Myklebust9d12b212012-01-17 22:04:25 -05001572 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001573 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001574 data->rpc_done = 0;
1575 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001576 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001577 if (isrecover)
1578 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001579 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001580 if (IS_ERR(task))
1581 return PTR_ERR(task);
1582 status = nfs4_wait_for_completion_rpc_task(task);
1583 if (status != 0) {
1584 data->cancelled = 1;
1585 smp_wmb();
1586 } else
1587 status = data->rpc_status;
1588 rpc_put_task(task);
1589
1590 return status;
1591}
1592
1593static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1594{
1595 struct inode *dir = data->dir->d_inode;
1596 struct nfs_openres *o_res = &data->o_res;
1597 int status;
1598
1599 status = nfs4_run_open_task(data, 1);
1600 if (status != 0 || !data->rpc_done)
1601 return status;
1602
Trond Myklebust6926afd2012-01-07 13:22:46 -05001603 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1604
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001605 nfs_refresh_inode(dir, o_res->dir_attr);
1606
1607 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1608 status = _nfs4_proc_open_confirm(data);
1609 if (status != 0)
1610 return status;
1611 }
1612
1613 return status;
1614}
1615
1616/*
1617 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1618 */
1619static int _nfs4_proc_open(struct nfs4_opendata *data)
1620{
1621 struct inode *dir = data->dir->d_inode;
1622 struct nfs_server *server = NFS_SERVER(dir);
1623 struct nfs_openargs *o_arg = &data->o_arg;
1624 struct nfs_openres *o_res = &data->o_res;
1625 int status;
1626
1627 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001628 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001629 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001630 if (status != 0) {
1631 if (status == -NFS4ERR_BADNAME &&
1632 !(o_arg->open_flags & O_CREAT))
1633 return -ENOENT;
1634 return status;
1635 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001636
Trond Myklebust6926afd2012-01-07 13:22:46 -05001637 nfs_fattr_map_and_free_names(server, &data->f_attr);
1638
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001639 if (o_arg->open_flags & O_CREAT) {
1640 update_changeattr(dir, &o_res->cinfo);
1641 nfs_post_op_update_inode(dir, o_res->dir_attr);
1642 } else
1643 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001644 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1645 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001647 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001649 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
1651 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001652 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001653 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
Andy Adamsond83217c2011-03-01 01:34:17 +00001656static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001657{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001658 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001659 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001660
Trond Myklebusta78cb572009-08-09 15:06:19 -04001661 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001662 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001663 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001664 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001665 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1666 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001667 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001668 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001669 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001670 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001671 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001672}
1673
Andy Adamsond83217c2011-03-01 01:34:17 +00001674static int nfs4_recover_expired_lease(struct nfs_server *server)
1675{
1676 return nfs4_client_recover_expired_lease(server->nfs_client);
1677}
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679/*
1680 * OPEN_EXPIRED:
1681 * reclaim state on the server after a network partition.
1682 * Assumes caller holds the appropriate lock
1683 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001684static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001686 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001687 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001689 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1690 if (IS_ERR(opendata))
1691 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001692 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001693 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001694 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001695 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001696 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001699static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001700{
Trond Myklebust539cd032007-06-05 11:46:42 -04001701 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001702 struct nfs4_exception exception = { };
1703 int err;
1704
1705 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001706 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001707 switch (err) {
1708 default:
1709 goto out;
1710 case -NFS4ERR_GRACE:
1711 case -NFS4ERR_DELAY:
1712 nfs4_handle_exception(server, err, &exception);
1713 err = 0;
1714 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001715 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001716out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001717 return err;
1718}
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1721{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001723 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Trond Myklebust864472e2006-01-03 09:55:15 +01001725 ctx = nfs4_state_find_open_context(state);
1726 if (IS_ERR(ctx))
1727 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001728 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001729 put_nfs_open_context(ctx);
1730 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001733#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001734static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001735{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001736 int status = NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001737 struct nfs_server *server = NFS_SERVER(state->inode);
1738
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001739 if (state->flags & flags) {
1740 status = nfs41_test_stateid(server, stateid);
1741 if (status != NFS_OK) {
1742 nfs41_free_stateid(server, stateid);
1743 state->flags &= ~flags;
1744 }
1745 }
1746 return status;
1747}
1748
1749static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1750{
1751 int deleg_status, open_status;
1752 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1753 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1754
1755 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1756 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1757
1758 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1759 return NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001760 return nfs4_open_expired(sp, state);
1761}
1762#endif
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001765 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1766 * fields corresponding to attributes that were used to store the verifier.
1767 * Make sure we clobber those fields in the later setattr call
1768 */
1769static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1770{
1771 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1772 !(sattr->ia_valid & ATTR_ATIME_SET))
1773 sattr->ia_valid |= ATTR_ATIME;
1774
1775 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1776 !(sattr->ia_valid & ATTR_MTIME_SET))
1777 sattr->ia_valid |= ATTR_MTIME;
1778}
1779
1780/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001781 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001783static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
1785 struct nfs4_state_owner *sp;
1786 struct nfs4_state *state = NULL;
1787 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001788 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001789 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001793 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1794 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1796 goto out_err;
1797 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001798 status = nfs4_recover_expired_lease(server);
1799 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001800 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001801 if (dentry->d_inode != NULL)
1802 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001803 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001804 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001805 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001806 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Al Viro82a2c1b2011-06-22 18:30:55 -04001808 if (dentry->d_inode != NULL)
1809 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001810
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001811 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001813 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001815 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001816 status = PTR_ERR(state);
1817 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001818 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001819 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001820 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001821
1822 if (opendata->o_arg.open_flags & O_EXCL) {
1823 nfs4_exclusive_attrset(opendata, sattr);
1824
1825 nfs_fattr_init(opendata->o_res.f_attr);
1826 status = nfs4_do_setattr(state->inode, cred,
1827 opendata->o_res.f_attr, sattr,
1828 state);
1829 if (status == 0)
1830 nfs_setattr_update_inode(state->inode, sattr);
1831 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1832 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001833 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 *res = state;
1836 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001837err_opendata_put:
1838 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001839err_put_state_owner:
1840 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 *res = NULL;
1843 return status;
1844}
1845
1846
Al Viro82a2c1b2011-06-22 18:30:55 -04001847static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
1849 struct nfs4_exception exception = { };
1850 struct nfs4_state *res;
1851 int status;
1852
1853 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001854 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (status == 0)
1856 break;
1857 /* NOTE: BAD_SEQID means the server and client disagree about the
1858 * book-keeping w.r.t. state-changing operations
1859 * (OPEN/CLOSE/LOCK/LOCKU...)
1860 * It is actually a sign of a bug on the client or on the server.
1861 *
1862 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001863 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 * have unhashed the old state_owner for us, and that we can
1865 * therefore safely retry using a new one. We should still warn
1866 * the user though...
1867 */
1868 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001869 printk(KERN_WARNING "NFS: v4 server %s "
1870 " returned a bad sequence-id error!\n",
1871 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 exception.retry = 1;
1873 continue;
1874 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001875 /*
1876 * BAD_STATEID on OPEN means that the server cancelled our
1877 * state before it received the OPEN_CONFIRM.
1878 * Recover by retrying the request as per the discussion
1879 * on Page 181 of RFC3530.
1880 */
1881 if (status == -NFS4ERR_BAD_STATEID) {
1882 exception.retry = 1;
1883 continue;
1884 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001885 if (status == -EAGAIN) {
1886 /* We must have found a delegation */
1887 exception.retry = 1;
1888 continue;
1889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1891 status, &exception));
1892 } while (exception.retry);
1893 return res;
1894}
1895
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001896static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1897 struct nfs_fattr *fattr, struct iattr *sattr,
1898 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001900 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001902 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 .iap = sattr,
1904 .server = server,
1905 .bitmask = server->attr_bitmask,
1906 };
1907 struct nfs_setattrres res = {
1908 .fattr = fattr,
1909 .server = server,
1910 };
1911 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001912 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1913 .rpc_argp = &arg,
1914 .rpc_resp = &res,
1915 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001917 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001918 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Trond Myklebust0e574af2005-10-27 22:12:38 -04001920 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001922 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1923 /* Use that stateid */
1924 } else if (state != NULL) {
Trond Myklebust1e3987c2012-03-04 18:13:56 -05001925 nfs4_select_rw_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001926 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05001927 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Bryan Schumaker7c513052011-03-24 17:12:24 +00001929 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001930 if (status == 0 && state != NULL)
1931 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001932 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001935static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1936 struct nfs_fattr *fattr, struct iattr *sattr,
1937 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001939 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001940 struct nfs4_exception exception = {
1941 .state = state,
1942 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 int err;
1944 do {
1945 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001946 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 &exception);
1948 } while (exception.retry);
1949 return err;
1950}
1951
1952struct nfs4_closedata {
1953 struct inode *inode;
1954 struct nfs4_state *state;
1955 struct nfs_closeargs arg;
1956 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001957 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001958 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001959 bool roc;
1960 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961};
1962
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001963static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001964{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001965 struct nfs4_closedata *calldata = data;
1966 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001967 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001968
Fred Isamanf7e89172011-01-06 11:36:32 +00001969 if (calldata->roc)
1970 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001971 nfs4_put_open_state(calldata->state);
1972 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001973 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001974 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001975 kfree(calldata);
1976}
1977
Trond Myklebust88069f72009-12-08 08:33:16 -05001978static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1979 fmode_t fmode)
1980{
1981 spin_lock(&state->owner->so_lock);
1982 if (!(fmode & FMODE_READ))
1983 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1984 if (!(fmode & FMODE_WRITE))
1985 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1986 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1987 spin_unlock(&state->owner->so_lock);
1988}
1989
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001990static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001992 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 struct nfs_server *server = NFS_SERVER(calldata->inode);
1995
Chuck Levera3ca5652012-03-01 17:00:40 -05001996 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04001997 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1998 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 /* hmm. we are done with the inode, and in the process of freeing
2000 * the state_owner. we keep this around to process errors
2001 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 switch (task->tk_status) {
2003 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002004 if (calldata->roc)
2005 pnfs_roc_set_barrier(state->inode,
2006 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002007 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002008 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002009 nfs4_close_clear_stateid_flags(state,
2010 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 break;
2012 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002013 case -NFS4ERR_OLD_STATEID:
2014 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002016 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002017 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002019 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2020 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002022 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002023 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002024 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002027static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002029 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002030 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002031 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002032
Chuck Levera3ca5652012-03-01 17:00:40 -05002033 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002034 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002035 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002036
Trond Myklebust88069f72009-12-08 08:33:16 -05002037 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2038 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002039 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002040 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002041 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002042 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002043 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2044 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2045 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002046 }
2047 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002048 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2049 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2050 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002051 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002052 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002053 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002054
2055 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002056 /* Note: exit _without_ calling nfs4_close_done */
2057 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002058 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002059 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002060
Fred Isamanf7e89172011-01-06 11:36:32 +00002061 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002062 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002063 if (calldata->roc &&
2064 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2065 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2066 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002067 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002068 }
2069 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002070
Trond Myklebust516a6af2005-10-27 22:12:41 -04002071 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002072 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04002073 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002074 &calldata->arg.seq_args,
2075 &calldata->res.seq_res,
2076 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002077 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002078 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002079out:
2080 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081}
2082
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002083static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002084 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002085 .rpc_call_done = nfs4_close_done,
2086 .rpc_release = nfs4_free_closedata,
2087};
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089/*
2090 * It is possible for data to be read/written from a mem-mapped file
2091 * after the sys_close call (which hits the vfs layer as a flush).
2092 * This means that we can't safely call nfsv4 close on a file until
2093 * the inode is cleared. This in turn means that we are not good
2094 * NFSv4 citizens - we do not indicate to the server to update the file's
2095 * share state even when we are done with one of the three share
2096 * stateid's in the inode.
2097 *
2098 * NOTE: Caller must be holding the sp->so_owner semaphore!
2099 */
Al Viro643168c2011-06-22 18:20:23 -04002100int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002102 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002104 struct nfs4_state_owner *sp = state->owner;
2105 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002106 struct rpc_message msg = {
2107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2108 .rpc_cred = state->owner->so_cred,
2109 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002110 struct rpc_task_setup task_setup_data = {
2111 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002112 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002113 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002114 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002115 .flags = RPC_TASK_ASYNC,
2116 };
Trond Myklebust95121352005-10-18 14:20:12 -07002117 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002119 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002121 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002122 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002123 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002125 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002126 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002128 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002129 if (calldata->arg.seqid == NULL)
2130 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002131 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002132 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002133 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002134 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002135 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002136 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002137 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002138
Trond Myklebust1174dd12010-12-21 10:52:24 -05002139 msg.rpc_argp = &calldata->arg;
2140 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002141 task_setup_data.callback_data = calldata;
2142 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002143 if (IS_ERR(task))
2144 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002145 status = 0;
2146 if (wait)
2147 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002148 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002149 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002150out_free_calldata:
2151 kfree(calldata);
2152out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002153 if (roc)
2154 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002155 nfs4_put_open_state(state);
2156 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002157 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158}
2159
Trond Myklebust2b484292010-09-17 10:56:51 -04002160static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002161nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 struct nfs4_state *state;
2164
Trond Myklebust565277f2007-10-15 18:17:53 -04002165 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002166 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002167 if (IS_ERR(state))
2168 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002169 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002170 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
Trond Myklebust1185a552009-12-03 15:54:02 -05002173static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002174{
2175 if (ctx->state == NULL)
2176 return;
2177 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002178 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002179 else
Al Viro643168c2011-06-22 18:20:23 -04002180 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002181}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
2183static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2184{
Benny Halevy43652ad2009-04-01 09:21:54 -04002185 struct nfs4_server_caps_arg args = {
2186 .fhandle = fhandle,
2187 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 struct nfs4_server_caps_res res = {};
2189 struct rpc_message msg = {
2190 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002191 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 .rpc_resp = &res,
2193 };
2194 int status;
2195
Bryan Schumaker7c513052011-03-24 17:12:24 +00002196 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if (status == 0) {
2198 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002199 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2200 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2201 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2202 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2203 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2205 server->caps |= NFS_CAP_ACLS;
2206 if (res.has_links != 0)
2207 server->caps |= NFS_CAP_HARDLINKS;
2208 if (res.has_symlinks != 0)
2209 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002210 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2211 server->caps |= NFS_CAP_FILEID;
2212 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2213 server->caps |= NFS_CAP_MODE;
2214 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2215 server->caps |= NFS_CAP_NLINK;
2216 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2217 server->caps |= NFS_CAP_OWNER;
2218 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2219 server->caps |= NFS_CAP_OWNER_GROUP;
2220 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2221 server->caps |= NFS_CAP_ATIME;
2222 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2223 server->caps |= NFS_CAP_CTIME;
2224 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2225 server->caps |= NFS_CAP_MTIME;
2226
Trond Myklebusta65318b2009-03-11 14:10:28 -04002227 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2228 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2229 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002231 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 return status;
2235}
2236
Trond Myklebust55a97592006-06-09 09:34:19 -04002237int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
2239 struct nfs4_exception exception = { };
2240 int err;
2241 do {
2242 err = nfs4_handle_exception(server,
2243 _nfs4_server_capabilities(server, fhandle),
2244 &exception);
2245 } while (exception.retry);
2246 return err;
2247}
2248
2249static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2250 struct nfs_fsinfo *info)
2251{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 struct nfs4_lookup_root_arg args = {
2253 .bitmask = nfs4_fattr_bitmap,
2254 };
2255 struct nfs4_lookup_res res = {
2256 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002257 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 .fh = fhandle,
2259 };
2260 struct rpc_message msg = {
2261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2262 .rpc_argp = &args,
2263 .rpc_resp = &res,
2264 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002265
Trond Myklebust0e574af2005-10-27 22:12:38 -04002266 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002267 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268}
2269
2270static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2271 struct nfs_fsinfo *info)
2272{
2273 struct nfs4_exception exception = { };
2274 int err;
2275 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002276 err = _nfs4_lookup_root(server, fhandle, info);
2277 switch (err) {
2278 case 0:
2279 case -NFS4ERR_WRONGSEC:
2280 break;
2281 default:
2282 err = nfs4_handle_exception(server, err, &exception);
2283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 } while (exception.retry);
2285 return err;
2286}
2287
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002288static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2289 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2290{
2291 struct rpc_auth *auth;
2292 int ret;
2293
2294 auth = rpcauth_create(flavor, server->client);
2295 if (!auth) {
2296 ret = -EIO;
2297 goto out;
2298 }
2299 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002300out:
2301 return ret;
2302}
2303
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002304static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002305 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002307 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002308 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002310 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2311 flav_array[len] = RPC_AUTH_NULL;
2312 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002313
2314 for (i = 0; i < len; i++) {
2315 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002316 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002317 continue;
2318 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002319 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002320 /*
2321 * -EACCESS could mean that the user doesn't have correct permissions
2322 * to access the mount. It could also mean that we tried to mount
2323 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2324 * existing mount programs don't handle -EACCES very well so it should
2325 * be mapped to -EPERM instead.
2326 */
2327 if (status == -EACCES)
2328 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002329 return status;
2330}
2331
2332/*
2333 * get the file handle for the "/" directory on the server
2334 */
2335static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2336 struct nfs_fsinfo *info)
2337{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002338 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002339 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002340 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2341 /*
2342 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2343 * by nfs4_map_errors() as this function exits.
2344 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002345 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 if (status == 0)
2347 status = nfs4_server_capabilities(server, fhandle);
2348 if (status == 0)
2349 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002350 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351}
2352
Manoj Naik6b97fd32006-06-09 09:34:29 -04002353/*
2354 * Get locations and (maybe) other attributes of a referral.
2355 * Note that we'll actually follow the referral later when
2356 * we detect fsid mismatch in inode revalidation
2357 */
Andy Adamson533eb462011-06-13 18:25:56 -04002358static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2359 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002360{
2361 int status = -ENOMEM;
2362 struct page *page = NULL;
2363 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002364
2365 page = alloc_page(GFP_KERNEL);
2366 if (page == NULL)
2367 goto out;
2368 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2369 if (locations == NULL)
2370 goto out;
2371
Trond Myklebustc228fd32007-01-13 02:28:11 -05002372 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002373 if (status != 0)
2374 goto out;
2375 /* Make sure server returned a different fsid for the referral */
2376 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002377 dprintk("%s: server did not return a different fsid for"
2378 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002379 status = -EIO;
2380 goto out;
2381 }
Andy Adamson533eb462011-06-13 18:25:56 -04002382 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2383 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002384
Andy Adamson533eb462011-06-13 18:25:56 -04002385 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002386 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002387 memset(fhandle, 0, sizeof(struct nfs_fh));
2388out:
2389 if (page)
2390 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002391 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002392 return status;
2393}
2394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2396{
2397 struct nfs4_getattr_arg args = {
2398 .fh = fhandle,
2399 .bitmask = server->attr_bitmask,
2400 };
2401 struct nfs4_getattr_res res = {
2402 .fattr = fattr,
2403 .server = server,
2404 };
2405 struct rpc_message msg = {
2406 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2407 .rpc_argp = &args,
2408 .rpc_resp = &res,
2409 };
2410
Trond Myklebust0e574af2005-10-27 22:12:38 -04002411 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002412 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413}
2414
2415static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2416{
2417 struct nfs4_exception exception = { };
2418 int err;
2419 do {
2420 err = nfs4_handle_exception(server,
2421 _nfs4_proc_getattr(server, fhandle, fattr),
2422 &exception);
2423 } while (exception.retry);
2424 return err;
2425}
2426
2427/*
2428 * The file is not closed if it is opened due to the a request to change
2429 * the size of the file. The open call will not be needed once the
2430 * VFS layer lookup-intents are implemented.
2431 *
2432 * Close is called when the inode is destroyed.
2433 * If we haven't opened the file for O_WRONLY, we
2434 * need to in the size_change case to obtain a stateid.
2435 *
2436 * Got race?
2437 * Because OPEN is always done by name in nfsv4, it is
2438 * possible that we opened a different file by the same
2439 * name. We can recognize this race condition, but we
2440 * can't do anything about it besides returning an error.
2441 *
2442 * This will be fixed with VFS changes (lookup-intent).
2443 */
2444static int
2445nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2446 struct iattr *sattr)
2447{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002448 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002449 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002450 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 int status;
2452
Benny Halevy8a1636c2010-07-14 15:43:57 -04002453 if (pnfs_ld_layoutret_on_setattr(inode))
2454 pnfs_return_layout(inode);
2455
Trond Myklebust0e574af2005-10-27 22:12:38 -04002456 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Trond Myklebustd5308382005-11-04 15:33:38 -05002458 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002459 if (sattr->ia_valid & ATTR_FILE) {
2460 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002461
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002462 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002463 if (ctx) {
2464 cred = ctx->cred;
2465 state = ctx->state;
2466 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002467 }
2468
Trond Myklebust536e43d2012-01-17 22:04:26 -05002469 /* Deal with open(O_TRUNC) */
2470 if (sattr->ia_valid & ATTR_OPEN)
2471 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2472
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002473 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002474 if (status == 0)
2475 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 return status;
2477}
2478
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002479static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2480 const struct qstr *name, struct nfs_fh *fhandle,
2481 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002482{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002483 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002484 int status;
2485 struct nfs4_lookup_arg args = {
2486 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002487 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002488 .name = name,
2489 };
2490 struct nfs4_lookup_res res = {
2491 .server = server,
2492 .fattr = fattr,
2493 .fh = fhandle,
2494 };
2495 struct rpc_message msg = {
2496 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2497 .rpc_argp = &args,
2498 .rpc_resp = &res,
2499 };
2500
2501 nfs_fattr_init(fattr);
2502
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002504 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 dprintk("NFS reply lookup: %d\n", status);
2506 return status;
2507}
2508
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002509void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2510{
2511 memset(fh, 0, sizeof(struct nfs_fh));
2512 fattr->fsid.major = 1;
2513 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2514 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2515 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2516 fattr->nlink = 2;
2517}
2518
Bryan Schumaker7c513052011-03-24 17:12:24 +00002519static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2520 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
2522 struct nfs4_exception exception = { };
2523 int err;
2524 do {
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002525 int status;
2526
2527 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2528 switch (status) {
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002529 case -NFS4ERR_BADNAME:
2530 return -ENOENT;
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002531 case -NFS4ERR_MOVED:
Trond Myklebusta6f498a2011-11-07 15:50:32 -05002532 return nfs4_get_referral(dir, name, fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002533 case -NFS4ERR_WRONGSEC:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002534 nfs_fixup_secinfo_attributes(fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002535 }
2536 err = nfs4_handle_exception(NFS_SERVER(dir),
2537 status, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 } while (exception.retry);
2539 return err;
2540}
2541
2542static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2543{
Trond Myklebust76b32992007-08-10 17:45:11 -04002544 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 struct nfs4_accessargs args = {
2546 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002547 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002549 struct nfs4_accessres res = {
2550 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002551 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 struct rpc_message msg = {
2553 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2554 .rpc_argp = &args,
2555 .rpc_resp = &res,
2556 .rpc_cred = entry->cred,
2557 };
2558 int mode = entry->mask;
2559 int status;
2560
2561 /*
2562 * Determine which access bits we want to ask for...
2563 */
2564 if (mode & MAY_READ)
2565 args.access |= NFS4_ACCESS_READ;
2566 if (S_ISDIR(inode->i_mode)) {
2567 if (mode & MAY_WRITE)
2568 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2569 if (mode & MAY_EXEC)
2570 args.access |= NFS4_ACCESS_LOOKUP;
2571 } else {
2572 if (mode & MAY_WRITE)
2573 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2574 if (mode & MAY_EXEC)
2575 args.access |= NFS4_ACCESS_EXECUTE;
2576 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002577
2578 res.fattr = nfs_alloc_fattr();
2579 if (res.fattr == NULL)
2580 return -ENOMEM;
2581
Bryan Schumaker7c513052011-03-24 17:12:24 +00002582 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 if (!status) {
2584 entry->mask = 0;
2585 if (res.access & NFS4_ACCESS_READ)
2586 entry->mask |= MAY_READ;
2587 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2588 entry->mask |= MAY_WRITE;
2589 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2590 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002591 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002593 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 return status;
2595}
2596
2597static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2598{
2599 struct nfs4_exception exception = { };
2600 int err;
2601 do {
2602 err = nfs4_handle_exception(NFS_SERVER(inode),
2603 _nfs4_proc_access(inode, entry),
2604 &exception);
2605 } while (exception.retry);
2606 return err;
2607}
2608
2609/*
2610 * TODO: For the time being, we don't try to get any attributes
2611 * along with any of the zero-copy operations READ, READDIR,
2612 * READLINK, WRITE.
2613 *
2614 * In the case of the first three, we want to put the GETATTR
2615 * after the read-type operation -- this is because it is hard
2616 * to predict the length of a GETATTR response in v4, and thus
2617 * align the READ data correctly. This means that the GETATTR
2618 * may end up partially falling into the page cache, and we should
2619 * shift it into the 'tail' of the xdr_buf before processing.
2620 * To do this efficiently, we need to know the total length
2621 * of data received, which doesn't seem to be available outside
2622 * of the RPC layer.
2623 *
2624 * In the case of WRITE, we also want to put the GETATTR after
2625 * the operation -- in this case because we want to make sure
2626 * we get the post-operation mtime and size. This means that
2627 * we can't use xdr_encode_pages() as written: we need a variant
2628 * of it which would leave room in the 'tail' iovec.
2629 *
2630 * Both of these changes to the XDR layer would in fact be quite
2631 * minor, but I decided to leave them for a subsequent patch.
2632 */
2633static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2634 unsigned int pgbase, unsigned int pglen)
2635{
2636 struct nfs4_readlink args = {
2637 .fh = NFS_FH(inode),
2638 .pgbase = pgbase,
2639 .pglen = pglen,
2640 .pages = &page,
2641 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002642 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 struct rpc_message msg = {
2644 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2645 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002646 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 };
2648
Bryan Schumaker7c513052011-03-24 17:12:24 +00002649 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 -07002650}
2651
2652static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2653 unsigned int pgbase, unsigned int pglen)
2654{
2655 struct nfs4_exception exception = { };
2656 int err;
2657 do {
2658 err = nfs4_handle_exception(NFS_SERVER(inode),
2659 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2660 &exception);
2661 } while (exception.retry);
2662 return err;
2663}
2664
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665/*
2666 * Got race?
2667 * We will need to arrange for the VFS layer to provide an atomic open.
2668 * Until then, this create/open method is prone to inefficiency and race
2669 * conditions due to the lookup, create, and open VFS calls from sys_open()
2670 * placed on the wire.
2671 *
2672 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2673 * The file will be opened again in the subsequent VFS open call
2674 * (nfs4_proc_file_open).
2675 *
2676 * The open for read will just hang around to be used by any process that
2677 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2678 */
2679
2680static int
2681nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002682 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
Al Viro82a2c1b2011-06-22 18:30:55 -04002684 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002686 struct rpc_cred *cred = NULL;
2687 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 int status = 0;
2689
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002690 if (ctx != NULL) {
2691 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002692 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002693 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002695 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002696 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002697 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 if (IS_ERR(state)) {
2699 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002700 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002702 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002703 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002704 if (ctx != NULL)
2705 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002706 else
Al Viro643168c2011-06-22 18:20:23 -04002707 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708out:
2709 return status;
2710}
2711
2712static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2713{
Trond Myklebust16e42952005-10-27 22:12:44 -04002714 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002715 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002717 .name.len = name->len,
2718 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002719 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002721 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002722 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002723 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002725 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2726 .rpc_argp = &args,
2727 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002729 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Trond Myklebustd3468902010-04-16 16:22:50 -04002731 res.dir_attr = nfs_alloc_fattr();
2732 if (res.dir_attr == NULL)
2733 goto out;
2734
Bryan Schumaker7c513052011-03-24 17:12:24 +00002735 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002736 if (status == 0) {
2737 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002738 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002739 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002740 nfs_free_fattr(res.dir_attr);
2741out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 return status;
2743}
2744
2745static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2746{
2747 struct nfs4_exception exception = { };
2748 int err;
2749 do {
2750 err = nfs4_handle_exception(NFS_SERVER(dir),
2751 _nfs4_proc_remove(dir, name),
2752 &exception);
2753 } while (exception.retry);
2754 return err;
2755}
2756
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002757static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002759 struct nfs_server *server = NFS_SERVER(dir);
2760 struct nfs_removeargs *args = msg->rpc_argp;
2761 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
Trond Myklebusta65318b2009-03-11 14:10:28 -04002763 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002764 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002766 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767}
2768
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002769static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002771 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2772
Trond Myklebust14516c32010-07-31 14:29:06 -04002773 if (!nfs4_sequence_done(task, &res->seq_res))
2774 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002775 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002776 return 0;
2777 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002778 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002779 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780}
2781
Jeff Laytond3d41522010-09-17 17:31:57 -04002782static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2783{
2784 struct nfs_server *server = NFS_SERVER(dir);
2785 struct nfs_renameargs *arg = msg->rpc_argp;
2786 struct nfs_renameres *res = msg->rpc_resp;
2787
2788 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2789 arg->bitmask = server->attr_bitmask;
2790 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002791 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002792}
2793
2794static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2795 struct inode *new_dir)
2796{
2797 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2798
2799 if (!nfs4_sequence_done(task, &res->seq_res))
2800 return 0;
2801 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2802 return 0;
2803
2804 update_changeattr(old_dir, &res->old_cinfo);
2805 nfs_post_op_update_inode(old_dir, res->old_fattr);
2806 update_changeattr(new_dir, &res->new_cinfo);
2807 nfs_post_op_update_inode(new_dir, res->new_fattr);
2808 return 1;
2809}
2810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2812 struct inode *new_dir, struct qstr *new_name)
2813{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002814 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002815 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 .old_dir = NFS_FH(old_dir),
2817 .new_dir = NFS_FH(new_dir),
2818 .old_name = old_name,
2819 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002820 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002822 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002823 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002824 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 struct rpc_message msg = {
2826 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2827 .rpc_argp = &arg,
2828 .rpc_resp = &res,
2829 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002830 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Trond Myklebust011fff72010-04-16 16:22:49 -04002832 res.old_fattr = nfs_alloc_fattr();
2833 res.new_fattr = nfs_alloc_fattr();
2834 if (res.old_fattr == NULL || res.new_fattr == NULL)
2835 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
Bryan Schumaker7c513052011-03-24 17:12:24 +00002837 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 if (!status) {
2839 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002840 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002842 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002844out:
2845 nfs_free_fattr(res.new_fattr);
2846 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 return status;
2848}
2849
2850static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2851 struct inode *new_dir, struct qstr *new_name)
2852{
2853 struct nfs4_exception exception = { };
2854 int err;
2855 do {
2856 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2857 _nfs4_proc_rename(old_dir, old_name,
2858 new_dir, new_name),
2859 &exception);
2860 } while (exception.retry);
2861 return err;
2862}
2863
2864static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2865{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002866 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 struct nfs4_link_arg arg = {
2868 .fh = NFS_FH(inode),
2869 .dir_fh = NFS_FH(dir),
2870 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002871 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002873 struct nfs4_link_res res = {
2874 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002875 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 struct rpc_message msg = {
2877 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2878 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002879 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002881 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
Trond Myklebust136f2622010-04-16 16:22:49 -04002883 res.fattr = nfs_alloc_fattr();
2884 res.dir_attr = nfs_alloc_fattr();
2885 if (res.fattr == NULL || res.dir_attr == NULL)
2886 goto out;
2887
Bryan Schumaker7c513052011-03-24 17:12:24 +00002888 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002889 if (!status) {
2890 update_changeattr(dir, &res.cinfo);
2891 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002892 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002893 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002894out:
2895 nfs_free_fattr(res.dir_attr);
2896 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 return status;
2898}
2899
2900static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2901{
2902 struct nfs4_exception exception = { };
2903 int err;
2904 do {
2905 err = nfs4_handle_exception(NFS_SERVER(inode),
2906 _nfs4_proc_link(inode, dir, name),
2907 &exception);
2908 } while (exception.retry);
2909 return err;
2910}
2911
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002912struct nfs4_createdata {
2913 struct rpc_message msg;
2914 struct nfs4_create_arg arg;
2915 struct nfs4_create_res res;
2916 struct nfs_fh fh;
2917 struct nfs_fattr fattr;
2918 struct nfs_fattr dir_fattr;
2919};
2920
2921static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2922 struct qstr *name, struct iattr *sattr, u32 ftype)
2923{
2924 struct nfs4_createdata *data;
2925
2926 data = kzalloc(sizeof(*data), GFP_KERNEL);
2927 if (data != NULL) {
2928 struct nfs_server *server = NFS_SERVER(dir);
2929
2930 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2931 data->msg.rpc_argp = &data->arg;
2932 data->msg.rpc_resp = &data->res;
2933 data->arg.dir_fh = NFS_FH(dir);
2934 data->arg.server = server;
2935 data->arg.name = name;
2936 data->arg.attrs = sattr;
2937 data->arg.ftype = ftype;
2938 data->arg.bitmask = server->attr_bitmask;
2939 data->res.server = server;
2940 data->res.fh = &data->fh;
2941 data->res.fattr = &data->fattr;
2942 data->res.dir_fattr = &data->dir_fattr;
2943 nfs_fattr_init(data->res.fattr);
2944 nfs_fattr_init(data->res.dir_fattr);
2945 }
2946 return data;
2947}
2948
2949static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2950{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002951 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002952 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002953 if (status == 0) {
2954 update_changeattr(dir, &data->res.dir_cinfo);
2955 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2956 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2957 }
2958 return status;
2959}
2960
2961static void nfs4_free_createdata(struct nfs4_createdata *data)
2962{
2963 kfree(data);
2964}
2965
Chuck Lever4f390c12006-08-22 20:06:22 -04002966static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002967 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002969 struct nfs4_createdata *data;
2970 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Chuck Lever94a6d752006-08-22 20:06:23 -04002972 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002973 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002974
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002975 status = -ENOMEM;
2976 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2977 if (data == NULL)
2978 goto out;
2979
2980 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2981 data->arg.u.symlink.pages = &page;
2982 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002984 status = nfs4_do_create(dir, dentry, data);
2985
2986 nfs4_free_createdata(data);
2987out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 return status;
2989}
2990
Chuck Lever4f390c12006-08-22 20:06:22 -04002991static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002992 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993{
2994 struct nfs4_exception exception = { };
2995 int err;
2996 do {
2997 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002998 _nfs4_proc_symlink(dir, dentry, page,
2999 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 &exception);
3001 } while (exception.retry);
3002 return err;
3003}
3004
3005static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3006 struct iattr *sattr)
3007{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003008 struct nfs4_createdata *data;
3009 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003011 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3012 if (data == NULL)
3013 goto out;
3014
3015 status = nfs4_do_create(dir, dentry, data);
3016
3017 nfs4_free_createdata(data);
3018out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 return status;
3020}
3021
3022static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3023 struct iattr *sattr)
3024{
3025 struct nfs4_exception exception = { };
3026 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003027
3028 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 do {
3030 err = nfs4_handle_exception(NFS_SERVER(dir),
3031 _nfs4_proc_mkdir(dir, dentry, sattr),
3032 &exception);
3033 } while (exception.retry);
3034 return err;
3035}
3036
3037static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003038 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039{
3040 struct inode *dir = dentry->d_inode;
3041 struct nfs4_readdir_arg args = {
3042 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003043 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 .pgbase = 0,
3045 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003046 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003047 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 };
3049 struct nfs4_readdir_res res;
3050 struct rpc_message msg = {
3051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3052 .rpc_argp = &args,
3053 .rpc_resp = &res,
3054 .rpc_cred = cred,
3055 };
3056 int status;
3057
Harvey Harrison3110ff82008-05-02 13:42:44 -07003058 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003059 dentry->d_parent->d_name.name,
3060 dentry->d_name.name,
3061 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3063 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003064 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 -05003065 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003067 status += args.pgbase;
3068 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003069
3070 nfs_invalidate_atime(dir);
3071
Harvey Harrison3110ff82008-05-02 13:42:44 -07003072 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 return status;
3074}
3075
3076static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003077 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078{
3079 struct nfs4_exception exception = { };
3080 int err;
3081 do {
3082 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3083 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003084 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 &exception);
3086 } while (exception.retry);
3087 return err;
3088}
3089
3090static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3091 struct iattr *sattr, dev_t rdev)
3092{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003093 struct nfs4_createdata *data;
3094 int mode = sattr->ia_mode;
3095 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
3097 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3098 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003099
3100 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3101 if (data == NULL)
3102 goto out;
3103
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003105 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003107 data->arg.ftype = NF4BLK;
3108 data->arg.u.device.specdata1 = MAJOR(rdev);
3109 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 }
3111 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003112 data->arg.ftype = NF4CHR;
3113 data->arg.u.device.specdata1 = MAJOR(rdev);
3114 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003117 status = nfs4_do_create(dir, dentry, data);
3118
3119 nfs4_free_createdata(data);
3120out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 return status;
3122}
3123
3124static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3125 struct iattr *sattr, dev_t rdev)
3126{
3127 struct nfs4_exception exception = { };
3128 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003129
3130 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 do {
3132 err = nfs4_handle_exception(NFS_SERVER(dir),
3133 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3134 &exception);
3135 } while (exception.retry);
3136 return err;
3137}
3138
3139static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3140 struct nfs_fsstat *fsstat)
3141{
3142 struct nfs4_statfs_arg args = {
3143 .fh = fhandle,
3144 .bitmask = server->attr_bitmask,
3145 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003146 struct nfs4_statfs_res res = {
3147 .fsstat = fsstat,
3148 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 struct rpc_message msg = {
3150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3151 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003152 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 };
3154
Trond Myklebust0e574af2005-10-27 22:12:38 -04003155 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003156 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157}
3158
3159static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3160{
3161 struct nfs4_exception exception = { };
3162 int err;
3163 do {
3164 err = nfs4_handle_exception(server,
3165 _nfs4_proc_statfs(server, fhandle, fsstat),
3166 &exception);
3167 } while (exception.retry);
3168 return err;
3169}
3170
3171static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3172 struct nfs_fsinfo *fsinfo)
3173{
3174 struct nfs4_fsinfo_arg args = {
3175 .fh = fhandle,
3176 .bitmask = server->attr_bitmask,
3177 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003178 struct nfs4_fsinfo_res res = {
3179 .fsinfo = fsinfo,
3180 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 struct rpc_message msg = {
3182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3183 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003184 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 };
3186
Bryan Schumaker7c513052011-03-24 17:12:24 +00003187 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188}
3189
3190static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3191{
3192 struct nfs4_exception exception = { };
3193 int err;
3194
3195 do {
3196 err = nfs4_handle_exception(server,
3197 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3198 &exception);
3199 } while (exception.retry);
3200 return err;
3201}
3202
3203static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3204{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003205 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3207}
3208
3209static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3210 struct nfs_pathconf *pathconf)
3211{
3212 struct nfs4_pathconf_arg args = {
3213 .fh = fhandle,
3214 .bitmask = server->attr_bitmask,
3215 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003216 struct nfs4_pathconf_res res = {
3217 .pathconf = pathconf,
3218 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 struct rpc_message msg = {
3220 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3221 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003222 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 };
3224
3225 /* None of the pathconf attributes are mandatory to implement */
3226 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3227 memset(pathconf, 0, sizeof(*pathconf));
3228 return 0;
3229 }
3230
Trond Myklebust0e574af2005-10-27 22:12:38 -04003231 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003232 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233}
3234
3235static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3236 struct nfs_pathconf *pathconf)
3237{
3238 struct nfs4_exception exception = { };
3239 int err;
3240
3241 do {
3242 err = nfs4_handle_exception(server,
3243 _nfs4_proc_pathconf(server, fhandle, pathconf),
3244 &exception);
3245 } while (exception.retry);
3246 return err;
3247}
3248
Benny Halevyd20581a2011-05-22 19:52:03 +03003249void __nfs4_read_done_cb(struct nfs_read_data *data)
3250{
3251 nfs_invalidate_atime(data->inode);
3252}
3253
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003254static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255{
Trond Myklebustec06c092006-03-20 13:44:27 -05003256 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003258 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003259 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003260 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003262
Benny Halevyd20581a2011-05-22 19:52:03 +03003263 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003265 renew_lease(server, data->timestamp);
3266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267}
3268
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003269static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3270{
3271
3272 dprintk("--> %s\n", __func__);
3273
3274 if (!nfs4_sequence_done(task, &data->res.seq_res))
3275 return -EAGAIN;
3276
Benny Halevyd20581a2011-05-22 19:52:03 +03003277 return data->read_done_cb ? data->read_done_cb(task, data) :
3278 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003279}
3280
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003281static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003284 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003285 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003286 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287}
3288
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003289/* Reset the the nfs_read_data to send the read to the MDS. */
3290void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3291{
3292 dprintk("%s Reset task for i/o through\n", __func__);
3293 put_lseg(data->lseg);
3294 data->lseg = NULL;
3295 /* offsets will differ in the dense stripe case */
3296 data->args.offset = data->mds_offset;
3297 data->ds_clp = NULL;
3298 data->args.fh = NFS_FH(data->inode);
3299 data->read_done_cb = nfs4_read_done_cb;
3300 task->tk_ops = data->mds_ops;
3301 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3302}
3303EXPORT_SYMBOL_GPL(nfs4_reset_read);
3304
Fred Isamanb029bc92011-03-03 15:13:42 +00003305static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 struct inode *inode = data->inode;
3308
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003309 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003310 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003311 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003313 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003315 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003316 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003317 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318}
3319
Fred Isamanb029bc92011-03-03 15:13:42 +00003320static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3321{
3322 if (!nfs4_sequence_done(task, &data->res.seq_res))
3323 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003324 return data->write_done_cb ? data->write_done_cb(task, data) :
3325 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003326}
3327
Fred Isamana69aef12011-03-03 15:13:47 +00003328/* Reset the the nfs_write_data to send the write to the MDS. */
3329void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3330{
3331 dprintk("%s Reset task for i/o through\n", __func__);
3332 put_lseg(data->lseg);
3333 data->lseg = NULL;
3334 data->ds_clp = NULL;
3335 data->write_done_cb = nfs4_write_done_cb;
3336 data->args.fh = NFS_FH(data->inode);
3337 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3338 data->args.offset = data->mds_offset;
3339 data->res.fattr = &data->fattr;
3340 task->tk_ops = data->mds_ops;
3341 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3342}
3343EXPORT_SYMBOL_GPL(nfs4_reset_write);
3344
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003345static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003347 struct nfs_server *server = NFS_SERVER(data->inode);
3348
Fred Isaman7ffd1062011-03-03 15:13:46 +00003349 if (data->lseg) {
3350 data->args.bitmask = NULL;
3351 data->res.fattr = NULL;
3352 } else
3353 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003354 if (!data->write_done_cb)
3355 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003356 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 data->timestamp = jiffies;
3358
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003359 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003360 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361}
3362
Fred Isaman5f452432011-03-23 13:27:46 +00003363static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003366
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003367 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003368 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003369 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003371 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373}
3374
Fred Isaman5f452432011-03-23 13:27:46 +00003375static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3376{
3377 if (!nfs4_sequence_done(task, &data->res.seq_res))
3378 return -EAGAIN;
3379 return data->write_done_cb(task, data);
3380}
3381
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003382static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003384 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003385
3386 if (data->lseg) {
3387 data->args.bitmask = NULL;
3388 data->res.fattr = NULL;
3389 } else
3390 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003391 if (!data->write_done_cb)
3392 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003393 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003394 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003395 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396}
3397
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003398struct nfs4_renewdata {
3399 struct nfs_client *client;
3400 unsigned long timestamp;
3401};
3402
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403/*
3404 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3405 * standalone procedure for queueing an asynchronous RENEW.
3406 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003407static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003408{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003409 struct nfs4_renewdata *data = calldata;
3410 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003411
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003412 if (atomic_read(&clp->cl_count) > 1)
3413 nfs4_schedule_state_renewal(clp);
3414 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003415 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003416}
3417
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003418static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003420 struct nfs4_renewdata *data = calldata;
3421 struct nfs_client *clp = data->client;
3422 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
3424 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003425 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003426 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3427 return;
3428 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003429 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003430 return;
3431 }
3432 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003434 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435}
3436
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003437static const struct rpc_call_ops nfs4_renew_ops = {
3438 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003439 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003440};
3441
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003442static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443{
3444 struct rpc_message msg = {
3445 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3446 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003447 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003449 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003451 if (renew_flags == 0)
3452 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003453 if (!atomic_inc_not_zero(&clp->cl_count))
3454 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003455 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003456 if (data == NULL)
3457 return -ENOMEM;
3458 data->client = clp;
3459 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003461 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462}
3463
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003464static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465{
3466 struct rpc_message msg = {
3467 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3468 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003469 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 };
3471 unsigned long now = jiffies;
3472 int status;
3473
3474 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3475 if (status < 0)
3476 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003477 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 return 0;
3479}
3480
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003481static inline int nfs4_server_supports_acls(struct nfs_server *server)
3482{
3483 return (server->caps & NFS_CAP_ACLS)
3484 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3485 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3486}
3487
3488/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3489 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3490 * the stack.
3491 */
3492#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3493
Neil Hormane9e3d722011-03-04 19:26:03 -05003494static int buf_to_pages_noslab(const void *buf, size_t buflen,
3495 struct page **pages, unsigned int *pgbase)
3496{
3497 struct page *newpage, **spages;
3498 int rc = 0;
3499 size_t len;
3500 spages = pages;
3501
3502 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003503 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003504 newpage = alloc_page(GFP_KERNEL);
3505
3506 if (newpage == NULL)
3507 goto unwind;
3508 memcpy(page_address(newpage), buf, len);
3509 buf += len;
3510 buflen -= len;
3511 *pages++ = newpage;
3512 rc++;
3513 } while (buflen != 0);
3514
3515 return rc;
3516
3517unwind:
3518 for(; rc > 0; rc--)
3519 __free_page(spages[rc-1]);
3520 return -ENOMEM;
3521}
3522
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003523struct nfs4_cached_acl {
3524 int cached;
3525 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003526 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003527};
3528
3529static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003530{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003531 struct nfs_inode *nfsi = NFS_I(inode);
3532
3533 spin_lock(&inode->i_lock);
3534 kfree(nfsi->nfs4_acl);
3535 nfsi->nfs4_acl = acl;
3536 spin_unlock(&inode->i_lock);
3537}
3538
3539static void nfs4_zap_acl_attr(struct inode *inode)
3540{
3541 nfs4_set_cached_acl(inode, NULL);
3542}
3543
3544static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3545{
3546 struct nfs_inode *nfsi = NFS_I(inode);
3547 struct nfs4_cached_acl *acl;
3548 int ret = -ENOENT;
3549
3550 spin_lock(&inode->i_lock);
3551 acl = nfsi->nfs4_acl;
3552 if (acl == NULL)
3553 goto out;
3554 if (buf == NULL) /* user is just asking for length */
3555 goto out_len;
3556 if (acl->cached == 0)
3557 goto out;
3558 ret = -ERANGE; /* see getxattr(2) man page */
3559 if (acl->len > buflen)
3560 goto out;
3561 memcpy(buf, acl->data, acl->len);
3562out_len:
3563 ret = acl->len;
3564out:
3565 spin_unlock(&inode->i_lock);
3566 return ret;
3567}
3568
3569static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3570{
3571 struct nfs4_cached_acl *acl;
3572
3573 if (buf && acl_len <= PAGE_SIZE) {
3574 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3575 if (acl == NULL)
3576 goto out;
3577 acl->cached = 1;
3578 memcpy(acl->data, buf, acl_len);
3579 } else {
3580 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3581 if (acl == NULL)
3582 goto out;
3583 acl->cached = 0;
3584 }
3585 acl->len = acl_len;
3586out:
3587 nfs4_set_cached_acl(inode, acl);
3588}
3589
Andy Adamsonbf118a32011-12-07 11:55:27 -05003590/*
3591 * The getxattr API returns the required buffer length when called with a
3592 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3593 * the required buf. On a NULL buf, we send a page of data to the server
3594 * guessing that the ACL request can be serviced by a page. If so, we cache
3595 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3596 * the cache. If not so, we throw away the page, and cache the required
3597 * length. The next getxattr call will then produce another round trip to
3598 * the server, this time with the input buf of the required size.
3599 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003600static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003601{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003602 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003603 struct nfs_getaclargs args = {
3604 .fh = NFS_FH(inode),
3605 .acl_pages = pages,
3606 .acl_len = buflen,
3607 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003608 struct nfs_getaclres res = {
3609 .acl_len = buflen,
3610 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003611 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003612 struct rpc_message msg = {
3613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3614 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003615 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003616 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003617 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003618
Andy Adamsonbf118a32011-12-07 11:55:27 -05003619 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3620 /* As long as we're doing a round trip to the server anyway,
3621 * let's be prepared for a page of acl data. */
3622 if (npages == 0)
3623 npages = 1;
3624
3625 for (i = 0; i < npages; i++) {
3626 pages[i] = alloc_page(GFP_KERNEL);
3627 if (!pages[i])
3628 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003629 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003630 if (npages > 1) {
3631 /* for decoding across pages */
Trond Myklebust331818f2012-02-03 18:30:53 -05003632 res.acl_scratch = alloc_page(GFP_KERNEL);
3633 if (!res.acl_scratch)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003634 goto out_free;
3635 }
3636 args.acl_len = npages * PAGE_SIZE;
3637 args.acl_pgbase = 0;
3638 /* Let decode_getfacl know not to fail if the ACL data is larger than
3639 * the page we send as a guess */
3640 if (buf == NULL)
3641 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3642 resp_buf = page_address(pages[0]);
3643
Peng Taode040be2012-01-10 22:42:47 +08003644 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003645 __func__, buf, buflen, npages, args.acl_len);
3646 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3647 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003648 if (ret)
3649 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003650
3651 acl_len = res.acl_len - res.acl_data_offset;
3652 if (acl_len > args.acl_len)
3653 nfs4_write_cached_acl(inode, NULL, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003654 else
Andy Adamsonbf118a32011-12-07 11:55:27 -05003655 nfs4_write_cached_acl(inode, resp_buf + res.acl_data_offset,
3656 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003657 if (buf) {
3658 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003659 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003660 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003661 _copy_from_pages(buf, pages, res.acl_data_offset,
3662 res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003663 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003664 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003665out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003666 for (i = 0; i < npages; i++)
3667 if (pages[i])
3668 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003669 if (res.acl_scratch)
3670 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003671 return ret;
3672}
3673
Trond Myklebust16b42892006-08-24 12:27:15 -04003674static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3675{
3676 struct nfs4_exception exception = { };
3677 ssize_t ret;
3678 do {
3679 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3680 if (ret >= 0)
3681 break;
3682 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3683 } while (exception.retry);
3684 return ret;
3685}
3686
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003687static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3688{
3689 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003690 int ret;
3691
3692 if (!nfs4_server_supports_acls(server))
3693 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003694 ret = nfs_revalidate_inode(server, inode);
3695 if (ret < 0)
3696 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003697 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3698 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003699 ret = nfs4_read_cached_acl(inode, buf, buflen);
3700 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003701 /* -ENOENT is returned if there is no ACL or if there is an ACL
3702 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003703 return ret;
3704 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003705}
3706
Trond Myklebust16b42892006-08-24 12:27:15 -04003707static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003708{
3709 struct nfs_server *server = NFS_SERVER(inode);
3710 struct page *pages[NFS4ACL_MAXPAGES];
3711 struct nfs_setaclargs arg = {
3712 .fh = NFS_FH(inode),
3713 .acl_pages = pages,
3714 .acl_len = buflen,
3715 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003716 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003717 struct rpc_message msg = {
3718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3719 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003720 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003721 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003722 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003723
3724 if (!nfs4_server_supports_acls(server))
3725 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003726 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3727 if (i < 0)
3728 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003729 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003730 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003731
3732 /*
3733 * Free each page after tx, so the only ref left is
3734 * held by the network stack
3735 */
3736 for (; i > 0; i--)
3737 put_page(pages[i-1]);
3738
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003739 /*
3740 * Acl update can result in inode attribute update.
3741 * so mark the attribute cache invalid.
3742 */
3743 spin_lock(&inode->i_lock);
3744 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3745 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003746 nfs_access_zap_cache(inode);
3747 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003748 return ret;
3749}
3750
Trond Myklebust16b42892006-08-24 12:27:15 -04003751static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3752{
3753 struct nfs4_exception exception = { };
3754 int err;
3755 do {
3756 err = nfs4_handle_exception(NFS_SERVER(inode),
3757 __nfs4_proc_set_acl(inode, buf, buflen),
3758 &exception);
3759 } while (exception.retry);
3760 return err;
3761}
3762
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003764nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003766 struct nfs_client *clp = server->nfs_client;
3767
3768 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 return 0;
3770 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003771 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003772 case -NFS4ERR_ADMIN_REVOKED:
3773 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003774 if (state != NULL)
3775 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003776 case -NFS4ERR_OPENMODE:
3777 if (state == NULL)
3778 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003779 nfs4_schedule_stateid_recovery(server, state);
3780 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003781 case -NFS4ERR_EXPIRED:
3782 if (state != NULL)
3783 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003785 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003786 nfs4_schedule_lease_recovery(clp);
3787 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003788#if defined(CONFIG_NFS_V4_1)
3789 case -NFS4ERR_BADSESSION:
3790 case -NFS4ERR_BADSLOT:
3791 case -NFS4ERR_BAD_HIGH_SLOT:
3792 case -NFS4ERR_DEADSESSION:
3793 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3794 case -NFS4ERR_SEQ_FALSE_RETRY:
3795 case -NFS4ERR_SEQ_MISORDERED:
3796 dprintk("%s ERROR %d, Reset session\n", __func__,
3797 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003798 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003799 task->tk_status = 0;
3800 return -EAGAIN;
3801#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003803 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003804 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003805 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3807 task->tk_status = 0;
3808 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003809 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 case -NFS4ERR_OLD_STATEID:
3811 task->tk_status = 0;
3812 return -EAGAIN;
3813 }
3814 task->tk_status = nfs4_map_errors(task->tk_status);
3815 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003816wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003817 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003818 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3819 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3820 task->tk_status = 0;
3821 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822}
3823
Chuck Levercd937102012-03-02 17:14:31 -05003824static void nfs4_construct_boot_verifier(struct nfs_client *clp,
3825 nfs4_verifier *bootverf)
3826{
3827 __be32 verf[2];
3828
3829 verf[0] = htonl((u32)clp->cl_boot_time.tv_sec);
3830 verf[1] = htonl((u32)clp->cl_boot_time.tv_nsec);
3831 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3832}
3833
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003834int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3835 unsigned short port, struct rpc_cred *cred,
3836 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837{
3838 nfs4_verifier sc_verifier;
3839 struct nfs4_setclientid setclientid = {
3840 .sc_verifier = &sc_verifier,
3841 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003842 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 };
3844 struct rpc_message msg = {
3845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3846 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003847 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003848 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 int loop = 0;
3851 int status;
3852
Chuck Levercd937102012-03-02 17:14:31 -05003853 nfs4_construct_boot_verifier(clp, &sc_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854
3855 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05003856 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003858 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003859 clp->cl_ipaddr,
3860 rpc_peeraddr2str(clp->cl_rpcclient,
3861 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003862 rpc_peeraddr2str(clp->cl_rpcclient,
3863 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003864 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 clp->cl_id_uniquifier);
3866 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003867 sizeof(setclientid.sc_netid),
3868 rpc_peeraddr2str(clp->cl_rpcclient,
3869 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003871 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05003873 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003875 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 if (status != -NFS4ERR_CLID_INUSE)
3877 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003878 if (loop != 0) {
3879 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003881 }
3882 ++loop;
3883 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 }
3885 return status;
3886}
3887
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003888int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003889 struct nfs4_setclientid_res *arg,
3890 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891{
3892 struct nfs_fsinfo fsinfo;
3893 struct rpc_message msg = {
3894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003895 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003897 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 };
3899 unsigned long now;
3900 int status;
3901
3902 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003903 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 if (status == 0) {
3905 spin_lock(&clp->cl_lock);
3906 clp->cl_lease_time = fsinfo.lease_time * HZ;
3907 clp->cl_last_renewal = now;
3908 spin_unlock(&clp->cl_lock);
3909 }
3910 return status;
3911}
3912
Trond Myklebustfe650402006-01-03 09:55:18 +01003913struct nfs4_delegreturndata {
3914 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003915 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003916 struct nfs_fh fh;
3917 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003918 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003919 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003920 int rpc_status;
3921};
3922
Trond Myklebustfe650402006-01-03 09:55:18 +01003923static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3924{
3925 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003926
Trond Myklebust14516c32010-07-31 14:29:06 -04003927 if (!nfs4_sequence_done(task, &data->res.seq_res))
3928 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003929
Ricardo Labiaga79708862009-12-07 09:23:21 -05003930 switch (task->tk_status) {
3931 case -NFS4ERR_STALE_STATEID:
3932 case -NFS4ERR_EXPIRED:
3933 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003934 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003935 break;
3936 default:
3937 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3938 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003939 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003940 return;
3941 }
3942 }
3943 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003944}
3945
3946static void nfs4_delegreturn_release(void *calldata)
3947{
Trond Myklebustfe650402006-01-03 09:55:18 +01003948 kfree(calldata);
3949}
3950
Andy Adamson938e1012009-04-01 09:22:28 -04003951#if defined(CONFIG_NFS_V4_1)
3952static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3953{
3954 struct nfs4_delegreturndata *d_data;
3955
3956 d_data = (struct nfs4_delegreturndata *)data;
3957
Trond Myklebust035168ab2010-06-16 09:52:26 -04003958 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003959 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003960 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04003961 return;
3962 rpc_call_start(task);
3963}
3964#endif /* CONFIG_NFS_V4_1 */
3965
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003966static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003967#if defined(CONFIG_NFS_V4_1)
3968 .rpc_call_prepare = nfs4_delegreturn_prepare,
3969#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003970 .rpc_call_done = nfs4_delegreturn_done,
3971 .rpc_release = nfs4_delegreturn_release,
3972};
3973
Trond Myklebuste6f81072008-01-24 18:14:34 -05003974static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003975{
3976 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003977 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003978 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003979 struct rpc_message msg = {
3980 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3981 .rpc_cred = cred,
3982 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003983 struct rpc_task_setup task_setup_data = {
3984 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003985 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003986 .callback_ops = &nfs4_delegreturn_ops,
3987 .flags = RPC_TASK_ASYNC,
3988 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003989 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003990
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003991 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003992 if (data == NULL)
3993 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003994 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01003995 data->args.fhandle = &data->fh;
3996 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003997 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003998 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05003999 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004000 data->res.fattr = &data->fattr;
4001 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004002 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004003 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004004 data->rpc_status = 0;
4005
Trond Myklebustc970aa82007-07-14 15:39:59 -04004006 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004007 msg.rpc_argp = &data->args;
4008 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004009 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004010 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004011 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004012 if (!issync)
4013 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004014 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004015 if (status != 0)
4016 goto out;
4017 status = data->rpc_status;
4018 if (status != 0)
4019 goto out;
4020 nfs_refresh_inode(inode, &data->fattr);
4021out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004022 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004023 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024}
4025
Trond Myklebuste6f81072008-01-24 18:14:34 -05004026int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027{
4028 struct nfs_server *server = NFS_SERVER(inode);
4029 struct nfs4_exception exception = { };
4030 int err;
4031 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004032 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 switch (err) {
4034 case -NFS4ERR_STALE_STATEID:
4035 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 case 0:
4037 return 0;
4038 }
4039 err = nfs4_handle_exception(server, err, &exception);
4040 } while (exception.retry);
4041 return err;
4042}
4043
4044#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4045#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4046
4047/*
4048 * sleep, with exponential backoff, and retry the LOCK operation.
4049 */
4050static unsigned long
4051nfs4_set_lock_task_retry(unsigned long timeout)
4052{
Jeff Laytond3103102011-12-01 22:44:39 +01004053 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 timeout <<= 1;
4055 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4056 return NFS4_LOCK_MAXTIMEOUT;
4057 return timeout;
4058}
4059
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4061{
4062 struct inode *inode = state->inode;
4063 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004064 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004065 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004067 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004069 struct nfs_lockt_res res = {
4070 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 };
4072 struct rpc_message msg = {
4073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4074 .rpc_argp = &arg,
4075 .rpc_resp = &res,
4076 .rpc_cred = state->owner->so_cred,
4077 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 struct nfs4_lock_state *lsp;
4079 int status;
4080
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004081 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004082 status = nfs4_set_lock_state(state, request);
4083 if (status != 0)
4084 goto out;
4085 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004086 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004087 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004088 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004089 switch (status) {
4090 case 0:
4091 request->fl_type = F_UNLCK;
4092 break;
4093 case -NFS4ERR_DENIED:
4094 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004096 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004097out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 return status;
4099}
4100
4101static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4102{
4103 struct nfs4_exception exception = { };
4104 int err;
4105
4106 do {
4107 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4108 _nfs4_proc_getlk(state, cmd, request),
4109 &exception);
4110 } while (exception.retry);
4111 return err;
4112}
4113
4114static int do_vfs_lock(struct file *file, struct file_lock *fl)
4115{
4116 int res = 0;
4117 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4118 case FL_POSIX:
4119 res = posix_lock_file_wait(file, fl);
4120 break;
4121 case FL_FLOCK:
4122 res = flock_lock_file_wait(file, fl);
4123 break;
4124 default:
4125 BUG();
4126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 return res;
4128}
4129
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004130struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004131 struct nfs_locku_args arg;
4132 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004133 struct nfs4_lock_state *lsp;
4134 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004135 struct file_lock fl;
4136 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004137 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004138};
4139
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004140static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4141 struct nfs_open_context *ctx,
4142 struct nfs4_lock_state *lsp,
4143 struct nfs_seqid *seqid)
4144{
4145 struct nfs4_unlockdata *p;
4146 struct inode *inode = lsp->ls_state->inode;
4147
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004148 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004149 if (p == NULL)
4150 return NULL;
4151 p->arg.fh = NFS_FH(inode);
4152 p->arg.fl = &p->fl;
4153 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004154 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004155 p->arg.stateid = &lsp->ls_stateid;
4156 p->lsp = lsp;
4157 atomic_inc(&lsp->ls_count);
4158 /* Ensure we don't close file until we're done freeing locks! */
4159 p->ctx = get_nfs_open_context(ctx);
4160 memcpy(&p->fl, fl, sizeof(p->fl));
4161 p->server = NFS_SERVER(inode);
4162 return p;
4163}
4164
Trond Myklebust06f814a2006-01-03 09:55:07 +01004165static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004166{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004167 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004168 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004169 nfs4_put_lock_state(calldata->lsp);
4170 put_nfs_open_context(calldata->ctx);
4171 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004172}
4173
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004174static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004175{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004176 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004177
Trond Myklebust14516c32010-07-31 14:29:06 -04004178 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4179 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004180 switch (task->tk_status) {
4181 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004182 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4183 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004184 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004185 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004186 case -NFS4ERR_BAD_STATEID:
4187 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004188 case -NFS4ERR_STALE_STATEID:
4189 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004190 break;
4191 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004192 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004193 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004194 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004195}
4196
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004197static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004198{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004199 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004201 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004202 return;
4203 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004204 /* Note: exit _without_ running nfs4_locku_done */
4205 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004206 return;
4207 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004208 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004209 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004210 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004211 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004212 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004213 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214}
4215
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004216static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004217 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004218 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004219 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004220};
4221
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004222static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4223 struct nfs_open_context *ctx,
4224 struct nfs4_lock_state *lsp,
4225 struct nfs_seqid *seqid)
4226{
4227 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004228 struct rpc_message msg = {
4229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4230 .rpc_cred = ctx->cred,
4231 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004232 struct rpc_task_setup task_setup_data = {
4233 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004234 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004235 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004236 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004237 .flags = RPC_TASK_ASYNC,
4238 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004239
Frank Filz137d6ac2007-07-09 15:32:29 -07004240 /* Ensure this is an unlock - when canceling a lock, the
4241 * canceled lock is passed in, and it won't be an unlock.
4242 */
4243 fl->fl_type = F_UNLCK;
4244
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004245 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4246 if (data == NULL) {
4247 nfs_free_seqid(seqid);
4248 return ERR_PTR(-ENOMEM);
4249 }
4250
Trond Myklebust9d12b212012-01-17 22:04:25 -05004251 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004252 msg.rpc_argp = &data->arg;
4253 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004254 task_setup_data.callback_data = data;
4255 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004256}
4257
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4259{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004260 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004261 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004262 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004263 struct rpc_task *task;
4264 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004265 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
Trond Myklebust9b073572006-06-29 16:38:34 -04004267 status = nfs4_set_lock_state(state, request);
4268 /* Unlock _before_ we do the RPC call */
4269 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004270 down_read(&nfsi->rwsem);
4271 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4272 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004273 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004274 }
4275 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004276 if (status != 0)
4277 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004278 /* Is this a delegated lock? */
4279 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004280 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004281 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004282 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004283 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004284 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004285 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004286 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004287 status = PTR_ERR(task);
4288 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004289 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004290 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004291 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004292out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004293 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004294 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295}
4296
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004297struct nfs4_lockdata {
4298 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004299 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004300 struct nfs4_lock_state *lsp;
4301 struct nfs_open_context *ctx;
4302 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004303 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004304 int rpc_status;
4305 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004306 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004307};
4308
4309static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004310 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4311 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004312{
4313 struct nfs4_lockdata *p;
4314 struct inode *inode = lsp->ls_state->inode;
4315 struct nfs_server *server = NFS_SERVER(inode);
4316
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004317 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004318 if (p == NULL)
4319 return NULL;
4320
4321 p->arg.fh = NFS_FH(inode);
4322 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004323 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004324 if (p->arg.open_seqid == NULL)
4325 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004326 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004327 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004328 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004329 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004330 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004331 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004332 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004333 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004334 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004335 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004336 atomic_inc(&lsp->ls_count);
4337 p->ctx = get_nfs_open_context(ctx);
4338 memcpy(&p->fl, fl, sizeof(p->fl));
4339 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004340out_free_seqid:
4341 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004342out_free:
4343 kfree(p);
4344 return NULL;
4345}
4346
4347static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4348{
4349 struct nfs4_lockdata *data = calldata;
4350 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
Harvey Harrison3110ff82008-05-02 13:42:44 -07004352 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004353 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4354 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004355 /* Do we need to do an open_to_lock_owner? */
4356 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004357 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4358 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004359 data->arg.open_stateid = &state->stateid;
4360 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004361 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004362 } else
4363 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004364 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004365 if (nfs4_setup_sequence(data->server,
4366 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004367 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004368 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004369 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004370 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004371}
4372
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004373static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4374{
4375 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4376 nfs4_lock_prepare(task, calldata);
4377}
4378
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004379static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4380{
4381 struct nfs4_lockdata *data = calldata;
4382
Harvey Harrison3110ff82008-05-02 13:42:44 -07004383 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004384
Trond Myklebust14516c32010-07-31 14:29:06 -04004385 if (!nfs4_sequence_done(task, &data->res.seq_res))
4386 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004387
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004388 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004389 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004390 if (data->rpc_status == 0)
4391 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4392 else
4393 goto out;
4394 }
4395 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004396 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004397 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004398 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004399 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004400out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004401 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004402}
4403
4404static void nfs4_lock_release(void *calldata)
4405{
4406 struct nfs4_lockdata *data = calldata;
4407
Harvey Harrison3110ff82008-05-02 13:42:44 -07004408 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004409 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004410 if (data->cancelled != 0) {
4411 struct rpc_task *task;
4412 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4413 data->arg.lock_seqid);
4414 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004415 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004416 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004417 } else
4418 nfs_free_seqid(data->arg.lock_seqid);
4419 nfs4_put_lock_state(data->lsp);
4420 put_nfs_open_context(data->ctx);
4421 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004422 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004423}
4424
4425static const struct rpc_call_ops nfs4_lock_ops = {
4426 .rpc_call_prepare = nfs4_lock_prepare,
4427 .rpc_call_done = nfs4_lock_done,
4428 .rpc_release = nfs4_lock_release,
4429};
4430
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004431static const struct rpc_call_ops nfs4_recover_lock_ops = {
4432 .rpc_call_prepare = nfs4_recover_lock_prepare,
4433 .rpc_call_done = nfs4_lock_done,
4434 .rpc_release = nfs4_lock_release,
4435};
4436
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004437static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4438{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004439 switch (error) {
4440 case -NFS4ERR_ADMIN_REVOKED:
4441 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004442 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004443 if (new_lock_owner != 0 ||
4444 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004445 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004446 break;
4447 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004448 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004449 case -NFS4ERR_EXPIRED:
4450 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004451 };
4452}
4453
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004454static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004455{
4456 struct nfs4_lockdata *data;
4457 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004458 struct rpc_message msg = {
4459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4460 .rpc_cred = state->owner->so_cred,
4461 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004462 struct rpc_task_setup task_setup_data = {
4463 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004464 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004465 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004466 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004467 .flags = RPC_TASK_ASYNC,
4468 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004469 int ret;
4470
Harvey Harrison3110ff82008-05-02 13:42:44 -07004471 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004472 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004473 fl->fl_u.nfs4_fl.owner,
4474 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004475 if (data == NULL)
4476 return -ENOMEM;
4477 if (IS_SETLKW(cmd))
4478 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004479 if (recovery_type > NFS_LOCK_NEW) {
4480 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004481 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004482 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4483 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004484 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004485 msg.rpc_argp = &data->arg;
4486 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004487 task_setup_data.callback_data = data;
4488 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004489 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004490 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004491 ret = nfs4_wait_for_completion_rpc_task(task);
4492 if (ret == 0) {
4493 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004494 if (ret)
4495 nfs4_handle_setlk_error(data->server, data->lsp,
4496 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004497 } else
4498 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004499 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004500 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004501 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502}
4503
4504static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4505{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004506 struct nfs_server *server = NFS_SERVER(state->inode);
4507 struct nfs4_exception exception = { };
4508 int err;
4509
4510 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004511 /* Cache the lock if possible... */
4512 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4513 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004514 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004515 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004516 break;
4517 nfs4_handle_exception(server, err, &exception);
4518 } while (exception.retry);
4519 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520}
4521
4522static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4523{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004524 struct nfs_server *server = NFS_SERVER(state->inode);
4525 struct nfs4_exception exception = { };
4526 int err;
4527
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004528 err = nfs4_set_lock_state(state, request);
4529 if (err != 0)
4530 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004531 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004532 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4533 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004534 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004535 switch (err) {
4536 default:
4537 goto out;
4538 case -NFS4ERR_GRACE:
4539 case -NFS4ERR_DELAY:
4540 nfs4_handle_exception(server, err, &exception);
4541 err = 0;
4542 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004543 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004544out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004545 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546}
4547
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004548#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004549static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004550{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004551 int status, ret = NFS_OK;
4552 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004553 struct nfs_server *server = NFS_SERVER(state->inode);
4554
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004555 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4556 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4557 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4558 if (status != NFS_OK) {
4559 nfs41_free_stateid(server, &lsp->ls_stateid);
4560 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4561 ret = status;
4562 }
4563 }
4564 };
4565
4566 return ret;
4567}
4568
4569static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4570{
4571 int status = NFS_OK;
4572
4573 if (test_bit(LK_STATE_IN_USE, &state->flags))
4574 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004575 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004576 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004577 return nfs4_lock_expired(state, request);
4578}
4579#endif
4580
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4582{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004583 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004584 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004585 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004587 if ((fl_flags & FL_POSIX) &&
4588 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4589 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004590 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004591 status = nfs4_set_lock_state(state, request);
4592 if (status != 0)
4593 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004594 request->fl_flags |= FL_ACCESS;
4595 status = do_vfs_lock(request->fl_file, request);
4596 if (status < 0)
4597 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004598 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004599 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004600 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004601 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004602 request->fl_flags = fl_flags & ~FL_SLEEP;
4603 status = do_vfs_lock(request->fl_file, request);
4604 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004605 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004606 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004607 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004608 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004609 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004610 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004611 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004612 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4613 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004614out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004615 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004616out:
4617 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 return status;
4619}
4620
4621static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4622{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004623 struct nfs4_exception exception = {
4624 .state = state,
4625 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 int err;
4627
4628 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004629 err = _nfs4_proc_setlk(state, cmd, request);
4630 if (err == -NFS4ERR_DENIED)
4631 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004633 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 } while (exception.retry);
4635 return err;
4636}
4637
4638static int
4639nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4640{
4641 struct nfs_open_context *ctx;
4642 struct nfs4_state *state;
4643 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4644 int status;
4645
4646 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004647 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 state = ctx->state;
4649
4650 if (request->fl_start < 0 || request->fl_end < 0)
4651 return -EINVAL;
4652
Trond Myklebustd9531262009-07-21 19:22:38 -04004653 if (IS_GETLK(cmd)) {
4654 if (state != NULL)
4655 return nfs4_proc_getlk(state, F_GETLK, request);
4656 return 0;
4657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
4659 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4660 return -EINVAL;
4661
Trond Myklebustd9531262009-07-21 19:22:38 -04004662 if (request->fl_type == F_UNLCK) {
4663 if (state != NULL)
4664 return nfs4_proc_unlck(state, cmd, request);
4665 return 0;
4666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
Trond Myklebustd9531262009-07-21 19:22:38 -04004668 if (state == NULL)
4669 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 do {
4671 status = nfs4_proc_setlk(state, cmd, request);
4672 if ((status != -EAGAIN) || IS_SETLK(cmd))
4673 break;
4674 timeout = nfs4_set_lock_task_retry(timeout);
4675 status = -ERESTARTSYS;
4676 if (signalled())
4677 break;
4678 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 return status;
4680}
4681
Trond Myklebust888e6942005-11-04 15:38:11 -05004682int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4683{
4684 struct nfs_server *server = NFS_SERVER(state->inode);
4685 struct nfs4_exception exception = { };
4686 int err;
4687
4688 err = nfs4_set_lock_state(state, fl);
4689 if (err != 0)
4690 goto out;
4691 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004692 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004693 switch (err) {
4694 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004695 printk(KERN_ERR "NFS: %s: unhandled error "
4696 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004697 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004698 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004699 goto out;
4700 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004701 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004702 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004703 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004704 nfs4_schedule_lease_recovery(server->nfs_client);
4705 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004706 case -NFS4ERR_BADSESSION:
4707 case -NFS4ERR_BADSLOT:
4708 case -NFS4ERR_BAD_HIGH_SLOT:
4709 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4710 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004711 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004712 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004713 case -ERESTARTSYS:
4714 /*
4715 * The show must go on: exit, but mark the
4716 * stateid as needing recovery.
4717 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004718 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004719 case -NFS4ERR_ADMIN_REVOKED:
4720 case -NFS4ERR_BAD_STATEID:
4721 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004722 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004723 err = 0;
4724 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004725 case -EKEYEXPIRED:
4726 /*
4727 * User RPCSEC_GSS context has expired.
4728 * We cannot recover this stateid now, so
4729 * skip it and allow recovery thread to
4730 * proceed.
4731 */
4732 err = 0;
4733 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004734 case -ENOMEM:
4735 case -NFS4ERR_DENIED:
4736 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4737 err = 0;
4738 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004739 case -NFS4ERR_DELAY:
4740 break;
4741 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004742 err = nfs4_handle_exception(server, err, &exception);
4743 } while (exception.retry);
4744out:
4745 return err;
4746}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004747
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004748static void nfs4_release_lockowner_release(void *calldata)
4749{
4750 kfree(calldata);
4751}
4752
4753const struct rpc_call_ops nfs4_release_lockowner_ops = {
4754 .rpc_release = nfs4_release_lockowner_release,
4755};
4756
4757void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4758{
4759 struct nfs_server *server = lsp->ls_state->owner->so_server;
4760 struct nfs_release_lockowner_args *args;
4761 struct rpc_message msg = {
4762 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4763 };
4764
4765 if (server->nfs_client->cl_mvops->minor_version != 0)
4766 return;
4767 args = kmalloc(sizeof(*args), GFP_NOFS);
4768 if (!args)
4769 return;
4770 args->lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004771 args->lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004772 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004773 msg.rpc_argp = args;
4774 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4775}
4776
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004777#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4778
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004779static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4780 const void *buf, size_t buflen,
4781 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004782{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004783 if (strcmp(key, "") != 0)
4784 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004785
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004786 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004787}
4788
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004789static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4790 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004791{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004792 if (strcmp(key, "") != 0)
4793 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004794
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004795 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004796}
4797
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004798static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4799 size_t list_len, const char *name,
4800 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004801{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004802 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004803
J. Bruce Fields096455a2006-03-20 23:23:42 -05004804 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4805 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004806
4807 if (list && len <= list_len)
4808 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004809 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004810}
4811
Andy Adamson533eb462011-06-13 18:25:56 -04004812/*
4813 * nfs_fhget will use either the mounted_on_fileid or the fileid
4814 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004815static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4816{
Andy Adamson533eb462011-06-13 18:25:56 -04004817 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4818 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4819 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05004820 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004821 return;
4822
4823 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05004824 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004825 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4826 fattr->nlink = 2;
4827}
4828
Trond Myklebust56659e92007-07-17 21:52:39 -04004829int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004830 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004831{
4832 struct nfs_server *server = NFS_SERVER(dir);
4833 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004834 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004835 };
4836 struct nfs4_fs_locations_arg args = {
4837 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004838 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004839 .page = page,
4840 .bitmask = bitmask,
4841 };
Benny Halevy22958462009-04-01 09:22:02 -04004842 struct nfs4_fs_locations_res res = {
4843 .fs_locations = fs_locations,
4844 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004845 struct rpc_message msg = {
4846 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4847 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004848 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004849 };
4850 int status;
4851
Harvey Harrison3110ff82008-05-02 13:42:44 -07004852 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004853
4854 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4855 * is not supported */
4856 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4857 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4858 else
4859 bitmask[0] |= FATTR4_WORD0_FILEID;
4860
Trond Myklebustc228fd32007-01-13 02:28:11 -05004861 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004862 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004863 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004864 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004865 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004866 return status;
4867}
4868
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004869static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4870{
4871 int status;
4872 struct nfs4_secinfo_arg args = {
4873 .dir_fh = NFS_FH(dir),
4874 .name = name,
4875 };
4876 struct nfs4_secinfo_res res = {
4877 .flavors = flavors,
4878 };
4879 struct rpc_message msg = {
4880 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4881 .rpc_argp = &args,
4882 .rpc_resp = &res,
4883 };
4884
4885 dprintk("NFS call secinfo %s\n", name->name);
4886 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4887 dprintk("NFS reply secinfo: %d\n", status);
4888 return status;
4889}
4890
4891int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4892{
4893 struct nfs4_exception exception = { };
4894 int err;
4895 do {
4896 err = nfs4_handle_exception(NFS_SERVER(dir),
4897 _nfs4_proc_secinfo(dir, name, flavors),
4898 &exception);
4899 } while (exception.retry);
4900 return err;
4901}
4902
Andy Adamson557134a2009-04-01 09:21:53 -04004903#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004904/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004905 * Check the exchange flags returned by the server for invalid flags, having
4906 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4907 * DS flags set.
4908 */
4909static int nfs4_check_cl_exchange_flags(u32 flags)
4910{
4911 if (flags & ~EXCHGID4_FLAG_MASK_R)
4912 goto out_inval;
4913 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4914 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4915 goto out_inval;
4916 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4917 goto out_inval;
4918 return NFS_OK;
4919out_inval:
4920 return -NFS4ERR_INVAL;
4921}
4922
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004923static bool
4924nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4925{
4926 if (a->server_scope_sz == b->server_scope_sz &&
4927 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4928 return true;
4929
4930 return false;
4931}
4932
Andy Adamson357f54d2010-12-14 10:11:57 -05004933/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004934 * nfs4_proc_exchange_id()
4935 *
4936 * Since the clientid has expired, all compounds using sessions
4937 * associated with the stale clientid will be returning
4938 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4939 * be in some phase of session reset.
4940 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004941int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004942{
4943 nfs4_verifier verifier;
4944 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05004945 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004946 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004947 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004948 };
4949 struct nfs41_exchange_id_res res = {
4950 .client = clp,
4951 };
4952 int status;
4953 struct rpc_message msg = {
4954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4955 .rpc_argp = &args,
4956 .rpc_resp = &res,
4957 .rpc_cred = cred,
4958 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04004959
4960 dprintk("--> %s\n", __func__);
4961 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004962
Chuck Levercd937102012-03-02 17:14:31 -05004963 nfs4_construct_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004964
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004965 args.id_len = scnprintf(args.id, sizeof(args.id),
4966 "%s/%s.%s/%u",
4967 clp->cl_ipaddr,
4968 init_utsname()->nodename,
4969 init_utsname()->domainname,
4970 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004971
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004972 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05004973 if (unlikely(!res.server_scope)) {
4974 status = -ENOMEM;
4975 goto out;
4976 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004977
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05004978 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
4979 if (unlikely(!res.impl_id)) {
4980 status = -ENOMEM;
4981 goto out_server_scope;
4982 }
4983
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004984 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004985 if (!status)
4986 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004987
4988 if (!status) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05004989 /* use the most recent implementation id */
4990 kfree(clp->impl_id);
4991 clp->impl_id = res.impl_id;
4992 } else
4993 kfree(res.impl_id);
4994
4995 if (!status) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004996 if (clp->server_scope &&
4997 !nfs41_same_server_scope(clp->server_scope,
4998 res.server_scope)) {
4999 dprintk("%s: server_scope mismatch detected\n",
5000 __func__);
5001 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5002 kfree(clp->server_scope);
5003 clp->server_scope = NULL;
5004 }
5005
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05005006 if (!clp->server_scope) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005007 clp->server_scope = res.server_scope;
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05005008 goto out;
5009 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005010 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005011
5012out_server_scope:
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05005013 kfree(res.server_scope);
5014out:
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005015 if (clp->impl_id)
5016 dprintk("%s: Server Implementation ID: "
5017 "domain: %s, name: %s, date: %llu,%u\n",
5018 __func__, clp->impl_id->domain, clp->impl_id->name,
5019 clp->impl_id->date.seconds,
5020 clp->impl_id->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005021 dprintk("<-- %s status= %d\n", __func__, status);
5022 return status;
5023}
5024
Andy Adamson2050f0c2009-04-01 09:22:30 -04005025struct nfs4_get_lease_time_data {
5026 struct nfs4_get_lease_time_args *args;
5027 struct nfs4_get_lease_time_res *res;
5028 struct nfs_client *clp;
5029};
5030
5031static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5032 void *calldata)
5033{
5034 int ret;
5035 struct nfs4_get_lease_time_data *data =
5036 (struct nfs4_get_lease_time_data *)calldata;
5037
5038 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005039 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005040 /* just setup sequence, do not trigger session recovery
5041 since we're invoked within one */
5042 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005043 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005044 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005045
5046 BUG_ON(ret == -EAGAIN);
5047 rpc_call_start(task);
5048 dprintk("<-- %s\n", __func__);
5049}
5050
5051/*
5052 * Called from nfs4_state_manager thread for session setup, so don't recover
5053 * from sequence operation or clientid errors.
5054 */
5055static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5056{
5057 struct nfs4_get_lease_time_data *data =
5058 (struct nfs4_get_lease_time_data *)calldata;
5059
5060 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005061 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5062 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005063 switch (task->tk_status) {
5064 case -NFS4ERR_DELAY:
5065 case -NFS4ERR_GRACE:
5066 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5067 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5068 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005069 /* fall through */
5070 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005071 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005072 return;
5073 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005074 dprintk("<-- %s\n", __func__);
5075}
5076
5077struct rpc_call_ops nfs4_get_lease_time_ops = {
5078 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5079 .rpc_call_done = nfs4_get_lease_time_done,
5080};
5081
5082int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5083{
5084 struct rpc_task *task;
5085 struct nfs4_get_lease_time_args args;
5086 struct nfs4_get_lease_time_res res = {
5087 .lr_fsinfo = fsinfo,
5088 };
5089 struct nfs4_get_lease_time_data data = {
5090 .args = &args,
5091 .res = &res,
5092 .clp = clp,
5093 };
5094 struct rpc_message msg = {
5095 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5096 .rpc_argp = &args,
5097 .rpc_resp = &res,
5098 };
5099 struct rpc_task_setup task_setup = {
5100 .rpc_client = clp->cl_rpcclient,
5101 .rpc_message = &msg,
5102 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005103 .callback_data = &data,
5104 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005105 };
5106 int status;
5107
Trond Myklebust9d12b212012-01-17 22:04:25 -05005108 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005109 dprintk("--> %s\n", __func__);
5110 task = rpc_run_task(&task_setup);
5111
5112 if (IS_ERR(task))
5113 status = PTR_ERR(task);
5114 else {
5115 status = task->tk_status;
5116 rpc_put_task(task);
5117 }
5118 dprintk("<-- %s return %d\n", __func__, status);
5119
5120 return status;
5121}
5122
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005123static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5124{
5125 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5126}
5127
5128static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5129 struct nfs4_slot *new,
5130 u32 max_slots,
5131 u32 ivalue)
5132{
5133 struct nfs4_slot *old = NULL;
5134 u32 i;
5135
5136 spin_lock(&tbl->slot_tbl_lock);
5137 if (new) {
5138 old = tbl->slots;
5139 tbl->slots = new;
5140 tbl->max_slots = max_slots;
5141 }
5142 tbl->highest_used_slotid = -1; /* no slot is currently used */
5143 for (i = 0; i < tbl->max_slots; i++)
5144 tbl->slots[i].seq_nr = ivalue;
5145 spin_unlock(&tbl->slot_tbl_lock);
5146 kfree(old);
5147}
5148
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005149/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005150 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005151 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005152static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5153 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005154{
Andy Adamson104aeba2010-01-14 17:45:10 -05005155 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005156 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005157
Andy Adamson104aeba2010-01-14 17:45:10 -05005158 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5159 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005160
Andy Adamson104aeba2010-01-14 17:45:10 -05005161 /* Does the newly negotiated max_reqs match the existing slot table? */
5162 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005163 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005164 if (!new)
5165 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005166 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005167 ret = 0;
5168
5169 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005170 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5171 tbl, tbl->slots, tbl->max_slots);
5172out:
5173 dprintk("<-- %s: return %d\n", __func__, ret);
5174 return ret;
5175}
5176
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005177/* Destroy the slot table */
5178static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5179{
5180 if (session->fc_slot_table.slots != NULL) {
5181 kfree(session->fc_slot_table.slots);
5182 session->fc_slot_table.slots = NULL;
5183 }
5184 if (session->bc_slot_table.slots != NULL) {
5185 kfree(session->bc_slot_table.slots);
5186 session->bc_slot_table.slots = NULL;
5187 }
5188 return;
5189}
5190
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005191/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005192 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005193 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005194static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005195{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005196 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005197 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005198
Andy Adamsonaacd5532011-11-09 13:58:21 -05005199 dprintk("--> %s\n", __func__);
5200 /* Fore channel */
5201 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005202 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5203 if (status) /* -ENOMEM */
5204 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005205 /* Back channel */
5206 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005207 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5208 if (status && tbl->slots == NULL)
5209 /* Fore and back channel share a connection so get
5210 * both slot tables or neither */
5211 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005212 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005213}
5214
5215struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5216{
5217 struct nfs4_session *session;
5218 struct nfs4_slot_table *tbl;
5219
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005220 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005221 if (!session)
5222 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005223
Andy Adamson557134a2009-04-01 09:21:53 -04005224 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005225 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005226 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005227 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005228 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005229
5230 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005231 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005232 spin_lock_init(&tbl->slot_tbl_lock);
5233 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005234 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005235
Trond Myklebust1055d762010-06-16 09:52:27 -04005236 session->session_state = 1<<NFS4_SESSION_INITING;
5237
Andy Adamson557134a2009-04-01 09:21:53 -04005238 session->clp = clp;
5239 return session;
5240}
5241
5242void nfs4_destroy_session(struct nfs4_session *session)
5243{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005244 struct rpc_xprt *xprt;
5245
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005246 nfs4_proc_destroy_session(session);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005247
5248 rcu_read_lock();
5249 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5250 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005251 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005252 __func__, xprt);
5253 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005254 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005255 kfree(session);
5256}
5257
Andy Adamsonfc931582009-04-01 09:22:31 -04005258/*
5259 * Initialize the values to be used by the client in CREATE_SESSION
5260 * If nfs4_init_session set the fore channel request and response sizes,
5261 * use them.
5262 *
5263 * Set the back channel max_resp_sz_cached to zero to force the client to
5264 * always set csa_cachethis to FALSE because the current implementation
5265 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5266 */
5267static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5268{
5269 struct nfs4_session *session = args->client->cl_session;
5270 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5271 mxresp_sz = session->fc_attrs.max_resp_sz;
5272
5273 if (mxrqst_sz == 0)
5274 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5275 if (mxresp_sz == 0)
5276 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5277 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005278 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5279 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005280 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005281 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005282
5283 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005284 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005285 __func__,
5286 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005287 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005288
5289 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005290 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5291 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5292 args->bc_attrs.max_resp_sz_cached = 0;
5293 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5294 args->bc_attrs.max_reqs = 1;
5295
5296 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5297 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5298 __func__,
5299 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5300 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5301 args->bc_attrs.max_reqs);
5302}
5303
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005304static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005305{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005306 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5307 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5308
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005309 if (rcvd->max_resp_sz > sent->max_resp_sz)
5310 return -EINVAL;
5311 /*
5312 * Our requested max_ops is the minimum we need; we're not
5313 * prepared to break up compounds into smaller pieces than that.
5314 * So, no point even trying to continue if the server won't
5315 * cooperate:
5316 */
5317 if (rcvd->max_ops < sent->max_ops)
5318 return -EINVAL;
5319 if (rcvd->max_reqs == 0)
5320 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005321 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5322 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005323 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005324}
5325
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005326static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5327{
5328 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5329 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005330
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005331 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5332 return -EINVAL;
5333 if (rcvd->max_resp_sz < sent->max_resp_sz)
5334 return -EINVAL;
5335 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5336 return -EINVAL;
5337 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005338 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005339 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005340 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005341 return -EINVAL;
5342 return 0;
5343}
Andy Adamson8d353012009-04-01 09:22:32 -04005344
Andy Adamson8d353012009-04-01 09:22:32 -04005345static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5346 struct nfs4_session *session)
5347{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005348 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005349
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005350 ret = nfs4_verify_fore_channel_attrs(args, session);
5351 if (ret)
5352 return ret;
5353 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005354}
5355
Andy Adamsonfc931582009-04-01 09:22:31 -04005356static int _nfs4_proc_create_session(struct nfs_client *clp)
5357{
5358 struct nfs4_session *session = clp->cl_session;
5359 struct nfs41_create_session_args args = {
5360 .client = clp,
5361 .cb_program = NFS4_CALLBACK,
5362 };
5363 struct nfs41_create_session_res res = {
5364 .client = clp,
5365 };
5366 struct rpc_message msg = {
5367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5368 .rpc_argp = &args,
5369 .rpc_resp = &res,
5370 };
5371 int status;
5372
5373 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005374 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005375
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005376 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005377
Andy Adamson8d353012009-04-01 09:22:32 -04005378 if (!status)
5379 /* Verify the session's negotiated channel_attrs values */
5380 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005381 if (!status) {
5382 /* Increment the clientid slot sequence id */
5383 clp->cl_seqid++;
5384 }
5385
5386 return status;
5387}
5388
5389/*
5390 * Issues a CREATE_SESSION operation to the server.
5391 * It is the responsibility of the caller to verify the session is
5392 * expired before calling this routine.
5393 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005394int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005395{
5396 int status;
5397 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005398 struct nfs4_session *session = clp->cl_session;
5399
5400 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5401
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005402 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005403 if (status)
5404 goto out;
5405
Andy Adamsonaacd5532011-11-09 13:58:21 -05005406 /* Init or reset the session slot tables */
5407 status = nfs4_setup_session_slot_tables(session);
5408 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005409 if (status)
5410 goto out;
5411
5412 ptr = (unsigned *)&session->sess_id.data[0];
5413 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5414 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005415out:
5416 dprintk("<-- %s\n", __func__);
5417 return status;
5418}
5419
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005420/*
5421 * Issue the over-the-wire RPC DESTROY_SESSION.
5422 * The caller must serialize access to this routine.
5423 */
5424int nfs4_proc_destroy_session(struct nfs4_session *session)
5425{
5426 int status = 0;
5427 struct rpc_message msg;
5428
5429 dprintk("--> nfs4_proc_destroy_session\n");
5430
5431 /* session is still being setup */
5432 if (session->clp->cl_cons_state != NFS_CS_READY)
5433 return status;
5434
5435 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5436 msg.rpc_argp = session;
5437 msg.rpc_resp = NULL;
5438 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005439 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005440
5441 if (status)
5442 printk(KERN_WARNING
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -05005443 "NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005444 "Session has been destroyed regardless...\n", status);
5445
5446 dprintk("<-- nfs4_proc_destroy_session\n");
5447 return status;
5448}
5449
Trond Myklebustfccba802009-07-21 16:48:07 -04005450int nfs4_init_session(struct nfs_server *server)
5451{
5452 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005453 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005454 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005455 int ret;
5456
5457 if (!nfs4_has_session(clp))
5458 return 0;
5459
Trond Myklebust1055d762010-06-16 09:52:27 -04005460 session = clp->cl_session;
5461 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5462 return 0;
5463
Andy Adamson68bf05e2009-12-15 12:55:02 -05005464 rsize = server->rsize;
5465 if (rsize == 0)
5466 rsize = NFS_MAX_FILE_IO_SIZE;
5467 wsize = server->wsize;
5468 if (wsize == 0)
5469 wsize = NFS_MAX_FILE_IO_SIZE;
5470
Andy Adamson68bf05e2009-12-15 12:55:02 -05005471 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5472 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005473
Trond Myklebustfccba802009-07-21 16:48:07 -04005474 ret = nfs4_recover_expired_lease(server);
5475 if (!ret)
5476 ret = nfs4_check_client_ready(clp);
5477 return ret;
5478}
5479
Andy Adamsond83217c2011-03-01 01:34:17 +00005480int nfs4_init_ds_session(struct nfs_client *clp)
5481{
5482 struct nfs4_session *session = clp->cl_session;
5483 int ret;
5484
5485 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5486 return 0;
5487
5488 ret = nfs4_client_recover_expired_lease(clp);
5489 if (!ret)
5490 /* Test for the DS role */
5491 if (!is_ds_client(clp))
5492 ret = -ENODEV;
5493 if (!ret)
5494 ret = nfs4_check_client_ready(clp);
5495 return ret;
5496
5497}
5498EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5499
5500
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005501/*
5502 * Renew the cl_session lease.
5503 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005504struct nfs4_sequence_data {
5505 struct nfs_client *clp;
5506 struct nfs4_sequence_args args;
5507 struct nfs4_sequence_res res;
5508};
5509
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005510static void nfs41_sequence_release(void *data)
5511{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005512 struct nfs4_sequence_data *calldata = data;
5513 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005514
Alexandros Batsakis71358402010-02-05 03:45:05 -08005515 if (atomic_read(&clp->cl_count) > 1)
5516 nfs4_schedule_state_renewal(clp);
5517 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005518 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005519}
5520
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005521static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5522{
5523 switch(task->tk_status) {
5524 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005525 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5526 return -EAGAIN;
5527 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005528 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005529 }
5530 return 0;
5531}
5532
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005533static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005534{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005535 struct nfs4_sequence_data *calldata = data;
5536 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005537
Trond Myklebust14516c32010-07-31 14:29:06 -04005538 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5539 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005540
5541 if (task->tk_status < 0) {
5542 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005543 if (atomic_read(&clp->cl_count) == 1)
5544 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005545
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005546 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5547 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005548 return;
5549 }
5550 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005551 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005552out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005553 dprintk("<-- %s\n", __func__);
5554}
5555
5556static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5557{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005558 struct nfs4_sequence_data *calldata = data;
5559 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005560 struct nfs4_sequence_args *args;
5561 struct nfs4_sequence_res *res;
5562
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005563 args = task->tk_msg.rpc_argp;
5564 res = task->tk_msg.rpc_resp;
5565
Trond Myklebust9d12b212012-01-17 22:04:25 -05005566 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005567 return;
5568 rpc_call_start(task);
5569}
5570
5571static const struct rpc_call_ops nfs41_sequence_ops = {
5572 .rpc_call_done = nfs41_sequence_call_done,
5573 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005574 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005575};
5576
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005577static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005578{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005579 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005580 struct rpc_message msg = {
5581 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5582 .rpc_cred = cred,
5583 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005584 struct rpc_task_setup task_setup_data = {
5585 .rpc_client = clp->cl_rpcclient,
5586 .rpc_message = &msg,
5587 .callback_ops = &nfs41_sequence_ops,
5588 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5589 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005590
Alexandros Batsakis71358402010-02-05 03:45:05 -08005591 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005592 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005593 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005594 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005595 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005596 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005597 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005598 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005599 msg.rpc_argp = &calldata->args;
5600 msg.rpc_resp = &calldata->res;
5601 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005602 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005603
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005604 return rpc_run_task(&task_setup_data);
5605}
5606
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005607static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005608{
5609 struct rpc_task *task;
5610 int ret = 0;
5611
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005612 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5613 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005614 task = _nfs41_proc_sequence(clp, cred);
5615 if (IS_ERR(task))
5616 ret = PTR_ERR(task);
5617 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005618 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005619 dprintk("<-- %s status=%d\n", __func__, ret);
5620 return ret;
5621}
5622
5623static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5624{
5625 struct rpc_task *task;
5626 int ret;
5627
5628 task = _nfs41_proc_sequence(clp, cred);
5629 if (IS_ERR(task)) {
5630 ret = PTR_ERR(task);
5631 goto out;
5632 }
5633 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005634 if (!ret) {
5635 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5636
5637 if (task->tk_status == 0)
5638 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005639 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005640 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005641 rpc_put_task(task);
5642out:
5643 dprintk("<-- %s status=%d\n", __func__, ret);
5644 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005645}
5646
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005647struct nfs4_reclaim_complete_data {
5648 struct nfs_client *clp;
5649 struct nfs41_reclaim_complete_args arg;
5650 struct nfs41_reclaim_complete_res res;
5651};
5652
5653static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5654{
5655 struct nfs4_reclaim_complete_data *calldata = data;
5656
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005657 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168ab2010-06-16 09:52:26 -04005658 if (nfs41_setup_sequence(calldata->clp->cl_session,
5659 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005660 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005661 return;
5662
5663 rpc_call_start(task);
5664}
5665
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005666static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5667{
5668 switch(task->tk_status) {
5669 case 0:
5670 case -NFS4ERR_COMPLETE_ALREADY:
5671 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5672 break;
5673 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005674 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005675 /* fall through */
5676 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005677 return -EAGAIN;
5678 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005679 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005680 }
5681 return 0;
5682}
5683
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005684static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5685{
5686 struct nfs4_reclaim_complete_data *calldata = data;
5687 struct nfs_client *clp = calldata->clp;
5688 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5689
5690 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005691 if (!nfs41_sequence_done(task, res))
5692 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005693
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005694 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5695 rpc_restart_call_prepare(task);
5696 return;
5697 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005698 dprintk("<-- %s\n", __func__);
5699}
5700
5701static void nfs4_free_reclaim_complete_data(void *data)
5702{
5703 struct nfs4_reclaim_complete_data *calldata = data;
5704
5705 kfree(calldata);
5706}
5707
5708static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5709 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5710 .rpc_call_done = nfs4_reclaim_complete_done,
5711 .rpc_release = nfs4_free_reclaim_complete_data,
5712};
5713
5714/*
5715 * Issue a global reclaim complete.
5716 */
5717static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5718{
5719 struct nfs4_reclaim_complete_data *calldata;
5720 struct rpc_task *task;
5721 struct rpc_message msg = {
5722 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5723 };
5724 struct rpc_task_setup task_setup_data = {
5725 .rpc_client = clp->cl_rpcclient,
5726 .rpc_message = &msg,
5727 .callback_ops = &nfs4_reclaim_complete_call_ops,
5728 .flags = RPC_TASK_ASYNC,
5729 };
5730 int status = -ENOMEM;
5731
5732 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005733 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005734 if (calldata == NULL)
5735 goto out;
5736 calldata->clp = clp;
5737 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005738
Trond Myklebust9d12b212012-01-17 22:04:25 -05005739 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005740 msg.rpc_argp = &calldata->arg;
5741 msg.rpc_resp = &calldata->res;
5742 task_setup_data.callback_data = calldata;
5743 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005744 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005745 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005746 goto out;
5747 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005748 status = nfs4_wait_for_completion_rpc_task(task);
5749 if (status == 0)
5750 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005751 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005752 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005753out:
5754 dprintk("<-- %s status=%d\n", __func__, status);
5755 return status;
5756}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005757
5758static void
5759nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5760{
5761 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005762 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005763
5764 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005765 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5766 * right now covering the LAYOUTGET we are about to send.
5767 * However, that is not so catastrophic, and there seems
5768 * to be no way to prevent it completely.
5769 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005770 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005771 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005772 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005773 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5774 NFS_I(lgp->args.inode)->layout,
5775 lgp->args.ctx->state)) {
5776 rpc_exit(task, NFS4_OK);
5777 return;
5778 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005779 rpc_call_start(task);
5780}
5781
5782static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5783{
5784 struct nfs4_layoutget *lgp = calldata;
5785 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5786
5787 dprintk("--> %s\n", __func__);
5788
5789 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5790 return;
5791
5792 switch (task->tk_status) {
5793 case 0:
5794 break;
5795 case -NFS4ERR_LAYOUTTRYLATER:
5796 case -NFS4ERR_RECALLCONFLICT:
5797 task->tk_status = -NFS4ERR_DELAY;
5798 /* Fall through */
5799 default:
5800 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5801 rpc_restart_call_prepare(task);
5802 return;
5803 }
5804 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005805 dprintk("<-- %s\n", __func__);
5806}
5807
5808static void nfs4_layoutget_release(void *calldata)
5809{
5810 struct nfs4_layoutget *lgp = calldata;
5811
5812 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005813 put_nfs_open_context(lgp->args.ctx);
5814 kfree(calldata);
5815 dprintk("<-- %s\n", __func__);
5816}
5817
5818static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5819 .rpc_call_prepare = nfs4_layoutget_prepare,
5820 .rpc_call_done = nfs4_layoutget_done,
5821 .rpc_release = nfs4_layoutget_release,
5822};
5823
5824int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5825{
5826 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5827 struct rpc_task *task;
5828 struct rpc_message msg = {
5829 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5830 .rpc_argp = &lgp->args,
5831 .rpc_resp = &lgp->res,
5832 };
5833 struct rpc_task_setup task_setup_data = {
5834 .rpc_client = server->client,
5835 .rpc_message = &msg,
5836 .callback_ops = &nfs4_layoutget_call_ops,
5837 .callback_data = lgp,
5838 .flags = RPC_TASK_ASYNC,
5839 };
5840 int status = 0;
5841
5842 dprintk("--> %s\n", __func__);
5843
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005844 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005845 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05005846 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005847 task = rpc_run_task(&task_setup_data);
5848 if (IS_ERR(task))
5849 return PTR_ERR(task);
5850 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005851 if (status == 0)
5852 status = task->tk_status;
5853 if (status == 0)
5854 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005855 rpc_put_task(task);
5856 dprintk("<-- %s status=%d\n", __func__, status);
5857 return status;
5858}
5859
Benny Halevycbe82602011-05-22 19:52:37 +03005860static void
5861nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5862{
5863 struct nfs4_layoutreturn *lrp = calldata;
5864
5865 dprintk("--> %s\n", __func__);
5866 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005867 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03005868 return;
5869 rpc_call_start(task);
5870}
5871
5872static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5873{
5874 struct nfs4_layoutreturn *lrp = calldata;
5875 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005876 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005877
5878 dprintk("--> %s\n", __func__);
5879
5880 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5881 return;
5882
5883 server = NFS_SERVER(lrp->args.inode);
5884 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005885 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03005886 return;
5887 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005888 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005889 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005890 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005891 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005892 } else
5893 BUG_ON(!list_empty(&lo->plh_segs));
5894 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005895 lo->plh_block_lgets--;
5896 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005897 dprintk("<-- %s\n", __func__);
5898}
5899
5900static void nfs4_layoutreturn_release(void *calldata)
5901{
5902 struct nfs4_layoutreturn *lrp = calldata;
5903
5904 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005905 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005906 kfree(calldata);
5907 dprintk("<-- %s\n", __func__);
5908}
5909
5910static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5911 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5912 .rpc_call_done = nfs4_layoutreturn_done,
5913 .rpc_release = nfs4_layoutreturn_release,
5914};
5915
5916int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5917{
5918 struct rpc_task *task;
5919 struct rpc_message msg = {
5920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5921 .rpc_argp = &lrp->args,
5922 .rpc_resp = &lrp->res,
5923 };
5924 struct rpc_task_setup task_setup_data = {
5925 .rpc_client = lrp->clp->cl_rpcclient,
5926 .rpc_message = &msg,
5927 .callback_ops = &nfs4_layoutreturn_call_ops,
5928 .callback_data = lrp,
5929 };
5930 int status;
5931
5932 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05005933 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03005934 task = rpc_run_task(&task_setup_data);
5935 if (IS_ERR(task))
5936 return PTR_ERR(task);
5937 status = task->tk_status;
5938 dprintk("<-- %s status=%d\n", __func__, status);
5939 rpc_put_task(task);
5940 return status;
5941}
5942
Andy Adamson7f11d8d2011-07-30 20:52:35 -04005943/*
5944 * Retrieve the list of Data Server devices from the MDS.
5945 */
5946static int _nfs4_getdevicelist(struct nfs_server *server,
5947 const struct nfs_fh *fh,
5948 struct pnfs_devicelist *devlist)
5949{
5950 struct nfs4_getdevicelist_args args = {
5951 .fh = fh,
5952 .layoutclass = server->pnfs_curr_ld->id,
5953 };
5954 struct nfs4_getdevicelist_res res = {
5955 .devlist = devlist,
5956 };
5957 struct rpc_message msg = {
5958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5959 .rpc_argp = &args,
5960 .rpc_resp = &res,
5961 };
5962 int status;
5963
5964 dprintk("--> %s\n", __func__);
5965 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5966 &res.seq_res, 0);
5967 dprintk("<-- %s status=%d\n", __func__, status);
5968 return status;
5969}
5970
5971int nfs4_proc_getdevicelist(struct nfs_server *server,
5972 const struct nfs_fh *fh,
5973 struct pnfs_devicelist *devlist)
5974{
5975 struct nfs4_exception exception = { };
5976 int err;
5977
5978 do {
5979 err = nfs4_handle_exception(server,
5980 _nfs4_getdevicelist(server, fh, devlist),
5981 &exception);
5982 } while (exception.retry);
5983
5984 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5985 err, devlist->num_devs);
5986
5987 return err;
5988}
5989EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5990
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005991static int
5992_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5993{
5994 struct nfs4_getdeviceinfo_args args = {
5995 .pdev = pdev,
5996 };
5997 struct nfs4_getdeviceinfo_res res = {
5998 .pdev = pdev,
5999 };
6000 struct rpc_message msg = {
6001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6002 .rpc_argp = &args,
6003 .rpc_resp = &res,
6004 };
6005 int status;
6006
6007 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006008 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006009 dprintk("<-- %s status=%d\n", __func__, status);
6010
6011 return status;
6012}
6013
6014int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6015{
6016 struct nfs4_exception exception = { };
6017 int err;
6018
6019 do {
6020 err = nfs4_handle_exception(server,
6021 _nfs4_proc_getdeviceinfo(server, pdev),
6022 &exception);
6023 } while (exception.retry);
6024 return err;
6025}
6026EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6027
Andy Adamson863a3c62011-03-23 13:27:54 +00006028static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6029{
6030 struct nfs4_layoutcommit_data *data = calldata;
6031 struct nfs_server *server = NFS_SERVER(data->args.inode);
6032
6033 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006034 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006035 return;
6036 rpc_call_start(task);
6037}
6038
6039static void
6040nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6041{
6042 struct nfs4_layoutcommit_data *data = calldata;
6043 struct nfs_server *server = NFS_SERVER(data->args.inode);
6044
6045 if (!nfs4_sequence_done(task, &data->res.seq_res))
6046 return;
6047
6048 switch (task->tk_status) { /* Just ignore these failures */
6049 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6050 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6051 case NFS4ERR_BADLAYOUT: /* no layout */
6052 case NFS4ERR_GRACE: /* loca_recalim always false */
6053 task->tk_status = 0;
6054 }
6055
6056 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006057 rpc_restart_call_prepare(task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006058 return;
6059 }
6060
6061 if (task->tk_status == 0)
6062 nfs_post_op_update_inode_force_wcc(data->args.inode,
6063 data->res.fattr);
6064}
6065
6066static void nfs4_layoutcommit_release(void *calldata)
6067{
6068 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006069 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006070 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006071
Andy Adamsondb29c082011-07-30 20:52:38 -04006072 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006073 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006074 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6075 list_del_init(&lseg->pls_lc_list);
6076 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6077 &lseg->pls_flags))
6078 put_lseg(lseg);
6079 }
Peng Tao92407e72011-10-23 20:21:17 -07006080
6081 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6082 smp_mb__after_clear_bit();
6083 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6084
Andy Adamson863a3c62011-03-23 13:27:54 +00006085 put_rpccred(data->cred);
6086 kfree(data);
6087}
6088
6089static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6090 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6091 .rpc_call_done = nfs4_layoutcommit_done,
6092 .rpc_release = nfs4_layoutcommit_release,
6093};
6094
6095int
Andy Adamsonef311532011-03-12 02:58:10 -05006096nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006097{
6098 struct rpc_message msg = {
6099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6100 .rpc_argp = &data->args,
6101 .rpc_resp = &data->res,
6102 .rpc_cred = data->cred,
6103 };
6104 struct rpc_task_setup task_setup_data = {
6105 .task = &data->task,
6106 .rpc_client = NFS_CLIENT(data->args.inode),
6107 .rpc_message = &msg,
6108 .callback_ops = &nfs4_layoutcommit_ops,
6109 .callback_data = data,
6110 .flags = RPC_TASK_ASYNC,
6111 };
6112 struct rpc_task *task;
6113 int status = 0;
6114
6115 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6116 "lbw: %llu inode %lu\n",
6117 data->task.tk_pid, sync,
6118 data->args.lastbytewritten,
6119 data->args.inode->i_ino);
6120
Trond Myklebust9d12b212012-01-17 22:04:25 -05006121 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006122 task = rpc_run_task(&task_setup_data);
6123 if (IS_ERR(task))
6124 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006125 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006126 goto out;
6127 status = nfs4_wait_for_completion_rpc_task(task);
6128 if (status != 0)
6129 goto out;
6130 status = task->tk_status;
6131out:
6132 dprintk("%s: status %d\n", __func__, status);
6133 rpc_put_task(task);
6134 return status;
6135}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006136
6137static int
6138_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6139 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6140{
6141 struct nfs41_secinfo_no_name_args args = {
6142 .style = SECINFO_STYLE_CURRENT_FH,
6143 };
6144 struct nfs4_secinfo_res res = {
6145 .flavors = flavors,
6146 };
6147 struct rpc_message msg = {
6148 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6149 .rpc_argp = &args,
6150 .rpc_resp = &res,
6151 };
6152 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6153}
6154
6155static int
6156nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6157 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6158{
6159 struct nfs4_exception exception = { };
6160 int err;
6161 do {
6162 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6163 switch (err) {
6164 case 0:
6165 case -NFS4ERR_WRONGSEC:
6166 case -NFS4ERR_NOTSUPP:
6167 break;
6168 default:
6169 err = nfs4_handle_exception(server, err, &exception);
6170 }
6171 } while (exception.retry);
6172 return err;
6173}
6174
6175static int
6176nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6177 struct nfs_fsinfo *info)
6178{
6179 int err;
6180 struct page *page;
6181 rpc_authflavor_t flavor;
6182 struct nfs4_secinfo_flavors *flavors;
6183
6184 page = alloc_page(GFP_KERNEL);
6185 if (!page) {
6186 err = -ENOMEM;
6187 goto out;
6188 }
6189
6190 flavors = page_address(page);
6191 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6192
6193 /*
6194 * Fall back on "guess and check" method if
6195 * the server doesn't support SECINFO_NO_NAME
6196 */
6197 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6198 err = nfs4_find_root_sec(server, fhandle, info);
6199 goto out_freepage;
6200 }
6201 if (err)
6202 goto out_freepage;
6203
6204 flavor = nfs_find_best_sec(flavors);
6205 if (err == 0)
6206 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6207
6208out_freepage:
6209 put_page(page);
6210 if (err == -EACCES)
6211 return -EPERM;
6212out:
6213 return err;
6214}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006215
6216static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006217{
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006218 int status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006219 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006220 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006221 };
6222 struct nfs41_test_stateid_res res;
6223 struct rpc_message msg = {
6224 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6225 .rpc_argp = &args,
6226 .rpc_resp = &res,
6227 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006228
Trond Myklebust9d12b212012-01-17 22:04:25 -05006229 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006230 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6231
6232 if (status == NFS_OK)
6233 return res.status;
6234 return status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006235}
6236
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006237static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006238{
6239 struct nfs4_exception exception = { };
6240 int err;
6241 do {
6242 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006243 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006244 &exception);
6245 } while (exception.retry);
6246 return err;
6247}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006248
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006249static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006250{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006251 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006252 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006253 };
6254 struct nfs41_free_stateid_res res;
6255 struct rpc_message msg = {
6256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6257 .rpc_argp = &args,
6258 .rpc_resp = &res,
6259 };
6260
Trond Myklebust9d12b212012-01-17 22:04:25 -05006261 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6262 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006263}
6264
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006265static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006266{
6267 struct nfs4_exception exception = { };
6268 int err;
6269 do {
6270 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006271 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006272 &exception);
6273 } while (exception.retry);
6274 return err;
6275}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006276
6277static bool nfs41_match_stateid(const nfs4_stateid *s1,
6278 const nfs4_stateid *s2)
6279{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006280 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006281 return false;
6282
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006283 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006284 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006285 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006286 return true;
6287
6288 return false;
6289}
6290
Andy Adamson557134a2009-04-01 09:21:53 -04006291#endif /* CONFIG_NFS_V4_1 */
6292
Trond Myklebust36281ca2012-03-04 18:13:56 -05006293static bool nfs4_match_stateid(const nfs4_stateid *s1,
6294 const nfs4_stateid *s2)
6295{
Trond Myklebustf597c532012-03-04 18:13:56 -05006296 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006297}
6298
6299
Andy Adamson591d71c2009-04-01 09:22:47 -04006300struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006301 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006302 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303 .recover_open = nfs4_open_reclaim,
6304 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006305 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006306 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307};
6308
Andy Adamson591d71c2009-04-01 09:22:47 -04006309#if defined(CONFIG_NFS_V4_1)
6310struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6311 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6312 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6313 .recover_open = nfs4_open_reclaim,
6314 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006315 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006316 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006317 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006318};
6319#endif /* CONFIG_NFS_V4_1 */
6320
6321struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006322 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006323 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 .recover_open = nfs4_open_expired,
6325 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006326 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006327 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328};
6329
Andy Adamson591d71c2009-04-01 09:22:47 -04006330#if defined(CONFIG_NFS_V4_1)
6331struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6332 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6333 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006334 .recover_open = nfs41_open_expired,
6335 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006336 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006337 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006338};
6339#endif /* CONFIG_NFS_V4_1 */
6340
Benny Halevy29fba382009-04-01 09:22:44 -04006341struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6342 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006343 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006344 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006345};
6346
6347#if defined(CONFIG_NFS_V4_1)
6348struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6349 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006350 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006351 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006352};
6353#endif
6354
Trond Myklebust97dc1352010-06-16 09:52:26 -04006355static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6356 .minor_version = 0,
6357 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006358 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006359 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006360 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6361 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6362 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006363};
6364
6365#if defined(CONFIG_NFS_V4_1)
6366static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6367 .minor_version = 1,
6368 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006369 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006370 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006371 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6372 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6373 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006374};
6375#endif
6376
Trond Myklebust97dc1352010-06-16 09:52:26 -04006377const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6378 [0] = &nfs_v4_0_minor_ops,
6379#if defined(CONFIG_NFS_V4_1)
6380 [1] = &nfs_v4_1_minor_ops,
6381#endif
6382};
6383
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006384static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006385 .permission = nfs_permission,
6386 .getattr = nfs_getattr,
6387 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006388 .getxattr = generic_getxattr,
6389 .setxattr = generic_setxattr,
6390 .listxattr = generic_listxattr,
6391 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006392};
6393
David Howells509de812006-08-22 20:06:11 -04006394const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395 .version = 4, /* protocol version */
6396 .dentry_ops = &nfs4_dentry_operations,
6397 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006398 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006399 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 .getroot = nfs4_proc_get_root,
6401 .getattr = nfs4_proc_getattr,
6402 .setattr = nfs4_proc_setattr,
6403 .lookup = nfs4_proc_lookup,
6404 .access = nfs4_proc_access,
6405 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 .create = nfs4_proc_create,
6407 .remove = nfs4_proc_remove,
6408 .unlink_setup = nfs4_proc_unlink_setup,
6409 .unlink_done = nfs4_proc_unlink_done,
6410 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006411 .rename_setup = nfs4_proc_rename_setup,
6412 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413 .link = nfs4_proc_link,
6414 .symlink = nfs4_proc_symlink,
6415 .mkdir = nfs4_proc_mkdir,
6416 .rmdir = nfs4_proc_remove,
6417 .readdir = nfs4_proc_readdir,
6418 .mknod = nfs4_proc_mknod,
6419 .statfs = nfs4_proc_statfs,
6420 .fsinfo = nfs4_proc_fsinfo,
6421 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006422 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 .decode_dirent = nfs4_decode_dirent,
6424 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006425 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006427 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006429 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006431 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006432 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006433 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006434 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006435 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436};
6437
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006438static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6439 .prefix = XATTR_NAME_NFSV4_ACL,
6440 .list = nfs4_xattr_list_nfs4_acl,
6441 .get = nfs4_xattr_get_nfs4_acl,
6442 .set = nfs4_xattr_set_nfs4_acl,
6443};
6444
6445const struct xattr_handler *nfs4_xattr_handlers[] = {
6446 &nfs4_xattr_nfs4_acl_handler,
6447 NULL
6448};
6449
Trond Myklebustef159e92012-02-06 19:50:40 -05006450module_param(max_session_slots, ushort, 0644);
6451MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6452 "requests the client will negotiate");
6453
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454/*
6455 * Local variables:
6456 * c-basic-offset: 8
6457 * End:
6458 */