blob: 54767dd66cf974236178391a6866bddae3ac3753 [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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust4e0038b2012-03-01 17:01:05 -05001103 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001104 int delegation_flags = 0;
1105
Trond Myklebust003707c2007-07-05 18:07:55 -04001106 rcu_read_lock();
1107 delegation = rcu_dereference(NFS_I(inode)->delegation);
1108 if (delegation)
1109 delegation_flags = delegation->flags;
1110 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001111 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1112 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1113 "returning a delegation for "
1114 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001115 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001116 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001117 nfs_inode_set_delegation(state->inode,
1118 data->owner->so_cred,
1119 &data->o_res);
1120 else
1121 nfs_inode_reclaim_delegation(state->inode,
1122 data->owner->so_cred,
1123 &data->o_res);
1124 }
Trond Myklebust34310432008-12-23 15:21:38 -05001125
1126 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001127 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001128 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001129out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001130 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001131err_put_inode:
1132 iput(inode);
1133err:
1134 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001135}
1136
Trond Myklebust864472e2006-01-03 09:55:15 +01001137static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1138{
1139 struct nfs_inode *nfsi = NFS_I(state->inode);
1140 struct nfs_open_context *ctx;
1141
1142 spin_lock(&state->inode->i_lock);
1143 list_for_each_entry(ctx, &nfsi->open_files, list) {
1144 if (ctx->state != state)
1145 continue;
1146 get_nfs_open_context(ctx);
1147 spin_unlock(&state->inode->i_lock);
1148 return ctx;
1149 }
1150 spin_unlock(&state->inode->i_lock);
1151 return ERR_PTR(-ENOENT);
1152}
1153
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001154static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1155{
1156 struct nfs4_opendata *opendata;
1157
Al Viro3d4ff432011-06-22 18:40:12 -04001158 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001159 if (opendata == NULL)
1160 return ERR_PTR(-ENOMEM);
1161 opendata->state = state;
1162 atomic_inc(&state->count);
1163 return opendata;
1164}
1165
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001166static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001167{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001168 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001169 int ret;
1170
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001171 opendata->o_arg.open_flags = 0;
1172 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001173 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1174 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1175 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001176 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001177 if (ret != 0)
1178 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001179 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001180 if (IS_ERR(newstate))
1181 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001182 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001183 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001184 return 0;
1185}
1186
1187static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1188{
Trond Myklebust864472e2006-01-03 09:55:15 +01001189 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001190 int ret;
1191
1192 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001193 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001194 smp_rmb();
1195 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001196 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001197 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001198 if (ret != 0)
1199 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001200 if (newstate != state)
1201 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001202 }
1203 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001204 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001205 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001206 if (ret != 0)
1207 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001208 if (newstate != state)
1209 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001210 }
1211 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001212 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001213 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001214 if (ret != 0)
1215 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001216 if (newstate != state)
1217 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001218 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001219 /*
1220 * We may have performed cached opens for all three recoveries.
1221 * Check if we need to update the current stateid.
1222 */
1223 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1224 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001225 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001226 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1227 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001228 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001229 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001230 return 0;
1231}
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233/*
1234 * OPEN_RECLAIM:
1235 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001237static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001239 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001240 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001241 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 int status;
1243
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001244 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1245 if (IS_ERR(opendata))
1246 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001247 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1248 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001249 rcu_read_lock();
1250 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001251 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001252 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001253 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001254 opendata->o_arg.u.delegation_type = delegation_type;
1255 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001256 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return status;
1258}
1259
Trond Myklebust539cd032007-06-05 11:46:42 -04001260static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 struct nfs_server *server = NFS_SERVER(state->inode);
1263 struct nfs4_exception exception = { };
1264 int err;
1265 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001266 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001267 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001268 break;
1269 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 } while (exception.retry);
1271 return err;
1272}
1273
Trond Myklebust864472e2006-01-03 09:55:15 +01001274static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1275{
1276 struct nfs_open_context *ctx;
1277 int ret;
1278
1279 ctx = nfs4_state_find_open_context(state);
1280 if (IS_ERR(ctx))
1281 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001282 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001283 put_nfs_open_context(ctx);
1284 return ret;
1285}
1286
Trond Myklebust13437e12007-07-06 15:10:43 -04001287static 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 -07001288{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001289 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001290 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001292 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1293 if (IS_ERR(opendata))
1294 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001295 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001296 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001297 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001298 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001299 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001300 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
Trond Myklebust13437e12007-07-06 15:10:43 -04001303int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
1305 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001306 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 int err;
1308 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001309 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 switch (err) {
1311 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001312 case -ENOENT:
1313 case -ESTALE:
1314 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001315 case -NFS4ERR_BADSESSION:
1316 case -NFS4ERR_BADSLOT:
1317 case -NFS4ERR_BAD_HIGH_SLOT:
1318 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1319 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001320 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001321 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 case -NFS4ERR_STALE_CLIENTID:
1323 case -NFS4ERR_STALE_STATEID:
1324 case -NFS4ERR_EXPIRED:
1325 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001326 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001327 goto out;
1328 case -ERESTARTSYS:
1329 /*
1330 * The show must go on: exit, but mark the
1331 * stateid as needing recovery.
1332 */
1333 case -NFS4ERR_ADMIN_REVOKED:
1334 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001335 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001336 case -EKEYEXPIRED:
1337 /*
1338 * User RPCSEC_GSS context has expired.
1339 * We cannot recover this stateid now, so
1340 * skip it and allow recovery thread to
1341 * proceed.
1342 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001343 case -ENOMEM:
1344 err = 0;
1345 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
1347 err = nfs4_handle_exception(server, err, &exception);
1348 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001349out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return err;
1351}
1352
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001353static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1354{
1355 struct nfs4_opendata *data = calldata;
1356
1357 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001358 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001359 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1360 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001361 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001362 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001363 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001364 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001365}
1366
1367static void nfs4_open_confirm_release(void *calldata)
1368{
1369 struct nfs4_opendata *data = calldata;
1370 struct nfs4_state *state = NULL;
1371
1372 /* If this request hasn't been cancelled, do nothing */
1373 if (data->cancelled == 0)
1374 goto out_free;
1375 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001376 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001377 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001378 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001379 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001380 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001381out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001382 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001383}
1384
1385static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001386 .rpc_call_done = nfs4_open_confirm_done,
1387 .rpc_release = nfs4_open_confirm_release,
1388};
1389
1390/*
1391 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1392 */
1393static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1394{
1395 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1396 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001397 struct rpc_message msg = {
1398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1399 .rpc_argp = &data->c_arg,
1400 .rpc_resp = &data->c_res,
1401 .rpc_cred = data->owner->so_cred,
1402 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001403 struct rpc_task_setup task_setup_data = {
1404 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001405 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001406 .callback_ops = &nfs4_open_confirm_ops,
1407 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001408 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001409 .flags = RPC_TASK_ASYNC,
1410 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 int status;
1412
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001413 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001414 data->rpc_done = 0;
1415 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001416 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001417 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001418 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001419 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001420 status = nfs4_wait_for_completion_rpc_task(task);
1421 if (status != 0) {
1422 data->cancelled = 1;
1423 smp_wmb();
1424 } else
1425 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001426 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return status;
1428}
1429
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001430static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001432 struct nfs4_opendata *data = calldata;
1433 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001434
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001435 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1436 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001437 /*
1438 * Check if we still need to send an OPEN call, or if we can use
1439 * a delegation instead.
1440 */
1441 if (data->state != NULL) {
1442 struct nfs_delegation *delegation;
1443
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001444 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001445 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001446 rcu_read_lock();
1447 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001448 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1449 can_open_delegated(delegation, data->o_arg.fmode))
1450 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001451 rcu_read_unlock();
1452 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001453 /* Update sequence id. */
Trond Myklebust48c22eb2012-01-17 22:04:25 -05001454 data->o_arg.id = sp->so_seqid.owner_id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001455 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001456 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001457 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001458 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1459 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001460 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001461 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001462 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001463 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001464 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001465 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001466 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001467unlock_no_action:
1468 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001469out_no_action:
1470 task->tk_action = NULL;
1471
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001472}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001474static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1475{
1476 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1477 nfs4_open_prepare(task, calldata);
1478}
1479
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001480static void nfs4_open_done(struct rpc_task *task, void *calldata)
1481{
1482 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001484 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001485
Trond Myklebust14516c32010-07-31 14:29:06 -04001486 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1487 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001488
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001489 if (task->tk_status == 0) {
1490 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001491 case S_IFREG:
1492 break;
1493 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001494 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001495 break;
1496 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001497 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001498 break;
1499 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001500 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001501 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001502 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001503 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1504 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001505 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001506 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001507}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001508
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001509static void nfs4_open_release(void *calldata)
1510{
1511 struct nfs4_opendata *data = calldata;
1512 struct nfs4_state *state = NULL;
1513
1514 /* If this request hasn't been cancelled, do nothing */
1515 if (data->cancelled == 0)
1516 goto out_free;
1517 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001518 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001519 goto out_free;
1520 /* In case we need an open_confirm, no cleanup! */
1521 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1522 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001523 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001524 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001525 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001526out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001527 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001528}
1529
1530static const struct rpc_call_ops nfs4_open_ops = {
1531 .rpc_call_prepare = nfs4_open_prepare,
1532 .rpc_call_done = nfs4_open_done,
1533 .rpc_release = nfs4_open_release,
1534};
1535
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001536static const struct rpc_call_ops nfs4_recover_open_ops = {
1537 .rpc_call_prepare = nfs4_recover_open_prepare,
1538 .rpc_call_done = nfs4_open_done,
1539 .rpc_release = nfs4_open_release,
1540};
1541
1542static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001543{
1544 struct inode *dir = data->dir->d_inode;
1545 struct nfs_server *server = NFS_SERVER(dir);
1546 struct nfs_openargs *o_arg = &data->o_arg;
1547 struct nfs_openres *o_res = &data->o_res;
1548 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001549 struct rpc_message msg = {
1550 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1551 .rpc_argp = o_arg,
1552 .rpc_resp = o_res,
1553 .rpc_cred = data->owner->so_cred,
1554 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001555 struct rpc_task_setup task_setup_data = {
1556 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001557 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001558 .callback_ops = &nfs4_open_ops,
1559 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001560 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001561 .flags = RPC_TASK_ASYNC,
1562 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001563 int status;
1564
Trond Myklebust9d12b212012-01-17 22:04:25 -05001565 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001566 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001567 data->rpc_done = 0;
1568 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001569 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001570 if (isrecover)
1571 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001572 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001573 if (IS_ERR(task))
1574 return PTR_ERR(task);
1575 status = nfs4_wait_for_completion_rpc_task(task);
1576 if (status != 0) {
1577 data->cancelled = 1;
1578 smp_wmb();
1579 } else
1580 status = data->rpc_status;
1581 rpc_put_task(task);
1582
1583 return status;
1584}
1585
1586static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1587{
1588 struct inode *dir = data->dir->d_inode;
1589 struct nfs_openres *o_res = &data->o_res;
1590 int status;
1591
1592 status = nfs4_run_open_task(data, 1);
1593 if (status != 0 || !data->rpc_done)
1594 return status;
1595
Trond Myklebust6926afd2012-01-07 13:22:46 -05001596 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1597
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001598 nfs_refresh_inode(dir, o_res->dir_attr);
1599
1600 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1601 status = _nfs4_proc_open_confirm(data);
1602 if (status != 0)
1603 return status;
1604 }
1605
1606 return status;
1607}
1608
1609/*
1610 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1611 */
1612static int _nfs4_proc_open(struct nfs4_opendata *data)
1613{
1614 struct inode *dir = data->dir->d_inode;
1615 struct nfs_server *server = NFS_SERVER(dir);
1616 struct nfs_openargs *o_arg = &data->o_arg;
1617 struct nfs_openres *o_res = &data->o_res;
1618 int status;
1619
1620 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001621 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001622 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001623 if (status != 0) {
1624 if (status == -NFS4ERR_BADNAME &&
1625 !(o_arg->open_flags & O_CREAT))
1626 return -ENOENT;
1627 return status;
1628 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001629
Trond Myklebust6926afd2012-01-07 13:22:46 -05001630 nfs_fattr_map_and_free_names(server, &data->f_attr);
1631
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001632 if (o_arg->open_flags & O_CREAT) {
1633 update_changeattr(dir, &o_res->cinfo);
1634 nfs_post_op_update_inode(dir, o_res->dir_attr);
1635 } else
1636 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001637 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1638 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001640 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001642 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001645 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001646 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
Andy Adamsond83217c2011-03-01 01:34:17 +00001649static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001650{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001651 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001652 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001653
Trond Myklebusta78cb572009-08-09 15:06:19 -04001654 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001655 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001656 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001657 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001658 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1659 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001660 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001661 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001662 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001663 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001664 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001665}
1666
Andy Adamsond83217c2011-03-01 01:34:17 +00001667static int nfs4_recover_expired_lease(struct nfs_server *server)
1668{
1669 return nfs4_client_recover_expired_lease(server->nfs_client);
1670}
1671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672/*
1673 * OPEN_EXPIRED:
1674 * reclaim state on the server after a network partition.
1675 * Assumes caller holds the appropriate lock
1676 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001677static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001679 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001680 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001682 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1683 if (IS_ERR(opendata))
1684 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001685 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001686 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001687 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001688 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001689 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690}
1691
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001692static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001693{
Trond Myklebust539cd032007-06-05 11:46:42 -04001694 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001695 struct nfs4_exception exception = { };
1696 int err;
1697
1698 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001699 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001700 switch (err) {
1701 default:
1702 goto out;
1703 case -NFS4ERR_GRACE:
1704 case -NFS4ERR_DELAY:
1705 nfs4_handle_exception(server, err, &exception);
1706 err = 0;
1707 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001708 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001709out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001710 return err;
1711}
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1714{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001716 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Trond Myklebust864472e2006-01-03 09:55:15 +01001718 ctx = nfs4_state_find_open_context(state);
1719 if (IS_ERR(ctx))
1720 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001721 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001722 put_nfs_open_context(ctx);
1723 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001726#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001727static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001728{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001729 int status = NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001730 struct nfs_server *server = NFS_SERVER(state->inode);
1731
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001732 if (state->flags & flags) {
1733 status = nfs41_test_stateid(server, stateid);
1734 if (status != NFS_OK) {
1735 nfs41_free_stateid(server, stateid);
1736 state->flags &= ~flags;
1737 }
1738 }
1739 return status;
1740}
1741
1742static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1743{
1744 int deleg_status, open_status;
1745 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1746 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1747
1748 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1749 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1750
1751 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1752 return NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001753 return nfs4_open_expired(sp, state);
1754}
1755#endif
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001758 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1759 * fields corresponding to attributes that were used to store the verifier.
1760 * Make sure we clobber those fields in the later setattr call
1761 */
1762static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1763{
1764 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1765 !(sattr->ia_valid & ATTR_ATIME_SET))
1766 sattr->ia_valid |= ATTR_ATIME;
1767
1768 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1769 !(sattr->ia_valid & ATTR_MTIME_SET))
1770 sattr->ia_valid |= ATTR_MTIME;
1771}
1772
1773/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001774 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001776static 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 -07001777{
1778 struct nfs4_state_owner *sp;
1779 struct nfs4_state *state = NULL;
1780 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001781 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001782 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001786 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1787 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1789 goto out_err;
1790 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001791 status = nfs4_recover_expired_lease(server);
1792 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001793 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001794 if (dentry->d_inode != NULL)
1795 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001796 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001797 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001798 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001799 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Al Viro82a2c1b2011-06-22 18:30:55 -04001801 if (dentry->d_inode != NULL)
1802 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001803
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001804 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001806 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001808 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001809 status = PTR_ERR(state);
1810 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001811 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001812 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001813 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001814
1815 if (opendata->o_arg.open_flags & O_EXCL) {
1816 nfs4_exclusive_attrset(opendata, sattr);
1817
1818 nfs_fattr_init(opendata->o_res.f_attr);
1819 status = nfs4_do_setattr(state->inode, cred,
1820 opendata->o_res.f_attr, sattr,
1821 state);
1822 if (status == 0)
1823 nfs_setattr_update_inode(state->inode, sattr);
1824 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1825 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001826 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 *res = state;
1829 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001830err_opendata_put:
1831 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001832err_put_state_owner:
1833 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 *res = NULL;
1836 return status;
1837}
1838
1839
Al Viro82a2c1b2011-06-22 18:30:55 -04001840static 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 -07001841{
1842 struct nfs4_exception exception = { };
1843 struct nfs4_state *res;
1844 int status;
1845
1846 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001847 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (status == 0)
1849 break;
1850 /* NOTE: BAD_SEQID means the server and client disagree about the
1851 * book-keeping w.r.t. state-changing operations
1852 * (OPEN/CLOSE/LOCK/LOCKU...)
1853 * It is actually a sign of a bug on the client or on the server.
1854 *
1855 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001856 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 * have unhashed the old state_owner for us, and that we can
1858 * therefore safely retry using a new one. We should still warn
1859 * the user though...
1860 */
1861 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001862 printk(KERN_WARNING "NFS: v4 server %s "
1863 " returned a bad sequence-id error!\n",
1864 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 exception.retry = 1;
1866 continue;
1867 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001868 /*
1869 * BAD_STATEID on OPEN means that the server cancelled our
1870 * state before it received the OPEN_CONFIRM.
1871 * Recover by retrying the request as per the discussion
1872 * on Page 181 of RFC3530.
1873 */
1874 if (status == -NFS4ERR_BAD_STATEID) {
1875 exception.retry = 1;
1876 continue;
1877 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001878 if (status == -EAGAIN) {
1879 /* We must have found a delegation */
1880 exception.retry = 1;
1881 continue;
1882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1884 status, &exception));
1885 } while (exception.retry);
1886 return res;
1887}
1888
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001889static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1890 struct nfs_fattr *fattr, struct iattr *sattr,
1891 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001893 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001895 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 .iap = sattr,
1897 .server = server,
1898 .bitmask = server->attr_bitmask,
1899 };
1900 struct nfs_setattrres res = {
1901 .fattr = fattr,
1902 .server = server,
1903 };
1904 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1906 .rpc_argp = &arg,
1907 .rpc_resp = &res,
1908 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001910 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001911 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Trond Myklebust0e574af2005-10-27 22:12:38 -04001913 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001915 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1916 /* Use that stateid */
1917 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001918 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001919 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1921
Bryan Schumaker7c513052011-03-24 17:12:24 +00001922 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001923 if (status == 0 && state != NULL)
1924 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001925 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001928static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1929 struct nfs_fattr *fattr, struct iattr *sattr,
1930 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001932 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 struct nfs4_exception exception = { };
1934 int err;
1935 do {
1936 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001937 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 &exception);
1939 } while (exception.retry);
1940 return err;
1941}
1942
1943struct nfs4_closedata {
1944 struct inode *inode;
1945 struct nfs4_state *state;
1946 struct nfs_closeargs arg;
1947 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001948 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001949 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001950 bool roc;
1951 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952};
1953
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001954static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001955{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001956 struct nfs4_closedata *calldata = data;
1957 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001958 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001959
Fred Isamanf7e89172011-01-06 11:36:32 +00001960 if (calldata->roc)
1961 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001962 nfs4_put_open_state(calldata->state);
1963 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001964 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001965 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001966 kfree(calldata);
1967}
1968
Trond Myklebust88069f72009-12-08 08:33:16 -05001969static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1970 fmode_t fmode)
1971{
1972 spin_lock(&state->owner->so_lock);
1973 if (!(fmode & FMODE_READ))
1974 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1975 if (!(fmode & FMODE_WRITE))
1976 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1977 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1978 spin_unlock(&state->owner->so_lock);
1979}
1980
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001981static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001983 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 struct nfs_server *server = NFS_SERVER(calldata->inode);
1986
Chuck Levera3ca5652012-03-01 17:00:40 -05001987 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04001988 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1989 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 /* hmm. we are done with the inode, and in the process of freeing
1991 * the state_owner. we keep this around to process errors
1992 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 switch (task->tk_status) {
1994 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001995 if (calldata->roc)
1996 pnfs_roc_set_barrier(state->inode,
1997 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001998 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001999 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002000 nfs4_close_clear_stateid_flags(state,
2001 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 break;
2003 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002004 case -NFS4ERR_OLD_STATEID:
2005 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002007 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002008 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002010 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2011 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002013 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002014 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002015 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016}
2017
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002018static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002020 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002021 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002022 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002023
Chuck Levera3ca5652012-03-01 17:00:40 -05002024 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002025 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002026 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002027
Trond Myklebust88069f72009-12-08 08:33:16 -05002028 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2029 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002030 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002031 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002032 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002033 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002034 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2035 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2036 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002037 }
2038 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002039 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2040 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2041 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002042 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002043 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002044 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002045
2046 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002047 /* Note: exit _without_ calling nfs4_close_done */
2048 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002049 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002050 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002051
Fred Isamanf7e89172011-01-06 11:36:32 +00002052 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002053 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002054 if (calldata->roc &&
2055 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2056 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2057 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002058 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002059 }
2060 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002061
Trond Myklebust516a6af2005-10-27 22:12:41 -04002062 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002063 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04002064 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002065 &calldata->arg.seq_args,
2066 &calldata->res.seq_res,
2067 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002068 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002069 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002070out:
2071 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002074static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002075 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002076 .rpc_call_done = nfs4_close_done,
2077 .rpc_release = nfs4_free_closedata,
2078};
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080/*
2081 * It is possible for data to be read/written from a mem-mapped file
2082 * after the sys_close call (which hits the vfs layer as a flush).
2083 * This means that we can't safely call nfsv4 close on a file until
2084 * the inode is cleared. This in turn means that we are not good
2085 * NFSv4 citizens - we do not indicate to the server to update the file's
2086 * share state even when we are done with one of the three share
2087 * stateid's in the inode.
2088 *
2089 * NOTE: Caller must be holding the sp->so_owner semaphore!
2090 */
Al Viro643168c2011-06-22 18:20:23 -04002091int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002093 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002095 struct nfs4_state_owner *sp = state->owner;
2096 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002097 struct rpc_message msg = {
2098 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2099 .rpc_cred = state->owner->so_cred,
2100 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002101 struct rpc_task_setup task_setup_data = {
2102 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002103 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002104 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002105 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002106 .flags = RPC_TASK_ASYNC,
2107 };
Trond Myklebust95121352005-10-18 14:20:12 -07002108 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002110 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002112 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002113 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002114 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002116 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002117 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002119 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002120 if (calldata->arg.seqid == NULL)
2121 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002122 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002123 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002124 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002125 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002126 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002127 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002128 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002129
Trond Myklebust1174dd12010-12-21 10:52:24 -05002130 msg.rpc_argp = &calldata->arg;
2131 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002132 task_setup_data.callback_data = calldata;
2133 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002134 if (IS_ERR(task))
2135 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002136 status = 0;
2137 if (wait)
2138 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002139 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002140 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002141out_free_calldata:
2142 kfree(calldata);
2143out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002144 if (roc)
2145 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002146 nfs4_put_open_state(state);
2147 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002148 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
2150
Trond Myklebust2b484292010-09-17 10:56:51 -04002151static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002152nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 struct nfs4_state *state;
2155
Trond Myklebust565277f2007-10-15 18:17:53 -04002156 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002157 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002158 if (IS_ERR(state))
2159 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002160 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002161 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
2163
Trond Myklebust1185a552009-12-03 15:54:02 -05002164static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002165{
2166 if (ctx->state == NULL)
2167 return;
2168 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002169 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002170 else
Al Viro643168c2011-06-22 18:20:23 -04002171 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002172}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2175{
Benny Halevy43652ad2009-04-01 09:21:54 -04002176 struct nfs4_server_caps_arg args = {
2177 .fhandle = fhandle,
2178 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 struct nfs4_server_caps_res res = {};
2180 struct rpc_message msg = {
2181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002182 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 .rpc_resp = &res,
2184 };
2185 int status;
2186
Bryan Schumaker7c513052011-03-24 17:12:24 +00002187 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 if (status == 0) {
2189 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002190 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2191 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2192 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2193 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2194 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2196 server->caps |= NFS_CAP_ACLS;
2197 if (res.has_links != 0)
2198 server->caps |= NFS_CAP_HARDLINKS;
2199 if (res.has_symlinks != 0)
2200 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002201 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2202 server->caps |= NFS_CAP_FILEID;
2203 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2204 server->caps |= NFS_CAP_MODE;
2205 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2206 server->caps |= NFS_CAP_NLINK;
2207 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2208 server->caps |= NFS_CAP_OWNER;
2209 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2210 server->caps |= NFS_CAP_OWNER_GROUP;
2211 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2212 server->caps |= NFS_CAP_ATIME;
2213 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2214 server->caps |= NFS_CAP_CTIME;
2215 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2216 server->caps |= NFS_CAP_MTIME;
2217
Trond Myklebusta65318b2009-03-11 14:10:28 -04002218 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2219 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2220 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 server->acl_bitmask = res.acl_bitmask;
2222 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002223
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 return status;
2225}
2226
Trond Myklebust55a97592006-06-09 09:34:19 -04002227int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
2229 struct nfs4_exception exception = { };
2230 int err;
2231 do {
2232 err = nfs4_handle_exception(server,
2233 _nfs4_server_capabilities(server, fhandle),
2234 &exception);
2235 } while (exception.retry);
2236 return err;
2237}
2238
2239static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2240 struct nfs_fsinfo *info)
2241{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 struct nfs4_lookup_root_arg args = {
2243 .bitmask = nfs4_fattr_bitmap,
2244 };
2245 struct nfs4_lookup_res res = {
2246 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002247 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 .fh = fhandle,
2249 };
2250 struct rpc_message msg = {
2251 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2252 .rpc_argp = &args,
2253 .rpc_resp = &res,
2254 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002255
Trond Myklebust0e574af2005-10-27 22:12:38 -04002256 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002257 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258}
2259
2260static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2261 struct nfs_fsinfo *info)
2262{
2263 struct nfs4_exception exception = { };
2264 int err;
2265 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002266 err = _nfs4_lookup_root(server, fhandle, info);
2267 switch (err) {
2268 case 0:
2269 case -NFS4ERR_WRONGSEC:
2270 break;
2271 default:
2272 err = nfs4_handle_exception(server, err, &exception);
2273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 } while (exception.retry);
2275 return err;
2276}
2277
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002278static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2279 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2280{
2281 struct rpc_auth *auth;
2282 int ret;
2283
2284 auth = rpcauth_create(flavor, server->client);
2285 if (!auth) {
2286 ret = -EIO;
2287 goto out;
2288 }
2289 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002290out:
2291 return ret;
2292}
2293
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002294static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002295 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002297 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002298 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002300 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2301 flav_array[len] = RPC_AUTH_NULL;
2302 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002303
2304 for (i = 0; i < len; i++) {
2305 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002306 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002307 continue;
2308 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002309 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002310 /*
2311 * -EACCESS could mean that the user doesn't have correct permissions
2312 * to access the mount. It could also mean that we tried to mount
2313 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2314 * existing mount programs don't handle -EACCES very well so it should
2315 * be mapped to -EPERM instead.
2316 */
2317 if (status == -EACCES)
2318 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002319 return status;
2320}
2321
2322/*
2323 * get the file handle for the "/" directory on the server
2324 */
2325static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2326 struct nfs_fsinfo *info)
2327{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002328 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002329 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002330 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2331 /*
2332 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2333 * by nfs4_map_errors() as this function exits.
2334 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002335 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 if (status == 0)
2337 status = nfs4_server_capabilities(server, fhandle);
2338 if (status == 0)
2339 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002340 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341}
2342
Andy Adamson533eb462011-06-13 18:25:56 -04002343static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002344/*
2345 * Get locations and (maybe) other attributes of a referral.
2346 * Note that we'll actually follow the referral later when
2347 * we detect fsid mismatch in inode revalidation
2348 */
Andy Adamson533eb462011-06-13 18:25:56 -04002349static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2350 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002351{
2352 int status = -ENOMEM;
2353 struct page *page = NULL;
2354 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002355
2356 page = alloc_page(GFP_KERNEL);
2357 if (page == NULL)
2358 goto out;
2359 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2360 if (locations == NULL)
2361 goto out;
2362
Trond Myklebustc228fd32007-01-13 02:28:11 -05002363 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002364 if (status != 0)
2365 goto out;
2366 /* Make sure server returned a different fsid for the referral */
2367 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002368 dprintk("%s: server did not return a different fsid for"
2369 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002370 status = -EIO;
2371 goto out;
2372 }
Andy Adamson533eb462011-06-13 18:25:56 -04002373 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2374 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002375
Andy Adamson533eb462011-06-13 18:25:56 -04002376 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002377 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002378 memset(fhandle, 0, sizeof(struct nfs_fh));
2379out:
2380 if (page)
2381 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002382 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002383 return status;
2384}
2385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2387{
2388 struct nfs4_getattr_arg args = {
2389 .fh = fhandle,
2390 .bitmask = server->attr_bitmask,
2391 };
2392 struct nfs4_getattr_res res = {
2393 .fattr = fattr,
2394 .server = server,
2395 };
2396 struct rpc_message msg = {
2397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2398 .rpc_argp = &args,
2399 .rpc_resp = &res,
2400 };
2401
Trond Myklebust0e574af2005-10-27 22:12:38 -04002402 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002403 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404}
2405
2406static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2407{
2408 struct nfs4_exception exception = { };
2409 int err;
2410 do {
2411 err = nfs4_handle_exception(server,
2412 _nfs4_proc_getattr(server, fhandle, fattr),
2413 &exception);
2414 } while (exception.retry);
2415 return err;
2416}
2417
2418/*
2419 * The file is not closed if it is opened due to the a request to change
2420 * the size of the file. The open call will not be needed once the
2421 * VFS layer lookup-intents are implemented.
2422 *
2423 * Close is called when the inode is destroyed.
2424 * If we haven't opened the file for O_WRONLY, we
2425 * need to in the size_change case to obtain a stateid.
2426 *
2427 * Got race?
2428 * Because OPEN is always done by name in nfsv4, it is
2429 * possible that we opened a different file by the same
2430 * name. We can recognize this race condition, but we
2431 * can't do anything about it besides returning an error.
2432 *
2433 * This will be fixed with VFS changes (lookup-intent).
2434 */
2435static int
2436nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2437 struct iattr *sattr)
2438{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002439 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002440 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002441 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 int status;
2443
Benny Halevy8a1636c2010-07-14 15:43:57 -04002444 if (pnfs_ld_layoutret_on_setattr(inode))
2445 pnfs_return_layout(inode);
2446
Trond Myklebust0e574af2005-10-27 22:12:38 -04002447 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Trond Myklebustd5308382005-11-04 15:33:38 -05002449 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002450 if (sattr->ia_valid & ATTR_FILE) {
2451 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002452
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002453 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002454 if (ctx) {
2455 cred = ctx->cred;
2456 state = ctx->state;
2457 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002458 }
2459
Trond Myklebust536e43d2012-01-17 22:04:26 -05002460 /* Deal with open(O_TRUNC) */
2461 if (sattr->ia_valid & ATTR_OPEN)
2462 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2463
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002464 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002465 if (status == 0)
2466 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 return status;
2468}
2469
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002470static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2471 const struct qstr *name, struct nfs_fh *fhandle,
2472 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002473{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002474 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002475 int status;
2476 struct nfs4_lookup_arg args = {
2477 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002478 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002479 .name = name,
2480 };
2481 struct nfs4_lookup_res res = {
2482 .server = server,
2483 .fattr = fattr,
2484 .fh = fhandle,
2485 };
2486 struct rpc_message msg = {
2487 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2488 .rpc_argp = &args,
2489 .rpc_resp = &res,
2490 };
2491
2492 nfs_fattr_init(fattr);
2493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002495 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 dprintk("NFS reply lookup: %d\n", status);
2497 return status;
2498}
2499
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002500void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2501{
2502 memset(fh, 0, sizeof(struct nfs_fh));
2503 fattr->fsid.major = 1;
2504 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2505 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2506 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2507 fattr->nlink = 2;
2508}
2509
Bryan Schumaker7c513052011-03-24 17:12:24 +00002510static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2511 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
2513 struct nfs4_exception exception = { };
2514 int err;
2515 do {
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002516 int status;
2517
2518 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2519 switch (status) {
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002520 case -NFS4ERR_BADNAME:
2521 return -ENOENT;
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002522 case -NFS4ERR_MOVED:
Trond Myklebusta6f498a2011-11-07 15:50:32 -05002523 return nfs4_get_referral(dir, name, fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002524 case -NFS4ERR_WRONGSEC:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002525 nfs_fixup_secinfo_attributes(fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002526 }
2527 err = nfs4_handle_exception(NFS_SERVER(dir),
2528 status, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 } while (exception.retry);
2530 return err;
2531}
2532
2533static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2534{
Trond Myklebust76b32992007-08-10 17:45:11 -04002535 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 struct nfs4_accessargs args = {
2537 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002538 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002540 struct nfs4_accessres res = {
2541 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002542 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 struct rpc_message msg = {
2544 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2545 .rpc_argp = &args,
2546 .rpc_resp = &res,
2547 .rpc_cred = entry->cred,
2548 };
2549 int mode = entry->mask;
2550 int status;
2551
2552 /*
2553 * Determine which access bits we want to ask for...
2554 */
2555 if (mode & MAY_READ)
2556 args.access |= NFS4_ACCESS_READ;
2557 if (S_ISDIR(inode->i_mode)) {
2558 if (mode & MAY_WRITE)
2559 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2560 if (mode & MAY_EXEC)
2561 args.access |= NFS4_ACCESS_LOOKUP;
2562 } else {
2563 if (mode & MAY_WRITE)
2564 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2565 if (mode & MAY_EXEC)
2566 args.access |= NFS4_ACCESS_EXECUTE;
2567 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002568
2569 res.fattr = nfs_alloc_fattr();
2570 if (res.fattr == NULL)
2571 return -ENOMEM;
2572
Bryan Schumaker7c513052011-03-24 17:12:24 +00002573 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 if (!status) {
2575 entry->mask = 0;
2576 if (res.access & NFS4_ACCESS_READ)
2577 entry->mask |= MAY_READ;
2578 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2579 entry->mask |= MAY_WRITE;
2580 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2581 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002582 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002584 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 return status;
2586}
2587
2588static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2589{
2590 struct nfs4_exception exception = { };
2591 int err;
2592 do {
2593 err = nfs4_handle_exception(NFS_SERVER(inode),
2594 _nfs4_proc_access(inode, entry),
2595 &exception);
2596 } while (exception.retry);
2597 return err;
2598}
2599
2600/*
2601 * TODO: For the time being, we don't try to get any attributes
2602 * along with any of the zero-copy operations READ, READDIR,
2603 * READLINK, WRITE.
2604 *
2605 * In the case of the first three, we want to put the GETATTR
2606 * after the read-type operation -- this is because it is hard
2607 * to predict the length of a GETATTR response in v4, and thus
2608 * align the READ data correctly. This means that the GETATTR
2609 * may end up partially falling into the page cache, and we should
2610 * shift it into the 'tail' of the xdr_buf before processing.
2611 * To do this efficiently, we need to know the total length
2612 * of data received, which doesn't seem to be available outside
2613 * of the RPC layer.
2614 *
2615 * In the case of WRITE, we also want to put the GETATTR after
2616 * the operation -- in this case because we want to make sure
2617 * we get the post-operation mtime and size. This means that
2618 * we can't use xdr_encode_pages() as written: we need a variant
2619 * of it which would leave room in the 'tail' iovec.
2620 *
2621 * Both of these changes to the XDR layer would in fact be quite
2622 * minor, but I decided to leave them for a subsequent patch.
2623 */
2624static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2625 unsigned int pgbase, unsigned int pglen)
2626{
2627 struct nfs4_readlink args = {
2628 .fh = NFS_FH(inode),
2629 .pgbase = pgbase,
2630 .pglen = pglen,
2631 .pages = &page,
2632 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002633 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 struct rpc_message msg = {
2635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2636 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002637 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 };
2639
Bryan Schumaker7c513052011-03-24 17:12:24 +00002640 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 -07002641}
2642
2643static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2644 unsigned int pgbase, unsigned int pglen)
2645{
2646 struct nfs4_exception exception = { };
2647 int err;
2648 do {
2649 err = nfs4_handle_exception(NFS_SERVER(inode),
2650 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2651 &exception);
2652 } while (exception.retry);
2653 return err;
2654}
2655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656/*
2657 * Got race?
2658 * We will need to arrange for the VFS layer to provide an atomic open.
2659 * Until then, this create/open method is prone to inefficiency and race
2660 * conditions due to the lookup, create, and open VFS calls from sys_open()
2661 * placed on the wire.
2662 *
2663 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2664 * The file will be opened again in the subsequent VFS open call
2665 * (nfs4_proc_file_open).
2666 *
2667 * The open for read will just hang around to be used by any process that
2668 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2669 */
2670
2671static int
2672nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002673 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
Al Viro82a2c1b2011-06-22 18:30:55 -04002675 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002677 struct rpc_cred *cred = NULL;
2678 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 int status = 0;
2680
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002681 if (ctx != NULL) {
2682 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002683 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002684 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002686 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002687 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002688 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 if (IS_ERR(state)) {
2690 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002691 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002693 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002694 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002695 if (ctx != NULL)
2696 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002697 else
Al Viro643168c2011-06-22 18:20:23 -04002698 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699out:
2700 return status;
2701}
2702
2703static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2704{
Trond Myklebust16e42952005-10-27 22:12:44 -04002705 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002706 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002708 .name.len = name->len,
2709 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002710 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002712 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002713 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002714 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002716 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2717 .rpc_argp = &args,
2718 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002720 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Trond Myklebustd3468902010-04-16 16:22:50 -04002722 res.dir_attr = nfs_alloc_fattr();
2723 if (res.dir_attr == NULL)
2724 goto out;
2725
Bryan Schumaker7c513052011-03-24 17:12:24 +00002726 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002727 if (status == 0) {
2728 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002729 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002730 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002731 nfs_free_fattr(res.dir_attr);
2732out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 return status;
2734}
2735
2736static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2737{
2738 struct nfs4_exception exception = { };
2739 int err;
2740 do {
2741 err = nfs4_handle_exception(NFS_SERVER(dir),
2742 _nfs4_proc_remove(dir, name),
2743 &exception);
2744 } while (exception.retry);
2745 return err;
2746}
2747
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002748static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002750 struct nfs_server *server = NFS_SERVER(dir);
2751 struct nfs_removeargs *args = msg->rpc_argp;
2752 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Trond Myklebusta65318b2009-03-11 14:10:28 -04002754 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002755 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002757 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758}
2759
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002760static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002762 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2763
Trond Myklebust14516c32010-07-31 14:29:06 -04002764 if (!nfs4_sequence_done(task, &res->seq_res))
2765 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002766 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002767 return 0;
2768 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002769 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002770 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771}
2772
Jeff Laytond3d41522010-09-17 17:31:57 -04002773static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2774{
2775 struct nfs_server *server = NFS_SERVER(dir);
2776 struct nfs_renameargs *arg = msg->rpc_argp;
2777 struct nfs_renameres *res = msg->rpc_resp;
2778
2779 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2780 arg->bitmask = server->attr_bitmask;
2781 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002782 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002783}
2784
2785static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2786 struct inode *new_dir)
2787{
2788 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2789
2790 if (!nfs4_sequence_done(task, &res->seq_res))
2791 return 0;
2792 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2793 return 0;
2794
2795 update_changeattr(old_dir, &res->old_cinfo);
2796 nfs_post_op_update_inode(old_dir, res->old_fattr);
2797 update_changeattr(new_dir, &res->new_cinfo);
2798 nfs_post_op_update_inode(new_dir, res->new_fattr);
2799 return 1;
2800}
2801
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2803 struct inode *new_dir, struct qstr *new_name)
2804{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002805 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002806 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 .old_dir = NFS_FH(old_dir),
2808 .new_dir = NFS_FH(new_dir),
2809 .old_name = old_name,
2810 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002811 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002813 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002814 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002815 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 struct rpc_message msg = {
2817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2818 .rpc_argp = &arg,
2819 .rpc_resp = &res,
2820 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002821 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Trond Myklebust011fff72010-04-16 16:22:49 -04002823 res.old_fattr = nfs_alloc_fattr();
2824 res.new_fattr = nfs_alloc_fattr();
2825 if (res.old_fattr == NULL || res.new_fattr == NULL)
2826 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
Bryan Schumaker7c513052011-03-24 17:12:24 +00002828 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 if (!status) {
2830 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002831 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002833 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002835out:
2836 nfs_free_fattr(res.new_fattr);
2837 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 return status;
2839}
2840
2841static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2842 struct inode *new_dir, struct qstr *new_name)
2843{
2844 struct nfs4_exception exception = { };
2845 int err;
2846 do {
2847 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2848 _nfs4_proc_rename(old_dir, old_name,
2849 new_dir, new_name),
2850 &exception);
2851 } while (exception.retry);
2852 return err;
2853}
2854
2855static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2856{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002857 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 struct nfs4_link_arg arg = {
2859 .fh = NFS_FH(inode),
2860 .dir_fh = NFS_FH(dir),
2861 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002862 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002864 struct nfs4_link_res res = {
2865 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002866 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 struct rpc_message msg = {
2868 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2869 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002870 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002872 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Trond Myklebust136f2622010-04-16 16:22:49 -04002874 res.fattr = nfs_alloc_fattr();
2875 res.dir_attr = nfs_alloc_fattr();
2876 if (res.fattr == NULL || res.dir_attr == NULL)
2877 goto out;
2878
Bryan Schumaker7c513052011-03-24 17:12:24 +00002879 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002880 if (!status) {
2881 update_changeattr(dir, &res.cinfo);
2882 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002883 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002884 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002885out:
2886 nfs_free_fattr(res.dir_attr);
2887 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 return status;
2889}
2890
2891static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2892{
2893 struct nfs4_exception exception = { };
2894 int err;
2895 do {
2896 err = nfs4_handle_exception(NFS_SERVER(inode),
2897 _nfs4_proc_link(inode, dir, name),
2898 &exception);
2899 } while (exception.retry);
2900 return err;
2901}
2902
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002903struct nfs4_createdata {
2904 struct rpc_message msg;
2905 struct nfs4_create_arg arg;
2906 struct nfs4_create_res res;
2907 struct nfs_fh fh;
2908 struct nfs_fattr fattr;
2909 struct nfs_fattr dir_fattr;
2910};
2911
2912static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2913 struct qstr *name, struct iattr *sattr, u32 ftype)
2914{
2915 struct nfs4_createdata *data;
2916
2917 data = kzalloc(sizeof(*data), GFP_KERNEL);
2918 if (data != NULL) {
2919 struct nfs_server *server = NFS_SERVER(dir);
2920
2921 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2922 data->msg.rpc_argp = &data->arg;
2923 data->msg.rpc_resp = &data->res;
2924 data->arg.dir_fh = NFS_FH(dir);
2925 data->arg.server = server;
2926 data->arg.name = name;
2927 data->arg.attrs = sattr;
2928 data->arg.ftype = ftype;
2929 data->arg.bitmask = server->attr_bitmask;
2930 data->res.server = server;
2931 data->res.fh = &data->fh;
2932 data->res.fattr = &data->fattr;
2933 data->res.dir_fattr = &data->dir_fattr;
2934 nfs_fattr_init(data->res.fattr);
2935 nfs_fattr_init(data->res.dir_fattr);
2936 }
2937 return data;
2938}
2939
2940static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2941{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002942 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002943 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002944 if (status == 0) {
2945 update_changeattr(dir, &data->res.dir_cinfo);
2946 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2947 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2948 }
2949 return status;
2950}
2951
2952static void nfs4_free_createdata(struct nfs4_createdata *data)
2953{
2954 kfree(data);
2955}
2956
Chuck Lever4f390c12006-08-22 20:06:22 -04002957static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002958 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002960 struct nfs4_createdata *data;
2961 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
Chuck Lever94a6d752006-08-22 20:06:23 -04002963 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002964 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002965
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002966 status = -ENOMEM;
2967 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2968 if (data == NULL)
2969 goto out;
2970
2971 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2972 data->arg.u.symlink.pages = &page;
2973 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002975 status = nfs4_do_create(dir, dentry, data);
2976
2977 nfs4_free_createdata(data);
2978out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 return status;
2980}
2981
Chuck Lever4f390c12006-08-22 20:06:22 -04002982static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002983 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984{
2985 struct nfs4_exception exception = { };
2986 int err;
2987 do {
2988 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002989 _nfs4_proc_symlink(dir, dentry, page,
2990 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 &exception);
2992 } while (exception.retry);
2993 return err;
2994}
2995
2996static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2997 struct iattr *sattr)
2998{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002999 struct nfs4_createdata *data;
3000 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003002 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3003 if (data == NULL)
3004 goto out;
3005
3006 status = nfs4_do_create(dir, dentry, data);
3007
3008 nfs4_free_createdata(data);
3009out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 return status;
3011}
3012
3013static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3014 struct iattr *sattr)
3015{
3016 struct nfs4_exception exception = { };
3017 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003018
3019 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 do {
3021 err = nfs4_handle_exception(NFS_SERVER(dir),
3022 _nfs4_proc_mkdir(dir, dentry, sattr),
3023 &exception);
3024 } while (exception.retry);
3025 return err;
3026}
3027
3028static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003029 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
3031 struct inode *dir = dentry->d_inode;
3032 struct nfs4_readdir_arg args = {
3033 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003034 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 .pgbase = 0,
3036 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003037 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003038 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 };
3040 struct nfs4_readdir_res res;
3041 struct rpc_message msg = {
3042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3043 .rpc_argp = &args,
3044 .rpc_resp = &res,
3045 .rpc_cred = cred,
3046 };
3047 int status;
3048
Harvey Harrison3110ff82008-05-02 13:42:44 -07003049 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003050 dentry->d_parent->d_name.name,
3051 dentry->d_name.name,
3052 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3054 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003055 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 -05003056 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003058 status += args.pgbase;
3059 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003060
3061 nfs_invalidate_atime(dir);
3062
Harvey Harrison3110ff82008-05-02 13:42:44 -07003063 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 return status;
3065}
3066
3067static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003068 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
3070 struct nfs4_exception exception = { };
3071 int err;
3072 do {
3073 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3074 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003075 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 &exception);
3077 } while (exception.retry);
3078 return err;
3079}
3080
3081static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3082 struct iattr *sattr, dev_t rdev)
3083{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003084 struct nfs4_createdata *data;
3085 int mode = sattr->ia_mode;
3086 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
3088 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3089 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003090
3091 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3092 if (data == NULL)
3093 goto out;
3094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003096 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003098 data->arg.ftype = NF4BLK;
3099 data->arg.u.device.specdata1 = MAJOR(rdev);
3100 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 }
3102 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003103 data->arg.ftype = NF4CHR;
3104 data->arg.u.device.specdata1 = MAJOR(rdev);
3105 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003108 status = nfs4_do_create(dir, dentry, data);
3109
3110 nfs4_free_createdata(data);
3111out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 return status;
3113}
3114
3115static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3116 struct iattr *sattr, dev_t rdev)
3117{
3118 struct nfs4_exception exception = { };
3119 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003120
3121 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 do {
3123 err = nfs4_handle_exception(NFS_SERVER(dir),
3124 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3125 &exception);
3126 } while (exception.retry);
3127 return err;
3128}
3129
3130static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3131 struct nfs_fsstat *fsstat)
3132{
3133 struct nfs4_statfs_arg args = {
3134 .fh = fhandle,
3135 .bitmask = server->attr_bitmask,
3136 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003137 struct nfs4_statfs_res res = {
3138 .fsstat = fsstat,
3139 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 struct rpc_message msg = {
3141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3142 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003143 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 };
3145
Trond Myklebust0e574af2005-10-27 22:12:38 -04003146 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003147 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148}
3149
3150static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3151{
3152 struct nfs4_exception exception = { };
3153 int err;
3154 do {
3155 err = nfs4_handle_exception(server,
3156 _nfs4_proc_statfs(server, fhandle, fsstat),
3157 &exception);
3158 } while (exception.retry);
3159 return err;
3160}
3161
3162static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3163 struct nfs_fsinfo *fsinfo)
3164{
3165 struct nfs4_fsinfo_arg args = {
3166 .fh = fhandle,
3167 .bitmask = server->attr_bitmask,
3168 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003169 struct nfs4_fsinfo_res res = {
3170 .fsinfo = fsinfo,
3171 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 struct rpc_message msg = {
3173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3174 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003175 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 };
3177
Bryan Schumaker7c513052011-03-24 17:12:24 +00003178 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179}
3180
3181static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3182{
3183 struct nfs4_exception exception = { };
3184 int err;
3185
3186 do {
3187 err = nfs4_handle_exception(server,
3188 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3189 &exception);
3190 } while (exception.retry);
3191 return err;
3192}
3193
3194static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3195{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003196 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3198}
3199
3200static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3201 struct nfs_pathconf *pathconf)
3202{
3203 struct nfs4_pathconf_arg args = {
3204 .fh = fhandle,
3205 .bitmask = server->attr_bitmask,
3206 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003207 struct nfs4_pathconf_res res = {
3208 .pathconf = pathconf,
3209 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 struct rpc_message msg = {
3211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3212 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003213 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 };
3215
3216 /* None of the pathconf attributes are mandatory to implement */
3217 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3218 memset(pathconf, 0, sizeof(*pathconf));
3219 return 0;
3220 }
3221
Trond Myklebust0e574af2005-10-27 22:12:38 -04003222 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003223 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224}
3225
3226static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3227 struct nfs_pathconf *pathconf)
3228{
3229 struct nfs4_exception exception = { };
3230 int err;
3231
3232 do {
3233 err = nfs4_handle_exception(server,
3234 _nfs4_proc_pathconf(server, fhandle, pathconf),
3235 &exception);
3236 } while (exception.retry);
3237 return err;
3238}
3239
Benny Halevyd20581a2011-05-22 19:52:03 +03003240void __nfs4_read_done_cb(struct nfs_read_data *data)
3241{
3242 nfs_invalidate_atime(data->inode);
3243}
3244
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003245static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246{
Trond Myklebustec06c092006-03-20 13:44:27 -05003247 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003249 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003250 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003251 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003253
Benny Halevyd20581a2011-05-22 19:52:03 +03003254 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003256 renew_lease(server, data->timestamp);
3257 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
3259
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003260static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3261{
3262
3263 dprintk("--> %s\n", __func__);
3264
3265 if (!nfs4_sequence_done(task, &data->res.seq_res))
3266 return -EAGAIN;
3267
Benny Halevyd20581a2011-05-22 19:52:03 +03003268 return data->read_done_cb ? data->read_done_cb(task, data) :
3269 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003270}
3271
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003272static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003275 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003276 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003277 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278}
3279
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003280/* Reset the the nfs_read_data to send the read to the MDS. */
3281void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3282{
3283 dprintk("%s Reset task for i/o through\n", __func__);
3284 put_lseg(data->lseg);
3285 data->lseg = NULL;
3286 /* offsets will differ in the dense stripe case */
3287 data->args.offset = data->mds_offset;
3288 data->ds_clp = NULL;
3289 data->args.fh = NFS_FH(data->inode);
3290 data->read_done_cb = nfs4_read_done_cb;
3291 task->tk_ops = data->mds_ops;
3292 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3293}
3294EXPORT_SYMBOL_GPL(nfs4_reset_read);
3295
Fred Isamanb029bc92011-03-03 15:13:42 +00003296static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 struct inode *inode = data->inode;
3299
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003300 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003301 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003302 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003304 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003306 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003307 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003308 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309}
3310
Fred Isamanb029bc92011-03-03 15:13:42 +00003311static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3312{
3313 if (!nfs4_sequence_done(task, &data->res.seq_res))
3314 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003315 return data->write_done_cb ? data->write_done_cb(task, data) :
3316 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003317}
3318
Fred Isamana69aef12011-03-03 15:13:47 +00003319/* Reset the the nfs_write_data to send the write to the MDS. */
3320void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3321{
3322 dprintk("%s Reset task for i/o through\n", __func__);
3323 put_lseg(data->lseg);
3324 data->lseg = NULL;
3325 data->ds_clp = NULL;
3326 data->write_done_cb = nfs4_write_done_cb;
3327 data->args.fh = NFS_FH(data->inode);
3328 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3329 data->args.offset = data->mds_offset;
3330 data->res.fattr = &data->fattr;
3331 task->tk_ops = data->mds_ops;
3332 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3333}
3334EXPORT_SYMBOL_GPL(nfs4_reset_write);
3335
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003336static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003338 struct nfs_server *server = NFS_SERVER(data->inode);
3339
Fred Isaman7ffd1062011-03-03 15:13:46 +00003340 if (data->lseg) {
3341 data->args.bitmask = NULL;
3342 data->res.fattr = NULL;
3343 } else
3344 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003345 if (!data->write_done_cb)
3346 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003347 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 data->timestamp = jiffies;
3349
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003350 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003351 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352}
3353
Fred Isaman5f452432011-03-23 13:27:46 +00003354static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003357
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003358 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003359 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003360 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003362 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364}
3365
Fred Isaman5f452432011-03-23 13:27:46 +00003366static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3367{
3368 if (!nfs4_sequence_done(task, &data->res.seq_res))
3369 return -EAGAIN;
3370 return data->write_done_cb(task, data);
3371}
3372
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003373static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003375 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003376
3377 if (data->lseg) {
3378 data->args.bitmask = NULL;
3379 data->res.fattr = NULL;
3380 } else
3381 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003382 if (!data->write_done_cb)
3383 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003384 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003385 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003386 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387}
3388
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003389struct nfs4_renewdata {
3390 struct nfs_client *client;
3391 unsigned long timestamp;
3392};
3393
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394/*
3395 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3396 * standalone procedure for queueing an asynchronous RENEW.
3397 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003398static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003399{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003400 struct nfs4_renewdata *data = calldata;
3401 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003402
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003403 if (atomic_read(&clp->cl_count) > 1)
3404 nfs4_schedule_state_renewal(clp);
3405 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003406 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003407}
3408
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003409static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003411 struct nfs4_renewdata *data = calldata;
3412 struct nfs_client *clp = data->client;
3413 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
3415 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003416 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003417 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3418 return;
3419 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003420 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003421 return;
3422 }
3423 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003425 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426}
3427
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003428static const struct rpc_call_ops nfs4_renew_ops = {
3429 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003430 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003431};
3432
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003433static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434{
3435 struct rpc_message msg = {
3436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3437 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003438 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003440 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003442 if (renew_flags == 0)
3443 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003444 if (!atomic_inc_not_zero(&clp->cl_count))
3445 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003446 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003447 if (data == NULL)
3448 return -ENOMEM;
3449 data->client = clp;
3450 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003452 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453}
3454
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003455static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456{
3457 struct rpc_message msg = {
3458 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3459 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003460 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 };
3462 unsigned long now = jiffies;
3463 int status;
3464
3465 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3466 if (status < 0)
3467 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003468 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 return 0;
3470}
3471
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003472static inline int nfs4_server_supports_acls(struct nfs_server *server)
3473{
3474 return (server->caps & NFS_CAP_ACLS)
3475 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3476 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3477}
3478
3479/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3480 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3481 * the stack.
3482 */
3483#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3484
Neil Hormane9e3d722011-03-04 19:26:03 -05003485static int buf_to_pages_noslab(const void *buf, size_t buflen,
3486 struct page **pages, unsigned int *pgbase)
3487{
3488 struct page *newpage, **spages;
3489 int rc = 0;
3490 size_t len;
3491 spages = pages;
3492
3493 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003494 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003495 newpage = alloc_page(GFP_KERNEL);
3496
3497 if (newpage == NULL)
3498 goto unwind;
3499 memcpy(page_address(newpage), buf, len);
3500 buf += len;
3501 buflen -= len;
3502 *pages++ = newpage;
3503 rc++;
3504 } while (buflen != 0);
3505
3506 return rc;
3507
3508unwind:
3509 for(; rc > 0; rc--)
3510 __free_page(spages[rc-1]);
3511 return -ENOMEM;
3512}
3513
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003514struct nfs4_cached_acl {
3515 int cached;
3516 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003517 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003518};
3519
3520static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003521{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003522 struct nfs_inode *nfsi = NFS_I(inode);
3523
3524 spin_lock(&inode->i_lock);
3525 kfree(nfsi->nfs4_acl);
3526 nfsi->nfs4_acl = acl;
3527 spin_unlock(&inode->i_lock);
3528}
3529
3530static void nfs4_zap_acl_attr(struct inode *inode)
3531{
3532 nfs4_set_cached_acl(inode, NULL);
3533}
3534
3535static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3536{
3537 struct nfs_inode *nfsi = NFS_I(inode);
3538 struct nfs4_cached_acl *acl;
3539 int ret = -ENOENT;
3540
3541 spin_lock(&inode->i_lock);
3542 acl = nfsi->nfs4_acl;
3543 if (acl == NULL)
3544 goto out;
3545 if (buf == NULL) /* user is just asking for length */
3546 goto out_len;
3547 if (acl->cached == 0)
3548 goto out;
3549 ret = -ERANGE; /* see getxattr(2) man page */
3550 if (acl->len > buflen)
3551 goto out;
3552 memcpy(buf, acl->data, acl->len);
3553out_len:
3554 ret = acl->len;
3555out:
3556 spin_unlock(&inode->i_lock);
3557 return ret;
3558}
3559
3560static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3561{
3562 struct nfs4_cached_acl *acl;
3563
3564 if (buf && acl_len <= PAGE_SIZE) {
3565 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3566 if (acl == NULL)
3567 goto out;
3568 acl->cached = 1;
3569 memcpy(acl->data, buf, acl_len);
3570 } else {
3571 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3572 if (acl == NULL)
3573 goto out;
3574 acl->cached = 0;
3575 }
3576 acl->len = acl_len;
3577out:
3578 nfs4_set_cached_acl(inode, acl);
3579}
3580
Andy Adamsonbf118a32011-12-07 11:55:27 -05003581/*
3582 * The getxattr API returns the required buffer length when called with a
3583 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3584 * the required buf. On a NULL buf, we send a page of data to the server
3585 * guessing that the ACL request can be serviced by a page. If so, we cache
3586 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3587 * the cache. If not so, we throw away the page, and cache the required
3588 * length. The next getxattr call will then produce another round trip to
3589 * the server, this time with the input buf of the required size.
3590 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003591static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003592{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003593 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003594 struct nfs_getaclargs args = {
3595 .fh = NFS_FH(inode),
3596 .acl_pages = pages,
3597 .acl_len = buflen,
3598 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003599 struct nfs_getaclres res = {
3600 .acl_len = buflen,
3601 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003602 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003603 struct rpc_message msg = {
3604 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3605 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003606 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003607 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003608 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003609
Andy Adamsonbf118a32011-12-07 11:55:27 -05003610 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3611 /* As long as we're doing a round trip to the server anyway,
3612 * let's be prepared for a page of acl data. */
3613 if (npages == 0)
3614 npages = 1;
3615
3616 for (i = 0; i < npages; i++) {
3617 pages[i] = alloc_page(GFP_KERNEL);
3618 if (!pages[i])
3619 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003620 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003621 if (npages > 1) {
3622 /* for decoding across pages */
3623 args.acl_scratch = alloc_page(GFP_KERNEL);
3624 if (!args.acl_scratch)
3625 goto out_free;
3626 }
3627 args.acl_len = npages * PAGE_SIZE;
3628 args.acl_pgbase = 0;
3629 /* Let decode_getfacl know not to fail if the ACL data is larger than
3630 * the page we send as a guess */
3631 if (buf == NULL)
3632 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3633 resp_buf = page_address(pages[0]);
3634
Peng Taode040be2012-01-10 22:42:47 +08003635 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003636 __func__, buf, buflen, npages, args.acl_len);
3637 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3638 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003639 if (ret)
3640 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003641
3642 acl_len = res.acl_len - res.acl_data_offset;
3643 if (acl_len > args.acl_len)
3644 nfs4_write_cached_acl(inode, NULL, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003645 else
Andy Adamsonbf118a32011-12-07 11:55:27 -05003646 nfs4_write_cached_acl(inode, resp_buf + res.acl_data_offset,
3647 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003648 if (buf) {
3649 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003650 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003651 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003652 _copy_from_pages(buf, pages, res.acl_data_offset,
3653 res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003654 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003655 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003656out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003657 for (i = 0; i < npages; i++)
3658 if (pages[i])
3659 __free_page(pages[i]);
3660 if (args.acl_scratch)
3661 __free_page(args.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003662 return ret;
3663}
3664
Trond Myklebust16b42892006-08-24 12:27:15 -04003665static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3666{
3667 struct nfs4_exception exception = { };
3668 ssize_t ret;
3669 do {
3670 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3671 if (ret >= 0)
3672 break;
3673 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3674 } while (exception.retry);
3675 return ret;
3676}
3677
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003678static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3679{
3680 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003681 int ret;
3682
3683 if (!nfs4_server_supports_acls(server))
3684 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003685 ret = nfs_revalidate_inode(server, inode);
3686 if (ret < 0)
3687 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003688 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3689 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003690 ret = nfs4_read_cached_acl(inode, buf, buflen);
3691 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003692 /* -ENOENT is returned if there is no ACL or if there is an ACL
3693 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003694 return ret;
3695 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003696}
3697
Trond Myklebust16b42892006-08-24 12:27:15 -04003698static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003699{
3700 struct nfs_server *server = NFS_SERVER(inode);
3701 struct page *pages[NFS4ACL_MAXPAGES];
3702 struct nfs_setaclargs arg = {
3703 .fh = NFS_FH(inode),
3704 .acl_pages = pages,
3705 .acl_len = buflen,
3706 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003707 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003708 struct rpc_message msg = {
3709 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3710 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003711 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003712 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003713 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003714
3715 if (!nfs4_server_supports_acls(server))
3716 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003717 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3718 if (i < 0)
3719 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003720 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003721 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003722
3723 /*
3724 * Free each page after tx, so the only ref left is
3725 * held by the network stack
3726 */
3727 for (; i > 0; i--)
3728 put_page(pages[i-1]);
3729
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003730 /*
3731 * Acl update can result in inode attribute update.
3732 * so mark the attribute cache invalid.
3733 */
3734 spin_lock(&inode->i_lock);
3735 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3736 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003737 nfs_access_zap_cache(inode);
3738 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003739 return ret;
3740}
3741
Trond Myklebust16b42892006-08-24 12:27:15 -04003742static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3743{
3744 struct nfs4_exception exception = { };
3745 int err;
3746 do {
3747 err = nfs4_handle_exception(NFS_SERVER(inode),
3748 __nfs4_proc_set_acl(inode, buf, buflen),
3749 &exception);
3750 } while (exception.retry);
3751 return err;
3752}
3753
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003755nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003757 struct nfs_client *clp = server->nfs_client;
3758
3759 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 return 0;
3761 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003762 case -NFS4ERR_ADMIN_REVOKED:
3763 case -NFS4ERR_BAD_STATEID:
3764 case -NFS4ERR_OPENMODE:
3765 if (state == NULL)
3766 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003767 nfs4_schedule_stateid_recovery(server, state);
3768 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003769 case -NFS4ERR_EXPIRED:
3770 if (state != NULL)
3771 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003773 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003774 nfs4_schedule_lease_recovery(clp);
3775 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003776#if defined(CONFIG_NFS_V4_1)
3777 case -NFS4ERR_BADSESSION:
3778 case -NFS4ERR_BADSLOT:
3779 case -NFS4ERR_BAD_HIGH_SLOT:
3780 case -NFS4ERR_DEADSESSION:
3781 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3782 case -NFS4ERR_SEQ_FALSE_RETRY:
3783 case -NFS4ERR_SEQ_MISORDERED:
3784 dprintk("%s ERROR %d, Reset session\n", __func__,
3785 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003786 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003787 task->tk_status = 0;
3788 return -EAGAIN;
3789#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003791 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003792 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003793 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3795 task->tk_status = 0;
3796 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003797 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 case -NFS4ERR_OLD_STATEID:
3799 task->tk_status = 0;
3800 return -EAGAIN;
3801 }
3802 task->tk_status = nfs4_map_errors(task->tk_status);
3803 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003804wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003805 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003806 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3807 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3808 task->tk_status = 0;
3809 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810}
3811
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003812int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3813 unsigned short port, struct rpc_cred *cred,
3814 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815{
3816 nfs4_verifier sc_verifier;
3817 struct nfs4_setclientid setclientid = {
3818 .sc_verifier = &sc_verifier,
3819 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003820 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 };
3822 struct rpc_message msg = {
3823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3824 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003825 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003826 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 };
Al Virobc4785c2006-10-19 23:28:51 -07003828 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 int loop = 0;
3830 int status;
3831
Al Virobc4785c2006-10-19 23:28:51 -07003832 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3834 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3835
3836 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05003837 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003839 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003840 clp->cl_ipaddr,
3841 rpc_peeraddr2str(clp->cl_rpcclient,
3842 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003843 rpc_peeraddr2str(clp->cl_rpcclient,
3844 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003845 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 clp->cl_id_uniquifier);
3847 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003848 sizeof(setclientid.sc_netid),
3849 rpc_peeraddr2str(clp->cl_rpcclient,
3850 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003852 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05003854 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003856 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 if (status != -NFS4ERR_CLID_INUSE)
3858 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003859 if (loop != 0) {
3860 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003862 }
3863 ++loop;
3864 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 }
3866 return status;
3867}
3868
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003869int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003870 struct nfs4_setclientid_res *arg,
3871 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872{
3873 struct nfs_fsinfo fsinfo;
3874 struct rpc_message msg = {
3875 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003876 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003878 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 };
3880 unsigned long now;
3881 int status;
3882
3883 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003884 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 if (status == 0) {
3886 spin_lock(&clp->cl_lock);
3887 clp->cl_lease_time = fsinfo.lease_time * HZ;
3888 clp->cl_last_renewal = now;
3889 spin_unlock(&clp->cl_lock);
3890 }
3891 return status;
3892}
3893
Trond Myklebustfe650402006-01-03 09:55:18 +01003894struct nfs4_delegreturndata {
3895 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003896 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003897 struct nfs_fh fh;
3898 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003899 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003900 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003901 int rpc_status;
3902};
3903
Trond Myklebustfe650402006-01-03 09:55:18 +01003904static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3905{
3906 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003907
Trond Myklebust14516c32010-07-31 14:29:06 -04003908 if (!nfs4_sequence_done(task, &data->res.seq_res))
3909 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003910
Ricardo Labiaga79708862009-12-07 09:23:21 -05003911 switch (task->tk_status) {
3912 case -NFS4ERR_STALE_STATEID:
3913 case -NFS4ERR_EXPIRED:
3914 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003915 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003916 break;
3917 default:
3918 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3919 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003920 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003921 return;
3922 }
3923 }
3924 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003925}
3926
3927static void nfs4_delegreturn_release(void *calldata)
3928{
Trond Myklebustfe650402006-01-03 09:55:18 +01003929 kfree(calldata);
3930}
3931
Andy Adamson938e1012009-04-01 09:22:28 -04003932#if defined(CONFIG_NFS_V4_1)
3933static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3934{
3935 struct nfs4_delegreturndata *d_data;
3936
3937 d_data = (struct nfs4_delegreturndata *)data;
3938
Trond Myklebust035168ab2010-06-16 09:52:26 -04003939 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003940 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003941 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04003942 return;
3943 rpc_call_start(task);
3944}
3945#endif /* CONFIG_NFS_V4_1 */
3946
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003947static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003948#if defined(CONFIG_NFS_V4_1)
3949 .rpc_call_prepare = nfs4_delegreturn_prepare,
3950#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003951 .rpc_call_done = nfs4_delegreturn_done,
3952 .rpc_release = nfs4_delegreturn_release,
3953};
3954
Trond Myklebuste6f81072008-01-24 18:14:34 -05003955static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003956{
3957 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003958 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003959 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003960 struct rpc_message msg = {
3961 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3962 .rpc_cred = cred,
3963 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003964 struct rpc_task_setup task_setup_data = {
3965 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003966 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003967 .callback_ops = &nfs4_delegreturn_ops,
3968 .flags = RPC_TASK_ASYNC,
3969 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003970 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003971
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003972 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003973 if (data == NULL)
3974 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003975 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01003976 data->args.fhandle = &data->fh;
3977 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003978 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003979 nfs_copy_fh(&data->fh, NFS_FH(inode));
3980 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003981 data->res.fattr = &data->fattr;
3982 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003983 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003984 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003985 data->rpc_status = 0;
3986
Trond Myklebustc970aa82007-07-14 15:39:59 -04003987 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003988 msg.rpc_argp = &data->args;
3989 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003990 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003991 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003992 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003993 if (!issync)
3994 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003995 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003996 if (status != 0)
3997 goto out;
3998 status = data->rpc_status;
3999 if (status != 0)
4000 goto out;
4001 nfs_refresh_inode(inode, &data->fattr);
4002out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004003 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004004 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005}
4006
Trond Myklebuste6f81072008-01-24 18:14:34 -05004007int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008{
4009 struct nfs_server *server = NFS_SERVER(inode);
4010 struct nfs4_exception exception = { };
4011 int err;
4012 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004013 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 switch (err) {
4015 case -NFS4ERR_STALE_STATEID:
4016 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 case 0:
4018 return 0;
4019 }
4020 err = nfs4_handle_exception(server, err, &exception);
4021 } while (exception.retry);
4022 return err;
4023}
4024
4025#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4026#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4027
4028/*
4029 * sleep, with exponential backoff, and retry the LOCK operation.
4030 */
4031static unsigned long
4032nfs4_set_lock_task_retry(unsigned long timeout)
4033{
Jeff Laytond3103102011-12-01 22:44:39 +01004034 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 timeout <<= 1;
4036 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4037 return NFS4_LOCK_MAXTIMEOUT;
4038 return timeout;
4039}
4040
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4042{
4043 struct inode *inode = state->inode;
4044 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004045 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004046 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004048 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004050 struct nfs_lockt_res res = {
4051 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 };
4053 struct rpc_message msg = {
4054 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4055 .rpc_argp = &arg,
4056 .rpc_resp = &res,
4057 .rpc_cred = state->owner->so_cred,
4058 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 struct nfs4_lock_state *lsp;
4060 int status;
4061
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004062 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004063 status = nfs4_set_lock_state(state, request);
4064 if (status != 0)
4065 goto out;
4066 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004067 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004068 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004069 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004070 switch (status) {
4071 case 0:
4072 request->fl_type = F_UNLCK;
4073 break;
4074 case -NFS4ERR_DENIED:
4075 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004077 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004078out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 return status;
4080}
4081
4082static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4083{
4084 struct nfs4_exception exception = { };
4085 int err;
4086
4087 do {
4088 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4089 _nfs4_proc_getlk(state, cmd, request),
4090 &exception);
4091 } while (exception.retry);
4092 return err;
4093}
4094
4095static int do_vfs_lock(struct file *file, struct file_lock *fl)
4096{
4097 int res = 0;
4098 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4099 case FL_POSIX:
4100 res = posix_lock_file_wait(file, fl);
4101 break;
4102 case FL_FLOCK:
4103 res = flock_lock_file_wait(file, fl);
4104 break;
4105 default:
4106 BUG();
4107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 return res;
4109}
4110
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004111struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004112 struct nfs_locku_args arg;
4113 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004114 struct nfs4_lock_state *lsp;
4115 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004116 struct file_lock fl;
4117 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004118 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004119};
4120
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004121static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4122 struct nfs_open_context *ctx,
4123 struct nfs4_lock_state *lsp,
4124 struct nfs_seqid *seqid)
4125{
4126 struct nfs4_unlockdata *p;
4127 struct inode *inode = lsp->ls_state->inode;
4128
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004129 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004130 if (p == NULL)
4131 return NULL;
4132 p->arg.fh = NFS_FH(inode);
4133 p->arg.fl = &p->fl;
4134 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004135 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004136 p->arg.stateid = &lsp->ls_stateid;
4137 p->lsp = lsp;
4138 atomic_inc(&lsp->ls_count);
4139 /* Ensure we don't close file until we're done freeing locks! */
4140 p->ctx = get_nfs_open_context(ctx);
4141 memcpy(&p->fl, fl, sizeof(p->fl));
4142 p->server = NFS_SERVER(inode);
4143 return p;
4144}
4145
Trond Myklebust06f814a2006-01-03 09:55:07 +01004146static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004147{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004148 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004149 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004150 nfs4_put_lock_state(calldata->lsp);
4151 put_nfs_open_context(calldata->ctx);
4152 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004153}
4154
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004155static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004156{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004157 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004158
Trond Myklebust14516c32010-07-31 14:29:06 -04004159 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4160 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004161 switch (task->tk_status) {
4162 case 0:
4163 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004164 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004165 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004166 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004167 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004168 case -NFS4ERR_BAD_STATEID:
4169 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004170 case -NFS4ERR_STALE_STATEID:
4171 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004172 break;
4173 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004174 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004175 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004176 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004177}
4178
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004179static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004180{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004181 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004183 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004184 return;
4185 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004186 /* Note: exit _without_ running nfs4_locku_done */
4187 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004188 return;
4189 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004190 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004191 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004192 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004193 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004194 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004195 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196}
4197
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004198static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004199 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004200 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004201 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004202};
4203
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004204static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4205 struct nfs_open_context *ctx,
4206 struct nfs4_lock_state *lsp,
4207 struct nfs_seqid *seqid)
4208{
4209 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004210 struct rpc_message msg = {
4211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4212 .rpc_cred = ctx->cred,
4213 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004214 struct rpc_task_setup task_setup_data = {
4215 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004216 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004217 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004218 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004219 .flags = RPC_TASK_ASYNC,
4220 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004221
Frank Filz137d6ac2007-07-09 15:32:29 -07004222 /* Ensure this is an unlock - when canceling a lock, the
4223 * canceled lock is passed in, and it won't be an unlock.
4224 */
4225 fl->fl_type = F_UNLCK;
4226
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004227 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4228 if (data == NULL) {
4229 nfs_free_seqid(seqid);
4230 return ERR_PTR(-ENOMEM);
4231 }
4232
Trond Myklebust9d12b212012-01-17 22:04:25 -05004233 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004234 msg.rpc_argp = &data->arg;
4235 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004236 task_setup_data.callback_data = data;
4237 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004238}
4239
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4241{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004242 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004243 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004244 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004245 struct rpc_task *task;
4246 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004247 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Trond Myklebust9b073572006-06-29 16:38:34 -04004249 status = nfs4_set_lock_state(state, request);
4250 /* Unlock _before_ we do the RPC call */
4251 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004252 down_read(&nfsi->rwsem);
4253 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4254 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004255 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004256 }
4257 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004258 if (status != 0)
4259 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004260 /* Is this a delegated lock? */
4261 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004262 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004263 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004264 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004265 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004266 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004267 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004268 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004269 status = PTR_ERR(task);
4270 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004271 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004272 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004273 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004274out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004275 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004276 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277}
4278
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004279struct nfs4_lockdata {
4280 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004281 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004282 struct nfs4_lock_state *lsp;
4283 struct nfs_open_context *ctx;
4284 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004285 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004286 int rpc_status;
4287 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004288 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004289};
4290
4291static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004292 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4293 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004294{
4295 struct nfs4_lockdata *p;
4296 struct inode *inode = lsp->ls_state->inode;
4297 struct nfs_server *server = NFS_SERVER(inode);
4298
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004299 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004300 if (p == NULL)
4301 return NULL;
4302
4303 p->arg.fh = NFS_FH(inode);
4304 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004305 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004306 if (p->arg.open_seqid == NULL)
4307 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004308 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004309 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004310 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004311 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004312 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004313 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004314 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004315 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004316 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004317 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004318 atomic_inc(&lsp->ls_count);
4319 p->ctx = get_nfs_open_context(ctx);
4320 memcpy(&p->fl, fl, sizeof(p->fl));
4321 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004322out_free_seqid:
4323 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004324out_free:
4325 kfree(p);
4326 return NULL;
4327}
4328
4329static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4330{
4331 struct nfs4_lockdata *data = calldata;
4332 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
Harvey Harrison3110ff82008-05-02 13:42:44 -07004334 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004335 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4336 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004337 /* Do we need to do an open_to_lock_owner? */
4338 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004339 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4340 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004341 data->arg.open_stateid = &state->stateid;
4342 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004343 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004344 } else
4345 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004346 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004347 if (nfs4_setup_sequence(data->server,
4348 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004349 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004350 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004351 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004352 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004353}
4354
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004355static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4356{
4357 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4358 nfs4_lock_prepare(task, calldata);
4359}
4360
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004361static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4362{
4363 struct nfs4_lockdata *data = calldata;
4364
Harvey Harrison3110ff82008-05-02 13:42:44 -07004365 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004366
Trond Myklebust14516c32010-07-31 14:29:06 -04004367 if (!nfs4_sequence_done(task, &data->res.seq_res))
4368 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004369
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004370 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004371 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004372 if (data->rpc_status == 0)
4373 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4374 else
4375 goto out;
4376 }
4377 if (data->rpc_status == 0) {
4378 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4379 sizeof(data->lsp->ls_stateid.data));
4380 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004381 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004382 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004383out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004384 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004385}
4386
4387static void nfs4_lock_release(void *calldata)
4388{
4389 struct nfs4_lockdata *data = calldata;
4390
Harvey Harrison3110ff82008-05-02 13:42:44 -07004391 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004392 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004393 if (data->cancelled != 0) {
4394 struct rpc_task *task;
4395 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4396 data->arg.lock_seqid);
4397 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004398 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004399 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004400 } else
4401 nfs_free_seqid(data->arg.lock_seqid);
4402 nfs4_put_lock_state(data->lsp);
4403 put_nfs_open_context(data->ctx);
4404 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004405 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004406}
4407
4408static const struct rpc_call_ops nfs4_lock_ops = {
4409 .rpc_call_prepare = nfs4_lock_prepare,
4410 .rpc_call_done = nfs4_lock_done,
4411 .rpc_release = nfs4_lock_release,
4412};
4413
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004414static const struct rpc_call_ops nfs4_recover_lock_ops = {
4415 .rpc_call_prepare = nfs4_recover_lock_prepare,
4416 .rpc_call_done = nfs4_lock_done,
4417 .rpc_release = nfs4_lock_release,
4418};
4419
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004420static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4421{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004422 switch (error) {
4423 case -NFS4ERR_ADMIN_REVOKED:
4424 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004425 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004426 if (new_lock_owner != 0 ||
4427 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004428 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004429 break;
4430 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004431 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004432 case -NFS4ERR_EXPIRED:
4433 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004434 };
4435}
4436
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004437static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004438{
4439 struct nfs4_lockdata *data;
4440 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004441 struct rpc_message msg = {
4442 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4443 .rpc_cred = state->owner->so_cred,
4444 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004445 struct rpc_task_setup task_setup_data = {
4446 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004447 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004448 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004449 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004450 .flags = RPC_TASK_ASYNC,
4451 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004452 int ret;
4453
Harvey Harrison3110ff82008-05-02 13:42:44 -07004454 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004455 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004456 fl->fl_u.nfs4_fl.owner,
4457 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004458 if (data == NULL)
4459 return -ENOMEM;
4460 if (IS_SETLKW(cmd))
4461 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004462 if (recovery_type > NFS_LOCK_NEW) {
4463 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004464 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004465 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4466 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004467 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004468 msg.rpc_argp = &data->arg;
4469 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004470 task_setup_data.callback_data = data;
4471 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004472 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004473 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004474 ret = nfs4_wait_for_completion_rpc_task(task);
4475 if (ret == 0) {
4476 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004477 if (ret)
4478 nfs4_handle_setlk_error(data->server, data->lsp,
4479 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004480 } else
4481 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004482 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004483 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004484 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485}
4486
4487static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4488{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004489 struct nfs_server *server = NFS_SERVER(state->inode);
4490 struct nfs4_exception exception = { };
4491 int err;
4492
4493 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004494 /* Cache the lock if possible... */
4495 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4496 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004497 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004498 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004499 break;
4500 nfs4_handle_exception(server, err, &exception);
4501 } while (exception.retry);
4502 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503}
4504
4505static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4506{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004507 struct nfs_server *server = NFS_SERVER(state->inode);
4508 struct nfs4_exception exception = { };
4509 int err;
4510
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004511 err = nfs4_set_lock_state(state, request);
4512 if (err != 0)
4513 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004514 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004515 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4516 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004517 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004518 switch (err) {
4519 default:
4520 goto out;
4521 case -NFS4ERR_GRACE:
4522 case -NFS4ERR_DELAY:
4523 nfs4_handle_exception(server, err, &exception);
4524 err = 0;
4525 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004526 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004527out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004528 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529}
4530
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004531#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004532static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004533{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004534 int status, ret = NFS_OK;
4535 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004536 struct nfs_server *server = NFS_SERVER(state->inode);
4537
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004538 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4539 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4540 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4541 if (status != NFS_OK) {
4542 nfs41_free_stateid(server, &lsp->ls_stateid);
4543 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4544 ret = status;
4545 }
4546 }
4547 };
4548
4549 return ret;
4550}
4551
4552static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4553{
4554 int status = NFS_OK;
4555
4556 if (test_bit(LK_STATE_IN_USE, &state->flags))
4557 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004558 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004559 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004560 return nfs4_lock_expired(state, request);
4561}
4562#endif
4563
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4565{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004566 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004567 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004568 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004570 if ((fl_flags & FL_POSIX) &&
4571 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4572 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004573 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004574 status = nfs4_set_lock_state(state, request);
4575 if (status != 0)
4576 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004577 request->fl_flags |= FL_ACCESS;
4578 status = do_vfs_lock(request->fl_file, request);
4579 if (status < 0)
4580 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004581 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004582 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004583 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004584 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004585 request->fl_flags = fl_flags & ~FL_SLEEP;
4586 status = do_vfs_lock(request->fl_file, request);
4587 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004588 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004589 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004590 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004591 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004592 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004593 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004594 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004595 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4596 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004597out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004598 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004599out:
4600 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 return status;
4602}
4603
4604static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4605{
4606 struct nfs4_exception exception = { };
4607 int err;
4608
4609 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004610 err = _nfs4_proc_setlk(state, cmd, request);
4611 if (err == -NFS4ERR_DENIED)
4612 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004614 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 } while (exception.retry);
4616 return err;
4617}
4618
4619static int
4620nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4621{
4622 struct nfs_open_context *ctx;
4623 struct nfs4_state *state;
4624 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4625 int status;
4626
4627 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004628 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 state = ctx->state;
4630
4631 if (request->fl_start < 0 || request->fl_end < 0)
4632 return -EINVAL;
4633
Trond Myklebustd9531262009-07-21 19:22:38 -04004634 if (IS_GETLK(cmd)) {
4635 if (state != NULL)
4636 return nfs4_proc_getlk(state, F_GETLK, request);
4637 return 0;
4638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639
4640 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4641 return -EINVAL;
4642
Trond Myklebustd9531262009-07-21 19:22:38 -04004643 if (request->fl_type == F_UNLCK) {
4644 if (state != NULL)
4645 return nfs4_proc_unlck(state, cmd, request);
4646 return 0;
4647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
Trond Myklebustd9531262009-07-21 19:22:38 -04004649 if (state == NULL)
4650 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 do {
4652 status = nfs4_proc_setlk(state, cmd, request);
4653 if ((status != -EAGAIN) || IS_SETLK(cmd))
4654 break;
4655 timeout = nfs4_set_lock_task_retry(timeout);
4656 status = -ERESTARTSYS;
4657 if (signalled())
4658 break;
4659 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 return status;
4661}
4662
Trond Myklebust888e6942005-11-04 15:38:11 -05004663int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4664{
4665 struct nfs_server *server = NFS_SERVER(state->inode);
4666 struct nfs4_exception exception = { };
4667 int err;
4668
4669 err = nfs4_set_lock_state(state, fl);
4670 if (err != 0)
4671 goto out;
4672 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004673 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004674 switch (err) {
4675 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004676 printk(KERN_ERR "NFS: %s: unhandled error "
4677 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004678 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004679 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004680 goto out;
4681 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004682 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004683 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004684 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004685 nfs4_schedule_lease_recovery(server->nfs_client);
4686 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004687 case -NFS4ERR_BADSESSION:
4688 case -NFS4ERR_BADSLOT:
4689 case -NFS4ERR_BAD_HIGH_SLOT:
4690 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4691 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004692 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004693 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004694 case -ERESTARTSYS:
4695 /*
4696 * The show must go on: exit, but mark the
4697 * stateid as needing recovery.
4698 */
4699 case -NFS4ERR_ADMIN_REVOKED:
4700 case -NFS4ERR_BAD_STATEID:
4701 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004702 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004703 err = 0;
4704 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004705 case -EKEYEXPIRED:
4706 /*
4707 * User RPCSEC_GSS context has expired.
4708 * We cannot recover this stateid now, so
4709 * skip it and allow recovery thread to
4710 * proceed.
4711 */
4712 err = 0;
4713 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004714 case -ENOMEM:
4715 case -NFS4ERR_DENIED:
4716 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4717 err = 0;
4718 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004719 case -NFS4ERR_DELAY:
4720 break;
4721 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004722 err = nfs4_handle_exception(server, err, &exception);
4723 } while (exception.retry);
4724out:
4725 return err;
4726}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004727
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004728static void nfs4_release_lockowner_release(void *calldata)
4729{
4730 kfree(calldata);
4731}
4732
4733const struct rpc_call_ops nfs4_release_lockowner_ops = {
4734 .rpc_release = nfs4_release_lockowner_release,
4735};
4736
4737void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4738{
4739 struct nfs_server *server = lsp->ls_state->owner->so_server;
4740 struct nfs_release_lockowner_args *args;
4741 struct rpc_message msg = {
4742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4743 };
4744
4745 if (server->nfs_client->cl_mvops->minor_version != 0)
4746 return;
4747 args = kmalloc(sizeof(*args), GFP_NOFS);
4748 if (!args)
4749 return;
4750 args->lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004751 args->lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004752 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004753 msg.rpc_argp = args;
4754 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4755}
4756
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004757#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4758
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004759static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4760 const void *buf, size_t buflen,
4761 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004762{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004763 if (strcmp(key, "") != 0)
4764 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004765
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004766 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004767}
4768
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004769static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4770 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004771{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004772 if (strcmp(key, "") != 0)
4773 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004774
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004775 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004776}
4777
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004778static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4779 size_t list_len, const char *name,
4780 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004781{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004782 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004783
J. Bruce Fields096455a2006-03-20 23:23:42 -05004784 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4785 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004786
4787 if (list && len <= list_len)
4788 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004789 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004790}
4791
Andy Adamson533eb462011-06-13 18:25:56 -04004792/*
4793 * nfs_fhget will use either the mounted_on_fileid or the fileid
4794 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004795static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4796{
Andy Adamson533eb462011-06-13 18:25:56 -04004797 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4798 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4799 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4800 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004801 return;
4802
4803 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4804 NFS_ATTR_FATTR_NLINK;
4805 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4806 fattr->nlink = 2;
4807}
4808
Trond Myklebust56659e92007-07-17 21:52:39 -04004809int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004810 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004811{
4812 struct nfs_server *server = NFS_SERVER(dir);
4813 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004814 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004815 };
4816 struct nfs4_fs_locations_arg args = {
4817 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004818 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004819 .page = page,
4820 .bitmask = bitmask,
4821 };
Benny Halevy22958462009-04-01 09:22:02 -04004822 struct nfs4_fs_locations_res res = {
4823 .fs_locations = fs_locations,
4824 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004825 struct rpc_message msg = {
4826 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4827 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004828 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004829 };
4830 int status;
4831
Harvey Harrison3110ff82008-05-02 13:42:44 -07004832 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004833
4834 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4835 * is not supported */
4836 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4837 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4838 else
4839 bitmask[0] |= FATTR4_WORD0_FILEID;
4840
Trond Myklebustc228fd32007-01-13 02:28:11 -05004841 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004842 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004843 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004844 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004845 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004846 return status;
4847}
4848
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004849static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4850{
4851 int status;
4852 struct nfs4_secinfo_arg args = {
4853 .dir_fh = NFS_FH(dir),
4854 .name = name,
4855 };
4856 struct nfs4_secinfo_res res = {
4857 .flavors = flavors,
4858 };
4859 struct rpc_message msg = {
4860 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4861 .rpc_argp = &args,
4862 .rpc_resp = &res,
4863 };
4864
4865 dprintk("NFS call secinfo %s\n", name->name);
4866 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4867 dprintk("NFS reply secinfo: %d\n", status);
4868 return status;
4869}
4870
4871int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4872{
4873 struct nfs4_exception exception = { };
4874 int err;
4875 do {
4876 err = nfs4_handle_exception(NFS_SERVER(dir),
4877 _nfs4_proc_secinfo(dir, name, flavors),
4878 &exception);
4879 } while (exception.retry);
4880 return err;
4881}
4882
Andy Adamson557134a2009-04-01 09:21:53 -04004883#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004884/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004885 * Check the exchange flags returned by the server for invalid flags, having
4886 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4887 * DS flags set.
4888 */
4889static int nfs4_check_cl_exchange_flags(u32 flags)
4890{
4891 if (flags & ~EXCHGID4_FLAG_MASK_R)
4892 goto out_inval;
4893 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4894 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4895 goto out_inval;
4896 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4897 goto out_inval;
4898 return NFS_OK;
4899out_inval:
4900 return -NFS4ERR_INVAL;
4901}
4902
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004903static bool
4904nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4905{
4906 if (a->server_scope_sz == b->server_scope_sz &&
4907 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4908 return true;
4909
4910 return false;
4911}
4912
Andy Adamson357f54d2010-12-14 10:11:57 -05004913/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004914 * nfs4_proc_exchange_id()
4915 *
4916 * Since the clientid has expired, all compounds using sessions
4917 * associated with the stale clientid will be returning
4918 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4919 * be in some phase of session reset.
4920 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004921int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004922{
4923 nfs4_verifier verifier;
4924 struct nfs41_exchange_id_args args = {
4925 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004926 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004927 };
4928 struct nfs41_exchange_id_res res = {
4929 .client = clp,
4930 };
4931 int status;
4932 struct rpc_message msg = {
4933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4934 .rpc_argp = &args,
4935 .rpc_resp = &res,
4936 .rpc_cred = cred,
4937 };
4938 __be32 *p;
4939
4940 dprintk("--> %s\n", __func__);
4941 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004942
Benny Halevy99fe60d2009-04-01 09:22:29 -04004943 p = (u32 *)verifier.data;
4944 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4945 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4946 args.verifier = &verifier;
4947
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004948 args.id_len = scnprintf(args.id, sizeof(args.id),
4949 "%s/%s.%s/%u",
4950 clp->cl_ipaddr,
4951 init_utsname()->nodename,
4952 init_utsname()->domainname,
4953 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004954
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004955 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05004956 if (unlikely(!res.server_scope)) {
4957 status = -ENOMEM;
4958 goto out;
4959 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004960
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05004961 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
4962 if (unlikely(!res.impl_id)) {
4963 status = -ENOMEM;
4964 goto out_server_scope;
4965 }
4966
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004967 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004968 if (!status)
4969 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004970
4971 if (!status) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05004972 /* use the most recent implementation id */
4973 kfree(clp->impl_id);
4974 clp->impl_id = res.impl_id;
4975 } else
4976 kfree(res.impl_id);
4977
4978 if (!status) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004979 if (clp->server_scope &&
4980 !nfs41_same_server_scope(clp->server_scope,
4981 res.server_scope)) {
4982 dprintk("%s: server_scope mismatch detected\n",
4983 __func__);
4984 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4985 kfree(clp->server_scope);
4986 clp->server_scope = NULL;
4987 }
4988
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05004989 if (!clp->server_scope) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004990 clp->server_scope = res.server_scope;
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05004991 goto out;
4992 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004993 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05004994
4995out_server_scope:
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05004996 kfree(res.server_scope);
4997out:
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05004998 if (clp->impl_id)
4999 dprintk("%s: Server Implementation ID: "
5000 "domain: %s, name: %s, date: %llu,%u\n",
5001 __func__, clp->impl_id->domain, clp->impl_id->name,
5002 clp->impl_id->date.seconds,
5003 clp->impl_id->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005004 dprintk("<-- %s status= %d\n", __func__, status);
5005 return status;
5006}
5007
Andy Adamson2050f0c2009-04-01 09:22:30 -04005008struct nfs4_get_lease_time_data {
5009 struct nfs4_get_lease_time_args *args;
5010 struct nfs4_get_lease_time_res *res;
5011 struct nfs_client *clp;
5012};
5013
5014static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5015 void *calldata)
5016{
5017 int ret;
5018 struct nfs4_get_lease_time_data *data =
5019 (struct nfs4_get_lease_time_data *)calldata;
5020
5021 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005022 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005023 /* just setup sequence, do not trigger session recovery
5024 since we're invoked within one */
5025 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005026 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005027 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005028
5029 BUG_ON(ret == -EAGAIN);
5030 rpc_call_start(task);
5031 dprintk("<-- %s\n", __func__);
5032}
5033
5034/*
5035 * Called from nfs4_state_manager thread for session setup, so don't recover
5036 * from sequence operation or clientid errors.
5037 */
5038static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5039{
5040 struct nfs4_get_lease_time_data *data =
5041 (struct nfs4_get_lease_time_data *)calldata;
5042
5043 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005044 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5045 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005046 switch (task->tk_status) {
5047 case -NFS4ERR_DELAY:
5048 case -NFS4ERR_GRACE:
5049 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5050 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5051 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005052 /* fall through */
5053 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005054 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005055 return;
5056 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005057 dprintk("<-- %s\n", __func__);
5058}
5059
5060struct rpc_call_ops nfs4_get_lease_time_ops = {
5061 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5062 .rpc_call_done = nfs4_get_lease_time_done,
5063};
5064
5065int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5066{
5067 struct rpc_task *task;
5068 struct nfs4_get_lease_time_args args;
5069 struct nfs4_get_lease_time_res res = {
5070 .lr_fsinfo = fsinfo,
5071 };
5072 struct nfs4_get_lease_time_data data = {
5073 .args = &args,
5074 .res = &res,
5075 .clp = clp,
5076 };
5077 struct rpc_message msg = {
5078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5079 .rpc_argp = &args,
5080 .rpc_resp = &res,
5081 };
5082 struct rpc_task_setup task_setup = {
5083 .rpc_client = clp->cl_rpcclient,
5084 .rpc_message = &msg,
5085 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005086 .callback_data = &data,
5087 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005088 };
5089 int status;
5090
Trond Myklebust9d12b212012-01-17 22:04:25 -05005091 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005092 dprintk("--> %s\n", __func__);
5093 task = rpc_run_task(&task_setup);
5094
5095 if (IS_ERR(task))
5096 status = PTR_ERR(task);
5097 else {
5098 status = task->tk_status;
5099 rpc_put_task(task);
5100 }
5101 dprintk("<-- %s return %d\n", __func__, status);
5102
5103 return status;
5104}
5105
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005106/*
5107 * Reset a slot table
5108 */
Andy Adamson104aeba2010-01-14 17:45:10 -05005109static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5110 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005111{
Andy Adamson104aeba2010-01-14 17:45:10 -05005112 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005113 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005114 int ret = 0;
5115
Andy Adamson104aeba2010-01-14 17:45:10 -05005116 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5117 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005118
Andy Adamson104aeba2010-01-14 17:45:10 -05005119 /* Does the newly negotiated max_reqs match the existing slot table? */
5120 if (max_reqs != tbl->max_slots) {
5121 ret = -ENOMEM;
5122 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005123 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005124 if (!new)
5125 goto out;
5126 ret = 0;
5127 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005128 }
5129 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05005130 if (new) {
5131 tbl->slots = new;
5132 tbl->max_slots = max_reqs;
5133 }
5134 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005135 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005136 spin_unlock(&tbl->slot_tbl_lock);
5137 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5138 tbl, tbl->slots, tbl->max_slots);
5139out:
5140 dprintk("<-- %s: return %d\n", __func__, ret);
5141 return ret;
5142}
5143
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005144/* Destroy the slot table */
5145static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5146{
5147 if (session->fc_slot_table.slots != NULL) {
5148 kfree(session->fc_slot_table.slots);
5149 session->fc_slot_table.slots = NULL;
5150 }
5151 if (session->bc_slot_table.slots != NULL) {
5152 kfree(session->bc_slot_table.slots);
5153 session->bc_slot_table.slots = NULL;
5154 }
5155 return;
5156}
5157
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005158/*
Andy Adamsonfc931582009-04-01 09:22:31 -04005159 * Initialize slot table
5160 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005161static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5162 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04005163{
Andy Adamsonfc931582009-04-01 09:22:31 -04005164 struct nfs4_slot *slot;
5165 int ret = -ENOMEM;
5166
5167 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5168
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005169 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04005170
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005171 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04005172 if (!slot)
5173 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04005174 ret = 0;
5175
5176 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04005177 tbl->max_slots = max_slots;
5178 tbl->slots = slot;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005179 tbl->highest_used_slotid = NFS4_NO_SLOT; /* no slot is currently used */
Andy Adamsonfc931582009-04-01 09:22:31 -04005180 spin_unlock(&tbl->slot_tbl_lock);
5181 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5182 tbl, tbl->slots, tbl->max_slots);
5183out:
5184 dprintk("<-- %s: return %d\n", __func__, ret);
5185 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04005186}
5187
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005188/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005189 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005190 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005191static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005192{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005193 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005194 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005195
Andy Adamsonaacd5532011-11-09 13:58:21 -05005196 dprintk("--> %s\n", __func__);
5197 /* Fore channel */
5198 tbl = &ses->fc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005199 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005200 status = nfs4_init_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5201 if (status) /* -ENOMEM */
5202 return status;
5203 } else {
5204 status = nfs4_reset_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005205 if (status)
5206 return status;
5207 }
Andy Adamsonaacd5532011-11-09 13:58:21 -05005208 /* Back channel */
5209 tbl = &ses->bc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005210 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005211 status = nfs4_init_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005212 if (status)
Andy Adamsonaacd5532011-11-09 13:58:21 -05005213 /* Fore and back channel share a connection so get
5214 * both slot tables or neither */
5215 nfs4_destroy_slot_tables(ses);
5216 } else
5217 status = nfs4_reset_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005218 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005219}
5220
5221struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5222{
5223 struct nfs4_session *session;
5224 struct nfs4_slot_table *tbl;
5225
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005226 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005227 if (!session)
5228 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005229
Andy Adamson557134a2009-04-01 09:21:53 -04005230 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005231 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005232 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005233 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005234 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005235
5236 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005237 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005238 spin_lock_init(&tbl->slot_tbl_lock);
5239 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005240 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005241
Trond Myklebust1055d762010-06-16 09:52:27 -04005242 session->session_state = 1<<NFS4_SESSION_INITING;
5243
Andy Adamson557134a2009-04-01 09:21:53 -04005244 session->clp = clp;
5245 return session;
5246}
5247
5248void nfs4_destroy_session(struct nfs4_session *session)
5249{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005250 struct rpc_xprt *xprt;
5251
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005252 nfs4_proc_destroy_session(session);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005253
5254 rcu_read_lock();
5255 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5256 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005257 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005258 __func__, xprt);
5259 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005260 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005261 kfree(session);
5262}
5263
Andy Adamsonfc931582009-04-01 09:22:31 -04005264/*
5265 * Initialize the values to be used by the client in CREATE_SESSION
5266 * If nfs4_init_session set the fore channel request and response sizes,
5267 * use them.
5268 *
5269 * Set the back channel max_resp_sz_cached to zero to force the client to
5270 * always set csa_cachethis to FALSE because the current implementation
5271 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5272 */
5273static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5274{
5275 struct nfs4_session *session = args->client->cl_session;
5276 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5277 mxresp_sz = session->fc_attrs.max_resp_sz;
5278
5279 if (mxrqst_sz == 0)
5280 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5281 if (mxresp_sz == 0)
5282 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5283 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005284 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5285 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005286 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005287 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005288
5289 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005290 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005291 __func__,
5292 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005293 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005294
5295 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005296 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5297 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5298 args->bc_attrs.max_resp_sz_cached = 0;
5299 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5300 args->bc_attrs.max_reqs = 1;
5301
5302 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5303 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5304 __func__,
5305 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5306 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5307 args->bc_attrs.max_reqs);
5308}
5309
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005310static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005311{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005312 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5313 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5314
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005315 if (rcvd->max_resp_sz > sent->max_resp_sz)
5316 return -EINVAL;
5317 /*
5318 * Our requested max_ops is the minimum we need; we're not
5319 * prepared to break up compounds into smaller pieces than that.
5320 * So, no point even trying to continue if the server won't
5321 * cooperate:
5322 */
5323 if (rcvd->max_ops < sent->max_ops)
5324 return -EINVAL;
5325 if (rcvd->max_reqs == 0)
5326 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005327 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5328 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005329 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005330}
5331
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005332static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5333{
5334 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5335 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005336
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005337 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5338 return -EINVAL;
5339 if (rcvd->max_resp_sz < sent->max_resp_sz)
5340 return -EINVAL;
5341 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5342 return -EINVAL;
5343 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005344 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005345 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005346 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005347 return -EINVAL;
5348 return 0;
5349}
Andy Adamson8d353012009-04-01 09:22:32 -04005350
Andy Adamson8d353012009-04-01 09:22:32 -04005351static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5352 struct nfs4_session *session)
5353{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005354 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005355
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005356 ret = nfs4_verify_fore_channel_attrs(args, session);
5357 if (ret)
5358 return ret;
5359 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005360}
5361
Andy Adamsonfc931582009-04-01 09:22:31 -04005362static int _nfs4_proc_create_session(struct nfs_client *clp)
5363{
5364 struct nfs4_session *session = clp->cl_session;
5365 struct nfs41_create_session_args args = {
5366 .client = clp,
5367 .cb_program = NFS4_CALLBACK,
5368 };
5369 struct nfs41_create_session_res res = {
5370 .client = clp,
5371 };
5372 struct rpc_message msg = {
5373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5374 .rpc_argp = &args,
5375 .rpc_resp = &res,
5376 };
5377 int status;
5378
5379 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005380 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005381
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005382 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005383
Andy Adamson8d353012009-04-01 09:22:32 -04005384 if (!status)
5385 /* Verify the session's negotiated channel_attrs values */
5386 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005387 if (!status) {
5388 /* Increment the clientid slot sequence id */
5389 clp->cl_seqid++;
5390 }
5391
5392 return status;
5393}
5394
5395/*
5396 * Issues a CREATE_SESSION operation to the server.
5397 * It is the responsibility of the caller to verify the session is
5398 * expired before calling this routine.
5399 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005400int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005401{
5402 int status;
5403 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005404 struct nfs4_session *session = clp->cl_session;
5405
5406 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5407
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005408 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005409 if (status)
5410 goto out;
5411
Andy Adamsonaacd5532011-11-09 13:58:21 -05005412 /* Init or reset the session slot tables */
5413 status = nfs4_setup_session_slot_tables(session);
5414 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005415 if (status)
5416 goto out;
5417
5418 ptr = (unsigned *)&session->sess_id.data[0];
5419 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5420 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005421out:
5422 dprintk("<-- %s\n", __func__);
5423 return status;
5424}
5425
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005426/*
5427 * Issue the over-the-wire RPC DESTROY_SESSION.
5428 * The caller must serialize access to this routine.
5429 */
5430int nfs4_proc_destroy_session(struct nfs4_session *session)
5431{
5432 int status = 0;
5433 struct rpc_message msg;
5434
5435 dprintk("--> nfs4_proc_destroy_session\n");
5436
5437 /* session is still being setup */
5438 if (session->clp->cl_cons_state != NFS_CS_READY)
5439 return status;
5440
5441 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5442 msg.rpc_argp = session;
5443 msg.rpc_resp = NULL;
5444 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005445 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005446
5447 if (status)
5448 printk(KERN_WARNING
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -05005449 "NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005450 "Session has been destroyed regardless...\n", status);
5451
5452 dprintk("<-- nfs4_proc_destroy_session\n");
5453 return status;
5454}
5455
Trond Myklebustfccba802009-07-21 16:48:07 -04005456int nfs4_init_session(struct nfs_server *server)
5457{
5458 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005459 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005460 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005461 int ret;
5462
5463 if (!nfs4_has_session(clp))
5464 return 0;
5465
Trond Myklebust1055d762010-06-16 09:52:27 -04005466 session = clp->cl_session;
5467 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5468 return 0;
5469
Andy Adamson68bf05e2009-12-15 12:55:02 -05005470 rsize = server->rsize;
5471 if (rsize == 0)
5472 rsize = NFS_MAX_FILE_IO_SIZE;
5473 wsize = server->wsize;
5474 if (wsize == 0)
5475 wsize = NFS_MAX_FILE_IO_SIZE;
5476
Andy Adamson68bf05e2009-12-15 12:55:02 -05005477 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5478 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005479
Trond Myklebustfccba802009-07-21 16:48:07 -04005480 ret = nfs4_recover_expired_lease(server);
5481 if (!ret)
5482 ret = nfs4_check_client_ready(clp);
5483 return ret;
5484}
5485
Andy Adamsond83217c2011-03-01 01:34:17 +00005486int nfs4_init_ds_session(struct nfs_client *clp)
5487{
5488 struct nfs4_session *session = clp->cl_session;
5489 int ret;
5490
5491 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5492 return 0;
5493
5494 ret = nfs4_client_recover_expired_lease(clp);
5495 if (!ret)
5496 /* Test for the DS role */
5497 if (!is_ds_client(clp))
5498 ret = -ENODEV;
5499 if (!ret)
5500 ret = nfs4_check_client_ready(clp);
5501 return ret;
5502
5503}
5504EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5505
5506
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005507/*
5508 * Renew the cl_session lease.
5509 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005510struct nfs4_sequence_data {
5511 struct nfs_client *clp;
5512 struct nfs4_sequence_args args;
5513 struct nfs4_sequence_res res;
5514};
5515
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005516static void nfs41_sequence_release(void *data)
5517{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005518 struct nfs4_sequence_data *calldata = data;
5519 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005520
Alexandros Batsakis71358402010-02-05 03:45:05 -08005521 if (atomic_read(&clp->cl_count) > 1)
5522 nfs4_schedule_state_renewal(clp);
5523 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005524 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005525}
5526
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005527static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5528{
5529 switch(task->tk_status) {
5530 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005531 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5532 return -EAGAIN;
5533 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005534 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005535 }
5536 return 0;
5537}
5538
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005539static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005540{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005541 struct nfs4_sequence_data *calldata = data;
5542 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005543
Trond Myklebust14516c32010-07-31 14:29:06 -04005544 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5545 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005546
5547 if (task->tk_status < 0) {
5548 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005549 if (atomic_read(&clp->cl_count) == 1)
5550 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005551
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005552 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5553 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005554 return;
5555 }
5556 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005557 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005558out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005559 dprintk("<-- %s\n", __func__);
5560}
5561
5562static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5563{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005564 struct nfs4_sequence_data *calldata = data;
5565 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005566 struct nfs4_sequence_args *args;
5567 struct nfs4_sequence_res *res;
5568
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005569 args = task->tk_msg.rpc_argp;
5570 res = task->tk_msg.rpc_resp;
5571
Trond Myklebust9d12b212012-01-17 22:04:25 -05005572 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005573 return;
5574 rpc_call_start(task);
5575}
5576
5577static const struct rpc_call_ops nfs41_sequence_ops = {
5578 .rpc_call_done = nfs41_sequence_call_done,
5579 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005580 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005581};
5582
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005583static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005584{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005585 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005586 struct rpc_message msg = {
5587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5588 .rpc_cred = cred,
5589 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005590 struct rpc_task_setup task_setup_data = {
5591 .rpc_client = clp->cl_rpcclient,
5592 .rpc_message = &msg,
5593 .callback_ops = &nfs41_sequence_ops,
5594 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5595 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005596
Alexandros Batsakis71358402010-02-05 03:45:05 -08005597 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005598 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005599 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005600 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005601 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005602 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005603 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005604 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005605 msg.rpc_argp = &calldata->args;
5606 msg.rpc_resp = &calldata->res;
5607 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005608 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005609
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005610 return rpc_run_task(&task_setup_data);
5611}
5612
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005613static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005614{
5615 struct rpc_task *task;
5616 int ret = 0;
5617
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005618 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5619 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005620 task = _nfs41_proc_sequence(clp, cred);
5621 if (IS_ERR(task))
5622 ret = PTR_ERR(task);
5623 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005624 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005625 dprintk("<-- %s status=%d\n", __func__, ret);
5626 return ret;
5627}
5628
5629static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5630{
5631 struct rpc_task *task;
5632 int ret;
5633
5634 task = _nfs41_proc_sequence(clp, cred);
5635 if (IS_ERR(task)) {
5636 ret = PTR_ERR(task);
5637 goto out;
5638 }
5639 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005640 if (!ret) {
5641 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5642
5643 if (task->tk_status == 0)
5644 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005645 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005646 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005647 rpc_put_task(task);
5648out:
5649 dprintk("<-- %s status=%d\n", __func__, ret);
5650 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005651}
5652
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005653struct nfs4_reclaim_complete_data {
5654 struct nfs_client *clp;
5655 struct nfs41_reclaim_complete_args arg;
5656 struct nfs41_reclaim_complete_res res;
5657};
5658
5659static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5660{
5661 struct nfs4_reclaim_complete_data *calldata = data;
5662
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005663 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168ab2010-06-16 09:52:26 -04005664 if (nfs41_setup_sequence(calldata->clp->cl_session,
5665 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005666 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005667 return;
5668
5669 rpc_call_start(task);
5670}
5671
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005672static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5673{
5674 switch(task->tk_status) {
5675 case 0:
5676 case -NFS4ERR_COMPLETE_ALREADY:
5677 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5678 break;
5679 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005680 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005681 /* fall through */
5682 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005683 return -EAGAIN;
5684 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005685 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005686 }
5687 return 0;
5688}
5689
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005690static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5691{
5692 struct nfs4_reclaim_complete_data *calldata = data;
5693 struct nfs_client *clp = calldata->clp;
5694 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5695
5696 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005697 if (!nfs41_sequence_done(task, res))
5698 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005699
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005700 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5701 rpc_restart_call_prepare(task);
5702 return;
5703 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005704 dprintk("<-- %s\n", __func__);
5705}
5706
5707static void nfs4_free_reclaim_complete_data(void *data)
5708{
5709 struct nfs4_reclaim_complete_data *calldata = data;
5710
5711 kfree(calldata);
5712}
5713
5714static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5715 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5716 .rpc_call_done = nfs4_reclaim_complete_done,
5717 .rpc_release = nfs4_free_reclaim_complete_data,
5718};
5719
5720/*
5721 * Issue a global reclaim complete.
5722 */
5723static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5724{
5725 struct nfs4_reclaim_complete_data *calldata;
5726 struct rpc_task *task;
5727 struct rpc_message msg = {
5728 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5729 };
5730 struct rpc_task_setup task_setup_data = {
5731 .rpc_client = clp->cl_rpcclient,
5732 .rpc_message = &msg,
5733 .callback_ops = &nfs4_reclaim_complete_call_ops,
5734 .flags = RPC_TASK_ASYNC,
5735 };
5736 int status = -ENOMEM;
5737
5738 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005739 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005740 if (calldata == NULL)
5741 goto out;
5742 calldata->clp = clp;
5743 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005744
Trond Myklebust9d12b212012-01-17 22:04:25 -05005745 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005746 msg.rpc_argp = &calldata->arg;
5747 msg.rpc_resp = &calldata->res;
5748 task_setup_data.callback_data = calldata;
5749 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005750 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005751 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005752 goto out;
5753 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005754 status = nfs4_wait_for_completion_rpc_task(task);
5755 if (status == 0)
5756 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005757 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005758 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005759out:
5760 dprintk("<-- %s status=%d\n", __func__, status);
5761 return status;
5762}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005763
5764static void
5765nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5766{
5767 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005768 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005769
5770 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005771 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5772 * right now covering the LAYOUTGET we are about to send.
5773 * However, that is not so catastrophic, and there seems
5774 * to be no way to prevent it completely.
5775 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005776 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005777 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005778 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005779 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5780 NFS_I(lgp->args.inode)->layout,
5781 lgp->args.ctx->state)) {
5782 rpc_exit(task, NFS4_OK);
5783 return;
5784 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005785 rpc_call_start(task);
5786}
5787
5788static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5789{
5790 struct nfs4_layoutget *lgp = calldata;
5791 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5792
5793 dprintk("--> %s\n", __func__);
5794
5795 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5796 return;
5797
5798 switch (task->tk_status) {
5799 case 0:
5800 break;
5801 case -NFS4ERR_LAYOUTTRYLATER:
5802 case -NFS4ERR_RECALLCONFLICT:
5803 task->tk_status = -NFS4ERR_DELAY;
5804 /* Fall through */
5805 default:
5806 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5807 rpc_restart_call_prepare(task);
5808 return;
5809 }
5810 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005811 dprintk("<-- %s\n", __func__);
5812}
5813
5814static void nfs4_layoutget_release(void *calldata)
5815{
5816 struct nfs4_layoutget *lgp = calldata;
5817
5818 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005819 put_nfs_open_context(lgp->args.ctx);
5820 kfree(calldata);
5821 dprintk("<-- %s\n", __func__);
5822}
5823
5824static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5825 .rpc_call_prepare = nfs4_layoutget_prepare,
5826 .rpc_call_done = nfs4_layoutget_done,
5827 .rpc_release = nfs4_layoutget_release,
5828};
5829
5830int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5831{
5832 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5833 struct rpc_task *task;
5834 struct rpc_message msg = {
5835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5836 .rpc_argp = &lgp->args,
5837 .rpc_resp = &lgp->res,
5838 };
5839 struct rpc_task_setup task_setup_data = {
5840 .rpc_client = server->client,
5841 .rpc_message = &msg,
5842 .callback_ops = &nfs4_layoutget_call_ops,
5843 .callback_data = lgp,
5844 .flags = RPC_TASK_ASYNC,
5845 };
5846 int status = 0;
5847
5848 dprintk("--> %s\n", __func__);
5849
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005850 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005851 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05005852 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005853 task = rpc_run_task(&task_setup_data);
5854 if (IS_ERR(task))
5855 return PTR_ERR(task);
5856 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005857 if (status == 0)
5858 status = task->tk_status;
5859 if (status == 0)
5860 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005861 rpc_put_task(task);
5862 dprintk("<-- %s status=%d\n", __func__, status);
5863 return status;
5864}
5865
Benny Halevycbe82602011-05-22 19:52:37 +03005866static void
5867nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5868{
5869 struct nfs4_layoutreturn *lrp = calldata;
5870
5871 dprintk("--> %s\n", __func__);
5872 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005873 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03005874 return;
5875 rpc_call_start(task);
5876}
5877
5878static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5879{
5880 struct nfs4_layoutreturn *lrp = calldata;
5881 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005882 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005883
5884 dprintk("--> %s\n", __func__);
5885
5886 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5887 return;
5888
5889 server = NFS_SERVER(lrp->args.inode);
5890 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005891 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03005892 return;
5893 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005894 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005895 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005896 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005897 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005898 } else
5899 BUG_ON(!list_empty(&lo->plh_segs));
5900 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005901 lo->plh_block_lgets--;
5902 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005903 dprintk("<-- %s\n", __func__);
5904}
5905
5906static void nfs4_layoutreturn_release(void *calldata)
5907{
5908 struct nfs4_layoutreturn *lrp = calldata;
5909
5910 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005911 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005912 kfree(calldata);
5913 dprintk("<-- %s\n", __func__);
5914}
5915
5916static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5917 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5918 .rpc_call_done = nfs4_layoutreturn_done,
5919 .rpc_release = nfs4_layoutreturn_release,
5920};
5921
5922int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5923{
5924 struct rpc_task *task;
5925 struct rpc_message msg = {
5926 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5927 .rpc_argp = &lrp->args,
5928 .rpc_resp = &lrp->res,
5929 };
5930 struct rpc_task_setup task_setup_data = {
5931 .rpc_client = lrp->clp->cl_rpcclient,
5932 .rpc_message = &msg,
5933 .callback_ops = &nfs4_layoutreturn_call_ops,
5934 .callback_data = lrp,
5935 };
5936 int status;
5937
5938 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05005939 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03005940 task = rpc_run_task(&task_setup_data);
5941 if (IS_ERR(task))
5942 return PTR_ERR(task);
5943 status = task->tk_status;
5944 dprintk("<-- %s status=%d\n", __func__, status);
5945 rpc_put_task(task);
5946 return status;
5947}
5948
Andy Adamson7f11d8d2011-07-30 20:52:35 -04005949/*
5950 * Retrieve the list of Data Server devices from the MDS.
5951 */
5952static int _nfs4_getdevicelist(struct nfs_server *server,
5953 const struct nfs_fh *fh,
5954 struct pnfs_devicelist *devlist)
5955{
5956 struct nfs4_getdevicelist_args args = {
5957 .fh = fh,
5958 .layoutclass = server->pnfs_curr_ld->id,
5959 };
5960 struct nfs4_getdevicelist_res res = {
5961 .devlist = devlist,
5962 };
5963 struct rpc_message msg = {
5964 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5965 .rpc_argp = &args,
5966 .rpc_resp = &res,
5967 };
5968 int status;
5969
5970 dprintk("--> %s\n", __func__);
5971 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5972 &res.seq_res, 0);
5973 dprintk("<-- %s status=%d\n", __func__, status);
5974 return status;
5975}
5976
5977int nfs4_proc_getdevicelist(struct nfs_server *server,
5978 const struct nfs_fh *fh,
5979 struct pnfs_devicelist *devlist)
5980{
5981 struct nfs4_exception exception = { };
5982 int err;
5983
5984 do {
5985 err = nfs4_handle_exception(server,
5986 _nfs4_getdevicelist(server, fh, devlist),
5987 &exception);
5988 } while (exception.retry);
5989
5990 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5991 err, devlist->num_devs);
5992
5993 return err;
5994}
5995EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5996
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005997static int
5998_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5999{
6000 struct nfs4_getdeviceinfo_args args = {
6001 .pdev = pdev,
6002 };
6003 struct nfs4_getdeviceinfo_res res = {
6004 .pdev = pdev,
6005 };
6006 struct rpc_message msg = {
6007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6008 .rpc_argp = &args,
6009 .rpc_resp = &res,
6010 };
6011 int status;
6012
6013 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006014 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006015 dprintk("<-- %s status=%d\n", __func__, status);
6016
6017 return status;
6018}
6019
6020int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6021{
6022 struct nfs4_exception exception = { };
6023 int err;
6024
6025 do {
6026 err = nfs4_handle_exception(server,
6027 _nfs4_proc_getdeviceinfo(server, pdev),
6028 &exception);
6029 } while (exception.retry);
6030 return err;
6031}
6032EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6033
Andy Adamson863a3c62011-03-23 13:27:54 +00006034static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6035{
6036 struct nfs4_layoutcommit_data *data = calldata;
6037 struct nfs_server *server = NFS_SERVER(data->args.inode);
6038
6039 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006040 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006041 return;
6042 rpc_call_start(task);
6043}
6044
6045static void
6046nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6047{
6048 struct nfs4_layoutcommit_data *data = calldata;
6049 struct nfs_server *server = NFS_SERVER(data->args.inode);
6050
6051 if (!nfs4_sequence_done(task, &data->res.seq_res))
6052 return;
6053
6054 switch (task->tk_status) { /* Just ignore these failures */
6055 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6056 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6057 case NFS4ERR_BADLAYOUT: /* no layout */
6058 case NFS4ERR_GRACE: /* loca_recalim always false */
6059 task->tk_status = 0;
6060 }
6061
6062 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006063 rpc_restart_call_prepare(task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006064 return;
6065 }
6066
6067 if (task->tk_status == 0)
6068 nfs_post_op_update_inode_force_wcc(data->args.inode,
6069 data->res.fattr);
6070}
6071
6072static void nfs4_layoutcommit_release(void *calldata)
6073{
6074 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006075 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006076 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006077
Andy Adamsondb29c082011-07-30 20:52:38 -04006078 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006079 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006080 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6081 list_del_init(&lseg->pls_lc_list);
6082 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6083 &lseg->pls_flags))
6084 put_lseg(lseg);
6085 }
Peng Tao92407e72011-10-23 20:21:17 -07006086
6087 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6088 smp_mb__after_clear_bit();
6089 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6090
Andy Adamson863a3c62011-03-23 13:27:54 +00006091 put_rpccred(data->cred);
6092 kfree(data);
6093}
6094
6095static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6096 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6097 .rpc_call_done = nfs4_layoutcommit_done,
6098 .rpc_release = nfs4_layoutcommit_release,
6099};
6100
6101int
Andy Adamsonef311532011-03-12 02:58:10 -05006102nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006103{
6104 struct rpc_message msg = {
6105 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6106 .rpc_argp = &data->args,
6107 .rpc_resp = &data->res,
6108 .rpc_cred = data->cred,
6109 };
6110 struct rpc_task_setup task_setup_data = {
6111 .task = &data->task,
6112 .rpc_client = NFS_CLIENT(data->args.inode),
6113 .rpc_message = &msg,
6114 .callback_ops = &nfs4_layoutcommit_ops,
6115 .callback_data = data,
6116 .flags = RPC_TASK_ASYNC,
6117 };
6118 struct rpc_task *task;
6119 int status = 0;
6120
6121 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6122 "lbw: %llu inode %lu\n",
6123 data->task.tk_pid, sync,
6124 data->args.lastbytewritten,
6125 data->args.inode->i_ino);
6126
Trond Myklebust9d12b212012-01-17 22:04:25 -05006127 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006128 task = rpc_run_task(&task_setup_data);
6129 if (IS_ERR(task))
6130 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006131 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006132 goto out;
6133 status = nfs4_wait_for_completion_rpc_task(task);
6134 if (status != 0)
6135 goto out;
6136 status = task->tk_status;
6137out:
6138 dprintk("%s: status %d\n", __func__, status);
6139 rpc_put_task(task);
6140 return status;
6141}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006142
6143static int
6144_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6145 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6146{
6147 struct nfs41_secinfo_no_name_args args = {
6148 .style = SECINFO_STYLE_CURRENT_FH,
6149 };
6150 struct nfs4_secinfo_res res = {
6151 .flavors = flavors,
6152 };
6153 struct rpc_message msg = {
6154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6155 .rpc_argp = &args,
6156 .rpc_resp = &res,
6157 };
6158 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6159}
6160
6161static int
6162nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6163 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6164{
6165 struct nfs4_exception exception = { };
6166 int err;
6167 do {
6168 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6169 switch (err) {
6170 case 0:
6171 case -NFS4ERR_WRONGSEC:
6172 case -NFS4ERR_NOTSUPP:
6173 break;
6174 default:
6175 err = nfs4_handle_exception(server, err, &exception);
6176 }
6177 } while (exception.retry);
6178 return err;
6179}
6180
6181static int
6182nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6183 struct nfs_fsinfo *info)
6184{
6185 int err;
6186 struct page *page;
6187 rpc_authflavor_t flavor;
6188 struct nfs4_secinfo_flavors *flavors;
6189
6190 page = alloc_page(GFP_KERNEL);
6191 if (!page) {
6192 err = -ENOMEM;
6193 goto out;
6194 }
6195
6196 flavors = page_address(page);
6197 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6198
6199 /*
6200 * Fall back on "guess and check" method if
6201 * the server doesn't support SECINFO_NO_NAME
6202 */
6203 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6204 err = nfs4_find_root_sec(server, fhandle, info);
6205 goto out_freepage;
6206 }
6207 if (err)
6208 goto out_freepage;
6209
6210 flavor = nfs_find_best_sec(flavors);
6211 if (err == 0)
6212 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6213
6214out_freepage:
6215 put_page(page);
6216 if (err == -EACCES)
6217 return -EPERM;
6218out:
6219 return err;
6220}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006221
6222static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006223{
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006224 int status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006225 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006226 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006227 };
6228 struct nfs41_test_stateid_res res;
6229 struct rpc_message msg = {
6230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6231 .rpc_argp = &args,
6232 .rpc_resp = &res,
6233 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006234
Trond Myklebust9d12b212012-01-17 22:04:25 -05006235 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006236 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6237
6238 if (status == NFS_OK)
6239 return res.status;
6240 return status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006241}
6242
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006243static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006244{
6245 struct nfs4_exception exception = { };
6246 int err;
6247 do {
6248 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006249 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006250 &exception);
6251 } while (exception.retry);
6252 return err;
6253}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006254
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006255static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006256{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006257 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006258 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006259 };
6260 struct nfs41_free_stateid_res res;
6261 struct rpc_message msg = {
6262 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6263 .rpc_argp = &args,
6264 .rpc_resp = &res,
6265 };
6266
Trond Myklebust9d12b212012-01-17 22:04:25 -05006267 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6268 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006269}
6270
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006271static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006272{
6273 struct nfs4_exception exception = { };
6274 int err;
6275 do {
6276 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006277 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006278 &exception);
6279 } while (exception.retry);
6280 return err;
6281}
Andy Adamson557134a2009-04-01 09:21:53 -04006282#endif /* CONFIG_NFS_V4_1 */
6283
Andy Adamson591d71c2009-04-01 09:22:47 -04006284struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006285 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006286 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287 .recover_open = nfs4_open_reclaim,
6288 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006289 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006290 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291};
6292
Andy Adamson591d71c2009-04-01 09:22:47 -04006293#if defined(CONFIG_NFS_V4_1)
6294struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6295 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6296 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6297 .recover_open = nfs4_open_reclaim,
6298 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006299 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006300 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006301 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006302};
6303#endif /* CONFIG_NFS_V4_1 */
6304
6305struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006306 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006307 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 .recover_open = nfs4_open_expired,
6309 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006310 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006311 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312};
6313
Andy Adamson591d71c2009-04-01 09:22:47 -04006314#if defined(CONFIG_NFS_V4_1)
6315struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6316 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6317 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006318 .recover_open = nfs41_open_expired,
6319 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006320 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006321 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006322};
6323#endif /* CONFIG_NFS_V4_1 */
6324
Benny Halevy29fba382009-04-01 09:22:44 -04006325struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6326 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006327 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006328 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006329};
6330
6331#if defined(CONFIG_NFS_V4_1)
6332struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6333 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006334 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006335 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006336};
6337#endif
6338
Trond Myklebust97dc1352010-06-16 09:52:26 -04006339static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6340 .minor_version = 0,
6341 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04006342 .validate_stateid = nfs4_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006343 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006344 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6345 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6346 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006347};
6348
6349#if defined(CONFIG_NFS_V4_1)
6350static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6351 .minor_version = 1,
6352 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04006353 .validate_stateid = nfs41_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006354 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006355 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6356 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6357 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006358};
6359#endif
6360
Trond Myklebust97dc1352010-06-16 09:52:26 -04006361const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6362 [0] = &nfs_v4_0_minor_ops,
6363#if defined(CONFIG_NFS_V4_1)
6364 [1] = &nfs_v4_1_minor_ops,
6365#endif
6366};
6367
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006368static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006369 .permission = nfs_permission,
6370 .getattr = nfs_getattr,
6371 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006372 .getxattr = generic_getxattr,
6373 .setxattr = generic_setxattr,
6374 .listxattr = generic_listxattr,
6375 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006376};
6377
David Howells509de812006-08-22 20:06:11 -04006378const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 .version = 4, /* protocol version */
6380 .dentry_ops = &nfs4_dentry_operations,
6381 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006382 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006383 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 .getroot = nfs4_proc_get_root,
6385 .getattr = nfs4_proc_getattr,
6386 .setattr = nfs4_proc_setattr,
6387 .lookup = nfs4_proc_lookup,
6388 .access = nfs4_proc_access,
6389 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390 .create = nfs4_proc_create,
6391 .remove = nfs4_proc_remove,
6392 .unlink_setup = nfs4_proc_unlink_setup,
6393 .unlink_done = nfs4_proc_unlink_done,
6394 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006395 .rename_setup = nfs4_proc_rename_setup,
6396 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397 .link = nfs4_proc_link,
6398 .symlink = nfs4_proc_symlink,
6399 .mkdir = nfs4_proc_mkdir,
6400 .rmdir = nfs4_proc_remove,
6401 .readdir = nfs4_proc_readdir,
6402 .mknod = nfs4_proc_mknod,
6403 .statfs = nfs4_proc_statfs,
6404 .fsinfo = nfs4_proc_fsinfo,
6405 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006406 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407 .decode_dirent = nfs4_decode_dirent,
6408 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006409 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006411 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006413 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006415 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006416 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006417 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006418 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006419 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420};
6421
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006422static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6423 .prefix = XATTR_NAME_NFSV4_ACL,
6424 .list = nfs4_xattr_list_nfs4_acl,
6425 .get = nfs4_xattr_get_nfs4_acl,
6426 .set = nfs4_xattr_set_nfs4_acl,
6427};
6428
6429const struct xattr_handler *nfs4_xattr_handlers[] = {
6430 &nfs4_xattr_nfs4_acl_handler,
6431 NULL
6432};
6433
Trond Myklebustef159e92012-02-06 19:50:40 -05006434module_param(max_session_slots, ushort, 0644);
6435MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6436 "requests the client will negotiate");
6437
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438/*
6439 * Local variables:
6440 * c-basic-offset: 8
6441 * End:
6442 */