blob: e9b1eb701a01c11c4f4b7ac00bac5bf2c92438fa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
Bryan Schumaker8f70e952011-03-24 17:12:31 +000046#include <linux/sunrpc/gss_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050055#include <linux/nfs_idmap.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040056#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000057#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050058#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010059#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Trond Myklebust4ce79712005-06-22 17:16:21 +000061#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050063#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050064#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040065#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040066#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define NFSDBG_FACILITY NFSDBG_PROC
69
Trond Myklebust2066fe82006-09-15 08:30:46 -040070#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFS4_POLL_RETRY_MAX (15*HZ)
72
Trond Myklebusta78cb572009-08-09 15:06:19 -040073#define NFS4_MAX_LOOP_ON_RECOVER (10)
74
Trond Myklebustef159e92012-02-06 19:50:40 -050075static unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
76
Trond Myklebustcdd4e682006-01-03 09:55:12 +010077struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010078static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080079static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050081static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050082static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Trond Myklebust99367812007-07-17 21:52:41 -040083static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040084static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
86 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040087#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050088static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
89static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040090#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050092static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Trond Myklebust52567b02009-10-23 14:46:42 -040094 if (err >= -1000)
95 return err;
96 switch (err) {
97 case -NFS4ERR_RESOURCE:
98 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000099 case -NFS4ERR_WRONGSEC:
100 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800101 case -NFS4ERR_BADOWNER:
102 case -NFS4ERR_BADNAME:
103 return -EINVAL;
Trond Myklebust27b0a532012-05-28 11:36:28 -0400104 case -NFS4ERR_SHARE_DENIED:
105 return -EACCES;
Trond Myklebust52567b02009-10-23 14:46:42 -0400106 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700108 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400109 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400111 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114/*
115 * This is our standard bitmap for GETATTR requests.
116 */
117const u32 nfs4_fattr_bitmap[2] = {
118 FATTR4_WORD0_TYPE
119 | FATTR4_WORD0_CHANGE
120 | FATTR4_WORD0_SIZE
121 | FATTR4_WORD0_FSID
122 | FATTR4_WORD0_FILEID,
123 FATTR4_WORD1_MODE
124 | FATTR4_WORD1_NUMLINKS
125 | FATTR4_WORD1_OWNER
126 | FATTR4_WORD1_OWNER_GROUP
127 | FATTR4_WORD1_RAWDEV
128 | FATTR4_WORD1_SPACE_USED
129 | FATTR4_WORD1_TIME_ACCESS
130 | FATTR4_WORD1_TIME_METADATA
131 | FATTR4_WORD1_TIME_MODIFY
132};
133
134const u32 nfs4_statfs_bitmap[2] = {
135 FATTR4_WORD0_FILES_AVAIL
136 | FATTR4_WORD0_FILES_FREE
137 | FATTR4_WORD0_FILES_TOTAL,
138 FATTR4_WORD1_SPACE_AVAIL
139 | FATTR4_WORD1_SPACE_FREE
140 | FATTR4_WORD1_SPACE_TOTAL
141};
142
Trond Myklebust4ce79712005-06-22 17:16:21 +0000143const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 FATTR4_WORD0_MAXLINK
145 | FATTR4_WORD0_MAXNAME,
146 0
147};
148
Fred Isamandae100c2011-07-30 20:52:37 -0400149const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 | FATTR4_WORD0_MAXREAD
151 | FATTR4_WORD0_MAXWRITE
152 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700153 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400154 | FATTR4_WORD1_FS_LAYOUT_TYPES,
155 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
Manoj Naik830b8e32006-06-09 09:34:25 -0400158const u32 nfs4_fs_locations_bitmap[2] = {
159 FATTR4_WORD0_TYPE
160 | FATTR4_WORD0_CHANGE
161 | FATTR4_WORD0_SIZE
162 | FATTR4_WORD0_FSID
163 | FATTR4_WORD0_FILEID
164 | FATTR4_WORD0_FS_LOCATIONS,
165 FATTR4_WORD1_MODE
166 | FATTR4_WORD1_NUMLINKS
167 | FATTR4_WORD1_OWNER
168 | FATTR4_WORD1_OWNER_GROUP
169 | FATTR4_WORD1_RAWDEV
170 | FATTR4_WORD1_SPACE_USED
171 | FATTR4_WORD1_TIME_ACCESS
172 | FATTR4_WORD1_TIME_METADATA
173 | FATTR4_WORD1_TIME_MODIFY
174 | FATTR4_WORD1_MOUNTED_ON_FILEID
175};
176
Al Virobc4785c2006-10-19 23:28:51 -0700177static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 struct nfs4_readdir_arg *readdir)
179{
Al Viro0dbb4c62006-10-19 23:28:49 -0700180 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 BUG_ON(readdir->count < 80);
183 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000184 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
186 return;
187 }
188
189 readdir->cookie = 0;
190 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
191 if (cookie == 2)
192 return;
193
194 /*
195 * NFSv4 servers do not return entries for '.' and '..'
196 * Therefore, we fake these entries here. We let '.'
197 * have cookie 0 and '..' have cookie 1. Note that
198 * when talking to the server, we always send cookie 0
199 * instead of 1 or 2.
200 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800201 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 if (cookie == 0) {
204 *p++ = xdr_one; /* next */
205 *p++ = xdr_zero; /* cookie, first word */
206 *p++ = xdr_one; /* cookie, second word */
207 *p++ = xdr_one; /* entry len */
208 memcpy(p, ".\0\0\0", 4); /* entry */
209 p++;
210 *p++ = xdr_one; /* bitmap length */
211 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
212 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400213 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215
216 *p++ = xdr_one; /* next */
217 *p++ = xdr_zero; /* cookie, first word */
218 *p++ = xdr_two; /* cookie, second word */
219 *p++ = xdr_two; /* entry len */
220 memcpy(p, "..\0\0", 4); /* entry */
221 p++;
222 *p++ = xdr_one; /* bitmap length */
223 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
224 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400225 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 readdir->pgbase = (char *)p - (char *)start;
228 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800229 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Trond Myklebust65de8722008-12-23 15:21:44 -0500232static int nfs4_wait_clnt_recover(struct nfs_client *clp)
233{
234 int res;
235
236 might_sleep();
237
Trond Myklebuste005e802008-12-23 15:21:48 -0500238 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400239 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500240 return res;
241}
242
243static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
244{
245 int res = 0;
246
247 might_sleep();
248
249 if (*timeout <= 0)
250 *timeout = NFS4_POLL_RETRY_MIN;
251 if (*timeout > NFS4_POLL_RETRY_MAX)
252 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100253 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500254 if (fatal_signal_pending(current))
255 res = -ERESTARTSYS;
256 *timeout <<= 1;
257 return res;
258}
259
260/* This is the error handling routine for processes that are allowed
261 * to sleep.
262 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800263static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500264{
265 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500266 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500267 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500268 int ret = errorcode;
269
270 exception->retry = 0;
271 switch(errorcode) {
272 case 0:
273 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500274 case -NFS4ERR_OPENMODE:
Trond Myklebust14977482012-03-27 18:31:25 -0400275 if (inode && nfs_have_delegation(inode, FMODE_READ)) {
Trond Myklebust3114ea72012-03-07 16:39:06 -0500276 nfs_inode_return_delegation(inode);
277 exception->retry = 1;
278 return 0;
279 }
280 if (state == NULL)
281 break;
282 nfs4_schedule_stateid_recovery(server, state);
283 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500284 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500285 case -NFS4ERR_ADMIN_REVOKED:
286 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500287 if (state == NULL)
288 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400289 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500290 nfs4_schedule_stateid_recovery(server, state);
291 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400292 case -NFS4ERR_EXPIRED:
293 if (state != NULL)
294 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500295 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500296 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500297 nfs4_schedule_lease_recovery(clp);
298 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500299#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400300 case -NFS4ERR_BADSESSION:
301 case -NFS4ERR_BADSLOT:
302 case -NFS4ERR_BAD_HIGH_SLOT:
303 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
304 case -NFS4ERR_DEADSESSION:
305 case -NFS4ERR_SEQ_FALSE_RETRY:
306 case -NFS4ERR_SEQ_MISORDERED:
307 dprintk("%s ERROR: %d Reset session\n", __func__,
308 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500309 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400310 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500311 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500312#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500313 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500314 if (exception->timeout > HZ) {
315 /* We have retried a decent amount, time to
316 * fail
317 */
318 ret = -EBUSY;
319 break;
320 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500321 case -NFS4ERR_GRACE:
322 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500323 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500324 ret = nfs4_delay(server->client, &exception->timeout);
325 if (ret != 0)
326 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400327 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500328 case -NFS4ERR_OLD_STATEID:
329 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800330 break;
331 case -NFS4ERR_BADOWNER:
332 /* The following works around a Linux server bug! */
333 case -NFS4ERR_BADNAME:
334 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
335 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
336 exception->retry = 1;
337 printk(KERN_WARNING "NFS: v4 server %s "
338 "does not accept raw "
339 "uid/gids. "
340 "Reenabling the idmapper.\n",
341 server->nfs_client->cl_hostname);
342 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500343 }
344 /* We failed to handle the error */
345 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500346wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500347 ret = nfs4_wait_clnt_recover(clp);
348 if (ret == 0)
349 exception->retry = 1;
350 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500351}
352
353
Trond Myklebust452e9352010-07-31 14:29:06 -0400354static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 spin_lock(&clp->cl_lock);
357 if (time_before(clp->cl_last_renewal,timestamp))
358 clp->cl_last_renewal = timestamp;
359 spin_unlock(&clp->cl_lock);
360}
361
Trond Myklebust452e9352010-07-31 14:29:06 -0400362static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
363{
364 do_renew_lease(server->nfs_client, timestamp);
365}
366
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400367#if defined(CONFIG_NFS_V4_1)
368
Benny Halevy510b8172009-04-01 09:22:14 -0400369/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400370 * nfs4_free_slot - free a slot and efficiently update slot table.
371 *
372 * freeing a slot is trivially done by clearing its respective bit
373 * in the bitmap.
374 * If the freed slotid equals highest_used_slotid we want to update it
375 * so that the server would be able to size down the slot table if needed,
376 * otherwise we know that the highest_used_slotid is still in use.
377 * When updating highest_used_slotid there may be "holes" in the bitmap
378 * so we need to scan down from highest_used_slotid to 0 looking for the now
379 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500380 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500381 *
382 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400383 */
384static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500385nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400386{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500387 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400388 /* clear used bit in bitmap */
389 __clear_bit(slotid, tbl->used_slots);
390
391 /* update highest_used_slotid when it is freed */
392 if (slotid == tbl->highest_used_slotid) {
393 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500394 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400395 tbl->highest_used_slotid = slotid;
396 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500397 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400398 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500399 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
400 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400401}
402
Trond Myklebust961a8282012-01-17 22:57:37 -0500403bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
404{
405 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
406 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400407}
408
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800409/*
Andy Adamson42acd022011-01-06 02:04:34 +0000410 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800411 */
Andy Adamson42acd022011-01-06 02:04:34 +0000412static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800413{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400414 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500415 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
416 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800417 return;
418 }
419
Trond Myklebust45d43c22012-02-06 19:38:51 -0500420 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800421 return;
422
Andy Adamson42acd022011-01-06 02:04:34 +0000423 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
424 complete(&ses->fc_slot_table.complete);
425}
426
427/*
428 * Signal state manager thread if session back channel is drained
429 */
430void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
431{
432 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500433 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000434 return;
435 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
436 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800437}
438
Trond Myklebustd185a332010-06-16 09:52:25 -0400439static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400440{
441 struct nfs4_slot_table *tbl;
442
Trond Myklebustd185a332010-06-16 09:52:25 -0400443 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f302010-09-24 09:17:01 -0400444 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400445 /* just wake up the next guy waiting since
446 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500447 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500448 return;
Andy Adamson13615872009-04-01 09:22:17 -0400449 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500450
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500451 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500452 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000453 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500454 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f302010-09-24 09:17:01 -0400455 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400456}
457
Trond Myklebust14516c32010-07-31 14:29:06 -0400458static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400459{
460 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400461 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400462
463 /*
464 * sr_status remains 1 if an RPC level error occurred. The server
465 * may or may not have processed the sequence operation..
466 * Proceed as if the server received and processed the sequence
467 * operation.
468 */
469 if (res->sr_status == 1)
470 res->sr_status = NFS_OK;
471
Bryan Schumaker468f8612011-04-18 15:57:32 -0400472 /* don't increment the sequence number if the task wasn't sent */
473 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400474 goto out;
475
Andy Adamson691daf32009-12-04 15:55:39 -0500476 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400477 switch (res->sr_status) {
478 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400479 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f302010-09-24 09:17:01 -0400480 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400481 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400482 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400483 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500484 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500485 if (res->sr_status_flags != 0)
486 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400487 break;
488 case -NFS4ERR_DELAY:
489 /* The server detected a resend of the RPC call and
490 * returned NFS4ERR_DELAY as per Section 2.10.6.2
491 * of RFC5661.
492 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200493 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f302010-09-24 09:17:01 -0400494 __func__,
495 res->sr_slot - res->sr_session->fc_slot_table.slots,
496 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400497 goto out_retry;
498 default:
499 /* Just update the slot sequence no. */
Benny Halevydfb4f302010-09-24 09:17:01 -0400500 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400501 }
502out:
503 /* The session may be reset by one of the error handlers. */
504 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400505 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400506 return 1;
507out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400508 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400509 goto out;
510 rpc_delay(task, NFS4_POLL_RETRY_MAX);
511 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400512}
513
Trond Myklebust14516c32010-07-31 14:29:06 -0400514static int nfs4_sequence_done(struct rpc_task *task,
515 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400516{
Trond Myklebust14516c32010-07-31 14:29:06 -0400517 if (res->sr_session == NULL)
518 return 1;
519 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400520}
521
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400522/*
Benny Halevy510b8172009-04-01 09:22:14 -0400523 * nfs4_find_slot - efficiently look for a free slot
524 *
525 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
526 * If found, we mark the slot as used, update the highest_used_slotid,
527 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500528 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400529 *
530 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400531 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500532static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800533nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400534{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500535 u32 slotid;
536 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400537
Trond Myklebust45d43c22012-02-06 19:38:51 -0500538 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400539 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
540 tbl->max_slots);
541 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
542 if (slotid >= tbl->max_slots)
543 goto out;
544 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500545 if (slotid > tbl->highest_used_slotid ||
546 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400547 tbl->highest_used_slotid = slotid;
548 ret_id = slotid;
549out:
550 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
551 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400552 return ret_id;
553}
554
Trond Myklebust9d12b212012-01-17 22:04:25 -0500555static void nfs41_init_sequence(struct nfs4_sequence_args *args,
556 struct nfs4_sequence_res *res, int cache_reply)
557{
558 args->sa_session = NULL;
559 args->sa_cache_this = 0;
560 if (cache_reply)
561 args->sa_cache_this = 1;
562 res->sr_session = NULL;
563 res->sr_slot = NULL;
564}
565
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000566int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400567 struct nfs4_sequence_args *args,
568 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400569 struct rpc_task *task)
570{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400571 struct nfs4_slot *slot;
572 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500573 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400574
575 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400576 /* slot already allocated? */
Benny Halevydfb4f302010-09-24 09:17:01 -0400577 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400578 return 0;
579
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400580 tbl = &session->fc_slot_table;
581
582 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400583 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800584 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500585 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500586 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400587 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500588 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500589 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400590 }
591
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800592 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
593 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
594 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
595 spin_unlock(&tbl->slot_tbl_lock);
596 dprintk("%s enforce FIFO order\n", __func__);
597 return -EAGAIN;
598 }
599
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800600 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500601 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400602 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
603 spin_unlock(&tbl->slot_tbl_lock);
604 dprintk("<-- %s: no free slots\n", __func__);
605 return -EAGAIN;
606 }
607 spin_unlock(&tbl->slot_tbl_lock);
608
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800609 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400610 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400611 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400612 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400613
614 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
615
Benny Halevy99fe60d2009-04-01 09:22:29 -0400616 res->sr_session = session;
Benny Halevydfb4f302010-09-24 09:17:01 -0400617 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400618 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400619 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400620 /*
621 * sr_status is only set in decode_sequence, and so will remain
622 * set to 1 if an rpc level failure occurs.
623 */
624 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625 return 0;
626}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000627EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400628
Trond Myklebust035168a2010-06-16 09:52:26 -0400629int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400630 struct nfs4_sequence_args *args,
631 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400632 struct rpc_task *task)
633{
Trond Myklebust035168a2010-06-16 09:52:26 -0400634 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400635 int ret = 0;
636
Trond Myklebust9d12b212012-01-17 22:04:25 -0500637 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400638 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400639
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200640 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f302010-09-24 09:17:01 -0400641 __func__, session->clp, session, res->sr_slot ?
642 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400643
Trond Myklebust9d12b212012-01-17 22:04:25 -0500644 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400645out:
646 dprintk("<-- %s status=%d\n", __func__, ret);
647 return ret;
648}
649
650struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400651 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400652 struct nfs4_sequence_args *seq_args;
653 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654};
655
656static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
657{
658 struct nfs41_call_sync_data *data = calldata;
659
Trond Myklebust035168a2010-06-16 09:52:26 -0400660 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
661
662 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500663 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400664 return;
665 rpc_call_start(task);
666}
667
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800668static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
669{
670 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
671 nfs41_call_sync_prepare(task, calldata);
672}
673
Andy Adamson69ab40c2009-04-01 09:22:19 -0400674static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
675{
676 struct nfs41_call_sync_data *data = calldata;
677
Trond Myklebust14516c32010-07-31 14:29:06 -0400678 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400679}
680
Trond Myklebust17280172012-03-11 13:11:00 -0400681static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400682 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400683 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400684};
685
Trond Myklebust17280172012-03-11 13:11:00 -0400686static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800687 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
688 .rpc_call_done = nfs41_call_sync_done,
689};
690
Bryan Schumaker7c513052011-03-24 17:12:24 +0000691static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
692 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400693 struct rpc_message *msg,
694 struct nfs4_sequence_args *args,
695 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800696 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400697{
698 int ret;
699 struct rpc_task *task;
700 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400701 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400702 .seq_args = args,
703 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400704 };
705 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000706 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400707 .rpc_message = msg,
708 .callback_ops = &nfs41_call_sync_ops,
709 .callback_data = &data
710 };
711
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800712 if (privileged)
713 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400714 task = rpc_run_task(&task_setup);
715 if (IS_ERR(task))
716 ret = PTR_ERR(task);
717 else {
718 ret = task->tk_status;
719 rpc_put_task(task);
720 }
721 return ret;
722}
723
Bryan Schumaker7c513052011-03-24 17:12:24 +0000724int _nfs4_call_sync_session(struct rpc_clnt *clnt,
725 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400726 struct rpc_message *msg,
727 struct nfs4_sequence_args *args,
728 struct nfs4_sequence_res *res,
729 int cache_reply)
730{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500731 nfs41_init_sequence(args, res, cache_reply);
732 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400733}
734
Trond Myklebustdf896452010-06-16 09:52:26 -0400735#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500736static inline
737void nfs41_init_sequence(struct nfs4_sequence_args *args,
738 struct nfs4_sequence_res *res, int cache_reply)
739{
740}
741
Trond Myklebust14516c32010-07-31 14:29:06 -0400742static int nfs4_sequence_done(struct rpc_task *task,
743 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400744{
Trond Myklebust14516c32010-07-31 14:29:06 -0400745 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400746}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400747#endif /* CONFIG_NFS_V4_1 */
748
Bryan Schumaker7c513052011-03-24 17:12:24 +0000749int _nfs4_call_sync(struct rpc_clnt *clnt,
750 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400751 struct rpc_message *msg,
752 struct nfs4_sequence_args *args,
753 struct nfs4_sequence_res *res,
754 int cache_reply)
755{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500756 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000757 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400758}
759
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000760static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000761int nfs4_call_sync(struct rpc_clnt *clnt,
762 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000763 struct rpc_message *msg,
764 struct nfs4_sequence_args *args,
765 struct nfs4_sequence_res *res,
766 int cache_reply)
767{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000768 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
769 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000770}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400771
Trond Myklebust38478b22006-05-25 01:40:57 -0400772static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Trond Myklebust38478b22006-05-25 01:40:57 -0400774 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Trond Myklebust38478b22006-05-25 01:40:57 -0400776 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400777 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700778 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400779 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700780 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400781 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100784struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400785 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100786 struct nfs_openargs o_arg;
787 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100788 struct nfs_open_confirmargs c_arg;
789 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500790 struct nfs4_string owner_name;
791 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100792 struct nfs_fattr f_attr;
793 struct nfs_fattr dir_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100794 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400795 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100796 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400797 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100798 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100799 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400800 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100801 int rpc_status;
802 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100803};
804
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400805
806static void nfs4_init_opendata_res(struct nfs4_opendata *p)
807{
808 p->o_res.f_attr = &p->f_attr;
809 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400810 p->o_res.seqid = p->o_arg.seqid;
811 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400812 p->o_res.server = p->o_arg.server;
813 nfs_fattr_init(&p->f_attr);
814 nfs_fattr_init(&p->dir_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500815 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400816}
817
Al Viro82a2c1b2011-06-22 18:30:55 -0400818static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500819 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400820 const struct iattr *attrs,
821 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100822{
Al Viro82a2c1b2011-06-22 18:30:55 -0400823 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100824 struct inode *dir = parent->d_inode;
825 struct nfs_server *server = NFS_SERVER(dir);
826 struct nfs4_opendata *p;
827
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400828 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100829 if (p == NULL)
830 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400831 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100832 if (p->o_arg.seqid == NULL)
833 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400834 nfs_sb_active(dentry->d_sb);
835 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100836 p->dir = parent;
837 p->owner = sp;
838 atomic_inc(&sp->so_count);
839 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500840 p->o_arg.open_flags = flags;
841 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400842 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400843 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
844 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400845 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100846 p->o_arg.server = server;
847 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500848 p->o_arg.dir_bitmask = server->cache_consistency_bitmask;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100849 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500850 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500851 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400852
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100853 p->o_arg.u.attrs = &p->attrs;
854 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500855
856 verf[0] = jiffies;
857 verf[1] = current->pid;
858 memcpy(p->o_arg.u.verifier.data, verf,
859 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100860 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100861 p->c_arg.fh = &p->o_res.fh;
862 p->c_arg.stateid = &p->o_res.stateid;
863 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400864 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400865 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100866 return p;
867err_free:
868 kfree(p);
869err:
870 dput(parent);
871 return NULL;
872}
873
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400874static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100875{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400876 struct nfs4_opendata *p = container_of(kref,
877 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400878 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400879
880 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400881 if (p->state != NULL)
882 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400883 nfs4_put_state_owner(p->owner);
884 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400885 dput(p->dentry);
886 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500887 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400888 kfree(p);
889}
890
891static void nfs4_opendata_put(struct nfs4_opendata *p)
892{
893 if (p != NULL)
894 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100895}
896
Trond Myklebust06f814a2006-01-03 09:55:07 +0100897static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
898{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100899 int ret;
900
Trond Myklebust06f814a2006-01-03 09:55:07 +0100901 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100902 return ret;
903}
904
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500905static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400906{
907 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908
Trond Myklebust536e43d2012-01-17 22:04:26 -0500909 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500910 goto out;
911 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400912 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500913 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
914 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400915 break;
916 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500917 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
918 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400919 break;
920 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500921 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
922 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400923 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400925 return ret;
926}
927
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400929{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500930 if (delegation == NULL)
931 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500932 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400933 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500934 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400935 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500936 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400937 return 1;
938}
939
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100941{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500942 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100943 case FMODE_WRITE:
944 state->n_wronly++;
945 break;
946 case FMODE_READ:
947 state->n_rdonly++;
948 break;
949 case FMODE_READ|FMODE_WRITE:
950 state->n_rdwr++;
951 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500952 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100953}
954
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500955static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400956{
957 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500958 nfs4_stateid_copy(&state->stateid, stateid);
959 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500960 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400961 case FMODE_READ:
962 set_bit(NFS_O_RDONLY_STATE, &state->flags);
963 break;
964 case FMODE_WRITE:
965 set_bit(NFS_O_WRONLY_STATE, &state->flags);
966 break;
967 case FMODE_READ|FMODE_WRITE:
968 set_bit(NFS_O_RDWR_STATE, &state->flags);
969 }
970}
971
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500972static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400973{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400974 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500975 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400976 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400977}
978
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500979static 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 -0700980{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400981 /*
982 * Protect the call to nfs4_state_set_mode_locked and
983 * serialise the stateid update
984 */
985 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400986 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500987 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400988 set_bit(NFS_DELEGATED_STATE, &state->flags);
989 }
990 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500991 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400992 write_sequnlock(&state->seqlock);
993 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500994 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700995 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998static 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 -0500999{
1000 struct nfs_inode *nfsi = NFS_I(state->inode);
1001 struct nfs_delegation *deleg_cur;
1002 int ret = 0;
1003
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001004 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001005
1006 rcu_read_lock();
1007 deleg_cur = rcu_dereference(nfsi->delegation);
1008 if (deleg_cur == NULL)
1009 goto no_delegation;
1010
1011 spin_lock(&deleg_cur->lock);
1012 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001013 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001014 goto no_delegation_unlock;
1015
1016 if (delegation == NULL)
1017 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001018 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001019 goto no_delegation_unlock;
1020
Trond Myklebustb7391f42008-12-23 15:21:52 -05001021 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001022 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001023 ret = 1;
1024no_delegation_unlock:
1025 spin_unlock(&deleg_cur->lock);
1026no_delegation:
1027 rcu_read_unlock();
1028
1029 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001030 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001031 ret = 1;
1032 }
1033
1034 return ret;
1035}
1036
1037
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001038static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001039{
1040 struct nfs_delegation *delegation;
1041
1042 rcu_read_lock();
1043 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001044 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001045 rcu_read_unlock();
1046 return;
1047 }
1048 rcu_read_unlock();
1049 nfs_inode_return_delegation(inode);
1050}
1051
Trond Myklebust6ee41262007-07-08 14:11:36 -04001052static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001053{
1054 struct nfs4_state *state = opendata->state;
1055 struct nfs_inode *nfsi = NFS_I(state->inode);
1056 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001057 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001058 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001059 nfs4_stateid stateid;
1060 int ret = -EAGAIN;
1061
Trond Myklebustaac00a82007-07-05 19:02:21 -04001062 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001063 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001064 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001065 if (can_open_cached(state, fmode, open_mode)) {
1066 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001067 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001068 goto out_return_state;
1069 }
1070 spin_unlock(&state->owner->so_lock);
1071 }
Trond Myklebust34310432008-12-23 15:21:38 -05001072 rcu_read_lock();
1073 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001074 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001075 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001076 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001077 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001078 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001079 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001080 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001081 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001082 if (ret != 0)
1083 goto out;
1084 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001085
1086 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001087 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001088 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001089 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001090out:
1091 return ERR_PTR(ret);
1092out_return_state:
1093 atomic_inc(&state->count);
1094 return state;
1095}
1096
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001097static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1098{
1099 struct inode *inode;
1100 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001101 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001102 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001103
Trond Myklebustaac00a82007-07-05 19:02:21 -04001104 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001105 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001106 goto out;
1107 }
1108
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001109 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001110 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001111 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001112 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001113 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001114 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001115 goto err;
1116 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001117 state = nfs4_get_open_state(inode, data->owner);
1118 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001119 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001120 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001121 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001122 int delegation_flags = 0;
1123
Trond Myklebust003707c2007-07-05 18:07:55 -04001124 rcu_read_lock();
1125 delegation = rcu_dereference(NFS_I(inode)->delegation);
1126 if (delegation)
1127 delegation_flags = delegation->flags;
1128 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001129 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1130 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1131 "returning a delegation for "
1132 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001133 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001134 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001135 nfs_inode_set_delegation(state->inode,
1136 data->owner->so_cred,
1137 &data->o_res);
1138 else
1139 nfs_inode_reclaim_delegation(state->inode,
1140 data->owner->so_cred,
1141 &data->o_res);
1142 }
Trond Myklebust34310432008-12-23 15:21:38 -05001143
1144 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001145 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001146 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001147out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001148 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001149err_put_inode:
1150 iput(inode);
1151err:
1152 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001153}
1154
Trond Myklebust864472e2006-01-03 09:55:15 +01001155static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1156{
1157 struct nfs_inode *nfsi = NFS_I(state->inode);
1158 struct nfs_open_context *ctx;
1159
1160 spin_lock(&state->inode->i_lock);
1161 list_for_each_entry(ctx, &nfsi->open_files, list) {
1162 if (ctx->state != state)
1163 continue;
1164 get_nfs_open_context(ctx);
1165 spin_unlock(&state->inode->i_lock);
1166 return ctx;
1167 }
1168 spin_unlock(&state->inode->i_lock);
1169 return ERR_PTR(-ENOENT);
1170}
1171
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001172static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1173{
1174 struct nfs4_opendata *opendata;
1175
Al Viro3d4ff432011-06-22 18:40:12 -04001176 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001177 if (opendata == NULL)
1178 return ERR_PTR(-ENOMEM);
1179 opendata->state = state;
1180 atomic_inc(&state->count);
1181 return opendata;
1182}
1183
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001184static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001185{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001186 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001187 int ret;
1188
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001189 opendata->o_arg.open_flags = 0;
1190 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001191 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1192 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1193 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001194 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001195 if (ret != 0)
1196 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001197 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001198 if (IS_ERR(newstate))
1199 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001200 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001201 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001202 return 0;
1203}
1204
1205static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1206{
Trond Myklebust864472e2006-01-03 09:55:15 +01001207 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001208 int ret;
1209
1210 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001211 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001212 smp_rmb();
1213 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001214 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001215 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001216 if (ret != 0)
1217 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001218 if (newstate != state)
1219 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001220 }
1221 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001222 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001223 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001224 if (ret != 0)
1225 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001226 if (newstate != state)
1227 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001228 }
1229 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001230 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001231 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001232 if (ret != 0)
1233 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001234 if (newstate != state)
1235 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001236 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001237 /*
1238 * We may have performed cached opens for all three recoveries.
1239 * Check if we need to update the current stateid.
1240 */
1241 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001242 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001243 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001244 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001245 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001246 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001247 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001248 return 0;
1249}
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251/*
1252 * OPEN_RECLAIM:
1253 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001255static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001257 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001258 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001259 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 int status;
1261
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001262 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1263 if (IS_ERR(opendata))
1264 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001265 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1266 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001267 rcu_read_lock();
1268 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001269 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001270 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001271 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001272 opendata->o_arg.u.delegation_type = delegation_type;
1273 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001274 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return status;
1276}
1277
Trond Myklebust539cd032007-06-05 11:46:42 -04001278static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 struct nfs_server *server = NFS_SERVER(state->inode);
1281 struct nfs4_exception exception = { };
1282 int err;
1283 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001284 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001285 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001286 break;
1287 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 } while (exception.retry);
1289 return err;
1290}
1291
Trond Myklebust864472e2006-01-03 09:55:15 +01001292static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1293{
1294 struct nfs_open_context *ctx;
1295 int ret;
1296
1297 ctx = nfs4_state_find_open_context(state);
1298 if (IS_ERR(ctx))
1299 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001300 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001301 put_nfs_open_context(ctx);
1302 return ret;
1303}
1304
Trond Myklebust13437e12007-07-06 15:10:43 -04001305static 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 -07001306{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001307 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001308 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001310 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1311 if (IS_ERR(opendata))
1312 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001313 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001314 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001315 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001316 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001317 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318}
1319
Trond Myklebust13437e12007-07-06 15:10:43 -04001320int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
1322 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001323 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 int err;
1325 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001326 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 switch (err) {
1328 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001329 case -ENOENT:
1330 case -ESTALE:
1331 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001332 case -NFS4ERR_BADSESSION:
1333 case -NFS4ERR_BADSLOT:
1334 case -NFS4ERR_BAD_HIGH_SLOT:
1335 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1336 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001337 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001338 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 case -NFS4ERR_STALE_CLIENTID:
1340 case -NFS4ERR_STALE_STATEID:
1341 case -NFS4ERR_EXPIRED:
1342 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001343 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001344 goto out;
1345 case -ERESTARTSYS:
1346 /*
1347 * The show must go on: exit, but mark the
1348 * stateid as needing recovery.
1349 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001350 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001351 case -NFS4ERR_ADMIN_REVOKED:
1352 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001353 nfs_inode_find_state_and_recover(state->inode,
1354 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001355 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001356 case -EKEYEXPIRED:
1357 /*
1358 * User RPCSEC_GSS context has expired.
1359 * We cannot recover this stateid now, so
1360 * skip it and allow recovery thread to
1361 * proceed.
1362 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001363 case -ENOMEM:
1364 err = 0;
1365 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367 err = nfs4_handle_exception(server, err, &exception);
1368 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001369out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return err;
1371}
1372
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001373static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1374{
1375 struct nfs4_opendata *data = calldata;
1376
1377 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001378 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001379 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001380 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001381 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001382 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001383 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001384}
1385
1386static void nfs4_open_confirm_release(void *calldata)
1387{
1388 struct nfs4_opendata *data = calldata;
1389 struct nfs4_state *state = NULL;
1390
1391 /* If this request hasn't been cancelled, do nothing */
1392 if (data->cancelled == 0)
1393 goto out_free;
1394 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001395 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001396 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001397 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001398 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001399 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001400out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001401 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001402}
1403
1404static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001405 .rpc_call_done = nfs4_open_confirm_done,
1406 .rpc_release = nfs4_open_confirm_release,
1407};
1408
1409/*
1410 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1411 */
1412static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1413{
1414 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1415 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001416 struct rpc_message msg = {
1417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1418 .rpc_argp = &data->c_arg,
1419 .rpc_resp = &data->c_res,
1420 .rpc_cred = data->owner->so_cred,
1421 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001422 struct rpc_task_setup task_setup_data = {
1423 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001424 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001425 .callback_ops = &nfs4_open_confirm_ops,
1426 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001427 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001428 .flags = RPC_TASK_ASYNC,
1429 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 int status;
1431
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001432 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001433 data->rpc_done = 0;
1434 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001435 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001436 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001437 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001438 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001439 status = nfs4_wait_for_completion_rpc_task(task);
1440 if (status != 0) {
1441 data->cancelled = 1;
1442 smp_wmb();
1443 } else
1444 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001445 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return status;
1447}
1448
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001449static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001451 struct nfs4_opendata *data = calldata;
1452 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001453
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001454 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1455 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001456 /*
1457 * Check if we still need to send an OPEN call, or if we can use
1458 * a delegation instead.
1459 */
1460 if (data->state != NULL) {
1461 struct nfs_delegation *delegation;
1462
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001463 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001464 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001465 rcu_read_lock();
1466 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001467 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1468 can_open_delegated(delegation, data->o_arg.fmode))
1469 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001470 rcu_read_unlock();
1471 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001472 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001473 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001474 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001475 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001476 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1477 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001478 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001479 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001480 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001481 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001482 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001483 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001484 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001485unlock_no_action:
1486 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001487out_no_action:
1488 task->tk_action = NULL;
1489
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001490}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001492static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1493{
1494 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1495 nfs4_open_prepare(task, calldata);
1496}
1497
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001498static void nfs4_open_done(struct rpc_task *task, void *calldata)
1499{
1500 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001502 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001503
Trond Myklebust14516c32010-07-31 14:29:06 -04001504 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1505 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001506
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001507 if (task->tk_status == 0) {
1508 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001509 case S_IFREG:
1510 break;
1511 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001512 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001513 break;
1514 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001515 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001516 break;
1517 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001518 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001519 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001520 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001521 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1522 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001523 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001524 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001526
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001527static void nfs4_open_release(void *calldata)
1528{
1529 struct nfs4_opendata *data = calldata;
1530 struct nfs4_state *state = NULL;
1531
1532 /* If this request hasn't been cancelled, do nothing */
1533 if (data->cancelled == 0)
1534 goto out_free;
1535 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001536 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001537 goto out_free;
1538 /* In case we need an open_confirm, no cleanup! */
1539 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1540 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001541 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001542 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001543 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001544out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001545 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001546}
1547
1548static const struct rpc_call_ops nfs4_open_ops = {
1549 .rpc_call_prepare = nfs4_open_prepare,
1550 .rpc_call_done = nfs4_open_done,
1551 .rpc_release = nfs4_open_release,
1552};
1553
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001554static const struct rpc_call_ops nfs4_recover_open_ops = {
1555 .rpc_call_prepare = nfs4_recover_open_prepare,
1556 .rpc_call_done = nfs4_open_done,
1557 .rpc_release = nfs4_open_release,
1558};
1559
1560static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001561{
1562 struct inode *dir = data->dir->d_inode;
1563 struct nfs_server *server = NFS_SERVER(dir);
1564 struct nfs_openargs *o_arg = &data->o_arg;
1565 struct nfs_openres *o_res = &data->o_res;
1566 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001567 struct rpc_message msg = {
1568 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1569 .rpc_argp = o_arg,
1570 .rpc_resp = o_res,
1571 .rpc_cred = data->owner->so_cred,
1572 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001573 struct rpc_task_setup task_setup_data = {
1574 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001575 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001576 .callback_ops = &nfs4_open_ops,
1577 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001578 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001579 .flags = RPC_TASK_ASYNC,
1580 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001581 int status;
1582
Trond Myklebust9d12b212012-01-17 22:04:25 -05001583 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001584 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001585 data->rpc_done = 0;
1586 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001587 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001588 if (isrecover)
1589 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001590 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001591 if (IS_ERR(task))
1592 return PTR_ERR(task);
1593 status = nfs4_wait_for_completion_rpc_task(task);
1594 if (status != 0) {
1595 data->cancelled = 1;
1596 smp_wmb();
1597 } else
1598 status = data->rpc_status;
1599 rpc_put_task(task);
1600
1601 return status;
1602}
1603
1604static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1605{
1606 struct inode *dir = data->dir->d_inode;
1607 struct nfs_openres *o_res = &data->o_res;
1608 int status;
1609
1610 status = nfs4_run_open_task(data, 1);
1611 if (status != 0 || !data->rpc_done)
1612 return status;
1613
Trond Myklebust6926afd2012-01-07 13:22:46 -05001614 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1615
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001616 nfs_refresh_inode(dir, o_res->dir_attr);
1617
1618 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1619 status = _nfs4_proc_open_confirm(data);
1620 if (status != 0)
1621 return status;
1622 }
1623
1624 return status;
1625}
1626
1627/*
1628 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1629 */
1630static int _nfs4_proc_open(struct nfs4_opendata *data)
1631{
1632 struct inode *dir = data->dir->d_inode;
1633 struct nfs_server *server = NFS_SERVER(dir);
1634 struct nfs_openargs *o_arg = &data->o_arg;
1635 struct nfs_openres *o_res = &data->o_res;
1636 int status;
1637
1638 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001639 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001640 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001641 if (status != 0) {
1642 if (status == -NFS4ERR_BADNAME &&
1643 !(o_arg->open_flags & O_CREAT))
1644 return -ENOENT;
1645 return status;
1646 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001647
Trond Myklebust6926afd2012-01-07 13:22:46 -05001648 nfs_fattr_map_and_free_names(server, &data->f_attr);
1649
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001650 if (o_arg->open_flags & O_CREAT) {
1651 update_changeattr(dir, &o_res->cinfo);
1652 nfs_post_op_update_inode(dir, o_res->dir_attr);
1653 } else
1654 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001655 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1656 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001658 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001660 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
1662 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001663 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
Andy Adamsond83217c2011-03-01 01:34:17 +00001667static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001668{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001669 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001670 int ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001671
Trond Myklebusta78cb572009-08-09 15:06:19 -04001672 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001673 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001674 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001675 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001676 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1677 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001678 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001679 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001680 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001681 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001682 return ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001683}
1684
Andy Adamsond83217c2011-03-01 01:34:17 +00001685static int nfs4_recover_expired_lease(struct nfs_server *server)
1686{
1687 return nfs4_client_recover_expired_lease(server->nfs_client);
1688}
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690/*
1691 * OPEN_EXPIRED:
1692 * reclaim state on the server after a network partition.
1693 * Assumes caller holds the appropriate lock
1694 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001695static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001697 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001698 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001700 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1701 if (IS_ERR(opendata))
1702 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001703 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001704 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001705 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001706 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001707 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001710static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001711{
Trond Myklebust539cd032007-06-05 11:46:42 -04001712 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001713 struct nfs4_exception exception = { };
1714 int err;
1715
1716 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001717 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001718 switch (err) {
1719 default:
1720 goto out;
1721 case -NFS4ERR_GRACE:
1722 case -NFS4ERR_DELAY:
1723 nfs4_handle_exception(server, err, &exception);
1724 err = 0;
1725 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001726 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001727out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001728 return err;
1729}
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1732{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001734 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Trond Myklebust864472e2006-01-03 09:55:15 +01001736 ctx = nfs4_state_find_open_context(state);
1737 if (IS_ERR(ctx))
1738 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001739 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001740 put_nfs_open_context(ctx);
1741 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742}
1743
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001744#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001745static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001746{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001747 int status = NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001748 struct nfs_server *server = NFS_SERVER(state->inode);
1749
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001750 if (state->flags & flags) {
1751 status = nfs41_test_stateid(server, stateid);
1752 if (status != NFS_OK) {
1753 nfs41_free_stateid(server, stateid);
1754 state->flags &= ~flags;
1755 }
1756 }
1757 return status;
1758}
1759
1760static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1761{
1762 int deleg_status, open_status;
1763 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1764 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1765
1766 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1767 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1768
1769 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1770 return NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001771 return nfs4_open_expired(sp, state);
1772}
1773#endif
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001776 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1777 * fields corresponding to attributes that were used to store the verifier.
1778 * Make sure we clobber those fields in the later setattr call
1779 */
1780static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1781{
1782 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1783 !(sattr->ia_valid & ATTR_ATIME_SET))
1784 sattr->ia_valid |= ATTR_ATIME;
1785
1786 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1787 !(sattr->ia_valid & ATTR_MTIME_SET))
1788 sattr->ia_valid |= ATTR_MTIME;
1789}
1790
1791/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001792 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001794static 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 -07001795{
1796 struct nfs4_state_owner *sp;
1797 struct nfs4_state *state = NULL;
1798 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001799 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001800 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001804 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1805 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1807 goto out_err;
1808 }
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001809 status = nfs4_recover_expired_lease(server);
1810 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001811 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001812 if (dentry->d_inode != NULL)
1813 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001814 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001815 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001816 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001817 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Al Viro82a2c1b2011-06-22 18:30:55 -04001819 if (dentry->d_inode != NULL)
1820 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001821
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001822 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001824 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001826 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001827 status = PTR_ERR(state);
1828 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001829 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001830 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001831 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001832
1833 if (opendata->o_arg.open_flags & O_EXCL) {
1834 nfs4_exclusive_attrset(opendata, sattr);
1835
1836 nfs_fattr_init(opendata->o_res.f_attr);
1837 status = nfs4_do_setattr(state->inode, cred,
1838 opendata->o_res.f_attr, sattr,
1839 state);
1840 if (status == 0)
1841 nfs_setattr_update_inode(state->inode, sattr);
1842 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1843 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001844 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 *res = state;
1847 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001848err_opendata_put:
1849 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001850err_put_state_owner:
1851 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 *res = NULL;
1854 return status;
1855}
1856
1857
Al Viro82a2c1b2011-06-22 18:30:55 -04001858static 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 -07001859{
1860 struct nfs4_exception exception = { };
1861 struct nfs4_state *res;
1862 int status;
1863
1864 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001865 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 if (status == 0)
1867 break;
1868 /* NOTE: BAD_SEQID means the server and client disagree about the
1869 * book-keeping w.r.t. state-changing operations
1870 * (OPEN/CLOSE/LOCK/LOCKU...)
1871 * It is actually a sign of a bug on the client or on the server.
1872 *
1873 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001874 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 * have unhashed the old state_owner for us, and that we can
1876 * therefore safely retry using a new one. We should still warn
1877 * the user though...
1878 */
1879 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001880 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001881 " returned a bad sequence-id error!\n",
1882 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 exception.retry = 1;
1884 continue;
1885 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001886 /*
1887 * BAD_STATEID on OPEN means that the server cancelled our
1888 * state before it received the OPEN_CONFIRM.
1889 * Recover by retrying the request as per the discussion
1890 * on Page 181 of RFC3530.
1891 */
1892 if (status == -NFS4ERR_BAD_STATEID) {
1893 exception.retry = 1;
1894 continue;
1895 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001896 if (status == -EAGAIN) {
1897 /* We must have found a delegation */
1898 exception.retry = 1;
1899 continue;
1900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1902 status, &exception));
1903 } while (exception.retry);
1904 return res;
1905}
1906
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001907static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1908 struct nfs_fattr *fattr, struct iattr *sattr,
1909 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001911 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001913 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 .iap = sattr,
1915 .server = server,
1916 .bitmask = server->attr_bitmask,
1917 };
1918 struct nfs_setattrres res = {
1919 .fattr = fattr,
1920 .server = server,
1921 };
1922 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001923 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1924 .rpc_argp = &arg,
1925 .rpc_resp = &res,
1926 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001928 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001929 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Trond Myklebust0e574af2005-10-27 22:12:38 -04001931 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Trond Myklebust4fc87962012-03-08 17:42:01 -05001933 if (state != NULL) {
1934 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1935 current->files, current->tgid);
1936 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1937 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001938 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001939 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05001940 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Bryan Schumaker7c513052011-03-24 17:12:24 +00001942 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001943 if (status == 0 && state != NULL)
1944 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001945 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
1947
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001948static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1949 struct nfs_fattr *fattr, struct iattr *sattr,
1950 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001952 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001953 struct nfs4_exception exception = {
1954 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05001955 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001956 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 int err;
1958 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04001959 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
1960 switch (err) {
1961 case -NFS4ERR_OPENMODE:
1962 if (state && !(state->state & FMODE_WRITE)) {
1963 err = -EBADF;
1964 if (sattr->ia_valid & ATTR_OPEN)
1965 err = -EACCES;
1966 goto out;
1967 }
1968 }
1969 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04001971out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return err;
1973}
1974
1975struct nfs4_closedata {
1976 struct inode *inode;
1977 struct nfs4_state *state;
1978 struct nfs_closeargs arg;
1979 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001980 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001981 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001982 bool roc;
1983 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984};
1985
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001986static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001987{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001988 struct nfs4_closedata *calldata = data;
1989 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001990 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001991
Fred Isamanf7e89172011-01-06 11:36:32 +00001992 if (calldata->roc)
1993 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001994 nfs4_put_open_state(calldata->state);
1995 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001996 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001997 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001998 kfree(calldata);
1999}
2000
Trond Myklebust88069f72009-12-08 08:33:16 -05002001static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2002 fmode_t fmode)
2003{
2004 spin_lock(&state->owner->so_lock);
2005 if (!(fmode & FMODE_READ))
2006 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2007 if (!(fmode & FMODE_WRITE))
2008 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2009 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2010 spin_unlock(&state->owner->so_lock);
2011}
2012
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002013static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002015 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 struct nfs_server *server = NFS_SERVER(calldata->inode);
2018
Chuck Levera3ca5652012-03-01 17:00:40 -05002019 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002020 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2021 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 /* hmm. we are done with the inode, and in the process of freeing
2023 * the state_owner. we keep this around to process errors
2024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 switch (task->tk_status) {
2026 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002027 if (calldata->roc)
2028 pnfs_roc_set_barrier(state->inode,
2029 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002030 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002031 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002032 nfs4_close_clear_stateid_flags(state,
2033 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 break;
2035 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002036 case -NFS4ERR_OLD_STATEID:
2037 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002039 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002040 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002042 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2043 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002045 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002046 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002047 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002050static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002052 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002053 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002054 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002055
Chuck Levera3ca5652012-03-01 17:00:40 -05002056 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002057 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002058 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002059
Trond Myklebust88069f72009-12-08 08:33:16 -05002060 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2061 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002062 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002063 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002064 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002065 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002066 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2067 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2068 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002069 }
2070 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002071 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2072 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2073 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002074 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002075 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002076 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002077
2078 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002079 /* Note: exit _without_ calling nfs4_close_done */
2080 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002081 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002082 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002083
Fred Isamanf7e89172011-01-06 11:36:32 +00002084 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002085 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002086 if (calldata->roc &&
2087 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2088 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2089 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002090 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002091 }
2092 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002093
Trond Myklebust516a6af2005-10-27 22:12:41 -04002094 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002095 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002096 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002097 &calldata->arg.seq_args,
2098 &calldata->res.seq_res,
2099 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002100 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002101 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002102out:
2103 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104}
2105
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002106static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002107 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002108 .rpc_call_done = nfs4_close_done,
2109 .rpc_release = nfs4_free_closedata,
2110};
2111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112/*
2113 * It is possible for data to be read/written from a mem-mapped file
2114 * after the sys_close call (which hits the vfs layer as a flush).
2115 * This means that we can't safely call nfsv4 close on a file until
2116 * the inode is cleared. This in turn means that we are not good
2117 * NFSv4 citizens - we do not indicate to the server to update the file's
2118 * share state even when we are done with one of the three share
2119 * stateid's in the inode.
2120 *
2121 * NOTE: Caller must be holding the sp->so_owner semaphore!
2122 */
Al Viro643168c2011-06-22 18:20:23 -04002123int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002125 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002127 struct nfs4_state_owner *sp = state->owner;
2128 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002129 struct rpc_message msg = {
2130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2131 .rpc_cred = state->owner->so_cred,
2132 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002133 struct rpc_task_setup task_setup_data = {
2134 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002135 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002136 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002137 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002138 .flags = RPC_TASK_ASYNC,
2139 };
Trond Myklebust95121352005-10-18 14:20:12 -07002140 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002142 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002144 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002145 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002146 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002148 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002149 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002151 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002152 if (calldata->arg.seqid == NULL)
2153 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002154 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002155 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002156 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002157 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002158 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002159 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002160 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002161
Trond Myklebust1174dd12010-12-21 10:52:24 -05002162 msg.rpc_argp = &calldata->arg;
2163 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002164 task_setup_data.callback_data = calldata;
2165 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002166 if (IS_ERR(task))
2167 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002168 status = 0;
2169 if (wait)
2170 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002171 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002172 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002173out_free_calldata:
2174 kfree(calldata);
2175out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002176 if (roc)
2177 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002178 nfs4_put_open_state(state);
2179 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002180 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
Trond Myklebust2b484292010-09-17 10:56:51 -04002183static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002184nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 struct nfs4_state *state;
2187
Trond Myklebust565277f2007-10-15 18:17:53 -04002188 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002189 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002190 if (IS_ERR(state))
2191 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002192 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002193 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
2195
Trond Myklebust1185a552009-12-03 15:54:02 -05002196static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002197{
2198 if (ctx->state == NULL)
2199 return;
2200 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002201 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002202 else
Al Viro643168c2011-06-22 18:20:23 -04002203 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002204}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
2206static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2207{
Benny Halevy43652ad2009-04-01 09:21:54 -04002208 struct nfs4_server_caps_arg args = {
2209 .fhandle = fhandle,
2210 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 struct nfs4_server_caps_res res = {};
2212 struct rpc_message msg = {
2213 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002214 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 .rpc_resp = &res,
2216 };
2217 int status;
2218
Bryan Schumaker7c513052011-03-24 17:12:24 +00002219 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (status == 0) {
2221 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002222 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2223 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2224 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2225 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2226 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2228 server->caps |= NFS_CAP_ACLS;
2229 if (res.has_links != 0)
2230 server->caps |= NFS_CAP_HARDLINKS;
2231 if (res.has_symlinks != 0)
2232 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002233 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2234 server->caps |= NFS_CAP_FILEID;
2235 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2236 server->caps |= NFS_CAP_MODE;
2237 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2238 server->caps |= NFS_CAP_NLINK;
2239 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2240 server->caps |= NFS_CAP_OWNER;
2241 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2242 server->caps |= NFS_CAP_OWNER_GROUP;
2243 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2244 server->caps |= NFS_CAP_ATIME;
2245 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2246 server->caps |= NFS_CAP_CTIME;
2247 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2248 server->caps |= NFS_CAP_MTIME;
2249
Trond Myklebusta65318b2009-03-11 14:10:28 -04002250 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2251 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2252 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002254 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 return status;
2258}
2259
Trond Myklebust55a97592006-06-09 09:34:19 -04002260int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261{
2262 struct nfs4_exception exception = { };
2263 int err;
2264 do {
2265 err = nfs4_handle_exception(server,
2266 _nfs4_server_capabilities(server, fhandle),
2267 &exception);
2268 } while (exception.retry);
2269 return err;
2270}
2271
2272static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2273 struct nfs_fsinfo *info)
2274{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 struct nfs4_lookup_root_arg args = {
2276 .bitmask = nfs4_fattr_bitmap,
2277 };
2278 struct nfs4_lookup_res res = {
2279 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002280 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 .fh = fhandle,
2282 };
2283 struct rpc_message msg = {
2284 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2285 .rpc_argp = &args,
2286 .rpc_resp = &res,
2287 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002288
Trond Myklebust0e574af2005-10-27 22:12:38 -04002289 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002290 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
2292
2293static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2294 struct nfs_fsinfo *info)
2295{
2296 struct nfs4_exception exception = { };
2297 int err;
2298 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002299 err = _nfs4_lookup_root(server, fhandle, info);
2300 switch (err) {
2301 case 0:
2302 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002303 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002304 default:
2305 err = nfs4_handle_exception(server, err, &exception);
2306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002308out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return err;
2310}
2311
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002312static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2313 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2314{
2315 struct rpc_auth *auth;
2316 int ret;
2317
2318 auth = rpcauth_create(flavor, server->client);
2319 if (!auth) {
2320 ret = -EIO;
2321 goto out;
2322 }
2323 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002324out:
2325 return ret;
2326}
2327
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002328static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002329 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002331 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002332 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002334 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2335 flav_array[len] = RPC_AUTH_NULL;
2336 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002337
2338 for (i = 0; i < len; i++) {
2339 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002340 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002341 continue;
2342 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002343 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002344 /*
2345 * -EACCESS could mean that the user doesn't have correct permissions
2346 * to access the mount. It could also mean that we tried to mount
2347 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2348 * existing mount programs don't handle -EACCES very well so it should
2349 * be mapped to -EPERM instead.
2350 */
2351 if (status == -EACCES)
2352 status = -EPERM;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002353 return status;
2354}
2355
2356/*
2357 * get the file handle for the "/" directory on the server
2358 */
2359static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2360 struct nfs_fsinfo *info)
2361{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002362 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002363 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002364 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2365 /*
2366 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2367 * by nfs4_map_errors() as this function exits.
2368 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002369 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (status == 0)
2371 status = nfs4_server_capabilities(server, fhandle);
2372 if (status == 0)
2373 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002374 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375}
2376
Manoj Naik6b97fd32006-06-09 09:34:29 -04002377/*
2378 * Get locations and (maybe) other attributes of a referral.
2379 * Note that we'll actually follow the referral later when
2380 * we detect fsid mismatch in inode revalidation
2381 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002382static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2383 const struct qstr *name, struct nfs_fattr *fattr,
2384 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002385{
2386 int status = -ENOMEM;
2387 struct page *page = NULL;
2388 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002389
2390 page = alloc_page(GFP_KERNEL);
2391 if (page == NULL)
2392 goto out;
2393 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2394 if (locations == NULL)
2395 goto out;
2396
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002397 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002398 if (status != 0)
2399 goto out;
2400 /* Make sure server returned a different fsid for the referral */
2401 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002402 dprintk("%s: server did not return a different fsid for"
2403 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002404 status = -EIO;
2405 goto out;
2406 }
Andy Adamson533eb462011-06-13 18:25:56 -04002407 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2408 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002409
Andy Adamson533eb462011-06-13 18:25:56 -04002410 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002411 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002412 memset(fhandle, 0, sizeof(struct nfs_fh));
2413out:
2414 if (page)
2415 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002416 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002417 return status;
2418}
2419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2421{
2422 struct nfs4_getattr_arg args = {
2423 .fh = fhandle,
2424 .bitmask = server->attr_bitmask,
2425 };
2426 struct nfs4_getattr_res res = {
2427 .fattr = fattr,
2428 .server = server,
2429 };
2430 struct rpc_message msg = {
2431 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2432 .rpc_argp = &args,
2433 .rpc_resp = &res,
2434 };
2435
Trond Myklebust0e574af2005-10-27 22:12:38 -04002436 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002437 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438}
2439
2440static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2441{
2442 struct nfs4_exception exception = { };
2443 int err;
2444 do {
2445 err = nfs4_handle_exception(server,
2446 _nfs4_proc_getattr(server, fhandle, fattr),
2447 &exception);
2448 } while (exception.retry);
2449 return err;
2450}
2451
2452/*
2453 * The file is not closed if it is opened due to the a request to change
2454 * the size of the file. The open call will not be needed once the
2455 * VFS layer lookup-intents are implemented.
2456 *
2457 * Close is called when the inode is destroyed.
2458 * If we haven't opened the file for O_WRONLY, we
2459 * need to in the size_change case to obtain a stateid.
2460 *
2461 * Got race?
2462 * Because OPEN is always done by name in nfsv4, it is
2463 * possible that we opened a different file by the same
2464 * name. We can recognize this race condition, but we
2465 * can't do anything about it besides returning an error.
2466 *
2467 * This will be fixed with VFS changes (lookup-intent).
2468 */
2469static int
2470nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2471 struct iattr *sattr)
2472{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002473 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002474 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002475 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 int status;
2477
Benny Halevy8a1636c2010-07-14 15:43:57 -04002478 if (pnfs_ld_layoutret_on_setattr(inode))
2479 pnfs_return_layout(inode);
2480
Trond Myklebust0e574af2005-10-27 22:12:38 -04002481 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Trond Myklebustd5308382005-11-04 15:33:38 -05002483 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002484 if (sattr->ia_valid & ATTR_FILE) {
2485 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002486
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002487 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002488 if (ctx) {
2489 cred = ctx->cred;
2490 state = ctx->state;
2491 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002492 }
2493
Trond Myklebust536e43d2012-01-17 22:04:26 -05002494 /* Deal with open(O_TRUNC) */
2495 if (sattr->ia_valid & ATTR_OPEN)
2496 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2497
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002498 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002499 if (status == 0)
2500 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 return status;
2502}
2503
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002504static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2505 const struct qstr *name, struct nfs_fh *fhandle,
2506 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002507{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002508 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002509 int status;
2510 struct nfs4_lookup_arg args = {
2511 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002512 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002513 .name = name,
2514 };
2515 struct nfs4_lookup_res res = {
2516 .server = server,
2517 .fattr = fattr,
2518 .fh = fhandle,
2519 };
2520 struct rpc_message msg = {
2521 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2522 .rpc_argp = &args,
2523 .rpc_resp = &res,
2524 };
2525
2526 nfs_fattr_init(fattr);
2527
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002529 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 dprintk("NFS reply lookup: %d\n", status);
2531 return status;
2532}
2533
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002534static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002535{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002536 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002537 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002538 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2539 fattr->nlink = 2;
2540}
2541
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002542static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2543 struct qstr *name, struct nfs_fh *fhandle,
2544 struct nfs_fattr *fattr)
2545{
2546 struct nfs4_exception exception = { };
2547 struct rpc_clnt *client = *clnt;
2548 int err;
2549 do {
2550 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2551 switch (err) {
2552 case -NFS4ERR_BADNAME:
2553 err = -ENOENT;
2554 goto out;
2555 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002556 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002557 goto out;
2558 case -NFS4ERR_WRONGSEC:
2559 err = -EPERM;
2560 if (client != *clnt)
2561 goto out;
2562
2563 client = nfs4_create_sec_client(client, dir, name);
2564 if (IS_ERR(client))
2565 return PTR_ERR(client);
2566
2567 exception.retry = 1;
2568 break;
2569 default:
2570 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2571 }
2572 } while (exception.retry);
2573
2574out:
2575 if (err == 0)
2576 *clnt = client;
2577 else if (client != *clnt)
2578 rpc_shutdown_client(client);
2579
2580 return err;
2581}
2582
Bryan Schumaker7c513052011-03-24 17:12:24 +00002583static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2584 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002586 int status;
2587 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002588
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002589 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2590 if (client != NFS_CLIENT(dir)) {
2591 rpc_shutdown_client(client);
2592 nfs_fixup_secinfo_attributes(fattr);
2593 }
2594 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595}
2596
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002597struct rpc_clnt *
2598nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2599 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2600{
2601 int status;
2602 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2603
2604 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2605 if (status < 0) {
2606 rpc_shutdown_client(client);
2607 return ERR_PTR(status);
2608 }
2609 return client;
2610}
2611
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2613{
Trond Myklebust76b32992007-08-10 17:45:11 -04002614 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 struct nfs4_accessargs args = {
2616 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002617 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002619 struct nfs4_accessres res = {
2620 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002621 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 struct rpc_message msg = {
2623 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2624 .rpc_argp = &args,
2625 .rpc_resp = &res,
2626 .rpc_cred = entry->cred,
2627 };
2628 int mode = entry->mask;
2629 int status;
2630
2631 /*
2632 * Determine which access bits we want to ask for...
2633 */
2634 if (mode & MAY_READ)
2635 args.access |= NFS4_ACCESS_READ;
2636 if (S_ISDIR(inode->i_mode)) {
2637 if (mode & MAY_WRITE)
2638 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2639 if (mode & MAY_EXEC)
2640 args.access |= NFS4_ACCESS_LOOKUP;
2641 } else {
2642 if (mode & MAY_WRITE)
2643 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2644 if (mode & MAY_EXEC)
2645 args.access |= NFS4_ACCESS_EXECUTE;
2646 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002647
2648 res.fattr = nfs_alloc_fattr();
2649 if (res.fattr == NULL)
2650 return -ENOMEM;
2651
Bryan Schumaker7c513052011-03-24 17:12:24 +00002652 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 if (!status) {
2654 entry->mask = 0;
2655 if (res.access & NFS4_ACCESS_READ)
2656 entry->mask |= MAY_READ;
2657 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2658 entry->mask |= MAY_WRITE;
2659 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2660 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002661 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002663 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 return status;
2665}
2666
2667static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2668{
2669 struct nfs4_exception exception = { };
2670 int err;
2671 do {
2672 err = nfs4_handle_exception(NFS_SERVER(inode),
2673 _nfs4_proc_access(inode, entry),
2674 &exception);
2675 } while (exception.retry);
2676 return err;
2677}
2678
2679/*
2680 * TODO: For the time being, we don't try to get any attributes
2681 * along with any of the zero-copy operations READ, READDIR,
2682 * READLINK, WRITE.
2683 *
2684 * In the case of the first three, we want to put the GETATTR
2685 * after the read-type operation -- this is because it is hard
2686 * to predict the length of a GETATTR response in v4, and thus
2687 * align the READ data correctly. This means that the GETATTR
2688 * may end up partially falling into the page cache, and we should
2689 * shift it into the 'tail' of the xdr_buf before processing.
2690 * To do this efficiently, we need to know the total length
2691 * of data received, which doesn't seem to be available outside
2692 * of the RPC layer.
2693 *
2694 * In the case of WRITE, we also want to put the GETATTR after
2695 * the operation -- in this case because we want to make sure
2696 * we get the post-operation mtime and size. This means that
2697 * we can't use xdr_encode_pages() as written: we need a variant
2698 * of it which would leave room in the 'tail' iovec.
2699 *
2700 * Both of these changes to the XDR layer would in fact be quite
2701 * minor, but I decided to leave them for a subsequent patch.
2702 */
2703static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2704 unsigned int pgbase, unsigned int pglen)
2705{
2706 struct nfs4_readlink args = {
2707 .fh = NFS_FH(inode),
2708 .pgbase = pgbase,
2709 .pglen = pglen,
2710 .pages = &page,
2711 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002712 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 struct rpc_message msg = {
2714 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2715 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002716 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 };
2718
Bryan Schumaker7c513052011-03-24 17:12:24 +00002719 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 -07002720}
2721
2722static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2723 unsigned int pgbase, unsigned int pglen)
2724{
2725 struct nfs4_exception exception = { };
2726 int err;
2727 do {
2728 err = nfs4_handle_exception(NFS_SERVER(inode),
2729 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2730 &exception);
2731 } while (exception.retry);
2732 return err;
2733}
2734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735/*
2736 * Got race?
2737 * We will need to arrange for the VFS layer to provide an atomic open.
2738 * Until then, this create/open method is prone to inefficiency and race
2739 * conditions due to the lookup, create, and open VFS calls from sys_open()
2740 * placed on the wire.
2741 *
2742 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2743 * The file will be opened again in the subsequent VFS open call
2744 * (nfs4_proc_file_open).
2745 *
2746 * The open for read will just hang around to be used by any process that
2747 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2748 */
2749
2750static int
2751nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002752 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753{
Al Viro82a2c1b2011-06-22 18:30:55 -04002754 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002756 struct rpc_cred *cred = NULL;
2757 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 int status = 0;
2759
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002760 if (ctx != NULL) {
2761 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002762 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002763 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002765 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002766 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002767 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 if (IS_ERR(state)) {
2769 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002770 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002772 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002773 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002774 if (ctx != NULL)
2775 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002776 else
Al Viro643168c2011-06-22 18:20:23 -04002777 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778out:
2779 return status;
2780}
2781
2782static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2783{
Trond Myklebust16e42952005-10-27 22:12:44 -04002784 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002785 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002787 .name.len = name->len,
2788 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002789 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002791 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002792 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002793 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2796 .rpc_argp = &args,
2797 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002799 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Trond Myklebustd3468902010-04-16 16:22:50 -04002801 res.dir_attr = nfs_alloc_fattr();
2802 if (res.dir_attr == NULL)
2803 goto out;
2804
Bryan Schumaker7c513052011-03-24 17:12:24 +00002805 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002806 if (status == 0) {
2807 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002808 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002809 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002810 nfs_free_fattr(res.dir_attr);
2811out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 return status;
2813}
2814
2815static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2816{
2817 struct nfs4_exception exception = { };
2818 int err;
2819 do {
2820 err = nfs4_handle_exception(NFS_SERVER(dir),
2821 _nfs4_proc_remove(dir, name),
2822 &exception);
2823 } while (exception.retry);
2824 return err;
2825}
2826
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002827static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002829 struct nfs_server *server = NFS_SERVER(dir);
2830 struct nfs_removeargs *args = msg->rpc_argp;
2831 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
Trond Myklebusta65318b2009-03-11 14:10:28 -04002833 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002834 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002836 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837}
2838
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002839static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2840{
2841 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2842 &data->args.seq_args,
2843 &data->res.seq_res,
2844 task))
2845 return;
2846 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847}
2848
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002849static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002851 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2852
Trond Myklebust14516c32010-07-31 14:29:06 -04002853 if (!nfs4_sequence_done(task, &res->seq_res))
2854 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002855 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002856 return 0;
2857 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002858 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002859 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860}
2861
Jeff Laytond3d41522010-09-17 17:31:57 -04002862static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2863{
2864 struct nfs_server *server = NFS_SERVER(dir);
2865 struct nfs_renameargs *arg = msg->rpc_argp;
2866 struct nfs_renameres *res = msg->rpc_resp;
2867
2868 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2869 arg->bitmask = server->attr_bitmask;
2870 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002871 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002872}
2873
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002874static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2875{
2876 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2877 &data->args.seq_args,
2878 &data->res.seq_res,
2879 task))
2880 return;
2881 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002882}
2883
2884static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2885 struct inode *new_dir)
2886{
2887 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2888
2889 if (!nfs4_sequence_done(task, &res->seq_res))
2890 return 0;
2891 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2892 return 0;
2893
2894 update_changeattr(old_dir, &res->old_cinfo);
2895 nfs_post_op_update_inode(old_dir, res->old_fattr);
2896 update_changeattr(new_dir, &res->new_cinfo);
2897 nfs_post_op_update_inode(new_dir, res->new_fattr);
2898 return 1;
2899}
2900
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2902 struct inode *new_dir, struct qstr *new_name)
2903{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002904 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002905 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 .old_dir = NFS_FH(old_dir),
2907 .new_dir = NFS_FH(new_dir),
2908 .old_name = old_name,
2909 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002910 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002912 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002913 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002914 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 struct rpc_message msg = {
2916 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2917 .rpc_argp = &arg,
2918 .rpc_resp = &res,
2919 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002920 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Trond Myklebust011fff72010-04-16 16:22:49 -04002922 res.old_fattr = nfs_alloc_fattr();
2923 res.new_fattr = nfs_alloc_fattr();
2924 if (res.old_fattr == NULL || res.new_fattr == NULL)
2925 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Bryan Schumaker7c513052011-03-24 17:12:24 +00002927 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 if (!status) {
2929 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002930 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002932 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002934out:
2935 nfs_free_fattr(res.new_fattr);
2936 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 return status;
2938}
2939
2940static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2941 struct inode *new_dir, struct qstr *new_name)
2942{
2943 struct nfs4_exception exception = { };
2944 int err;
2945 do {
2946 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2947 _nfs4_proc_rename(old_dir, old_name,
2948 new_dir, new_name),
2949 &exception);
2950 } while (exception.retry);
2951 return err;
2952}
2953
2954static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2955{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002956 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 struct nfs4_link_arg arg = {
2958 .fh = NFS_FH(inode),
2959 .dir_fh = NFS_FH(dir),
2960 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002961 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002963 struct nfs4_link_res res = {
2964 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002965 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 struct rpc_message msg = {
2967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2968 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002969 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002971 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Trond Myklebust136f2622010-04-16 16:22:49 -04002973 res.fattr = nfs_alloc_fattr();
2974 res.dir_attr = nfs_alloc_fattr();
2975 if (res.fattr == NULL || res.dir_attr == NULL)
2976 goto out;
2977
Bryan Schumaker7c513052011-03-24 17:12:24 +00002978 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002979 if (!status) {
2980 update_changeattr(dir, &res.cinfo);
2981 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002982 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002983 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002984out:
2985 nfs_free_fattr(res.dir_attr);
2986 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 return status;
2988}
2989
2990static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2991{
2992 struct nfs4_exception exception = { };
2993 int err;
2994 do {
2995 err = nfs4_handle_exception(NFS_SERVER(inode),
2996 _nfs4_proc_link(inode, dir, name),
2997 &exception);
2998 } while (exception.retry);
2999 return err;
3000}
3001
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003002struct nfs4_createdata {
3003 struct rpc_message msg;
3004 struct nfs4_create_arg arg;
3005 struct nfs4_create_res res;
3006 struct nfs_fh fh;
3007 struct nfs_fattr fattr;
3008 struct nfs_fattr dir_fattr;
3009};
3010
3011static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3012 struct qstr *name, struct iattr *sattr, u32 ftype)
3013{
3014 struct nfs4_createdata *data;
3015
3016 data = kzalloc(sizeof(*data), GFP_KERNEL);
3017 if (data != NULL) {
3018 struct nfs_server *server = NFS_SERVER(dir);
3019
3020 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3021 data->msg.rpc_argp = &data->arg;
3022 data->msg.rpc_resp = &data->res;
3023 data->arg.dir_fh = NFS_FH(dir);
3024 data->arg.server = server;
3025 data->arg.name = name;
3026 data->arg.attrs = sattr;
3027 data->arg.ftype = ftype;
3028 data->arg.bitmask = server->attr_bitmask;
3029 data->res.server = server;
3030 data->res.fh = &data->fh;
3031 data->res.fattr = &data->fattr;
3032 data->res.dir_fattr = &data->dir_fattr;
3033 nfs_fattr_init(data->res.fattr);
3034 nfs_fattr_init(data->res.dir_fattr);
3035 }
3036 return data;
3037}
3038
3039static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3040{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003041 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003042 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003043 if (status == 0) {
3044 update_changeattr(dir, &data->res.dir_cinfo);
3045 nfs_post_op_update_inode(dir, data->res.dir_fattr);
3046 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3047 }
3048 return status;
3049}
3050
3051static void nfs4_free_createdata(struct nfs4_createdata *data)
3052{
3053 kfree(data);
3054}
3055
Chuck Lever4f390c12006-08-22 20:06:22 -04003056static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003057 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003059 struct nfs4_createdata *data;
3060 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
Chuck Lever94a6d752006-08-22 20:06:23 -04003062 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003063 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003064
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003065 status = -ENOMEM;
3066 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3067 if (data == NULL)
3068 goto out;
3069
3070 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3071 data->arg.u.symlink.pages = &page;
3072 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003074 status = nfs4_do_create(dir, dentry, data);
3075
3076 nfs4_free_createdata(data);
3077out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 return status;
3079}
3080
Chuck Lever4f390c12006-08-22 20:06:22 -04003081static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003082 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
3084 struct nfs4_exception exception = { };
3085 int err;
3086 do {
3087 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003088 _nfs4_proc_symlink(dir, dentry, page,
3089 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 &exception);
3091 } while (exception.retry);
3092 return err;
3093}
3094
3095static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3096 struct iattr *sattr)
3097{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003098 struct nfs4_createdata *data;
3099 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003101 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3102 if (data == NULL)
3103 goto out;
3104
3105 status = nfs4_do_create(dir, dentry, data);
3106
3107 nfs4_free_createdata(data);
3108out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 return status;
3110}
3111
3112static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3113 struct iattr *sattr)
3114{
3115 struct nfs4_exception exception = { };
3116 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003117
3118 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 do {
3120 err = nfs4_handle_exception(NFS_SERVER(dir),
3121 _nfs4_proc_mkdir(dir, dentry, sattr),
3122 &exception);
3123 } while (exception.retry);
3124 return err;
3125}
3126
3127static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003128 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
3130 struct inode *dir = dentry->d_inode;
3131 struct nfs4_readdir_arg args = {
3132 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003133 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 .pgbase = 0,
3135 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003136 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003137 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 };
3139 struct nfs4_readdir_res res;
3140 struct rpc_message msg = {
3141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3142 .rpc_argp = &args,
3143 .rpc_resp = &res,
3144 .rpc_cred = cred,
3145 };
3146 int status;
3147
Harvey Harrison3110ff82008-05-02 13:42:44 -07003148 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003149 dentry->d_parent->d_name.name,
3150 dentry->d_name.name,
3151 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3153 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003154 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 -05003155 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003157 status += args.pgbase;
3158 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003159
3160 nfs_invalidate_atime(dir);
3161
Harvey Harrison3110ff82008-05-02 13:42:44 -07003162 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 return status;
3164}
3165
3166static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003167 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168{
3169 struct nfs4_exception exception = { };
3170 int err;
3171 do {
3172 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3173 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003174 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 &exception);
3176 } while (exception.retry);
3177 return err;
3178}
3179
3180static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3181 struct iattr *sattr, dev_t rdev)
3182{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003183 struct nfs4_createdata *data;
3184 int mode = sattr->ia_mode;
3185 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
3187 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3188 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003189
3190 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3191 if (data == NULL)
3192 goto out;
3193
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003195 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003197 data->arg.ftype = NF4BLK;
3198 data->arg.u.device.specdata1 = MAJOR(rdev);
3199 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 }
3201 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003202 data->arg.ftype = NF4CHR;
3203 data->arg.u.device.specdata1 = MAJOR(rdev);
3204 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003207 status = nfs4_do_create(dir, dentry, data);
3208
3209 nfs4_free_createdata(data);
3210out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 return status;
3212}
3213
3214static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3215 struct iattr *sattr, dev_t rdev)
3216{
3217 struct nfs4_exception exception = { };
3218 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003219
3220 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 do {
3222 err = nfs4_handle_exception(NFS_SERVER(dir),
3223 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3224 &exception);
3225 } while (exception.retry);
3226 return err;
3227}
3228
3229static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3230 struct nfs_fsstat *fsstat)
3231{
3232 struct nfs4_statfs_arg args = {
3233 .fh = fhandle,
3234 .bitmask = server->attr_bitmask,
3235 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003236 struct nfs4_statfs_res res = {
3237 .fsstat = fsstat,
3238 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 struct rpc_message msg = {
3240 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3241 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003242 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 };
3244
Trond Myklebust0e574af2005-10-27 22:12:38 -04003245 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003246 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247}
3248
3249static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3250{
3251 struct nfs4_exception exception = { };
3252 int err;
3253 do {
3254 err = nfs4_handle_exception(server,
3255 _nfs4_proc_statfs(server, fhandle, fsstat),
3256 &exception);
3257 } while (exception.retry);
3258 return err;
3259}
3260
3261static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3262 struct nfs_fsinfo *fsinfo)
3263{
3264 struct nfs4_fsinfo_arg args = {
3265 .fh = fhandle,
3266 .bitmask = server->attr_bitmask,
3267 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003268 struct nfs4_fsinfo_res res = {
3269 .fsinfo = fsinfo,
3270 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 struct rpc_message msg = {
3272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3273 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003274 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 };
3276
Bryan Schumaker7c513052011-03-24 17:12:24 +00003277 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278}
3279
3280static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3281{
3282 struct nfs4_exception exception = { };
3283 int err;
3284
3285 do {
3286 err = nfs4_handle_exception(server,
3287 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3288 &exception);
3289 } while (exception.retry);
3290 return err;
3291}
3292
3293static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3294{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003295 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3297}
3298
3299static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3300 struct nfs_pathconf *pathconf)
3301{
3302 struct nfs4_pathconf_arg args = {
3303 .fh = fhandle,
3304 .bitmask = server->attr_bitmask,
3305 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003306 struct nfs4_pathconf_res res = {
3307 .pathconf = pathconf,
3308 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 struct rpc_message msg = {
3310 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3311 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003312 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 };
3314
3315 /* None of the pathconf attributes are mandatory to implement */
3316 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3317 memset(pathconf, 0, sizeof(*pathconf));
3318 return 0;
3319 }
3320
Trond Myklebust0e574af2005-10-27 22:12:38 -04003321 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003322 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323}
3324
3325static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3326 struct nfs_pathconf *pathconf)
3327{
3328 struct nfs4_exception exception = { };
3329 int err;
3330
3331 do {
3332 err = nfs4_handle_exception(server,
3333 _nfs4_proc_pathconf(server, fhandle, pathconf),
3334 &exception);
3335 } while (exception.retry);
3336 return err;
3337}
3338
Benny Halevyd20581a2011-05-22 19:52:03 +03003339void __nfs4_read_done_cb(struct nfs_read_data *data)
3340{
3341 nfs_invalidate_atime(data->inode);
3342}
3343
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003344static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345{
Trond Myklebustec06c092006-03-20 13:44:27 -05003346 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003348 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003349 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003350 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003352
Benny Halevyd20581a2011-05-22 19:52:03 +03003353 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003355 renew_lease(server, data->timestamp);
3356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357}
3358
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003359static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3360{
3361
3362 dprintk("--> %s\n", __func__);
3363
3364 if (!nfs4_sequence_done(task, &data->res.seq_res))
3365 return -EAGAIN;
3366
Benny Halevyd20581a2011-05-22 19:52:03 +03003367 return data->read_done_cb ? data->read_done_cb(task, data) :
3368 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003369}
3370
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003371static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003374 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003375 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003376 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377}
3378
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003379static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3380{
3381 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3382 &data->args.seq_args,
3383 &data->res.seq_res,
3384 task))
3385 return;
3386 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387}
3388
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003389/* Reset the the nfs_read_data to send the read to the MDS. */
3390void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3391{
3392 dprintk("%s Reset task for i/o through\n", __func__);
3393 put_lseg(data->lseg);
3394 data->lseg = NULL;
3395 /* offsets will differ in the dense stripe case */
3396 data->args.offset = data->mds_offset;
3397 data->ds_clp = NULL;
3398 data->args.fh = NFS_FH(data->inode);
3399 data->read_done_cb = nfs4_read_done_cb;
3400 task->tk_ops = data->mds_ops;
3401 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3402}
3403EXPORT_SYMBOL_GPL(nfs4_reset_read);
3404
Fred Isamanb029bc92011-03-03 15:13:42 +00003405static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 struct inode *inode = data->inode;
3408
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003409 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003410 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003411 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003413 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003415 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003416 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003417 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418}
3419
Fred Isamanb029bc92011-03-03 15:13:42 +00003420static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3421{
3422 if (!nfs4_sequence_done(task, &data->res.seq_res))
3423 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003424 return data->write_done_cb ? data->write_done_cb(task, data) :
3425 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003426}
3427
Fred Isamana69aef12011-03-03 15:13:47 +00003428/* Reset the the nfs_write_data to send the write to the MDS. */
3429void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3430{
3431 dprintk("%s Reset task for i/o through\n", __func__);
3432 put_lseg(data->lseg);
3433 data->lseg = NULL;
3434 data->ds_clp = NULL;
3435 data->write_done_cb = nfs4_write_done_cb;
3436 data->args.fh = NFS_FH(data->inode);
3437 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3438 data->args.offset = data->mds_offset;
3439 data->res.fattr = &data->fattr;
3440 task->tk_ops = data->mds_ops;
3441 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3442}
3443EXPORT_SYMBOL_GPL(nfs4_reset_write);
3444
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003445static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003447 struct nfs_server *server = NFS_SERVER(data->inode);
3448
Fred Isaman7ffd1062011-03-03 15:13:46 +00003449 if (data->lseg) {
3450 data->args.bitmask = NULL;
3451 data->res.fattr = NULL;
3452 } else
3453 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003454 if (!data->write_done_cb)
3455 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003456 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 data->timestamp = jiffies;
3458
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003459 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003460 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461}
3462
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003463static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3464{
3465 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3466 &data->args.seq_args,
3467 &data->res.seq_res,
3468 task))
3469 return;
3470 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471}
3472
Fred Isaman5f452432011-03-23 13:27:46 +00003473static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003476
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003477 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003478 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003479 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003481 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003482 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483}
3484
Fred Isaman5f452432011-03-23 13:27:46 +00003485static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3486{
3487 if (!nfs4_sequence_done(task, &data->res.seq_res))
3488 return -EAGAIN;
3489 return data->write_done_cb(task, data);
3490}
3491
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003492static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003494 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003495
3496 if (data->lseg) {
3497 data->args.bitmask = NULL;
3498 data->res.fattr = NULL;
3499 } else
3500 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003501 if (!data->write_done_cb)
3502 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003503 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003504 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003505 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506}
3507
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003508struct nfs4_renewdata {
3509 struct nfs_client *client;
3510 unsigned long timestamp;
3511};
3512
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513/*
3514 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3515 * standalone procedure for queueing an asynchronous RENEW.
3516 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003517static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003518{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003519 struct nfs4_renewdata *data = calldata;
3520 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003521
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003522 if (atomic_read(&clp->cl_count) > 1)
3523 nfs4_schedule_state_renewal(clp);
3524 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003525 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003526}
3527
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003528static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003530 struct nfs4_renewdata *data = calldata;
3531 struct nfs_client *clp = data->client;
3532 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
3534 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003535 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003536 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3537 return;
3538 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003539 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003540 return;
3541 }
3542 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003544 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545}
3546
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003547static const struct rpc_call_ops nfs4_renew_ops = {
3548 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003549 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003550};
3551
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003552static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553{
3554 struct rpc_message msg = {
3555 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3556 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003557 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003559 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003561 if (renew_flags == 0)
3562 return 0;
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003563 if (!atomic_inc_not_zero(&clp->cl_count))
3564 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003565 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003566 if (data == NULL)
3567 return -ENOMEM;
3568 data->client = clp;
3569 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003571 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572}
3573
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003574static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575{
3576 struct rpc_message msg = {
3577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3578 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003579 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 };
3581 unsigned long now = jiffies;
3582 int status;
3583
3584 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3585 if (status < 0)
3586 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003587 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 return 0;
3589}
3590
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003591static inline int nfs4_server_supports_acls(struct nfs_server *server)
3592{
3593 return (server->caps & NFS_CAP_ACLS)
3594 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3595 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3596}
3597
3598/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3599 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3600 * the stack.
3601 */
3602#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3603
Neil Hormane9e3d722011-03-04 19:26:03 -05003604static int buf_to_pages_noslab(const void *buf, size_t buflen,
3605 struct page **pages, unsigned int *pgbase)
3606{
3607 struct page *newpage, **spages;
3608 int rc = 0;
3609 size_t len;
3610 spages = pages;
3611
3612 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003613 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003614 newpage = alloc_page(GFP_KERNEL);
3615
3616 if (newpage == NULL)
3617 goto unwind;
3618 memcpy(page_address(newpage), buf, len);
3619 buf += len;
3620 buflen -= len;
3621 *pages++ = newpage;
3622 rc++;
3623 } while (buflen != 0);
3624
3625 return rc;
3626
3627unwind:
3628 for(; rc > 0; rc--)
3629 __free_page(spages[rc-1]);
3630 return -ENOMEM;
3631}
3632
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003633struct nfs4_cached_acl {
3634 int cached;
3635 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003636 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003637};
3638
3639static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003640{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003641 struct nfs_inode *nfsi = NFS_I(inode);
3642
3643 spin_lock(&inode->i_lock);
3644 kfree(nfsi->nfs4_acl);
3645 nfsi->nfs4_acl = acl;
3646 spin_unlock(&inode->i_lock);
3647}
3648
3649static void nfs4_zap_acl_attr(struct inode *inode)
3650{
3651 nfs4_set_cached_acl(inode, NULL);
3652}
3653
3654static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3655{
3656 struct nfs_inode *nfsi = NFS_I(inode);
3657 struct nfs4_cached_acl *acl;
3658 int ret = -ENOENT;
3659
3660 spin_lock(&inode->i_lock);
3661 acl = nfsi->nfs4_acl;
3662 if (acl == NULL)
3663 goto out;
3664 if (buf == NULL) /* user is just asking for length */
3665 goto out_len;
3666 if (acl->cached == 0)
3667 goto out;
3668 ret = -ERANGE; /* see getxattr(2) man page */
3669 if (acl->len > buflen)
3670 goto out;
3671 memcpy(buf, acl->data, acl->len);
3672out_len:
3673 ret = acl->len;
3674out:
3675 spin_unlock(&inode->i_lock);
3676 return ret;
3677}
3678
Sachin Prabhu5794d212012-04-17 14:36:40 +01003679static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003680{
3681 struct nfs4_cached_acl *acl;
3682
Sachin Prabhu5794d212012-04-17 14:36:40 +01003683 if (pages && acl_len <= PAGE_SIZE) {
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003684 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3685 if (acl == NULL)
3686 goto out;
3687 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003688 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003689 } else {
3690 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3691 if (acl == NULL)
3692 goto out;
3693 acl->cached = 0;
3694 }
3695 acl->len = acl_len;
3696out:
3697 nfs4_set_cached_acl(inode, acl);
3698}
3699
Andy Adamsonbf118a32011-12-07 11:55:27 -05003700/*
3701 * The getxattr API returns the required buffer length when called with a
3702 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3703 * the required buf. On a NULL buf, we send a page of data to the server
3704 * guessing that the ACL request can be serviced by a page. If so, we cache
3705 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3706 * the cache. If not so, we throw away the page, and cache the required
3707 * length. The next getxattr call will then produce another round trip to
3708 * the server, this time with the input buf of the required size.
3709 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003710static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003711{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003712 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003713 struct nfs_getaclargs args = {
3714 .fh = NFS_FH(inode),
3715 .acl_pages = pages,
3716 .acl_len = buflen,
3717 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003718 struct nfs_getaclres res = {
3719 .acl_len = buflen,
3720 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003721 struct rpc_message msg = {
3722 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3723 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003724 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003725 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003726 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003727
Andy Adamsonbf118a32011-12-07 11:55:27 -05003728 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3729 /* As long as we're doing a round trip to the server anyway,
3730 * let's be prepared for a page of acl data. */
3731 if (npages == 0)
3732 npages = 1;
3733
Sachin Prabhu5a006892012-04-17 14:35:39 +01003734 /* Add an extra page to handle the bitmap returned */
3735 npages++;
3736
Andy Adamsonbf118a32011-12-07 11:55:27 -05003737 for (i = 0; i < npages; i++) {
3738 pages[i] = alloc_page(GFP_KERNEL);
3739 if (!pages[i])
3740 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003741 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003742
3743 /* for decoding across pages */
3744 res.acl_scratch = alloc_page(GFP_KERNEL);
3745 if (!res.acl_scratch)
3746 goto out_free;
3747
Andy Adamsonbf118a32011-12-07 11:55:27 -05003748 args.acl_len = npages * PAGE_SIZE;
3749 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003750
Andy Adamsonbf118a32011-12-07 11:55:27 -05003751 /* Let decode_getfacl know not to fail if the ACL data is larger than
3752 * the page we send as a guess */
3753 if (buf == NULL)
3754 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003755
Peng Taode040be2012-01-10 22:42:47 +08003756 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003757 __func__, buf, buflen, npages, args.acl_len);
3758 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3759 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003760 if (ret)
3761 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003762
3763 acl_len = res.acl_len - res.acl_data_offset;
3764 if (acl_len > args.acl_len)
Sachin Prabhu5794d212012-04-17 14:36:40 +01003765 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003766 else
Sachin Prabhu5794d212012-04-17 14:36:40 +01003767 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
Andy Adamsonbf118a32011-12-07 11:55:27 -05003768 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003769 if (buf) {
3770 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003771 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003772 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003773 _copy_from_pages(buf, pages, res.acl_data_offset,
Sachin Prabhu20e0fa92012-03-22 16:46:28 +00003774 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003775 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003776 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003777out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003778 for (i = 0; i < npages; i++)
3779 if (pages[i])
3780 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003781 if (res.acl_scratch)
3782 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003783 return ret;
3784}
3785
Trond Myklebust16b42892006-08-24 12:27:15 -04003786static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3787{
3788 struct nfs4_exception exception = { };
3789 ssize_t ret;
3790 do {
3791 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3792 if (ret >= 0)
3793 break;
3794 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3795 } while (exception.retry);
3796 return ret;
3797}
3798
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003799static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3800{
3801 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003802 int ret;
3803
3804 if (!nfs4_server_supports_acls(server))
3805 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003806 ret = nfs_revalidate_inode(server, inode);
3807 if (ret < 0)
3808 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003809 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3810 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003811 ret = nfs4_read_cached_acl(inode, buf, buflen);
3812 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003813 /* -ENOENT is returned if there is no ACL or if there is an ACL
3814 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003815 return ret;
3816 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003817}
3818
Trond Myklebust16b42892006-08-24 12:27:15 -04003819static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003820{
3821 struct nfs_server *server = NFS_SERVER(inode);
3822 struct page *pages[NFS4ACL_MAXPAGES];
3823 struct nfs_setaclargs arg = {
3824 .fh = NFS_FH(inode),
3825 .acl_pages = pages,
3826 .acl_len = buflen,
3827 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003828 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003829 struct rpc_message msg = {
3830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3831 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003832 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003833 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003834 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003835
3836 if (!nfs4_server_supports_acls(server))
3837 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003838 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3839 if (i < 0)
3840 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003841 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003842 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003843
3844 /*
3845 * Free each page after tx, so the only ref left is
3846 * held by the network stack
3847 */
3848 for (; i > 0; i--)
3849 put_page(pages[i-1]);
3850
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003851 /*
3852 * Acl update can result in inode attribute update.
3853 * so mark the attribute cache invalid.
3854 */
3855 spin_lock(&inode->i_lock);
3856 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3857 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003858 nfs_access_zap_cache(inode);
3859 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003860 return ret;
3861}
3862
Trond Myklebust16b42892006-08-24 12:27:15 -04003863static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3864{
3865 struct nfs4_exception exception = { };
3866 int err;
3867 do {
3868 err = nfs4_handle_exception(NFS_SERVER(inode),
3869 __nfs4_proc_set_acl(inode, buf, buflen),
3870 &exception);
3871 } while (exception.retry);
3872 return err;
3873}
3874
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003876nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003878 struct nfs_client *clp = server->nfs_client;
3879
3880 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 return 0;
3882 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003883 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003884 case -NFS4ERR_ADMIN_REVOKED:
3885 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003886 if (state == NULL)
3887 break;
3888 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003889 case -NFS4ERR_OPENMODE:
3890 if (state == NULL)
3891 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003892 nfs4_schedule_stateid_recovery(server, state);
3893 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003894 case -NFS4ERR_EXPIRED:
3895 if (state != NULL)
3896 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003898 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003899 nfs4_schedule_lease_recovery(clp);
3900 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003901#if defined(CONFIG_NFS_V4_1)
3902 case -NFS4ERR_BADSESSION:
3903 case -NFS4ERR_BADSLOT:
3904 case -NFS4ERR_BAD_HIGH_SLOT:
3905 case -NFS4ERR_DEADSESSION:
3906 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3907 case -NFS4ERR_SEQ_FALSE_RETRY:
3908 case -NFS4ERR_SEQ_MISORDERED:
3909 dprintk("%s ERROR %d, Reset session\n", __func__,
3910 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003911 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003912 task->tk_status = 0;
3913 return -EAGAIN;
3914#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003916 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003917 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003918 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3920 task->tk_status = 0;
3921 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003922 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 case -NFS4ERR_OLD_STATEID:
3924 task->tk_status = 0;
3925 return -EAGAIN;
3926 }
3927 task->tk_status = nfs4_map_errors(task->tk_status);
3928 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003929wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003930 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003931 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3932 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3933 task->tk_status = 0;
3934 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935}
3936
Chuck Levercd937102012-03-02 17:14:31 -05003937static void nfs4_construct_boot_verifier(struct nfs_client *clp,
3938 nfs4_verifier *bootverf)
3939{
3940 __be32 verf[2];
3941
3942 verf[0] = htonl((u32)clp->cl_boot_time.tv_sec);
3943 verf[1] = htonl((u32)clp->cl_boot_time.tv_nsec);
3944 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3945}
3946
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003947int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3948 unsigned short port, struct rpc_cred *cred,
3949 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950{
3951 nfs4_verifier sc_verifier;
3952 struct nfs4_setclientid setclientid = {
3953 .sc_verifier = &sc_verifier,
3954 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003955 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 };
3957 struct rpc_message msg = {
3958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3959 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003960 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003961 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 int loop = 0;
3964 int status;
3965
Chuck Levercd937102012-03-02 17:14:31 -05003966 nfs4_construct_boot_verifier(clp, &sc_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967
3968 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05003969 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003971 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003972 clp->cl_ipaddr,
3973 rpc_peeraddr2str(clp->cl_rpcclient,
3974 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003975 rpc_peeraddr2str(clp->cl_rpcclient,
3976 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003977 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 clp->cl_id_uniquifier);
3979 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003980 sizeof(setclientid.sc_netid),
3981 rpc_peeraddr2str(clp->cl_rpcclient,
3982 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003984 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05003986 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003988 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 if (status != -NFS4ERR_CLID_INUSE)
3990 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003991 if (loop != 0) {
3992 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003994 }
3995 ++loop;
3996 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 }
3998 return status;
3999}
4000
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004001int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004002 struct nfs4_setclientid_res *arg,
4003 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004{
4005 struct nfs_fsinfo fsinfo;
4006 struct rpc_message msg = {
4007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004008 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004010 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 };
4012 unsigned long now;
4013 int status;
4014
4015 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004016 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 if (status == 0) {
4018 spin_lock(&clp->cl_lock);
4019 clp->cl_lease_time = fsinfo.lease_time * HZ;
4020 clp->cl_last_renewal = now;
4021 spin_unlock(&clp->cl_lock);
4022 }
4023 return status;
4024}
4025
Trond Myklebustfe650402006-01-03 09:55:18 +01004026struct nfs4_delegreturndata {
4027 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004028 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004029 struct nfs_fh fh;
4030 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004031 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004032 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004033 int rpc_status;
4034};
4035
Trond Myklebustfe650402006-01-03 09:55:18 +01004036static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4037{
4038 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004039
Trond Myklebust14516c32010-07-31 14:29:06 -04004040 if (!nfs4_sequence_done(task, &data->res.seq_res))
4041 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004042
Ricardo Labiaga79708862009-12-07 09:23:21 -05004043 switch (task->tk_status) {
4044 case -NFS4ERR_STALE_STATEID:
4045 case -NFS4ERR_EXPIRED:
4046 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004047 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004048 break;
4049 default:
4050 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4051 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004052 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004053 return;
4054 }
4055 }
4056 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004057}
4058
4059static void nfs4_delegreturn_release(void *calldata)
4060{
Trond Myklebustfe650402006-01-03 09:55:18 +01004061 kfree(calldata);
4062}
4063
Andy Adamson938e1012009-04-01 09:22:28 -04004064#if defined(CONFIG_NFS_V4_1)
4065static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4066{
4067 struct nfs4_delegreturndata *d_data;
4068
4069 d_data = (struct nfs4_delegreturndata *)data;
4070
Trond Myklebust035168a2010-06-16 09:52:26 -04004071 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004072 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004073 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004074 return;
4075 rpc_call_start(task);
4076}
4077#endif /* CONFIG_NFS_V4_1 */
4078
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004079static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004080#if defined(CONFIG_NFS_V4_1)
4081 .rpc_call_prepare = nfs4_delegreturn_prepare,
4082#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004083 .rpc_call_done = nfs4_delegreturn_done,
4084 .rpc_release = nfs4_delegreturn_release,
4085};
4086
Trond Myklebuste6f81072008-01-24 18:14:34 -05004087static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004088{
4089 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004090 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004091 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004092 struct rpc_message msg = {
4093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4094 .rpc_cred = cred,
4095 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004096 struct rpc_task_setup task_setup_data = {
4097 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004098 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004099 .callback_ops = &nfs4_delegreturn_ops,
4100 .flags = RPC_TASK_ASYNC,
4101 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004102 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004103
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004104 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004105 if (data == NULL)
4106 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004107 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004108 data->args.fhandle = &data->fh;
4109 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004110 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004111 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004112 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004113 data->res.fattr = &data->fattr;
4114 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004115 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004116 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004117 data->rpc_status = 0;
4118
Trond Myklebustc970aa82007-07-14 15:39:59 -04004119 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004120 msg.rpc_argp = &data->args;
4121 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004122 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004123 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004124 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004125 if (!issync)
4126 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004127 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004128 if (status != 0)
4129 goto out;
4130 status = data->rpc_status;
4131 if (status != 0)
4132 goto out;
4133 nfs_refresh_inode(inode, &data->fattr);
4134out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004135 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004136 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137}
4138
Trond Myklebuste6f81072008-01-24 18:14:34 -05004139int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140{
4141 struct nfs_server *server = NFS_SERVER(inode);
4142 struct nfs4_exception exception = { };
4143 int err;
4144 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004145 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 switch (err) {
4147 case -NFS4ERR_STALE_STATEID:
4148 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 case 0:
4150 return 0;
4151 }
4152 err = nfs4_handle_exception(server, err, &exception);
4153 } while (exception.retry);
4154 return err;
4155}
4156
4157#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4158#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4159
4160/*
4161 * sleep, with exponential backoff, and retry the LOCK operation.
4162 */
4163static unsigned long
4164nfs4_set_lock_task_retry(unsigned long timeout)
4165{
Jeff Laytond3103102011-12-01 22:44:39 +01004166 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 timeout <<= 1;
4168 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4169 return NFS4_LOCK_MAXTIMEOUT;
4170 return timeout;
4171}
4172
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4174{
4175 struct inode *inode = state->inode;
4176 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004177 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004178 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004180 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004182 struct nfs_lockt_res res = {
4183 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 };
4185 struct rpc_message msg = {
4186 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4187 .rpc_argp = &arg,
4188 .rpc_resp = &res,
4189 .rpc_cred = state->owner->so_cred,
4190 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 struct nfs4_lock_state *lsp;
4192 int status;
4193
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004194 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004195 status = nfs4_set_lock_state(state, request);
4196 if (status != 0)
4197 goto out;
4198 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004199 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004200 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004201 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004202 switch (status) {
4203 case 0:
4204 request->fl_type = F_UNLCK;
4205 break;
4206 case -NFS4ERR_DENIED:
4207 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004209 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004210out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 return status;
4212}
4213
4214static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4215{
4216 struct nfs4_exception exception = { };
4217 int err;
4218
4219 do {
4220 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4221 _nfs4_proc_getlk(state, cmd, request),
4222 &exception);
4223 } while (exception.retry);
4224 return err;
4225}
4226
4227static int do_vfs_lock(struct file *file, struct file_lock *fl)
4228{
4229 int res = 0;
4230 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4231 case FL_POSIX:
4232 res = posix_lock_file_wait(file, fl);
4233 break;
4234 case FL_FLOCK:
4235 res = flock_lock_file_wait(file, fl);
4236 break;
4237 default:
4238 BUG();
4239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 return res;
4241}
4242
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004243struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004244 struct nfs_locku_args arg;
4245 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004246 struct nfs4_lock_state *lsp;
4247 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004248 struct file_lock fl;
4249 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004250 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004251};
4252
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004253static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4254 struct nfs_open_context *ctx,
4255 struct nfs4_lock_state *lsp,
4256 struct nfs_seqid *seqid)
4257{
4258 struct nfs4_unlockdata *p;
4259 struct inode *inode = lsp->ls_state->inode;
4260
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004261 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004262 if (p == NULL)
4263 return NULL;
4264 p->arg.fh = NFS_FH(inode);
4265 p->arg.fl = &p->fl;
4266 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004267 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004268 p->arg.stateid = &lsp->ls_stateid;
4269 p->lsp = lsp;
4270 atomic_inc(&lsp->ls_count);
4271 /* Ensure we don't close file until we're done freeing locks! */
4272 p->ctx = get_nfs_open_context(ctx);
4273 memcpy(&p->fl, fl, sizeof(p->fl));
4274 p->server = NFS_SERVER(inode);
4275 return p;
4276}
4277
Trond Myklebust06f814a2006-01-03 09:55:07 +01004278static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004279{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004280 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004281 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004282 nfs4_put_lock_state(calldata->lsp);
4283 put_nfs_open_context(calldata->ctx);
4284 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004285}
4286
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004287static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004288{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004289 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004290
Trond Myklebust14516c32010-07-31 14:29:06 -04004291 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4292 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004293 switch (task->tk_status) {
4294 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004295 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4296 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004297 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004298 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004299 case -NFS4ERR_BAD_STATEID:
4300 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004301 case -NFS4ERR_STALE_STATEID:
4302 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004303 break;
4304 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004305 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004306 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004307 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004308}
4309
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004310static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004311{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004312 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004314 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004315 return;
4316 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004317 /* Note: exit _without_ running nfs4_locku_done */
4318 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004319 return;
4320 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004321 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004322 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004323 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004324 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004325 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004326 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327}
4328
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004329static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004330 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004331 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004332 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004333};
4334
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004335static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4336 struct nfs_open_context *ctx,
4337 struct nfs4_lock_state *lsp,
4338 struct nfs_seqid *seqid)
4339{
4340 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004341 struct rpc_message msg = {
4342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4343 .rpc_cred = ctx->cred,
4344 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004345 struct rpc_task_setup task_setup_data = {
4346 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004347 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004348 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004349 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004350 .flags = RPC_TASK_ASYNC,
4351 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004352
Frank Filz137d6ac2007-07-09 15:32:29 -07004353 /* Ensure this is an unlock - when canceling a lock, the
4354 * canceled lock is passed in, and it won't be an unlock.
4355 */
4356 fl->fl_type = F_UNLCK;
4357
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004358 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4359 if (data == NULL) {
4360 nfs_free_seqid(seqid);
4361 return ERR_PTR(-ENOMEM);
4362 }
4363
Trond Myklebust9d12b212012-01-17 22:04:25 -05004364 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004365 msg.rpc_argp = &data->arg;
4366 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004367 task_setup_data.callback_data = data;
4368 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004369}
4370
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4372{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004373 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004374 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004375 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004376 struct rpc_task *task;
4377 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004378 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
Trond Myklebust9b073572006-06-29 16:38:34 -04004380 status = nfs4_set_lock_state(state, request);
4381 /* Unlock _before_ we do the RPC call */
4382 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004383 down_read(&nfsi->rwsem);
4384 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4385 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004386 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004387 }
4388 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004389 if (status != 0)
4390 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004391 /* Is this a delegated lock? */
4392 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004393 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004394 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004395 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004396 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004397 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004398 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004399 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004400 status = PTR_ERR(task);
4401 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004402 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004403 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004404 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004405out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004406 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004407 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408}
4409
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004410struct nfs4_lockdata {
4411 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004412 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004413 struct nfs4_lock_state *lsp;
4414 struct nfs_open_context *ctx;
4415 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004416 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004417 int rpc_status;
4418 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004419 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004420};
4421
4422static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004423 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4424 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004425{
4426 struct nfs4_lockdata *p;
4427 struct inode *inode = lsp->ls_state->inode;
4428 struct nfs_server *server = NFS_SERVER(inode);
4429
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004430 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004431 if (p == NULL)
4432 return NULL;
4433
4434 p->arg.fh = NFS_FH(inode);
4435 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004436 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004437 if (p->arg.open_seqid == NULL)
4438 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004439 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004440 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004441 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004442 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004443 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004444 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004445 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004446 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004447 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004448 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004449 atomic_inc(&lsp->ls_count);
4450 p->ctx = get_nfs_open_context(ctx);
4451 memcpy(&p->fl, fl, sizeof(p->fl));
4452 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004453out_free_seqid:
4454 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004455out_free:
4456 kfree(p);
4457 return NULL;
4458}
4459
4460static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4461{
4462 struct nfs4_lockdata *data = calldata;
4463 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
Harvey Harrison3110ff82008-05-02 13:42:44 -07004465 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004466 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4467 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004468 /* Do we need to do an open_to_lock_owner? */
4469 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004470 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4471 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004472 data->arg.open_stateid = &state->stateid;
4473 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004474 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004475 } else
4476 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004477 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004478 if (nfs4_setup_sequence(data->server,
4479 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004480 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004481 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004482 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004483 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004484}
4485
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004486static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4487{
4488 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4489 nfs4_lock_prepare(task, calldata);
4490}
4491
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004492static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4493{
4494 struct nfs4_lockdata *data = calldata;
4495
Harvey Harrison3110ff82008-05-02 13:42:44 -07004496 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004497
Trond Myklebust14516c32010-07-31 14:29:06 -04004498 if (!nfs4_sequence_done(task, &data->res.seq_res))
4499 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004500
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004501 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004502 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004503 if (data->rpc_status == 0)
4504 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4505 else
4506 goto out;
4507 }
4508 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004509 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004510 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004511 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004512 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004513out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004514 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004515}
4516
4517static void nfs4_lock_release(void *calldata)
4518{
4519 struct nfs4_lockdata *data = calldata;
4520
Harvey Harrison3110ff82008-05-02 13:42:44 -07004521 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004522 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004523 if (data->cancelled != 0) {
4524 struct rpc_task *task;
4525 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4526 data->arg.lock_seqid);
4527 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004528 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004529 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004530 } else
4531 nfs_free_seqid(data->arg.lock_seqid);
4532 nfs4_put_lock_state(data->lsp);
4533 put_nfs_open_context(data->ctx);
4534 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004535 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004536}
4537
4538static const struct rpc_call_ops nfs4_lock_ops = {
4539 .rpc_call_prepare = nfs4_lock_prepare,
4540 .rpc_call_done = nfs4_lock_done,
4541 .rpc_release = nfs4_lock_release,
4542};
4543
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004544static const struct rpc_call_ops nfs4_recover_lock_ops = {
4545 .rpc_call_prepare = nfs4_recover_lock_prepare,
4546 .rpc_call_done = nfs4_lock_done,
4547 .rpc_release = nfs4_lock_release,
4548};
4549
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004550static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4551{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004552 switch (error) {
4553 case -NFS4ERR_ADMIN_REVOKED:
4554 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004555 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004556 if (new_lock_owner != 0 ||
4557 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004558 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004559 break;
4560 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004561 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004562 case -NFS4ERR_EXPIRED:
4563 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004564 };
4565}
4566
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004567static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004568{
4569 struct nfs4_lockdata *data;
4570 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004571 struct rpc_message msg = {
4572 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4573 .rpc_cred = state->owner->so_cred,
4574 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004575 struct rpc_task_setup task_setup_data = {
4576 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004577 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004578 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004579 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004580 .flags = RPC_TASK_ASYNC,
4581 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004582 int ret;
4583
Harvey Harrison3110ff82008-05-02 13:42:44 -07004584 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004585 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004586 fl->fl_u.nfs4_fl.owner,
4587 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004588 if (data == NULL)
4589 return -ENOMEM;
4590 if (IS_SETLKW(cmd))
4591 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004592 if (recovery_type > NFS_LOCK_NEW) {
4593 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004594 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004595 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4596 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004597 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004598 msg.rpc_argp = &data->arg;
4599 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004600 task_setup_data.callback_data = data;
4601 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004602 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004603 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004604 ret = nfs4_wait_for_completion_rpc_task(task);
4605 if (ret == 0) {
4606 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004607 if (ret)
4608 nfs4_handle_setlk_error(data->server, data->lsp,
4609 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004610 } else
4611 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004612 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004613 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004614 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615}
4616
4617static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4618{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004619 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004620 struct nfs4_exception exception = {
4621 .inode = state->inode,
4622 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004623 int err;
4624
4625 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004626 /* Cache the lock if possible... */
4627 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4628 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004629 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004630 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004631 break;
4632 nfs4_handle_exception(server, err, &exception);
4633 } while (exception.retry);
4634 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635}
4636
4637static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4638{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004639 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004640 struct nfs4_exception exception = {
4641 .inode = state->inode,
4642 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004643 int err;
4644
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004645 err = nfs4_set_lock_state(state, request);
4646 if (err != 0)
4647 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004648 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004649 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4650 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004651 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004652 switch (err) {
4653 default:
4654 goto out;
4655 case -NFS4ERR_GRACE:
4656 case -NFS4ERR_DELAY:
4657 nfs4_handle_exception(server, err, &exception);
4658 err = 0;
4659 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004660 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004661out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004662 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663}
4664
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004665#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004666static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004667{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004668 int status, ret = NFS_OK;
4669 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004670 struct nfs_server *server = NFS_SERVER(state->inode);
4671
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004672 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4673 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4674 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4675 if (status != NFS_OK) {
4676 nfs41_free_stateid(server, &lsp->ls_stateid);
4677 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4678 ret = status;
4679 }
4680 }
4681 };
4682
4683 return ret;
4684}
4685
4686static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4687{
4688 int status = NFS_OK;
4689
4690 if (test_bit(LK_STATE_IN_USE, &state->flags))
4691 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004692 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004693 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004694 return nfs4_lock_expired(state, request);
4695}
4696#endif
4697
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4699{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004700 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004701 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004702 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004704 if ((fl_flags & FL_POSIX) &&
4705 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4706 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004707 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004708 status = nfs4_set_lock_state(state, request);
4709 if (status != 0)
4710 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004711 request->fl_flags |= FL_ACCESS;
4712 status = do_vfs_lock(request->fl_file, request);
4713 if (status < 0)
4714 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004715 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004716 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004717 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004718 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004719 request->fl_flags = fl_flags & ~FL_SLEEP;
4720 status = do_vfs_lock(request->fl_file, request);
4721 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004722 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004723 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004724 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004725 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004726 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004727 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004728 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004729 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4730 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004731out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004732 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004733out:
4734 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 return status;
4736}
4737
4738static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4739{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004740 struct nfs4_exception exception = {
4741 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004742 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004743 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 int err;
4745
4746 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004747 err = _nfs4_proc_setlk(state, cmd, request);
4748 if (err == -NFS4ERR_DENIED)
4749 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004751 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 } while (exception.retry);
4753 return err;
4754}
4755
4756static int
4757nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4758{
4759 struct nfs_open_context *ctx;
4760 struct nfs4_state *state;
4761 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4762 int status;
4763
4764 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004765 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 state = ctx->state;
4767
4768 if (request->fl_start < 0 || request->fl_end < 0)
4769 return -EINVAL;
4770
Trond Myklebustd9531262009-07-21 19:22:38 -04004771 if (IS_GETLK(cmd)) {
4772 if (state != NULL)
4773 return nfs4_proc_getlk(state, F_GETLK, request);
4774 return 0;
4775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
4777 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4778 return -EINVAL;
4779
Trond Myklebustd9531262009-07-21 19:22:38 -04004780 if (request->fl_type == F_UNLCK) {
4781 if (state != NULL)
4782 return nfs4_proc_unlck(state, cmd, request);
4783 return 0;
4784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
Trond Myklebustd9531262009-07-21 19:22:38 -04004786 if (state == NULL)
4787 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004788 /*
4789 * Don't rely on the VFS having checked the file open mode,
4790 * since it won't do this for flock() locks.
4791 */
4792 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4793 case F_RDLCK:
4794 if (!(filp->f_mode & FMODE_READ))
4795 return -EBADF;
4796 break;
4797 case F_WRLCK:
4798 if (!(filp->f_mode & FMODE_WRITE))
4799 return -EBADF;
4800 }
4801
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 do {
4803 status = nfs4_proc_setlk(state, cmd, request);
4804 if ((status != -EAGAIN) || IS_SETLK(cmd))
4805 break;
4806 timeout = nfs4_set_lock_task_retry(timeout);
4807 status = -ERESTARTSYS;
4808 if (signalled())
4809 break;
4810 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 return status;
4812}
4813
Trond Myklebust888e6942005-11-04 15:38:11 -05004814int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4815{
4816 struct nfs_server *server = NFS_SERVER(state->inode);
4817 struct nfs4_exception exception = { };
4818 int err;
4819
4820 err = nfs4_set_lock_state(state, fl);
4821 if (err != 0)
4822 goto out;
4823 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004824 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004825 switch (err) {
4826 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004827 printk(KERN_ERR "NFS: %s: unhandled error "
4828 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004829 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004830 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004831 goto out;
4832 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004833 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004834 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004835 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004836 nfs4_schedule_lease_recovery(server->nfs_client);
4837 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004838 case -NFS4ERR_BADSESSION:
4839 case -NFS4ERR_BADSLOT:
4840 case -NFS4ERR_BAD_HIGH_SLOT:
4841 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4842 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004843 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004844 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004845 case -ERESTARTSYS:
4846 /*
4847 * The show must go on: exit, but mark the
4848 * stateid as needing recovery.
4849 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004850 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004851 case -NFS4ERR_ADMIN_REVOKED:
4852 case -NFS4ERR_BAD_STATEID:
4853 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004854 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004855 err = 0;
4856 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004857 case -EKEYEXPIRED:
4858 /*
4859 * User RPCSEC_GSS context has expired.
4860 * We cannot recover this stateid now, so
4861 * skip it and allow recovery thread to
4862 * proceed.
4863 */
4864 err = 0;
4865 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004866 case -ENOMEM:
4867 case -NFS4ERR_DENIED:
4868 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4869 err = 0;
4870 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004871 case -NFS4ERR_DELAY:
4872 break;
4873 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004874 err = nfs4_handle_exception(server, err, &exception);
4875 } while (exception.retry);
4876out:
4877 return err;
4878}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004879
Trond Myklebustcf470c32012-03-07 13:49:12 -05004880struct nfs_release_lockowner_data {
4881 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004882 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004883 struct nfs_release_lockowner_args args;
4884};
4885
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004886static void nfs4_release_lockowner_release(void *calldata)
4887{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004888 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004889 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004890 kfree(calldata);
4891}
4892
Trond Myklebust17280172012-03-11 13:11:00 -04004893static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004894 .rpc_release = nfs4_release_lockowner_release,
4895};
4896
Trond Myklebustcf470c32012-03-07 13:49:12 -05004897int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004898{
4899 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004900 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004901 struct rpc_message msg = {
4902 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4903 };
4904
4905 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004906 return -EINVAL;
4907 data = kmalloc(sizeof(*data), GFP_NOFS);
4908 if (!data)
4909 return -ENOMEM;
4910 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004911 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004912 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4913 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4914 data->args.lock_owner.s_dev = server->s_dev;
4915 msg.rpc_argp = &data->args;
4916 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4917 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004918}
4919
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004920#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4921
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004922static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4923 const void *buf, size_t buflen,
4924 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004925{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004926 if (strcmp(key, "") != 0)
4927 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004928
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004929 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004930}
4931
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004932static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4933 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004934{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004935 if (strcmp(key, "") != 0)
4936 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004937
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004938 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004939}
4940
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004941static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4942 size_t list_len, const char *name,
4943 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004944{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004945 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004946
J. Bruce Fields096455a2006-03-20 23:23:42 -05004947 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4948 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004949
4950 if (list && len <= list_len)
4951 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004952 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004953}
4954
Andy Adamson533eb462011-06-13 18:25:56 -04004955/*
4956 * nfs_fhget will use either the mounted_on_fileid or the fileid
4957 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004958static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4959{
Andy Adamson533eb462011-06-13 18:25:56 -04004960 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4961 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4962 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05004963 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004964 return;
4965
4966 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05004967 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004968 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4969 fattr->nlink = 2;
4970}
4971
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004972static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
4973 const struct qstr *name,
4974 struct nfs4_fs_locations *fs_locations,
4975 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004976{
4977 struct nfs_server *server = NFS_SERVER(dir);
4978 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004979 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004980 };
4981 struct nfs4_fs_locations_arg args = {
4982 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004983 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004984 .page = page,
4985 .bitmask = bitmask,
4986 };
Benny Halevy22958462009-04-01 09:22:02 -04004987 struct nfs4_fs_locations_res res = {
4988 .fs_locations = fs_locations,
4989 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004990 struct rpc_message msg = {
4991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4992 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004993 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004994 };
4995 int status;
4996
Harvey Harrison3110ff82008-05-02 13:42:44 -07004997 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004998
4999 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5000 * is not supported */
5001 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5002 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5003 else
5004 bitmask[0] |= FATTR4_WORD0_FILEID;
5005
Trond Myklebustc228fd32007-01-13 02:28:11 -05005006 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005007 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005008 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005009 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005010 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005011 return status;
5012}
5013
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005014int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5015 const struct qstr *name,
5016 struct nfs4_fs_locations *fs_locations,
5017 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005018{
5019 struct nfs4_exception exception = { };
5020 int err;
5021 do {
5022 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005023 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005024 &exception);
5025 } while (exception.retry);
5026 return err;
5027}
5028
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005029static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5030{
5031 int status;
5032 struct nfs4_secinfo_arg args = {
5033 .dir_fh = NFS_FH(dir),
5034 .name = name,
5035 };
5036 struct nfs4_secinfo_res res = {
5037 .flavors = flavors,
5038 };
5039 struct rpc_message msg = {
5040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5041 .rpc_argp = &args,
5042 .rpc_resp = &res,
5043 };
5044
5045 dprintk("NFS call secinfo %s\n", name->name);
5046 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5047 dprintk("NFS reply secinfo: %d\n", status);
5048 return status;
5049}
5050
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005051int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5052 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005053{
5054 struct nfs4_exception exception = { };
5055 int err;
5056 do {
5057 err = nfs4_handle_exception(NFS_SERVER(dir),
5058 _nfs4_proc_secinfo(dir, name, flavors),
5059 &exception);
5060 } while (exception.retry);
5061 return err;
5062}
5063
Andy Adamson557134a2009-04-01 09:21:53 -04005064#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005065/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005066 * Check the exchange flags returned by the server for invalid flags, having
5067 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5068 * DS flags set.
5069 */
5070static int nfs4_check_cl_exchange_flags(u32 flags)
5071{
5072 if (flags & ~EXCHGID4_FLAG_MASK_R)
5073 goto out_inval;
5074 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5075 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5076 goto out_inval;
5077 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5078 goto out_inval;
5079 return NFS_OK;
5080out_inval:
5081 return -NFS4ERR_INVAL;
5082}
5083
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005084static bool
5085nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
5086{
5087 if (a->server_scope_sz == b->server_scope_sz &&
5088 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5089 return true;
5090
5091 return false;
5092}
5093
Andy Adamson357f54d2010-12-14 10:11:57 -05005094/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005095 * nfs4_proc_exchange_id()
5096 *
5097 * Since the clientid has expired, all compounds using sessions
5098 * associated with the stale clientid will be returning
5099 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5100 * be in some phase of session reset.
5101 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005102int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005103{
5104 nfs4_verifier verifier;
5105 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005106 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005107 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005108 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005109 };
5110 struct nfs41_exchange_id_res res = {
5111 .client = clp,
5112 };
5113 int status;
5114 struct rpc_message msg = {
5115 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5116 .rpc_argp = &args,
5117 .rpc_resp = &res,
5118 .rpc_cred = cred,
5119 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005120
5121 dprintk("--> %s\n", __func__);
5122 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04005123
Chuck Levercd937102012-03-02 17:14:31 -05005124 nfs4_construct_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005125
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005126 args.id_len = scnprintf(args.id, sizeof(args.id),
Trond Myklebust3617e502012-04-30 12:04:58 -04005127 "%s/%s/%u",
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005128 clp->cl_ipaddr,
Trond Myklebust3617e502012-04-30 12:04:58 -04005129 clp->cl_rpcclient->cl_nodename,
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005130 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005131
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005132 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005133 if (unlikely(!res.server_scope)) {
5134 status = -ENOMEM;
5135 goto out;
5136 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005137
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005138 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
5139 if (unlikely(!res.impl_id)) {
5140 status = -ENOMEM;
5141 goto out_server_scope;
5142 }
5143
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005144 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005145 if (!status)
5146 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005147
5148 if (!status) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005149 /* use the most recent implementation id */
5150 kfree(clp->impl_id);
5151 clp->impl_id = res.impl_id;
5152 } else
5153 kfree(res.impl_id);
5154
5155 if (!status) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005156 if (clp->server_scope &&
5157 !nfs41_same_server_scope(clp->server_scope,
5158 res.server_scope)) {
5159 dprintk("%s: server_scope mismatch detected\n",
5160 __func__);
5161 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5162 kfree(clp->server_scope);
5163 clp->server_scope = NULL;
5164 }
5165
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005166 if (!clp->server_scope) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005167 clp->server_scope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005168 goto out;
5169 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005170 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005171
5172out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005173 kfree(res.server_scope);
5174out:
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005175 if (clp->impl_id)
5176 dprintk("%s: Server Implementation ID: "
5177 "domain: %s, name: %s, date: %llu,%u\n",
5178 __func__, clp->impl_id->domain, clp->impl_id->name,
5179 clp->impl_id->date.seconds,
5180 clp->impl_id->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005181 dprintk("<-- %s status= %d\n", __func__, status);
5182 return status;
5183}
5184
Andy Adamson2050f0c2009-04-01 09:22:30 -04005185struct nfs4_get_lease_time_data {
5186 struct nfs4_get_lease_time_args *args;
5187 struct nfs4_get_lease_time_res *res;
5188 struct nfs_client *clp;
5189};
5190
5191static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5192 void *calldata)
5193{
5194 int ret;
5195 struct nfs4_get_lease_time_data *data =
5196 (struct nfs4_get_lease_time_data *)calldata;
5197
5198 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005199 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005200 /* just setup sequence, do not trigger session recovery
5201 since we're invoked within one */
5202 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005203 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005204 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005205
5206 BUG_ON(ret == -EAGAIN);
5207 rpc_call_start(task);
5208 dprintk("<-- %s\n", __func__);
5209}
5210
5211/*
5212 * Called from nfs4_state_manager thread for session setup, so don't recover
5213 * from sequence operation or clientid errors.
5214 */
5215static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5216{
5217 struct nfs4_get_lease_time_data *data =
5218 (struct nfs4_get_lease_time_data *)calldata;
5219
5220 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005221 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5222 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005223 switch (task->tk_status) {
5224 case -NFS4ERR_DELAY:
5225 case -NFS4ERR_GRACE:
5226 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5227 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5228 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005229 /* fall through */
5230 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005231 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005232 return;
5233 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005234 dprintk("<-- %s\n", __func__);
5235}
5236
Trond Myklebust17280172012-03-11 13:11:00 -04005237static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005238 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5239 .rpc_call_done = nfs4_get_lease_time_done,
5240};
5241
5242int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5243{
5244 struct rpc_task *task;
5245 struct nfs4_get_lease_time_args args;
5246 struct nfs4_get_lease_time_res res = {
5247 .lr_fsinfo = fsinfo,
5248 };
5249 struct nfs4_get_lease_time_data data = {
5250 .args = &args,
5251 .res = &res,
5252 .clp = clp,
5253 };
5254 struct rpc_message msg = {
5255 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5256 .rpc_argp = &args,
5257 .rpc_resp = &res,
5258 };
5259 struct rpc_task_setup task_setup = {
5260 .rpc_client = clp->cl_rpcclient,
5261 .rpc_message = &msg,
5262 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005263 .callback_data = &data,
5264 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005265 };
5266 int status;
5267
Trond Myklebust9d12b212012-01-17 22:04:25 -05005268 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005269 dprintk("--> %s\n", __func__);
5270 task = rpc_run_task(&task_setup);
5271
5272 if (IS_ERR(task))
5273 status = PTR_ERR(task);
5274 else {
5275 status = task->tk_status;
5276 rpc_put_task(task);
5277 }
5278 dprintk("<-- %s return %d\n", __func__, status);
5279
5280 return status;
5281}
5282
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005283static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5284{
5285 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5286}
5287
5288static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5289 struct nfs4_slot *new,
5290 u32 max_slots,
5291 u32 ivalue)
5292{
5293 struct nfs4_slot *old = NULL;
5294 u32 i;
5295
5296 spin_lock(&tbl->slot_tbl_lock);
5297 if (new) {
5298 old = tbl->slots;
5299 tbl->slots = new;
5300 tbl->max_slots = max_slots;
5301 }
5302 tbl->highest_used_slotid = -1; /* no slot is currently used */
5303 for (i = 0; i < tbl->max_slots; i++)
5304 tbl->slots[i].seq_nr = ivalue;
5305 spin_unlock(&tbl->slot_tbl_lock);
5306 kfree(old);
5307}
5308
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005309/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005310 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005311 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005312static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5313 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005314{
Andy Adamson104aeba2010-01-14 17:45:10 -05005315 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005316 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005317
Andy Adamson104aeba2010-01-14 17:45:10 -05005318 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5319 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005320
Andy Adamson104aeba2010-01-14 17:45:10 -05005321 /* Does the newly negotiated max_reqs match the existing slot table? */
5322 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005323 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005324 if (!new)
5325 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005326 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005327 ret = 0;
5328
5329 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005330 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5331 tbl, tbl->slots, tbl->max_slots);
5332out:
5333 dprintk("<-- %s: return %d\n", __func__, ret);
5334 return ret;
5335}
5336
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005337/* Destroy the slot table */
5338static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5339{
5340 if (session->fc_slot_table.slots != NULL) {
5341 kfree(session->fc_slot_table.slots);
5342 session->fc_slot_table.slots = NULL;
5343 }
5344 if (session->bc_slot_table.slots != NULL) {
5345 kfree(session->bc_slot_table.slots);
5346 session->bc_slot_table.slots = NULL;
5347 }
5348 return;
5349}
5350
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005351/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005352 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005353 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005354static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005355{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005356 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005357 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005358
Andy Adamsonaacd5532011-11-09 13:58:21 -05005359 dprintk("--> %s\n", __func__);
5360 /* Fore channel */
5361 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005362 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5363 if (status) /* -ENOMEM */
5364 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005365 /* Back channel */
5366 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005367 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5368 if (status && tbl->slots == NULL)
5369 /* Fore and back channel share a connection so get
5370 * both slot tables or neither */
5371 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005372 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005373}
5374
5375struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5376{
5377 struct nfs4_session *session;
5378 struct nfs4_slot_table *tbl;
5379
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005380 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005381 if (!session)
5382 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005383
Andy Adamson557134a2009-04-01 09:21:53 -04005384 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005385 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005386 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005387 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005388 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005389
5390 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005391 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005392 spin_lock_init(&tbl->slot_tbl_lock);
5393 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005394 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005395
Trond Myklebust1055d762010-06-16 09:52:27 -04005396 session->session_state = 1<<NFS4_SESSION_INITING;
5397
Andy Adamson557134a2009-04-01 09:21:53 -04005398 session->clp = clp;
5399 return session;
5400}
5401
5402void nfs4_destroy_session(struct nfs4_session *session)
5403{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005404 struct rpc_xprt *xprt;
5405
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005406 nfs4_proc_destroy_session(session);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005407
5408 rcu_read_lock();
5409 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5410 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005411 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005412 __func__, xprt);
5413 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005414 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005415 kfree(session);
5416}
5417
Andy Adamsonfc931582009-04-01 09:22:31 -04005418/*
5419 * Initialize the values to be used by the client in CREATE_SESSION
5420 * If nfs4_init_session set the fore channel request and response sizes,
5421 * use them.
5422 *
5423 * Set the back channel max_resp_sz_cached to zero to force the client to
5424 * always set csa_cachethis to FALSE because the current implementation
5425 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5426 */
5427static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5428{
5429 struct nfs4_session *session = args->client->cl_session;
5430 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5431 mxresp_sz = session->fc_attrs.max_resp_sz;
5432
5433 if (mxrqst_sz == 0)
5434 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5435 if (mxresp_sz == 0)
5436 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5437 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005438 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5439 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005440 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005441 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005442
5443 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005444 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005445 __func__,
5446 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005447 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005448
5449 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005450 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5451 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5452 args->bc_attrs.max_resp_sz_cached = 0;
5453 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5454 args->bc_attrs.max_reqs = 1;
5455
5456 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5457 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5458 __func__,
5459 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5460 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5461 args->bc_attrs.max_reqs);
5462}
5463
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005464static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005465{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005466 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5467 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5468
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005469 if (rcvd->max_resp_sz > sent->max_resp_sz)
5470 return -EINVAL;
5471 /*
5472 * Our requested max_ops is the minimum we need; we're not
5473 * prepared to break up compounds into smaller pieces than that.
5474 * So, no point even trying to continue if the server won't
5475 * cooperate:
5476 */
5477 if (rcvd->max_ops < sent->max_ops)
5478 return -EINVAL;
5479 if (rcvd->max_reqs == 0)
5480 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005481 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5482 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005483 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005484}
5485
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005486static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5487{
5488 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5489 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005490
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005491 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5492 return -EINVAL;
5493 if (rcvd->max_resp_sz < sent->max_resp_sz)
5494 return -EINVAL;
5495 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5496 return -EINVAL;
5497 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005498 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005499 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005500 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005501 return -EINVAL;
5502 return 0;
5503}
Andy Adamson8d353012009-04-01 09:22:32 -04005504
Andy Adamson8d353012009-04-01 09:22:32 -04005505static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5506 struct nfs4_session *session)
5507{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005508 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005509
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005510 ret = nfs4_verify_fore_channel_attrs(args, session);
5511 if (ret)
5512 return ret;
5513 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005514}
5515
Andy Adamsonfc931582009-04-01 09:22:31 -04005516static int _nfs4_proc_create_session(struct nfs_client *clp)
5517{
5518 struct nfs4_session *session = clp->cl_session;
5519 struct nfs41_create_session_args args = {
5520 .client = clp,
5521 .cb_program = NFS4_CALLBACK,
5522 };
5523 struct nfs41_create_session_res res = {
5524 .client = clp,
5525 };
5526 struct rpc_message msg = {
5527 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5528 .rpc_argp = &args,
5529 .rpc_resp = &res,
5530 };
5531 int status;
5532
5533 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005534 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005535
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005536 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005537
Andy Adamson8d353012009-04-01 09:22:32 -04005538 if (!status)
5539 /* Verify the session's negotiated channel_attrs values */
5540 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005541 if (!status) {
5542 /* Increment the clientid slot sequence id */
5543 clp->cl_seqid++;
5544 }
5545
5546 return status;
5547}
5548
5549/*
5550 * Issues a CREATE_SESSION operation to the server.
5551 * It is the responsibility of the caller to verify the session is
5552 * expired before calling this routine.
5553 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005554int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005555{
5556 int status;
5557 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005558 struct nfs4_session *session = clp->cl_session;
5559
5560 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5561
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005562 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005563 if (status)
5564 goto out;
5565
Andy Adamsonaacd5532011-11-09 13:58:21 -05005566 /* Init or reset the session slot tables */
5567 status = nfs4_setup_session_slot_tables(session);
5568 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005569 if (status)
5570 goto out;
5571
5572 ptr = (unsigned *)&session->sess_id.data[0];
5573 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5574 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005575out:
5576 dprintk("<-- %s\n", __func__);
5577 return status;
5578}
5579
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005580/*
5581 * Issue the over-the-wire RPC DESTROY_SESSION.
5582 * The caller must serialize access to this routine.
5583 */
5584int nfs4_proc_destroy_session(struct nfs4_session *session)
5585{
5586 int status = 0;
5587 struct rpc_message msg;
5588
5589 dprintk("--> nfs4_proc_destroy_session\n");
5590
5591 /* session is still being setup */
5592 if (session->clp->cl_cons_state != NFS_CS_READY)
5593 return status;
5594
5595 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5596 msg.rpc_argp = session;
5597 msg.rpc_resp = NULL;
5598 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005599 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005600
5601 if (status)
5602 printk(KERN_WARNING
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -05005603 "NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005604 "Session has been destroyed regardless...\n", status);
5605
5606 dprintk("<-- nfs4_proc_destroy_session\n");
5607 return status;
5608}
5609
Trond Myklebustfccba802009-07-21 16:48:07 -04005610int nfs4_init_session(struct nfs_server *server)
5611{
5612 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005613 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005614 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005615 int ret;
5616
5617 if (!nfs4_has_session(clp))
5618 return 0;
5619
Trond Myklebust1055d762010-06-16 09:52:27 -04005620 session = clp->cl_session;
5621 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5622 return 0;
5623
Andy Adamson68bf05e2009-12-15 12:55:02 -05005624 rsize = server->rsize;
5625 if (rsize == 0)
5626 rsize = NFS_MAX_FILE_IO_SIZE;
5627 wsize = server->wsize;
5628 if (wsize == 0)
5629 wsize = NFS_MAX_FILE_IO_SIZE;
5630
Andy Adamson68bf05e2009-12-15 12:55:02 -05005631 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5632 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005633
Trond Myklebustfccba802009-07-21 16:48:07 -04005634 ret = nfs4_recover_expired_lease(server);
5635 if (!ret)
5636 ret = nfs4_check_client_ready(clp);
5637 return ret;
5638}
5639
Andy Adamsond83217c2011-03-01 01:34:17 +00005640int nfs4_init_ds_session(struct nfs_client *clp)
5641{
5642 struct nfs4_session *session = clp->cl_session;
5643 int ret;
5644
5645 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5646 return 0;
5647
5648 ret = nfs4_client_recover_expired_lease(clp);
5649 if (!ret)
5650 /* Test for the DS role */
5651 if (!is_ds_client(clp))
5652 ret = -ENODEV;
5653 if (!ret)
5654 ret = nfs4_check_client_ready(clp);
5655 return ret;
5656
5657}
5658EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5659
5660
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005661/*
5662 * Renew the cl_session lease.
5663 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005664struct nfs4_sequence_data {
5665 struct nfs_client *clp;
5666 struct nfs4_sequence_args args;
5667 struct nfs4_sequence_res res;
5668};
5669
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005670static void nfs41_sequence_release(void *data)
5671{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005672 struct nfs4_sequence_data *calldata = data;
5673 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005674
Alexandros Batsakis71358402010-02-05 03:45:05 -08005675 if (atomic_read(&clp->cl_count) > 1)
5676 nfs4_schedule_state_renewal(clp);
5677 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005678 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005679}
5680
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005681static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5682{
5683 switch(task->tk_status) {
5684 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005685 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5686 return -EAGAIN;
5687 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005688 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005689 }
5690 return 0;
5691}
5692
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005693static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005694{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005695 struct nfs4_sequence_data *calldata = data;
5696 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005697
Trond Myklebust14516c32010-07-31 14:29:06 -04005698 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5699 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005700
5701 if (task->tk_status < 0) {
5702 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005703 if (atomic_read(&clp->cl_count) == 1)
5704 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005705
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005706 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5707 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005708 return;
5709 }
5710 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005711 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005712out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005713 dprintk("<-- %s\n", __func__);
5714}
5715
5716static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5717{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005718 struct nfs4_sequence_data *calldata = data;
5719 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005720 struct nfs4_sequence_args *args;
5721 struct nfs4_sequence_res *res;
5722
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005723 args = task->tk_msg.rpc_argp;
5724 res = task->tk_msg.rpc_resp;
5725
Trond Myklebust9d12b212012-01-17 22:04:25 -05005726 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005727 return;
5728 rpc_call_start(task);
5729}
5730
5731static const struct rpc_call_ops nfs41_sequence_ops = {
5732 .rpc_call_done = nfs41_sequence_call_done,
5733 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005734 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005735};
5736
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005737static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005738{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005739 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005740 struct rpc_message msg = {
5741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5742 .rpc_cred = cred,
5743 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005744 struct rpc_task_setup task_setup_data = {
5745 .rpc_client = clp->cl_rpcclient,
5746 .rpc_message = &msg,
5747 .callback_ops = &nfs41_sequence_ops,
5748 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5749 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005750
Alexandros Batsakis71358402010-02-05 03:45:05 -08005751 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005752 return ERR_PTR(-EIO);
Benny Halevydfb4f302010-09-24 09:17:01 -04005753 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005754 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005755 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005756 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005757 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005758 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005759 msg.rpc_argp = &calldata->args;
5760 msg.rpc_resp = &calldata->res;
5761 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005762 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005763
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005764 return rpc_run_task(&task_setup_data);
5765}
5766
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005767static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005768{
5769 struct rpc_task *task;
5770 int ret = 0;
5771
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005772 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5773 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005774 task = _nfs41_proc_sequence(clp, cred);
5775 if (IS_ERR(task))
5776 ret = PTR_ERR(task);
5777 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005778 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005779 dprintk("<-- %s status=%d\n", __func__, ret);
5780 return ret;
5781}
5782
5783static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5784{
5785 struct rpc_task *task;
5786 int ret;
5787
5788 task = _nfs41_proc_sequence(clp, cred);
5789 if (IS_ERR(task)) {
5790 ret = PTR_ERR(task);
5791 goto out;
5792 }
5793 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005794 if (!ret) {
5795 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5796
5797 if (task->tk_status == 0)
5798 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005799 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005800 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005801 rpc_put_task(task);
5802out:
5803 dprintk("<-- %s status=%d\n", __func__, ret);
5804 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005805}
5806
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005807struct nfs4_reclaim_complete_data {
5808 struct nfs_client *clp;
5809 struct nfs41_reclaim_complete_args arg;
5810 struct nfs41_reclaim_complete_res res;
5811};
5812
5813static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5814{
5815 struct nfs4_reclaim_complete_data *calldata = data;
5816
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005817 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005818 if (nfs41_setup_sequence(calldata->clp->cl_session,
5819 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005820 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005821 return;
5822
5823 rpc_call_start(task);
5824}
5825
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005826static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5827{
5828 switch(task->tk_status) {
5829 case 0:
5830 case -NFS4ERR_COMPLETE_ALREADY:
5831 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5832 break;
5833 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005834 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005835 /* fall through */
5836 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005837 return -EAGAIN;
5838 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005839 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005840 }
5841 return 0;
5842}
5843
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005844static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5845{
5846 struct nfs4_reclaim_complete_data *calldata = data;
5847 struct nfs_client *clp = calldata->clp;
5848 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5849
5850 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005851 if (!nfs41_sequence_done(task, res))
5852 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005853
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005854 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5855 rpc_restart_call_prepare(task);
5856 return;
5857 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005858 dprintk("<-- %s\n", __func__);
5859}
5860
5861static void nfs4_free_reclaim_complete_data(void *data)
5862{
5863 struct nfs4_reclaim_complete_data *calldata = data;
5864
5865 kfree(calldata);
5866}
5867
5868static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5869 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5870 .rpc_call_done = nfs4_reclaim_complete_done,
5871 .rpc_release = nfs4_free_reclaim_complete_data,
5872};
5873
5874/*
5875 * Issue a global reclaim complete.
5876 */
5877static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5878{
5879 struct nfs4_reclaim_complete_data *calldata;
5880 struct rpc_task *task;
5881 struct rpc_message msg = {
5882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5883 };
5884 struct rpc_task_setup task_setup_data = {
5885 .rpc_client = clp->cl_rpcclient,
5886 .rpc_message = &msg,
5887 .callback_ops = &nfs4_reclaim_complete_call_ops,
5888 .flags = RPC_TASK_ASYNC,
5889 };
5890 int status = -ENOMEM;
5891
5892 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005893 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005894 if (calldata == NULL)
5895 goto out;
5896 calldata->clp = clp;
5897 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005898
Trond Myklebust9d12b212012-01-17 22:04:25 -05005899 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005900 msg.rpc_argp = &calldata->arg;
5901 msg.rpc_resp = &calldata->res;
5902 task_setup_data.callback_data = calldata;
5903 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005904 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005905 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005906 goto out;
5907 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005908 status = nfs4_wait_for_completion_rpc_task(task);
5909 if (status == 0)
5910 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005911 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005912 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005913out:
5914 dprintk("<-- %s status=%d\n", __func__, status);
5915 return status;
5916}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005917
5918static void
5919nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5920{
5921 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005922 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005923
5924 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005925 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5926 * right now covering the LAYOUTGET we are about to send.
5927 * However, that is not so catastrophic, and there seems
5928 * to be no way to prevent it completely.
5929 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005930 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005931 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005932 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005933 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5934 NFS_I(lgp->args.inode)->layout,
5935 lgp->args.ctx->state)) {
5936 rpc_exit(task, NFS4_OK);
5937 return;
5938 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005939 rpc_call_start(task);
5940}
5941
5942static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5943{
5944 struct nfs4_layoutget *lgp = calldata;
5945 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5946
5947 dprintk("--> %s\n", __func__);
5948
5949 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5950 return;
5951
5952 switch (task->tk_status) {
5953 case 0:
5954 break;
5955 case -NFS4ERR_LAYOUTTRYLATER:
5956 case -NFS4ERR_RECALLCONFLICT:
5957 task->tk_status = -NFS4ERR_DELAY;
5958 /* Fall through */
5959 default:
5960 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5961 rpc_restart_call_prepare(task);
5962 return;
5963 }
5964 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005965 dprintk("<-- %s\n", __func__);
5966}
5967
5968static void nfs4_layoutget_release(void *calldata)
5969{
5970 struct nfs4_layoutget *lgp = calldata;
5971
5972 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005973 put_nfs_open_context(lgp->args.ctx);
5974 kfree(calldata);
5975 dprintk("<-- %s\n", __func__);
5976}
5977
5978static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5979 .rpc_call_prepare = nfs4_layoutget_prepare,
5980 .rpc_call_done = nfs4_layoutget_done,
5981 .rpc_release = nfs4_layoutget_release,
5982};
5983
5984int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5985{
5986 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5987 struct rpc_task *task;
5988 struct rpc_message msg = {
5989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5990 .rpc_argp = &lgp->args,
5991 .rpc_resp = &lgp->res,
5992 };
5993 struct rpc_task_setup task_setup_data = {
5994 .rpc_client = server->client,
5995 .rpc_message = &msg,
5996 .callback_ops = &nfs4_layoutget_call_ops,
5997 .callback_data = lgp,
5998 .flags = RPC_TASK_ASYNC,
5999 };
6000 int status = 0;
6001
6002 dprintk("--> %s\n", __func__);
6003
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006004 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006005 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006006 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006007 task = rpc_run_task(&task_setup_data);
6008 if (IS_ERR(task))
6009 return PTR_ERR(task);
6010 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006011 if (status == 0)
6012 status = task->tk_status;
6013 if (status == 0)
6014 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006015 rpc_put_task(task);
6016 dprintk("<-- %s status=%d\n", __func__, status);
6017 return status;
6018}
6019
Benny Halevycbe82602011-05-22 19:52:37 +03006020static void
6021nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6022{
6023 struct nfs4_layoutreturn *lrp = calldata;
6024
6025 dprintk("--> %s\n", __func__);
6026 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006027 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006028 return;
6029 rpc_call_start(task);
6030}
6031
6032static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6033{
6034 struct nfs4_layoutreturn *lrp = calldata;
6035 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006036 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006037
6038 dprintk("--> %s\n", __func__);
6039
6040 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6041 return;
6042
6043 server = NFS_SERVER(lrp->args.inode);
6044 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006045 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006046 return;
6047 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006048 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006049 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03006050 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03006051 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03006052 } else
6053 BUG_ON(!list_empty(&lo->plh_segs));
6054 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006055 lo->plh_block_lgets--;
6056 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006057 dprintk("<-- %s\n", __func__);
6058}
6059
6060static void nfs4_layoutreturn_release(void *calldata)
6061{
6062 struct nfs4_layoutreturn *lrp = calldata;
6063
6064 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006065 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006066 kfree(calldata);
6067 dprintk("<-- %s\n", __func__);
6068}
6069
6070static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6071 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6072 .rpc_call_done = nfs4_layoutreturn_done,
6073 .rpc_release = nfs4_layoutreturn_release,
6074};
6075
6076int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6077{
6078 struct rpc_task *task;
6079 struct rpc_message msg = {
6080 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6081 .rpc_argp = &lrp->args,
6082 .rpc_resp = &lrp->res,
6083 };
6084 struct rpc_task_setup task_setup_data = {
6085 .rpc_client = lrp->clp->cl_rpcclient,
6086 .rpc_message = &msg,
6087 .callback_ops = &nfs4_layoutreturn_call_ops,
6088 .callback_data = lrp,
6089 };
6090 int status;
6091
6092 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006093 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006094 task = rpc_run_task(&task_setup_data);
6095 if (IS_ERR(task))
6096 return PTR_ERR(task);
6097 status = task->tk_status;
6098 dprintk("<-- %s status=%d\n", __func__, status);
6099 rpc_put_task(task);
6100 return status;
6101}
6102
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006103/*
6104 * Retrieve the list of Data Server devices from the MDS.
6105 */
6106static int _nfs4_getdevicelist(struct nfs_server *server,
6107 const struct nfs_fh *fh,
6108 struct pnfs_devicelist *devlist)
6109{
6110 struct nfs4_getdevicelist_args args = {
6111 .fh = fh,
6112 .layoutclass = server->pnfs_curr_ld->id,
6113 };
6114 struct nfs4_getdevicelist_res res = {
6115 .devlist = devlist,
6116 };
6117 struct rpc_message msg = {
6118 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6119 .rpc_argp = &args,
6120 .rpc_resp = &res,
6121 };
6122 int status;
6123
6124 dprintk("--> %s\n", __func__);
6125 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6126 &res.seq_res, 0);
6127 dprintk("<-- %s status=%d\n", __func__, status);
6128 return status;
6129}
6130
6131int nfs4_proc_getdevicelist(struct nfs_server *server,
6132 const struct nfs_fh *fh,
6133 struct pnfs_devicelist *devlist)
6134{
6135 struct nfs4_exception exception = { };
6136 int err;
6137
6138 do {
6139 err = nfs4_handle_exception(server,
6140 _nfs4_getdevicelist(server, fh, devlist),
6141 &exception);
6142 } while (exception.retry);
6143
6144 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6145 err, devlist->num_devs);
6146
6147 return err;
6148}
6149EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6150
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006151static int
6152_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6153{
6154 struct nfs4_getdeviceinfo_args args = {
6155 .pdev = pdev,
6156 };
6157 struct nfs4_getdeviceinfo_res res = {
6158 .pdev = pdev,
6159 };
6160 struct rpc_message msg = {
6161 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6162 .rpc_argp = &args,
6163 .rpc_resp = &res,
6164 };
6165 int status;
6166
6167 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006168 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006169 dprintk("<-- %s status=%d\n", __func__, status);
6170
6171 return status;
6172}
6173
6174int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6175{
6176 struct nfs4_exception exception = { };
6177 int err;
6178
6179 do {
6180 err = nfs4_handle_exception(server,
6181 _nfs4_proc_getdeviceinfo(server, pdev),
6182 &exception);
6183 } while (exception.retry);
6184 return err;
6185}
6186EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6187
Andy Adamson863a3c62011-03-23 13:27:54 +00006188static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6189{
6190 struct nfs4_layoutcommit_data *data = calldata;
6191 struct nfs_server *server = NFS_SERVER(data->args.inode);
6192
6193 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006194 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006195 return;
6196 rpc_call_start(task);
6197}
6198
6199static void
6200nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6201{
6202 struct nfs4_layoutcommit_data *data = calldata;
6203 struct nfs_server *server = NFS_SERVER(data->args.inode);
6204
6205 if (!nfs4_sequence_done(task, &data->res.seq_res))
6206 return;
6207
6208 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006209 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6210 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6211 case -NFS4ERR_BADLAYOUT: /* no layout */
6212 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006213 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006214 break;
6215 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006216 nfs_post_op_update_inode_force_wcc(data->args.inode,
6217 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006218 break;
6219 default:
6220 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6221 rpc_restart_call_prepare(task);
6222 return;
6223 }
6224 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006225}
6226
6227static void nfs4_layoutcommit_release(void *calldata)
6228{
6229 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006230 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006231 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006232
Andy Adamsondb29c082011-07-30 20:52:38 -04006233 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006234 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006235 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6236 list_del_init(&lseg->pls_lc_list);
6237 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6238 &lseg->pls_flags))
6239 put_lseg(lseg);
6240 }
Peng Tao92407e72011-10-23 20:21:17 -07006241
6242 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6243 smp_mb__after_clear_bit();
6244 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6245
Andy Adamson863a3c62011-03-23 13:27:54 +00006246 put_rpccred(data->cred);
6247 kfree(data);
6248}
6249
6250static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6251 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6252 .rpc_call_done = nfs4_layoutcommit_done,
6253 .rpc_release = nfs4_layoutcommit_release,
6254};
6255
6256int
Andy Adamsonef311532011-03-12 02:58:10 -05006257nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006258{
6259 struct rpc_message msg = {
6260 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6261 .rpc_argp = &data->args,
6262 .rpc_resp = &data->res,
6263 .rpc_cred = data->cred,
6264 };
6265 struct rpc_task_setup task_setup_data = {
6266 .task = &data->task,
6267 .rpc_client = NFS_CLIENT(data->args.inode),
6268 .rpc_message = &msg,
6269 .callback_ops = &nfs4_layoutcommit_ops,
6270 .callback_data = data,
6271 .flags = RPC_TASK_ASYNC,
6272 };
6273 struct rpc_task *task;
6274 int status = 0;
6275
6276 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6277 "lbw: %llu inode %lu\n",
6278 data->task.tk_pid, sync,
6279 data->args.lastbytewritten,
6280 data->args.inode->i_ino);
6281
Trond Myklebust9d12b212012-01-17 22:04:25 -05006282 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006283 task = rpc_run_task(&task_setup_data);
6284 if (IS_ERR(task))
6285 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006286 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006287 goto out;
6288 status = nfs4_wait_for_completion_rpc_task(task);
6289 if (status != 0)
6290 goto out;
6291 status = task->tk_status;
6292out:
6293 dprintk("%s: status %d\n", __func__, status);
6294 rpc_put_task(task);
6295 return status;
6296}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006297
6298static int
6299_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6300 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6301{
6302 struct nfs41_secinfo_no_name_args args = {
6303 .style = SECINFO_STYLE_CURRENT_FH,
6304 };
6305 struct nfs4_secinfo_res res = {
6306 .flavors = flavors,
6307 };
6308 struct rpc_message msg = {
6309 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6310 .rpc_argp = &args,
6311 .rpc_resp = &res,
6312 };
6313 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6314}
6315
6316static int
6317nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6318 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6319{
6320 struct nfs4_exception exception = { };
6321 int err;
6322 do {
6323 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6324 switch (err) {
6325 case 0:
6326 case -NFS4ERR_WRONGSEC:
6327 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006328 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006329 default:
6330 err = nfs4_handle_exception(server, err, &exception);
6331 }
6332 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006333out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006334 return err;
6335}
6336
6337static int
6338nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6339 struct nfs_fsinfo *info)
6340{
6341 int err;
6342 struct page *page;
6343 rpc_authflavor_t flavor;
6344 struct nfs4_secinfo_flavors *flavors;
6345
6346 page = alloc_page(GFP_KERNEL);
6347 if (!page) {
6348 err = -ENOMEM;
6349 goto out;
6350 }
6351
6352 flavors = page_address(page);
6353 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6354
6355 /*
6356 * Fall back on "guess and check" method if
6357 * the server doesn't support SECINFO_NO_NAME
6358 */
6359 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6360 err = nfs4_find_root_sec(server, fhandle, info);
6361 goto out_freepage;
6362 }
6363 if (err)
6364 goto out_freepage;
6365
6366 flavor = nfs_find_best_sec(flavors);
6367 if (err == 0)
6368 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6369
6370out_freepage:
6371 put_page(page);
6372 if (err == -EACCES)
6373 return -EPERM;
6374out:
6375 return err;
6376}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006377
6378static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006379{
6380 int status;
6381 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006382 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006383 };
6384 struct nfs41_test_stateid_res res;
6385 struct rpc_message msg = {
6386 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6387 .rpc_argp = &args,
6388 .rpc_resp = &res,
6389 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006390
Trond Myklebust9d12b212012-01-17 22:04:25 -05006391 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006392 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6393
6394 if (status == NFS_OK)
6395 return res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006396 return status;
6397}
6398
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006399static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006400{
6401 struct nfs4_exception exception = { };
6402 int err;
6403 do {
6404 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006405 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006406 &exception);
6407 } while (exception.retry);
6408 return err;
6409}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006410
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006411static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006412{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006413 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006414 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006415 };
6416 struct nfs41_free_stateid_res res;
6417 struct rpc_message msg = {
6418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6419 .rpc_argp = &args,
6420 .rpc_resp = &res,
6421 };
6422
Trond Myklebust9d12b212012-01-17 22:04:25 -05006423 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6424 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006425}
6426
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006427static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006428{
6429 struct nfs4_exception exception = { };
6430 int err;
6431 do {
6432 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006433 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006434 &exception);
6435 } while (exception.retry);
6436 return err;
6437}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006438
6439static bool nfs41_match_stateid(const nfs4_stateid *s1,
6440 const nfs4_stateid *s2)
6441{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006442 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006443 return false;
6444
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006445 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006446 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006447 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006448 return true;
6449
6450 return false;
6451}
6452
Andy Adamson557134a2009-04-01 09:21:53 -04006453#endif /* CONFIG_NFS_V4_1 */
6454
Trond Myklebust36281ca2012-03-04 18:13:56 -05006455static bool nfs4_match_stateid(const nfs4_stateid *s1,
6456 const nfs4_stateid *s2)
6457{
Trond Myklebustf597c532012-03-04 18:13:56 -05006458 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006459}
6460
6461
Trond Myklebust17280172012-03-11 13:11:00 -04006462static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006463 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006464 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 .recover_open = nfs4_open_reclaim,
6466 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006467 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006468 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469};
6470
Andy Adamson591d71c2009-04-01 09:22:47 -04006471#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006472static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006473 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6474 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6475 .recover_open = nfs4_open_reclaim,
6476 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006477 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006478 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006479 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006480};
6481#endif /* CONFIG_NFS_V4_1 */
6482
Trond Myklebust17280172012-03-11 13:11:00 -04006483static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006484 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006485 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486 .recover_open = nfs4_open_expired,
6487 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006488 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006489 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490};
6491
Andy Adamson591d71c2009-04-01 09:22:47 -04006492#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006493static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006494 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6495 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006496 .recover_open = nfs41_open_expired,
6497 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006498 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006499 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006500};
6501#endif /* CONFIG_NFS_V4_1 */
6502
Trond Myklebust17280172012-03-11 13:11:00 -04006503static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006504 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006505 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006506 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006507};
6508
6509#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006510static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006511 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006512 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006513 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006514};
6515#endif
6516
Trond Myklebust97dc1352010-06-16 09:52:26 -04006517static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6518 .minor_version = 0,
6519 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006520 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006521 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006522 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6523 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6524 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006525};
6526
6527#if defined(CONFIG_NFS_V4_1)
6528static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6529 .minor_version = 1,
6530 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006531 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006532 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006533 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6534 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6535 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006536};
6537#endif
6538
Trond Myklebust97dc1352010-06-16 09:52:26 -04006539const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6540 [0] = &nfs_v4_0_minor_ops,
6541#if defined(CONFIG_NFS_V4_1)
6542 [1] = &nfs_v4_1_minor_ops,
6543#endif
6544};
6545
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006546static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006547 .permission = nfs_permission,
6548 .getattr = nfs_getattr,
6549 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006550 .getxattr = generic_getxattr,
6551 .setxattr = generic_setxattr,
6552 .listxattr = generic_listxattr,
6553 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006554};
6555
David Howells509de812006-08-22 20:06:11 -04006556const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557 .version = 4, /* protocol version */
6558 .dentry_ops = &nfs4_dentry_operations,
6559 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006560 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006561 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562 .getroot = nfs4_proc_get_root,
6563 .getattr = nfs4_proc_getattr,
6564 .setattr = nfs4_proc_setattr,
6565 .lookup = nfs4_proc_lookup,
6566 .access = nfs4_proc_access,
6567 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 .create = nfs4_proc_create,
6569 .remove = nfs4_proc_remove,
6570 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006571 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 .unlink_done = nfs4_proc_unlink_done,
6573 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006574 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006575 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006576 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577 .link = nfs4_proc_link,
6578 .symlink = nfs4_proc_symlink,
6579 .mkdir = nfs4_proc_mkdir,
6580 .rmdir = nfs4_proc_remove,
6581 .readdir = nfs4_proc_readdir,
6582 .mknod = nfs4_proc_mknod,
6583 .statfs = nfs4_proc_statfs,
6584 .fsinfo = nfs4_proc_fsinfo,
6585 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006586 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 .decode_dirent = nfs4_decode_dirent,
6588 .read_setup = nfs4_proc_read_setup,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006589 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006590 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591 .write_setup = nfs4_proc_write_setup,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006592 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006593 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006595 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006597 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006598 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006599 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006600 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006601 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602};
6603
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006604static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6605 .prefix = XATTR_NAME_NFSV4_ACL,
6606 .list = nfs4_xattr_list_nfs4_acl,
6607 .get = nfs4_xattr_get_nfs4_acl,
6608 .set = nfs4_xattr_set_nfs4_acl,
6609};
6610
6611const struct xattr_handler *nfs4_xattr_handlers[] = {
6612 &nfs4_xattr_nfs4_acl_handler,
6613 NULL
6614};
6615
Trond Myklebustef159e92012-02-06 19:50:40 -05006616module_param(max_session_slots, ushort, 0644);
6617MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6618 "requests the client will negotiate");
6619
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620/*
6621 * Local variables:
6622 * c-basic-offset: 8
6623 * End:
6624 */