blob: f31fcea1af7ee5fd26f9c0e71d5ec27cef1bcff0 [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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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) {
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400839 u32 *s;
840
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100841 p->o_arg.u.attrs = &p->attrs;
842 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400843 s = (u32 *) p->o_arg.u.verifier.data;
844 s[0] = jiffies;
845 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100846 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100847 p->c_arg.fh = &p->o_res.fh;
848 p->c_arg.stateid = &p->o_res.stateid;
849 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400850 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400851 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100852 return p;
853err_free:
854 kfree(p);
855err:
856 dput(parent);
857 return NULL;
858}
859
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400860static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100861{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400862 struct nfs4_opendata *p = container_of(kref,
863 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400864 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400865
866 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400867 if (p->state != NULL)
868 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400869 nfs4_put_state_owner(p->owner);
870 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400871 dput(p->dentry);
872 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500873 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400874 kfree(p);
875}
876
877static void nfs4_opendata_put(struct nfs4_opendata *p)
878{
879 if (p != NULL)
880 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100881}
882
Trond Myklebust06f814a2006-01-03 09:55:07 +0100883static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
884{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100885 int ret;
886
Trond Myklebust06f814a2006-01-03 09:55:07 +0100887 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100888 return ret;
889}
890
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500891static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400892{
893 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500894
Trond Myklebust536e43d2012-01-17 22:04:26 -0500895 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500896 goto out;
897 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400898 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500899 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
900 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400901 break;
902 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500903 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
904 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400905 break;
906 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500907 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
908 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400909 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500910out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400911 return ret;
912}
913
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500914static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400915{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500916 if (delegation == NULL)
917 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500918 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400919 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500920 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400921 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500922 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400923 return 1;
924}
925
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500926static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100927{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100929 case FMODE_WRITE:
930 state->n_wronly++;
931 break;
932 case FMODE_READ:
933 state->n_rdonly++;
934 break;
935 case FMODE_READ|FMODE_WRITE:
936 state->n_rdwr++;
937 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500938 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100939}
940
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500941static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400942{
943 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
944 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
945 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400947 case FMODE_READ:
948 set_bit(NFS_O_RDONLY_STATE, &state->flags);
949 break;
950 case FMODE_WRITE:
951 set_bit(NFS_O_WRONLY_STATE, &state->flags);
952 break;
953 case FMODE_READ|FMODE_WRITE:
954 set_bit(NFS_O_RDWR_STATE, &state->flags);
955 }
956}
957
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500958static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400959{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400960 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400962 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400963}
964
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500965static 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 -0700966{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400967 /*
968 * Protect the call to nfs4_state_set_mode_locked and
969 * serialise the stateid update
970 */
971 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400972 if (deleg_stateid != NULL) {
973 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
974 set_bit(NFS_DELEGATED_STATE, &state->flags);
975 }
976 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400978 write_sequnlock(&state->seqlock);
979 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500980 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700981 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500984static 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 -0500985{
986 struct nfs_inode *nfsi = NFS_I(state->inode);
987 struct nfs_delegation *deleg_cur;
988 int ret = 0;
989
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500990 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500991
992 rcu_read_lock();
993 deleg_cur = rcu_dereference(nfsi->delegation);
994 if (deleg_cur == NULL)
995 goto no_delegation;
996
997 spin_lock(&deleg_cur->lock);
998 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500999 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001000 goto no_delegation_unlock;
1001
1002 if (delegation == NULL)
1003 delegation = &deleg_cur->stateid;
1004 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
1005 goto no_delegation_unlock;
1006
Trond Myklebustb7391f42008-12-23 15:21:52 -05001007 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001008 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001009 ret = 1;
1010no_delegation_unlock:
1011 spin_unlock(&deleg_cur->lock);
1012no_delegation:
1013 rcu_read_unlock();
1014
1015 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001016 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001017 ret = 1;
1018 }
1019
1020 return ret;
1021}
1022
1023
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001024static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001025{
1026 struct nfs_delegation *delegation;
1027
1028 rcu_read_lock();
1029 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001030 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001031 rcu_read_unlock();
1032 return;
1033 }
1034 rcu_read_unlock();
1035 nfs_inode_return_delegation(inode);
1036}
1037
Trond Myklebust6ee41262007-07-08 14:11:36 -04001038static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001039{
1040 struct nfs4_state *state = opendata->state;
1041 struct nfs_inode *nfsi = NFS_I(state->inode);
1042 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001043 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001044 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001045 nfs4_stateid stateid;
1046 int ret = -EAGAIN;
1047
Trond Myklebustaac00a82007-07-05 19:02:21 -04001048 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001049 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001050 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001051 if (can_open_cached(state, fmode, open_mode)) {
1052 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001053 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001054 goto out_return_state;
1055 }
1056 spin_unlock(&state->owner->so_lock);
1057 }
Trond Myklebust34310432008-12-23 15:21:38 -05001058 rcu_read_lock();
1059 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001060 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001061 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001062 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001063 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001064 /* Save the delegation */
1065 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1066 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001067 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001068 if (ret != 0)
1069 goto out;
1070 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001071
1072 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001073 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001074 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001075 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001076out:
1077 return ERR_PTR(ret);
1078out_return_state:
1079 atomic_inc(&state->count);
1080 return state;
1081}
1082
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001083static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1084{
1085 struct inode *inode;
1086 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001087 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001088 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001089
Trond Myklebustaac00a82007-07-05 19:02:21 -04001090 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001091 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001092 goto out;
1093 }
1094
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001095 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001096 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001097 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001098 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001099 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001100 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001101 goto err;
1102 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001103 state = nfs4_get_open_state(inode, data->owner);
1104 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001105 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001106 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001107 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001108 int delegation_flags = 0;
1109
Trond Myklebust003707c2007-07-05 18:07:55 -04001110 rcu_read_lock();
1111 delegation = rcu_dereference(NFS_I(inode)->delegation);
1112 if (delegation)
1113 delegation_flags = delegation->flags;
1114 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001115 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1116 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1117 "returning a delegation for "
1118 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001119 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001120 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001121 nfs_inode_set_delegation(state->inode,
1122 data->owner->so_cred,
1123 &data->o_res);
1124 else
1125 nfs_inode_reclaim_delegation(state->inode,
1126 data->owner->so_cred,
1127 &data->o_res);
1128 }
Trond Myklebust34310432008-12-23 15:21:38 -05001129
1130 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001131 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001132 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001133out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001134 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001135err_put_inode:
1136 iput(inode);
1137err:
1138 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001139}
1140
Trond Myklebust864472e2006-01-03 09:55:15 +01001141static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1142{
1143 struct nfs_inode *nfsi = NFS_I(state->inode);
1144 struct nfs_open_context *ctx;
1145
1146 spin_lock(&state->inode->i_lock);
1147 list_for_each_entry(ctx, &nfsi->open_files, list) {
1148 if (ctx->state != state)
1149 continue;
1150 get_nfs_open_context(ctx);
1151 spin_unlock(&state->inode->i_lock);
1152 return ctx;
1153 }
1154 spin_unlock(&state->inode->i_lock);
1155 return ERR_PTR(-ENOENT);
1156}
1157
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001158static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1159{
1160 struct nfs4_opendata *opendata;
1161
Al Viro3d4ff432011-06-22 18:40:12 -04001162 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001163 if (opendata == NULL)
1164 return ERR_PTR(-ENOMEM);
1165 opendata->state = state;
1166 atomic_inc(&state->count);
1167 return opendata;
1168}
1169
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001170static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001171{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001172 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001173 int ret;
1174
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001175 opendata->o_arg.open_flags = 0;
1176 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001177 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1178 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1179 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001180 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001181 if (ret != 0)
1182 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001183 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001184 if (IS_ERR(newstate))
1185 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001186 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001187 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001188 return 0;
1189}
1190
1191static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1192{
Trond Myklebust864472e2006-01-03 09:55:15 +01001193 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001194 int ret;
1195
1196 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001197 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001198 smp_rmb();
1199 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001200 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001201 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001202 if (ret != 0)
1203 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001204 if (newstate != state)
1205 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001206 }
1207 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001208 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001209 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001210 if (ret != 0)
1211 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001212 if (newstate != state)
1213 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001214 }
1215 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001216 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001217 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001218 if (ret != 0)
1219 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001220 if (newstate != state)
1221 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001222 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001223 /*
1224 * We may have performed cached opens for all three recoveries.
1225 * Check if we need to update the current stateid.
1226 */
1227 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1228 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001229 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001230 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1231 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001232 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001233 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001234 return 0;
1235}
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237/*
1238 * OPEN_RECLAIM:
1239 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001241static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001243 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001244 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001245 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 int status;
1247
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001248 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1249 if (IS_ERR(opendata))
1250 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001251 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1252 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001253 rcu_read_lock();
1254 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001255 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001256 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001257 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001258 opendata->o_arg.u.delegation_type = delegation_type;
1259 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001260 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return status;
1262}
1263
Trond Myklebust539cd032007-06-05 11:46:42 -04001264static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
1266 struct nfs_server *server = NFS_SERVER(state->inode);
1267 struct nfs4_exception exception = { };
1268 int err;
1269 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001270 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001271 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001272 break;
1273 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 } while (exception.retry);
1275 return err;
1276}
1277
Trond Myklebust864472e2006-01-03 09:55:15 +01001278static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1279{
1280 struct nfs_open_context *ctx;
1281 int ret;
1282
1283 ctx = nfs4_state_find_open_context(state);
1284 if (IS_ERR(ctx))
1285 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001286 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001287 put_nfs_open_context(ctx);
1288 return ret;
1289}
1290
Trond Myklebust13437e12007-07-06 15:10:43 -04001291static 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 -07001292{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001293 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001294 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001296 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1297 if (IS_ERR(opendata))
1298 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001299 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001300 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001301 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001302 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001303 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001304 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
Trond Myklebust13437e12007-07-06 15:10:43 -04001307int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
1309 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001310 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 int err;
1312 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001313 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 switch (err) {
1315 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001316 case -ENOENT:
1317 case -ESTALE:
1318 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001319 case -NFS4ERR_BADSESSION:
1320 case -NFS4ERR_BADSLOT:
1321 case -NFS4ERR_BAD_HIGH_SLOT:
1322 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1323 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001324 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001325 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 case -NFS4ERR_STALE_CLIENTID:
1327 case -NFS4ERR_STALE_STATEID:
1328 case -NFS4ERR_EXPIRED:
1329 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001330 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001331 goto out;
1332 case -ERESTARTSYS:
1333 /*
1334 * The show must go on: exit, but mark the
1335 * stateid as needing recovery.
1336 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001337 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001338 case -NFS4ERR_ADMIN_REVOKED:
1339 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001340 nfs_inode_find_state_and_recover(state->inode,
1341 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001342 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001343 case -EKEYEXPIRED:
1344 /*
1345 * User RPCSEC_GSS context has expired.
1346 * We cannot recover this stateid now, so
1347 * skip it and allow recovery thread to
1348 * proceed.
1349 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001350 case -ENOMEM:
1351 err = 0;
1352 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
1354 err = nfs4_handle_exception(server, err, &exception);
1355 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001356out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return err;
1358}
1359
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001360static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1361{
1362 struct nfs4_opendata *data = calldata;
1363
1364 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001365 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001366 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1367 sizeof(data->o_res.stateid.data));
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 Myklebust035168a2010-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 Myklebust77041ed2010-07-01 12:49:11 -04001925 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001926 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1928
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 Myklebust035168a2010-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 Myklebust62ab460c2009-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 Myklebust62ab460c2009-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 Adamsoncbdabc7f2011-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 Adamsoncbdabc7f2011-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 Adamsoncbdabc7f2011-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 Adamsoncbdabc7f2011-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 Adamsoncbdabc7f2011-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 Myklebust16b4289c2006-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 Myklebust16b4289c2006-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 Myklebust16b4289c2006-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 Myklebust16b4289c2006-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
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003824int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3825 unsigned short port, struct rpc_cred *cred,
3826 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827{
3828 nfs4_verifier sc_verifier;
3829 struct nfs4_setclientid setclientid = {
3830 .sc_verifier = &sc_verifier,
3831 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003832 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 };
3834 struct rpc_message msg = {
3835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3836 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003837 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003838 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 };
Al Virobc4785c2006-10-19 23:28:51 -07003840 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 int loop = 0;
3842 int status;
3843
Al Virobc4785c2006-10-19 23:28:51 -07003844 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3846 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3847
3848 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05003849 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003851 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003852 clp->cl_ipaddr,
3853 rpc_peeraddr2str(clp->cl_rpcclient,
3854 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003855 rpc_peeraddr2str(clp->cl_rpcclient,
3856 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003857 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 clp->cl_id_uniquifier);
3859 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003860 sizeof(setclientid.sc_netid),
3861 rpc_peeraddr2str(clp->cl_rpcclient,
3862 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003864 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05003866 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003868 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 if (status != -NFS4ERR_CLID_INUSE)
3870 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003871 if (loop != 0) {
3872 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003874 }
3875 ++loop;
3876 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 }
3878 return status;
3879}
3880
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003881int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003882 struct nfs4_setclientid_res *arg,
3883 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884{
3885 struct nfs_fsinfo fsinfo;
3886 struct rpc_message msg = {
3887 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003888 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003890 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 };
3892 unsigned long now;
3893 int status;
3894
3895 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003896 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 if (status == 0) {
3898 spin_lock(&clp->cl_lock);
3899 clp->cl_lease_time = fsinfo.lease_time * HZ;
3900 clp->cl_last_renewal = now;
3901 spin_unlock(&clp->cl_lock);
3902 }
3903 return status;
3904}
3905
Trond Myklebustfe650402006-01-03 09:55:18 +01003906struct nfs4_delegreturndata {
3907 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003908 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003909 struct nfs_fh fh;
3910 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003911 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003912 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003913 int rpc_status;
3914};
3915
Trond Myklebustfe650402006-01-03 09:55:18 +01003916static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3917{
3918 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003919
Trond Myklebust14516c32010-07-31 14:29:06 -04003920 if (!nfs4_sequence_done(task, &data->res.seq_res))
3921 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003922
Ricardo Labiaga79708862009-12-07 09:23:21 -05003923 switch (task->tk_status) {
3924 case -NFS4ERR_STALE_STATEID:
3925 case -NFS4ERR_EXPIRED:
3926 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003927 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003928 break;
3929 default:
3930 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3931 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003932 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003933 return;
3934 }
3935 }
3936 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003937}
3938
3939static void nfs4_delegreturn_release(void *calldata)
3940{
Trond Myklebustfe650402006-01-03 09:55:18 +01003941 kfree(calldata);
3942}
3943
Andy Adamson938e1012009-04-01 09:22:28 -04003944#if defined(CONFIG_NFS_V4_1)
3945static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3946{
3947 struct nfs4_delegreturndata *d_data;
3948
3949 d_data = (struct nfs4_delegreturndata *)data;
3950
Trond Myklebust035168a2010-06-16 09:52:26 -04003951 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003952 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003953 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04003954 return;
3955 rpc_call_start(task);
3956}
3957#endif /* CONFIG_NFS_V4_1 */
3958
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003959static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003960#if defined(CONFIG_NFS_V4_1)
3961 .rpc_call_prepare = nfs4_delegreturn_prepare,
3962#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003963 .rpc_call_done = nfs4_delegreturn_done,
3964 .rpc_release = nfs4_delegreturn_release,
3965};
3966
Trond Myklebuste6f81072008-01-24 18:14:34 -05003967static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003968{
3969 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003970 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003971 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003972 struct rpc_message msg = {
3973 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3974 .rpc_cred = cred,
3975 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003976 struct rpc_task_setup task_setup_data = {
3977 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003978 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003979 .callback_ops = &nfs4_delegreturn_ops,
3980 .flags = RPC_TASK_ASYNC,
3981 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003982 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003983
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003984 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003985 if (data == NULL)
3986 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003987 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01003988 data->args.fhandle = &data->fh;
3989 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003990 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003991 nfs_copy_fh(&data->fh, NFS_FH(inode));
3992 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003993 data->res.fattr = &data->fattr;
3994 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003995 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003996 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003997 data->rpc_status = 0;
3998
Trond Myklebustc970aa82007-07-14 15:39:59 -04003999 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004000 msg.rpc_argp = &data->args;
4001 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004002 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004003 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004004 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004005 if (!issync)
4006 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004007 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004008 if (status != 0)
4009 goto out;
4010 status = data->rpc_status;
4011 if (status != 0)
4012 goto out;
4013 nfs_refresh_inode(inode, &data->fattr);
4014out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004015 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004016 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017}
4018
Trond Myklebuste6f81072008-01-24 18:14:34 -05004019int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020{
4021 struct nfs_server *server = NFS_SERVER(inode);
4022 struct nfs4_exception exception = { };
4023 int err;
4024 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004025 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 switch (err) {
4027 case -NFS4ERR_STALE_STATEID:
4028 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 case 0:
4030 return 0;
4031 }
4032 err = nfs4_handle_exception(server, err, &exception);
4033 } while (exception.retry);
4034 return err;
4035}
4036
4037#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4038#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4039
4040/*
4041 * sleep, with exponential backoff, and retry the LOCK operation.
4042 */
4043static unsigned long
4044nfs4_set_lock_task_retry(unsigned long timeout)
4045{
Jeff Laytond3103102011-12-01 22:44:39 +01004046 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 timeout <<= 1;
4048 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4049 return NFS4_LOCK_MAXTIMEOUT;
4050 return timeout;
4051}
4052
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4054{
4055 struct inode *inode = state->inode;
4056 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004057 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004058 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004060 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004062 struct nfs_lockt_res res = {
4063 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 };
4065 struct rpc_message msg = {
4066 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4067 .rpc_argp = &arg,
4068 .rpc_resp = &res,
4069 .rpc_cred = state->owner->so_cred,
4070 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 struct nfs4_lock_state *lsp;
4072 int status;
4073
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004074 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004075 status = nfs4_set_lock_state(state, request);
4076 if (status != 0)
4077 goto out;
4078 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004079 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004080 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004081 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004082 switch (status) {
4083 case 0:
4084 request->fl_type = F_UNLCK;
4085 break;
4086 case -NFS4ERR_DENIED:
4087 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004089 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004090out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 return status;
4092}
4093
4094static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4095{
4096 struct nfs4_exception exception = { };
4097 int err;
4098
4099 do {
4100 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4101 _nfs4_proc_getlk(state, cmd, request),
4102 &exception);
4103 } while (exception.retry);
4104 return err;
4105}
4106
4107static int do_vfs_lock(struct file *file, struct file_lock *fl)
4108{
4109 int res = 0;
4110 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4111 case FL_POSIX:
4112 res = posix_lock_file_wait(file, fl);
4113 break;
4114 case FL_FLOCK:
4115 res = flock_lock_file_wait(file, fl);
4116 break;
4117 default:
4118 BUG();
4119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 return res;
4121}
4122
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004123struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004124 struct nfs_locku_args arg;
4125 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004126 struct nfs4_lock_state *lsp;
4127 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004128 struct file_lock fl;
4129 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004130 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004131};
4132
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004133static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4134 struct nfs_open_context *ctx,
4135 struct nfs4_lock_state *lsp,
4136 struct nfs_seqid *seqid)
4137{
4138 struct nfs4_unlockdata *p;
4139 struct inode *inode = lsp->ls_state->inode;
4140
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004141 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004142 if (p == NULL)
4143 return NULL;
4144 p->arg.fh = NFS_FH(inode);
4145 p->arg.fl = &p->fl;
4146 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004147 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004148 p->arg.stateid = &lsp->ls_stateid;
4149 p->lsp = lsp;
4150 atomic_inc(&lsp->ls_count);
4151 /* Ensure we don't close file until we're done freeing locks! */
4152 p->ctx = get_nfs_open_context(ctx);
4153 memcpy(&p->fl, fl, sizeof(p->fl));
4154 p->server = NFS_SERVER(inode);
4155 return p;
4156}
4157
Trond Myklebust06f814a2006-01-03 09:55:07 +01004158static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004159{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004160 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004161 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004162 nfs4_put_lock_state(calldata->lsp);
4163 put_nfs_open_context(calldata->ctx);
4164 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004165}
4166
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004167static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004168{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004169 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004170
Trond Myklebust14516c32010-07-31 14:29:06 -04004171 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4172 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004173 switch (task->tk_status) {
4174 case 0:
4175 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004176 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004177 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004178 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004179 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004180 case -NFS4ERR_BAD_STATEID:
4181 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004182 case -NFS4ERR_STALE_STATEID:
4183 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004184 break;
4185 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004186 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004187 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004188 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004189}
4190
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004191static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004192{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004193 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004195 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004196 return;
4197 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004198 /* Note: exit _without_ running nfs4_locku_done */
4199 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004200 return;
4201 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004202 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004203 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004204 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004205 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004206 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004207 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208}
4209
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004210static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004211 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004212 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004213 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004214};
4215
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004216static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4217 struct nfs_open_context *ctx,
4218 struct nfs4_lock_state *lsp,
4219 struct nfs_seqid *seqid)
4220{
4221 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004222 struct rpc_message msg = {
4223 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4224 .rpc_cred = ctx->cred,
4225 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004226 struct rpc_task_setup task_setup_data = {
4227 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004228 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004229 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004230 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004231 .flags = RPC_TASK_ASYNC,
4232 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004233
Frank Filz137d6ac2007-07-09 15:32:29 -07004234 /* Ensure this is an unlock - when canceling a lock, the
4235 * canceled lock is passed in, and it won't be an unlock.
4236 */
4237 fl->fl_type = F_UNLCK;
4238
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004239 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4240 if (data == NULL) {
4241 nfs_free_seqid(seqid);
4242 return ERR_PTR(-ENOMEM);
4243 }
4244
Trond Myklebust9d12b212012-01-17 22:04:25 -05004245 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004246 msg.rpc_argp = &data->arg;
4247 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004248 task_setup_data.callback_data = data;
4249 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004250}
4251
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4253{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004254 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004255 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004256 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004257 struct rpc_task *task;
4258 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004259 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
Trond Myklebust9b073572006-06-29 16:38:34 -04004261 status = nfs4_set_lock_state(state, request);
4262 /* Unlock _before_ we do the RPC call */
4263 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004264 down_read(&nfsi->rwsem);
4265 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4266 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004267 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004268 }
4269 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004270 if (status != 0)
4271 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004272 /* Is this a delegated lock? */
4273 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004274 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004275 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004276 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004277 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004278 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004279 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004280 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004281 status = PTR_ERR(task);
4282 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004283 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004284 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004285 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004286out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004287 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004288 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289}
4290
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004291struct nfs4_lockdata {
4292 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004293 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004294 struct nfs4_lock_state *lsp;
4295 struct nfs_open_context *ctx;
4296 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004297 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004298 int rpc_status;
4299 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004300 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004301};
4302
4303static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004304 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4305 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004306{
4307 struct nfs4_lockdata *p;
4308 struct inode *inode = lsp->ls_state->inode;
4309 struct nfs_server *server = NFS_SERVER(inode);
4310
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004311 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004312 if (p == NULL)
4313 return NULL;
4314
4315 p->arg.fh = NFS_FH(inode);
4316 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004317 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004318 if (p->arg.open_seqid == NULL)
4319 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004320 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004321 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004322 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004323 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004324 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004325 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004326 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004327 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004328 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004329 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004330 atomic_inc(&lsp->ls_count);
4331 p->ctx = get_nfs_open_context(ctx);
4332 memcpy(&p->fl, fl, sizeof(p->fl));
4333 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004334out_free_seqid:
4335 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004336out_free:
4337 kfree(p);
4338 return NULL;
4339}
4340
4341static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4342{
4343 struct nfs4_lockdata *data = calldata;
4344 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
Harvey Harrison3110ff82008-05-02 13:42:44 -07004346 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004347 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4348 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004349 /* Do we need to do an open_to_lock_owner? */
4350 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004351 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4352 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004353 data->arg.open_stateid = &state->stateid;
4354 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004355 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004356 } else
4357 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004358 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004359 if (nfs4_setup_sequence(data->server,
4360 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004361 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004362 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004363 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004364 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004365}
4366
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004367static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4368{
4369 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4370 nfs4_lock_prepare(task, calldata);
4371}
4372
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004373static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4374{
4375 struct nfs4_lockdata *data = calldata;
4376
Harvey Harrison3110ff82008-05-02 13:42:44 -07004377 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004378
Trond Myklebust14516c32010-07-31 14:29:06 -04004379 if (!nfs4_sequence_done(task, &data->res.seq_res))
4380 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004381
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004382 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004383 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004384 if (data->rpc_status == 0)
4385 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4386 else
4387 goto out;
4388 }
4389 if (data->rpc_status == 0) {
4390 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4391 sizeof(data->lsp->ls_stateid.data));
4392 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004393 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004394 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004395out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004396 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004397}
4398
4399static void nfs4_lock_release(void *calldata)
4400{
4401 struct nfs4_lockdata *data = calldata;
4402
Harvey Harrison3110ff82008-05-02 13:42:44 -07004403 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004404 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004405 if (data->cancelled != 0) {
4406 struct rpc_task *task;
4407 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4408 data->arg.lock_seqid);
4409 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004410 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004411 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004412 } else
4413 nfs_free_seqid(data->arg.lock_seqid);
4414 nfs4_put_lock_state(data->lsp);
4415 put_nfs_open_context(data->ctx);
4416 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004417 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004418}
4419
4420static const struct rpc_call_ops nfs4_lock_ops = {
4421 .rpc_call_prepare = nfs4_lock_prepare,
4422 .rpc_call_done = nfs4_lock_done,
4423 .rpc_release = nfs4_lock_release,
4424};
4425
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004426static const struct rpc_call_ops nfs4_recover_lock_ops = {
4427 .rpc_call_prepare = nfs4_recover_lock_prepare,
4428 .rpc_call_done = nfs4_lock_done,
4429 .rpc_release = nfs4_lock_release,
4430};
4431
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004432static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4433{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004434 switch (error) {
4435 case -NFS4ERR_ADMIN_REVOKED:
4436 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004437 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004438 if (new_lock_owner != 0 ||
4439 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004440 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004441 break;
4442 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004443 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004444 case -NFS4ERR_EXPIRED:
4445 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004446 };
4447}
4448
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004449static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004450{
4451 struct nfs4_lockdata *data;
4452 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004453 struct rpc_message msg = {
4454 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4455 .rpc_cred = state->owner->so_cred,
4456 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004457 struct rpc_task_setup task_setup_data = {
4458 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004459 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004460 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004461 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004462 .flags = RPC_TASK_ASYNC,
4463 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004464 int ret;
4465
Harvey Harrison3110ff82008-05-02 13:42:44 -07004466 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004467 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004468 fl->fl_u.nfs4_fl.owner,
4469 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004470 if (data == NULL)
4471 return -ENOMEM;
4472 if (IS_SETLKW(cmd))
4473 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004474 if (recovery_type > NFS_LOCK_NEW) {
4475 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004476 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004477 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4478 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004479 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004480 msg.rpc_argp = &data->arg;
4481 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004482 task_setup_data.callback_data = data;
4483 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004484 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004485 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004486 ret = nfs4_wait_for_completion_rpc_task(task);
4487 if (ret == 0) {
4488 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004489 if (ret)
4490 nfs4_handle_setlk_error(data->server, data->lsp,
4491 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004492 } else
4493 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004494 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004495 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004496 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497}
4498
4499static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4500{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004501 struct nfs_server *server = NFS_SERVER(state->inode);
4502 struct nfs4_exception exception = { };
4503 int err;
4504
4505 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004506 /* Cache the lock if possible... */
4507 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4508 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004509 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004510 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004511 break;
4512 nfs4_handle_exception(server, err, &exception);
4513 } while (exception.retry);
4514 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515}
4516
4517static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4518{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004519 struct nfs_server *server = NFS_SERVER(state->inode);
4520 struct nfs4_exception exception = { };
4521 int err;
4522
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004523 err = nfs4_set_lock_state(state, request);
4524 if (err != 0)
4525 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004526 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004527 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4528 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004529 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004530 switch (err) {
4531 default:
4532 goto out;
4533 case -NFS4ERR_GRACE:
4534 case -NFS4ERR_DELAY:
4535 nfs4_handle_exception(server, err, &exception);
4536 err = 0;
4537 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004538 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004539out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004540 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541}
4542
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004543#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004544static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004545{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004546 int status, ret = NFS_OK;
4547 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004548 struct nfs_server *server = NFS_SERVER(state->inode);
4549
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004550 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4551 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4552 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4553 if (status != NFS_OK) {
4554 nfs41_free_stateid(server, &lsp->ls_stateid);
4555 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4556 ret = status;
4557 }
4558 }
4559 };
4560
4561 return ret;
4562}
4563
4564static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4565{
4566 int status = NFS_OK;
4567
4568 if (test_bit(LK_STATE_IN_USE, &state->flags))
4569 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004570 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004571 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004572 return nfs4_lock_expired(state, request);
4573}
4574#endif
4575
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4577{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004578 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004579 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004580 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004582 if ((fl_flags & FL_POSIX) &&
4583 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4584 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004585 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004586 status = nfs4_set_lock_state(state, request);
4587 if (status != 0)
4588 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004589 request->fl_flags |= FL_ACCESS;
4590 status = do_vfs_lock(request->fl_file, request);
4591 if (status < 0)
4592 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004593 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004594 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004595 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004596 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004597 request->fl_flags = fl_flags & ~FL_SLEEP;
4598 status = do_vfs_lock(request->fl_file, request);
4599 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004600 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004601 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004602 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004603 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004604 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004605 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004606 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004607 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4608 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004609out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004610 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004611out:
4612 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 return status;
4614}
4615
4616static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4617{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004618 struct nfs4_exception exception = {
4619 .state = state,
4620 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 int err;
4622
4623 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004624 err = _nfs4_proc_setlk(state, cmd, request);
4625 if (err == -NFS4ERR_DENIED)
4626 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004628 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 } while (exception.retry);
4630 return err;
4631}
4632
4633static int
4634nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4635{
4636 struct nfs_open_context *ctx;
4637 struct nfs4_state *state;
4638 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4639 int status;
4640
4641 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004642 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 state = ctx->state;
4644
4645 if (request->fl_start < 0 || request->fl_end < 0)
4646 return -EINVAL;
4647
Trond Myklebustd9531262009-07-21 19:22:38 -04004648 if (IS_GETLK(cmd)) {
4649 if (state != NULL)
4650 return nfs4_proc_getlk(state, F_GETLK, request);
4651 return 0;
4652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
4654 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4655 return -EINVAL;
4656
Trond Myklebustd9531262009-07-21 19:22:38 -04004657 if (request->fl_type == F_UNLCK) {
4658 if (state != NULL)
4659 return nfs4_proc_unlck(state, cmd, request);
4660 return 0;
4661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
Trond Myklebustd9531262009-07-21 19:22:38 -04004663 if (state == NULL)
4664 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 do {
4666 status = nfs4_proc_setlk(state, cmd, request);
4667 if ((status != -EAGAIN) || IS_SETLK(cmd))
4668 break;
4669 timeout = nfs4_set_lock_task_retry(timeout);
4670 status = -ERESTARTSYS;
4671 if (signalled())
4672 break;
4673 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 return status;
4675}
4676
Trond Myklebust888e6942005-11-04 15:38:11 -05004677int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4678{
4679 struct nfs_server *server = NFS_SERVER(state->inode);
4680 struct nfs4_exception exception = { };
4681 int err;
4682
4683 err = nfs4_set_lock_state(state, fl);
4684 if (err != 0)
4685 goto out;
4686 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004687 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004688 switch (err) {
4689 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004690 printk(KERN_ERR "NFS: %s: unhandled error "
4691 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004692 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004693 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004694 goto out;
4695 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004696 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004697 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004698 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004699 nfs4_schedule_lease_recovery(server->nfs_client);
4700 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004701 case -NFS4ERR_BADSESSION:
4702 case -NFS4ERR_BADSLOT:
4703 case -NFS4ERR_BAD_HIGH_SLOT:
4704 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4705 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004706 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004707 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004708 case -ERESTARTSYS:
4709 /*
4710 * The show must go on: exit, but mark the
4711 * stateid as needing recovery.
4712 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004713 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004714 case -NFS4ERR_ADMIN_REVOKED:
4715 case -NFS4ERR_BAD_STATEID:
4716 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004717 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004718 err = 0;
4719 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004720 case -EKEYEXPIRED:
4721 /*
4722 * User RPCSEC_GSS context has expired.
4723 * We cannot recover this stateid now, so
4724 * skip it and allow recovery thread to
4725 * proceed.
4726 */
4727 err = 0;
4728 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004729 case -ENOMEM:
4730 case -NFS4ERR_DENIED:
4731 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4732 err = 0;
4733 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004734 case -NFS4ERR_DELAY:
4735 break;
4736 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004737 err = nfs4_handle_exception(server, err, &exception);
4738 } while (exception.retry);
4739out:
4740 return err;
4741}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004742
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004743static void nfs4_release_lockowner_release(void *calldata)
4744{
4745 kfree(calldata);
4746}
4747
4748const struct rpc_call_ops nfs4_release_lockowner_ops = {
4749 .rpc_release = nfs4_release_lockowner_release,
4750};
4751
4752void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4753{
4754 struct nfs_server *server = lsp->ls_state->owner->so_server;
4755 struct nfs_release_lockowner_args *args;
4756 struct rpc_message msg = {
4757 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4758 };
4759
4760 if (server->nfs_client->cl_mvops->minor_version != 0)
4761 return;
4762 args = kmalloc(sizeof(*args), GFP_NOFS);
4763 if (!args)
4764 return;
4765 args->lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004766 args->lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004767 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004768 msg.rpc_argp = args;
4769 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4770}
4771
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004772#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4773
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004774static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4775 const void *buf, size_t buflen,
4776 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004777{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004778 if (strcmp(key, "") != 0)
4779 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004780
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004781 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004782}
4783
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004784static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4785 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004786{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004787 if (strcmp(key, "") != 0)
4788 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004789
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004790 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004791}
4792
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004793static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4794 size_t list_len, const char *name,
4795 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004796{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004797 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004798
J. Bruce Fields096455a2006-03-20 23:23:42 -05004799 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4800 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004801
4802 if (list && len <= list_len)
4803 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004804 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004805}
4806
Andy Adamson533eb462011-06-13 18:25:56 -04004807/*
4808 * nfs_fhget will use either the mounted_on_fileid or the fileid
4809 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004810static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4811{
Andy Adamson533eb462011-06-13 18:25:56 -04004812 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4813 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4814 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05004815 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004816 return;
4817
4818 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05004819 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004820 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4821 fattr->nlink = 2;
4822}
4823
Trond Myklebust56659e92007-07-17 21:52:39 -04004824int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004825 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004826{
4827 struct nfs_server *server = NFS_SERVER(dir);
4828 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004829 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004830 };
4831 struct nfs4_fs_locations_arg args = {
4832 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004833 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004834 .page = page,
4835 .bitmask = bitmask,
4836 };
Benny Halevy22958462009-04-01 09:22:02 -04004837 struct nfs4_fs_locations_res res = {
4838 .fs_locations = fs_locations,
4839 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004840 struct rpc_message msg = {
4841 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4842 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004843 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004844 };
4845 int status;
4846
Harvey Harrison3110ff82008-05-02 13:42:44 -07004847 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004848
4849 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4850 * is not supported */
4851 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4852 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4853 else
4854 bitmask[0] |= FATTR4_WORD0_FILEID;
4855
Trond Myklebustc228fd32007-01-13 02:28:11 -05004856 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004857 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004858 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004859 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004860 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004861 return status;
4862}
4863
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004864static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4865{
4866 int status;
4867 struct nfs4_secinfo_arg args = {
4868 .dir_fh = NFS_FH(dir),
4869 .name = name,
4870 };
4871 struct nfs4_secinfo_res res = {
4872 .flavors = flavors,
4873 };
4874 struct rpc_message msg = {
4875 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4876 .rpc_argp = &args,
4877 .rpc_resp = &res,
4878 };
4879
4880 dprintk("NFS call secinfo %s\n", name->name);
4881 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4882 dprintk("NFS reply secinfo: %d\n", status);
4883 return status;
4884}
4885
4886int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4887{
4888 struct nfs4_exception exception = { };
4889 int err;
4890 do {
4891 err = nfs4_handle_exception(NFS_SERVER(dir),
4892 _nfs4_proc_secinfo(dir, name, flavors),
4893 &exception);
4894 } while (exception.retry);
4895 return err;
4896}
4897
Andy Adamson557134a2009-04-01 09:21:53 -04004898#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004899/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004900 * Check the exchange flags returned by the server for invalid flags, having
4901 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4902 * DS flags set.
4903 */
4904static int nfs4_check_cl_exchange_flags(u32 flags)
4905{
4906 if (flags & ~EXCHGID4_FLAG_MASK_R)
4907 goto out_inval;
4908 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4909 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4910 goto out_inval;
4911 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4912 goto out_inval;
4913 return NFS_OK;
4914out_inval:
4915 return -NFS4ERR_INVAL;
4916}
4917
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004918static bool
4919nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4920{
4921 if (a->server_scope_sz == b->server_scope_sz &&
4922 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4923 return true;
4924
4925 return false;
4926}
4927
Andy Adamson357f54d2010-12-14 10:11:57 -05004928/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004929 * nfs4_proc_exchange_id()
4930 *
4931 * Since the clientid has expired, all compounds using sessions
4932 * associated with the stale clientid will be returning
4933 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4934 * be in some phase of session reset.
4935 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004936int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004937{
4938 nfs4_verifier verifier;
4939 struct nfs41_exchange_id_args args = {
4940 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004941 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004942 };
4943 struct nfs41_exchange_id_res res = {
4944 .client = clp,
4945 };
4946 int status;
4947 struct rpc_message msg = {
4948 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4949 .rpc_argp = &args,
4950 .rpc_resp = &res,
4951 .rpc_cred = cred,
4952 };
4953 __be32 *p;
4954
4955 dprintk("--> %s\n", __func__);
4956 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004957
Benny Halevy99fe60d2009-04-01 09:22:29 -04004958 p = (u32 *)verifier.data;
4959 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4960 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4961 args.verifier = &verifier;
4962
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004963 args.id_len = scnprintf(args.id, sizeof(args.id),
4964 "%s/%s.%s/%u",
4965 clp->cl_ipaddr,
4966 init_utsname()->nodename,
4967 init_utsname()->domainname,
4968 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004969
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004970 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05004971 if (unlikely(!res.server_scope)) {
4972 status = -ENOMEM;
4973 goto out;
4974 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004975
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05004976 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
4977 if (unlikely(!res.impl_id)) {
4978 status = -ENOMEM;
4979 goto out_server_scope;
4980 }
4981
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004982 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004983 if (!status)
4984 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004985
4986 if (!status) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05004987 /* use the most recent implementation id */
4988 kfree(clp->impl_id);
4989 clp->impl_id = res.impl_id;
4990 } else
4991 kfree(res.impl_id);
4992
4993 if (!status) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004994 if (clp->server_scope &&
4995 !nfs41_same_server_scope(clp->server_scope,
4996 res.server_scope)) {
4997 dprintk("%s: server_scope mismatch detected\n",
4998 __func__);
4999 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5000 kfree(clp->server_scope);
5001 clp->server_scope = NULL;
5002 }
5003
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05005004 if (!clp->server_scope) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005005 clp->server_scope = res.server_scope;
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05005006 goto out;
5007 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005008 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005009
5010out_server_scope:
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05005011 kfree(res.server_scope);
5012out:
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005013 if (clp->impl_id)
5014 dprintk("%s: Server Implementation ID: "
5015 "domain: %s, name: %s, date: %llu,%u\n",
5016 __func__, clp->impl_id->domain, clp->impl_id->name,
5017 clp->impl_id->date.seconds,
5018 clp->impl_id->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005019 dprintk("<-- %s status= %d\n", __func__, status);
5020 return status;
5021}
5022
Andy Adamson2050f0c2009-04-01 09:22:30 -04005023struct nfs4_get_lease_time_data {
5024 struct nfs4_get_lease_time_args *args;
5025 struct nfs4_get_lease_time_res *res;
5026 struct nfs_client *clp;
5027};
5028
5029static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5030 void *calldata)
5031{
5032 int ret;
5033 struct nfs4_get_lease_time_data *data =
5034 (struct nfs4_get_lease_time_data *)calldata;
5035
5036 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005037 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005038 /* just setup sequence, do not trigger session recovery
5039 since we're invoked within one */
5040 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005041 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005042 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005043
5044 BUG_ON(ret == -EAGAIN);
5045 rpc_call_start(task);
5046 dprintk("<-- %s\n", __func__);
5047}
5048
5049/*
5050 * Called from nfs4_state_manager thread for session setup, so don't recover
5051 * from sequence operation or clientid errors.
5052 */
5053static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5054{
5055 struct nfs4_get_lease_time_data *data =
5056 (struct nfs4_get_lease_time_data *)calldata;
5057
5058 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005059 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5060 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005061 switch (task->tk_status) {
5062 case -NFS4ERR_DELAY:
5063 case -NFS4ERR_GRACE:
5064 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5065 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5066 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005067 /* fall through */
5068 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005069 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005070 return;
5071 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005072 dprintk("<-- %s\n", __func__);
5073}
5074
5075struct rpc_call_ops nfs4_get_lease_time_ops = {
5076 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5077 .rpc_call_done = nfs4_get_lease_time_done,
5078};
5079
5080int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5081{
5082 struct rpc_task *task;
5083 struct nfs4_get_lease_time_args args;
5084 struct nfs4_get_lease_time_res res = {
5085 .lr_fsinfo = fsinfo,
5086 };
5087 struct nfs4_get_lease_time_data data = {
5088 .args = &args,
5089 .res = &res,
5090 .clp = clp,
5091 };
5092 struct rpc_message msg = {
5093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5094 .rpc_argp = &args,
5095 .rpc_resp = &res,
5096 };
5097 struct rpc_task_setup task_setup = {
5098 .rpc_client = clp->cl_rpcclient,
5099 .rpc_message = &msg,
5100 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005101 .callback_data = &data,
5102 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005103 };
5104 int status;
5105
Trond Myklebust9d12b212012-01-17 22:04:25 -05005106 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005107 dprintk("--> %s\n", __func__);
5108 task = rpc_run_task(&task_setup);
5109
5110 if (IS_ERR(task))
5111 status = PTR_ERR(task);
5112 else {
5113 status = task->tk_status;
5114 rpc_put_task(task);
5115 }
5116 dprintk("<-- %s return %d\n", __func__, status);
5117
5118 return status;
5119}
5120
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005121static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5122{
5123 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5124}
5125
5126static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5127 struct nfs4_slot *new,
5128 u32 max_slots,
5129 u32 ivalue)
5130{
5131 struct nfs4_slot *old = NULL;
5132 u32 i;
5133
5134 spin_lock(&tbl->slot_tbl_lock);
5135 if (new) {
5136 old = tbl->slots;
5137 tbl->slots = new;
5138 tbl->max_slots = max_slots;
5139 }
5140 tbl->highest_used_slotid = -1; /* no slot is currently used */
5141 for (i = 0; i < tbl->max_slots; i++)
5142 tbl->slots[i].seq_nr = ivalue;
5143 spin_unlock(&tbl->slot_tbl_lock);
5144 kfree(old);
5145}
5146
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005147/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005148 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005149 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005150static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5151 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005152{
Andy Adamson104aeba2010-01-14 17:45:10 -05005153 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005154 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005155
Andy Adamson104aeba2010-01-14 17:45:10 -05005156 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5157 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005158
Andy Adamson104aeba2010-01-14 17:45:10 -05005159 /* Does the newly negotiated max_reqs match the existing slot table? */
5160 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005161 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005162 if (!new)
5163 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005164 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005165 ret = 0;
5166
5167 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005168 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5169 tbl, tbl->slots, tbl->max_slots);
5170out:
5171 dprintk("<-- %s: return %d\n", __func__, ret);
5172 return ret;
5173}
5174
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005175/* Destroy the slot table */
5176static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5177{
5178 if (session->fc_slot_table.slots != NULL) {
5179 kfree(session->fc_slot_table.slots);
5180 session->fc_slot_table.slots = NULL;
5181 }
5182 if (session->bc_slot_table.slots != NULL) {
5183 kfree(session->bc_slot_table.slots);
5184 session->bc_slot_table.slots = NULL;
5185 }
5186 return;
5187}
5188
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005189/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005190 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005191 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005192static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005193{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005194 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005195 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005196
Andy Adamsonaacd5532011-11-09 13:58:21 -05005197 dprintk("--> %s\n", __func__);
5198 /* Fore channel */
5199 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005200 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5201 if (status) /* -ENOMEM */
5202 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005203 /* Back channel */
5204 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005205 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5206 if (status && tbl->slots == NULL)
5207 /* Fore and back channel share a connection so get
5208 * both slot tables or neither */
5209 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005210 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005211}
5212
5213struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5214{
5215 struct nfs4_session *session;
5216 struct nfs4_slot_table *tbl;
5217
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005218 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005219 if (!session)
5220 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005221
Andy Adamson557134a2009-04-01 09:21:53 -04005222 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005223 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005224 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005225 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005226 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005227
5228 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005229 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005230 spin_lock_init(&tbl->slot_tbl_lock);
5231 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005232 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005233
Trond Myklebust1055d762010-06-16 09:52:27 -04005234 session->session_state = 1<<NFS4_SESSION_INITING;
5235
Andy Adamson557134a2009-04-01 09:21:53 -04005236 session->clp = clp;
5237 return session;
5238}
5239
5240void nfs4_destroy_session(struct nfs4_session *session)
5241{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005242 struct rpc_xprt *xprt;
5243
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005244 nfs4_proc_destroy_session(session);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005245
5246 rcu_read_lock();
5247 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5248 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005249 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005250 __func__, xprt);
5251 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005252 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005253 kfree(session);
5254}
5255
Andy Adamsonfc931582009-04-01 09:22:31 -04005256/*
5257 * Initialize the values to be used by the client in CREATE_SESSION
5258 * If nfs4_init_session set the fore channel request and response sizes,
5259 * use them.
5260 *
5261 * Set the back channel max_resp_sz_cached to zero to force the client to
5262 * always set csa_cachethis to FALSE because the current implementation
5263 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5264 */
5265static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5266{
5267 struct nfs4_session *session = args->client->cl_session;
5268 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5269 mxresp_sz = session->fc_attrs.max_resp_sz;
5270
5271 if (mxrqst_sz == 0)
5272 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5273 if (mxresp_sz == 0)
5274 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5275 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005276 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5277 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005278 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005279 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005280
5281 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005282 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005283 __func__,
5284 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005285 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005286
5287 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005288 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5289 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5290 args->bc_attrs.max_resp_sz_cached = 0;
5291 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5292 args->bc_attrs.max_reqs = 1;
5293
5294 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5295 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5296 __func__,
5297 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5298 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5299 args->bc_attrs.max_reqs);
5300}
5301
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005302static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005303{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005304 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5305 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5306
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005307 if (rcvd->max_resp_sz > sent->max_resp_sz)
5308 return -EINVAL;
5309 /*
5310 * Our requested max_ops is the minimum we need; we're not
5311 * prepared to break up compounds into smaller pieces than that.
5312 * So, no point even trying to continue if the server won't
5313 * cooperate:
5314 */
5315 if (rcvd->max_ops < sent->max_ops)
5316 return -EINVAL;
5317 if (rcvd->max_reqs == 0)
5318 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005319 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5320 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005321 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005322}
5323
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005324static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5325{
5326 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5327 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005328
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005329 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5330 return -EINVAL;
5331 if (rcvd->max_resp_sz < sent->max_resp_sz)
5332 return -EINVAL;
5333 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5334 return -EINVAL;
5335 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005336 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005337 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005338 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005339 return -EINVAL;
5340 return 0;
5341}
Andy Adamson8d353012009-04-01 09:22:32 -04005342
Andy Adamson8d353012009-04-01 09:22:32 -04005343static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5344 struct nfs4_session *session)
5345{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005346 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005347
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005348 ret = nfs4_verify_fore_channel_attrs(args, session);
5349 if (ret)
5350 return ret;
5351 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005352}
5353
Andy Adamsonfc931582009-04-01 09:22:31 -04005354static int _nfs4_proc_create_session(struct nfs_client *clp)
5355{
5356 struct nfs4_session *session = clp->cl_session;
5357 struct nfs41_create_session_args args = {
5358 .client = clp,
5359 .cb_program = NFS4_CALLBACK,
5360 };
5361 struct nfs41_create_session_res res = {
5362 .client = clp,
5363 };
5364 struct rpc_message msg = {
5365 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5366 .rpc_argp = &args,
5367 .rpc_resp = &res,
5368 };
5369 int status;
5370
5371 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005372 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005373
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005374 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005375
Andy Adamson8d353012009-04-01 09:22:32 -04005376 if (!status)
5377 /* Verify the session's negotiated channel_attrs values */
5378 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005379 if (!status) {
5380 /* Increment the clientid slot sequence id */
5381 clp->cl_seqid++;
5382 }
5383
5384 return status;
5385}
5386
5387/*
5388 * Issues a CREATE_SESSION operation to the server.
5389 * It is the responsibility of the caller to verify the session is
5390 * expired before calling this routine.
5391 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005392int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005393{
5394 int status;
5395 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005396 struct nfs4_session *session = clp->cl_session;
5397
5398 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5399
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005400 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005401 if (status)
5402 goto out;
5403
Andy Adamsonaacd5532011-11-09 13:58:21 -05005404 /* Init or reset the session slot tables */
5405 status = nfs4_setup_session_slot_tables(session);
5406 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005407 if (status)
5408 goto out;
5409
5410 ptr = (unsigned *)&session->sess_id.data[0];
5411 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5412 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005413out:
5414 dprintk("<-- %s\n", __func__);
5415 return status;
5416}
5417
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005418/*
5419 * Issue the over-the-wire RPC DESTROY_SESSION.
5420 * The caller must serialize access to this routine.
5421 */
5422int nfs4_proc_destroy_session(struct nfs4_session *session)
5423{
5424 int status = 0;
5425 struct rpc_message msg;
5426
5427 dprintk("--> nfs4_proc_destroy_session\n");
5428
5429 /* session is still being setup */
5430 if (session->clp->cl_cons_state != NFS_CS_READY)
5431 return status;
5432
5433 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5434 msg.rpc_argp = session;
5435 msg.rpc_resp = NULL;
5436 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005437 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005438
5439 if (status)
5440 printk(KERN_WARNING
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -05005441 "NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005442 "Session has been destroyed regardless...\n", status);
5443
5444 dprintk("<-- nfs4_proc_destroy_session\n");
5445 return status;
5446}
5447
Trond Myklebustfccba802009-07-21 16:48:07 -04005448int nfs4_init_session(struct nfs_server *server)
5449{
5450 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005451 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005452 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005453 int ret;
5454
5455 if (!nfs4_has_session(clp))
5456 return 0;
5457
Trond Myklebust1055d762010-06-16 09:52:27 -04005458 session = clp->cl_session;
5459 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5460 return 0;
5461
Andy Adamson68bf05e2009-12-15 12:55:02 -05005462 rsize = server->rsize;
5463 if (rsize == 0)
5464 rsize = NFS_MAX_FILE_IO_SIZE;
5465 wsize = server->wsize;
5466 if (wsize == 0)
5467 wsize = NFS_MAX_FILE_IO_SIZE;
5468
Andy Adamson68bf05e2009-12-15 12:55:02 -05005469 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5470 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005471
Trond Myklebustfccba802009-07-21 16:48:07 -04005472 ret = nfs4_recover_expired_lease(server);
5473 if (!ret)
5474 ret = nfs4_check_client_ready(clp);
5475 return ret;
5476}
5477
Andy Adamsond83217c2011-03-01 01:34:17 +00005478int nfs4_init_ds_session(struct nfs_client *clp)
5479{
5480 struct nfs4_session *session = clp->cl_session;
5481 int ret;
5482
5483 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5484 return 0;
5485
5486 ret = nfs4_client_recover_expired_lease(clp);
5487 if (!ret)
5488 /* Test for the DS role */
5489 if (!is_ds_client(clp))
5490 ret = -ENODEV;
5491 if (!ret)
5492 ret = nfs4_check_client_ready(clp);
5493 return ret;
5494
5495}
5496EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5497
5498
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005499/*
5500 * Renew the cl_session lease.
5501 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005502struct nfs4_sequence_data {
5503 struct nfs_client *clp;
5504 struct nfs4_sequence_args args;
5505 struct nfs4_sequence_res res;
5506};
5507
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005508static void nfs41_sequence_release(void *data)
5509{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005510 struct nfs4_sequence_data *calldata = data;
5511 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005512
Alexandros Batsakis71358402010-02-05 03:45:05 -08005513 if (atomic_read(&clp->cl_count) > 1)
5514 nfs4_schedule_state_renewal(clp);
5515 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005516 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005517}
5518
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005519static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5520{
5521 switch(task->tk_status) {
5522 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005523 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5524 return -EAGAIN;
5525 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005526 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005527 }
5528 return 0;
5529}
5530
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005531static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005532{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005533 struct nfs4_sequence_data *calldata = data;
5534 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005535
Trond Myklebust14516c32010-07-31 14:29:06 -04005536 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5537 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005538
5539 if (task->tk_status < 0) {
5540 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005541 if (atomic_read(&clp->cl_count) == 1)
5542 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005543
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005544 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5545 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005546 return;
5547 }
5548 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005549 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005550out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005551 dprintk("<-- %s\n", __func__);
5552}
5553
5554static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5555{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005556 struct nfs4_sequence_data *calldata = data;
5557 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005558 struct nfs4_sequence_args *args;
5559 struct nfs4_sequence_res *res;
5560
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005561 args = task->tk_msg.rpc_argp;
5562 res = task->tk_msg.rpc_resp;
5563
Trond Myklebust9d12b212012-01-17 22:04:25 -05005564 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005565 return;
5566 rpc_call_start(task);
5567}
5568
5569static const struct rpc_call_ops nfs41_sequence_ops = {
5570 .rpc_call_done = nfs41_sequence_call_done,
5571 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005572 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005573};
5574
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005575static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005576{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005577 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005578 struct rpc_message msg = {
5579 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5580 .rpc_cred = cred,
5581 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005582 struct rpc_task_setup task_setup_data = {
5583 .rpc_client = clp->cl_rpcclient,
5584 .rpc_message = &msg,
5585 .callback_ops = &nfs41_sequence_ops,
5586 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5587 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005588
Alexandros Batsakis71358402010-02-05 03:45:05 -08005589 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005590 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005591 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005592 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005593 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005594 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005595 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005596 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005597 msg.rpc_argp = &calldata->args;
5598 msg.rpc_resp = &calldata->res;
5599 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005600 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005601
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005602 return rpc_run_task(&task_setup_data);
5603}
5604
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005605static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005606{
5607 struct rpc_task *task;
5608 int ret = 0;
5609
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005610 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5611 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005612 task = _nfs41_proc_sequence(clp, cred);
5613 if (IS_ERR(task))
5614 ret = PTR_ERR(task);
5615 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005616 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005617 dprintk("<-- %s status=%d\n", __func__, ret);
5618 return ret;
5619}
5620
5621static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5622{
5623 struct rpc_task *task;
5624 int ret;
5625
5626 task = _nfs41_proc_sequence(clp, cred);
5627 if (IS_ERR(task)) {
5628 ret = PTR_ERR(task);
5629 goto out;
5630 }
5631 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005632 if (!ret) {
5633 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5634
5635 if (task->tk_status == 0)
5636 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005637 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005638 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005639 rpc_put_task(task);
5640out:
5641 dprintk("<-- %s status=%d\n", __func__, ret);
5642 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005643}
5644
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005645struct nfs4_reclaim_complete_data {
5646 struct nfs_client *clp;
5647 struct nfs41_reclaim_complete_args arg;
5648 struct nfs41_reclaim_complete_res res;
5649};
5650
5651static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5652{
5653 struct nfs4_reclaim_complete_data *calldata = data;
5654
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005655 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005656 if (nfs41_setup_sequence(calldata->clp->cl_session,
5657 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005658 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005659 return;
5660
5661 rpc_call_start(task);
5662}
5663
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005664static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5665{
5666 switch(task->tk_status) {
5667 case 0:
5668 case -NFS4ERR_COMPLETE_ALREADY:
5669 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5670 break;
5671 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005672 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005673 /* fall through */
5674 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005675 return -EAGAIN;
5676 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005677 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005678 }
5679 return 0;
5680}
5681
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005682static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5683{
5684 struct nfs4_reclaim_complete_data *calldata = data;
5685 struct nfs_client *clp = calldata->clp;
5686 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5687
5688 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005689 if (!nfs41_sequence_done(task, res))
5690 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005691
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005692 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5693 rpc_restart_call_prepare(task);
5694 return;
5695 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005696 dprintk("<-- %s\n", __func__);
5697}
5698
5699static void nfs4_free_reclaim_complete_data(void *data)
5700{
5701 struct nfs4_reclaim_complete_data *calldata = data;
5702
5703 kfree(calldata);
5704}
5705
5706static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5707 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5708 .rpc_call_done = nfs4_reclaim_complete_done,
5709 .rpc_release = nfs4_free_reclaim_complete_data,
5710};
5711
5712/*
5713 * Issue a global reclaim complete.
5714 */
5715static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5716{
5717 struct nfs4_reclaim_complete_data *calldata;
5718 struct rpc_task *task;
5719 struct rpc_message msg = {
5720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5721 };
5722 struct rpc_task_setup task_setup_data = {
5723 .rpc_client = clp->cl_rpcclient,
5724 .rpc_message = &msg,
5725 .callback_ops = &nfs4_reclaim_complete_call_ops,
5726 .flags = RPC_TASK_ASYNC,
5727 };
5728 int status = -ENOMEM;
5729
5730 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005731 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005732 if (calldata == NULL)
5733 goto out;
5734 calldata->clp = clp;
5735 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005736
Trond Myklebust9d12b212012-01-17 22:04:25 -05005737 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005738 msg.rpc_argp = &calldata->arg;
5739 msg.rpc_resp = &calldata->res;
5740 task_setup_data.callback_data = calldata;
5741 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005742 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005743 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005744 goto out;
5745 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005746 status = nfs4_wait_for_completion_rpc_task(task);
5747 if (status == 0)
5748 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005749 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005750 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005751out:
5752 dprintk("<-- %s status=%d\n", __func__, status);
5753 return status;
5754}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005755
5756static void
5757nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5758{
5759 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005760 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005761
5762 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005763 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5764 * right now covering the LAYOUTGET we are about to send.
5765 * However, that is not so catastrophic, and there seems
5766 * to be no way to prevent it completely.
5767 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005768 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005769 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005770 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005771 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5772 NFS_I(lgp->args.inode)->layout,
5773 lgp->args.ctx->state)) {
5774 rpc_exit(task, NFS4_OK);
5775 return;
5776 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005777 rpc_call_start(task);
5778}
5779
5780static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5781{
5782 struct nfs4_layoutget *lgp = calldata;
5783 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5784
5785 dprintk("--> %s\n", __func__);
5786
5787 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5788 return;
5789
5790 switch (task->tk_status) {
5791 case 0:
5792 break;
5793 case -NFS4ERR_LAYOUTTRYLATER:
5794 case -NFS4ERR_RECALLCONFLICT:
5795 task->tk_status = -NFS4ERR_DELAY;
5796 /* Fall through */
5797 default:
5798 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5799 rpc_restart_call_prepare(task);
5800 return;
5801 }
5802 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005803 dprintk("<-- %s\n", __func__);
5804}
5805
5806static void nfs4_layoutget_release(void *calldata)
5807{
5808 struct nfs4_layoutget *lgp = calldata;
5809
5810 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005811 put_nfs_open_context(lgp->args.ctx);
5812 kfree(calldata);
5813 dprintk("<-- %s\n", __func__);
5814}
5815
5816static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5817 .rpc_call_prepare = nfs4_layoutget_prepare,
5818 .rpc_call_done = nfs4_layoutget_done,
5819 .rpc_release = nfs4_layoutget_release,
5820};
5821
5822int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5823{
5824 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5825 struct rpc_task *task;
5826 struct rpc_message msg = {
5827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5828 .rpc_argp = &lgp->args,
5829 .rpc_resp = &lgp->res,
5830 };
5831 struct rpc_task_setup task_setup_data = {
5832 .rpc_client = server->client,
5833 .rpc_message = &msg,
5834 .callback_ops = &nfs4_layoutget_call_ops,
5835 .callback_data = lgp,
5836 .flags = RPC_TASK_ASYNC,
5837 };
5838 int status = 0;
5839
5840 dprintk("--> %s\n", __func__);
5841
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005842 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005843 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05005844 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005845 task = rpc_run_task(&task_setup_data);
5846 if (IS_ERR(task))
5847 return PTR_ERR(task);
5848 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005849 if (status == 0)
5850 status = task->tk_status;
5851 if (status == 0)
5852 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005853 rpc_put_task(task);
5854 dprintk("<-- %s status=%d\n", __func__, status);
5855 return status;
5856}
5857
Benny Halevycbe82602011-05-22 19:52:37 +03005858static void
5859nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5860{
5861 struct nfs4_layoutreturn *lrp = calldata;
5862
5863 dprintk("--> %s\n", __func__);
5864 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005865 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03005866 return;
5867 rpc_call_start(task);
5868}
5869
5870static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5871{
5872 struct nfs4_layoutreturn *lrp = calldata;
5873 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005874 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005875
5876 dprintk("--> %s\n", __func__);
5877
5878 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5879 return;
5880
5881 server = NFS_SERVER(lrp->args.inode);
5882 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005883 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03005884 return;
5885 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005886 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005887 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005888 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005889 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005890 } else
5891 BUG_ON(!list_empty(&lo->plh_segs));
5892 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005893 lo->plh_block_lgets--;
5894 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005895 dprintk("<-- %s\n", __func__);
5896}
5897
5898static void nfs4_layoutreturn_release(void *calldata)
5899{
5900 struct nfs4_layoutreturn *lrp = calldata;
5901
5902 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005903 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005904 kfree(calldata);
5905 dprintk("<-- %s\n", __func__);
5906}
5907
5908static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5909 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5910 .rpc_call_done = nfs4_layoutreturn_done,
5911 .rpc_release = nfs4_layoutreturn_release,
5912};
5913
5914int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5915{
5916 struct rpc_task *task;
5917 struct rpc_message msg = {
5918 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5919 .rpc_argp = &lrp->args,
5920 .rpc_resp = &lrp->res,
5921 };
5922 struct rpc_task_setup task_setup_data = {
5923 .rpc_client = lrp->clp->cl_rpcclient,
5924 .rpc_message = &msg,
5925 .callback_ops = &nfs4_layoutreturn_call_ops,
5926 .callback_data = lrp,
5927 };
5928 int status;
5929
5930 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05005931 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03005932 task = rpc_run_task(&task_setup_data);
5933 if (IS_ERR(task))
5934 return PTR_ERR(task);
5935 status = task->tk_status;
5936 dprintk("<-- %s status=%d\n", __func__, status);
5937 rpc_put_task(task);
5938 return status;
5939}
5940
Andy Adamson7f11d8d2011-07-30 20:52:35 -04005941/*
5942 * Retrieve the list of Data Server devices from the MDS.
5943 */
5944static int _nfs4_getdevicelist(struct nfs_server *server,
5945 const struct nfs_fh *fh,
5946 struct pnfs_devicelist *devlist)
5947{
5948 struct nfs4_getdevicelist_args args = {
5949 .fh = fh,
5950 .layoutclass = server->pnfs_curr_ld->id,
5951 };
5952 struct nfs4_getdevicelist_res res = {
5953 .devlist = devlist,
5954 };
5955 struct rpc_message msg = {
5956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5957 .rpc_argp = &args,
5958 .rpc_resp = &res,
5959 };
5960 int status;
5961
5962 dprintk("--> %s\n", __func__);
5963 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5964 &res.seq_res, 0);
5965 dprintk("<-- %s status=%d\n", __func__, status);
5966 return status;
5967}
5968
5969int nfs4_proc_getdevicelist(struct nfs_server *server,
5970 const struct nfs_fh *fh,
5971 struct pnfs_devicelist *devlist)
5972{
5973 struct nfs4_exception exception = { };
5974 int err;
5975
5976 do {
5977 err = nfs4_handle_exception(server,
5978 _nfs4_getdevicelist(server, fh, devlist),
5979 &exception);
5980 } while (exception.retry);
5981
5982 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5983 err, devlist->num_devs);
5984
5985 return err;
5986}
5987EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5988
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005989static int
5990_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5991{
5992 struct nfs4_getdeviceinfo_args args = {
5993 .pdev = pdev,
5994 };
5995 struct nfs4_getdeviceinfo_res res = {
5996 .pdev = pdev,
5997 };
5998 struct rpc_message msg = {
5999 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6000 .rpc_argp = &args,
6001 .rpc_resp = &res,
6002 };
6003 int status;
6004
6005 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006006 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006007 dprintk("<-- %s status=%d\n", __func__, status);
6008
6009 return status;
6010}
6011
6012int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6013{
6014 struct nfs4_exception exception = { };
6015 int err;
6016
6017 do {
6018 err = nfs4_handle_exception(server,
6019 _nfs4_proc_getdeviceinfo(server, pdev),
6020 &exception);
6021 } while (exception.retry);
6022 return err;
6023}
6024EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6025
Andy Adamson863a3c62011-03-23 13:27:54 +00006026static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6027{
6028 struct nfs4_layoutcommit_data *data = calldata;
6029 struct nfs_server *server = NFS_SERVER(data->args.inode);
6030
6031 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006032 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006033 return;
6034 rpc_call_start(task);
6035}
6036
6037static void
6038nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6039{
6040 struct nfs4_layoutcommit_data *data = calldata;
6041 struct nfs_server *server = NFS_SERVER(data->args.inode);
6042
6043 if (!nfs4_sequence_done(task, &data->res.seq_res))
6044 return;
6045
6046 switch (task->tk_status) { /* Just ignore these failures */
6047 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6048 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6049 case NFS4ERR_BADLAYOUT: /* no layout */
6050 case NFS4ERR_GRACE: /* loca_recalim always false */
6051 task->tk_status = 0;
6052 }
6053
6054 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006055 rpc_restart_call_prepare(task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006056 return;
6057 }
6058
6059 if (task->tk_status == 0)
6060 nfs_post_op_update_inode_force_wcc(data->args.inode,
6061 data->res.fattr);
6062}
6063
6064static void nfs4_layoutcommit_release(void *calldata)
6065{
6066 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006067 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006068 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006069
Andy Adamsondb29c082011-07-30 20:52:38 -04006070 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006071 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006072 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6073 list_del_init(&lseg->pls_lc_list);
6074 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6075 &lseg->pls_flags))
6076 put_lseg(lseg);
6077 }
Peng Tao92407e72011-10-23 20:21:17 -07006078
6079 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6080 smp_mb__after_clear_bit();
6081 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6082
Andy Adamson863a3c62011-03-23 13:27:54 +00006083 put_rpccred(data->cred);
6084 kfree(data);
6085}
6086
6087static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6088 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6089 .rpc_call_done = nfs4_layoutcommit_done,
6090 .rpc_release = nfs4_layoutcommit_release,
6091};
6092
6093int
Andy Adamsonef311532011-03-12 02:58:10 -05006094nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006095{
6096 struct rpc_message msg = {
6097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6098 .rpc_argp = &data->args,
6099 .rpc_resp = &data->res,
6100 .rpc_cred = data->cred,
6101 };
6102 struct rpc_task_setup task_setup_data = {
6103 .task = &data->task,
6104 .rpc_client = NFS_CLIENT(data->args.inode),
6105 .rpc_message = &msg,
6106 .callback_ops = &nfs4_layoutcommit_ops,
6107 .callback_data = data,
6108 .flags = RPC_TASK_ASYNC,
6109 };
6110 struct rpc_task *task;
6111 int status = 0;
6112
6113 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6114 "lbw: %llu inode %lu\n",
6115 data->task.tk_pid, sync,
6116 data->args.lastbytewritten,
6117 data->args.inode->i_ino);
6118
Trond Myklebust9d12b212012-01-17 22:04:25 -05006119 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006120 task = rpc_run_task(&task_setup_data);
6121 if (IS_ERR(task))
6122 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006123 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006124 goto out;
6125 status = nfs4_wait_for_completion_rpc_task(task);
6126 if (status != 0)
6127 goto out;
6128 status = task->tk_status;
6129out:
6130 dprintk("%s: status %d\n", __func__, status);
6131 rpc_put_task(task);
6132 return status;
6133}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006134
6135static int
6136_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6137 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6138{
6139 struct nfs41_secinfo_no_name_args args = {
6140 .style = SECINFO_STYLE_CURRENT_FH,
6141 };
6142 struct nfs4_secinfo_res res = {
6143 .flavors = flavors,
6144 };
6145 struct rpc_message msg = {
6146 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6147 .rpc_argp = &args,
6148 .rpc_resp = &res,
6149 };
6150 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6151}
6152
6153static int
6154nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6155 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6156{
6157 struct nfs4_exception exception = { };
6158 int err;
6159 do {
6160 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6161 switch (err) {
6162 case 0:
6163 case -NFS4ERR_WRONGSEC:
6164 case -NFS4ERR_NOTSUPP:
6165 break;
6166 default:
6167 err = nfs4_handle_exception(server, err, &exception);
6168 }
6169 } while (exception.retry);
6170 return err;
6171}
6172
6173static int
6174nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6175 struct nfs_fsinfo *info)
6176{
6177 int err;
6178 struct page *page;
6179 rpc_authflavor_t flavor;
6180 struct nfs4_secinfo_flavors *flavors;
6181
6182 page = alloc_page(GFP_KERNEL);
6183 if (!page) {
6184 err = -ENOMEM;
6185 goto out;
6186 }
6187
6188 flavors = page_address(page);
6189 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6190
6191 /*
6192 * Fall back on "guess and check" method if
6193 * the server doesn't support SECINFO_NO_NAME
6194 */
6195 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6196 err = nfs4_find_root_sec(server, fhandle, info);
6197 goto out_freepage;
6198 }
6199 if (err)
6200 goto out_freepage;
6201
6202 flavor = nfs_find_best_sec(flavors);
6203 if (err == 0)
6204 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6205
6206out_freepage:
6207 put_page(page);
6208 if (err == -EACCES)
6209 return -EPERM;
6210out:
6211 return err;
6212}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006213
6214static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006215{
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006216 int status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006217 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006218 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006219 };
6220 struct nfs41_test_stateid_res res;
6221 struct rpc_message msg = {
6222 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6223 .rpc_argp = &args,
6224 .rpc_resp = &res,
6225 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006226
Trond Myklebust9d12b212012-01-17 22:04:25 -05006227 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006228 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6229
6230 if (status == NFS_OK)
6231 return res.status;
6232 return status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006233}
6234
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006235static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006236{
6237 struct nfs4_exception exception = { };
6238 int err;
6239 do {
6240 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006241 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006242 &exception);
6243 } while (exception.retry);
6244 return err;
6245}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006246
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006247static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006248{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006249 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006250 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006251 };
6252 struct nfs41_free_stateid_res res;
6253 struct rpc_message msg = {
6254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6255 .rpc_argp = &args,
6256 .rpc_resp = &res,
6257 };
6258
Trond Myklebust9d12b212012-01-17 22:04:25 -05006259 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6260 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006261}
6262
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006263static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006264{
6265 struct nfs4_exception exception = { };
6266 int err;
6267 do {
6268 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006269 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006270 &exception);
6271 } while (exception.retry);
6272 return err;
6273}
Andy Adamson557134a2009-04-01 09:21:53 -04006274#endif /* CONFIG_NFS_V4_1 */
6275
Andy Adamson591d71c2009-04-01 09:22:47 -04006276struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006277 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006278 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279 .recover_open = nfs4_open_reclaim,
6280 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006281 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006282 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283};
6284
Andy Adamson591d71c2009-04-01 09:22:47 -04006285#if defined(CONFIG_NFS_V4_1)
6286struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6287 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6288 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6289 .recover_open = nfs4_open_reclaim,
6290 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006291 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006292 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006293 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006294};
6295#endif /* CONFIG_NFS_V4_1 */
6296
6297struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006298 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006299 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300 .recover_open = nfs4_open_expired,
6301 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006302 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006303 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304};
6305
Andy Adamson591d71c2009-04-01 09:22:47 -04006306#if defined(CONFIG_NFS_V4_1)
6307struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6308 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6309 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006310 .recover_open = nfs41_open_expired,
6311 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006312 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006313 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006314};
6315#endif /* CONFIG_NFS_V4_1 */
6316
Benny Halevy29fba382009-04-01 09:22:44 -04006317struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6318 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006319 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006320 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006321};
6322
6323#if defined(CONFIG_NFS_V4_1)
6324struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6325 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006326 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006327 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006328};
6329#endif
6330
Trond Myklebust97dc1352010-06-16 09:52:26 -04006331static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6332 .minor_version = 0,
6333 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04006334 .validate_stateid = nfs4_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006335 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006336 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6337 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6338 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006339};
6340
6341#if defined(CONFIG_NFS_V4_1)
6342static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6343 .minor_version = 1,
6344 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04006345 .validate_stateid = nfs41_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006346 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006347 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6348 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6349 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006350};
6351#endif
6352
Trond Myklebust97dc1352010-06-16 09:52:26 -04006353const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6354 [0] = &nfs_v4_0_minor_ops,
6355#if defined(CONFIG_NFS_V4_1)
6356 [1] = &nfs_v4_1_minor_ops,
6357#endif
6358};
6359
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006360static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006361 .permission = nfs_permission,
6362 .getattr = nfs_getattr,
6363 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006364 .getxattr = generic_getxattr,
6365 .setxattr = generic_setxattr,
6366 .listxattr = generic_listxattr,
6367 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006368};
6369
David Howells509de812006-08-22 20:06:11 -04006370const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 .version = 4, /* protocol version */
6372 .dentry_ops = &nfs4_dentry_operations,
6373 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006374 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006375 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 .getroot = nfs4_proc_get_root,
6377 .getattr = nfs4_proc_getattr,
6378 .setattr = nfs4_proc_setattr,
6379 .lookup = nfs4_proc_lookup,
6380 .access = nfs4_proc_access,
6381 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382 .create = nfs4_proc_create,
6383 .remove = nfs4_proc_remove,
6384 .unlink_setup = nfs4_proc_unlink_setup,
6385 .unlink_done = nfs4_proc_unlink_done,
6386 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006387 .rename_setup = nfs4_proc_rename_setup,
6388 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389 .link = nfs4_proc_link,
6390 .symlink = nfs4_proc_symlink,
6391 .mkdir = nfs4_proc_mkdir,
6392 .rmdir = nfs4_proc_remove,
6393 .readdir = nfs4_proc_readdir,
6394 .mknod = nfs4_proc_mknod,
6395 .statfs = nfs4_proc_statfs,
6396 .fsinfo = nfs4_proc_fsinfo,
6397 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006398 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399 .decode_dirent = nfs4_decode_dirent,
6400 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006401 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006403 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006405 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006407 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006408 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006409 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006410 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006411 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412};
6413
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006414static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6415 .prefix = XATTR_NAME_NFSV4_ACL,
6416 .list = nfs4_xattr_list_nfs4_acl,
6417 .get = nfs4_xattr_get_nfs4_acl,
6418 .set = nfs4_xattr_set_nfs4_acl,
6419};
6420
6421const struct xattr_handler *nfs4_xattr_handlers[] = {
6422 &nfs4_xattr_nfs4_acl_handler,
6423 NULL
6424};
6425
Trond Myklebustef159e92012-02-06 19:50:40 -05006426module_param(max_session_slots, ushort, 0644);
6427MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6428 "requests the client will negotiate");
6429
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430/*
6431 * Local variables:
6432 * c-basic-offset: 8
6433 * End:
6434 */