blob: 5e801803072bc8a01fbfeb6f6727204376a75f66 [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 Myklebust0ada2102012-10-29 18:37:40 -04001481 &data->o_res.seq_res,
1482 task) != 0)
1483 nfs_release_seqid(data->o_arg.seqid);
1484 else
1485 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001486 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001487unlock_no_action:
1488 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001489out_no_action:
1490 task->tk_action = NULL;
1491
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001492}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001494static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1495{
1496 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1497 nfs4_open_prepare(task, calldata);
1498}
1499
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001500static void nfs4_open_done(struct rpc_task *task, void *calldata)
1501{
1502 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001504 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001505
Trond Myklebust14516c32010-07-31 14:29:06 -04001506 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1507 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001508
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001509 if (task->tk_status == 0) {
1510 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001511 case S_IFREG:
1512 break;
1513 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001514 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001515 break;
1516 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001517 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001518 break;
1519 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001520 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001521 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001522 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001523 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1524 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001525 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001526 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001527}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001528
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001529static void nfs4_open_release(void *calldata)
1530{
1531 struct nfs4_opendata *data = calldata;
1532 struct nfs4_state *state = NULL;
1533
1534 /* If this request hasn't been cancelled, do nothing */
1535 if (data->cancelled == 0)
1536 goto out_free;
1537 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001538 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001539 goto out_free;
1540 /* In case we need an open_confirm, no cleanup! */
1541 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1542 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001543 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001544 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001545 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001546out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001547 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001548}
1549
1550static const struct rpc_call_ops nfs4_open_ops = {
1551 .rpc_call_prepare = nfs4_open_prepare,
1552 .rpc_call_done = nfs4_open_done,
1553 .rpc_release = nfs4_open_release,
1554};
1555
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001556static const struct rpc_call_ops nfs4_recover_open_ops = {
1557 .rpc_call_prepare = nfs4_recover_open_prepare,
1558 .rpc_call_done = nfs4_open_done,
1559 .rpc_release = nfs4_open_release,
1560};
1561
1562static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001563{
1564 struct inode *dir = data->dir->d_inode;
1565 struct nfs_server *server = NFS_SERVER(dir);
1566 struct nfs_openargs *o_arg = &data->o_arg;
1567 struct nfs_openres *o_res = &data->o_res;
1568 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001569 struct rpc_message msg = {
1570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1571 .rpc_argp = o_arg,
1572 .rpc_resp = o_res,
1573 .rpc_cred = data->owner->so_cred,
1574 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001575 struct rpc_task_setup task_setup_data = {
1576 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001577 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001578 .callback_ops = &nfs4_open_ops,
1579 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001580 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001581 .flags = RPC_TASK_ASYNC,
1582 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001583 int status;
1584
Trond Myklebust9d12b212012-01-17 22:04:25 -05001585 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001586 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001587 data->rpc_done = 0;
1588 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001589 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001590 if (isrecover)
1591 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001592 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001593 if (IS_ERR(task))
1594 return PTR_ERR(task);
1595 status = nfs4_wait_for_completion_rpc_task(task);
1596 if (status != 0) {
1597 data->cancelled = 1;
1598 smp_wmb();
1599 } else
1600 status = data->rpc_status;
1601 rpc_put_task(task);
1602
1603 return status;
1604}
1605
1606static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1607{
1608 struct inode *dir = data->dir->d_inode;
1609 struct nfs_openres *o_res = &data->o_res;
1610 int status;
1611
1612 status = nfs4_run_open_task(data, 1);
1613 if (status != 0 || !data->rpc_done)
1614 return status;
1615
Trond Myklebust6926afd2012-01-07 13:22:46 -05001616 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1617
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001618 nfs_refresh_inode(dir, o_res->dir_attr);
1619
1620 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1621 status = _nfs4_proc_open_confirm(data);
1622 if (status != 0)
1623 return status;
1624 }
1625
1626 return status;
1627}
1628
1629/*
1630 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1631 */
1632static int _nfs4_proc_open(struct nfs4_opendata *data)
1633{
1634 struct inode *dir = data->dir->d_inode;
1635 struct nfs_server *server = NFS_SERVER(dir);
1636 struct nfs_openargs *o_arg = &data->o_arg;
1637 struct nfs_openres *o_res = &data->o_res;
1638 int status;
1639
1640 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001641 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001642 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001643 if (status != 0) {
1644 if (status == -NFS4ERR_BADNAME &&
1645 !(o_arg->open_flags & O_CREAT))
1646 return -ENOENT;
1647 return status;
1648 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001649
Trond Myklebust6926afd2012-01-07 13:22:46 -05001650 nfs_fattr_map_and_free_names(server, &data->f_attr);
1651
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001652 if (o_arg->open_flags & O_CREAT) {
1653 update_changeattr(dir, &o_res->cinfo);
1654 nfs_post_op_update_inode(dir, o_res->dir_attr);
1655 } else
1656 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001657 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1658 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001660 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001662 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
1664 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001665 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001666 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
1668
Andy Adamsond83217c2011-03-01 01:34:17 +00001669static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001670{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001671 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001672 int ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001673
Trond Myklebusta78cb572009-08-09 15:06:19 -04001674 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001675 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001676 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001677 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001678 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1679 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001680 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001681 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001682 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001683 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001684 return ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001685}
1686
Andy Adamsond83217c2011-03-01 01:34:17 +00001687static int nfs4_recover_expired_lease(struct nfs_server *server)
1688{
1689 return nfs4_client_recover_expired_lease(server->nfs_client);
1690}
1691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692/*
1693 * OPEN_EXPIRED:
1694 * reclaim state on the server after a network partition.
1695 * Assumes caller holds the appropriate lock
1696 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001697static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001699 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001700 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001702 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1703 if (IS_ERR(opendata))
1704 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001705 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001706 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001707 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001708 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001709 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
1711
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001712static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001713{
Trond Myklebust539cd032007-06-05 11:46:42 -04001714 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001715 struct nfs4_exception exception = { };
1716 int err;
1717
1718 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001719 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001720 switch (err) {
1721 default:
1722 goto out;
1723 case -NFS4ERR_GRACE:
1724 case -NFS4ERR_DELAY:
1725 nfs4_handle_exception(server, err, &exception);
1726 err = 0;
1727 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001728 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001729out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001730 return err;
1731}
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1734{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001736 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Trond Myklebust864472e2006-01-03 09:55:15 +01001738 ctx = nfs4_state_find_open_context(state);
1739 if (IS_ERR(ctx))
1740 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001741 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001742 put_nfs_open_context(ctx);
1743 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744}
1745
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001746#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001747static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001748{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001749 int status = NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001750 struct nfs_server *server = NFS_SERVER(state->inode);
1751
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001752 if (state->flags & flags) {
1753 status = nfs41_test_stateid(server, stateid);
1754 if (status != NFS_OK) {
1755 nfs41_free_stateid(server, stateid);
1756 state->flags &= ~flags;
1757 }
1758 }
1759 return status;
1760}
1761
1762static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1763{
1764 int deleg_status, open_status;
1765 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1766 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1767
1768 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1769 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1770
1771 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1772 return NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001773 return nfs4_open_expired(sp, state);
1774}
1775#endif
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001778 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1779 * fields corresponding to attributes that were used to store the verifier.
1780 * Make sure we clobber those fields in the later setattr call
1781 */
1782static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1783{
1784 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1785 !(sattr->ia_valid & ATTR_ATIME_SET))
1786 sattr->ia_valid |= ATTR_ATIME;
1787
1788 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1789 !(sattr->ia_valid & ATTR_MTIME_SET))
1790 sattr->ia_valid |= ATTR_MTIME;
1791}
1792
1793/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001794 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001796static 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 -07001797{
1798 struct nfs4_state_owner *sp;
1799 struct nfs4_state *state = NULL;
1800 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001801 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001802 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001806 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1807 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1809 goto out_err;
1810 }
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001811 status = nfs4_recover_expired_lease(server);
1812 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001813 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001814 if (dentry->d_inode != NULL)
1815 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001816 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001817 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001818 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001819 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Al Viro82a2c1b2011-06-22 18:30:55 -04001821 if (dentry->d_inode != NULL)
1822 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001823
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001824 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001826 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001828 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001829 status = PTR_ERR(state);
1830 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001831 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001832 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001833 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001834
1835 if (opendata->o_arg.open_flags & O_EXCL) {
1836 nfs4_exclusive_attrset(opendata, sattr);
1837
1838 nfs_fattr_init(opendata->o_res.f_attr);
1839 status = nfs4_do_setattr(state->inode, cred,
1840 opendata->o_res.f_attr, sattr,
1841 state);
1842 if (status == 0)
1843 nfs_setattr_update_inode(state->inode, sattr);
1844 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1845 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001846 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 *res = state;
1849 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001850err_opendata_put:
1851 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001852err_put_state_owner:
1853 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 *res = NULL;
1856 return status;
1857}
1858
1859
Al Viro82a2c1b2011-06-22 18:30:55 -04001860static 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 -07001861{
1862 struct nfs4_exception exception = { };
1863 struct nfs4_state *res;
1864 int status;
1865
Trond Myklebust5e74cfe2012-06-08 10:58:09 -04001866 fmode &= FMODE_READ|FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001868 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (status == 0)
1870 break;
1871 /* NOTE: BAD_SEQID means the server and client disagree about the
1872 * book-keeping w.r.t. state-changing operations
1873 * (OPEN/CLOSE/LOCK/LOCKU...)
1874 * It is actually a sign of a bug on the client or on the server.
1875 *
1876 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001877 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 * have unhashed the old state_owner for us, and that we can
1879 * therefore safely retry using a new one. We should still warn
1880 * the user though...
1881 */
1882 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001883 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001884 " returned a bad sequence-id error!\n",
1885 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 exception.retry = 1;
1887 continue;
1888 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001889 /*
1890 * BAD_STATEID on OPEN means that the server cancelled our
1891 * state before it received the OPEN_CONFIRM.
1892 * Recover by retrying the request as per the discussion
1893 * on Page 181 of RFC3530.
1894 */
1895 if (status == -NFS4ERR_BAD_STATEID) {
1896 exception.retry = 1;
1897 continue;
1898 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001899 if (status == -EAGAIN) {
1900 /* We must have found a delegation */
1901 exception.retry = 1;
1902 continue;
1903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1905 status, &exception));
1906 } while (exception.retry);
1907 return res;
1908}
1909
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001910static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1911 struct nfs_fattr *fattr, struct iattr *sattr,
1912 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001914 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001916 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 .iap = sattr,
1918 .server = server,
1919 .bitmask = server->attr_bitmask,
1920 };
1921 struct nfs_setattrres res = {
1922 .fattr = fattr,
1923 .server = server,
1924 };
1925 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001926 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1927 .rpc_argp = &arg,
1928 .rpc_resp = &res,
1929 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001931 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001932 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Trond Myklebust0e574af2005-10-27 22:12:38 -04001934 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Trond Myklebust4fc87962012-03-08 17:42:01 -05001936 if (state != NULL) {
1937 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1938 current->files, current->tgid);
1939 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1940 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001941 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001942 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05001943 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Bryan Schumaker7c513052011-03-24 17:12:24 +00001945 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001946 if (status == 0 && state != NULL)
1947 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001948 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949}
1950
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001951static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1952 struct nfs_fattr *fattr, struct iattr *sattr,
1953 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001955 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001956 struct nfs4_exception exception = {
1957 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05001958 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001959 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 int err;
1961 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04001962 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
1963 switch (err) {
1964 case -NFS4ERR_OPENMODE:
1965 if (state && !(state->state & FMODE_WRITE)) {
1966 err = -EBADF;
1967 if (sattr->ia_valid & ATTR_OPEN)
1968 err = -EACCES;
1969 goto out;
1970 }
1971 }
1972 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04001974out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return err;
1976}
1977
1978struct nfs4_closedata {
1979 struct inode *inode;
1980 struct nfs4_state *state;
1981 struct nfs_closeargs arg;
1982 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001983 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001984 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001985 bool roc;
1986 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987};
1988
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001989static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001990{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001991 struct nfs4_closedata *calldata = data;
1992 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001993 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001994
Fred Isamanf7e89172011-01-06 11:36:32 +00001995 if (calldata->roc)
1996 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001997 nfs4_put_open_state(calldata->state);
1998 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001999 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002000 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002001 kfree(calldata);
2002}
2003
Trond Myklebust88069f72009-12-08 08:33:16 -05002004static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2005 fmode_t fmode)
2006{
2007 spin_lock(&state->owner->so_lock);
2008 if (!(fmode & FMODE_READ))
2009 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2010 if (!(fmode & FMODE_WRITE))
2011 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2012 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2013 spin_unlock(&state->owner->so_lock);
2014}
2015
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002016static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002018 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 struct nfs_server *server = NFS_SERVER(calldata->inode);
2021
Chuck Levera3ca5652012-03-01 17:00:40 -05002022 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002023 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2024 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 /* hmm. we are done with the inode, and in the process of freeing
2026 * the state_owner. we keep this around to process errors
2027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 switch (task->tk_status) {
2029 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002030 if (calldata->roc)
2031 pnfs_roc_set_barrier(state->inode,
2032 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002033 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002034 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002035 nfs4_close_clear_stateid_flags(state,
2036 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 break;
2038 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002039 case -NFS4ERR_OLD_STATEID:
2040 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002042 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002043 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002045 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2046 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002048 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002049 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002050 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
2052
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002053static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002055 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002056 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002057 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002058
Chuck Levera3ca5652012-03-01 17:00:40 -05002059 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002060 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002061 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002062
Trond Myklebust88069f72009-12-08 08:33:16 -05002063 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2064 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002065 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002066 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002067 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002068 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002069 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2070 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2071 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002072 }
2073 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002074 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2075 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2076 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002077 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002078 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002079 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002080
2081 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002082 /* Note: exit _without_ calling nfs4_close_done */
2083 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002084 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002085 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002086
Fred Isamanf7e89172011-01-06 11:36:32 +00002087 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002088 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002089 if (calldata->roc &&
2090 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2091 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2092 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002093 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002094 }
2095 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002096
Trond Myklebust516a6af2005-10-27 22:12:41 -04002097 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002098 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002099 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002100 &calldata->arg.seq_args,
2101 &calldata->res.seq_res,
Trond Myklebust0ada2102012-10-29 18:37:40 -04002102 task) != 0)
2103 nfs_release_seqid(calldata->arg.seqid);
2104 else
2105 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002106out:
2107 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108}
2109
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002110static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002111 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002112 .rpc_call_done = nfs4_close_done,
2113 .rpc_release = nfs4_free_closedata,
2114};
2115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116/*
2117 * It is possible for data to be read/written from a mem-mapped file
2118 * after the sys_close call (which hits the vfs layer as a flush).
2119 * This means that we can't safely call nfsv4 close on a file until
2120 * the inode is cleared. This in turn means that we are not good
2121 * NFSv4 citizens - we do not indicate to the server to update the file's
2122 * share state even when we are done with one of the three share
2123 * stateid's in the inode.
2124 *
2125 * NOTE: Caller must be holding the sp->so_owner semaphore!
2126 */
Al Viro643168c2011-06-22 18:20:23 -04002127int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002129 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002131 struct nfs4_state_owner *sp = state->owner;
2132 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002133 struct rpc_message msg = {
2134 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2135 .rpc_cred = state->owner->so_cred,
2136 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002137 struct rpc_task_setup task_setup_data = {
2138 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002139 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002140 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002141 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002142 .flags = RPC_TASK_ASYNC,
2143 };
Trond Myklebust95121352005-10-18 14:20:12 -07002144 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002146 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002148 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002149 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002150 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002152 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002153 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002155 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002156 if (calldata->arg.seqid == NULL)
2157 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002158 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002159 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002160 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002161 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002162 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002163 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002164 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002165
Trond Myklebust1174dd12010-12-21 10:52:24 -05002166 msg.rpc_argp = &calldata->arg;
2167 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002168 task_setup_data.callback_data = calldata;
2169 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002170 if (IS_ERR(task))
2171 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002172 status = 0;
2173 if (wait)
2174 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002175 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002176 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002177out_free_calldata:
2178 kfree(calldata);
2179out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002180 if (roc)
2181 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002182 nfs4_put_open_state(state);
2183 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002184 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
2186
Trond Myklebust2b484292010-09-17 10:56:51 -04002187static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002188nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 struct nfs4_state *state;
2191
Trond Myklebust565277f2007-10-15 18:17:53 -04002192 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002193 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002194 if (IS_ERR(state))
2195 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002196 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002197 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
2199
Trond Myklebust1185a552009-12-03 15:54:02 -05002200static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002201{
2202 if (ctx->state == NULL)
2203 return;
2204 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002205 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002206 else
Al Viro643168c2011-06-22 18:20:23 -04002207 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002208}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2211{
Benny Halevy43652ad2009-04-01 09:21:54 -04002212 struct nfs4_server_caps_arg args = {
2213 .fhandle = fhandle,
2214 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 struct nfs4_server_caps_res res = {};
2216 struct rpc_message msg = {
2217 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002218 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 .rpc_resp = &res,
2220 };
2221 int status;
2222
Bryan Schumaker7c513052011-03-24 17:12:24 +00002223 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 if (status == 0) {
2225 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002226 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2227 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2228 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2229 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2230 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2232 server->caps |= NFS_CAP_ACLS;
2233 if (res.has_links != 0)
2234 server->caps |= NFS_CAP_HARDLINKS;
2235 if (res.has_symlinks != 0)
2236 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002237 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2238 server->caps |= NFS_CAP_FILEID;
2239 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2240 server->caps |= NFS_CAP_MODE;
2241 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2242 server->caps |= NFS_CAP_NLINK;
2243 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2244 server->caps |= NFS_CAP_OWNER;
2245 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2246 server->caps |= NFS_CAP_OWNER_GROUP;
2247 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2248 server->caps |= NFS_CAP_ATIME;
2249 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2250 server->caps |= NFS_CAP_CTIME;
2251 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2252 server->caps |= NFS_CAP_MTIME;
2253
Trond Myklebusta65318b2009-03-11 14:10:28 -04002254 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2255 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2256 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002258 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002260
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 return status;
2262}
2263
Trond Myklebust55a97592006-06-09 09:34:19 -04002264int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
2266 struct nfs4_exception exception = { };
2267 int err;
2268 do {
2269 err = nfs4_handle_exception(server,
2270 _nfs4_server_capabilities(server, fhandle),
2271 &exception);
2272 } while (exception.retry);
2273 return err;
2274}
2275
2276static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2277 struct nfs_fsinfo *info)
2278{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 struct nfs4_lookup_root_arg args = {
2280 .bitmask = nfs4_fattr_bitmap,
2281 };
2282 struct nfs4_lookup_res res = {
2283 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002284 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 .fh = fhandle,
2286 };
2287 struct rpc_message msg = {
2288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2289 .rpc_argp = &args,
2290 .rpc_resp = &res,
2291 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002292
Trond Myklebust0e574af2005-10-27 22:12:38 -04002293 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002294 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295}
2296
2297static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2298 struct nfs_fsinfo *info)
2299{
2300 struct nfs4_exception exception = { };
2301 int err;
2302 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002303 err = _nfs4_lookup_root(server, fhandle, info);
2304 switch (err) {
2305 case 0:
2306 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002307 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002308 default:
2309 err = nfs4_handle_exception(server, err, &exception);
2310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002312out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return err;
2314}
2315
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002316static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2317 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2318{
2319 struct rpc_auth *auth;
2320 int ret;
2321
2322 auth = rpcauth_create(flavor, server->client);
2323 if (!auth) {
2324 ret = -EIO;
2325 goto out;
2326 }
2327 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002328out:
2329 return ret;
2330}
2331
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002332static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002333 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002335 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002336 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002338 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2339 flav_array[len] = RPC_AUTH_NULL;
2340 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002341
2342 for (i = 0; i < len; i++) {
2343 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002344 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002345 continue;
2346 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002347 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002348 /*
2349 * -EACCESS could mean that the user doesn't have correct permissions
2350 * to access the mount. It could also mean that we tried to mount
2351 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2352 * existing mount programs don't handle -EACCES very well so it should
2353 * be mapped to -EPERM instead.
2354 */
2355 if (status == -EACCES)
2356 status = -EPERM;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002357 return status;
2358}
2359
2360/*
2361 * get the file handle for the "/" directory on the server
2362 */
2363static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2364 struct nfs_fsinfo *info)
2365{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002366 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002367 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002368 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2369 /*
2370 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2371 * by nfs4_map_errors() as this function exits.
2372 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002373 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 if (status == 0)
2375 status = nfs4_server_capabilities(server, fhandle);
2376 if (status == 0)
2377 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002378 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379}
2380
Manoj Naik6b97fd32006-06-09 09:34:29 -04002381/*
2382 * Get locations and (maybe) other attributes of a referral.
2383 * Note that we'll actually follow the referral later when
2384 * we detect fsid mismatch in inode revalidation
2385 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002386static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2387 const struct qstr *name, struct nfs_fattr *fattr,
2388 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002389{
2390 int status = -ENOMEM;
2391 struct page *page = NULL;
2392 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002393
2394 page = alloc_page(GFP_KERNEL);
2395 if (page == NULL)
2396 goto out;
2397 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2398 if (locations == NULL)
2399 goto out;
2400
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002401 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002402 if (status != 0)
2403 goto out;
2404 /* Make sure server returned a different fsid for the referral */
2405 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002406 dprintk("%s: server did not return a different fsid for"
2407 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002408 status = -EIO;
2409 goto out;
2410 }
Andy Adamson533eb462011-06-13 18:25:56 -04002411 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2412 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002413
Andy Adamson533eb462011-06-13 18:25:56 -04002414 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002415 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002416 memset(fhandle, 0, sizeof(struct nfs_fh));
2417out:
2418 if (page)
2419 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002420 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002421 return status;
2422}
2423
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2425{
2426 struct nfs4_getattr_arg args = {
2427 .fh = fhandle,
2428 .bitmask = server->attr_bitmask,
2429 };
2430 struct nfs4_getattr_res res = {
2431 .fattr = fattr,
2432 .server = server,
2433 };
2434 struct rpc_message msg = {
2435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2436 .rpc_argp = &args,
2437 .rpc_resp = &res,
2438 };
2439
Trond Myklebust0e574af2005-10-27 22:12:38 -04002440 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002441 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442}
2443
2444static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2445{
2446 struct nfs4_exception exception = { };
2447 int err;
2448 do {
2449 err = nfs4_handle_exception(server,
2450 _nfs4_proc_getattr(server, fhandle, fattr),
2451 &exception);
2452 } while (exception.retry);
2453 return err;
2454}
2455
2456/*
2457 * The file is not closed if it is opened due to the a request to change
2458 * the size of the file. The open call will not be needed once the
2459 * VFS layer lookup-intents are implemented.
2460 *
2461 * Close is called when the inode is destroyed.
2462 * If we haven't opened the file for O_WRONLY, we
2463 * need to in the size_change case to obtain a stateid.
2464 *
2465 * Got race?
2466 * Because OPEN is always done by name in nfsv4, it is
2467 * possible that we opened a different file by the same
2468 * name. We can recognize this race condition, but we
2469 * can't do anything about it besides returning an error.
2470 *
2471 * This will be fixed with VFS changes (lookup-intent).
2472 */
2473static int
2474nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2475 struct iattr *sattr)
2476{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002477 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002478 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002479 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 int status;
2481
Benny Halevy8a1636c2010-07-14 15:43:57 -04002482 if (pnfs_ld_layoutret_on_setattr(inode))
2483 pnfs_return_layout(inode);
2484
Trond Myklebust0e574af2005-10-27 22:12:38 -04002485 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Trond Myklebustd5308382005-11-04 15:33:38 -05002487 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002488 if (sattr->ia_valid & ATTR_FILE) {
2489 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002490
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002491 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002492 if (ctx) {
2493 cred = ctx->cred;
2494 state = ctx->state;
2495 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002496 }
2497
Trond Myklebust536e43d2012-01-17 22:04:26 -05002498 /* Deal with open(O_TRUNC) */
2499 if (sattr->ia_valid & ATTR_OPEN)
2500 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2501
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002502 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002503 if (status == 0)
2504 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 return status;
2506}
2507
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002508static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2509 const struct qstr *name, struct nfs_fh *fhandle,
2510 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002511{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002512 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002513 int status;
2514 struct nfs4_lookup_arg args = {
2515 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002516 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002517 .name = name,
2518 };
2519 struct nfs4_lookup_res res = {
2520 .server = server,
2521 .fattr = fattr,
2522 .fh = fhandle,
2523 };
2524 struct rpc_message msg = {
2525 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2526 .rpc_argp = &args,
2527 .rpc_resp = &res,
2528 };
2529
2530 nfs_fattr_init(fattr);
2531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002533 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 dprintk("NFS reply lookup: %d\n", status);
2535 return status;
2536}
2537
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002538static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002539{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002540 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002541 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002542 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2543 fattr->nlink = 2;
2544}
2545
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002546static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2547 struct qstr *name, struct nfs_fh *fhandle,
2548 struct nfs_fattr *fattr)
2549{
2550 struct nfs4_exception exception = { };
2551 struct rpc_clnt *client = *clnt;
2552 int err;
2553 do {
2554 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2555 switch (err) {
2556 case -NFS4ERR_BADNAME:
2557 err = -ENOENT;
2558 goto out;
2559 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002560 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002561 goto out;
2562 case -NFS4ERR_WRONGSEC:
2563 err = -EPERM;
2564 if (client != *clnt)
2565 goto out;
2566
2567 client = nfs4_create_sec_client(client, dir, name);
2568 if (IS_ERR(client))
2569 return PTR_ERR(client);
2570
2571 exception.retry = 1;
2572 break;
2573 default:
2574 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2575 }
2576 } while (exception.retry);
2577
2578out:
2579 if (err == 0)
2580 *clnt = client;
2581 else if (client != *clnt)
2582 rpc_shutdown_client(client);
2583
2584 return err;
2585}
2586
Bryan Schumaker7c513052011-03-24 17:12:24 +00002587static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2588 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002590 int status;
2591 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002592
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002593 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2594 if (client != NFS_CLIENT(dir)) {
2595 rpc_shutdown_client(client);
2596 nfs_fixup_secinfo_attributes(fattr);
2597 }
2598 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599}
2600
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002601struct rpc_clnt *
2602nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2603 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2604{
2605 int status;
2606 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2607
2608 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2609 if (status < 0) {
2610 rpc_shutdown_client(client);
2611 return ERR_PTR(status);
2612 }
2613 return client;
2614}
2615
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2617{
Trond Myklebust76b32992007-08-10 17:45:11 -04002618 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 struct nfs4_accessargs args = {
2620 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002621 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002623 struct nfs4_accessres res = {
2624 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002625 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 struct rpc_message msg = {
2627 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2628 .rpc_argp = &args,
2629 .rpc_resp = &res,
2630 .rpc_cred = entry->cred,
2631 };
2632 int mode = entry->mask;
2633 int status;
2634
2635 /*
2636 * Determine which access bits we want to ask for...
2637 */
2638 if (mode & MAY_READ)
2639 args.access |= NFS4_ACCESS_READ;
2640 if (S_ISDIR(inode->i_mode)) {
2641 if (mode & MAY_WRITE)
2642 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2643 if (mode & MAY_EXEC)
2644 args.access |= NFS4_ACCESS_LOOKUP;
2645 } else {
2646 if (mode & MAY_WRITE)
2647 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2648 if (mode & MAY_EXEC)
2649 args.access |= NFS4_ACCESS_EXECUTE;
2650 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002651
2652 res.fattr = nfs_alloc_fattr();
2653 if (res.fattr == NULL)
2654 return -ENOMEM;
2655
Bryan Schumaker7c513052011-03-24 17:12:24 +00002656 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 if (!status) {
2658 entry->mask = 0;
2659 if (res.access & NFS4_ACCESS_READ)
2660 entry->mask |= MAY_READ;
2661 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2662 entry->mask |= MAY_WRITE;
2663 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2664 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002665 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002667 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 return status;
2669}
2670
2671static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2672{
2673 struct nfs4_exception exception = { };
2674 int err;
2675 do {
2676 err = nfs4_handle_exception(NFS_SERVER(inode),
2677 _nfs4_proc_access(inode, entry),
2678 &exception);
2679 } while (exception.retry);
2680 return err;
2681}
2682
2683/*
2684 * TODO: For the time being, we don't try to get any attributes
2685 * along with any of the zero-copy operations READ, READDIR,
2686 * READLINK, WRITE.
2687 *
2688 * In the case of the first three, we want to put the GETATTR
2689 * after the read-type operation -- this is because it is hard
2690 * to predict the length of a GETATTR response in v4, and thus
2691 * align the READ data correctly. This means that the GETATTR
2692 * may end up partially falling into the page cache, and we should
2693 * shift it into the 'tail' of the xdr_buf before processing.
2694 * To do this efficiently, we need to know the total length
2695 * of data received, which doesn't seem to be available outside
2696 * of the RPC layer.
2697 *
2698 * In the case of WRITE, we also want to put the GETATTR after
2699 * the operation -- in this case because we want to make sure
2700 * we get the post-operation mtime and size. This means that
2701 * we can't use xdr_encode_pages() as written: we need a variant
2702 * of it which would leave room in the 'tail' iovec.
2703 *
2704 * Both of these changes to the XDR layer would in fact be quite
2705 * minor, but I decided to leave them for a subsequent patch.
2706 */
2707static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2708 unsigned int pgbase, unsigned int pglen)
2709{
2710 struct nfs4_readlink args = {
2711 .fh = NFS_FH(inode),
2712 .pgbase = pgbase,
2713 .pglen = pglen,
2714 .pages = &page,
2715 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002716 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 struct rpc_message msg = {
2718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2719 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002720 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 };
2722
Bryan Schumaker7c513052011-03-24 17:12:24 +00002723 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 -07002724}
2725
2726static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2727 unsigned int pgbase, unsigned int pglen)
2728{
2729 struct nfs4_exception exception = { };
2730 int err;
2731 do {
2732 err = nfs4_handle_exception(NFS_SERVER(inode),
2733 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2734 &exception);
2735 } while (exception.retry);
2736 return err;
2737}
2738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739/*
2740 * Got race?
2741 * We will need to arrange for the VFS layer to provide an atomic open.
2742 * Until then, this create/open method is prone to inefficiency and race
2743 * conditions due to the lookup, create, and open VFS calls from sys_open()
2744 * placed on the wire.
2745 *
2746 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2747 * The file will be opened again in the subsequent VFS open call
2748 * (nfs4_proc_file_open).
2749 *
2750 * The open for read will just hang around to be used by any process that
2751 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2752 */
2753
2754static int
2755nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002756 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757{
Al Viro82a2c1b2011-06-22 18:30:55 -04002758 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002760 struct rpc_cred *cred = NULL;
2761 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 int status = 0;
2763
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002764 if (ctx != NULL) {
2765 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002766 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002767 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002769 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002770 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002771 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 if (IS_ERR(state)) {
2773 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002774 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002776 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002777 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002778 if (ctx != NULL)
2779 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002780 else
Al Viro643168c2011-06-22 18:20:23 -04002781 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782out:
2783 return status;
2784}
2785
2786static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2787{
Trond Myklebust16e42952005-10-27 22:12:44 -04002788 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002789 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002791 .name.len = name->len,
2792 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002793 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002795 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002796 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002797 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002799 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2800 .rpc_argp = &args,
2801 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002803 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
Trond Myklebustd3468902010-04-16 16:22:50 -04002805 res.dir_attr = nfs_alloc_fattr();
2806 if (res.dir_attr == NULL)
2807 goto out;
2808
Bryan Schumaker7c513052011-03-24 17:12:24 +00002809 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002810 if (status == 0) {
2811 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002812 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002813 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002814 nfs_free_fattr(res.dir_attr);
2815out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 return status;
2817}
2818
2819static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2820{
2821 struct nfs4_exception exception = { };
2822 int err;
2823 do {
2824 err = nfs4_handle_exception(NFS_SERVER(dir),
2825 _nfs4_proc_remove(dir, name),
2826 &exception);
2827 } while (exception.retry);
2828 return err;
2829}
2830
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002831static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002833 struct nfs_server *server = NFS_SERVER(dir);
2834 struct nfs_removeargs *args = msg->rpc_argp;
2835 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
Trond Myklebusta65318b2009-03-11 14:10:28 -04002837 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002838 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002840 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841}
2842
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002843static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2844{
2845 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2846 &data->args.seq_args,
2847 &data->res.seq_res,
2848 task))
2849 return;
2850 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851}
2852
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002853static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002855 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2856
Trond Myklebust14516c32010-07-31 14:29:06 -04002857 if (!nfs4_sequence_done(task, &res->seq_res))
2858 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002859 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002860 return 0;
2861 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002862 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002863 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864}
2865
Jeff Laytond3d41522010-09-17 17:31:57 -04002866static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2867{
2868 struct nfs_server *server = NFS_SERVER(dir);
2869 struct nfs_renameargs *arg = msg->rpc_argp;
2870 struct nfs_renameres *res = msg->rpc_resp;
2871
2872 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2873 arg->bitmask = server->attr_bitmask;
2874 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002875 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002876}
2877
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002878static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2879{
2880 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2881 &data->args.seq_args,
2882 &data->res.seq_res,
2883 task))
2884 return;
2885 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002886}
2887
2888static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2889 struct inode *new_dir)
2890{
2891 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2892
2893 if (!nfs4_sequence_done(task, &res->seq_res))
2894 return 0;
2895 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2896 return 0;
2897
2898 update_changeattr(old_dir, &res->old_cinfo);
2899 nfs_post_op_update_inode(old_dir, res->old_fattr);
2900 update_changeattr(new_dir, &res->new_cinfo);
2901 nfs_post_op_update_inode(new_dir, res->new_fattr);
2902 return 1;
2903}
2904
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2906 struct inode *new_dir, struct qstr *new_name)
2907{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002908 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002909 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 .old_dir = NFS_FH(old_dir),
2911 .new_dir = NFS_FH(new_dir),
2912 .old_name = old_name,
2913 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002914 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002916 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002917 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002918 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 struct rpc_message msg = {
2920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2921 .rpc_argp = &arg,
2922 .rpc_resp = &res,
2923 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002924 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
Trond Myklebust011fff72010-04-16 16:22:49 -04002926 res.old_fattr = nfs_alloc_fattr();
2927 res.new_fattr = nfs_alloc_fattr();
2928 if (res.old_fattr == NULL || res.new_fattr == NULL)
2929 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Bryan Schumaker7c513052011-03-24 17:12:24 +00002931 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 if (!status) {
2933 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002934 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002936 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002938out:
2939 nfs_free_fattr(res.new_fattr);
2940 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return status;
2942}
2943
2944static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2945 struct inode *new_dir, struct qstr *new_name)
2946{
2947 struct nfs4_exception exception = { };
2948 int err;
2949 do {
2950 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2951 _nfs4_proc_rename(old_dir, old_name,
2952 new_dir, new_name),
2953 &exception);
2954 } while (exception.retry);
2955 return err;
2956}
2957
2958static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2959{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002960 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 struct nfs4_link_arg arg = {
2962 .fh = NFS_FH(inode),
2963 .dir_fh = NFS_FH(dir),
2964 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002965 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002967 struct nfs4_link_res res = {
2968 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002969 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 struct rpc_message msg = {
2971 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2972 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002973 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002975 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Trond Myklebust136f2622010-04-16 16:22:49 -04002977 res.fattr = nfs_alloc_fattr();
2978 res.dir_attr = nfs_alloc_fattr();
2979 if (res.fattr == NULL || res.dir_attr == NULL)
2980 goto out;
2981
Bryan Schumaker7c513052011-03-24 17:12:24 +00002982 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002983 if (!status) {
2984 update_changeattr(dir, &res.cinfo);
2985 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002986 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002987 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002988out:
2989 nfs_free_fattr(res.dir_attr);
2990 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 return status;
2992}
2993
2994static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2995{
2996 struct nfs4_exception exception = { };
2997 int err;
2998 do {
2999 err = nfs4_handle_exception(NFS_SERVER(inode),
3000 _nfs4_proc_link(inode, dir, name),
3001 &exception);
3002 } while (exception.retry);
3003 return err;
3004}
3005
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003006struct nfs4_createdata {
3007 struct rpc_message msg;
3008 struct nfs4_create_arg arg;
3009 struct nfs4_create_res res;
3010 struct nfs_fh fh;
3011 struct nfs_fattr fattr;
3012 struct nfs_fattr dir_fattr;
3013};
3014
3015static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3016 struct qstr *name, struct iattr *sattr, u32 ftype)
3017{
3018 struct nfs4_createdata *data;
3019
3020 data = kzalloc(sizeof(*data), GFP_KERNEL);
3021 if (data != NULL) {
3022 struct nfs_server *server = NFS_SERVER(dir);
3023
3024 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3025 data->msg.rpc_argp = &data->arg;
3026 data->msg.rpc_resp = &data->res;
3027 data->arg.dir_fh = NFS_FH(dir);
3028 data->arg.server = server;
3029 data->arg.name = name;
3030 data->arg.attrs = sattr;
3031 data->arg.ftype = ftype;
3032 data->arg.bitmask = server->attr_bitmask;
3033 data->res.server = server;
3034 data->res.fh = &data->fh;
3035 data->res.fattr = &data->fattr;
3036 data->res.dir_fattr = &data->dir_fattr;
3037 nfs_fattr_init(data->res.fattr);
3038 nfs_fattr_init(data->res.dir_fattr);
3039 }
3040 return data;
3041}
3042
3043static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3044{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003045 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003046 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003047 if (status == 0) {
3048 update_changeattr(dir, &data->res.dir_cinfo);
3049 nfs_post_op_update_inode(dir, data->res.dir_fattr);
3050 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3051 }
3052 return status;
3053}
3054
3055static void nfs4_free_createdata(struct nfs4_createdata *data)
3056{
3057 kfree(data);
3058}
3059
Chuck Lever4f390c12006-08-22 20:06:22 -04003060static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003061 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003063 struct nfs4_createdata *data;
3064 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
Chuck Lever94a6d752006-08-22 20:06:23 -04003066 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003067 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003068
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003069 status = -ENOMEM;
3070 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3071 if (data == NULL)
3072 goto out;
3073
3074 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3075 data->arg.u.symlink.pages = &page;
3076 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003078 status = nfs4_do_create(dir, dentry, data);
3079
3080 nfs4_free_createdata(data);
3081out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 return status;
3083}
3084
Chuck Lever4f390c12006-08-22 20:06:22 -04003085static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003086 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
3088 struct nfs4_exception exception = { };
3089 int err;
3090 do {
3091 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003092 _nfs4_proc_symlink(dir, dentry, page,
3093 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 &exception);
3095 } while (exception.retry);
3096 return err;
3097}
3098
3099static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3100 struct iattr *sattr)
3101{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003102 struct nfs4_createdata *data;
3103 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003105 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3106 if (data == NULL)
3107 goto out;
3108
3109 status = nfs4_do_create(dir, dentry, data);
3110
3111 nfs4_free_createdata(data);
3112out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 return status;
3114}
3115
3116static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3117 struct iattr *sattr)
3118{
3119 struct nfs4_exception exception = { };
3120 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003121
3122 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 do {
3124 err = nfs4_handle_exception(NFS_SERVER(dir),
3125 _nfs4_proc_mkdir(dir, dentry, sattr),
3126 &exception);
3127 } while (exception.retry);
3128 return err;
3129}
3130
3131static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003132 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133{
3134 struct inode *dir = dentry->d_inode;
3135 struct nfs4_readdir_arg args = {
3136 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003137 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 .pgbase = 0,
3139 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003140 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003141 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 };
3143 struct nfs4_readdir_res res;
3144 struct rpc_message msg = {
3145 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3146 .rpc_argp = &args,
3147 .rpc_resp = &res,
3148 .rpc_cred = cred,
3149 };
3150 int status;
3151
Harvey Harrison3110ff82008-05-02 13:42:44 -07003152 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003153 dentry->d_parent->d_name.name,
3154 dentry->d_name.name,
3155 (unsigned long long)cookie);
Trond Myklebustf15e7242012-09-03 14:56:02 -04003156 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003158 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 -05003159 if (status >= 0) {
Trond Myklebustf15e7242012-09-03 14:56:02 -04003160 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003161 status += args.pgbase;
3162 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003163
3164 nfs_invalidate_atime(dir);
3165
Harvey Harrison3110ff82008-05-02 13:42:44 -07003166 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 return status;
3168}
3169
3170static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003171 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
3173 struct nfs4_exception exception = { };
3174 int err;
3175 do {
3176 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3177 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003178 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 &exception);
3180 } while (exception.retry);
3181 return err;
3182}
3183
3184static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3185 struct iattr *sattr, dev_t rdev)
3186{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003187 struct nfs4_createdata *data;
3188 int mode = sattr->ia_mode;
3189 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
3191 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3192 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003193
3194 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3195 if (data == NULL)
3196 goto out;
3197
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003199 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003201 data->arg.ftype = NF4BLK;
3202 data->arg.u.device.specdata1 = MAJOR(rdev);
3203 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 }
3205 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003206 data->arg.ftype = NF4CHR;
3207 data->arg.u.device.specdata1 = MAJOR(rdev);
3208 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003211 status = nfs4_do_create(dir, dentry, data);
3212
3213 nfs4_free_createdata(data);
3214out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 return status;
3216}
3217
3218static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3219 struct iattr *sattr, dev_t rdev)
3220{
3221 struct nfs4_exception exception = { };
3222 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003223
3224 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 do {
3226 err = nfs4_handle_exception(NFS_SERVER(dir),
3227 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3228 &exception);
3229 } while (exception.retry);
3230 return err;
3231}
3232
3233static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3234 struct nfs_fsstat *fsstat)
3235{
3236 struct nfs4_statfs_arg args = {
3237 .fh = fhandle,
3238 .bitmask = server->attr_bitmask,
3239 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003240 struct nfs4_statfs_res res = {
3241 .fsstat = fsstat,
3242 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 struct rpc_message msg = {
3244 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3245 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003246 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 };
3248
Trond Myklebust0e574af2005-10-27 22:12:38 -04003249 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003250 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251}
3252
3253static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3254{
3255 struct nfs4_exception exception = { };
3256 int err;
3257 do {
3258 err = nfs4_handle_exception(server,
3259 _nfs4_proc_statfs(server, fhandle, fsstat),
3260 &exception);
3261 } while (exception.retry);
3262 return err;
3263}
3264
3265static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3266 struct nfs_fsinfo *fsinfo)
3267{
3268 struct nfs4_fsinfo_arg args = {
3269 .fh = fhandle,
3270 .bitmask = server->attr_bitmask,
3271 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003272 struct nfs4_fsinfo_res res = {
3273 .fsinfo = fsinfo,
3274 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 struct rpc_message msg = {
3276 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3277 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003278 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 };
3280
Bryan Schumaker7c513052011-03-24 17:12:24 +00003281 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282}
3283
3284static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3285{
3286 struct nfs4_exception exception = { };
3287 int err;
3288
3289 do {
3290 err = nfs4_handle_exception(server,
3291 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3292 &exception);
3293 } while (exception.retry);
3294 return err;
3295}
3296
3297static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3298{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003299 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3301}
3302
3303static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3304 struct nfs_pathconf *pathconf)
3305{
3306 struct nfs4_pathconf_arg args = {
3307 .fh = fhandle,
3308 .bitmask = server->attr_bitmask,
3309 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003310 struct nfs4_pathconf_res res = {
3311 .pathconf = pathconf,
3312 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 struct rpc_message msg = {
3314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3315 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003316 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 };
3318
3319 /* None of the pathconf attributes are mandatory to implement */
3320 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3321 memset(pathconf, 0, sizeof(*pathconf));
3322 return 0;
3323 }
3324
Trond Myklebust0e574af2005-10-27 22:12:38 -04003325 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003326 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327}
3328
3329static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3330 struct nfs_pathconf *pathconf)
3331{
3332 struct nfs4_exception exception = { };
3333 int err;
3334
3335 do {
3336 err = nfs4_handle_exception(server,
3337 _nfs4_proc_pathconf(server, fhandle, pathconf),
3338 &exception);
3339 } while (exception.retry);
3340 return err;
3341}
3342
Benny Halevyd20581a2011-05-22 19:52:03 +03003343void __nfs4_read_done_cb(struct nfs_read_data *data)
3344{
3345 nfs_invalidate_atime(data->inode);
3346}
3347
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003348static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349{
Trond Myklebustec06c092006-03-20 13:44:27 -05003350 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003352 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003353 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003354 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003356
Benny Halevyd20581a2011-05-22 19:52:03 +03003357 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003359 renew_lease(server, data->timestamp);
3360 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361}
3362
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003363static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3364{
3365
3366 dprintk("--> %s\n", __func__);
3367
3368 if (!nfs4_sequence_done(task, &data->res.seq_res))
3369 return -EAGAIN;
3370
Benny Halevyd20581a2011-05-22 19:52:03 +03003371 return data->read_done_cb ? data->read_done_cb(task, data) :
3372 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003373}
3374
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003375static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003378 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003379 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003380 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381}
3382
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003383static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3384{
3385 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3386 &data->args.seq_args,
3387 &data->res.seq_res,
3388 task))
3389 return;
3390 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391}
3392
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003393/* Reset the the nfs_read_data to send the read to the MDS. */
3394void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3395{
3396 dprintk("%s Reset task for i/o through\n", __func__);
3397 put_lseg(data->lseg);
3398 data->lseg = NULL;
3399 /* offsets will differ in the dense stripe case */
3400 data->args.offset = data->mds_offset;
3401 data->ds_clp = NULL;
3402 data->args.fh = NFS_FH(data->inode);
3403 data->read_done_cb = nfs4_read_done_cb;
3404 task->tk_ops = data->mds_ops;
3405 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3406}
3407EXPORT_SYMBOL_GPL(nfs4_reset_read);
3408
Fred Isamanb029bc92011-03-03 15:13:42 +00003409static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 struct inode *inode = data->inode;
3412
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003413 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003414 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003415 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003417 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003419 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003420 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422}
3423
Fred Isamanb029bc92011-03-03 15:13:42 +00003424static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3425{
3426 if (!nfs4_sequence_done(task, &data->res.seq_res))
3427 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003428 return data->write_done_cb ? data->write_done_cb(task, data) :
3429 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003430}
3431
Fred Isamana69aef12011-03-03 15:13:47 +00003432/* Reset the the nfs_write_data to send the write to the MDS. */
3433void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3434{
3435 dprintk("%s Reset task for i/o through\n", __func__);
3436 put_lseg(data->lseg);
3437 data->lseg = NULL;
3438 data->ds_clp = NULL;
3439 data->write_done_cb = nfs4_write_done_cb;
3440 data->args.fh = NFS_FH(data->inode);
3441 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3442 data->args.offset = data->mds_offset;
3443 data->res.fattr = &data->fattr;
3444 task->tk_ops = data->mds_ops;
3445 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3446}
3447EXPORT_SYMBOL_GPL(nfs4_reset_write);
3448
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003449static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003451 struct nfs_server *server = NFS_SERVER(data->inode);
3452
Fred Isaman7ffd1062011-03-03 15:13:46 +00003453 if (data->lseg) {
3454 data->args.bitmask = NULL;
3455 data->res.fattr = NULL;
3456 } else
3457 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003458 if (!data->write_done_cb)
3459 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003460 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 data->timestamp = jiffies;
3462
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003463 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003464 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465}
3466
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003467static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3468{
3469 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3470 &data->args.seq_args,
3471 &data->res.seq_res,
3472 task))
3473 return;
3474 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475}
3476
Fred Isaman5f452432011-03-23 13:27:46 +00003477static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003480
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003481 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003482 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003483 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003485 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003486 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487}
3488
Fred Isaman5f452432011-03-23 13:27:46 +00003489static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3490{
3491 if (!nfs4_sequence_done(task, &data->res.seq_res))
3492 return -EAGAIN;
3493 return data->write_done_cb(task, data);
3494}
3495
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003496static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003498 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003499
3500 if (data->lseg) {
3501 data->args.bitmask = NULL;
3502 data->res.fattr = NULL;
3503 } else
3504 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003505 if (!data->write_done_cb)
3506 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003507 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003508 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003509 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510}
3511
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003512struct nfs4_renewdata {
3513 struct nfs_client *client;
3514 unsigned long timestamp;
3515};
3516
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517/*
3518 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3519 * standalone procedure for queueing an asynchronous RENEW.
3520 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003521static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003522{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003523 struct nfs4_renewdata *data = calldata;
3524 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003525
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003526 if (atomic_read(&clp->cl_count) > 1)
3527 nfs4_schedule_state_renewal(clp);
3528 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003529 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003530}
3531
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003532static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003534 struct nfs4_renewdata *data = calldata;
3535 struct nfs_client *clp = data->client;
3536 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
3538 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003539 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003540 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3541 return;
3542 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003543 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003544 return;
3545 }
3546 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003548 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549}
3550
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003551static const struct rpc_call_ops nfs4_renew_ops = {
3552 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003553 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003554};
3555
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003556static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557{
3558 struct rpc_message msg = {
3559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3560 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003561 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003563 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003565 if (renew_flags == 0)
3566 return 0;
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003567 if (!atomic_inc_not_zero(&clp->cl_count))
3568 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003569 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003570 if (data == NULL)
3571 return -ENOMEM;
3572 data->client = clp;
3573 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003575 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576}
3577
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003578static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579{
3580 struct rpc_message msg = {
3581 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3582 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003583 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 };
3585 unsigned long now = jiffies;
3586 int status;
3587
3588 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3589 if (status < 0)
3590 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003591 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 return 0;
3593}
3594
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003595static inline int nfs4_server_supports_acls(struct nfs_server *server)
3596{
3597 return (server->caps & NFS_CAP_ACLS)
3598 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3599 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3600}
3601
3602/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3603 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3604 * the stack.
3605 */
3606#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3607
Neil Hormane9e3d722011-03-04 19:26:03 -05003608static int buf_to_pages_noslab(const void *buf, size_t buflen,
3609 struct page **pages, unsigned int *pgbase)
3610{
3611 struct page *newpage, **spages;
3612 int rc = 0;
3613 size_t len;
3614 spages = pages;
3615
3616 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003617 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003618 newpage = alloc_page(GFP_KERNEL);
3619
3620 if (newpage == NULL)
3621 goto unwind;
3622 memcpy(page_address(newpage), buf, len);
3623 buf += len;
3624 buflen -= len;
3625 *pages++ = newpage;
3626 rc++;
3627 } while (buflen != 0);
3628
3629 return rc;
3630
3631unwind:
3632 for(; rc > 0; rc--)
3633 __free_page(spages[rc-1]);
3634 return -ENOMEM;
3635}
3636
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003637struct nfs4_cached_acl {
3638 int cached;
3639 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003640 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003641};
3642
3643static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003644{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003645 struct nfs_inode *nfsi = NFS_I(inode);
3646
3647 spin_lock(&inode->i_lock);
3648 kfree(nfsi->nfs4_acl);
3649 nfsi->nfs4_acl = acl;
3650 spin_unlock(&inode->i_lock);
3651}
3652
3653static void nfs4_zap_acl_attr(struct inode *inode)
3654{
3655 nfs4_set_cached_acl(inode, NULL);
3656}
3657
3658static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3659{
3660 struct nfs_inode *nfsi = NFS_I(inode);
3661 struct nfs4_cached_acl *acl;
3662 int ret = -ENOENT;
3663
3664 spin_lock(&inode->i_lock);
3665 acl = nfsi->nfs4_acl;
3666 if (acl == NULL)
3667 goto out;
3668 if (buf == NULL) /* user is just asking for length */
3669 goto out_len;
3670 if (acl->cached == 0)
3671 goto out;
3672 ret = -ERANGE; /* see getxattr(2) man page */
3673 if (acl->len > buflen)
3674 goto out;
3675 memcpy(buf, acl->data, acl->len);
3676out_len:
3677 ret = acl->len;
3678out:
3679 spin_unlock(&inode->i_lock);
3680 return ret;
3681}
3682
Sachin Prabhu5794d212012-04-17 14:36:40 +01003683static 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 +00003684{
3685 struct nfs4_cached_acl *acl;
3686
Sachin Prabhu5794d212012-04-17 14:36:40 +01003687 if (pages && acl_len <= PAGE_SIZE) {
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003688 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3689 if (acl == NULL)
3690 goto out;
3691 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003692 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003693 } else {
3694 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3695 if (acl == NULL)
3696 goto out;
3697 acl->cached = 0;
3698 }
3699 acl->len = acl_len;
3700out:
3701 nfs4_set_cached_acl(inode, acl);
3702}
3703
Andy Adamsonbf118a32011-12-07 11:55:27 -05003704/*
3705 * The getxattr API returns the required buffer length when called with a
3706 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3707 * the required buf. On a NULL buf, we send a page of data to the server
3708 * guessing that the ACL request can be serviced by a page. If so, we cache
3709 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3710 * the cache. If not so, we throw away the page, and cache the required
3711 * length. The next getxattr call will then produce another round trip to
3712 * the server, this time with the input buf of the required size.
3713 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003714static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003715{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003716 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003717 struct nfs_getaclargs args = {
3718 .fh = NFS_FH(inode),
3719 .acl_pages = pages,
3720 .acl_len = buflen,
3721 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003722 struct nfs_getaclres res = {
3723 .acl_len = buflen,
3724 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003725 struct rpc_message msg = {
3726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3727 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003728 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003729 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003730 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003731
Andy Adamsonbf118a32011-12-07 11:55:27 -05003732 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3733 /* As long as we're doing a round trip to the server anyway,
3734 * let's be prepared for a page of acl data. */
3735 if (npages == 0)
3736 npages = 1;
3737
Sachin Prabhu5a006892012-04-17 14:35:39 +01003738 /* Add an extra page to handle the bitmap returned */
3739 npages++;
3740
Andy Adamsonbf118a32011-12-07 11:55:27 -05003741 for (i = 0; i < npages; i++) {
3742 pages[i] = alloc_page(GFP_KERNEL);
3743 if (!pages[i])
3744 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003745 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003746
3747 /* for decoding across pages */
3748 res.acl_scratch = alloc_page(GFP_KERNEL);
3749 if (!res.acl_scratch)
3750 goto out_free;
3751
Andy Adamsonbf118a32011-12-07 11:55:27 -05003752 args.acl_len = npages * PAGE_SIZE;
3753 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003754
Andy Adamsonbf118a32011-12-07 11:55:27 -05003755 /* Let decode_getfacl know not to fail if the ACL data is larger than
3756 * the page we send as a guess */
3757 if (buf == NULL)
3758 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003759
Peng Taode040be2012-01-10 22:42:47 +08003760 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003761 __func__, buf, buflen, npages, args.acl_len);
3762 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3763 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003764 if (ret)
3765 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003766
3767 acl_len = res.acl_len - res.acl_data_offset;
3768 if (acl_len > args.acl_len)
Sachin Prabhu5794d212012-04-17 14:36:40 +01003769 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003770 else
Sachin Prabhu5794d212012-04-17 14:36:40 +01003771 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
Andy Adamsonbf118a32011-12-07 11:55:27 -05003772 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003773 if (buf) {
3774 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003775 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003776 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003777 _copy_from_pages(buf, pages, res.acl_data_offset,
Sachin Prabhu20e0fa92012-03-22 16:46:28 +00003778 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003779 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003780 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003781out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003782 for (i = 0; i < npages; i++)
3783 if (pages[i])
3784 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003785 if (res.acl_scratch)
3786 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003787 return ret;
3788}
3789
Trond Myklebust16b42892006-08-24 12:27:15 -04003790static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3791{
3792 struct nfs4_exception exception = { };
3793 ssize_t ret;
3794 do {
3795 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3796 if (ret >= 0)
3797 break;
3798 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3799 } while (exception.retry);
3800 return ret;
3801}
3802
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003803static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3804{
3805 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003806 int ret;
3807
3808 if (!nfs4_server_supports_acls(server))
3809 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003810 ret = nfs_revalidate_inode(server, inode);
3811 if (ret < 0)
3812 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003813 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3814 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003815 ret = nfs4_read_cached_acl(inode, buf, buflen);
3816 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003817 /* -ENOENT is returned if there is no ACL or if there is an ACL
3818 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003819 return ret;
3820 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003821}
3822
Trond Myklebust16b42892006-08-24 12:27:15 -04003823static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003824{
3825 struct nfs_server *server = NFS_SERVER(inode);
3826 struct page *pages[NFS4ACL_MAXPAGES];
3827 struct nfs_setaclargs arg = {
3828 .fh = NFS_FH(inode),
3829 .acl_pages = pages,
3830 .acl_len = buflen,
3831 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003832 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003833 struct rpc_message msg = {
3834 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3835 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003836 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003837 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003838 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003839
3840 if (!nfs4_server_supports_acls(server))
3841 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003842 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3843 if (i < 0)
3844 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003845 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003846 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003847
3848 /*
3849 * Free each page after tx, so the only ref left is
3850 * held by the network stack
3851 */
3852 for (; i > 0; i--)
3853 put_page(pages[i-1]);
3854
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003855 /*
3856 * Acl update can result in inode attribute update.
3857 * so mark the attribute cache invalid.
3858 */
3859 spin_lock(&inode->i_lock);
3860 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3861 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003862 nfs_access_zap_cache(inode);
3863 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003864 return ret;
3865}
3866
Trond Myklebust16b42892006-08-24 12:27:15 -04003867static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3868{
3869 struct nfs4_exception exception = { };
3870 int err;
3871 do {
3872 err = nfs4_handle_exception(NFS_SERVER(inode),
3873 __nfs4_proc_set_acl(inode, buf, buflen),
3874 &exception);
3875 } while (exception.retry);
3876 return err;
3877}
3878
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003880nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003882 struct nfs_client *clp = server->nfs_client;
3883
3884 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 return 0;
3886 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003887 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003888 case -NFS4ERR_ADMIN_REVOKED:
3889 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003890 if (state == NULL)
3891 break;
3892 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003893 case -NFS4ERR_OPENMODE:
3894 if (state == NULL)
3895 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003896 nfs4_schedule_stateid_recovery(server, state);
3897 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003898 case -NFS4ERR_EXPIRED:
3899 if (state != NULL)
3900 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003902 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003903 nfs4_schedule_lease_recovery(clp);
3904 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003905#if defined(CONFIG_NFS_V4_1)
3906 case -NFS4ERR_BADSESSION:
3907 case -NFS4ERR_BADSLOT:
3908 case -NFS4ERR_BAD_HIGH_SLOT:
3909 case -NFS4ERR_DEADSESSION:
3910 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3911 case -NFS4ERR_SEQ_FALSE_RETRY:
3912 case -NFS4ERR_SEQ_MISORDERED:
3913 dprintk("%s ERROR %d, Reset session\n", __func__,
3914 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003915 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003916 task->tk_status = 0;
3917 return -EAGAIN;
3918#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003920 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003921 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003922 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3924 task->tk_status = 0;
3925 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003926 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 case -NFS4ERR_OLD_STATEID:
3928 task->tk_status = 0;
3929 return -EAGAIN;
3930 }
3931 task->tk_status = nfs4_map_errors(task->tk_status);
3932 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003933wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003934 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003935 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3936 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3937 task->tk_status = 0;
3938 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939}
3940
Chuck Levercd937102012-03-02 17:14:31 -05003941static void nfs4_construct_boot_verifier(struct nfs_client *clp,
3942 nfs4_verifier *bootverf)
3943{
3944 __be32 verf[2];
3945
3946 verf[0] = htonl((u32)clp->cl_boot_time.tv_sec);
3947 verf[1] = htonl((u32)clp->cl_boot_time.tv_nsec);
3948 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3949}
3950
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003951int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3952 unsigned short port, struct rpc_cred *cred,
3953 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954{
3955 nfs4_verifier sc_verifier;
3956 struct nfs4_setclientid setclientid = {
3957 .sc_verifier = &sc_verifier,
3958 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003959 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 };
3961 struct rpc_message msg = {
3962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3963 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003964 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003965 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 int loop = 0;
3968 int status;
3969
Chuck Levercd937102012-03-02 17:14:31 -05003970 nfs4_construct_boot_verifier(clp, &sc_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
3972 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05003973 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003975 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003976 clp->cl_ipaddr,
3977 rpc_peeraddr2str(clp->cl_rpcclient,
3978 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003979 rpc_peeraddr2str(clp->cl_rpcclient,
3980 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003981 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 clp->cl_id_uniquifier);
3983 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003984 sizeof(setclientid.sc_netid),
3985 rpc_peeraddr2str(clp->cl_rpcclient,
3986 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003988 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05003990 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003992 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 if (status != -NFS4ERR_CLID_INUSE)
3994 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003995 if (loop != 0) {
3996 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003998 }
3999 ++loop;
4000 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 }
4002 return status;
4003}
4004
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004005int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004006 struct nfs4_setclientid_res *arg,
4007 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008{
4009 struct nfs_fsinfo fsinfo;
4010 struct rpc_message msg = {
4011 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004012 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004014 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 };
4016 unsigned long now;
4017 int status;
4018
4019 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004020 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 if (status == 0) {
4022 spin_lock(&clp->cl_lock);
4023 clp->cl_lease_time = fsinfo.lease_time * HZ;
4024 clp->cl_last_renewal = now;
4025 spin_unlock(&clp->cl_lock);
4026 }
4027 return status;
4028}
4029
Trond Myklebustfe650402006-01-03 09:55:18 +01004030struct nfs4_delegreturndata {
4031 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004032 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004033 struct nfs_fh fh;
4034 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004035 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004036 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004037 int rpc_status;
4038};
4039
Trond Myklebustfe650402006-01-03 09:55:18 +01004040static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4041{
4042 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004043
Trond Myklebust14516c32010-07-31 14:29:06 -04004044 if (!nfs4_sequence_done(task, &data->res.seq_res))
4045 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004046
Ricardo Labiaga79708862009-12-07 09:23:21 -05004047 switch (task->tk_status) {
4048 case -NFS4ERR_STALE_STATEID:
4049 case -NFS4ERR_EXPIRED:
4050 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004051 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004052 break;
4053 default:
4054 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4055 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004056 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004057 return;
4058 }
4059 }
4060 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004061}
4062
4063static void nfs4_delegreturn_release(void *calldata)
4064{
Trond Myklebustfe650402006-01-03 09:55:18 +01004065 kfree(calldata);
4066}
4067
Andy Adamson938e1012009-04-01 09:22:28 -04004068#if defined(CONFIG_NFS_V4_1)
4069static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4070{
4071 struct nfs4_delegreturndata *d_data;
4072
4073 d_data = (struct nfs4_delegreturndata *)data;
4074
Trond Myklebust035168a2010-06-16 09:52:26 -04004075 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004076 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004077 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004078 return;
4079 rpc_call_start(task);
4080}
4081#endif /* CONFIG_NFS_V4_1 */
4082
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004083static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004084#if defined(CONFIG_NFS_V4_1)
4085 .rpc_call_prepare = nfs4_delegreturn_prepare,
4086#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004087 .rpc_call_done = nfs4_delegreturn_done,
4088 .rpc_release = nfs4_delegreturn_release,
4089};
4090
Trond Myklebuste6f81072008-01-24 18:14:34 -05004091static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004092{
4093 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004094 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004095 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004096 struct rpc_message msg = {
4097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4098 .rpc_cred = cred,
4099 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004100 struct rpc_task_setup task_setup_data = {
4101 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004102 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004103 .callback_ops = &nfs4_delegreturn_ops,
4104 .flags = RPC_TASK_ASYNC,
4105 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004106 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004107
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004108 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004109 if (data == NULL)
4110 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004111 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004112 data->args.fhandle = &data->fh;
4113 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004114 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004115 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004116 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004117 data->res.fattr = &data->fattr;
4118 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004119 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004120 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004121 data->rpc_status = 0;
4122
Trond Myklebustc970aa82007-07-14 15:39:59 -04004123 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004124 msg.rpc_argp = &data->args;
4125 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004126 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004127 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004128 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004129 if (!issync)
4130 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004131 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004132 if (status != 0)
4133 goto out;
4134 status = data->rpc_status;
4135 if (status != 0)
4136 goto out;
4137 nfs_refresh_inode(inode, &data->fattr);
4138out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004139 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004140 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141}
4142
Trond Myklebuste6f81072008-01-24 18:14:34 -05004143int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144{
4145 struct nfs_server *server = NFS_SERVER(inode);
4146 struct nfs4_exception exception = { };
4147 int err;
4148 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004149 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 switch (err) {
4151 case -NFS4ERR_STALE_STATEID:
4152 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 case 0:
4154 return 0;
4155 }
4156 err = nfs4_handle_exception(server, err, &exception);
4157 } while (exception.retry);
4158 return err;
4159}
4160
4161#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4162#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4163
4164/*
4165 * sleep, with exponential backoff, and retry the LOCK operation.
4166 */
4167static unsigned long
4168nfs4_set_lock_task_retry(unsigned long timeout)
4169{
Jeff Laytond3103102011-12-01 22:44:39 +01004170 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 timeout <<= 1;
4172 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4173 return NFS4_LOCK_MAXTIMEOUT;
4174 return timeout;
4175}
4176
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4178{
4179 struct inode *inode = state->inode;
4180 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004181 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004182 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004184 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004186 struct nfs_lockt_res res = {
4187 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 };
4189 struct rpc_message msg = {
4190 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4191 .rpc_argp = &arg,
4192 .rpc_resp = &res,
4193 .rpc_cred = state->owner->so_cred,
4194 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 struct nfs4_lock_state *lsp;
4196 int status;
4197
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004198 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004199 status = nfs4_set_lock_state(state, request);
4200 if (status != 0)
4201 goto out;
4202 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004203 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004204 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004205 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004206 switch (status) {
4207 case 0:
4208 request->fl_type = F_UNLCK;
4209 break;
4210 case -NFS4ERR_DENIED:
4211 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004213 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004214out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 return status;
4216}
4217
4218static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4219{
4220 struct nfs4_exception exception = { };
4221 int err;
4222
4223 do {
4224 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4225 _nfs4_proc_getlk(state, cmd, request),
4226 &exception);
4227 } while (exception.retry);
4228 return err;
4229}
4230
4231static int do_vfs_lock(struct file *file, struct file_lock *fl)
4232{
4233 int res = 0;
4234 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4235 case FL_POSIX:
4236 res = posix_lock_file_wait(file, fl);
4237 break;
4238 case FL_FLOCK:
4239 res = flock_lock_file_wait(file, fl);
4240 break;
4241 default:
4242 BUG();
4243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 return res;
4245}
4246
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004247struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004248 struct nfs_locku_args arg;
4249 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004250 struct nfs4_lock_state *lsp;
4251 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004252 struct file_lock fl;
4253 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004254 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004255};
4256
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004257static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4258 struct nfs_open_context *ctx,
4259 struct nfs4_lock_state *lsp,
4260 struct nfs_seqid *seqid)
4261{
4262 struct nfs4_unlockdata *p;
4263 struct inode *inode = lsp->ls_state->inode;
4264
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004265 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004266 if (p == NULL)
4267 return NULL;
4268 p->arg.fh = NFS_FH(inode);
4269 p->arg.fl = &p->fl;
4270 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004271 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004272 p->arg.stateid = &lsp->ls_stateid;
4273 p->lsp = lsp;
4274 atomic_inc(&lsp->ls_count);
4275 /* Ensure we don't close file until we're done freeing locks! */
4276 p->ctx = get_nfs_open_context(ctx);
4277 memcpy(&p->fl, fl, sizeof(p->fl));
4278 p->server = NFS_SERVER(inode);
4279 return p;
4280}
4281
Trond Myklebust06f814a2006-01-03 09:55:07 +01004282static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004283{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004284 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004285 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004286 nfs4_put_lock_state(calldata->lsp);
4287 put_nfs_open_context(calldata->ctx);
4288 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004289}
4290
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004291static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004292{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004293 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004294
Trond Myklebust14516c32010-07-31 14:29:06 -04004295 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4296 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004297 switch (task->tk_status) {
4298 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004299 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4300 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004301 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004302 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004303 case -NFS4ERR_BAD_STATEID:
4304 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004305 case -NFS4ERR_STALE_STATEID:
4306 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004307 break;
4308 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004309 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004310 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004311 }
Trond Myklebust8827f312012-10-29 18:53:23 -04004312 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004313}
4314
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004315static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004316{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004317 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004319 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004320 return;
4321 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004322 /* Note: exit _without_ running nfs4_locku_done */
4323 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004324 return;
4325 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004326 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004327 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004328 &calldata->arg.seq_args,
Trond Myklebust0ada2102012-10-29 18:37:40 -04004329 &calldata->res.seq_res,
4330 task) != 0)
4331 nfs_release_seqid(calldata->arg.seqid);
4332 else
4333 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334}
4335
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004336static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004337 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004338 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004339 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004340};
4341
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004342static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4343 struct nfs_open_context *ctx,
4344 struct nfs4_lock_state *lsp,
4345 struct nfs_seqid *seqid)
4346{
4347 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004348 struct rpc_message msg = {
4349 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4350 .rpc_cred = ctx->cred,
4351 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004352 struct rpc_task_setup task_setup_data = {
4353 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004354 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004355 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004356 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004357 .flags = RPC_TASK_ASYNC,
4358 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004359
Frank Filz137d6ac2007-07-09 15:32:29 -07004360 /* Ensure this is an unlock - when canceling a lock, the
4361 * canceled lock is passed in, and it won't be an unlock.
4362 */
4363 fl->fl_type = F_UNLCK;
4364
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004365 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4366 if (data == NULL) {
4367 nfs_free_seqid(seqid);
4368 return ERR_PTR(-ENOMEM);
4369 }
4370
Trond Myklebust9d12b212012-01-17 22:04:25 -05004371 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004372 msg.rpc_argp = &data->arg;
4373 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004374 task_setup_data.callback_data = data;
4375 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004376}
4377
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4379{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004380 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004381 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004382 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004383 struct rpc_task *task;
4384 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004385 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386
Trond Myklebust9b073572006-06-29 16:38:34 -04004387 status = nfs4_set_lock_state(state, request);
4388 /* Unlock _before_ we do the RPC call */
4389 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004390 down_read(&nfsi->rwsem);
4391 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4392 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004393 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004394 }
4395 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004396 if (status != 0)
4397 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004398 /* Is this a delegated lock? */
4399 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004400 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004401 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004402 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004403 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004404 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004405 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004406 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004407 status = PTR_ERR(task);
4408 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004409 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004410 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004411 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004412out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004413 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004414 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415}
4416
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004417struct nfs4_lockdata {
4418 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004419 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004420 struct nfs4_lock_state *lsp;
4421 struct nfs_open_context *ctx;
4422 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004423 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004424 int rpc_status;
4425 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004426 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004427};
4428
4429static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004430 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4431 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004432{
4433 struct nfs4_lockdata *p;
4434 struct inode *inode = lsp->ls_state->inode;
4435 struct nfs_server *server = NFS_SERVER(inode);
4436
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004437 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004438 if (p == NULL)
4439 return NULL;
4440
4441 p->arg.fh = NFS_FH(inode);
4442 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004443 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004444 if (p->arg.open_seqid == NULL)
4445 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004446 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004447 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004448 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004449 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004450 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004451 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004452 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004453 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004454 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004455 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004456 atomic_inc(&lsp->ls_count);
4457 p->ctx = get_nfs_open_context(ctx);
4458 memcpy(&p->fl, fl, sizeof(p->fl));
4459 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004460out_free_seqid:
4461 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004462out_free:
4463 kfree(p);
4464 return NULL;
4465}
4466
4467static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4468{
4469 struct nfs4_lockdata *data = calldata;
4470 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471
Harvey Harrison3110ff82008-05-02 13:42:44 -07004472 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004473 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4474 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004475 /* Do we need to do an open_to_lock_owner? */
4476 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004477 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust0ada2102012-10-29 18:37:40 -04004478 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004479 data->arg.open_stateid = &state->stateid;
4480 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004481 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004482 } else
4483 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004484 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004485 if (nfs4_setup_sequence(data->server,
4486 &data->arg.seq_args,
Trond Myklebust0ada2102012-10-29 18:37:40 -04004487 &data->res.seq_res,
4488 task) == 0) {
4489 rpc_call_start(task);
Andy Adamson66179ef2009-04-01 09:22:22 -04004490 return;
Trond Myklebust0ada2102012-10-29 18:37:40 -04004491 }
4492 nfs_release_seqid(data->arg.open_seqid);
4493out_release_lock_seqid:
4494 nfs_release_seqid(data->arg.lock_seqid);
4495 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004496}
4497
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004498static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4499{
4500 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4501 nfs4_lock_prepare(task, calldata);
4502}
4503
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004504static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4505{
4506 struct nfs4_lockdata *data = calldata;
4507
Harvey Harrison3110ff82008-05-02 13:42:44 -07004508 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004509
Trond Myklebust14516c32010-07-31 14:29:06 -04004510 if (!nfs4_sequence_done(task, &data->res.seq_res))
4511 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004512
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004513 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004514 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004515 if (data->rpc_status == 0)
4516 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4517 else
4518 goto out;
4519 }
4520 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004521 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004522 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004523 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004524 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004525out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004526 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004527}
4528
4529static void nfs4_lock_release(void *calldata)
4530{
4531 struct nfs4_lockdata *data = calldata;
4532
Harvey Harrison3110ff82008-05-02 13:42:44 -07004533 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004534 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004535 if (data->cancelled != 0) {
4536 struct rpc_task *task;
4537 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4538 data->arg.lock_seqid);
4539 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004540 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004541 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004542 } else
4543 nfs_free_seqid(data->arg.lock_seqid);
4544 nfs4_put_lock_state(data->lsp);
4545 put_nfs_open_context(data->ctx);
4546 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004547 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004548}
4549
4550static const struct rpc_call_ops nfs4_lock_ops = {
4551 .rpc_call_prepare = nfs4_lock_prepare,
4552 .rpc_call_done = nfs4_lock_done,
4553 .rpc_release = nfs4_lock_release,
4554};
4555
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004556static const struct rpc_call_ops nfs4_recover_lock_ops = {
4557 .rpc_call_prepare = nfs4_recover_lock_prepare,
4558 .rpc_call_done = nfs4_lock_done,
4559 .rpc_release = nfs4_lock_release,
4560};
4561
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004562static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4563{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004564 switch (error) {
4565 case -NFS4ERR_ADMIN_REVOKED:
4566 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004567 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004568 if (new_lock_owner != 0 ||
4569 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004570 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004571 break;
4572 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004573 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004574 case -NFS4ERR_EXPIRED:
4575 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004576 };
4577}
4578
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004579static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004580{
4581 struct nfs4_lockdata *data;
4582 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004583 struct rpc_message msg = {
4584 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4585 .rpc_cred = state->owner->so_cred,
4586 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004587 struct rpc_task_setup task_setup_data = {
4588 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004589 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004590 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004591 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004592 .flags = RPC_TASK_ASYNC,
4593 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004594 int ret;
4595
Harvey Harrison3110ff82008-05-02 13:42:44 -07004596 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004597 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004598 fl->fl_u.nfs4_fl.owner,
4599 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004600 if (data == NULL)
4601 return -ENOMEM;
4602 if (IS_SETLKW(cmd))
4603 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004604 if (recovery_type > NFS_LOCK_NEW) {
4605 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004606 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004607 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4608 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004609 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004610 msg.rpc_argp = &data->arg;
4611 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004612 task_setup_data.callback_data = data;
4613 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004614 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004615 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004616 ret = nfs4_wait_for_completion_rpc_task(task);
4617 if (ret == 0) {
4618 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004619 if (ret)
4620 nfs4_handle_setlk_error(data->server, data->lsp,
4621 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004622 } else
4623 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004624 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004625 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004626 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627}
4628
4629static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4630{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004631 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004632 struct nfs4_exception exception = {
4633 .inode = state->inode,
4634 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004635 int err;
4636
4637 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004638 /* Cache the lock if possible... */
4639 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4640 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004641 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004642 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004643 break;
4644 nfs4_handle_exception(server, err, &exception);
4645 } while (exception.retry);
4646 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647}
4648
4649static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4650{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004651 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004652 struct nfs4_exception exception = {
4653 .inode = state->inode,
4654 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004655 int err;
4656
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004657 err = nfs4_set_lock_state(state, request);
4658 if (err != 0)
4659 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004660 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004661 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4662 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004663 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004664 switch (err) {
4665 default:
4666 goto out;
4667 case -NFS4ERR_GRACE:
4668 case -NFS4ERR_DELAY:
4669 nfs4_handle_exception(server, err, &exception);
4670 err = 0;
4671 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004672 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004673out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004674 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675}
4676
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004677#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004678static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004679{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004680 int status, ret = NFS_OK;
4681 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004682 struct nfs_server *server = NFS_SERVER(state->inode);
4683
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004684 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4685 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4686 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4687 if (status != NFS_OK) {
4688 nfs41_free_stateid(server, &lsp->ls_stateid);
4689 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4690 ret = status;
4691 }
4692 }
4693 };
4694
4695 return ret;
4696}
4697
4698static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4699{
4700 int status = NFS_OK;
4701
4702 if (test_bit(LK_STATE_IN_USE, &state->flags))
4703 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004704 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004705 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004706 return nfs4_lock_expired(state, request);
4707}
4708#endif
4709
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4711{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004712 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004713 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004714 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004716 if ((fl_flags & FL_POSIX) &&
4717 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4718 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004719 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004720 status = nfs4_set_lock_state(state, request);
4721 if (status != 0)
4722 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004723 request->fl_flags |= FL_ACCESS;
4724 status = do_vfs_lock(request->fl_file, request);
4725 if (status < 0)
4726 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004727 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004728 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004729 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004730 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004731 request->fl_flags = fl_flags & ~FL_SLEEP;
4732 status = do_vfs_lock(request->fl_file, request);
4733 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004734 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004735 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004736 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004737 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004738 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004739 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004740 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004741 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4742 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004743out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004744 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004745out:
4746 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 return status;
4748}
4749
4750static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4751{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004752 struct nfs4_exception exception = {
4753 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004754 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004755 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 int err;
4757
4758 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004759 err = _nfs4_proc_setlk(state, cmd, request);
4760 if (err == -NFS4ERR_DENIED)
4761 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004763 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 } while (exception.retry);
4765 return err;
4766}
4767
4768static int
4769nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4770{
4771 struct nfs_open_context *ctx;
4772 struct nfs4_state *state;
4773 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4774 int status;
4775
4776 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004777 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 state = ctx->state;
4779
4780 if (request->fl_start < 0 || request->fl_end < 0)
4781 return -EINVAL;
4782
Trond Myklebustd9531262009-07-21 19:22:38 -04004783 if (IS_GETLK(cmd)) {
4784 if (state != NULL)
4785 return nfs4_proc_getlk(state, F_GETLK, request);
4786 return 0;
4787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788
4789 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4790 return -EINVAL;
4791
Trond Myklebustd9531262009-07-21 19:22:38 -04004792 if (request->fl_type == F_UNLCK) {
4793 if (state != NULL)
4794 return nfs4_proc_unlck(state, cmd, request);
4795 return 0;
4796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797
Trond Myklebustd9531262009-07-21 19:22:38 -04004798 if (state == NULL)
4799 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004800 /*
4801 * Don't rely on the VFS having checked the file open mode,
4802 * since it won't do this for flock() locks.
4803 */
4804 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4805 case F_RDLCK:
4806 if (!(filp->f_mode & FMODE_READ))
4807 return -EBADF;
4808 break;
4809 case F_WRLCK:
4810 if (!(filp->f_mode & FMODE_WRITE))
4811 return -EBADF;
4812 }
4813
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 do {
4815 status = nfs4_proc_setlk(state, cmd, request);
4816 if ((status != -EAGAIN) || IS_SETLK(cmd))
4817 break;
4818 timeout = nfs4_set_lock_task_retry(timeout);
4819 status = -ERESTARTSYS;
4820 if (signalled())
4821 break;
4822 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 return status;
4824}
4825
Trond Myklebust888e6942005-11-04 15:38:11 -05004826int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4827{
4828 struct nfs_server *server = NFS_SERVER(state->inode);
4829 struct nfs4_exception exception = { };
4830 int err;
4831
4832 err = nfs4_set_lock_state(state, fl);
4833 if (err != 0)
4834 goto out;
4835 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004836 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004837 switch (err) {
4838 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004839 printk(KERN_ERR "NFS: %s: unhandled error "
4840 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004841 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004842 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004843 goto out;
4844 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004845 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004846 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004847 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004848 nfs4_schedule_lease_recovery(server->nfs_client);
4849 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004850 case -NFS4ERR_BADSESSION:
4851 case -NFS4ERR_BADSLOT:
4852 case -NFS4ERR_BAD_HIGH_SLOT:
4853 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4854 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004855 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004856 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004857 case -ERESTARTSYS:
4858 /*
4859 * The show must go on: exit, but mark the
4860 * stateid as needing recovery.
4861 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004862 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004863 case -NFS4ERR_ADMIN_REVOKED:
4864 case -NFS4ERR_BAD_STATEID:
4865 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004866 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004867 err = 0;
4868 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004869 case -EKEYEXPIRED:
4870 /*
4871 * User RPCSEC_GSS context has expired.
4872 * We cannot recover this stateid now, so
4873 * skip it and allow recovery thread to
4874 * proceed.
4875 */
4876 err = 0;
4877 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004878 case -ENOMEM:
4879 case -NFS4ERR_DENIED:
4880 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4881 err = 0;
4882 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004883 case -NFS4ERR_DELAY:
4884 break;
4885 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004886 err = nfs4_handle_exception(server, err, &exception);
4887 } while (exception.retry);
4888out:
4889 return err;
4890}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004891
Trond Myklebustcf470c32012-03-07 13:49:12 -05004892struct nfs_release_lockowner_data {
4893 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004894 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004895 struct nfs_release_lockowner_args args;
4896};
4897
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004898static void nfs4_release_lockowner_release(void *calldata)
4899{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004900 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004901 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004902 kfree(calldata);
4903}
4904
Trond Myklebust17280172012-03-11 13:11:00 -04004905static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004906 .rpc_release = nfs4_release_lockowner_release,
4907};
4908
Trond Myklebustcf470c32012-03-07 13:49:12 -05004909int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004910{
4911 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004912 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004913 struct rpc_message msg = {
4914 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4915 };
4916
4917 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004918 return -EINVAL;
4919 data = kmalloc(sizeof(*data), GFP_NOFS);
4920 if (!data)
4921 return -ENOMEM;
4922 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004923 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004924 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4925 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4926 data->args.lock_owner.s_dev = server->s_dev;
4927 msg.rpc_argp = &data->args;
4928 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4929 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004930}
4931
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004932#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4933
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004934static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4935 const void *buf, size_t buflen,
4936 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004937{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004938 if (strcmp(key, "") != 0)
4939 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004940
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004941 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004942}
4943
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004944static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4945 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004946{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004947 if (strcmp(key, "") != 0)
4948 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004949
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004950 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004951}
4952
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004953static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4954 size_t list_len, const char *name,
4955 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004956{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004957 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004958
J. Bruce Fields096455a2006-03-20 23:23:42 -05004959 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4960 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004961
4962 if (list && len <= list_len)
4963 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004964 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004965}
4966
Andy Adamson533eb462011-06-13 18:25:56 -04004967/*
4968 * nfs_fhget will use either the mounted_on_fileid or the fileid
4969 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004970static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4971{
Andy Adamson533eb462011-06-13 18:25:56 -04004972 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4973 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4974 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05004975 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004976 return;
4977
4978 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05004979 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004980 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4981 fattr->nlink = 2;
4982}
4983
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004984static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
4985 const struct qstr *name,
4986 struct nfs4_fs_locations *fs_locations,
4987 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004988{
4989 struct nfs_server *server = NFS_SERVER(dir);
4990 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004991 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004992 };
4993 struct nfs4_fs_locations_arg args = {
4994 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004995 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004996 .page = page,
4997 .bitmask = bitmask,
4998 };
Benny Halevy22958462009-04-01 09:22:02 -04004999 struct nfs4_fs_locations_res res = {
5000 .fs_locations = fs_locations,
5001 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005002 struct rpc_message msg = {
5003 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5004 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005005 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005006 };
5007 int status;
5008
Harvey Harrison3110ff82008-05-02 13:42:44 -07005009 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005010
5011 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5012 * is not supported */
5013 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5014 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5015 else
5016 bitmask[0] |= FATTR4_WORD0_FILEID;
5017
Trond Myklebustc228fd32007-01-13 02:28:11 -05005018 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005019 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005020 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005021 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005022 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005023 return status;
5024}
5025
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005026int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5027 const struct qstr *name,
5028 struct nfs4_fs_locations *fs_locations,
5029 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005030{
5031 struct nfs4_exception exception = { };
5032 int err;
5033 do {
5034 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005035 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005036 &exception);
5037 } while (exception.retry);
5038 return err;
5039}
5040
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005041static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5042{
5043 int status;
5044 struct nfs4_secinfo_arg args = {
5045 .dir_fh = NFS_FH(dir),
5046 .name = name,
5047 };
5048 struct nfs4_secinfo_res res = {
5049 .flavors = flavors,
5050 };
5051 struct rpc_message msg = {
5052 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5053 .rpc_argp = &args,
5054 .rpc_resp = &res,
5055 };
5056
5057 dprintk("NFS call secinfo %s\n", name->name);
5058 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5059 dprintk("NFS reply secinfo: %d\n", status);
5060 return status;
5061}
5062
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005063int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5064 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005065{
5066 struct nfs4_exception exception = { };
5067 int err;
5068 do {
5069 err = nfs4_handle_exception(NFS_SERVER(dir),
5070 _nfs4_proc_secinfo(dir, name, flavors),
5071 &exception);
5072 } while (exception.retry);
5073 return err;
5074}
5075
Andy Adamson557134a2009-04-01 09:21:53 -04005076#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005077/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005078 * Check the exchange flags returned by the server for invalid flags, having
5079 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5080 * DS flags set.
5081 */
5082static int nfs4_check_cl_exchange_flags(u32 flags)
5083{
5084 if (flags & ~EXCHGID4_FLAG_MASK_R)
5085 goto out_inval;
5086 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5087 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5088 goto out_inval;
5089 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5090 goto out_inval;
5091 return NFS_OK;
5092out_inval:
5093 return -NFS4ERR_INVAL;
5094}
5095
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005096static bool
5097nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
5098{
5099 if (a->server_scope_sz == b->server_scope_sz &&
5100 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5101 return true;
5102
5103 return false;
5104}
5105
Andy Adamson357f54d2010-12-14 10:11:57 -05005106/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005107 * nfs4_proc_exchange_id()
5108 *
5109 * Since the clientid has expired, all compounds using sessions
5110 * associated with the stale clientid will be returning
5111 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5112 * be in some phase of session reset.
5113 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005114int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005115{
5116 nfs4_verifier verifier;
5117 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005118 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005119 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005120 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005121 };
5122 struct nfs41_exchange_id_res res = {
5123 .client = clp,
5124 };
5125 int status;
5126 struct rpc_message msg = {
5127 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5128 .rpc_argp = &args,
5129 .rpc_resp = &res,
5130 .rpc_cred = cred,
5131 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005132
5133 dprintk("--> %s\n", __func__);
5134 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04005135
Chuck Levercd937102012-03-02 17:14:31 -05005136 nfs4_construct_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005137
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005138 args.id_len = scnprintf(args.id, sizeof(args.id),
Trond Myklebust3617e502012-04-30 12:04:58 -04005139 "%s/%s/%u",
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005140 clp->cl_ipaddr,
Trond Myklebust3617e502012-04-30 12:04:58 -04005141 clp->cl_rpcclient->cl_nodename,
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005142 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005143
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005144 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005145 if (unlikely(!res.server_scope)) {
5146 status = -ENOMEM;
5147 goto out;
5148 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005149
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005150 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
5151 if (unlikely(!res.impl_id)) {
5152 status = -ENOMEM;
5153 goto out_server_scope;
5154 }
5155
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005156 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005157 if (!status)
5158 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005159
5160 if (!status) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005161 /* use the most recent implementation id */
5162 kfree(clp->impl_id);
5163 clp->impl_id = res.impl_id;
5164 } else
5165 kfree(res.impl_id);
5166
5167 if (!status) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005168 if (clp->server_scope &&
5169 !nfs41_same_server_scope(clp->server_scope,
5170 res.server_scope)) {
5171 dprintk("%s: server_scope mismatch detected\n",
5172 __func__);
5173 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5174 kfree(clp->server_scope);
5175 clp->server_scope = NULL;
5176 }
5177
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005178 if (!clp->server_scope) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005179 clp->server_scope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005180 goto out;
5181 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005182 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005183
5184out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005185 kfree(res.server_scope);
5186out:
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005187 if (clp->impl_id)
5188 dprintk("%s: Server Implementation ID: "
5189 "domain: %s, name: %s, date: %llu,%u\n",
5190 __func__, clp->impl_id->domain, clp->impl_id->name,
5191 clp->impl_id->date.seconds,
5192 clp->impl_id->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005193 dprintk("<-- %s status= %d\n", __func__, status);
5194 return status;
5195}
5196
Andy Adamson2050f0c2009-04-01 09:22:30 -04005197struct nfs4_get_lease_time_data {
5198 struct nfs4_get_lease_time_args *args;
5199 struct nfs4_get_lease_time_res *res;
5200 struct nfs_client *clp;
5201};
5202
5203static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5204 void *calldata)
5205{
5206 int ret;
5207 struct nfs4_get_lease_time_data *data =
5208 (struct nfs4_get_lease_time_data *)calldata;
5209
5210 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005211 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005212 /* just setup sequence, do not trigger session recovery
5213 since we're invoked within one */
5214 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005215 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005216 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005217
5218 BUG_ON(ret == -EAGAIN);
5219 rpc_call_start(task);
5220 dprintk("<-- %s\n", __func__);
5221}
5222
5223/*
5224 * Called from nfs4_state_manager thread for session setup, so don't recover
5225 * from sequence operation or clientid errors.
5226 */
5227static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5228{
5229 struct nfs4_get_lease_time_data *data =
5230 (struct nfs4_get_lease_time_data *)calldata;
5231
5232 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005233 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5234 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005235 switch (task->tk_status) {
5236 case -NFS4ERR_DELAY:
5237 case -NFS4ERR_GRACE:
5238 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5239 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5240 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005241 /* fall through */
5242 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005243 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005244 return;
5245 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005246 dprintk("<-- %s\n", __func__);
5247}
5248
Trond Myklebust17280172012-03-11 13:11:00 -04005249static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005250 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5251 .rpc_call_done = nfs4_get_lease_time_done,
5252};
5253
5254int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5255{
5256 struct rpc_task *task;
5257 struct nfs4_get_lease_time_args args;
5258 struct nfs4_get_lease_time_res res = {
5259 .lr_fsinfo = fsinfo,
5260 };
5261 struct nfs4_get_lease_time_data data = {
5262 .args = &args,
5263 .res = &res,
5264 .clp = clp,
5265 };
5266 struct rpc_message msg = {
5267 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5268 .rpc_argp = &args,
5269 .rpc_resp = &res,
5270 };
5271 struct rpc_task_setup task_setup = {
5272 .rpc_client = clp->cl_rpcclient,
5273 .rpc_message = &msg,
5274 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005275 .callback_data = &data,
5276 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005277 };
5278 int status;
5279
Trond Myklebust9d12b212012-01-17 22:04:25 -05005280 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005281 dprintk("--> %s\n", __func__);
5282 task = rpc_run_task(&task_setup);
5283
5284 if (IS_ERR(task))
5285 status = PTR_ERR(task);
5286 else {
5287 status = task->tk_status;
5288 rpc_put_task(task);
5289 }
5290 dprintk("<-- %s return %d\n", __func__, status);
5291
5292 return status;
5293}
5294
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005295static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5296{
5297 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5298}
5299
5300static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5301 struct nfs4_slot *new,
5302 u32 max_slots,
5303 u32 ivalue)
5304{
5305 struct nfs4_slot *old = NULL;
5306 u32 i;
5307
5308 spin_lock(&tbl->slot_tbl_lock);
5309 if (new) {
5310 old = tbl->slots;
5311 tbl->slots = new;
5312 tbl->max_slots = max_slots;
5313 }
5314 tbl->highest_used_slotid = -1; /* no slot is currently used */
5315 for (i = 0; i < tbl->max_slots; i++)
5316 tbl->slots[i].seq_nr = ivalue;
5317 spin_unlock(&tbl->slot_tbl_lock);
5318 kfree(old);
5319}
5320
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005321/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005322 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005323 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005324static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5325 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005326{
Andy Adamson104aeba2010-01-14 17:45:10 -05005327 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005328 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005329
Andy Adamson104aeba2010-01-14 17:45:10 -05005330 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5331 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005332
Andy Adamson104aeba2010-01-14 17:45:10 -05005333 /* Does the newly negotiated max_reqs match the existing slot table? */
5334 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005335 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005336 if (!new)
5337 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005338 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005339 ret = 0;
5340
5341 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005342 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5343 tbl, tbl->slots, tbl->max_slots);
5344out:
5345 dprintk("<-- %s: return %d\n", __func__, ret);
5346 return ret;
5347}
5348
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005349/* Destroy the slot table */
5350static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5351{
5352 if (session->fc_slot_table.slots != NULL) {
5353 kfree(session->fc_slot_table.slots);
5354 session->fc_slot_table.slots = NULL;
5355 }
5356 if (session->bc_slot_table.slots != NULL) {
5357 kfree(session->bc_slot_table.slots);
5358 session->bc_slot_table.slots = NULL;
5359 }
5360 return;
5361}
5362
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005363/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005364 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005365 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005366static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005367{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005368 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005369 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005370
Andy Adamsonaacd5532011-11-09 13:58:21 -05005371 dprintk("--> %s\n", __func__);
5372 /* Fore channel */
5373 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005374 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5375 if (status) /* -ENOMEM */
5376 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005377 /* Back channel */
5378 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005379 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5380 if (status && tbl->slots == NULL)
5381 /* Fore and back channel share a connection so get
5382 * both slot tables or neither */
5383 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005384 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005385}
5386
5387struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5388{
5389 struct nfs4_session *session;
5390 struct nfs4_slot_table *tbl;
5391
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005392 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005393 if (!session)
5394 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005395
Andy Adamson557134a2009-04-01 09:21:53 -04005396 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005397 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005398 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005399 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005400 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005401
5402 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005403 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005404 spin_lock_init(&tbl->slot_tbl_lock);
5405 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005406 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005407
Trond Myklebust1055d762010-06-16 09:52:27 -04005408 session->session_state = 1<<NFS4_SESSION_INITING;
5409
Andy Adamson557134a2009-04-01 09:21:53 -04005410 session->clp = clp;
5411 return session;
5412}
5413
5414void nfs4_destroy_session(struct nfs4_session *session)
5415{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005416 struct rpc_xprt *xprt;
5417
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005418 nfs4_proc_destroy_session(session);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005419
5420 rcu_read_lock();
5421 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5422 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005423 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005424 __func__, xprt);
5425 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005426 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005427 kfree(session);
5428}
5429
Andy Adamsonfc931582009-04-01 09:22:31 -04005430/*
5431 * Initialize the values to be used by the client in CREATE_SESSION
5432 * If nfs4_init_session set the fore channel request and response sizes,
5433 * use them.
5434 *
5435 * Set the back channel max_resp_sz_cached to zero to force the client to
5436 * always set csa_cachethis to FALSE because the current implementation
5437 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5438 */
5439static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5440{
5441 struct nfs4_session *session = args->client->cl_session;
5442 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5443 mxresp_sz = session->fc_attrs.max_resp_sz;
5444
5445 if (mxrqst_sz == 0)
5446 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5447 if (mxresp_sz == 0)
5448 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5449 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005450 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5451 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005452 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005453 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005454
5455 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005456 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005457 __func__,
5458 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005459 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005460
5461 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005462 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5463 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5464 args->bc_attrs.max_resp_sz_cached = 0;
5465 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5466 args->bc_attrs.max_reqs = 1;
5467
5468 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5469 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5470 __func__,
5471 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5472 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5473 args->bc_attrs.max_reqs);
5474}
5475
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005476static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005477{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005478 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5479 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5480
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005481 if (rcvd->max_resp_sz > sent->max_resp_sz)
5482 return -EINVAL;
5483 /*
5484 * Our requested max_ops is the minimum we need; we're not
5485 * prepared to break up compounds into smaller pieces than that.
5486 * So, no point even trying to continue if the server won't
5487 * cooperate:
5488 */
5489 if (rcvd->max_ops < sent->max_ops)
5490 return -EINVAL;
5491 if (rcvd->max_reqs == 0)
5492 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005493 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5494 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005495 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005496}
5497
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005498static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5499{
5500 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5501 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005502
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005503 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5504 return -EINVAL;
5505 if (rcvd->max_resp_sz < sent->max_resp_sz)
5506 return -EINVAL;
5507 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5508 return -EINVAL;
5509 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005510 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005511 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005512 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005513 return -EINVAL;
5514 return 0;
5515}
Andy Adamson8d353012009-04-01 09:22:32 -04005516
Andy Adamson8d353012009-04-01 09:22:32 -04005517static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5518 struct nfs4_session *session)
5519{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005520 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005521
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005522 ret = nfs4_verify_fore_channel_attrs(args, session);
5523 if (ret)
5524 return ret;
5525 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005526}
5527
Andy Adamsonfc931582009-04-01 09:22:31 -04005528static int _nfs4_proc_create_session(struct nfs_client *clp)
5529{
5530 struct nfs4_session *session = clp->cl_session;
5531 struct nfs41_create_session_args args = {
5532 .client = clp,
5533 .cb_program = NFS4_CALLBACK,
5534 };
5535 struct nfs41_create_session_res res = {
5536 .client = clp,
5537 };
5538 struct rpc_message msg = {
5539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5540 .rpc_argp = &args,
5541 .rpc_resp = &res,
5542 };
5543 int status;
5544
5545 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005546 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005547
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005548 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005549
Andy Adamson8d353012009-04-01 09:22:32 -04005550 if (!status)
5551 /* Verify the session's negotiated channel_attrs values */
5552 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005553 if (!status) {
5554 /* Increment the clientid slot sequence id */
5555 clp->cl_seqid++;
5556 }
5557
5558 return status;
5559}
5560
5561/*
5562 * Issues a CREATE_SESSION operation to the server.
5563 * It is the responsibility of the caller to verify the session is
5564 * expired before calling this routine.
5565 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005566int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005567{
5568 int status;
5569 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005570 struct nfs4_session *session = clp->cl_session;
5571
5572 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5573
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005574 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005575 if (status)
5576 goto out;
5577
Andy Adamsonaacd5532011-11-09 13:58:21 -05005578 /* Init or reset the session slot tables */
5579 status = nfs4_setup_session_slot_tables(session);
5580 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005581 if (status)
5582 goto out;
5583
5584 ptr = (unsigned *)&session->sess_id.data[0];
5585 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5586 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005587out:
5588 dprintk("<-- %s\n", __func__);
5589 return status;
5590}
5591
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005592/*
5593 * Issue the over-the-wire RPC DESTROY_SESSION.
5594 * The caller must serialize access to this routine.
5595 */
5596int nfs4_proc_destroy_session(struct nfs4_session *session)
5597{
5598 int status = 0;
5599 struct rpc_message msg;
5600
5601 dprintk("--> nfs4_proc_destroy_session\n");
5602
5603 /* session is still being setup */
5604 if (session->clp->cl_cons_state != NFS_CS_READY)
5605 return status;
5606
5607 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5608 msg.rpc_argp = session;
5609 msg.rpc_resp = NULL;
5610 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005611 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005612
5613 if (status)
5614 printk(KERN_WARNING
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -05005615 "NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005616 "Session has been destroyed regardless...\n", status);
5617
5618 dprintk("<-- nfs4_proc_destroy_session\n");
5619 return status;
5620}
5621
Trond Myklebustfccba802009-07-21 16:48:07 -04005622int nfs4_init_session(struct nfs_server *server)
5623{
5624 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005625 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005626 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005627 int ret;
5628
5629 if (!nfs4_has_session(clp))
5630 return 0;
5631
Trond Myklebust1055d762010-06-16 09:52:27 -04005632 session = clp->cl_session;
5633 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5634 return 0;
5635
Andy Adamson68bf05e2009-12-15 12:55:02 -05005636 rsize = server->rsize;
5637 if (rsize == 0)
5638 rsize = NFS_MAX_FILE_IO_SIZE;
5639 wsize = server->wsize;
5640 if (wsize == 0)
5641 wsize = NFS_MAX_FILE_IO_SIZE;
5642
Andy Adamson68bf05e2009-12-15 12:55:02 -05005643 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5644 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005645
Trond Myklebustfccba802009-07-21 16:48:07 -04005646 ret = nfs4_recover_expired_lease(server);
5647 if (!ret)
5648 ret = nfs4_check_client_ready(clp);
5649 return ret;
5650}
5651
Andy Adamsond83217c2011-03-01 01:34:17 +00005652int nfs4_init_ds_session(struct nfs_client *clp)
5653{
5654 struct nfs4_session *session = clp->cl_session;
5655 int ret;
5656
5657 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5658 return 0;
5659
5660 ret = nfs4_client_recover_expired_lease(clp);
5661 if (!ret)
5662 /* Test for the DS role */
5663 if (!is_ds_client(clp))
5664 ret = -ENODEV;
5665 if (!ret)
5666 ret = nfs4_check_client_ready(clp);
5667 return ret;
5668
5669}
5670EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5671
5672
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005673/*
5674 * Renew the cl_session lease.
5675 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005676struct nfs4_sequence_data {
5677 struct nfs_client *clp;
5678 struct nfs4_sequence_args args;
5679 struct nfs4_sequence_res res;
5680};
5681
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005682static void nfs41_sequence_release(void *data)
5683{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005684 struct nfs4_sequence_data *calldata = data;
5685 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005686
Alexandros Batsakis71358402010-02-05 03:45:05 -08005687 if (atomic_read(&clp->cl_count) > 1)
5688 nfs4_schedule_state_renewal(clp);
5689 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005690 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005691}
5692
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005693static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5694{
5695 switch(task->tk_status) {
5696 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005697 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5698 return -EAGAIN;
5699 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005700 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005701 }
5702 return 0;
5703}
5704
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005705static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005706{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005707 struct nfs4_sequence_data *calldata = data;
5708 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005709
Trond Myklebust14516c32010-07-31 14:29:06 -04005710 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5711 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005712
5713 if (task->tk_status < 0) {
5714 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005715 if (atomic_read(&clp->cl_count) == 1)
5716 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005717
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005718 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5719 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005720 return;
5721 }
5722 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005723 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005724out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005725 dprintk("<-- %s\n", __func__);
5726}
5727
5728static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5729{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005730 struct nfs4_sequence_data *calldata = data;
5731 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005732 struct nfs4_sequence_args *args;
5733 struct nfs4_sequence_res *res;
5734
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005735 args = task->tk_msg.rpc_argp;
5736 res = task->tk_msg.rpc_resp;
5737
Trond Myklebust9d12b212012-01-17 22:04:25 -05005738 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005739 return;
5740 rpc_call_start(task);
5741}
5742
5743static const struct rpc_call_ops nfs41_sequence_ops = {
5744 .rpc_call_done = nfs41_sequence_call_done,
5745 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005746 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005747};
5748
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005749static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005750{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005751 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005752 struct rpc_message msg = {
5753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5754 .rpc_cred = cred,
5755 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005756 struct rpc_task_setup task_setup_data = {
5757 .rpc_client = clp->cl_rpcclient,
5758 .rpc_message = &msg,
5759 .callback_ops = &nfs41_sequence_ops,
5760 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5761 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005762
Alexandros Batsakis71358402010-02-05 03:45:05 -08005763 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005764 return ERR_PTR(-EIO);
Benny Halevydfb4f302010-09-24 09:17:01 -04005765 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005766 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005767 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005768 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005769 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005770 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005771 msg.rpc_argp = &calldata->args;
5772 msg.rpc_resp = &calldata->res;
5773 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005774 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005775
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005776 return rpc_run_task(&task_setup_data);
5777}
5778
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005779static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005780{
5781 struct rpc_task *task;
5782 int ret = 0;
5783
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005784 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5785 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005786 task = _nfs41_proc_sequence(clp, cred);
5787 if (IS_ERR(task))
5788 ret = PTR_ERR(task);
5789 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005790 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005791 dprintk("<-- %s status=%d\n", __func__, ret);
5792 return ret;
5793}
5794
5795static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5796{
5797 struct rpc_task *task;
5798 int ret;
5799
5800 task = _nfs41_proc_sequence(clp, cred);
5801 if (IS_ERR(task)) {
5802 ret = PTR_ERR(task);
5803 goto out;
5804 }
5805 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005806 if (!ret) {
5807 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5808
5809 if (task->tk_status == 0)
5810 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005811 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005812 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005813 rpc_put_task(task);
5814out:
5815 dprintk("<-- %s status=%d\n", __func__, ret);
5816 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005817}
5818
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005819struct nfs4_reclaim_complete_data {
5820 struct nfs_client *clp;
5821 struct nfs41_reclaim_complete_args arg;
5822 struct nfs41_reclaim_complete_res res;
5823};
5824
5825static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5826{
5827 struct nfs4_reclaim_complete_data *calldata = data;
5828
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005829 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005830 if (nfs41_setup_sequence(calldata->clp->cl_session,
5831 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005832 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005833 return;
5834
5835 rpc_call_start(task);
5836}
5837
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005838static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5839{
5840 switch(task->tk_status) {
5841 case 0:
5842 case -NFS4ERR_COMPLETE_ALREADY:
5843 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5844 break;
5845 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005846 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005847 /* fall through */
5848 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005849 return -EAGAIN;
5850 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005851 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005852 }
5853 return 0;
5854}
5855
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005856static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5857{
5858 struct nfs4_reclaim_complete_data *calldata = data;
5859 struct nfs_client *clp = calldata->clp;
5860 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5861
5862 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005863 if (!nfs41_sequence_done(task, res))
5864 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005865
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005866 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5867 rpc_restart_call_prepare(task);
5868 return;
5869 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005870 dprintk("<-- %s\n", __func__);
5871}
5872
5873static void nfs4_free_reclaim_complete_data(void *data)
5874{
5875 struct nfs4_reclaim_complete_data *calldata = data;
5876
5877 kfree(calldata);
5878}
5879
5880static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5881 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5882 .rpc_call_done = nfs4_reclaim_complete_done,
5883 .rpc_release = nfs4_free_reclaim_complete_data,
5884};
5885
5886/*
5887 * Issue a global reclaim complete.
5888 */
5889static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5890{
5891 struct nfs4_reclaim_complete_data *calldata;
5892 struct rpc_task *task;
5893 struct rpc_message msg = {
5894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5895 };
5896 struct rpc_task_setup task_setup_data = {
5897 .rpc_client = clp->cl_rpcclient,
5898 .rpc_message = &msg,
5899 .callback_ops = &nfs4_reclaim_complete_call_ops,
5900 .flags = RPC_TASK_ASYNC,
5901 };
5902 int status = -ENOMEM;
5903
5904 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005905 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005906 if (calldata == NULL)
5907 goto out;
5908 calldata->clp = clp;
5909 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005910
Trond Myklebust9d12b212012-01-17 22:04:25 -05005911 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005912 msg.rpc_argp = &calldata->arg;
5913 msg.rpc_resp = &calldata->res;
5914 task_setup_data.callback_data = calldata;
5915 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005916 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005917 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005918 goto out;
5919 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005920 status = nfs4_wait_for_completion_rpc_task(task);
5921 if (status == 0)
5922 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005923 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005924 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005925out:
5926 dprintk("<-- %s status=%d\n", __func__, status);
5927 return status;
5928}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005929
5930static void
5931nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5932{
5933 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005934 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005935
5936 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005937 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5938 * right now covering the LAYOUTGET we are about to send.
5939 * However, that is not so catastrophic, and there seems
5940 * to be no way to prevent it completely.
5941 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005942 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005943 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005944 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005945 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5946 NFS_I(lgp->args.inode)->layout,
5947 lgp->args.ctx->state)) {
5948 rpc_exit(task, NFS4_OK);
5949 return;
5950 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005951 rpc_call_start(task);
5952}
5953
5954static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5955{
5956 struct nfs4_layoutget *lgp = calldata;
5957 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5958
5959 dprintk("--> %s\n", __func__);
5960
5961 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5962 return;
5963
5964 switch (task->tk_status) {
5965 case 0:
5966 break;
5967 case -NFS4ERR_LAYOUTTRYLATER:
5968 case -NFS4ERR_RECALLCONFLICT:
5969 task->tk_status = -NFS4ERR_DELAY;
5970 /* Fall through */
5971 default:
5972 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5973 rpc_restart_call_prepare(task);
5974 return;
5975 }
5976 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005977 dprintk("<-- %s\n", __func__);
5978}
5979
Idan Kedarc2bf3222012-08-02 11:47:10 +03005980static size_t max_response_pages(struct nfs_server *server)
5981{
5982 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
5983 return nfs_page_array_len(0, max_resp_sz);
5984}
5985
5986static void nfs4_free_pages(struct page **pages, size_t size)
5987{
5988 int i;
5989
5990 if (!pages)
5991 return;
5992
5993 for (i = 0; i < size; i++) {
5994 if (!pages[i])
5995 break;
5996 __free_page(pages[i]);
5997 }
5998 kfree(pages);
5999}
6000
6001static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6002{
6003 struct page **pages;
6004 int i;
6005
6006 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6007 if (!pages) {
6008 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6009 return NULL;
6010 }
6011
6012 for (i = 0; i < size; i++) {
6013 pages[i] = alloc_page(gfp_flags);
6014 if (!pages[i]) {
6015 dprintk("%s: failed to allocate page\n", __func__);
6016 nfs4_free_pages(pages, size);
6017 return NULL;
6018 }
6019 }
6020
6021 return pages;
6022}
6023
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006024static void nfs4_layoutget_release(void *calldata)
6025{
6026 struct nfs4_layoutget *lgp = calldata;
Idan Kedarc2bf3222012-08-02 11:47:10 +03006027 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6028 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006029
6030 dprintk("--> %s\n", __func__);
Idan Kedarc2bf3222012-08-02 11:47:10 +03006031 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006032 put_nfs_open_context(lgp->args.ctx);
6033 kfree(calldata);
6034 dprintk("<-- %s\n", __func__);
6035}
6036
6037static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6038 .rpc_call_prepare = nfs4_layoutget_prepare,
6039 .rpc_call_done = nfs4_layoutget_done,
6040 .rpc_release = nfs4_layoutget_release,
6041};
6042
Idan Kedarc2bf3222012-08-02 11:47:10 +03006043int nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006044{
6045 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedarc2bf3222012-08-02 11:47:10 +03006046 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006047 struct rpc_task *task;
6048 struct rpc_message msg = {
6049 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6050 .rpc_argp = &lgp->args,
6051 .rpc_resp = &lgp->res,
6052 };
6053 struct rpc_task_setup task_setup_data = {
6054 .rpc_client = server->client,
6055 .rpc_message = &msg,
6056 .callback_ops = &nfs4_layoutget_call_ops,
6057 .callback_data = lgp,
6058 .flags = RPC_TASK_ASYNC,
6059 };
6060 int status = 0;
6061
6062 dprintk("--> %s\n", __func__);
6063
Idan Kedarc2bf3222012-08-02 11:47:10 +03006064 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6065 if (!lgp->args.layout.pages) {
6066 nfs4_layoutget_release(lgp);
6067 return -ENOMEM;
6068 }
6069 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6070
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006071 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006072 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006073 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006074 task = rpc_run_task(&task_setup_data);
6075 if (IS_ERR(task))
6076 return PTR_ERR(task);
6077 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006078 if (status == 0)
6079 status = task->tk_status;
6080 if (status == 0)
6081 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006082 rpc_put_task(task);
6083 dprintk("<-- %s status=%d\n", __func__, status);
6084 return status;
6085}
6086
Benny Halevycbe82602011-05-22 19:52:37 +03006087static void
6088nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6089{
6090 struct nfs4_layoutreturn *lrp = calldata;
6091
6092 dprintk("--> %s\n", __func__);
6093 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006094 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006095 return;
6096 rpc_call_start(task);
6097}
6098
6099static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6100{
6101 struct nfs4_layoutreturn *lrp = calldata;
6102 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006103 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006104
6105 dprintk("--> %s\n", __func__);
6106
6107 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6108 return;
6109
6110 server = NFS_SERVER(lrp->args.inode);
6111 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006112 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006113 return;
6114 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006115 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebustf401b102012-08-08 16:03:13 -04006116 if (task->tk_status == 0 && lrp->res.lrs_present)
6117 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006118 lo->plh_block_lgets--;
6119 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006120 dprintk("<-- %s\n", __func__);
6121}
6122
6123static void nfs4_layoutreturn_release(void *calldata)
6124{
6125 struct nfs4_layoutreturn *lrp = calldata;
6126
6127 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006128 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006129 kfree(calldata);
6130 dprintk("<-- %s\n", __func__);
6131}
6132
6133static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6134 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6135 .rpc_call_done = nfs4_layoutreturn_done,
6136 .rpc_release = nfs4_layoutreturn_release,
6137};
6138
6139int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6140{
6141 struct rpc_task *task;
6142 struct rpc_message msg = {
6143 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6144 .rpc_argp = &lrp->args,
6145 .rpc_resp = &lrp->res,
6146 };
6147 struct rpc_task_setup task_setup_data = {
6148 .rpc_client = lrp->clp->cl_rpcclient,
6149 .rpc_message = &msg,
6150 .callback_ops = &nfs4_layoutreturn_call_ops,
6151 .callback_data = lrp,
6152 };
6153 int status;
6154
6155 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006156 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006157 task = rpc_run_task(&task_setup_data);
6158 if (IS_ERR(task))
6159 return PTR_ERR(task);
6160 status = task->tk_status;
6161 dprintk("<-- %s status=%d\n", __func__, status);
6162 rpc_put_task(task);
6163 return status;
6164}
6165
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006166/*
6167 * Retrieve the list of Data Server devices from the MDS.
6168 */
6169static int _nfs4_getdevicelist(struct nfs_server *server,
6170 const struct nfs_fh *fh,
6171 struct pnfs_devicelist *devlist)
6172{
6173 struct nfs4_getdevicelist_args args = {
6174 .fh = fh,
6175 .layoutclass = server->pnfs_curr_ld->id,
6176 };
6177 struct nfs4_getdevicelist_res res = {
6178 .devlist = devlist,
6179 };
6180 struct rpc_message msg = {
6181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6182 .rpc_argp = &args,
6183 .rpc_resp = &res,
6184 };
6185 int status;
6186
6187 dprintk("--> %s\n", __func__);
6188 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6189 &res.seq_res, 0);
6190 dprintk("<-- %s status=%d\n", __func__, status);
6191 return status;
6192}
6193
6194int nfs4_proc_getdevicelist(struct nfs_server *server,
6195 const struct nfs_fh *fh,
6196 struct pnfs_devicelist *devlist)
6197{
6198 struct nfs4_exception exception = { };
6199 int err;
6200
6201 do {
6202 err = nfs4_handle_exception(server,
6203 _nfs4_getdevicelist(server, fh, devlist),
6204 &exception);
6205 } while (exception.retry);
6206
6207 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6208 err, devlist->num_devs);
6209
6210 return err;
6211}
6212EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6213
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006214static int
6215_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6216{
6217 struct nfs4_getdeviceinfo_args args = {
6218 .pdev = pdev,
6219 };
6220 struct nfs4_getdeviceinfo_res res = {
6221 .pdev = pdev,
6222 };
6223 struct rpc_message msg = {
6224 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6225 .rpc_argp = &args,
6226 .rpc_resp = &res,
6227 };
6228 int status;
6229
6230 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006231 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006232 dprintk("<-- %s status=%d\n", __func__, status);
6233
6234 return status;
6235}
6236
6237int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6238{
6239 struct nfs4_exception exception = { };
6240 int err;
6241
6242 do {
6243 err = nfs4_handle_exception(server,
6244 _nfs4_proc_getdeviceinfo(server, pdev),
6245 &exception);
6246 } while (exception.retry);
6247 return err;
6248}
6249EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6250
Andy Adamson863a3c62011-03-23 13:27:54 +00006251static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6252{
6253 struct nfs4_layoutcommit_data *data = calldata;
6254 struct nfs_server *server = NFS_SERVER(data->args.inode);
6255
6256 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006257 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006258 return;
6259 rpc_call_start(task);
6260}
6261
6262static void
6263nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6264{
6265 struct nfs4_layoutcommit_data *data = calldata;
6266 struct nfs_server *server = NFS_SERVER(data->args.inode);
6267
6268 if (!nfs4_sequence_done(task, &data->res.seq_res))
6269 return;
6270
6271 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006272 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6273 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6274 case -NFS4ERR_BADLAYOUT: /* no layout */
6275 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006276 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006277 break;
6278 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006279 nfs_post_op_update_inode_force_wcc(data->args.inode,
6280 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006281 break;
6282 default:
6283 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6284 rpc_restart_call_prepare(task);
6285 return;
6286 }
6287 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006288}
6289
6290static void nfs4_layoutcommit_release(void *calldata)
6291{
6292 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006293 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006294 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006295
Andy Adamsondb29c082011-07-30 20:52:38 -04006296 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006297 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006298 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6299 list_del_init(&lseg->pls_lc_list);
6300 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6301 &lseg->pls_flags))
6302 put_lseg(lseg);
6303 }
Peng Tao92407e72011-10-23 20:21:17 -07006304
6305 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6306 smp_mb__after_clear_bit();
6307 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6308
Andy Adamson863a3c62011-03-23 13:27:54 +00006309 put_rpccred(data->cred);
6310 kfree(data);
6311}
6312
6313static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6314 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6315 .rpc_call_done = nfs4_layoutcommit_done,
6316 .rpc_release = nfs4_layoutcommit_release,
6317};
6318
6319int
Andy Adamsonef311532011-03-12 02:58:10 -05006320nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006321{
6322 struct rpc_message msg = {
6323 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6324 .rpc_argp = &data->args,
6325 .rpc_resp = &data->res,
6326 .rpc_cred = data->cred,
6327 };
6328 struct rpc_task_setup task_setup_data = {
6329 .task = &data->task,
6330 .rpc_client = NFS_CLIENT(data->args.inode),
6331 .rpc_message = &msg,
6332 .callback_ops = &nfs4_layoutcommit_ops,
6333 .callback_data = data,
6334 .flags = RPC_TASK_ASYNC,
6335 };
6336 struct rpc_task *task;
6337 int status = 0;
6338
6339 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6340 "lbw: %llu inode %lu\n",
6341 data->task.tk_pid, sync,
6342 data->args.lastbytewritten,
6343 data->args.inode->i_ino);
6344
Trond Myklebust9d12b212012-01-17 22:04:25 -05006345 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006346 task = rpc_run_task(&task_setup_data);
6347 if (IS_ERR(task))
6348 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006349 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006350 goto out;
6351 status = nfs4_wait_for_completion_rpc_task(task);
6352 if (status != 0)
6353 goto out;
6354 status = task->tk_status;
6355out:
6356 dprintk("%s: status %d\n", __func__, status);
6357 rpc_put_task(task);
6358 return status;
6359}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006360
6361static int
6362_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6363 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6364{
6365 struct nfs41_secinfo_no_name_args args = {
6366 .style = SECINFO_STYLE_CURRENT_FH,
6367 };
6368 struct nfs4_secinfo_res res = {
6369 .flavors = flavors,
6370 };
6371 struct rpc_message msg = {
6372 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6373 .rpc_argp = &args,
6374 .rpc_resp = &res,
6375 };
6376 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6377}
6378
6379static int
6380nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6381 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6382{
6383 struct nfs4_exception exception = { };
6384 int err;
6385 do {
6386 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6387 switch (err) {
6388 case 0:
6389 case -NFS4ERR_WRONGSEC:
6390 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006391 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006392 default:
6393 err = nfs4_handle_exception(server, err, &exception);
6394 }
6395 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006396out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006397 return err;
6398}
6399
6400static int
6401nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6402 struct nfs_fsinfo *info)
6403{
6404 int err;
6405 struct page *page;
6406 rpc_authflavor_t flavor;
6407 struct nfs4_secinfo_flavors *flavors;
6408
6409 page = alloc_page(GFP_KERNEL);
6410 if (!page) {
6411 err = -ENOMEM;
6412 goto out;
6413 }
6414
6415 flavors = page_address(page);
6416 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6417
6418 /*
6419 * Fall back on "guess and check" method if
6420 * the server doesn't support SECINFO_NO_NAME
6421 */
6422 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6423 err = nfs4_find_root_sec(server, fhandle, info);
6424 goto out_freepage;
6425 }
6426 if (err)
6427 goto out_freepage;
6428
6429 flavor = nfs_find_best_sec(flavors);
6430 if (err == 0)
6431 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6432
6433out_freepage:
6434 put_page(page);
6435 if (err == -EACCES)
6436 return -EPERM;
6437out:
6438 return err;
6439}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006440
6441static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006442{
6443 int status;
6444 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006445 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006446 };
6447 struct nfs41_test_stateid_res res;
6448 struct rpc_message msg = {
6449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6450 .rpc_argp = &args,
6451 .rpc_resp = &res,
6452 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006453
Trond Myklebust9d12b212012-01-17 22:04:25 -05006454 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006455 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6456
6457 if (status == NFS_OK)
6458 return res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006459 return status;
6460}
6461
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006462static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006463{
6464 struct nfs4_exception exception = { };
6465 int err;
6466 do {
6467 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006468 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006469 &exception);
6470 } while (exception.retry);
6471 return err;
6472}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006473
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006474static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006475{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006476 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006477 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006478 };
6479 struct nfs41_free_stateid_res res;
6480 struct rpc_message msg = {
6481 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6482 .rpc_argp = &args,
6483 .rpc_resp = &res,
6484 };
6485
Trond Myklebust9d12b212012-01-17 22:04:25 -05006486 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6487 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006488}
6489
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006490static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006491{
6492 struct nfs4_exception exception = { };
6493 int err;
6494 do {
6495 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006496 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006497 &exception);
6498 } while (exception.retry);
6499 return err;
6500}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006501
6502static bool nfs41_match_stateid(const nfs4_stateid *s1,
6503 const nfs4_stateid *s2)
6504{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006505 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006506 return false;
6507
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006508 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006509 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006510 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006511 return true;
6512
6513 return false;
6514}
6515
Andy Adamson557134a2009-04-01 09:21:53 -04006516#endif /* CONFIG_NFS_V4_1 */
6517
Trond Myklebust36281ca2012-03-04 18:13:56 -05006518static bool nfs4_match_stateid(const nfs4_stateid *s1,
6519 const nfs4_stateid *s2)
6520{
Trond Myklebustf597c532012-03-04 18:13:56 -05006521 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006522}
6523
6524
Trond Myklebust17280172012-03-11 13:11:00 -04006525static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006526 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006527 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528 .recover_open = nfs4_open_reclaim,
6529 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006530 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006531 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532};
6533
Andy Adamson591d71c2009-04-01 09:22:47 -04006534#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006535static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006536 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6537 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6538 .recover_open = nfs4_open_reclaim,
6539 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006540 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006541 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006542 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006543};
6544#endif /* CONFIG_NFS_V4_1 */
6545
Trond Myklebust17280172012-03-11 13:11:00 -04006546static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006547 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006548 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549 .recover_open = nfs4_open_expired,
6550 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006551 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006552 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553};
6554
Andy Adamson591d71c2009-04-01 09:22:47 -04006555#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006556static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006557 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6558 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006559 .recover_open = nfs41_open_expired,
6560 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006561 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006562 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006563};
6564#endif /* CONFIG_NFS_V4_1 */
6565
Trond Myklebust17280172012-03-11 13:11:00 -04006566static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006567 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006568 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006569 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006570};
6571
6572#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006573static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006574 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006575 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006576 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006577};
6578#endif
6579
Trond Myklebust97dc1352010-06-16 09:52:26 -04006580static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6581 .minor_version = 0,
6582 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006583 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006584 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006585 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6586 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6587 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006588};
6589
6590#if defined(CONFIG_NFS_V4_1)
6591static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6592 .minor_version = 1,
6593 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006594 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006595 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006596 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6597 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6598 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006599};
6600#endif
6601
Trond Myklebust97dc1352010-06-16 09:52:26 -04006602const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6603 [0] = &nfs_v4_0_minor_ops,
6604#if defined(CONFIG_NFS_V4_1)
6605 [1] = &nfs_v4_1_minor_ops,
6606#endif
6607};
6608
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006609static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006610 .permission = nfs_permission,
6611 .getattr = nfs_getattr,
6612 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006613 .getxattr = generic_getxattr,
6614 .setxattr = generic_setxattr,
6615 .listxattr = generic_listxattr,
6616 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006617};
6618
David Howells509de812006-08-22 20:06:11 -04006619const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620 .version = 4, /* protocol version */
6621 .dentry_ops = &nfs4_dentry_operations,
6622 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006623 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006624 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625 .getroot = nfs4_proc_get_root,
6626 .getattr = nfs4_proc_getattr,
6627 .setattr = nfs4_proc_setattr,
6628 .lookup = nfs4_proc_lookup,
6629 .access = nfs4_proc_access,
6630 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631 .create = nfs4_proc_create,
6632 .remove = nfs4_proc_remove,
6633 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006634 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635 .unlink_done = nfs4_proc_unlink_done,
6636 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006637 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006638 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006639 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640 .link = nfs4_proc_link,
6641 .symlink = nfs4_proc_symlink,
6642 .mkdir = nfs4_proc_mkdir,
6643 .rmdir = nfs4_proc_remove,
6644 .readdir = nfs4_proc_readdir,
6645 .mknod = nfs4_proc_mknod,
6646 .statfs = nfs4_proc_statfs,
6647 .fsinfo = nfs4_proc_fsinfo,
6648 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006649 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650 .decode_dirent = nfs4_decode_dirent,
6651 .read_setup = nfs4_proc_read_setup,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006652 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006653 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 .write_setup = nfs4_proc_write_setup,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006655 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006656 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006658 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006660 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006661 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006662 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006663 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006664 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665};
6666
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006667static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6668 .prefix = XATTR_NAME_NFSV4_ACL,
6669 .list = nfs4_xattr_list_nfs4_acl,
6670 .get = nfs4_xattr_get_nfs4_acl,
6671 .set = nfs4_xattr_set_nfs4_acl,
6672};
6673
6674const struct xattr_handler *nfs4_xattr_handlers[] = {
6675 &nfs4_xattr_nfs4_acl_handler,
6676 NULL
6677};
6678
Trond Myklebustef159e92012-02-06 19:50:40 -05006679module_param(max_session_slots, ushort, 0644);
6680MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6681 "requests the client will negotiate");
6682
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683/*
6684 * Local variables:
6685 * c-basic-offset: 8
6686 * End:
6687 */