blob: 6c8e170e2e6bf941962255e6db0566c31bfece57 [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 *);
Trond Myklebust99367812007-07-17 21:52:41 -040082static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040083static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
84 struct nfs_fattr *fattr, struct iattr *sattr,
85 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040086#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050087static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
88static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040089#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050091static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Trond Myklebust52567b02009-10-23 14:46:42 -040093 if (err >= -1000)
94 return err;
95 switch (err) {
96 case -NFS4ERR_RESOURCE:
97 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000098 case -NFS4ERR_WRONGSEC:
99 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800100 case -NFS4ERR_BADOWNER:
101 case -NFS4ERR_BADNAME:
102 return -EINVAL;
Trond Myklebust52567b02009-10-23 14:46:42 -0400103 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700105 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400106 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400108 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
111/*
112 * This is our standard bitmap for GETATTR requests.
113 */
114const u32 nfs4_fattr_bitmap[2] = {
115 FATTR4_WORD0_TYPE
116 | FATTR4_WORD0_CHANGE
117 | FATTR4_WORD0_SIZE
118 | FATTR4_WORD0_FSID
119 | FATTR4_WORD0_FILEID,
120 FATTR4_WORD1_MODE
121 | FATTR4_WORD1_NUMLINKS
122 | FATTR4_WORD1_OWNER
123 | FATTR4_WORD1_OWNER_GROUP
124 | FATTR4_WORD1_RAWDEV
125 | FATTR4_WORD1_SPACE_USED
126 | FATTR4_WORD1_TIME_ACCESS
127 | FATTR4_WORD1_TIME_METADATA
128 | FATTR4_WORD1_TIME_MODIFY
129};
130
131const u32 nfs4_statfs_bitmap[2] = {
132 FATTR4_WORD0_FILES_AVAIL
133 | FATTR4_WORD0_FILES_FREE
134 | FATTR4_WORD0_FILES_TOTAL,
135 FATTR4_WORD1_SPACE_AVAIL
136 | FATTR4_WORD1_SPACE_FREE
137 | FATTR4_WORD1_SPACE_TOTAL
138};
139
Trond Myklebust4ce79712005-06-22 17:16:21 +0000140const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 FATTR4_WORD0_MAXLINK
142 | FATTR4_WORD0_MAXNAME,
143 0
144};
145
Fred Isamandae100c2011-07-30 20:52:37 -0400146const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 | FATTR4_WORD0_MAXREAD
148 | FATTR4_WORD0_MAXWRITE
149 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700150 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400151 | FATTR4_WORD1_FS_LAYOUT_TYPES,
152 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153};
154
Manoj Naik830b8e32006-06-09 09:34:25 -0400155const u32 nfs4_fs_locations_bitmap[2] = {
156 FATTR4_WORD0_TYPE
157 | FATTR4_WORD0_CHANGE
158 | FATTR4_WORD0_SIZE
159 | FATTR4_WORD0_FSID
160 | FATTR4_WORD0_FILEID
161 | FATTR4_WORD0_FS_LOCATIONS,
162 FATTR4_WORD1_MODE
163 | FATTR4_WORD1_NUMLINKS
164 | FATTR4_WORD1_OWNER
165 | FATTR4_WORD1_OWNER_GROUP
166 | FATTR4_WORD1_RAWDEV
167 | FATTR4_WORD1_SPACE_USED
168 | FATTR4_WORD1_TIME_ACCESS
169 | FATTR4_WORD1_TIME_METADATA
170 | FATTR4_WORD1_TIME_MODIFY
171 | FATTR4_WORD1_MOUNTED_ON_FILEID
172};
173
Al Virobc4785c2006-10-19 23:28:51 -0700174static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 struct nfs4_readdir_arg *readdir)
176{
Al Viro0dbb4c62006-10-19 23:28:49 -0700177 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 BUG_ON(readdir->count < 80);
180 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000181 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
183 return;
184 }
185
186 readdir->cookie = 0;
187 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
188 if (cookie == 2)
189 return;
190
191 /*
192 * NFSv4 servers do not return entries for '.' and '..'
193 * Therefore, we fake these entries here. We let '.'
194 * have cookie 0 and '..' have cookie 1. Note that
195 * when talking to the server, we always send cookie 0
196 * instead of 1 or 2.
197 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700198 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 if (cookie == 0) {
201 *p++ = xdr_one; /* next */
202 *p++ = xdr_zero; /* cookie, first word */
203 *p++ = xdr_one; /* cookie, second word */
204 *p++ = xdr_one; /* entry len */
205 memcpy(p, ".\0\0\0", 4); /* entry */
206 p++;
207 *p++ = xdr_one; /* bitmap length */
208 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
209 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400210 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212
213 *p++ = xdr_one; /* next */
214 *p++ = xdr_zero; /* cookie, first word */
215 *p++ = xdr_two; /* cookie, second word */
216 *p++ = xdr_two; /* entry len */
217 memcpy(p, "..\0\0", 4); /* entry */
218 p++;
219 *p++ = xdr_one; /* bitmap length */
220 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
221 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400222 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 readdir->pgbase = (char *)p - (char *)start;
225 readdir->count -= readdir->pgbase;
226 kunmap_atomic(start, KM_USER0);
227}
228
Trond Myklebust65de8722008-12-23 15:21:44 -0500229static int nfs4_wait_clnt_recover(struct nfs_client *clp)
230{
231 int res;
232
233 might_sleep();
234
Trond Myklebuste005e802008-12-23 15:21:48 -0500235 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400236 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500237 return res;
238}
239
240static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
241{
242 int res = 0;
243
244 might_sleep();
245
246 if (*timeout <= 0)
247 *timeout = NFS4_POLL_RETRY_MIN;
248 if (*timeout > NFS4_POLL_RETRY_MAX)
249 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100250 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500251 if (fatal_signal_pending(current))
252 res = -ERESTARTSYS;
253 *timeout <<= 1;
254 return res;
255}
256
257/* This is the error handling routine for processes that are allowed
258 * to sleep.
259 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800260static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500261{
262 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500263 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500264 int ret = errorcode;
265
266 exception->retry = 0;
267 switch(errorcode) {
268 case 0:
269 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500270 case -NFS4ERR_ADMIN_REVOKED:
271 case -NFS4ERR_BAD_STATEID:
272 case -NFS4ERR_OPENMODE:
273 if (state == NULL)
274 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500275 nfs4_schedule_stateid_recovery(server, state);
276 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400277 case -NFS4ERR_EXPIRED:
278 if (state != NULL)
279 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500280 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500281 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500282 nfs4_schedule_lease_recovery(clp);
283 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500284#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400285 case -NFS4ERR_BADSESSION:
286 case -NFS4ERR_BADSLOT:
287 case -NFS4ERR_BAD_HIGH_SLOT:
288 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
289 case -NFS4ERR_DEADSESSION:
290 case -NFS4ERR_SEQ_FALSE_RETRY:
291 case -NFS4ERR_SEQ_MISORDERED:
292 dprintk("%s ERROR: %d Reset session\n", __func__,
293 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500294 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400295 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500296 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500297#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500298 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500299 if (exception->timeout > HZ) {
300 /* We have retried a decent amount, time to
301 * fail
302 */
303 ret = -EBUSY;
304 break;
305 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500306 case -NFS4ERR_GRACE:
307 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500308 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500309 ret = nfs4_delay(server->client, &exception->timeout);
310 if (ret != 0)
311 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400312 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500313 case -NFS4ERR_OLD_STATEID:
314 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800315 break;
316 case -NFS4ERR_BADOWNER:
317 /* The following works around a Linux server bug! */
318 case -NFS4ERR_BADNAME:
319 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
320 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
321 exception->retry = 1;
322 printk(KERN_WARNING "NFS: v4 server %s "
323 "does not accept raw "
324 "uid/gids. "
325 "Reenabling the idmapper.\n",
326 server->nfs_client->cl_hostname);
327 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500328 }
329 /* We failed to handle the error */
330 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500331wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500332 ret = nfs4_wait_clnt_recover(clp);
333 if (ret == 0)
334 exception->retry = 1;
335 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500336}
337
338
Trond Myklebust452e9352010-07-31 14:29:06 -0400339static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 spin_lock(&clp->cl_lock);
342 if (time_before(clp->cl_last_renewal,timestamp))
343 clp->cl_last_renewal = timestamp;
344 spin_unlock(&clp->cl_lock);
345}
346
Trond Myklebust452e9352010-07-31 14:29:06 -0400347static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
348{
349 do_renew_lease(server->nfs_client, timestamp);
350}
351
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400352#if defined(CONFIG_NFS_V4_1)
353
Benny Halevy510b8172009-04-01 09:22:14 -0400354/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400355 * nfs4_free_slot - free a slot and efficiently update slot table.
356 *
357 * freeing a slot is trivially done by clearing its respective bit
358 * in the bitmap.
359 * If the freed slotid equals highest_used_slotid we want to update it
360 * so that the server would be able to size down the slot table if needed,
361 * otherwise we know that the highest_used_slotid is still in use.
362 * When updating highest_used_slotid there may be "holes" in the bitmap
363 * so we need to scan down from highest_used_slotid to 0 looking for the now
364 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500365 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500366 *
367 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400368 */
369static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500370nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400371{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500372 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400373 /* clear used bit in bitmap */
374 __clear_bit(slotid, tbl->used_slots);
375
376 /* update highest_used_slotid when it is freed */
377 if (slotid == tbl->highest_used_slotid) {
378 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500379 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400380 tbl->highest_used_slotid = slotid;
381 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500382 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400383 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500384 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
385 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400386}
387
Trond Myklebust961a8282012-01-17 22:57:37 -0500388bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
389{
390 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
391 return true;
392}
393
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800394/*
Andy Adamson42acd022011-01-06 02:04:34 +0000395 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800396 */
Andy Adamson42acd022011-01-06 02:04:34 +0000397static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800398{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400399 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500400 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
401 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800402 return;
403 }
404
Trond Myklebust45d43c22012-02-06 19:38:51 -0500405 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800406 return;
407
Andy Adamson42acd022011-01-06 02:04:34 +0000408 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
409 complete(&ses->fc_slot_table.complete);
410}
411
412/*
413 * Signal state manager thread if session back channel is drained
414 */
415void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
416{
417 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500418 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000419 return;
420 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
421 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800422}
423
Trond Myklebustd185a332010-06-16 09:52:25 -0400424static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400425{
426 struct nfs4_slot_table *tbl;
427
Trond Myklebustd185a332010-06-16 09:52:25 -0400428 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400429 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400430 /* just wake up the next guy waiting since
431 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500432 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500433 return;
Andy Adamson13615872009-04-01 09:22:17 -0400434 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500435
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500436 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500437 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000438 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500439 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400440 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400441}
442
Trond Myklebust14516c32010-07-31 14:29:06 -0400443static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400444{
445 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400446 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400447
448 /*
449 * sr_status remains 1 if an RPC level error occurred. The server
450 * may or may not have processed the sequence operation..
451 * Proceed as if the server received and processed the sequence
452 * operation.
453 */
454 if (res->sr_status == 1)
455 res->sr_status = NFS_OK;
456
Bryan Schumaker468f8612011-04-18 15:57:32 -0400457 /* don't increment the sequence number if the task wasn't sent */
458 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400459 goto out;
460
Andy Adamson691daf32009-12-04 15:55:39 -0500461 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400462 switch (res->sr_status) {
463 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400464 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400465 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400466 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400467 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400468 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500469 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500470 if (res->sr_status_flags != 0)
471 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400472 break;
473 case -NFS4ERR_DELAY:
474 /* The server detected a resend of the RPC call and
475 * returned NFS4ERR_DELAY as per Section 2.10.6.2
476 * of RFC5661.
477 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200478 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400479 __func__,
480 res->sr_slot - res->sr_session->fc_slot_table.slots,
481 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400482 goto out_retry;
483 default:
484 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400485 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400486 }
487out:
488 /* The session may be reset by one of the error handlers. */
489 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400490 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400491 return 1;
492out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400493 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400494 goto out;
495 rpc_delay(task, NFS4_POLL_RETRY_MAX);
496 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400497}
498
Trond Myklebust14516c32010-07-31 14:29:06 -0400499static int nfs4_sequence_done(struct rpc_task *task,
500 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400501{
Trond Myklebust14516c32010-07-31 14:29:06 -0400502 if (res->sr_session == NULL)
503 return 1;
504 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400505}
506
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400507/*
Benny Halevy510b8172009-04-01 09:22:14 -0400508 * nfs4_find_slot - efficiently look for a free slot
509 *
510 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
511 * If found, we mark the slot as used, update the highest_used_slotid,
512 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500513 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400514 *
515 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400516 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500517static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800518nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400519{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500520 u32 slotid;
521 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400522
Trond Myklebust45d43c22012-02-06 19:38:51 -0500523 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400524 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
525 tbl->max_slots);
526 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
527 if (slotid >= tbl->max_slots)
528 goto out;
529 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500530 if (slotid > tbl->highest_used_slotid ||
531 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400532 tbl->highest_used_slotid = slotid;
533 ret_id = slotid;
534out:
535 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
536 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400537 return ret_id;
538}
539
Trond Myklebust9d12b212012-01-17 22:04:25 -0500540static void nfs41_init_sequence(struct nfs4_sequence_args *args,
541 struct nfs4_sequence_res *res, int cache_reply)
542{
543 args->sa_session = NULL;
544 args->sa_cache_this = 0;
545 if (cache_reply)
546 args->sa_cache_this = 1;
547 res->sr_session = NULL;
548 res->sr_slot = NULL;
549}
550
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000551int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400552 struct nfs4_sequence_args *args,
553 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400554 struct rpc_task *task)
555{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400556 struct nfs4_slot *slot;
557 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500558 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400559
560 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400561 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400562 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400563 return 0;
564
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400565 tbl = &session->fc_slot_table;
566
567 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400568 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800569 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500570 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500571 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400572 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500573 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500574 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400575 }
576
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800577 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
578 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
579 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
580 spin_unlock(&tbl->slot_tbl_lock);
581 dprintk("%s enforce FIFO order\n", __func__);
582 return -EAGAIN;
583 }
584
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800585 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500586 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400587 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
588 spin_unlock(&tbl->slot_tbl_lock);
589 dprintk("<-- %s: no free slots\n", __func__);
590 return -EAGAIN;
591 }
592 spin_unlock(&tbl->slot_tbl_lock);
593
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800594 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400595 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400596 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400597 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400598
599 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
600
Benny Halevy99fe60d2009-04-01 09:22:29 -0400601 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400602 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400603 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400604 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400605 /*
606 * sr_status is only set in decode_sequence, and so will remain
607 * set to 1 if an rpc level failure occurs.
608 */
609 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400610 return 0;
611}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000612EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400613
Trond Myklebust035168a2010-06-16 09:52:26 -0400614int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400615 struct nfs4_sequence_args *args,
616 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400617 struct rpc_task *task)
618{
Trond Myklebust035168a2010-06-16 09:52:26 -0400619 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400620 int ret = 0;
621
Trond Myklebust9d12b212012-01-17 22:04:25 -0500622 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400624
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200625 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400626 __func__, session->clp, session, res->sr_slot ?
627 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400628
Trond Myklebust9d12b212012-01-17 22:04:25 -0500629 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400630out:
631 dprintk("<-- %s status=%d\n", __func__, ret);
632 return ret;
633}
634
635struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400636 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400637 struct nfs4_sequence_args *seq_args;
638 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400639};
640
641static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
642{
643 struct nfs41_call_sync_data *data = calldata;
644
Trond Myklebust035168a2010-06-16 09:52:26 -0400645 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
646
647 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500648 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400649 return;
650 rpc_call_start(task);
651}
652
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800653static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
654{
655 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
656 nfs41_call_sync_prepare(task, calldata);
657}
658
Andy Adamson69ab40c2009-04-01 09:22:19 -0400659static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
660{
661 struct nfs41_call_sync_data *data = calldata;
662
Trond Myklebust14516c32010-07-31 14:29:06 -0400663 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400664}
665
Andy Adamsonce5039c2009-04-01 09:22:13 -0400666struct rpc_call_ops nfs41_call_sync_ops = {
667 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400668 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400669};
670
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800671struct rpc_call_ops nfs41_call_priv_sync_ops = {
672 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
673 .rpc_call_done = nfs41_call_sync_done,
674};
675
Bryan Schumaker7c513052011-03-24 17:12:24 +0000676static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
677 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400678 struct rpc_message *msg,
679 struct nfs4_sequence_args *args,
680 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800681 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400682{
683 int ret;
684 struct rpc_task *task;
685 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400686 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400687 .seq_args = args,
688 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400689 };
690 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000691 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400692 .rpc_message = msg,
693 .callback_ops = &nfs41_call_sync_ops,
694 .callback_data = &data
695 };
696
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800697 if (privileged)
698 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400699 task = rpc_run_task(&task_setup);
700 if (IS_ERR(task))
701 ret = PTR_ERR(task);
702 else {
703 ret = task->tk_status;
704 rpc_put_task(task);
705 }
706 return ret;
707}
708
Bryan Schumaker7c513052011-03-24 17:12:24 +0000709int _nfs4_call_sync_session(struct rpc_clnt *clnt,
710 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400711 struct rpc_message *msg,
712 struct nfs4_sequence_args *args,
713 struct nfs4_sequence_res *res,
714 int cache_reply)
715{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500716 nfs41_init_sequence(args, res, cache_reply);
717 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400718}
719
Trond Myklebustdf896452010-06-16 09:52:26 -0400720#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500721static inline
722void nfs41_init_sequence(struct nfs4_sequence_args *args,
723 struct nfs4_sequence_res *res, int cache_reply)
724{
725}
726
Trond Myklebust14516c32010-07-31 14:29:06 -0400727static int nfs4_sequence_done(struct rpc_task *task,
728 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400729{
Trond Myklebust14516c32010-07-31 14:29:06 -0400730 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400731}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400732#endif /* CONFIG_NFS_V4_1 */
733
Bryan Schumaker7c513052011-03-24 17:12:24 +0000734int _nfs4_call_sync(struct rpc_clnt *clnt,
735 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400736 struct rpc_message *msg,
737 struct nfs4_sequence_args *args,
738 struct nfs4_sequence_res *res,
739 int cache_reply)
740{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500741 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000742 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400743}
744
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000745static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000746int nfs4_call_sync(struct rpc_clnt *clnt,
747 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000748 struct rpc_message *msg,
749 struct nfs4_sequence_args *args,
750 struct nfs4_sequence_res *res,
751 int cache_reply)
752{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000753 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
754 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000755}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400756
Trond Myklebust38478b22006-05-25 01:40:57 -0400757static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Trond Myklebust38478b22006-05-25 01:40:57 -0400759 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Trond Myklebust38478b22006-05-25 01:40:57 -0400761 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400762 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700763 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400764 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700765 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400766 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400770 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100771 struct nfs_openargs o_arg;
772 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100773 struct nfs_open_confirmargs c_arg;
774 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500775 struct nfs4_string owner_name;
776 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100777 struct nfs_fattr f_attr;
778 struct nfs_fattr dir_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100779 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400780 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100781 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400782 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100783 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100784 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400785 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100786 int rpc_status;
787 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100788};
789
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400790
791static void nfs4_init_opendata_res(struct nfs4_opendata *p)
792{
793 p->o_res.f_attr = &p->f_attr;
794 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400795 p->o_res.seqid = p->o_arg.seqid;
796 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400797 p->o_res.server = p->o_arg.server;
798 nfs_fattr_init(&p->f_attr);
799 nfs_fattr_init(&p->dir_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500800 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400801}
802
Al Viro82a2c1b2011-06-22 18:30:55 -0400803static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500804 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400805 const struct iattr *attrs,
806 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100807{
Al Viro82a2c1b2011-06-22 18:30:55 -0400808 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100809 struct inode *dir = parent->d_inode;
810 struct nfs_server *server = NFS_SERVER(dir);
811 struct nfs4_opendata *p;
812
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400813 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100814 if (p == NULL)
815 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400816 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100817 if (p->o_arg.seqid == NULL)
818 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400819 nfs_sb_active(dentry->d_sb);
820 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100821 p->dir = parent;
822 p->owner = sp;
823 atomic_inc(&sp->so_count);
824 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500825 p->o_arg.open_flags = flags;
826 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400827 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -0500828 p->o_arg.id = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400829 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100830 p->o_arg.server = server;
831 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500832 p->o_arg.dir_bitmask = server->cache_consistency_bitmask;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100833 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500834 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400835 u32 *s;
836
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100837 p->o_arg.u.attrs = &p->attrs;
838 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400839 s = (u32 *) p->o_arg.u.verifier.data;
840 s[0] = jiffies;
841 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100842 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100843 p->c_arg.fh = &p->o_res.fh;
844 p->c_arg.stateid = &p->o_res.stateid;
845 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400846 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400847 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100848 return p;
849err_free:
850 kfree(p);
851err:
852 dput(parent);
853 return NULL;
854}
855
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400856static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100857{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400858 struct nfs4_opendata *p = container_of(kref,
859 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400860 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400861
862 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400863 if (p->state != NULL)
864 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400865 nfs4_put_state_owner(p->owner);
866 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400867 dput(p->dentry);
868 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500869 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400870 kfree(p);
871}
872
873static void nfs4_opendata_put(struct nfs4_opendata *p)
874{
875 if (p != NULL)
876 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100877}
878
Trond Myklebust06f814a2006-01-03 09:55:07 +0100879static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
880{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100881 int ret;
882
Trond Myklebust06f814a2006-01-03 09:55:07 +0100883 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100884 return ret;
885}
886
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500887static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400888{
889 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500890
Trond Myklebust536e43d2012-01-17 22:04:26 -0500891 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500892 goto out;
893 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400894 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500895 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
896 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400897 break;
898 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500899 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
900 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400901 break;
902 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500903 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
904 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400905 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500906out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400907 return ret;
908}
909
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500910static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400911{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500912 if (delegation == NULL)
913 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500914 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400915 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500916 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400917 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500918 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400919 return 1;
920}
921
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100923{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100925 case FMODE_WRITE:
926 state->n_wronly++;
927 break;
928 case FMODE_READ:
929 state->n_rdonly++;
930 break;
931 case FMODE_READ|FMODE_WRITE:
932 state->n_rdwr++;
933 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500934 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100935}
936
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400938{
939 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
940 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
941 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500942 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400943 case FMODE_READ:
944 set_bit(NFS_O_RDONLY_STATE, &state->flags);
945 break;
946 case FMODE_WRITE:
947 set_bit(NFS_O_WRONLY_STATE, &state->flags);
948 break;
949 case FMODE_READ|FMODE_WRITE:
950 set_bit(NFS_O_RDWR_STATE, &state->flags);
951 }
952}
953
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400955{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400956 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500957 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400958 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400959}
960
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961static 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 -0700962{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400963 /*
964 * Protect the call to nfs4_state_set_mode_locked and
965 * serialise the stateid update
966 */
967 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400968 if (deleg_stateid != NULL) {
969 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
970 set_bit(NFS_DELEGATED_STATE, &state->flags);
971 }
972 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400974 write_sequnlock(&state->seqlock);
975 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500976 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700977 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500980static 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 -0500981{
982 struct nfs_inode *nfsi = NFS_I(state->inode);
983 struct nfs_delegation *deleg_cur;
984 int ret = 0;
985
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500986 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500987
988 rcu_read_lock();
989 deleg_cur = rcu_dereference(nfsi->delegation);
990 if (deleg_cur == NULL)
991 goto no_delegation;
992
993 spin_lock(&deleg_cur->lock);
994 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500995 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500996 goto no_delegation_unlock;
997
998 if (delegation == NULL)
999 delegation = &deleg_cur->stateid;
1000 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
1001 goto no_delegation_unlock;
1002
Trond Myklebustb7391f42008-12-23 15:21:52 -05001003 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001004 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001005 ret = 1;
1006no_delegation_unlock:
1007 spin_unlock(&deleg_cur->lock);
1008no_delegation:
1009 rcu_read_unlock();
1010
1011 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001012 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001013 ret = 1;
1014 }
1015
1016 return ret;
1017}
1018
1019
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001020static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001021{
1022 struct nfs_delegation *delegation;
1023
1024 rcu_read_lock();
1025 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001026 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001027 rcu_read_unlock();
1028 return;
1029 }
1030 rcu_read_unlock();
1031 nfs_inode_return_delegation(inode);
1032}
1033
Trond Myklebust6ee41262007-07-08 14:11:36 -04001034static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001035{
1036 struct nfs4_state *state = opendata->state;
1037 struct nfs_inode *nfsi = NFS_I(state->inode);
1038 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001039 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001040 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001041 nfs4_stateid stateid;
1042 int ret = -EAGAIN;
1043
Trond Myklebustaac00a82007-07-05 19:02:21 -04001044 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001045 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001046 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001047 if (can_open_cached(state, fmode, open_mode)) {
1048 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001049 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001050 goto out_return_state;
1051 }
1052 spin_unlock(&state->owner->so_lock);
1053 }
Trond Myklebust34310432008-12-23 15:21:38 -05001054 rcu_read_lock();
1055 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001056 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001057 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001058 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001059 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001060 /* Save the delegation */
1061 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1062 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001063 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001064 if (ret != 0)
1065 goto out;
1066 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001067
1068 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001069 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001070 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001071 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001072out:
1073 return ERR_PTR(ret);
1074out_return_state:
1075 atomic_inc(&state->count);
1076 return state;
1077}
1078
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001079static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1080{
1081 struct inode *inode;
1082 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001083 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001084 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001085
Trond Myklebustaac00a82007-07-05 19:02:21 -04001086 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001087 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001088 goto out;
1089 }
1090
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001091 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001092 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001093 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001094 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001095 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001096 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001097 goto err;
1098 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001099 state = nfs4_get_open_state(inode, data->owner);
1100 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001101 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001102 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001103 int delegation_flags = 0;
1104
Trond Myklebust003707c2007-07-05 18:07:55 -04001105 rcu_read_lock();
1106 delegation = rcu_dereference(NFS_I(inode)->delegation);
1107 if (delegation)
1108 delegation_flags = delegation->flags;
1109 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001110 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1111 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1112 "returning a delegation for "
1113 "OPEN(CLAIM_DELEGATE_CUR)\n",
1114 NFS_CLIENT(inode)->cl_server);
1115 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001116 nfs_inode_set_delegation(state->inode,
1117 data->owner->so_cred,
1118 &data->o_res);
1119 else
1120 nfs_inode_reclaim_delegation(state->inode,
1121 data->owner->so_cred,
1122 &data->o_res);
1123 }
Trond Myklebust34310432008-12-23 15:21:38 -05001124
1125 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001126 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001127 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001128out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001129 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001130err_put_inode:
1131 iput(inode);
1132err:
1133 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001134}
1135
Trond Myklebust864472e2006-01-03 09:55:15 +01001136static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1137{
1138 struct nfs_inode *nfsi = NFS_I(state->inode);
1139 struct nfs_open_context *ctx;
1140
1141 spin_lock(&state->inode->i_lock);
1142 list_for_each_entry(ctx, &nfsi->open_files, list) {
1143 if (ctx->state != state)
1144 continue;
1145 get_nfs_open_context(ctx);
1146 spin_unlock(&state->inode->i_lock);
1147 return ctx;
1148 }
1149 spin_unlock(&state->inode->i_lock);
1150 return ERR_PTR(-ENOENT);
1151}
1152
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001153static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1154{
1155 struct nfs4_opendata *opendata;
1156
Al Viro3d4ff432011-06-22 18:40:12 -04001157 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001158 if (opendata == NULL)
1159 return ERR_PTR(-ENOMEM);
1160 opendata->state = state;
1161 atomic_inc(&state->count);
1162 return opendata;
1163}
1164
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001165static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001166{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001167 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001168 int ret;
1169
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001170 opendata->o_arg.open_flags = 0;
1171 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001172 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1173 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1174 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001175 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001176 if (ret != 0)
1177 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001178 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001179 if (IS_ERR(newstate))
1180 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001181 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001182 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001183 return 0;
1184}
1185
1186static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1187{
Trond Myklebust864472e2006-01-03 09:55:15 +01001188 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001189 int ret;
1190
1191 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001192 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001193 smp_rmb();
1194 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001195 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001196 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001197 if (ret != 0)
1198 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001199 if (newstate != state)
1200 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001201 }
1202 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001203 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001204 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001205 if (ret != 0)
1206 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001207 if (newstate != state)
1208 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001209 }
1210 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001211 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001212 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001213 if (ret != 0)
1214 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001215 if (newstate != state)
1216 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001217 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001218 /*
1219 * We may have performed cached opens for all three recoveries.
1220 * Check if we need to update the current stateid.
1221 */
1222 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1223 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001224 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001225 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1226 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001227 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001228 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001229 return 0;
1230}
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232/*
1233 * OPEN_RECLAIM:
1234 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001236static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001238 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001239 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001240 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 int status;
1242
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001243 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1244 if (IS_ERR(opendata))
1245 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001246 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1247 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001248 rcu_read_lock();
1249 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001250 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001251 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001252 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001253 opendata->o_arg.u.delegation_type = delegation_type;
1254 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001255 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return status;
1257}
1258
Trond Myklebust539cd032007-06-05 11:46:42 -04001259static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
1261 struct nfs_server *server = NFS_SERVER(state->inode);
1262 struct nfs4_exception exception = { };
1263 int err;
1264 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001265 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001266 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001267 break;
1268 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 } while (exception.retry);
1270 return err;
1271}
1272
Trond Myklebust864472e2006-01-03 09:55:15 +01001273static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1274{
1275 struct nfs_open_context *ctx;
1276 int ret;
1277
1278 ctx = nfs4_state_find_open_context(state);
1279 if (IS_ERR(ctx))
1280 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001281 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001282 put_nfs_open_context(ctx);
1283 return ret;
1284}
1285
Trond Myklebust13437e12007-07-06 15:10:43 -04001286static 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 -07001287{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001288 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001289 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001291 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1292 if (IS_ERR(opendata))
1293 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001294 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001295 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001296 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001297 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001298 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001299 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
Trond Myklebust13437e12007-07-06 15:10:43 -04001302int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001305 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 int err;
1307 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001308 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 switch (err) {
1310 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001311 case -ENOENT:
1312 case -ESTALE:
1313 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001314 case -NFS4ERR_BADSESSION:
1315 case -NFS4ERR_BADSLOT:
1316 case -NFS4ERR_BAD_HIGH_SLOT:
1317 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1318 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001319 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001320 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 case -NFS4ERR_STALE_CLIENTID:
1322 case -NFS4ERR_STALE_STATEID:
1323 case -NFS4ERR_EXPIRED:
1324 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001325 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001326 goto out;
1327 case -ERESTARTSYS:
1328 /*
1329 * The show must go on: exit, but mark the
1330 * stateid as needing recovery.
1331 */
1332 case -NFS4ERR_ADMIN_REVOKED:
1333 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001334 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001335 case -EKEYEXPIRED:
1336 /*
1337 * User RPCSEC_GSS context has expired.
1338 * We cannot recover this stateid now, so
1339 * skip it and allow recovery thread to
1340 * proceed.
1341 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001342 case -ENOMEM:
1343 err = 0;
1344 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346 err = nfs4_handle_exception(server, err, &exception);
1347 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001348out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return err;
1350}
1351
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001352static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1353{
1354 struct nfs4_opendata *data = calldata;
1355
1356 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001357 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001358 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1359 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001360 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001361 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001362 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001363 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001364}
1365
1366static void nfs4_open_confirm_release(void *calldata)
1367{
1368 struct nfs4_opendata *data = calldata;
1369 struct nfs4_state *state = NULL;
1370
1371 /* If this request hasn't been cancelled, do nothing */
1372 if (data->cancelled == 0)
1373 goto out_free;
1374 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001375 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001376 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001377 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001378 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001379 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001380out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001381 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001382}
1383
1384static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001385 .rpc_call_done = nfs4_open_confirm_done,
1386 .rpc_release = nfs4_open_confirm_release,
1387};
1388
1389/*
1390 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1391 */
1392static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1393{
1394 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1395 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001396 struct rpc_message msg = {
1397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1398 .rpc_argp = &data->c_arg,
1399 .rpc_resp = &data->c_res,
1400 .rpc_cred = data->owner->so_cred,
1401 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001402 struct rpc_task_setup task_setup_data = {
1403 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001404 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001405 .callback_ops = &nfs4_open_confirm_ops,
1406 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001407 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001408 .flags = RPC_TASK_ASYNC,
1409 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 int status;
1411
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001412 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001413 data->rpc_done = 0;
1414 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001415 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001416 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001417 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001418 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001419 status = nfs4_wait_for_completion_rpc_task(task);
1420 if (status != 0) {
1421 data->cancelled = 1;
1422 smp_wmb();
1423 } else
1424 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001425 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return status;
1427}
1428
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001429static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001431 struct nfs4_opendata *data = calldata;
1432 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001433
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001434 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1435 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001436 /*
1437 * Check if we still need to send an OPEN call, or if we can use
1438 * a delegation instead.
1439 */
1440 if (data->state != NULL) {
1441 struct nfs_delegation *delegation;
1442
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001443 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001444 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001445 rcu_read_lock();
1446 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001447 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1448 can_open_delegated(delegation, data->o_arg.fmode))
1449 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001450 rcu_read_unlock();
1451 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001452 /* Update sequence id. */
Trond Myklebust48c22eb2012-01-17 22:04:25 -05001453 data->o_arg.id = sp->so_seqid.owner_id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001454 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001455 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001456 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001457 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1458 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001459 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001460 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001461 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001462 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001463 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001464 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001465 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001466unlock_no_action:
1467 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001468out_no_action:
1469 task->tk_action = NULL;
1470
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001471}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001473static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1474{
1475 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1476 nfs4_open_prepare(task, calldata);
1477}
1478
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001479static void nfs4_open_done(struct rpc_task *task, void *calldata)
1480{
1481 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001483 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001484
Trond Myklebust14516c32010-07-31 14:29:06 -04001485 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1486 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001487
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001488 if (task->tk_status == 0) {
1489 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001490 case S_IFREG:
1491 break;
1492 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001493 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001494 break;
1495 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001496 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001497 break;
1498 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001499 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001500 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001501 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001502 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1503 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001504 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001505 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001506}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001507
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001508static void nfs4_open_release(void *calldata)
1509{
1510 struct nfs4_opendata *data = calldata;
1511 struct nfs4_state *state = NULL;
1512
1513 /* If this request hasn't been cancelled, do nothing */
1514 if (data->cancelled == 0)
1515 goto out_free;
1516 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001517 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001518 goto out_free;
1519 /* In case we need an open_confirm, no cleanup! */
1520 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1521 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001522 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001523 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001524 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001526 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001527}
1528
1529static const struct rpc_call_ops nfs4_open_ops = {
1530 .rpc_call_prepare = nfs4_open_prepare,
1531 .rpc_call_done = nfs4_open_done,
1532 .rpc_release = nfs4_open_release,
1533};
1534
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001535static const struct rpc_call_ops nfs4_recover_open_ops = {
1536 .rpc_call_prepare = nfs4_recover_open_prepare,
1537 .rpc_call_done = nfs4_open_done,
1538 .rpc_release = nfs4_open_release,
1539};
1540
1541static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001542{
1543 struct inode *dir = data->dir->d_inode;
1544 struct nfs_server *server = NFS_SERVER(dir);
1545 struct nfs_openargs *o_arg = &data->o_arg;
1546 struct nfs_openres *o_res = &data->o_res;
1547 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001548 struct rpc_message msg = {
1549 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1550 .rpc_argp = o_arg,
1551 .rpc_resp = o_res,
1552 .rpc_cred = data->owner->so_cred,
1553 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001554 struct rpc_task_setup task_setup_data = {
1555 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001556 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001557 .callback_ops = &nfs4_open_ops,
1558 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001559 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001560 .flags = RPC_TASK_ASYNC,
1561 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001562 int status;
1563
Trond Myklebust9d12b212012-01-17 22:04:25 -05001564 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001565 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001566 data->rpc_done = 0;
1567 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001568 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001569 if (isrecover)
1570 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001571 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001572 if (IS_ERR(task))
1573 return PTR_ERR(task);
1574 status = nfs4_wait_for_completion_rpc_task(task);
1575 if (status != 0) {
1576 data->cancelled = 1;
1577 smp_wmb();
1578 } else
1579 status = data->rpc_status;
1580 rpc_put_task(task);
1581
1582 return status;
1583}
1584
1585static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1586{
1587 struct inode *dir = data->dir->d_inode;
1588 struct nfs_openres *o_res = &data->o_res;
1589 int status;
1590
1591 status = nfs4_run_open_task(data, 1);
1592 if (status != 0 || !data->rpc_done)
1593 return status;
1594
Trond Myklebust6926afd2012-01-07 13:22:46 -05001595 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1596
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001597 nfs_refresh_inode(dir, o_res->dir_attr);
1598
1599 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1600 status = _nfs4_proc_open_confirm(data);
1601 if (status != 0)
1602 return status;
1603 }
1604
1605 return status;
1606}
1607
1608/*
1609 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1610 */
1611static int _nfs4_proc_open(struct nfs4_opendata *data)
1612{
1613 struct inode *dir = data->dir->d_inode;
1614 struct nfs_server *server = NFS_SERVER(dir);
1615 struct nfs_openargs *o_arg = &data->o_arg;
1616 struct nfs_openres *o_res = &data->o_res;
1617 int status;
1618
1619 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001620 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001621 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001622 if (status != 0) {
1623 if (status == -NFS4ERR_BADNAME &&
1624 !(o_arg->open_flags & O_CREAT))
1625 return -ENOENT;
1626 return status;
1627 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001628
Trond Myklebust6926afd2012-01-07 13:22:46 -05001629 nfs_fattr_map_and_free_names(server, &data->f_attr);
1630
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001631 if (o_arg->open_flags & O_CREAT) {
1632 update_changeattr(dir, &o_res->cinfo);
1633 nfs_post_op_update_inode(dir, o_res->dir_attr);
1634 } else
1635 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001636 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1637 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001639 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001641 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
1643 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001644 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001645 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646}
1647
Andy Adamsond83217c2011-03-01 01:34:17 +00001648static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001649{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001650 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001651 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001652
Trond Myklebusta78cb572009-08-09 15:06:19 -04001653 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001654 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001655 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001656 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001657 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1658 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001659 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001660 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001661 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001662 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001663 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001664}
1665
Andy Adamsond83217c2011-03-01 01:34:17 +00001666static int nfs4_recover_expired_lease(struct nfs_server *server)
1667{
1668 return nfs4_client_recover_expired_lease(server->nfs_client);
1669}
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671/*
1672 * OPEN_EXPIRED:
1673 * reclaim state on the server after a network partition.
1674 * Assumes caller holds the appropriate lock
1675 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001676static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001678 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001679 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001681 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1682 if (IS_ERR(opendata))
1683 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001684 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001685 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001686 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001687 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001688 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001691static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001692{
Trond Myklebust539cd032007-06-05 11:46:42 -04001693 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001694 struct nfs4_exception exception = { };
1695 int err;
1696
1697 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001698 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001699 switch (err) {
1700 default:
1701 goto out;
1702 case -NFS4ERR_GRACE:
1703 case -NFS4ERR_DELAY:
1704 nfs4_handle_exception(server, err, &exception);
1705 err = 0;
1706 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001707 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001708out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001709 return err;
1710}
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1713{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001715 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Trond Myklebust864472e2006-01-03 09:55:15 +01001717 ctx = nfs4_state_find_open_context(state);
1718 if (IS_ERR(ctx))
1719 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001720 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001721 put_nfs_open_context(ctx);
1722 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
1724
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001725#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001726static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001727{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001728 int status = NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001729 struct nfs_server *server = NFS_SERVER(state->inode);
1730
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001731 if (state->flags & flags) {
1732 status = nfs41_test_stateid(server, stateid);
1733 if (status != NFS_OK) {
1734 nfs41_free_stateid(server, stateid);
1735 state->flags &= ~flags;
1736 }
1737 }
1738 return status;
1739}
1740
1741static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1742{
1743 int deleg_status, open_status;
1744 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1745 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1746
1747 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1748 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1749
1750 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1751 return NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001752 return nfs4_open_expired(sp, state);
1753}
1754#endif
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001757 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1758 * fields corresponding to attributes that were used to store the verifier.
1759 * Make sure we clobber those fields in the later setattr call
1760 */
1761static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1762{
1763 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1764 !(sattr->ia_valid & ATTR_ATIME_SET))
1765 sattr->ia_valid |= ATTR_ATIME;
1766
1767 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1768 !(sattr->ia_valid & ATTR_MTIME_SET))
1769 sattr->ia_valid |= ATTR_MTIME;
1770}
1771
1772/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001773 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001775static 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 -07001776{
1777 struct nfs4_state_owner *sp;
1778 struct nfs4_state *state = NULL;
1779 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001780 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001781 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001785 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1786 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1788 goto out_err;
1789 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001790 status = nfs4_recover_expired_lease(server);
1791 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001792 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001793 if (dentry->d_inode != NULL)
1794 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001795 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001796 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001797 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001798 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Al Viro82a2c1b2011-06-22 18:30:55 -04001800 if (dentry->d_inode != NULL)
1801 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001802
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001803 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001805 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001807 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001808 status = PTR_ERR(state);
1809 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001810 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001811 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001812 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001813
1814 if (opendata->o_arg.open_flags & O_EXCL) {
1815 nfs4_exclusive_attrset(opendata, sattr);
1816
1817 nfs_fattr_init(opendata->o_res.f_attr);
1818 status = nfs4_do_setattr(state->inode, cred,
1819 opendata->o_res.f_attr, sattr,
1820 state);
1821 if (status == 0)
1822 nfs_setattr_update_inode(state->inode, sattr);
1823 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1824 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001825 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 *res = state;
1828 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001829err_opendata_put:
1830 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001831err_put_state_owner:
1832 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 *res = NULL;
1835 return status;
1836}
1837
1838
Al Viro82a2c1b2011-06-22 18:30:55 -04001839static 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 -07001840{
1841 struct nfs4_exception exception = { };
1842 struct nfs4_state *res;
1843 int status;
1844
1845 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001846 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (status == 0)
1848 break;
1849 /* NOTE: BAD_SEQID means the server and client disagree about the
1850 * book-keeping w.r.t. state-changing operations
1851 * (OPEN/CLOSE/LOCK/LOCKU...)
1852 * It is actually a sign of a bug on the client or on the server.
1853 *
1854 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001855 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 * have unhashed the old state_owner for us, and that we can
1857 * therefore safely retry using a new one. We should still warn
1858 * the user though...
1859 */
1860 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001861 printk(KERN_WARNING "NFS: v4 server %s "
1862 " returned a bad sequence-id error!\n",
1863 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 exception.retry = 1;
1865 continue;
1866 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001867 /*
1868 * BAD_STATEID on OPEN means that the server cancelled our
1869 * state before it received the OPEN_CONFIRM.
1870 * Recover by retrying the request as per the discussion
1871 * on Page 181 of RFC3530.
1872 */
1873 if (status == -NFS4ERR_BAD_STATEID) {
1874 exception.retry = 1;
1875 continue;
1876 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001877 if (status == -EAGAIN) {
1878 /* We must have found a delegation */
1879 exception.retry = 1;
1880 continue;
1881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1883 status, &exception));
1884 } while (exception.retry);
1885 return res;
1886}
1887
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001888static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1889 struct nfs_fattr *fattr, struct iattr *sattr,
1890 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001892 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001894 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 .iap = sattr,
1896 .server = server,
1897 .bitmask = server->attr_bitmask,
1898 };
1899 struct nfs_setattrres res = {
1900 .fattr = fattr,
1901 .server = server,
1902 };
1903 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1905 .rpc_argp = &arg,
1906 .rpc_resp = &res,
1907 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001909 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001910 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Trond Myklebust0e574af2005-10-27 22:12:38 -04001912 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001914 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1915 /* Use that stateid */
1916 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001917 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001918 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1920
Bryan Schumaker7c513052011-03-24 17:12:24 +00001921 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001922 if (status == 0 && state != NULL)
1923 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001924 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001927static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1928 struct nfs_fattr *fattr, struct iattr *sattr,
1929 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001931 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 struct nfs4_exception exception = { };
1933 int err;
1934 do {
1935 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001936 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 &exception);
1938 } while (exception.retry);
1939 return err;
1940}
1941
1942struct nfs4_closedata {
1943 struct inode *inode;
1944 struct nfs4_state *state;
1945 struct nfs_closeargs arg;
1946 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001947 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001948 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001949 bool roc;
1950 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951};
1952
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001953static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001954{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001955 struct nfs4_closedata *calldata = data;
1956 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001957 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001958
Fred Isamanf7e89172011-01-06 11:36:32 +00001959 if (calldata->roc)
1960 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001961 nfs4_put_open_state(calldata->state);
1962 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001963 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001964 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001965 kfree(calldata);
1966}
1967
Trond Myklebust88069f72009-12-08 08:33:16 -05001968static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1969 fmode_t fmode)
1970{
1971 spin_lock(&state->owner->so_lock);
1972 if (!(fmode & FMODE_READ))
1973 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1974 if (!(fmode & FMODE_WRITE))
1975 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1976 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1977 spin_unlock(&state->owner->so_lock);
1978}
1979
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001980static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001982 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 struct nfs_server *server = NFS_SERVER(calldata->inode);
1985
Chuck Levera3ca5652012-03-01 17:00:40 -05001986 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04001987 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1988 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 /* hmm. we are done with the inode, and in the process of freeing
1990 * the state_owner. we keep this around to process errors
1991 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 switch (task->tk_status) {
1993 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001994 if (calldata->roc)
1995 pnfs_roc_set_barrier(state->inode,
1996 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001997 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001998 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001999 nfs4_close_clear_stateid_flags(state,
2000 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 break;
2002 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002003 case -NFS4ERR_OLD_STATEID:
2004 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002006 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002007 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002009 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2010 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002012 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002013 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002014 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015}
2016
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002017static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002019 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002020 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002021 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002022
Chuck Levera3ca5652012-03-01 17:00:40 -05002023 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002024 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002025 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002026
Trond Myklebust88069f72009-12-08 08:33:16 -05002027 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2028 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002029 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002030 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002031 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002032 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002033 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2034 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2035 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002036 }
2037 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002038 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2039 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2040 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002041 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002042 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002043 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002044
2045 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002046 /* Note: exit _without_ calling nfs4_close_done */
2047 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002048 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002049 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002050
Fred Isamanf7e89172011-01-06 11:36:32 +00002051 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002052 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002053 if (calldata->roc &&
2054 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2055 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2056 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002057 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002058 }
2059 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002060
Trond Myklebust516a6af2005-10-27 22:12:41 -04002061 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002062 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002063 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002064 &calldata->arg.seq_args,
2065 &calldata->res.seq_res,
2066 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002067 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002068 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002069out:
2070 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
2072
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002073static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002074 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002075 .rpc_call_done = nfs4_close_done,
2076 .rpc_release = nfs4_free_closedata,
2077};
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079/*
2080 * It is possible for data to be read/written from a mem-mapped file
2081 * after the sys_close call (which hits the vfs layer as a flush).
2082 * This means that we can't safely call nfsv4 close on a file until
2083 * the inode is cleared. This in turn means that we are not good
2084 * NFSv4 citizens - we do not indicate to the server to update the file's
2085 * share state even when we are done with one of the three share
2086 * stateid's in the inode.
2087 *
2088 * NOTE: Caller must be holding the sp->so_owner semaphore!
2089 */
Al Viro643168c2011-06-22 18:20:23 -04002090int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002092 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002094 struct nfs4_state_owner *sp = state->owner;
2095 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002096 struct rpc_message msg = {
2097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2098 .rpc_cred = state->owner->so_cred,
2099 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002100 struct rpc_task_setup task_setup_data = {
2101 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002102 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002103 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002104 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002105 .flags = RPC_TASK_ASYNC,
2106 };
Trond Myklebust95121352005-10-18 14:20:12 -07002107 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002109 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002111 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002112 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002113 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002115 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002116 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002118 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002119 if (calldata->arg.seqid == NULL)
2120 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002121 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002122 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002123 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002124 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002125 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002126 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002127 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002128
Trond Myklebust1174dd12010-12-21 10:52:24 -05002129 msg.rpc_argp = &calldata->arg;
2130 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002131 task_setup_data.callback_data = calldata;
2132 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002133 if (IS_ERR(task))
2134 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002135 status = 0;
2136 if (wait)
2137 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002138 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002139 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002140out_free_calldata:
2141 kfree(calldata);
2142out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002143 if (roc)
2144 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002145 nfs4_put_open_state(state);
2146 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002147 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
2149
Trond Myklebust2b484292010-09-17 10:56:51 -04002150static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002151nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 struct nfs4_state *state;
2154
Trond Myklebust565277f2007-10-15 18:17:53 -04002155 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002156 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002157 if (IS_ERR(state))
2158 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002159 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002160 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
2162
Trond Myklebust1185a552009-12-03 15:54:02 -05002163static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002164{
2165 if (ctx->state == NULL)
2166 return;
2167 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002168 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002169 else
Al Viro643168c2011-06-22 18:20:23 -04002170 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002171}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2174{
Benny Halevy43652ad2009-04-01 09:21:54 -04002175 struct nfs4_server_caps_arg args = {
2176 .fhandle = fhandle,
2177 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 struct nfs4_server_caps_res res = {};
2179 struct rpc_message msg = {
2180 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002181 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 .rpc_resp = &res,
2183 };
2184 int status;
2185
Bryan Schumaker7c513052011-03-24 17:12:24 +00002186 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 if (status == 0) {
2188 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002189 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2190 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2191 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2192 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2193 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2195 server->caps |= NFS_CAP_ACLS;
2196 if (res.has_links != 0)
2197 server->caps |= NFS_CAP_HARDLINKS;
2198 if (res.has_symlinks != 0)
2199 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002200 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2201 server->caps |= NFS_CAP_FILEID;
2202 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2203 server->caps |= NFS_CAP_MODE;
2204 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2205 server->caps |= NFS_CAP_NLINK;
2206 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2207 server->caps |= NFS_CAP_OWNER;
2208 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2209 server->caps |= NFS_CAP_OWNER_GROUP;
2210 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2211 server->caps |= NFS_CAP_ATIME;
2212 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2213 server->caps |= NFS_CAP_CTIME;
2214 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2215 server->caps |= NFS_CAP_MTIME;
2216
Trond Myklebusta65318b2009-03-11 14:10:28 -04002217 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2218 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2219 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 server->acl_bitmask = res.acl_bitmask;
2221 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 return status;
2224}
2225
Trond Myklebust55a97592006-06-09 09:34:19 -04002226int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
2228 struct nfs4_exception exception = { };
2229 int err;
2230 do {
2231 err = nfs4_handle_exception(server,
2232 _nfs4_server_capabilities(server, fhandle),
2233 &exception);
2234 } while (exception.retry);
2235 return err;
2236}
2237
2238static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2239 struct nfs_fsinfo *info)
2240{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 struct nfs4_lookup_root_arg args = {
2242 .bitmask = nfs4_fattr_bitmap,
2243 };
2244 struct nfs4_lookup_res res = {
2245 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002246 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 .fh = fhandle,
2248 };
2249 struct rpc_message msg = {
2250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2251 .rpc_argp = &args,
2252 .rpc_resp = &res,
2253 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002254
Trond Myklebust0e574af2005-10-27 22:12:38 -04002255 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002256 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257}
2258
2259static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2260 struct nfs_fsinfo *info)
2261{
2262 struct nfs4_exception exception = { };
2263 int err;
2264 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002265 err = _nfs4_lookup_root(server, fhandle, info);
2266 switch (err) {
2267 case 0:
2268 case -NFS4ERR_WRONGSEC:
2269 break;
2270 default:
2271 err = nfs4_handle_exception(server, err, &exception);
2272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 } while (exception.retry);
2274 return err;
2275}
2276
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002277static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2278 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2279{
2280 struct rpc_auth *auth;
2281 int ret;
2282
2283 auth = rpcauth_create(flavor, server->client);
2284 if (!auth) {
2285 ret = -EIO;
2286 goto out;
2287 }
2288 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002289out:
2290 return ret;
2291}
2292
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002293static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002294 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002296 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002297 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002299 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2300 flav_array[len] = RPC_AUTH_NULL;
2301 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002302
2303 for (i = 0; i < len; i++) {
2304 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002305 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002306 continue;
2307 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002308 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002309 /*
2310 * -EACCESS could mean that the user doesn't have correct permissions
2311 * to access the mount. It could also mean that we tried to mount
2312 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2313 * existing mount programs don't handle -EACCES very well so it should
2314 * be mapped to -EPERM instead.
2315 */
2316 if (status == -EACCES)
2317 status = -EPERM;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002318 return status;
2319}
2320
2321/*
2322 * get the file handle for the "/" directory on the server
2323 */
2324static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2325 struct nfs_fsinfo *info)
2326{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002327 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002328 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002329 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2330 /*
2331 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2332 * by nfs4_map_errors() as this function exits.
2333 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002334 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 if (status == 0)
2336 status = nfs4_server_capabilities(server, fhandle);
2337 if (status == 0)
2338 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002339 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340}
2341
Andy Adamson533eb462011-06-13 18:25:56 -04002342static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002343/*
2344 * Get locations and (maybe) other attributes of a referral.
2345 * Note that we'll actually follow the referral later when
2346 * we detect fsid mismatch in inode revalidation
2347 */
Andy Adamson533eb462011-06-13 18:25:56 -04002348static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2349 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002350{
2351 int status = -ENOMEM;
2352 struct page *page = NULL;
2353 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002354
2355 page = alloc_page(GFP_KERNEL);
2356 if (page == NULL)
2357 goto out;
2358 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2359 if (locations == NULL)
2360 goto out;
2361
Trond Myklebustc228fd32007-01-13 02:28:11 -05002362 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002363 if (status != 0)
2364 goto out;
2365 /* Make sure server returned a different fsid for the referral */
2366 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002367 dprintk("%s: server did not return a different fsid for"
2368 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002369 status = -EIO;
2370 goto out;
2371 }
Andy Adamson533eb462011-06-13 18:25:56 -04002372 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2373 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002374
Andy Adamson533eb462011-06-13 18:25:56 -04002375 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002376 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002377 memset(fhandle, 0, sizeof(struct nfs_fh));
2378out:
2379 if (page)
2380 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002381 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002382 return status;
2383}
2384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2386{
2387 struct nfs4_getattr_arg args = {
2388 .fh = fhandle,
2389 .bitmask = server->attr_bitmask,
2390 };
2391 struct nfs4_getattr_res res = {
2392 .fattr = fattr,
2393 .server = server,
2394 };
2395 struct rpc_message msg = {
2396 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2397 .rpc_argp = &args,
2398 .rpc_resp = &res,
2399 };
2400
Trond Myklebust0e574af2005-10-27 22:12:38 -04002401 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002402 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403}
2404
2405static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2406{
2407 struct nfs4_exception exception = { };
2408 int err;
2409 do {
2410 err = nfs4_handle_exception(server,
2411 _nfs4_proc_getattr(server, fhandle, fattr),
2412 &exception);
2413 } while (exception.retry);
2414 return err;
2415}
2416
2417/*
2418 * The file is not closed if it is opened due to the a request to change
2419 * the size of the file. The open call will not be needed once the
2420 * VFS layer lookup-intents are implemented.
2421 *
2422 * Close is called when the inode is destroyed.
2423 * If we haven't opened the file for O_WRONLY, we
2424 * need to in the size_change case to obtain a stateid.
2425 *
2426 * Got race?
2427 * Because OPEN is always done by name in nfsv4, it is
2428 * possible that we opened a different file by the same
2429 * name. We can recognize this race condition, but we
2430 * can't do anything about it besides returning an error.
2431 *
2432 * This will be fixed with VFS changes (lookup-intent).
2433 */
2434static int
2435nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2436 struct iattr *sattr)
2437{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002438 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002439 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002440 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 int status;
2442
Benny Halevy8a1636c2010-07-14 15:43:57 -04002443 if (pnfs_ld_layoutret_on_setattr(inode))
2444 pnfs_return_layout(inode);
2445
Trond Myklebust0e574af2005-10-27 22:12:38 -04002446 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Trond Myklebustd5308382005-11-04 15:33:38 -05002448 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002449 if (sattr->ia_valid & ATTR_FILE) {
2450 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002451
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002452 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002453 if (ctx) {
2454 cred = ctx->cred;
2455 state = ctx->state;
2456 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002457 }
2458
Trond Myklebust536e43d2012-01-17 22:04:26 -05002459 /* Deal with open(O_TRUNC) */
2460 if (sattr->ia_valid & ATTR_OPEN)
2461 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2462
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002463 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002464 if (status == 0)
2465 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 return status;
2467}
2468
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002469static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2470 const struct qstr *name, struct nfs_fh *fhandle,
2471 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002472{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002473 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002474 int status;
2475 struct nfs4_lookup_arg args = {
2476 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002477 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002478 .name = name,
2479 };
2480 struct nfs4_lookup_res res = {
2481 .server = server,
2482 .fattr = fattr,
2483 .fh = fhandle,
2484 };
2485 struct rpc_message msg = {
2486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2487 .rpc_argp = &args,
2488 .rpc_resp = &res,
2489 };
2490
2491 nfs_fattr_init(fattr);
2492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002494 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 dprintk("NFS reply lookup: %d\n", status);
2496 return status;
2497}
2498
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002499void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2500{
2501 memset(fh, 0, sizeof(struct nfs_fh));
2502 fattr->fsid.major = 1;
2503 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2504 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2505 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2506 fattr->nlink = 2;
2507}
2508
Bryan Schumaker7c513052011-03-24 17:12:24 +00002509static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2510 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
2512 struct nfs4_exception exception = { };
2513 int err;
2514 do {
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002515 int status;
2516
2517 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2518 switch (status) {
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002519 case -NFS4ERR_BADNAME:
2520 return -ENOENT;
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002521 case -NFS4ERR_MOVED:
Trond Myklebusta6f498a2011-11-07 15:50:32 -05002522 return nfs4_get_referral(dir, name, fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002523 case -NFS4ERR_WRONGSEC:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002524 nfs_fixup_secinfo_attributes(fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002525 }
2526 err = nfs4_handle_exception(NFS_SERVER(dir),
2527 status, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 } while (exception.retry);
2529 return err;
2530}
2531
2532static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2533{
Trond Myklebust76b32992007-08-10 17:45:11 -04002534 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 struct nfs4_accessargs args = {
2536 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002537 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002539 struct nfs4_accessres res = {
2540 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002541 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 struct rpc_message msg = {
2543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2544 .rpc_argp = &args,
2545 .rpc_resp = &res,
2546 .rpc_cred = entry->cred,
2547 };
2548 int mode = entry->mask;
2549 int status;
2550
2551 /*
2552 * Determine which access bits we want to ask for...
2553 */
2554 if (mode & MAY_READ)
2555 args.access |= NFS4_ACCESS_READ;
2556 if (S_ISDIR(inode->i_mode)) {
2557 if (mode & MAY_WRITE)
2558 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2559 if (mode & MAY_EXEC)
2560 args.access |= NFS4_ACCESS_LOOKUP;
2561 } else {
2562 if (mode & MAY_WRITE)
2563 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2564 if (mode & MAY_EXEC)
2565 args.access |= NFS4_ACCESS_EXECUTE;
2566 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002567
2568 res.fattr = nfs_alloc_fattr();
2569 if (res.fattr == NULL)
2570 return -ENOMEM;
2571
Bryan Schumaker7c513052011-03-24 17:12:24 +00002572 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 if (!status) {
2574 entry->mask = 0;
2575 if (res.access & NFS4_ACCESS_READ)
2576 entry->mask |= MAY_READ;
2577 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2578 entry->mask |= MAY_WRITE;
2579 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2580 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002581 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002583 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 return status;
2585}
2586
2587static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2588{
2589 struct nfs4_exception exception = { };
2590 int err;
2591 do {
2592 err = nfs4_handle_exception(NFS_SERVER(inode),
2593 _nfs4_proc_access(inode, entry),
2594 &exception);
2595 } while (exception.retry);
2596 return err;
2597}
2598
2599/*
2600 * TODO: For the time being, we don't try to get any attributes
2601 * along with any of the zero-copy operations READ, READDIR,
2602 * READLINK, WRITE.
2603 *
2604 * In the case of the first three, we want to put the GETATTR
2605 * after the read-type operation -- this is because it is hard
2606 * to predict the length of a GETATTR response in v4, and thus
2607 * align the READ data correctly. This means that the GETATTR
2608 * may end up partially falling into the page cache, and we should
2609 * shift it into the 'tail' of the xdr_buf before processing.
2610 * To do this efficiently, we need to know the total length
2611 * of data received, which doesn't seem to be available outside
2612 * of the RPC layer.
2613 *
2614 * In the case of WRITE, we also want to put the GETATTR after
2615 * the operation -- in this case because we want to make sure
2616 * we get the post-operation mtime and size. This means that
2617 * we can't use xdr_encode_pages() as written: we need a variant
2618 * of it which would leave room in the 'tail' iovec.
2619 *
2620 * Both of these changes to the XDR layer would in fact be quite
2621 * minor, but I decided to leave them for a subsequent patch.
2622 */
2623static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2624 unsigned int pgbase, unsigned int pglen)
2625{
2626 struct nfs4_readlink args = {
2627 .fh = NFS_FH(inode),
2628 .pgbase = pgbase,
2629 .pglen = pglen,
2630 .pages = &page,
2631 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002632 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 struct rpc_message msg = {
2634 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2635 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002636 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 };
2638
Bryan Schumaker7c513052011-03-24 17:12:24 +00002639 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 -07002640}
2641
2642static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2643 unsigned int pgbase, unsigned int pglen)
2644{
2645 struct nfs4_exception exception = { };
2646 int err;
2647 do {
2648 err = nfs4_handle_exception(NFS_SERVER(inode),
2649 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2650 &exception);
2651 } while (exception.retry);
2652 return err;
2653}
2654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655/*
2656 * Got race?
2657 * We will need to arrange for the VFS layer to provide an atomic open.
2658 * Until then, this create/open method is prone to inefficiency and race
2659 * conditions due to the lookup, create, and open VFS calls from sys_open()
2660 * placed on the wire.
2661 *
2662 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2663 * The file will be opened again in the subsequent VFS open call
2664 * (nfs4_proc_file_open).
2665 *
2666 * The open for read will just hang around to be used by any process that
2667 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2668 */
2669
2670static int
2671nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002672 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673{
Al Viro82a2c1b2011-06-22 18:30:55 -04002674 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002676 struct rpc_cred *cred = NULL;
2677 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 int status = 0;
2679
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002680 if (ctx != NULL) {
2681 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002682 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002683 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002685 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002686 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002687 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 if (IS_ERR(state)) {
2689 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002690 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002692 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002693 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002694 if (ctx != NULL)
2695 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002696 else
Al Viro643168c2011-06-22 18:20:23 -04002697 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698out:
2699 return status;
2700}
2701
2702static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2703{
Trond Myklebust16e42952005-10-27 22:12:44 -04002704 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002705 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002707 .name.len = name->len,
2708 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002709 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002711 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002712 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002713 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002715 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2716 .rpc_argp = &args,
2717 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002719 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
Trond Myklebustd3468902010-04-16 16:22:50 -04002721 res.dir_attr = nfs_alloc_fattr();
2722 if (res.dir_attr == NULL)
2723 goto out;
2724
Bryan Schumaker7c513052011-03-24 17:12:24 +00002725 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002726 if (status == 0) {
2727 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002728 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002729 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002730 nfs_free_fattr(res.dir_attr);
2731out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 return status;
2733}
2734
2735static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2736{
2737 struct nfs4_exception exception = { };
2738 int err;
2739 do {
2740 err = nfs4_handle_exception(NFS_SERVER(dir),
2741 _nfs4_proc_remove(dir, name),
2742 &exception);
2743 } while (exception.retry);
2744 return err;
2745}
2746
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002747static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002749 struct nfs_server *server = NFS_SERVER(dir);
2750 struct nfs_removeargs *args = msg->rpc_argp;
2751 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Trond Myklebusta65318b2009-03-11 14:10:28 -04002753 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002754 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002756 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757}
2758
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002759static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002761 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2762
Trond Myklebust14516c32010-07-31 14:29:06 -04002763 if (!nfs4_sequence_done(task, &res->seq_res))
2764 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002765 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002766 return 0;
2767 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002768 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002769 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770}
2771
Jeff Laytond3d41522010-09-17 17:31:57 -04002772static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2773{
2774 struct nfs_server *server = NFS_SERVER(dir);
2775 struct nfs_renameargs *arg = msg->rpc_argp;
2776 struct nfs_renameres *res = msg->rpc_resp;
2777
2778 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2779 arg->bitmask = server->attr_bitmask;
2780 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002781 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002782}
2783
2784static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2785 struct inode *new_dir)
2786{
2787 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2788
2789 if (!nfs4_sequence_done(task, &res->seq_res))
2790 return 0;
2791 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2792 return 0;
2793
2794 update_changeattr(old_dir, &res->old_cinfo);
2795 nfs_post_op_update_inode(old_dir, res->old_fattr);
2796 update_changeattr(new_dir, &res->new_cinfo);
2797 nfs_post_op_update_inode(new_dir, res->new_fattr);
2798 return 1;
2799}
2800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2802 struct inode *new_dir, struct qstr *new_name)
2803{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002804 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002805 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 .old_dir = NFS_FH(old_dir),
2807 .new_dir = NFS_FH(new_dir),
2808 .old_name = old_name,
2809 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002810 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002812 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002813 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002814 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 struct rpc_message msg = {
2816 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2817 .rpc_argp = &arg,
2818 .rpc_resp = &res,
2819 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002820 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Trond Myklebust011fff72010-04-16 16:22:49 -04002822 res.old_fattr = nfs_alloc_fattr();
2823 res.new_fattr = nfs_alloc_fattr();
2824 if (res.old_fattr == NULL || res.new_fattr == NULL)
2825 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
Bryan Schumaker7c513052011-03-24 17:12:24 +00002827 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 if (!status) {
2829 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002830 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002832 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002834out:
2835 nfs_free_fattr(res.new_fattr);
2836 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 return status;
2838}
2839
2840static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2841 struct inode *new_dir, struct qstr *new_name)
2842{
2843 struct nfs4_exception exception = { };
2844 int err;
2845 do {
2846 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2847 _nfs4_proc_rename(old_dir, old_name,
2848 new_dir, new_name),
2849 &exception);
2850 } while (exception.retry);
2851 return err;
2852}
2853
2854static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2855{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002856 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 struct nfs4_link_arg arg = {
2858 .fh = NFS_FH(inode),
2859 .dir_fh = NFS_FH(dir),
2860 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002861 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002863 struct nfs4_link_res res = {
2864 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002865 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 struct rpc_message msg = {
2867 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2868 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002869 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002871 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Trond Myklebust136f2622010-04-16 16:22:49 -04002873 res.fattr = nfs_alloc_fattr();
2874 res.dir_attr = nfs_alloc_fattr();
2875 if (res.fattr == NULL || res.dir_attr == NULL)
2876 goto out;
2877
Bryan Schumaker7c513052011-03-24 17:12:24 +00002878 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002879 if (!status) {
2880 update_changeattr(dir, &res.cinfo);
2881 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002882 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002883 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002884out:
2885 nfs_free_fattr(res.dir_attr);
2886 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 return status;
2888}
2889
2890static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2891{
2892 struct nfs4_exception exception = { };
2893 int err;
2894 do {
2895 err = nfs4_handle_exception(NFS_SERVER(inode),
2896 _nfs4_proc_link(inode, dir, name),
2897 &exception);
2898 } while (exception.retry);
2899 return err;
2900}
2901
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002902struct nfs4_createdata {
2903 struct rpc_message msg;
2904 struct nfs4_create_arg arg;
2905 struct nfs4_create_res res;
2906 struct nfs_fh fh;
2907 struct nfs_fattr fattr;
2908 struct nfs_fattr dir_fattr;
2909};
2910
2911static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2912 struct qstr *name, struct iattr *sattr, u32 ftype)
2913{
2914 struct nfs4_createdata *data;
2915
2916 data = kzalloc(sizeof(*data), GFP_KERNEL);
2917 if (data != NULL) {
2918 struct nfs_server *server = NFS_SERVER(dir);
2919
2920 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2921 data->msg.rpc_argp = &data->arg;
2922 data->msg.rpc_resp = &data->res;
2923 data->arg.dir_fh = NFS_FH(dir);
2924 data->arg.server = server;
2925 data->arg.name = name;
2926 data->arg.attrs = sattr;
2927 data->arg.ftype = ftype;
2928 data->arg.bitmask = server->attr_bitmask;
2929 data->res.server = server;
2930 data->res.fh = &data->fh;
2931 data->res.fattr = &data->fattr;
2932 data->res.dir_fattr = &data->dir_fattr;
2933 nfs_fattr_init(data->res.fattr);
2934 nfs_fattr_init(data->res.dir_fattr);
2935 }
2936 return data;
2937}
2938
2939static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2940{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002941 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002942 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002943 if (status == 0) {
2944 update_changeattr(dir, &data->res.dir_cinfo);
2945 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2946 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2947 }
2948 return status;
2949}
2950
2951static void nfs4_free_createdata(struct nfs4_createdata *data)
2952{
2953 kfree(data);
2954}
2955
Chuck Lever4f390c12006-08-22 20:06:22 -04002956static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002957 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002959 struct nfs4_createdata *data;
2960 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Chuck Lever94a6d752006-08-22 20:06:23 -04002962 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002963 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002964
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002965 status = -ENOMEM;
2966 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2967 if (data == NULL)
2968 goto out;
2969
2970 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2971 data->arg.u.symlink.pages = &page;
2972 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002974 status = nfs4_do_create(dir, dentry, data);
2975
2976 nfs4_free_createdata(data);
2977out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 return status;
2979}
2980
Chuck Lever4f390c12006-08-22 20:06:22 -04002981static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002982 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983{
2984 struct nfs4_exception exception = { };
2985 int err;
2986 do {
2987 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002988 _nfs4_proc_symlink(dir, dentry, page,
2989 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 &exception);
2991 } while (exception.retry);
2992 return err;
2993}
2994
2995static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2996 struct iattr *sattr)
2997{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002998 struct nfs4_createdata *data;
2999 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003001 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3002 if (data == NULL)
3003 goto out;
3004
3005 status = nfs4_do_create(dir, dentry, data);
3006
3007 nfs4_free_createdata(data);
3008out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 return status;
3010}
3011
3012static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3013 struct iattr *sattr)
3014{
3015 struct nfs4_exception exception = { };
3016 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003017
3018 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 do {
3020 err = nfs4_handle_exception(NFS_SERVER(dir),
3021 _nfs4_proc_mkdir(dir, dentry, sattr),
3022 &exception);
3023 } while (exception.retry);
3024 return err;
3025}
3026
3027static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003028 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
3030 struct inode *dir = dentry->d_inode;
3031 struct nfs4_readdir_arg args = {
3032 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003033 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 .pgbase = 0,
3035 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003036 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003037 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 };
3039 struct nfs4_readdir_res res;
3040 struct rpc_message msg = {
3041 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3042 .rpc_argp = &args,
3043 .rpc_resp = &res,
3044 .rpc_cred = cred,
3045 };
3046 int status;
3047
Harvey Harrison3110ff82008-05-02 13:42:44 -07003048 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003049 dentry->d_parent->d_name.name,
3050 dentry->d_name.name,
3051 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3053 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003054 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 -05003055 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003057 status += args.pgbase;
3058 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003059
3060 nfs_invalidate_atime(dir);
3061
Harvey Harrison3110ff82008-05-02 13:42:44 -07003062 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 return status;
3064}
3065
3066static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003067 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
3069 struct nfs4_exception exception = { };
3070 int err;
3071 do {
3072 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3073 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003074 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 &exception);
3076 } while (exception.retry);
3077 return err;
3078}
3079
3080static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3081 struct iattr *sattr, dev_t rdev)
3082{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003083 struct nfs4_createdata *data;
3084 int mode = sattr->ia_mode;
3085 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
3087 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3088 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003089
3090 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3091 if (data == NULL)
3092 goto out;
3093
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003095 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003097 data->arg.ftype = NF4BLK;
3098 data->arg.u.device.specdata1 = MAJOR(rdev);
3099 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 }
3101 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003102 data->arg.ftype = NF4CHR;
3103 data->arg.u.device.specdata1 = MAJOR(rdev);
3104 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003107 status = nfs4_do_create(dir, dentry, data);
3108
3109 nfs4_free_createdata(data);
3110out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 return status;
3112}
3113
3114static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3115 struct iattr *sattr, dev_t rdev)
3116{
3117 struct nfs4_exception exception = { };
3118 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003119
3120 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 do {
3122 err = nfs4_handle_exception(NFS_SERVER(dir),
3123 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3124 &exception);
3125 } while (exception.retry);
3126 return err;
3127}
3128
3129static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3130 struct nfs_fsstat *fsstat)
3131{
3132 struct nfs4_statfs_arg args = {
3133 .fh = fhandle,
3134 .bitmask = server->attr_bitmask,
3135 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003136 struct nfs4_statfs_res res = {
3137 .fsstat = fsstat,
3138 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 struct rpc_message msg = {
3140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3141 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003142 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 };
3144
Trond Myklebust0e574af2005-10-27 22:12:38 -04003145 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003146 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147}
3148
3149static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3150{
3151 struct nfs4_exception exception = { };
3152 int err;
3153 do {
3154 err = nfs4_handle_exception(server,
3155 _nfs4_proc_statfs(server, fhandle, fsstat),
3156 &exception);
3157 } while (exception.retry);
3158 return err;
3159}
3160
3161static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3162 struct nfs_fsinfo *fsinfo)
3163{
3164 struct nfs4_fsinfo_arg args = {
3165 .fh = fhandle,
3166 .bitmask = server->attr_bitmask,
3167 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003168 struct nfs4_fsinfo_res res = {
3169 .fsinfo = fsinfo,
3170 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 struct rpc_message msg = {
3172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3173 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003174 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 };
3176
Bryan Schumaker7c513052011-03-24 17:12:24 +00003177 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178}
3179
3180static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3181{
3182 struct nfs4_exception exception = { };
3183 int err;
3184
3185 do {
3186 err = nfs4_handle_exception(server,
3187 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3188 &exception);
3189 } while (exception.retry);
3190 return err;
3191}
3192
3193static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3194{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003195 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3197}
3198
3199static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3200 struct nfs_pathconf *pathconf)
3201{
3202 struct nfs4_pathconf_arg args = {
3203 .fh = fhandle,
3204 .bitmask = server->attr_bitmask,
3205 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003206 struct nfs4_pathconf_res res = {
3207 .pathconf = pathconf,
3208 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 struct rpc_message msg = {
3210 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3211 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003212 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 };
3214
3215 /* None of the pathconf attributes are mandatory to implement */
3216 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3217 memset(pathconf, 0, sizeof(*pathconf));
3218 return 0;
3219 }
3220
Trond Myklebust0e574af2005-10-27 22:12:38 -04003221 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003222 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223}
3224
3225static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3226 struct nfs_pathconf *pathconf)
3227{
3228 struct nfs4_exception exception = { };
3229 int err;
3230
3231 do {
3232 err = nfs4_handle_exception(server,
3233 _nfs4_proc_pathconf(server, fhandle, pathconf),
3234 &exception);
3235 } while (exception.retry);
3236 return err;
3237}
3238
Benny Halevyd20581a2011-05-22 19:52:03 +03003239void __nfs4_read_done_cb(struct nfs_read_data *data)
3240{
3241 nfs_invalidate_atime(data->inode);
3242}
3243
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003244static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245{
Trond Myklebustec06c092006-03-20 13:44:27 -05003246 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003248 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003249 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003250 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003252
Benny Halevyd20581a2011-05-22 19:52:03 +03003253 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003255 renew_lease(server, data->timestamp);
3256 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257}
3258
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003259static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3260{
3261
3262 dprintk("--> %s\n", __func__);
3263
3264 if (!nfs4_sequence_done(task, &data->res.seq_res))
3265 return -EAGAIN;
3266
Benny Halevyd20581a2011-05-22 19:52:03 +03003267 return data->read_done_cb ? data->read_done_cb(task, data) :
3268 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003269}
3270
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003271static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003274 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003275 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003276 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277}
3278
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003279/* Reset the the nfs_read_data to send the read to the MDS. */
3280void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3281{
3282 dprintk("%s Reset task for i/o through\n", __func__);
3283 put_lseg(data->lseg);
3284 data->lseg = NULL;
3285 /* offsets will differ in the dense stripe case */
3286 data->args.offset = data->mds_offset;
3287 data->ds_clp = NULL;
3288 data->args.fh = NFS_FH(data->inode);
3289 data->read_done_cb = nfs4_read_done_cb;
3290 task->tk_ops = data->mds_ops;
3291 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3292}
3293EXPORT_SYMBOL_GPL(nfs4_reset_read);
3294
Fred Isamanb029bc92011-03-03 15:13:42 +00003295static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 struct inode *inode = data->inode;
3298
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003299 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003300 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003301 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003303 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003305 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003306 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308}
3309
Fred Isamanb029bc92011-03-03 15:13:42 +00003310static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3311{
3312 if (!nfs4_sequence_done(task, &data->res.seq_res))
3313 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003314 return data->write_done_cb ? data->write_done_cb(task, data) :
3315 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003316}
3317
Fred Isamana69aef12011-03-03 15:13:47 +00003318/* Reset the the nfs_write_data to send the write to the MDS. */
3319void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3320{
3321 dprintk("%s Reset task for i/o through\n", __func__);
3322 put_lseg(data->lseg);
3323 data->lseg = NULL;
3324 data->ds_clp = NULL;
3325 data->write_done_cb = nfs4_write_done_cb;
3326 data->args.fh = NFS_FH(data->inode);
3327 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3328 data->args.offset = data->mds_offset;
3329 data->res.fattr = &data->fattr;
3330 task->tk_ops = data->mds_ops;
3331 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3332}
3333EXPORT_SYMBOL_GPL(nfs4_reset_write);
3334
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003335static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003337 struct nfs_server *server = NFS_SERVER(data->inode);
3338
Fred Isaman7ffd1062011-03-03 15:13:46 +00003339 if (data->lseg) {
3340 data->args.bitmask = NULL;
3341 data->res.fattr = NULL;
3342 } else
3343 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003344 if (!data->write_done_cb)
3345 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003346 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 data->timestamp = jiffies;
3348
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003349 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003350 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351}
3352
Fred Isaman5f452432011-03-23 13:27:46 +00003353static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003356
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003357 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003358 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003359 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003361 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363}
3364
Fred Isaman5f452432011-03-23 13:27:46 +00003365static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3366{
3367 if (!nfs4_sequence_done(task, &data->res.seq_res))
3368 return -EAGAIN;
3369 return data->write_done_cb(task, data);
3370}
3371
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003372static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003374 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003375
3376 if (data->lseg) {
3377 data->args.bitmask = NULL;
3378 data->res.fattr = NULL;
3379 } else
3380 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003381 if (!data->write_done_cb)
3382 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003383 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003384 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003385 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386}
3387
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003388struct nfs4_renewdata {
3389 struct nfs_client *client;
3390 unsigned long timestamp;
3391};
3392
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393/*
3394 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3395 * standalone procedure for queueing an asynchronous RENEW.
3396 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003397static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003398{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003399 struct nfs4_renewdata *data = calldata;
3400 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003401
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003402 if (atomic_read(&clp->cl_count) > 1)
3403 nfs4_schedule_state_renewal(clp);
3404 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003405 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003406}
3407
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003408static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003410 struct nfs4_renewdata *data = calldata;
3411 struct nfs_client *clp = data->client;
3412 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
3414 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003415 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003416 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3417 return;
3418 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003419 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003420 return;
3421 }
3422 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003424 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425}
3426
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003427static const struct rpc_call_ops nfs4_renew_ops = {
3428 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003429 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003430};
3431
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003432static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433{
3434 struct rpc_message msg = {
3435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3436 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003437 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003439 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003441 if (renew_flags == 0)
3442 return 0;
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003443 if (!atomic_inc_not_zero(&clp->cl_count))
3444 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003445 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003446 if (data == NULL)
3447 return -ENOMEM;
3448 data->client = clp;
3449 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003451 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452}
3453
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003454static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455{
3456 struct rpc_message msg = {
3457 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3458 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003459 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 };
3461 unsigned long now = jiffies;
3462 int status;
3463
3464 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3465 if (status < 0)
3466 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003467 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 return 0;
3469}
3470
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003471static inline int nfs4_server_supports_acls(struct nfs_server *server)
3472{
3473 return (server->caps & NFS_CAP_ACLS)
3474 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3475 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3476}
3477
3478/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3479 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3480 * the stack.
3481 */
3482#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3483
Neil Hormane9e3d722011-03-04 19:26:03 -05003484static int buf_to_pages_noslab(const void *buf, size_t buflen,
3485 struct page **pages, unsigned int *pgbase)
3486{
3487 struct page *newpage, **spages;
3488 int rc = 0;
3489 size_t len;
3490 spages = pages;
3491
3492 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003493 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003494 newpage = alloc_page(GFP_KERNEL);
3495
3496 if (newpage == NULL)
3497 goto unwind;
3498 memcpy(page_address(newpage), buf, len);
3499 buf += len;
3500 buflen -= len;
3501 *pages++ = newpage;
3502 rc++;
3503 } while (buflen != 0);
3504
3505 return rc;
3506
3507unwind:
3508 for(; rc > 0; rc--)
3509 __free_page(spages[rc-1]);
3510 return -ENOMEM;
3511}
3512
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003513struct nfs4_cached_acl {
3514 int cached;
3515 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003516 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003517};
3518
3519static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003520{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003521 struct nfs_inode *nfsi = NFS_I(inode);
3522
3523 spin_lock(&inode->i_lock);
3524 kfree(nfsi->nfs4_acl);
3525 nfsi->nfs4_acl = acl;
3526 spin_unlock(&inode->i_lock);
3527}
3528
3529static void nfs4_zap_acl_attr(struct inode *inode)
3530{
3531 nfs4_set_cached_acl(inode, NULL);
3532}
3533
3534static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3535{
3536 struct nfs_inode *nfsi = NFS_I(inode);
3537 struct nfs4_cached_acl *acl;
3538 int ret = -ENOENT;
3539
3540 spin_lock(&inode->i_lock);
3541 acl = nfsi->nfs4_acl;
3542 if (acl == NULL)
3543 goto out;
3544 if (buf == NULL) /* user is just asking for length */
3545 goto out_len;
3546 if (acl->cached == 0)
3547 goto out;
3548 ret = -ERANGE; /* see getxattr(2) man page */
3549 if (acl->len > buflen)
3550 goto out;
3551 memcpy(buf, acl->data, acl->len);
3552out_len:
3553 ret = acl->len;
3554out:
3555 spin_unlock(&inode->i_lock);
3556 return ret;
3557}
3558
3559static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3560{
3561 struct nfs4_cached_acl *acl;
3562
3563 if (buf && acl_len <= PAGE_SIZE) {
3564 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3565 if (acl == NULL)
3566 goto out;
3567 acl->cached = 1;
3568 memcpy(acl->data, buf, acl_len);
3569 } else {
3570 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3571 if (acl == NULL)
3572 goto out;
3573 acl->cached = 0;
3574 }
3575 acl->len = acl_len;
3576out:
3577 nfs4_set_cached_acl(inode, acl);
3578}
3579
Andy Adamsonbf118a32011-12-07 11:55:27 -05003580/*
3581 * The getxattr API returns the required buffer length when called with a
3582 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3583 * the required buf. On a NULL buf, we send a page of data to the server
3584 * guessing that the ACL request can be serviced by a page. If so, we cache
3585 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3586 * the cache. If not so, we throw away the page, and cache the required
3587 * length. The next getxattr call will then produce another round trip to
3588 * the server, this time with the input buf of the required size.
3589 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003590static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003591{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003592 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003593 struct nfs_getaclargs args = {
3594 .fh = NFS_FH(inode),
3595 .acl_pages = pages,
3596 .acl_len = buflen,
3597 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003598 struct nfs_getaclres res = {
3599 .acl_len = buflen,
3600 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003601 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003602 struct rpc_message msg = {
3603 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3604 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003605 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003606 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003607 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003608
Andy Adamsonbf118a32011-12-07 11:55:27 -05003609 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3610 /* As long as we're doing a round trip to the server anyway,
3611 * let's be prepared for a page of acl data. */
3612 if (npages == 0)
3613 npages = 1;
3614
3615 for (i = 0; i < npages; i++) {
3616 pages[i] = alloc_page(GFP_KERNEL);
3617 if (!pages[i])
3618 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003619 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003620 if (npages > 1) {
3621 /* for decoding across pages */
3622 args.acl_scratch = alloc_page(GFP_KERNEL);
3623 if (!args.acl_scratch)
3624 goto out_free;
3625 }
3626 args.acl_len = npages * PAGE_SIZE;
3627 args.acl_pgbase = 0;
3628 /* Let decode_getfacl know not to fail if the ACL data is larger than
3629 * the page we send as a guess */
3630 if (buf == NULL)
3631 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3632 resp_buf = page_address(pages[0]);
3633
Peng Taode040be2012-01-10 22:42:47 +08003634 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003635 __func__, buf, buflen, npages, args.acl_len);
3636 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3637 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003638 if (ret)
3639 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003640
3641 acl_len = res.acl_len - res.acl_data_offset;
3642 if (acl_len > args.acl_len)
3643 nfs4_write_cached_acl(inode, NULL, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003644 else
Andy Adamsonbf118a32011-12-07 11:55:27 -05003645 nfs4_write_cached_acl(inode, resp_buf + res.acl_data_offset,
3646 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003647 if (buf) {
3648 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003649 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003650 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003651 _copy_from_pages(buf, pages, res.acl_data_offset,
3652 res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003653 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003654 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003655out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003656 for (i = 0; i < npages; i++)
3657 if (pages[i])
3658 __free_page(pages[i]);
3659 if (args.acl_scratch)
3660 __free_page(args.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003661 return ret;
3662}
3663
Trond Myklebust16b42892006-08-24 12:27:15 -04003664static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3665{
3666 struct nfs4_exception exception = { };
3667 ssize_t ret;
3668 do {
3669 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3670 if (ret >= 0)
3671 break;
3672 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3673 } while (exception.retry);
3674 return ret;
3675}
3676
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003677static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3678{
3679 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003680 int ret;
3681
3682 if (!nfs4_server_supports_acls(server))
3683 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003684 ret = nfs_revalidate_inode(server, inode);
3685 if (ret < 0)
3686 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003687 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3688 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003689 ret = nfs4_read_cached_acl(inode, buf, buflen);
3690 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003691 /* -ENOENT is returned if there is no ACL or if there is an ACL
3692 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003693 return ret;
3694 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003695}
3696
Trond Myklebust16b42892006-08-24 12:27:15 -04003697static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003698{
3699 struct nfs_server *server = NFS_SERVER(inode);
3700 struct page *pages[NFS4ACL_MAXPAGES];
3701 struct nfs_setaclargs arg = {
3702 .fh = NFS_FH(inode),
3703 .acl_pages = pages,
3704 .acl_len = buflen,
3705 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003706 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003707 struct rpc_message msg = {
3708 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3709 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003710 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003711 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003712 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003713
3714 if (!nfs4_server_supports_acls(server))
3715 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003716 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3717 if (i < 0)
3718 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003719 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003720 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003721
3722 /*
3723 * Free each page after tx, so the only ref left is
3724 * held by the network stack
3725 */
3726 for (; i > 0; i--)
3727 put_page(pages[i-1]);
3728
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003729 /*
3730 * Acl update can result in inode attribute update.
3731 * so mark the attribute cache invalid.
3732 */
3733 spin_lock(&inode->i_lock);
3734 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3735 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003736 nfs_access_zap_cache(inode);
3737 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003738 return ret;
3739}
3740
Trond Myklebust16b42892006-08-24 12:27:15 -04003741static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3742{
3743 struct nfs4_exception exception = { };
3744 int err;
3745 do {
3746 err = nfs4_handle_exception(NFS_SERVER(inode),
3747 __nfs4_proc_set_acl(inode, buf, buflen),
3748 &exception);
3749 } while (exception.retry);
3750 return err;
3751}
3752
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003754nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003756 struct nfs_client *clp = server->nfs_client;
3757
3758 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 return 0;
3760 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003761 case -NFS4ERR_ADMIN_REVOKED:
3762 case -NFS4ERR_BAD_STATEID:
3763 case -NFS4ERR_OPENMODE:
3764 if (state == NULL)
3765 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003766 nfs4_schedule_stateid_recovery(server, state);
3767 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003768 case -NFS4ERR_EXPIRED:
3769 if (state != NULL)
3770 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003772 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003773 nfs4_schedule_lease_recovery(clp);
3774 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003775#if defined(CONFIG_NFS_V4_1)
3776 case -NFS4ERR_BADSESSION:
3777 case -NFS4ERR_BADSLOT:
3778 case -NFS4ERR_BAD_HIGH_SLOT:
3779 case -NFS4ERR_DEADSESSION:
3780 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3781 case -NFS4ERR_SEQ_FALSE_RETRY:
3782 case -NFS4ERR_SEQ_MISORDERED:
3783 dprintk("%s ERROR %d, Reset session\n", __func__,
3784 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003785 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003786 task->tk_status = 0;
3787 return -EAGAIN;
3788#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003790 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003791 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003792 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3794 task->tk_status = 0;
3795 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003796 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 case -NFS4ERR_OLD_STATEID:
3798 task->tk_status = 0;
3799 return -EAGAIN;
3800 }
3801 task->tk_status = nfs4_map_errors(task->tk_status);
3802 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003803wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003804 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003805 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3806 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3807 task->tk_status = 0;
3808 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809}
3810
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003811int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3812 unsigned short port, struct rpc_cred *cred,
3813 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814{
3815 nfs4_verifier sc_verifier;
3816 struct nfs4_setclientid setclientid = {
3817 .sc_verifier = &sc_verifier,
3818 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003819 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 };
3821 struct rpc_message msg = {
3822 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3823 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003824 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003825 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 };
Al Virobc4785c2006-10-19 23:28:51 -07003827 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 int loop = 0;
3829 int status;
3830
Al Virobc4785c2006-10-19 23:28:51 -07003831 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3833 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3834
3835 for(;;) {
3836 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003837 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003838 clp->cl_ipaddr,
3839 rpc_peeraddr2str(clp->cl_rpcclient,
3840 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003841 rpc_peeraddr2str(clp->cl_rpcclient,
3842 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003843 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 clp->cl_id_uniquifier);
3845 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003846 sizeof(setclientid.sc_netid),
3847 rpc_peeraddr2str(clp->cl_rpcclient,
3848 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003850 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 clp->cl_ipaddr, port >> 8, port & 255);
3852
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003853 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 if (status != -NFS4ERR_CLID_INUSE)
3855 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003856 if (loop != 0) {
3857 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003859 }
3860 ++loop;
3861 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 }
3863 return status;
3864}
3865
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003866int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003867 struct nfs4_setclientid_res *arg,
3868 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869{
3870 struct nfs_fsinfo fsinfo;
3871 struct rpc_message msg = {
3872 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003873 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003875 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 };
3877 unsigned long now;
3878 int status;
3879
3880 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003881 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 if (status == 0) {
3883 spin_lock(&clp->cl_lock);
3884 clp->cl_lease_time = fsinfo.lease_time * HZ;
3885 clp->cl_last_renewal = now;
3886 spin_unlock(&clp->cl_lock);
3887 }
3888 return status;
3889}
3890
Trond Myklebustfe650402006-01-03 09:55:18 +01003891struct nfs4_delegreturndata {
3892 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003893 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003894 struct nfs_fh fh;
3895 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003896 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003897 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003898 int rpc_status;
3899};
3900
Trond Myklebustfe650402006-01-03 09:55:18 +01003901static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3902{
3903 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003904
Trond Myklebust14516c32010-07-31 14:29:06 -04003905 if (!nfs4_sequence_done(task, &data->res.seq_res))
3906 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003907
Ricardo Labiaga79708862009-12-07 09:23:21 -05003908 switch (task->tk_status) {
3909 case -NFS4ERR_STALE_STATEID:
3910 case -NFS4ERR_EXPIRED:
3911 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003912 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003913 break;
3914 default:
3915 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3916 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003917 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003918 return;
3919 }
3920 }
3921 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003922}
3923
3924static void nfs4_delegreturn_release(void *calldata)
3925{
Trond Myklebustfe650402006-01-03 09:55:18 +01003926 kfree(calldata);
3927}
3928
Andy Adamson938e1012009-04-01 09:22:28 -04003929#if defined(CONFIG_NFS_V4_1)
3930static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3931{
3932 struct nfs4_delegreturndata *d_data;
3933
3934 d_data = (struct nfs4_delegreturndata *)data;
3935
Trond Myklebust035168a2010-06-16 09:52:26 -04003936 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003937 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003938 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04003939 return;
3940 rpc_call_start(task);
3941}
3942#endif /* CONFIG_NFS_V4_1 */
3943
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003944static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003945#if defined(CONFIG_NFS_V4_1)
3946 .rpc_call_prepare = nfs4_delegreturn_prepare,
3947#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003948 .rpc_call_done = nfs4_delegreturn_done,
3949 .rpc_release = nfs4_delegreturn_release,
3950};
3951
Trond Myklebuste6f81072008-01-24 18:14:34 -05003952static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003953{
3954 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003955 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003956 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003957 struct rpc_message msg = {
3958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3959 .rpc_cred = cred,
3960 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003961 struct rpc_task_setup task_setup_data = {
3962 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003963 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003964 .callback_ops = &nfs4_delegreturn_ops,
3965 .flags = RPC_TASK_ASYNC,
3966 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003967 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003968
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003969 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003970 if (data == NULL)
3971 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003972 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01003973 data->args.fhandle = &data->fh;
3974 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003975 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003976 nfs_copy_fh(&data->fh, NFS_FH(inode));
3977 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003978 data->res.fattr = &data->fattr;
3979 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003980 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003981 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003982 data->rpc_status = 0;
3983
Trond Myklebustc970aa82007-07-14 15:39:59 -04003984 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003985 msg.rpc_argp = &data->args;
3986 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003987 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003988 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003989 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003990 if (!issync)
3991 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003992 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003993 if (status != 0)
3994 goto out;
3995 status = data->rpc_status;
3996 if (status != 0)
3997 goto out;
3998 nfs_refresh_inode(inode, &data->fattr);
3999out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004000 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004001 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002}
4003
Trond Myklebuste6f81072008-01-24 18:14:34 -05004004int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005{
4006 struct nfs_server *server = NFS_SERVER(inode);
4007 struct nfs4_exception exception = { };
4008 int err;
4009 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004010 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 switch (err) {
4012 case -NFS4ERR_STALE_STATEID:
4013 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 case 0:
4015 return 0;
4016 }
4017 err = nfs4_handle_exception(server, err, &exception);
4018 } while (exception.retry);
4019 return err;
4020}
4021
4022#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4023#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4024
4025/*
4026 * sleep, with exponential backoff, and retry the LOCK operation.
4027 */
4028static unsigned long
4029nfs4_set_lock_task_retry(unsigned long timeout)
4030{
Jeff Laytond3103102011-12-01 22:44:39 +01004031 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 timeout <<= 1;
4033 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4034 return NFS4_LOCK_MAXTIMEOUT;
4035 return timeout;
4036}
4037
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4039{
4040 struct inode *inode = state->inode;
4041 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004042 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004043 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004045 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004047 struct nfs_lockt_res res = {
4048 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 };
4050 struct rpc_message msg = {
4051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4052 .rpc_argp = &arg,
4053 .rpc_resp = &res,
4054 .rpc_cred = state->owner->so_cred,
4055 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 struct nfs4_lock_state *lsp;
4057 int status;
4058
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004059 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004060 status = nfs4_set_lock_state(state, request);
4061 if (status != 0)
4062 goto out;
4063 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004064 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004065 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004066 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004067 switch (status) {
4068 case 0:
4069 request->fl_type = F_UNLCK;
4070 break;
4071 case -NFS4ERR_DENIED:
4072 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004074 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004075out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 return status;
4077}
4078
4079static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4080{
4081 struct nfs4_exception exception = { };
4082 int err;
4083
4084 do {
4085 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4086 _nfs4_proc_getlk(state, cmd, request),
4087 &exception);
4088 } while (exception.retry);
4089 return err;
4090}
4091
4092static int do_vfs_lock(struct file *file, struct file_lock *fl)
4093{
4094 int res = 0;
4095 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4096 case FL_POSIX:
4097 res = posix_lock_file_wait(file, fl);
4098 break;
4099 case FL_FLOCK:
4100 res = flock_lock_file_wait(file, fl);
4101 break;
4102 default:
4103 BUG();
4104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 return res;
4106}
4107
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004108struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004109 struct nfs_locku_args arg;
4110 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004111 struct nfs4_lock_state *lsp;
4112 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004113 struct file_lock fl;
4114 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004115 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004116};
4117
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004118static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4119 struct nfs_open_context *ctx,
4120 struct nfs4_lock_state *lsp,
4121 struct nfs_seqid *seqid)
4122{
4123 struct nfs4_unlockdata *p;
4124 struct inode *inode = lsp->ls_state->inode;
4125
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004126 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004127 if (p == NULL)
4128 return NULL;
4129 p->arg.fh = NFS_FH(inode);
4130 p->arg.fl = &p->fl;
4131 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004132 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004133 p->arg.stateid = &lsp->ls_stateid;
4134 p->lsp = lsp;
4135 atomic_inc(&lsp->ls_count);
4136 /* Ensure we don't close file until we're done freeing locks! */
4137 p->ctx = get_nfs_open_context(ctx);
4138 memcpy(&p->fl, fl, sizeof(p->fl));
4139 p->server = NFS_SERVER(inode);
4140 return p;
4141}
4142
Trond Myklebust06f814a2006-01-03 09:55:07 +01004143static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004144{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004145 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004146 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004147 nfs4_put_lock_state(calldata->lsp);
4148 put_nfs_open_context(calldata->ctx);
4149 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004150}
4151
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004152static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004153{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004154 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004155
Trond Myklebust14516c32010-07-31 14:29:06 -04004156 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4157 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004158 switch (task->tk_status) {
4159 case 0:
4160 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004161 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004162 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004163 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004164 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004165 case -NFS4ERR_BAD_STATEID:
4166 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004167 case -NFS4ERR_STALE_STATEID:
4168 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004169 break;
4170 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004171 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004172 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004173 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004174}
4175
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004176static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004177{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004178 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004180 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004181 return;
4182 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004183 /* Note: exit _without_ running nfs4_locku_done */
4184 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004185 return;
4186 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004187 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004188 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004189 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004190 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004191 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004192 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193}
4194
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004195static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004196 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004197 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004198 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004199};
4200
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004201static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4202 struct nfs_open_context *ctx,
4203 struct nfs4_lock_state *lsp,
4204 struct nfs_seqid *seqid)
4205{
4206 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004207 struct rpc_message msg = {
4208 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4209 .rpc_cred = ctx->cred,
4210 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004211 struct rpc_task_setup task_setup_data = {
4212 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004213 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004214 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004215 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004216 .flags = RPC_TASK_ASYNC,
4217 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004218
Frank Filz137d6ac2007-07-09 15:32:29 -07004219 /* Ensure this is an unlock - when canceling a lock, the
4220 * canceled lock is passed in, and it won't be an unlock.
4221 */
4222 fl->fl_type = F_UNLCK;
4223
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004224 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4225 if (data == NULL) {
4226 nfs_free_seqid(seqid);
4227 return ERR_PTR(-ENOMEM);
4228 }
4229
Trond Myklebust9d12b212012-01-17 22:04:25 -05004230 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004231 msg.rpc_argp = &data->arg;
4232 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004233 task_setup_data.callback_data = data;
4234 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004235}
4236
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4238{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004239 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004240 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004241 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004242 struct rpc_task *task;
4243 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004244 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
Trond Myklebust9b073572006-06-29 16:38:34 -04004246 status = nfs4_set_lock_state(state, request);
4247 /* Unlock _before_ we do the RPC call */
4248 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004249 down_read(&nfsi->rwsem);
4250 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4251 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004252 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004253 }
4254 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004255 if (status != 0)
4256 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004257 /* Is this a delegated lock? */
4258 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004259 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004260 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004261 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004262 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004263 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004264 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004265 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004266 status = PTR_ERR(task);
4267 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004268 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004269 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004270 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004271out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004272 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004273 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274}
4275
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004276struct nfs4_lockdata {
4277 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004278 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004279 struct nfs4_lock_state *lsp;
4280 struct nfs_open_context *ctx;
4281 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004282 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004283 int rpc_status;
4284 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004285 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004286};
4287
4288static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004289 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4290 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004291{
4292 struct nfs4_lockdata *p;
4293 struct inode *inode = lsp->ls_state->inode;
4294 struct nfs_server *server = NFS_SERVER(inode);
4295
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004296 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004297 if (p == NULL)
4298 return NULL;
4299
4300 p->arg.fh = NFS_FH(inode);
4301 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004302 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004303 if (p->arg.open_seqid == NULL)
4304 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004305 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004306 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004307 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004308 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004309 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004310 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004311 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004312 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004313 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004314 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004315 atomic_inc(&lsp->ls_count);
4316 p->ctx = get_nfs_open_context(ctx);
4317 memcpy(&p->fl, fl, sizeof(p->fl));
4318 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004319out_free_seqid:
4320 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004321out_free:
4322 kfree(p);
4323 return NULL;
4324}
4325
4326static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4327{
4328 struct nfs4_lockdata *data = calldata;
4329 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
Harvey Harrison3110ff82008-05-02 13:42:44 -07004331 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004332 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4333 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004334 /* Do we need to do an open_to_lock_owner? */
4335 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004336 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4337 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004338 data->arg.open_stateid = &state->stateid;
4339 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004340 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004341 } else
4342 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004343 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004344 if (nfs4_setup_sequence(data->server,
4345 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004346 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004347 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004348 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004349 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004350}
4351
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004352static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4353{
4354 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4355 nfs4_lock_prepare(task, calldata);
4356}
4357
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004358static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4359{
4360 struct nfs4_lockdata *data = calldata;
4361
Harvey Harrison3110ff82008-05-02 13:42:44 -07004362 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004363
Trond Myklebust14516c32010-07-31 14:29:06 -04004364 if (!nfs4_sequence_done(task, &data->res.seq_res))
4365 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004366
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004367 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004368 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004369 if (data->rpc_status == 0)
4370 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4371 else
4372 goto out;
4373 }
4374 if (data->rpc_status == 0) {
4375 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4376 sizeof(data->lsp->ls_stateid.data));
4377 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004378 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004379 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004380out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004381 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004382}
4383
4384static void nfs4_lock_release(void *calldata)
4385{
4386 struct nfs4_lockdata *data = calldata;
4387
Harvey Harrison3110ff82008-05-02 13:42:44 -07004388 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004389 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004390 if (data->cancelled != 0) {
4391 struct rpc_task *task;
4392 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4393 data->arg.lock_seqid);
4394 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004395 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004396 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004397 } else
4398 nfs_free_seqid(data->arg.lock_seqid);
4399 nfs4_put_lock_state(data->lsp);
4400 put_nfs_open_context(data->ctx);
4401 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004402 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004403}
4404
4405static const struct rpc_call_ops nfs4_lock_ops = {
4406 .rpc_call_prepare = nfs4_lock_prepare,
4407 .rpc_call_done = nfs4_lock_done,
4408 .rpc_release = nfs4_lock_release,
4409};
4410
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004411static const struct rpc_call_ops nfs4_recover_lock_ops = {
4412 .rpc_call_prepare = nfs4_recover_lock_prepare,
4413 .rpc_call_done = nfs4_lock_done,
4414 .rpc_release = nfs4_lock_release,
4415};
4416
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004417static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4418{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004419 switch (error) {
4420 case -NFS4ERR_ADMIN_REVOKED:
4421 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004422 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004423 if (new_lock_owner != 0 ||
4424 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004425 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004426 break;
4427 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004428 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004429 case -NFS4ERR_EXPIRED:
4430 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004431 };
4432}
4433
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004434static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004435{
4436 struct nfs4_lockdata *data;
4437 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004438 struct rpc_message msg = {
4439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4440 .rpc_cred = state->owner->so_cred,
4441 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004442 struct rpc_task_setup task_setup_data = {
4443 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004444 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004445 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004446 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004447 .flags = RPC_TASK_ASYNC,
4448 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004449 int ret;
4450
Harvey Harrison3110ff82008-05-02 13:42:44 -07004451 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004452 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004453 fl->fl_u.nfs4_fl.owner,
4454 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004455 if (data == NULL)
4456 return -ENOMEM;
4457 if (IS_SETLKW(cmd))
4458 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004459 if (recovery_type > NFS_LOCK_NEW) {
4460 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004461 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004462 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4463 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004464 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004465 msg.rpc_argp = &data->arg;
4466 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004467 task_setup_data.callback_data = data;
4468 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004469 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004470 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004471 ret = nfs4_wait_for_completion_rpc_task(task);
4472 if (ret == 0) {
4473 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004474 if (ret)
4475 nfs4_handle_setlk_error(data->server, data->lsp,
4476 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004477 } else
4478 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004479 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004480 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004481 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482}
4483
4484static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4485{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004486 struct nfs_server *server = NFS_SERVER(state->inode);
4487 struct nfs4_exception exception = { };
4488 int err;
4489
4490 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004491 /* Cache the lock if possible... */
4492 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4493 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004494 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004495 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004496 break;
4497 nfs4_handle_exception(server, err, &exception);
4498 } while (exception.retry);
4499 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500}
4501
4502static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4503{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004504 struct nfs_server *server = NFS_SERVER(state->inode);
4505 struct nfs4_exception exception = { };
4506 int err;
4507
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004508 err = nfs4_set_lock_state(state, request);
4509 if (err != 0)
4510 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004511 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004512 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4513 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004514 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004515 switch (err) {
4516 default:
4517 goto out;
4518 case -NFS4ERR_GRACE:
4519 case -NFS4ERR_DELAY:
4520 nfs4_handle_exception(server, err, &exception);
4521 err = 0;
4522 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004523 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004524out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004525 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526}
4527
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004528#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004529static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004530{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004531 int status, ret = NFS_OK;
4532 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004533 struct nfs_server *server = NFS_SERVER(state->inode);
4534
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004535 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4536 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4537 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4538 if (status != NFS_OK) {
4539 nfs41_free_stateid(server, &lsp->ls_stateid);
4540 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4541 ret = status;
4542 }
4543 }
4544 };
4545
4546 return ret;
4547}
4548
4549static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4550{
4551 int status = NFS_OK;
4552
4553 if (test_bit(LK_STATE_IN_USE, &state->flags))
4554 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004555 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004556 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004557 return nfs4_lock_expired(state, request);
4558}
4559#endif
4560
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4562{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004563 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004564 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004565 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004567 if ((fl_flags & FL_POSIX) &&
4568 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4569 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004570 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004571 status = nfs4_set_lock_state(state, request);
4572 if (status != 0)
4573 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004574 request->fl_flags |= FL_ACCESS;
4575 status = do_vfs_lock(request->fl_file, request);
4576 if (status < 0)
4577 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004578 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004579 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004580 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004581 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004582 request->fl_flags = fl_flags & ~FL_SLEEP;
4583 status = do_vfs_lock(request->fl_file, request);
4584 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004585 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004586 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004587 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004588 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004589 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004590 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004591 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004592 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4593 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004594out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004595 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004596out:
4597 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 return status;
4599}
4600
4601static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4602{
4603 struct nfs4_exception exception = { };
4604 int err;
4605
4606 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004607 err = _nfs4_proc_setlk(state, cmd, request);
4608 if (err == -NFS4ERR_DENIED)
4609 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004611 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 } while (exception.retry);
4613 return err;
4614}
4615
4616static int
4617nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4618{
4619 struct nfs_open_context *ctx;
4620 struct nfs4_state *state;
4621 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4622 int status;
4623
4624 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004625 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 state = ctx->state;
4627
4628 if (request->fl_start < 0 || request->fl_end < 0)
4629 return -EINVAL;
4630
Trond Myklebustd9531262009-07-21 19:22:38 -04004631 if (IS_GETLK(cmd)) {
4632 if (state != NULL)
4633 return nfs4_proc_getlk(state, F_GETLK, request);
4634 return 0;
4635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636
4637 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4638 return -EINVAL;
4639
Trond Myklebustd9531262009-07-21 19:22:38 -04004640 if (request->fl_type == F_UNLCK) {
4641 if (state != NULL)
4642 return nfs4_proc_unlck(state, cmd, request);
4643 return 0;
4644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645
Trond Myklebustd9531262009-07-21 19:22:38 -04004646 if (state == NULL)
4647 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 do {
4649 status = nfs4_proc_setlk(state, cmd, request);
4650 if ((status != -EAGAIN) || IS_SETLK(cmd))
4651 break;
4652 timeout = nfs4_set_lock_task_retry(timeout);
4653 status = -ERESTARTSYS;
4654 if (signalled())
4655 break;
4656 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 return status;
4658}
4659
Trond Myklebust888e6942005-11-04 15:38:11 -05004660int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4661{
4662 struct nfs_server *server = NFS_SERVER(state->inode);
4663 struct nfs4_exception exception = { };
4664 int err;
4665
4666 err = nfs4_set_lock_state(state, fl);
4667 if (err != 0)
4668 goto out;
4669 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004670 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004671 switch (err) {
4672 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004673 printk(KERN_ERR "NFS: %s: unhandled error "
4674 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004675 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004676 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004677 goto out;
4678 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004679 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004680 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004681 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004682 nfs4_schedule_lease_recovery(server->nfs_client);
4683 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004684 case -NFS4ERR_BADSESSION:
4685 case -NFS4ERR_BADSLOT:
4686 case -NFS4ERR_BAD_HIGH_SLOT:
4687 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4688 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004689 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004690 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004691 case -ERESTARTSYS:
4692 /*
4693 * The show must go on: exit, but mark the
4694 * stateid as needing recovery.
4695 */
4696 case -NFS4ERR_ADMIN_REVOKED:
4697 case -NFS4ERR_BAD_STATEID:
4698 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004699 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004700 err = 0;
4701 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004702 case -EKEYEXPIRED:
4703 /*
4704 * User RPCSEC_GSS context has expired.
4705 * We cannot recover this stateid now, so
4706 * skip it and allow recovery thread to
4707 * proceed.
4708 */
4709 err = 0;
4710 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004711 case -ENOMEM:
4712 case -NFS4ERR_DENIED:
4713 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4714 err = 0;
4715 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004716 case -NFS4ERR_DELAY:
4717 break;
4718 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004719 err = nfs4_handle_exception(server, err, &exception);
4720 } while (exception.retry);
4721out:
4722 return err;
4723}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004724
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004725static void nfs4_release_lockowner_release(void *calldata)
4726{
4727 kfree(calldata);
4728}
4729
4730const struct rpc_call_ops nfs4_release_lockowner_ops = {
4731 .rpc_release = nfs4_release_lockowner_release,
4732};
4733
4734void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4735{
4736 struct nfs_server *server = lsp->ls_state->owner->so_server;
4737 struct nfs_release_lockowner_args *args;
4738 struct rpc_message msg = {
4739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4740 };
4741
4742 if (server->nfs_client->cl_mvops->minor_version != 0)
4743 return;
4744 args = kmalloc(sizeof(*args), GFP_NOFS);
4745 if (!args)
4746 return;
4747 args->lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004748 args->lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004749 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004750 msg.rpc_argp = args;
4751 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4752}
4753
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004754#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4755
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004756static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4757 const void *buf, size_t buflen,
4758 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004759{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004760 if (strcmp(key, "") != 0)
4761 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004762
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004763 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004764}
4765
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004766static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4767 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004768{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004769 if (strcmp(key, "") != 0)
4770 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004771
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004772 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004773}
4774
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004775static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4776 size_t list_len, const char *name,
4777 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004778{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004779 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004780
J. Bruce Fields096455a2006-03-20 23:23:42 -05004781 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4782 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004783
4784 if (list && len <= list_len)
4785 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004786 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004787}
4788
Andy Adamson533eb462011-06-13 18:25:56 -04004789/*
4790 * nfs_fhget will use either the mounted_on_fileid or the fileid
4791 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004792static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4793{
Andy Adamson533eb462011-06-13 18:25:56 -04004794 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4795 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4796 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4797 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004798 return;
4799
4800 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4801 NFS_ATTR_FATTR_NLINK;
4802 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4803 fattr->nlink = 2;
4804}
4805
Trond Myklebust56659e92007-07-17 21:52:39 -04004806int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004807 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004808{
4809 struct nfs_server *server = NFS_SERVER(dir);
4810 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004811 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004812 };
4813 struct nfs4_fs_locations_arg args = {
4814 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004815 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004816 .page = page,
4817 .bitmask = bitmask,
4818 };
Benny Halevy22958462009-04-01 09:22:02 -04004819 struct nfs4_fs_locations_res res = {
4820 .fs_locations = fs_locations,
4821 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004822 struct rpc_message msg = {
4823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4824 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004825 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004826 };
4827 int status;
4828
Harvey Harrison3110ff82008-05-02 13:42:44 -07004829 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004830
4831 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4832 * is not supported */
4833 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4834 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4835 else
4836 bitmask[0] |= FATTR4_WORD0_FILEID;
4837
Trond Myklebustc228fd32007-01-13 02:28:11 -05004838 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004839 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004840 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004841 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004842 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004843 return status;
4844}
4845
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004846static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4847{
4848 int status;
4849 struct nfs4_secinfo_arg args = {
4850 .dir_fh = NFS_FH(dir),
4851 .name = name,
4852 };
4853 struct nfs4_secinfo_res res = {
4854 .flavors = flavors,
4855 };
4856 struct rpc_message msg = {
4857 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4858 .rpc_argp = &args,
4859 .rpc_resp = &res,
4860 };
4861
4862 dprintk("NFS call secinfo %s\n", name->name);
4863 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4864 dprintk("NFS reply secinfo: %d\n", status);
4865 return status;
4866}
4867
4868int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4869{
4870 struct nfs4_exception exception = { };
4871 int err;
4872 do {
4873 err = nfs4_handle_exception(NFS_SERVER(dir),
4874 _nfs4_proc_secinfo(dir, name, flavors),
4875 &exception);
4876 } while (exception.retry);
4877 return err;
4878}
4879
Andy Adamson557134a2009-04-01 09:21:53 -04004880#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004881/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004882 * Check the exchange flags returned by the server for invalid flags, having
4883 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4884 * DS flags set.
4885 */
4886static int nfs4_check_cl_exchange_flags(u32 flags)
4887{
4888 if (flags & ~EXCHGID4_FLAG_MASK_R)
4889 goto out_inval;
4890 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4891 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4892 goto out_inval;
4893 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4894 goto out_inval;
4895 return NFS_OK;
4896out_inval:
4897 return -NFS4ERR_INVAL;
4898}
4899
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004900static bool
4901nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4902{
4903 if (a->server_scope_sz == b->server_scope_sz &&
4904 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4905 return true;
4906
4907 return false;
4908}
4909
Andy Adamson357f54d2010-12-14 10:11:57 -05004910/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004911 * nfs4_proc_exchange_id()
4912 *
4913 * Since the clientid has expired, all compounds using sessions
4914 * associated with the stale clientid will be returning
4915 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4916 * be in some phase of session reset.
4917 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004918int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004919{
4920 nfs4_verifier verifier;
4921 struct nfs41_exchange_id_args args = {
4922 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004923 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004924 };
4925 struct nfs41_exchange_id_res res = {
4926 .client = clp,
4927 };
4928 int status;
4929 struct rpc_message msg = {
4930 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4931 .rpc_argp = &args,
4932 .rpc_resp = &res,
4933 .rpc_cred = cred,
4934 };
4935 __be32 *p;
4936
4937 dprintk("--> %s\n", __func__);
4938 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004939
Benny Halevy99fe60d2009-04-01 09:22:29 -04004940 p = (u32 *)verifier.data;
4941 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4942 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4943 args.verifier = &verifier;
4944
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004945 args.id_len = scnprintf(args.id, sizeof(args.id),
4946 "%s/%s.%s/%u",
4947 clp->cl_ipaddr,
4948 init_utsname()->nodename,
4949 init_utsname()->domainname,
4950 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004951
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004952 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05004953 if (unlikely(!res.server_scope)) {
4954 status = -ENOMEM;
4955 goto out;
4956 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004957
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05004958 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
4959 if (unlikely(!res.impl_id)) {
4960 status = -ENOMEM;
4961 goto out_server_scope;
4962 }
4963
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004964 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004965 if (!status)
4966 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004967
4968 if (!status) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05004969 /* use the most recent implementation id */
4970 kfree(clp->impl_id);
4971 clp->impl_id = res.impl_id;
4972 } else
4973 kfree(res.impl_id);
4974
4975 if (!status) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004976 if (clp->server_scope &&
4977 !nfs41_same_server_scope(clp->server_scope,
4978 res.server_scope)) {
4979 dprintk("%s: server_scope mismatch detected\n",
4980 __func__);
4981 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4982 kfree(clp->server_scope);
4983 clp->server_scope = NULL;
4984 }
4985
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05004986 if (!clp->server_scope) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004987 clp->server_scope = res.server_scope;
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05004988 goto out;
4989 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004990 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05004991
4992out_server_scope:
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05004993 kfree(res.server_scope);
4994out:
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05004995 if (clp->impl_id)
4996 dprintk("%s: Server Implementation ID: "
4997 "domain: %s, name: %s, date: %llu,%u\n",
4998 __func__, clp->impl_id->domain, clp->impl_id->name,
4999 clp->impl_id->date.seconds,
5000 clp->impl_id->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005001 dprintk("<-- %s status= %d\n", __func__, status);
5002 return status;
5003}
5004
Andy Adamson2050f0c2009-04-01 09:22:30 -04005005struct nfs4_get_lease_time_data {
5006 struct nfs4_get_lease_time_args *args;
5007 struct nfs4_get_lease_time_res *res;
5008 struct nfs_client *clp;
5009};
5010
5011static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5012 void *calldata)
5013{
5014 int ret;
5015 struct nfs4_get_lease_time_data *data =
5016 (struct nfs4_get_lease_time_data *)calldata;
5017
5018 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005019 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005020 /* just setup sequence, do not trigger session recovery
5021 since we're invoked within one */
5022 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005023 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005024 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005025
5026 BUG_ON(ret == -EAGAIN);
5027 rpc_call_start(task);
5028 dprintk("<-- %s\n", __func__);
5029}
5030
5031/*
5032 * Called from nfs4_state_manager thread for session setup, so don't recover
5033 * from sequence operation or clientid errors.
5034 */
5035static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5036{
5037 struct nfs4_get_lease_time_data *data =
5038 (struct nfs4_get_lease_time_data *)calldata;
5039
5040 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005041 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5042 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005043 switch (task->tk_status) {
5044 case -NFS4ERR_DELAY:
5045 case -NFS4ERR_GRACE:
5046 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5047 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5048 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005049 /* fall through */
5050 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005051 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005052 return;
5053 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005054 dprintk("<-- %s\n", __func__);
5055}
5056
5057struct rpc_call_ops nfs4_get_lease_time_ops = {
5058 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5059 .rpc_call_done = nfs4_get_lease_time_done,
5060};
5061
5062int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5063{
5064 struct rpc_task *task;
5065 struct nfs4_get_lease_time_args args;
5066 struct nfs4_get_lease_time_res res = {
5067 .lr_fsinfo = fsinfo,
5068 };
5069 struct nfs4_get_lease_time_data data = {
5070 .args = &args,
5071 .res = &res,
5072 .clp = clp,
5073 };
5074 struct rpc_message msg = {
5075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5076 .rpc_argp = &args,
5077 .rpc_resp = &res,
5078 };
5079 struct rpc_task_setup task_setup = {
5080 .rpc_client = clp->cl_rpcclient,
5081 .rpc_message = &msg,
5082 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005083 .callback_data = &data,
5084 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005085 };
5086 int status;
5087
Trond Myklebust9d12b212012-01-17 22:04:25 -05005088 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005089 dprintk("--> %s\n", __func__);
5090 task = rpc_run_task(&task_setup);
5091
5092 if (IS_ERR(task))
5093 status = PTR_ERR(task);
5094 else {
5095 status = task->tk_status;
5096 rpc_put_task(task);
5097 }
5098 dprintk("<-- %s return %d\n", __func__, status);
5099
5100 return status;
5101}
5102
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005103/*
5104 * Reset a slot table
5105 */
Andy Adamson104aeba2010-01-14 17:45:10 -05005106static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5107 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005108{
Andy Adamson104aeba2010-01-14 17:45:10 -05005109 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005110 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005111 int ret = 0;
5112
Andy Adamson104aeba2010-01-14 17:45:10 -05005113 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5114 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005115
Andy Adamson104aeba2010-01-14 17:45:10 -05005116 /* Does the newly negotiated max_reqs match the existing slot table? */
5117 if (max_reqs != tbl->max_slots) {
5118 ret = -ENOMEM;
5119 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005120 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005121 if (!new)
5122 goto out;
5123 ret = 0;
5124 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005125 }
5126 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05005127 if (new) {
5128 tbl->slots = new;
5129 tbl->max_slots = max_reqs;
5130 }
5131 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005132 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005133 spin_unlock(&tbl->slot_tbl_lock);
5134 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5135 tbl, tbl->slots, tbl->max_slots);
5136out:
5137 dprintk("<-- %s: return %d\n", __func__, ret);
5138 return ret;
5139}
5140
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005141/* Destroy the slot table */
5142static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5143{
5144 if (session->fc_slot_table.slots != NULL) {
5145 kfree(session->fc_slot_table.slots);
5146 session->fc_slot_table.slots = NULL;
5147 }
5148 if (session->bc_slot_table.slots != NULL) {
5149 kfree(session->bc_slot_table.slots);
5150 session->bc_slot_table.slots = NULL;
5151 }
5152 return;
5153}
5154
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005155/*
Andy Adamsonfc931582009-04-01 09:22:31 -04005156 * Initialize slot table
5157 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005158static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5159 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04005160{
Andy Adamsonfc931582009-04-01 09:22:31 -04005161 struct nfs4_slot *slot;
5162 int ret = -ENOMEM;
5163
5164 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5165
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005166 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04005167
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005168 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04005169 if (!slot)
5170 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04005171 ret = 0;
5172
5173 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04005174 tbl->max_slots = max_slots;
5175 tbl->slots = slot;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005176 tbl->highest_used_slotid = NFS4_NO_SLOT; /* no slot is currently used */
Andy Adamsonfc931582009-04-01 09:22:31 -04005177 spin_unlock(&tbl->slot_tbl_lock);
5178 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5179 tbl, tbl->slots, tbl->max_slots);
5180out:
5181 dprintk("<-- %s: return %d\n", __func__, ret);
5182 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04005183}
5184
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005185/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005186 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005187 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005188static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005189{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005190 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005191 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005192
Andy Adamsonaacd5532011-11-09 13:58:21 -05005193 dprintk("--> %s\n", __func__);
5194 /* Fore channel */
5195 tbl = &ses->fc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005196 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005197 status = nfs4_init_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5198 if (status) /* -ENOMEM */
5199 return status;
5200 } else {
5201 status = nfs4_reset_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005202 if (status)
5203 return status;
5204 }
Andy Adamsonaacd5532011-11-09 13:58:21 -05005205 /* Back channel */
5206 tbl = &ses->bc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005207 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005208 status = nfs4_init_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005209 if (status)
Andy Adamsonaacd5532011-11-09 13:58:21 -05005210 /* Fore and back channel share a connection so get
5211 * both slot tables or neither */
5212 nfs4_destroy_slot_tables(ses);
5213 } else
5214 status = nfs4_reset_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005215 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005216}
5217
5218struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5219{
5220 struct nfs4_session *session;
5221 struct nfs4_slot_table *tbl;
5222
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005223 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005224 if (!session)
5225 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005226
Andy Adamson557134a2009-04-01 09:21:53 -04005227 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005228 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005229 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005230 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005231 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005232
5233 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005234 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005235 spin_lock_init(&tbl->slot_tbl_lock);
5236 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005237 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005238
Trond Myklebust1055d762010-06-16 09:52:27 -04005239 session->session_state = 1<<NFS4_SESSION_INITING;
5240
Andy Adamson557134a2009-04-01 09:21:53 -04005241 session->clp = clp;
5242 return session;
5243}
5244
5245void nfs4_destroy_session(struct nfs4_session *session)
5246{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005247 nfs4_proc_destroy_session(session);
5248 dprintk("%s Destroy backchannel for xprt %p\n",
5249 __func__, session->clp->cl_rpcclient->cl_xprt);
5250 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5251 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 */